From 7e91621a2893e2b10b34772a4abd954fb41626ff Mon Sep 17 00:00:00 2001 From: david barinas Date: Tue, 30 Apr 2024 17:40:55 -0500 Subject: [PATCH] style: Fix formatting and code organization in key_pair.rs and vault.rs files --- libwallet/src/key_pair.rs | 5 +++-- libwallet/src/vault.rs | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libwallet/src/key_pair.rs b/libwallet/src/key_pair.rs index 080b264..3d13d5d 100644 --- a/libwallet/src/key_pair.rs +++ b/libwallet/src/key_pair.rs @@ -1,4 +1,5 @@ -use core::{convert::TryInto, fmt::Debug}; +use core::{convert::{TryFrom, TryInto}, fmt::Debug}; + pub use derive::Derive; use self::any::AnySignature; @@ -21,7 +22,7 @@ impl Public for Bytes {} pub trait Signature: AsRef<[u8]> + Debug + PartialEq { fn as_bytes(&self) -> Bytes { - self.as_ref().try_into().expect("error") + self.as_ref().try_into().expect("error getting the bytes") } } impl Signature for Bytes {} diff --git a/libwallet/src/vault.rs b/libwallet/src/vault.rs index e320904..f0c7a73 100644 --- a/libwallet/src/vault.rs +++ b/libwallet/src/vault.rs @@ -31,8 +31,7 @@ mod utils { const MAX_PATH_LEN: usize = 16; use arrayvec::ArrayString; - use crate::{account::Account, any::AnySignature, any, Derive, Network, Pair, Public}; - + use crate::{account::Account, any, any::AnySignature, Derive, Network, Pair, Public}; /// The root account is a container of the key pairs stored in the vault and cannot be /// used to sign messages directly, we always derive new key pairs from it to create @@ -82,7 +81,7 @@ mod utils { path: ArrayString, name: ArrayString<{ MAX_PATH_LEN - 2 }>, } - + impl Account for AccountSigner { fn public(&self) -> impl Public { self.pair.as_ref().expect("account unlocked").public() @@ -91,13 +90,22 @@ mod utils { impl AccountSigner { pub(crate) fn new<'a>(name: impl Into>) -> Self { - let n = name.into().unwrap_or_else(|| "default"); - let mut path = ArrayString::from("//").unwrap(); - path.push_str(&n); + let n = name.into().unwrap_or_else(|| ""); + let mut path = ArrayString::new(); + + if n == "default" { + path.push_str(n); + } + + if n != "default" && !n.is_empty() { + path.push_str("//"); + path.push_str(n); + } + AccountSigner { pair: None, network: Network::default(), - name: ArrayString::from(&n).expect("short name"), + name: ArrayString::from(n).expect("short name"), path, } }