Skip to content

Commit

Permalink
address PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeantonio21 committed Nov 16, 2022
1 parent 9b22bc6 commit 8b5dca7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion base_layer/key_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["lib", "cdylib"]
[dependencies]
tari_common_types = { version = "^0.39", path = "../../base_layer/common_types", optional = true }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.0" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.7" }
tari_utilities = { git = "https://github.com/AaronFeickert/tari_utilities.git", branch="clandestine" }

argon2 = { version = "0.4.1", features = ["std", "alloc"] }
blake2 = "0.9.1"
Expand Down
8 changes: 4 additions & 4 deletions base_layer/key_manager/src/cipher_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ mod test {
use std::str::FromStr;

use crc32fast::Hasher as CrcHasher;
use tari_utilities::SafePassword;
use tari_utilities::{Hidden, SafePassword};

use super::BIRTHDAY_GENESIS_FROM_UNIX_EPOCH;
use crate::{
Expand Down Expand Up @@ -594,9 +594,9 @@ mod test {
"cover", "vote", "federal", "husband", "cave", "alone", "dynamic", "reopen", "visa", "young", "gas",
]
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
let mnemonic_seq = SeedWords::new(&mnemonic_seq);
.map(|x| Hidden::hide(x.to_string()))
.collect::<Vec<Hidden<String>>>();
let mnemonic_seq = SeedWords::new(mnemonic_seq);
// Language not known
match CipherSeed::from_mnemonic(&mnemonic_seq, None) {
Ok(_k) => panic!(),
Expand Down
11 changes: 6 additions & 5 deletions base_layer/key_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use cipher_seed::BIRTHDAY_GENESIS_FROM_UNIX_EPOCH;
use digest::Digest;
use serde::{Deserialize, Serialize};
use tari_crypto::{
hash_domain,
hashing::{DomainSeparatedHasher, LengthExtensionAttackResistant},
Expand Down Expand Up @@ -57,10 +56,8 @@ pub struct SeedWords {
}

impl SeedWords {
pub fn new(words: &[String]) -> Self {
Self {
words: words.into_iter().map(|m| Hidden::hide(m.clone())).collect::<Vec<_>>(),
}
pub fn new(words: Vec<Hidden<String>>) -> Self {
Self { words }
}

pub fn len(&self) -> usize {
Expand All @@ -74,4 +71,8 @@ impl SeedWords {

Ok(self.words[index].reveal())
}

pub fn is_empty(&self) -> bool {
self.words.is_empty()
}
}
56 changes: 28 additions & 28 deletions base_layer/key_manager/src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub enum MnemonicLanguage {
impl MnemonicLanguage {
/// Detects the mnemonic language of a specific word by searching all defined mnemonic word lists
pub fn from(mnemonic_word: &str) -> Result<MnemonicLanguage, MnemonicError> {
let words = SeedWords::new(&[mnemonic_word.to_string()]);
let words = SeedWords::new(vec![Hidden::hide(mnemonic_word.to_string())]);
MnemonicLanguage::detect_language(&words)
}

Expand Down Expand Up @@ -157,7 +157,7 @@ fn find_mnemonic_index_from_word(word: &str, language: MnemonicLanguage) -> Resu
MnemonicLanguage::Japanese => MNEMONIC_JAPANESE_WORDS.binary_search(&lowercase_word.reveal().as_str()),
MnemonicLanguage::Korean => MNEMONIC_KOREAN_WORDS.binary_search(&lowercase_word.reveal().as_str()),
MnemonicLanguage::Spanish => {
MNEMONIC_SPANISH_WORDS.binary_search(&remove_diacritics(&lowercase_word.reveal()).reveal().as_str())
MNEMONIC_SPANISH_WORDS.binary_search(&remove_diacritics(lowercase_word.reveal()).reveal().as_str())
},
};
match search_result {
Expand Down Expand Up @@ -199,17 +199,17 @@ pub fn from_bytes(bytes: &[u8], language: MnemonicLanguage) -> Result<SeedWords,
bits.resize(padded_size, false);

// Group each set of 11 bits to form one mnemonic word
let mut mnemonic_sequence: Vec<String> = Vec::new();
let mut mnemonic_sequence: Vec<Hidden<String>> = Vec::new();
for i in 0..bits.len() / group_bit_count {
let start_index = i * group_bit_count;
let stop_index = start_index + group_bit_count;
let sub_v = &bits[start_index..stop_index];
let word_index = checked_bits_to_uint(sub_v).ok_or(MnemonicError::BitsToIntConversion)?;
let mnemonic_word = find_mnemonic_word_from_index(word_index, language)?;
let mnemonic_word = Hidden::hide(find_mnemonic_word_from_index(word_index, language)?);
mnemonic_sequence.push(mnemonic_word);
}

Ok(SeedWords::new(mnemonic_sequence.as_slice()))
Ok(SeedWords::new(mnemonic_sequence))
}

/// Generates a vector of bytes that represent the provided mnemonic sequence of words, the language of the mnemonic
Expand Down Expand Up @@ -355,52 +355,52 @@ mod test {
assert!(MnemonicLanguage::from("desvelado").is_err()); // Invalid Mnemonic Spanish word

// English/Spanish + English/French -> English
let words1 = SeedWords::new(&[
"album".to_string(),
"area".to_string(),
"opera".to_string(),
"abandon".to_string(),
let words1 = SeedWords::new(vec![
Hidden::hide("album".to_string()),
Hidden::hide("area".to_string()),
Hidden::hide("opera".to_string()),
Hidden::hide("abandon".to_string()),
]);
assert_eq!(
MnemonicLanguage::detect_language(&words1),
Ok(MnemonicLanguage::English)
);

// English/Spanish + English/French + Italian/Spanish
let words2 = SeedWords::new(&[
"album".to_string(),
"area".to_string(),
"opera".to_string(),
"abandon".to_string(),
"tipico".to_string(),
let words2 = SeedWords::new(vec![
Hidden::hide("album".to_string()),
Hidden::hide("area".to_string()),
Hidden::hide("opera".to_string()),
Hidden::hide("abandon".to_string()),
Hidden::hide("tipico".to_string()),
]);
assert!(MnemonicLanguage::detect_language(&words2).is_err());

// bounds check (last word is invalid)
let words3 = SeedWords::new(&[
"album".to_string(),
"area".to_string(),
"opera".to_string(),
"abandon".to_string(),
"topazio".to_string(),
let words3 = SeedWords::new(vec![
Hidden::hide("album".to_string()),
Hidden::hide("area".to_string()),
Hidden::hide("opera".to_string()),
Hidden::hide("abandon".to_string()),
Hidden::hide("topazio".to_string()),
]);
assert!(MnemonicLanguage::detect_language(&words3).is_err());

// building up a word list: English/French + French -> French
let mut words = Vec::with_capacity(3);
words.push("concert".to_string());
words.push(Hidden::hide("concert".to_string()));
assert_eq!(
MnemonicLanguage::detect_language(&SeedWords::new(&words)),
MnemonicLanguage::detect_language(&SeedWords::new(words.clone())),
Ok(MnemonicLanguage::English)
);
words.push("abandon".to_string());
words.push(Hidden::hide("abandon".to_string()));
assert_eq!(
MnemonicLanguage::detect_language(&SeedWords::new(&words)),
MnemonicLanguage::detect_language(&SeedWords::new(words.clone())),
Ok(MnemonicLanguage::English)
);
words.push("barbier".to_string());
words.push(Hidden::hide("barbier".to_string()));
assert_eq!(
MnemonicLanguage::detect_language(&SeedWords::new(&words)),
MnemonicLanguage::detect_language(&SeedWords::new(words)),
Ok(MnemonicLanguage::French)
);
}
Expand Down

0 comments on commit 8b5dca7

Please sign in to comment.