Skip to content

Commit

Permalink
Merge pull request #1420 from fluidvanadium/wallet_base_address
Browse files Browse the repository at this point in the history
added LightWallet::get_first_address
  • Loading branch information
zancas authored Sep 28, 2024
2 parents f294b3f + a29d736 commit 6fa8078
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 32 deletions.
4 changes: 2 additions & 2 deletions zingolib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
1 change: 1 addition & 0 deletions zingolib/src/lightclient/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
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
5 changes: 1 addition & 4 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 Expand Up @@ -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;

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
99 changes: 84 additions & 15 deletions zingolib/src/wallet/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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| {
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)
}
}

#[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 6fa8078

Please sign in to comment.