From 01ea17e48447fd7d4c6e8545581a8d4e9a7b5681 Mon Sep 17 00:00:00 2001 From: dariovp Date: Fri, 27 Sep 2024 20:45:39 -0300 Subject: [PATCH] chore: move transparent receiver gen to its own function chore: formatting --- zingolib/src/wallet/keys/unified.rs | 96 +++++++++++++++++------------ 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/zingolib/src/wallet/keys/unified.rs b/zingolib/src/wallet/keys/unified.rs index 3db5b6e23..e82cd465a 100644 --- a/zingolib/src/wallet/keys/unified.rs +++ b/zingolib/src/wallet/keys/unified.rs @@ -260,47 +260,14 @@ impl WalletCapability { None }; - let transparent_receiver = if desired_receivers.transparent { - let child_index = KeyIndex::from_index(self.addresses.len() as u32); - let child_pk = match &self.transparent { - Capability::Spend(ext_sk) => { - let secp = secp256k1::Secp256k1::new(); - Some( - match ext_sk.derive_private_key(child_index) { - Err(e) => { - self.addresses_write_lock - .swap(false, atomic::Ordering::Release); - return Err(format!( - "Transparent private key derivation failed: {e}" - )); - } - Ok(res) => res.private_key, - } - .public_key(&secp), - ) - } - Capability::View(ext_pk) => Some(match ext_pk.derive_public_key(child_index) { - Err(e) => { - self.addresses_write_lock - .swap(false, atomic::Ordering::Release); - return Err(format!("Transparent public key derivation failed: {e}")); - } - Ok(res) => res.public_key, - }), - Capability::None => None, - }; - if let Some(pk) = child_pk { - self.transparent_child_addresses.push(( - self.addresses.len(), - #[allow(deprecated)] - zcash_primitives::legacy::keys::pubkey_to_address(&pk), - )); - Some(pk) - } else { - None + let transparent_receiver = match self.generate_transparent_receiver(desired_receivers) { + Ok(Some(transparent_receiver)) => Some(transparent_receiver), + Ok(None) => None, + Err(e) => { + self.addresses_write_lock + .swap(false, atomic::Ordering::Release); + return Err(e); } - } else { - None }; let ua = UnifiedAddress::from_receivers( @@ -331,6 +298,55 @@ impl WalletCapability { Ok(ua) } + /// Generates a transparent receiver if the wallet is capable of it. + /// + /// If the wallet is not capable of generating a transparent receiver, + /// `None` is returned. + pub fn generate_transparent_receiver( + &self, + desired_receivers: ReceiverSelection, + ) -> Result, String> { + if !desired_receivers.transparent { + return Ok(None); + } + let child_index = KeyIndex::from_index(self.addresses.len() as u32); + let child_pk = match &self.transparent { + Capability::Spend(ext_sk) => { + let secp = secp256k1::Secp256k1::new(); + Some( + match ext_sk.derive_private_key(child_index) { + Err(e) => { + self.addresses_write_lock + .swap(false, atomic::Ordering::Release); + return Err(format!("Transparent private key derivation failed: {e}")); + } + Ok(res) => res.private_key, + } + .public_key(&secp), + ) + } + Capability::View(ext_pk) => Some(match ext_pk.derive_public_key(child_index) { + Err(e) => { + self.addresses_write_lock + .swap(false, atomic::Ordering::Release); + return Err(format!("Transparent public key derivation failed: {e}")); + } + Ok(res) => res.public_key, + }), + Capability::None => None, + }; + if let Some(pk) = child_pk { + self.transparent_child_addresses.push(( + self.addresses.len(), + #[allow(deprecated)] + zcash_primitives::legacy::keys::pubkey_to_address(&pk), + )); + Ok(Some(pk)) + } else { + Ok(None) + } + } + /// TODO: Add Doc Comment Here! pub fn get_taddr_to_secretkey_map( &self,