From 8b5dca7f33d1e5823c3040acba7d5b6aa57cda2d Mon Sep 17 00:00:00 2001 From: jorgeantonio21 Date: Wed, 16 Nov 2022 11:15:22 +0000 Subject: [PATCH] address PR changes --- Cargo.lock | 2 +- base_layer/key_manager/Cargo.toml | 2 +- base_layer/key_manager/src/cipher_seed.rs | 8 ++-- base_layer/key_manager/src/lib.rs | 11 +++-- base_layer/key_manager/src/mnemonic.rs | 56 +++++++++++------------ 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec8393e6ca..62df932352 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4874,7 +4874,7 @@ dependencies = [ "subtle", "tari_common_types", "tari_crypto", - "tari_utilities 0.4.7", + "tari_utilities 0.4.8", "thiserror", "wasm-bindgen", "wasm-bindgen-test", diff --git a/base_layer/key_manager/Cargo.toml b/base_layer/key_manager/Cargo.toml index 2eaf280371..46b36bf924 100644 --- a/base_layer/key_manager/Cargo.toml +++ b/base_layer/key_manager/Cargo.toml @@ -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" diff --git a/base_layer/key_manager/src/cipher_seed.rs b/base_layer/key_manager/src/cipher_seed.rs index 35eba4efd8..efe0a353c4 100644 --- a/base_layer/key_manager/src/cipher_seed.rs +++ b/base_layer/key_manager/src/cipher_seed.rs @@ -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::{ @@ -594,9 +594,9 @@ mod test { "cover", "vote", "federal", "husband", "cave", "alone", "dynamic", "reopen", "visa", "young", "gas", ] .iter() - .map(|x| x.to_string()) - .collect::>(); - let mnemonic_seq = SeedWords::new(&mnemonic_seq); + .map(|x| Hidden::hide(x.to_string())) + .collect::>>(); + let mnemonic_seq = SeedWords::new(mnemonic_seq); // Language not known match CipherSeed::from_mnemonic(&mnemonic_seq, None) { Ok(_k) => panic!(), diff --git a/base_layer/key_manager/src/lib.rs b/base_layer/key_manager/src/lib.rs index 51068e5aad..8cca15145b 100644 --- a/base_layer/key_manager/src/lib.rs +++ b/base_layer/key_manager/src/lib.rs @@ -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}, @@ -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::>(), - } + pub fn new(words: Vec>) -> Self { + Self { words } } pub fn len(&self) -> usize { @@ -74,4 +71,8 @@ impl SeedWords { Ok(self.words[index].reveal()) } + + pub fn is_empty(&self) -> bool { + self.words.is_empty() + } } diff --git a/base_layer/key_manager/src/mnemonic.rs b/base_layer/key_manager/src/mnemonic.rs index 2ac02eba7f..bcd4fe8455 100644 --- a/base_layer/key_manager/src/mnemonic.rs +++ b/base_layer/key_manager/src/mnemonic.rs @@ -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 { - let words = SeedWords::new(&[mnemonic_word.to_string()]); + let words = SeedWords::new(vec![Hidden::hide(mnemonic_word.to_string())]); MnemonicLanguage::detect_language(&words) } @@ -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 { @@ -199,17 +199,17 @@ pub fn from_bytes(bytes: &[u8], language: MnemonicLanguage) -> Result = Vec::new(); + let mut mnemonic_sequence: Vec> = 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 @@ -355,11 +355,11 @@ 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), @@ -367,40 +367,40 @@ mod test { ); // 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) ); }