From b0405d68e1eaedd5b48f8933b95fd616d0c5e473 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Thu, 7 Mar 2024 17:51:46 +0000 Subject: [PATCH 1/5] replaced missing transparent viewkey panic with handled error --- zingolib/src/wallet/keys/unified.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/zingolib/src/wallet/keys/unified.rs b/zingolib/src/wallet/keys/unified.rs index 5b3d9c77f2..3c49d65355 100644 --- a/zingolib/src/wallet/keys/unified.rs +++ b/zingolib/src/wallet/keys/unified.rs @@ -164,24 +164,19 @@ impl WalletCapability { } } - pub(crate) fn ufvk(&self) -> Result { - let o_fvk = Fvk::Orchard( - orchard::keys::FullViewingKey::try_from(self) - .unwrap() - .to_bytes(), - ); + pub(crate) fn ufvk(&self) -> Result { + let o_fvk = Fvk::Orchard(orchard::keys::FullViewingKey::try_from(self)?.to_bytes()); let s_fvk = Fvk::Sapling( - zcash_primitives::zip32::sapling::DiversifiableFullViewingKey::try_from(self) - .unwrap() + zcash_primitives::zip32::sapling::DiversifiableFullViewingKey::try_from(self)? .to_bytes(), ); let mut t_fvk_bytes = [0u8; 65]; - let t_ext_pk: ExtendedPubKey = self.try_into().unwrap(); + let t_ext_pk: ExtendedPubKey = self.try_into()?; t_fvk_bytes[0..32].copy_from_slice(&t_ext_pk.chain_code[..]); t_fvk_bytes[32..65].copy_from_slice(&t_ext_pk.public_key.serialize()[..]); let t_fvk = Fvk::P2pkh(t_fvk_bytes); use zcash_address::unified::Encoding as _; - Ufvk::try_from_items(vec![o_fvk, s_fvk, t_fvk]) + Ufvk::try_from_items(vec![o_fvk, s_fvk, t_fvk]).map_err(|e| e.to_string()) } pub fn new_address( From bfb2239dc743ba93d360c1fa8537db556e44c818 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Thu, 7 Mar 2024 18:05:40 +0000 Subject: [PATCH 2/5] merge commit test branch --- integration-tests/tests/integrations.rs | 13 +++++++++++++ zingolib/src/wallet/keys/unified.rs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/integration-tests/tests/integrations.rs b/integration-tests/tests/integrations.rs index 77ba3e22db..a3ed9c46fb 100644 --- a/integration-tests/tests/integrations.rs +++ b/integration-tests/tests/integrations.rs @@ -124,6 +124,9 @@ fn check_view_capability_bounds( } mod fast { + use zcash_address::unified::Encoding; + use zingolib::wallet::WalletBase; + use super::*; #[tokio::test] async fn utxos_are_not_prematurely_confirmed() { @@ -696,6 +699,16 @@ mod fast { assert!(addr.transparent().is_some()); } + let ufvk = wc.ufvk().unwrap(); + let ufvk_string = ufvk.encode(&config.chain.to_zcash_address_network()); + let ufvk_base = WalletBase::Ufvk(ufvk_string.clone()); + let view_wallet = + LightWallet::new(config.clone(), ufvk_base, wallet.get_birthday().await).unwrap(); + let v_wc = view_wallet.wallet_capability(); + let vv = v_wc.ufvk().unwrap(); + let vv_string = vv.encode(&config.chain.to_zcash_address_network()); + assert_eq!(ufvk_string, vv_string); + let client = LightClient::create_from_wallet_async(wallet, config) .await .unwrap(); diff --git a/zingolib/src/wallet/keys/unified.rs b/zingolib/src/wallet/keys/unified.rs index 3c49d65355..50a40b98f4 100644 --- a/zingolib/src/wallet/keys/unified.rs +++ b/zingolib/src/wallet/keys/unified.rs @@ -164,7 +164,7 @@ impl WalletCapability { } } - pub(crate) fn ufvk(&self) -> Result { + pub fn ufvk(&self) -> Result { let o_fvk = Fvk::Orchard(orchard::keys::FullViewingKey::try_from(self)?.to_bytes()); let s_fvk = Fvk::Sapling( zcash_primitives::zip32::sapling::DiversifiableFullViewingKey::try_from(self)? From 2093cdf1d362db77e65a80f993873536881e6aeb Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Fri, 8 Mar 2024 05:59:28 +0000 Subject: [PATCH 3/5] pass function with only missing transparent key to export the same viewkey that came in --- zingolib/src/wallet/keys/unified.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/zingolib/src/wallet/keys/unified.rs b/zingolib/src/wallet/keys/unified.rs index 50a40b98f4..d981b29e40 100644 --- a/zingolib/src/wallet/keys/unified.rs +++ b/zingolib/src/wallet/keys/unified.rs @@ -171,12 +171,15 @@ impl WalletCapability { .to_bytes(), ); let mut t_fvk_bytes = [0u8; 65]; - let t_ext_pk: ExtendedPubKey = self.try_into()?; - t_fvk_bytes[0..32].copy_from_slice(&t_ext_pk.chain_code[..]); - t_fvk_bytes[32..65].copy_from_slice(&t_ext_pk.public_key.serialize()[..]); - let t_fvk = Fvk::P2pkh(t_fvk_bytes); - use zcash_address::unified::Encoding as _; - Ufvk::try_from_items(vec![o_fvk, s_fvk, t_fvk]).map_err(|e| e.to_string()) + let possible_transparent_key: Result = self.try_into(); + if let Ok(t_ext_pk) = possible_transparent_key { + t_fvk_bytes[0..32].copy_from_slice(&t_ext_pk.chain_code[..]); + t_fvk_bytes[32..65].copy_from_slice(&t_ext_pk.public_key.serialize()[..]); + let t_fvk = Fvk::P2pkh(t_fvk_bytes); + Ufvk::try_from_items(vec![o_fvk, s_fvk, t_fvk]).map_err(|e| e.to_string()) + } else { + Ufvk::try_from_items(vec![o_fvk, s_fvk]).map_err(|e| e.to_string()) + } } pub fn new_address( From f7e5c13c204807cc6170d17bee884a2a1a288eb8 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Fri, 8 Mar 2024 06:06:26 +0000 Subject: [PATCH 4/5] bonus: replaced unwrap on line 1388 with ? --- zingolib/src/lightclient.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zingolib/src/lightclient.rs b/zingolib/src/lightclient.rs index f4b2f853a8..34e4e1df00 100644 --- a/zingolib/src/lightclient.rs +++ b/zingolib/src/lightclient.rs @@ -1384,8 +1384,7 @@ impl LightClient { self.get_server_uri(), last_synced_height, ) - .await - .unwrap(); + .await?; self.wallet.initiate_witness_trees(trees).await; }; From f867077cc124f6d31b272f66f6db712242b9db7b Mon Sep 17 00:00:00 2001 From: Oscar Pepper Date: Fri, 8 Mar 2024 10:47:16 +0000 Subject: [PATCH 5/5] fix clippy error --- zingolib/src/wallet.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zingolib/src/wallet.rs b/zingolib/src/wallet.rs index d5d1a7ff5b..2880418762 100644 --- a/zingolib/src/wallet.rs +++ b/zingolib/src/wallet.rs @@ -445,13 +445,14 @@ impl LightWallet { /// Determines the target height for a transaction, and the offset from which to /// select anchors, based on the current synchronised block chain. async fn get_target_height_and_anchor_offset(&self) -> Option<(u32, usize)> { - match { + let range = { let blocks = self.blocks.read().await; ( blocks.last().map(|block| block.height as u32), blocks.first().map(|block| block.height as u32), ) - } { + }; + match range { (Some(min_height), Some(max_height)) => { let target_height = max_height + 1;