Skip to content

Commit

Permalink
fixed clippy warnings in production code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar-Pepper committed Sep 30, 2024
1 parent a80dc4e commit eec7ddb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 45 deletions.
2 changes: 1 addition & 1 deletion zingolib/src/blaze/trial_decryptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl TrialDecryptions {
let config = config.clone();

workers.push(tokio::spawn(async move {
let Ok(fvk) = D::unified_key_store_to_fvk(&wc.unified_key_store()) else {
let Ok(fvk) = D::unified_key_store_to_fvk(wc.unified_key_store()) else {
// skip any scanning if the wallet doesn't have viewing capability
return Ok::<_, String>(());
};
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Command for WalletKindCommand {
"orchard" => ufvk.orchard().is_some(),
}
.pretty(4),
UnifiedKeyStore::None => object! {
UnifiedKeyStore::Empty => object! {
"kind" => "No keys found",
"transparent" => false,
"sapling" => false,
Expand Down
16 changes: 5 additions & 11 deletions zingolib/src/wallet/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,13 @@ impl LightWallet {
UnifiedKeyStore::Spend(_) => (),
UnifiedKeyStore::View(ufvk) => match D::SHIELDED_PROTOCOL {
ShieldedProtocol::Sapling => {
if ufvk.sapling().is_none() {
return None;
}
ufvk.sapling()?;
}
ShieldedProtocol::Orchard => {
if ufvk.orchard().is_none() {
return None;
}
ufvk.orchard()?;
}
},
UnifiedKeyStore::None => return None,
UnifiedKeyStore::Empty => return None,
}
Some(
self.transaction_context
Expand Down Expand Up @@ -115,11 +111,9 @@ impl LightWallet {
match self.wallet_capability().unified_key_store() {
UnifiedKeyStore::Spend(_) => (),
UnifiedKeyStore::View(ufvk) => {
if ufvk.transparent().is_none() {
return None;
}
ufvk.transparent()?;
}
UnifiedKeyStore::None => return None,
UnifiedKeyStore::Empty => return None,
}
Some(
self.get_utxos()
Expand Down
20 changes: 8 additions & 12 deletions zingolib/src/wallet/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,18 @@ impl LightWallet {
//
// UnifiedSpendingKey is initially set to None variant except when loading unified full viewing key where the mnemonic is not available.
// This is due to the legacy transparent extended private key (ExtendedPrivKey) not containing all information required for BIP0032.
if matches!(wallet_capability.unified_key_store(), UnifiedKeyStore::None)
&& mnemonic.is_some()
{
wallet_capability.set_unified_key_store(UnifiedKeyStore::Spend(
if let (true, Some(mnemonic)) = (
wallet_capability.unified_key_store().is_empty(),
mnemonic.as_ref(),
) {
wallet_capability.set_unified_key_store(UnifiedKeyStore::Spend(Box::new(
UnifiedSpendingKey::from_seed(
&config.chain,
&mnemonic
.as_ref()
.expect("mnemonic should not be None in this scope")
.0
.clone()
.to_seed(""),
&mnemonic.0.to_seed(""),
AccountId::ZERO,
)
.unwrap(),
));
)));
}

info!("Keys in this wallet:");
Expand All @@ -259,7 +255,7 @@ impl LightWallet {
info!(" - transparent extended public key");
}
}
UnifiedKeyStore::None => info!(" - no keys found"),
UnifiedKeyStore::Empty => info!(" - no keys found"),
}

// this initialization combines two types of data
Expand Down
45 changes: 25 additions & 20 deletions zingolib/src/wallet/keys/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ use super::ToBase58Check;
#[derive(Debug)]
pub enum UnifiedKeyStore {
/// Wallet with spend capability
Spend(UnifiedSpendingKey),
Spend(Box<UnifiedSpendingKey>),
/// Wallet with view capability
View(UnifiedFullViewingKey),
View(Box<UnifiedFullViewingKey>),
/// Wallet with no keys
None,
Empty,
}

impl UnifiedKeyStore {
/// Returns true if [`UnifiedKeyStore`] is of `Spend` variant
pub fn is_spending_key(&self) -> bool {
matches!(self, UnifiedKeyStore::Spend(_))
}

/// Returns true if [`UnifiedKeyStore`] is of `Spend` variant
pub fn is_empty(&self) -> bool {
matches!(self, UnifiedKeyStore::Empty)
}
}

impl ReadableWriteable<ChainType, ChainType> for UnifiedKeyStore {
Expand All @@ -56,9 +61,9 @@ impl ReadableWriteable<ChainType, ChainType> for UnifiedKeyStore {
let _version = Self::get_version(&mut reader)?;
let key_type = reader.read_u8()?;
Ok(match key_type {
0 => UnifiedKeyStore::Spend(UnifiedSpendingKey::read(reader, ())?),
1 => UnifiedKeyStore::View(UnifiedFullViewingKey::read(reader, input)?),
2 => UnifiedKeyStore::None,
0 => UnifiedKeyStore::Spend(Box::new(UnifiedSpendingKey::read(reader, ())?)),
1 => UnifiedKeyStore::View(Box::new(UnifiedFullViewingKey::read(reader, input)?)),
2 => UnifiedKeyStore::Empty,
x => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
Expand All @@ -79,7 +84,7 @@ impl ReadableWriteable<ChainType, ChainType> for UnifiedKeyStore {
writer.write_u8(1)?;
ufvk.write(&mut writer, input)
}
UnifiedKeyStore::None => writer.write_u8(2),
UnifiedKeyStore::Empty => writer.write_u8(2),
}
}
}
Expand Down Expand Up @@ -132,7 +137,7 @@ impl TryFrom<&UnifiedKeyStore> for UnifiedSpendingKey {
type Error = KeyError;
fn try_from(unified_key_store: &UnifiedKeyStore) -> Result<Self, Self::Error> {
match unified_key_store {
UnifiedKeyStore::Spend(usk) => Ok(usk.clone()),
UnifiedKeyStore::Spend(usk) => Ok(*usk.clone()),
_ => Err(KeyError::NoSpendCapability),
}
}
Expand All @@ -141,7 +146,7 @@ impl TryFrom<&UnifiedKeyStore> for orchard::keys::SpendingKey {
type Error = KeyError;
fn try_from(unified_key_store: &UnifiedKeyStore) -> Result<Self, Self::Error> {
let usk = UnifiedSpendingKey::try_from(unified_key_store)?;
Ok(usk.orchard().clone())
Ok(*usk.orchard())
}
}
impl TryFrom<&UnifiedKeyStore> for sapling_crypto::zip32::ExtendedSpendingKey {
Expand All @@ -164,8 +169,8 @@ impl TryFrom<&UnifiedKeyStore> for UnifiedFullViewingKey {
fn try_from(unified_key_store: &UnifiedKeyStore) -> Result<Self, Self::Error> {
match unified_key_store {
UnifiedKeyStore::Spend(usk) => Ok(usk.to_unified_full_viewing_key()),
UnifiedKeyStore::View(ufvk) => Ok(ufvk.clone()),
UnifiedKeyStore::None => Err(KeyError::NoViewCapability),
UnifiedKeyStore::View(ufvk) => Ok(*ufvk.clone()),
UnifiedKeyStore::Empty => Err(KeyError::NoViewCapability),
}
}
}
Expand Down Expand Up @@ -208,7 +213,7 @@ pub struct WalletCapability {
impl Default for WalletCapability {
fn default() -> Self {
Self {
unified_key_store: UnifiedKeyStore::None,
unified_key_store: UnifiedKeyStore::Empty,
transparent_child_addresses: Arc::new(AppendOnlyVec::new()),
addresses: AppendOnlyVec::new(),
addresses_write_lock: AtomicBool::new(false),
Expand Down Expand Up @@ -374,7 +379,7 @@ impl WalletCapability {
)
.derive_external_ivk()
.ok(),
UnifiedKeyStore::None => None,
UnifiedKeyStore::Empty => None,
};
if let Some(pk) = external_pubkey {
let t_addr = pk.derive_address(child_index).unwrap();
Expand Down Expand Up @@ -455,7 +460,7 @@ impl WalletCapability {
.map_err(|_| KeyError::KeyDerivationError)?;

Ok(Self {
unified_key_store: UnifiedKeyStore::Spend(usk),
unified_key_store: UnifiedKeyStore::Spend(Box::new(usk)),
..Default::default()
})
}
Expand All @@ -479,7 +484,7 @@ impl WalletCapability {
.map_err(|_| KeyError::KeyDecodingError)?;

Ok(Self {
unified_key_store: UnifiedKeyStore::Spend(usk),
unified_key_store: UnifiedKeyStore::Spend(Box::new(usk)),
..Default::default()
})
}
Expand All @@ -498,7 +503,7 @@ impl WalletCapability {
let ufvk = UnifiedFullViewingKey::parse(&ufvk).map_err(|_| KeyError::KeyDecodingError)?;

Ok(Self {
unified_key_store: UnifiedKeyStore::View(ufvk),
unified_key_store: UnifiedKeyStore::View(Box::new(ufvk)),
..Default::default()
})
}
Expand Down Expand Up @@ -554,7 +559,7 @@ impl WalletCapability {
sapling: ufvk.sapling().is_some(),
transparent: ufvk.transparent().is_some(),
},
UnifiedKeyStore::None => ReceiverSelection {
UnifiedKeyStore::Empty => ReceiverSelection {
orchard: false,
sapling: false,
transparent: false,
Expand All @@ -577,7 +582,7 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
sapling_crypto::zip32::ExtendedSpendingKey::read(&mut reader)?;
super::legacy::extended_transparent::ExtendedPrivKey::read(&mut reader, ())?;
Self {
unified_key_store: UnifiedKeyStore::None,
unified_key_store: UnifiedKeyStore::Empty,
..Default::default()
}
}
Expand Down Expand Up @@ -623,9 +628,9 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
&input,
)
.unwrap();
UnifiedKeyStore::View(ufvk)
UnifiedKeyStore::View(Box::new(ufvk))
} else {
UnifiedKeyStore::None
UnifiedKeyStore::Empty
};
Self {
unified_key_store,
Expand Down

0 comments on commit eec7ddb

Please sign in to comment.