Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
WIP display/debug and Wallet Account tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Jan 19, 2024
1 parent f6253ed commit 0579e3f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 27 deletions.
31 changes: 20 additions & 11 deletions profile/src/hierarchical_deterministic/bip39/bip39_passphrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ use crate::prelude::*;
)]
#[serde(transparent)]
#[display("<OBFUSCATED>")]
#[debug("{:?}", self.non_sensitive())]
#[debug("{:?}", self.partially_obfuscated_string())]
pub struct BIP39Passphrase(pub String);

impl SafeToLog for BIP39Passphrase {
/// Logs the word count and FactorSourceID o
fn non_sensitive(&self) -> impl std::fmt::Debug {
impl BIP39Passphrase {
pub fn partially_obfuscated_string(&self) -> String {
if self.0.is_empty() {
"<EMPTY>"
} else {
"<NOT EMPTY>"
}
.to_string()
}
}
impl SafeToLog for BIP39Passphrase {
/// Logs the word count and FactorSourceID o
fn non_sensitive(&self) -> impl std::fmt::Debug {
self.partially_obfuscated_string()
}
}

Expand Down Expand Up @@ -71,20 +77,23 @@ mod tests {
}

#[test]
fn display() {
fn debug() {
assert_eq!(
format!("{}", BIP39Passphrase::new("so secret")),
"<NOT EMPTY>"
format!("{:?}", BIP39Passphrase::new("so secret")),
format!("{:?}", "<NOT EMPTY>")
);
assert_eq!(
format!("{:?}", BIP39Passphrase::default()),
format!("{:?}", "<EMPTY>")
);
assert_eq!(format!("{}", BIP39Passphrase::default()), "<EMPTY>");
}

#[test]
fn debug() {
fn display() {
assert_eq!(
format!("{:?}", BIP39Passphrase::new("so secret")),
format!("{}", BIP39Passphrase::new("so secret")),
"<OBFUSCATED>"
);
assert_eq!(format!("{:?}", BIP39Passphrase::default()), "<OBFUSCATED>");
assert_eq!(format!("{}", BIP39Passphrase::default()), "<OBFUSCATED>");
}
}
29 changes: 22 additions & 7 deletions profile/src/hierarchical_deterministic/bip39/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@ use crate::prelude::*;
PartialEq,
Eq,
Hash,
SerializeDisplay,
DeserializeFromStr,
derive_more::Display,
derive_more::Debug,
uniffi::Record,
)]
#[display("OBFUSCATED")]
#[debug("{:?}", self.non_sensitive())]
#[display("{}", self.to_obfuscated_string())]
#[debug("{:?}", self.partially_obfuscated_string())]
pub struct Mnemonic {
pub words: Vec<BIP39Word>,
pub word_count: BIP39WordCount,
pub language: BIP39Language,
}

impl SafeToLog for Mnemonic {
/// Logs the word count and FactorSourceID o
fn non_sensitive(&self) -> impl std::fmt::Debug {
impl Serialize for Mnemonic {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.phrase().serialize(serializer)
}
}
impl Mnemonic {
pub fn partially_obfuscated_string(&self) -> String {
format!(
"{} ({}...{})",
self.word_count,
Expand All @@ -30,6 +36,12 @@ impl SafeToLog for Mnemonic {
)
}
}
impl SafeToLog for Mnemonic {
/// Logs the word count and FactorSourceID o
fn non_sensitive(&self) -> impl std::fmt::Debug {
self.partially_obfuscated_string()
}
}

/// Returns the words of a mnemonic as a String joined by spaces, e.g. "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong"
#[uniffi::export]
Expand Down Expand Up @@ -129,7 +141,10 @@ mod tests {
#[test]
fn debug() {
let mnemonic = Mnemonic::placeholder();
assert_eq!(format!("{:?}", mnemonic), "24 words (bright...mandate)")
assert_eq!(
format!("{:?}", mnemonic),
format!("{:?}", "24 words (bright...mandate)")
);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ use crate::prelude::*;
)]
#[serde(rename_all = "camelCase")]
#[display("<OBFUSCATED>")]
#[debug("{:?}", self.non_sensitive())]
#[debug("{:?}", self.partially_obfuscated_string())]
pub struct MnemonicWithPassphrase {
pub mnemonic: Mnemonic,
pub passphrase: BIP39Passphrase,
}

impl MnemonicWithPassphrase {
pub fn partially_obfuscated_string(&self) -> String {
format!(
"{} + {}",
self.mnemonic.partially_obfuscated_string(),
self.passphrase.partially_obfuscated_string()
)
}
}
impl SafeToLog for MnemonicWithPassphrase {
/// Logs the word count and FactorSourceID o
fn non_sensitive(&self) -> impl std::fmt::Debug {
format!(
"{:?} + {:?}",
self.mnemonic.non_sensitive(),
self.passphrase.non_sensitive()
)
self.partially_obfuscated_string()
}
}

Expand Down Expand Up @@ -152,11 +157,24 @@ mod tests {
MnemonicWithPassphrase::placeholder_other()
);
}

#[test]
fn display() {
assert_eq!(
format!("{}", MnemonicWithPassphrase::placeholder()),
"<NOT EMPTY>"
"<OBFUSCATED>"
);
}

#[test]
fn debug() {
assert_eq!(
format!("{:?}", MnemonicWithPassphrase::placeholder()),
format!("{:?}", "24 words (bright...mandate) + <NOT EMPTY>")
);
assert_eq!(
format!("{:?}", MnemonicWithPassphrase::placeholder_other()),
format!("{:?}", "12 words (zoo...wrong) + <EMPTY>")
);
}

Expand Down
8 changes: 7 additions & 1 deletion profile/src/wallet/secure_storage/wallet_client_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ impl WalletClientStorage {
self.interface.load_data(key).and_then(|o| match o {
None => Ok(None),
Some(j) => serde_json::from_slice(j.as_slice()).map_err(|_| {
let type_name = std::any::type_name::<T>().to_string();
error!(
"Failed to deserialize json to type: {}\nJSON (utf8):\n{:?}",
&type_name,
String::from_utf8(j.clone())
);
CommonError::FailedToDeserializeJSONToValue {
json_byte_count: j.len(),
type_name: std::any::type_name::<T>().to_string(),
type_name,
}
}),
})
Expand Down
9 changes: 8 additions & 1 deletion profile/src/wallet/wallet_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,14 @@ mod tests {
fn load_private_device_factor_source_by_id() {
let profile = Profile::placeholder();
let private = PrivateHierarchicalDeterministicFactorSource::placeholder();
let (wallet, _) = Wallet::ephemeral(profile.clone());
let (wallet, storage) = Wallet::ephemeral(profile.clone());

let data = serde_json::to_vec(&private.mnemonic_with_passphrase).unwrap();
let key = SecureStorageKey::DeviceFactorSourceMnemonic {
factor_source_id: private.clone().factor_source.id.clone(),
};
assert!(storage.save_data(key.clone(), data.clone()).is_ok());

assert_eq!(
wallet.load_private_device_factor_source_by_id(&private.factor_source.id.clone()),
Ok(private.clone())
Expand Down
1 change: 1 addition & 0 deletions profile/src/wallet_kit_common/types/logged_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ impl<T: SafeToLog> LoggedResult<T> for Result<T> {
F: FnOnce(&T) -> String,
{
self.inspect(|x| log::log!(level, "{}", format(&x)))
.inspect_err(|e| error!("Err: {}", e))
}
}

0 comments on commit 0579e3f

Please sign in to comment.