Skip to content

Commit

Permalink
Merge branch 'wallet_base_address' into generic_insufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
fluidvanadium committed Sep 24, 2024
2 parents 2022401 + 28a03c5 commit 64d2ff1
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 75 deletions.
2 changes: 1 addition & 1 deletion zingolib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod lightclient;
pub mod utils;
pub mod wallet;

#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub(crate) mod mocks;
#[cfg(any(test, feature = "test-elevation"))]
pub mod testutils;
Expand Down
4 changes: 2 additions & 2 deletions zingolib/src/lightclient/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ pub mod send_with_proposal {
#[tokio::test]
async fn complete_and_broadcast_unconnected_error() {
use crate::{
config::ZingoConfigBuilder, lightclient::LightClient, mocks::ProposalBuilder,
testvectors::seeds::ABANDON_ART_SEED,
config::ZingoConfigBuilder, lightclient::LightClient,
mocks::proposal::ProposalBuilder, testvectors::seeds::ABANDON_ART_SEED,
};
let lc = LightClient::create_unconnected(
&ZingoConfigBuilder::default().create(),
Expand Down
3 changes: 0 additions & 3 deletions zingolib/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

//! Tools to facilitate mocks for structs of external crates and general mocking utilities for testing

#[cfg(feature = "test-elevation")]
pub use proposal::ProposalBuilder;

pub use sapling_crypto_note::SaplingCryptoNoteBuilder;

fn zaddr_from_seed(
Expand Down
10 changes: 6 additions & 4 deletions zingolib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod conversion;
pub mod error;

#[cfg(any(test, feature = "test-elevation"))]
macro_rules! build_method {
($name:ident, $localtype:ty) => {
#[doc = "Set the $name field of the builder."]
Expand All @@ -12,7 +13,7 @@ macro_rules! build_method {
}
};
}
#[cfg(test)] // temporary test gate as no production builders use this macros yet
#[cfg(any(test, feature = "test-elevation"))]
macro_rules! build_method_push {
($name:ident, $localtype:ty) => {
#[doc = "Push a $ty to the builder."]
Expand All @@ -22,7 +23,7 @@ macro_rules! build_method_push {
}
};
}
#[cfg(test)] // temporary test gate as no production builders use this macros yet
#[cfg(any(test, feature = "test-elevation"))]
macro_rules! build_push_list {
($name:ident, $builder:ident, $struct:ident) => {
for i in &$builder.$name {
Expand All @@ -31,10 +32,11 @@ macro_rules! build_push_list {
};
}

#[cfg(any(test, feature = "test-elevation"))]
pub(crate) use build_method;
#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub(crate) use build_method_push;
#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub(crate) use build_push_list;

/// this mod exists to allow the use statement without cluttering the parent mod
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ fn read_write_empty_sapling_tree() {
)
}

#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub(crate) mod mocks {
use zcash_primitives::memo::Memo;

Expand Down
138 changes: 78 additions & 60 deletions zingolib/src/wallet/describe.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Wallet-State reporters as LightWallet methods.
use zcash_client_backend::PoolType;
use zcash_client_backend::ShieldedProtocol;

use orchard::note_encryption::OrchardDomain;
use sapling_crypto::note_encryption::SaplingDomain;

use zcash_primitives::consensus::NetworkConstants as _;
use zcash_primitives::transaction::components::amount::NonNegativeAmount;
use zcash_primitives::transaction::fees::zip317::MARGINAL_FEE;

Expand Down Expand Up @@ -304,76 +302,96 @@ impl LightWallet {
.map(|(_index, sk)| *sk)
.collect::<Vec<_>>()
}
}

#[allow(clippy::result_unit_err)]
/// gets a UnifiedAddress, the first the wallet. this is the only receiver implemented as 2024-09-22
pub fn get_first_ua(&self) -> Result<zcash_keys::address::UnifiedAddress, ()> {
if let Some(possible_ua) = self.wallet_capability().addresses().iter().next() {
Ok(possible_ua.clone())
} else {
Err(())
#[cfg(any(test, feature = "test-elevation"))]
mod test {

use zcash_client_backend::PoolType;
use zcash_client_backend::ShieldedProtocol;
use zcash_primitives::consensus::NetworkConstants as _;

use crate::wallet::LightWallet;

// these functions are tested gold and a better pattern for address lookup.
// maybe later they can be gated in.
impl LightWallet {
#[allow(clippy::result_unit_err)]
/// gets a UnifiedAddress, the first the wallet. this is the only receiver implemented as 2024-09-22
pub fn get_first_ua(&self) -> Result<zcash_keys::address::UnifiedAddress, ()> {
if let Some(possible_ua) = self.wallet_capability().addresses().iter().next() {
Ok(possible_ua.clone())
} else {
Err(())
}
}
}

#[allow(clippy::result_unit_err)]
/// UnifiedAddress type is not a string. to process it into a string requires chain date.
pub fn encode_ua_as_pool(
&self,
ua: &zcash_keys::address::UnifiedAddress,
pool: PoolType,
) -> Result<String, ()> {
match pool {
PoolType::Transparent => ua
.transparent()
.map(|taddr| {
super::keys::address_from_pubkeyhash(&self.transaction_context.config, *taddr)
})
.ok_or(()),
PoolType::Shielded(ShieldedProtocol::Sapling) => ua
.sapling()
.map(|z_addr| {
zcash_keys::encoding::encode_payment_address(
self.transaction_context
.config
.chain
.hrp_sapling_payment_address(),
z_addr,
)
})
.ok_or(()),
PoolType::Shielded(ShieldedProtocol::Orchard) => {
Ok(ua.encode(&self.transaction_context.config.chain))
#[allow(clippy::result_unit_err)]
/// UnifiedAddress type is not a string. to process it into a string requires chain date.
pub fn encode_ua_as_pool(
&self,
ua: &zcash_keys::address::UnifiedAddress,
pool: PoolType,
) -> Result<String, ()> {
match pool {
PoolType::Transparent => ua
.transparent()
.map(|taddr| {
crate::wallet::keys::address_from_pubkeyhash(
&self.transaction_context.config,
*taddr,
)
})
.ok_or(()),
PoolType::Shielded(ShieldedProtocol::Sapling) => ua
.sapling()
.map(|z_addr| {
zcash_keys::encoding::encode_payment_address(
self.transaction_context
.config
.chain
.hrp_sapling_payment_address(),
z_addr,
)
})
.ok_or(()),
PoolType::Shielded(ShieldedProtocol::Orchard) => {
Ok(ua.encode(&self.transaction_context.config.chain))
}
}
}
}

#[allow(clippy::result_unit_err)]
/// gets a string address for the wallet, based on pooltype
pub fn get_first_address(&self, pool: PoolType) -> Result<String, ()> {
let ua = self.get_first_ua()?;
self.encode_ua_as_pool(&ua, pool)
#[allow(clippy::result_unit_err)]
/// gets a string address for the wallet, based on pooltype
pub fn get_first_address(&self, pool: PoolType) -> Result<String, ()> {
let ua = self.get_first_ua()?;
self.encode_ua_as_pool(&ua, pool)
}
}
}

#[cfg(test)]
mod tests {
#[cfg(test)]
use orchard::note_encryption::OrchardDomain;
#[cfg(test)]
use sapling_crypto::note_encryption::SaplingDomain;

use crate::config::ZingoConfigBuilder;
#[cfg(test)]
use zingo_status::confirmation_status::ConfirmationStatus;

use crate::{
mocks::{orchard_note::OrchardCryptoNoteBuilder, SaplingCryptoNoteBuilder},
wallet::{
notes::{
orchard::mocks::OrchardNoteBuilder, sapling::mocks::SaplingNoteBuilder,
transparent::mocks::TransparentOutputBuilder,
},
transaction_record::mocks::TransactionRecordBuilder,
LightWallet, WalletBase,
},
};
#[cfg(test)]
use crate::config::ZingoConfigBuilder;
#[cfg(test)]
use crate::mocks::orchard_note::OrchardCryptoNoteBuilder;
#[cfg(test)]
use crate::mocks::SaplingCryptoNoteBuilder;
#[cfg(test)]
use crate::wallet::notes::orchard::mocks::OrchardNoteBuilder;
#[cfg(test)]
use crate::wallet::notes::sapling::mocks::SaplingNoteBuilder;
#[cfg(test)]
use crate::wallet::notes::transparent::mocks::TransparentOutputBuilder;
#[cfg(test)]
use crate::wallet::transaction_record::mocks::TransactionRecordBuilder;
#[cfg(test)]
use crate::wallet::WalletBase;

#[tokio::test]
async fn confirmed_balance_excluding_dust() {
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/notes/orchard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl ShieldedNoteInterface for OrchardNote {
}
}

#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub mod mocks {
//! Mock version of the struct for testing
use incrementalmerkletree::Position;
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/notes/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl ShieldedNoteInterface for SaplingNote {
}
}

#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub mod mocks {
//! Mock version of the struct for testing
use incrementalmerkletree::Position;
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/notes/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl TransparentOutput {
}
}

#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub mod mocks {
//! Mock version of the struct for testing
use zcash_primitives::{legacy::TransparentAddress, transaction::TxId};
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/transaction_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ pub enum SendType {
SendToSelf,
}

#[cfg(test)]
#[cfg(any(test, feature = "test-elevation"))]
pub mod mocks {
//! Mock version of the struct for testing
use zcash_primitives::transaction::TxId;
Expand Down

0 comments on commit 64d2ff1

Please sign in to comment.