diff --git a/zingolib/src/lib.rs b/zingolib/src/lib.rs index db341720b..589ebb51c 100644 --- a/zingolib/src/lib.rs +++ b/zingolib/src/lib.rs @@ -17,8 +17,8 @@ pub mod lightclient; pub mod utils; pub mod wallet; -#[cfg(test)] -pub(crate) mod mocks; +#[cfg(any(test, feature = "test-elevation"))] +pub mod mocks; #[cfg(any(test, feature = "test-elevation"))] pub mod testutils; #[cfg(any(test, feature = "testvectors"))] diff --git a/zingolib/src/lightclient/describe.rs b/zingolib/src/lightclient/describe.rs index ecdfa7d6f..a7f895838 100644 --- a/zingolib/src/lightclient/describe.rs +++ b/zingolib/src/lightclient/describe.rs @@ -56,6 +56,7 @@ impl LightClient { } /// TODO: Add Doc Comment Here! + // todo use helpers pub async fn do_addresses(&self) -> JsonValue { let mut objectified_addresses = Vec::new(); for address in self.wallet.wallet_capability().addresses().iter() { diff --git a/zingolib/src/lightclient/send.rs b/zingolib/src/lightclient/send.rs index 4b75be7c3..76db5677f 100644 --- a/zingolib/src/lightclient/send.rs +++ b/zingolib/src/lightclient/send.rs @@ -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(), diff --git a/zingolib/src/mocks.rs b/zingolib/src/mocks.rs index f3640664b..9665605a9 100644 --- a/zingolib/src/mocks.rs +++ b/zingolib/src/mocks.rs @@ -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( @@ -65,7 +62,7 @@ pub fn random_zaddr() -> ( } pub mod nullifier { - //! Module for mocking nullifiers from [`sapling_crypto::note::Nullifier`] and [`orchard::note::Nullifier`] + //! Module for mocking nullifiers from [`sapling_crypto::Nullifier`] and [`orchard::note::Nullifier`] use crate::utils::build_method; diff --git a/zingolib/src/utils.rs b/zingolib/src/utils.rs index cd380df65..15501f1fc 100644 --- a/zingolib/src/utils.rs +++ b/zingolib/src/utils.rs @@ -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."] @@ -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."] @@ -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 { @@ -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 diff --git a/zingolib/src/wallet/data.rs b/zingolib/src/wallet/data.rs index 61787f407..85d60a4a5 100644 --- a/zingolib/src/wallet/data.rs +++ b/zingolib/src/wallet/data.rs @@ -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; diff --git a/zingolib/src/wallet/describe.rs b/zingolib/src/wallet/describe.rs index 1c94e9853..a7fc9ec35 100644 --- a/zingolib/src/wallet/describe.rs +++ b/zingolib/src/wallet/describe.rs @@ -304,25 +304,94 @@ impl LightWallet { } } -#[cfg(test)] -mod tests { +#[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 { + 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 { + 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 { + let ua = self.get_first_ua()?; + self.encode_ua_as_pool(&ua, pool) + } + } + + #[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() { diff --git a/zingolib/src/wallet/notes/orchard.rs b/zingolib/src/wallet/notes/orchard.rs index b472882ae..21a0d7fd7 100644 --- a/zingolib/src/wallet/notes/orchard.rs +++ b/zingolib/src/wallet/notes/orchard.rs @@ -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; diff --git a/zingolib/src/wallet/notes/sapling.rs b/zingolib/src/wallet/notes/sapling.rs index 58e2d4f46..79b8a2fa3 100644 --- a/zingolib/src/wallet/notes/sapling.rs +++ b/zingolib/src/wallet/notes/sapling.rs @@ -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; diff --git a/zingolib/src/wallet/notes/transparent.rs b/zingolib/src/wallet/notes/transparent.rs index aa65f8f3b..b87dc3757 100644 --- a/zingolib/src/wallet/notes/transparent.rs +++ b/zingolib/src/wallet/notes/transparent.rs @@ -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}; diff --git a/zingolib/src/wallet/transaction_record.rs b/zingolib/src/wallet/transaction_record.rs index 735b45543..dc3986675 100644 --- a/zingolib/src/wallet/transaction_record.rs +++ b/zingolib/src/wallet/transaction_record.rs @@ -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;