Skip to content

Commit

Permalink
implement get_Transaparent_receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Sep 16, 2024
1 parent aaa5c07 commit 566616b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
62 changes: 61 additions & 1 deletion zcash_client_memory/src/types/account.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, TryFromInto};
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashMap},
ops::{Deref, DerefMut},
};
use subtle::ConditionallySelectable;
Expand All @@ -18,6 +18,13 @@ use zcash_client_backend::{
keys::{UnifiedAddressRequest, UnifiedFullViewingKey},
wallet::NoteId,
};

#[cfg(feature = "transparent-inputs")]
use {
zcash_client_backend::wallet::TransparentAddressMetadata,
zcash_primitives::legacy::TransparentAddress,
};

/// Internal representation of ID type for accounts. Will be unique for each account.
#[derive(
Debug, Copy, Clone, PartialEq, Eq, Hash, Default, PartialOrd, Ord, Serialize, Deserialize,
Expand Down Expand Up @@ -209,6 +216,59 @@ impl Account {
}
}

#[cfg(feature = "transparent-inputs")]
pub(crate) fn get_transparent_receivers(
&self,
) -> Result<HashMap<TransparentAddress, Option<TransparentAddressMetadata>>, Error> {
use zcash_primitives::legacy::keys::NonHardenedChildIndex;
use zip32::Scope;

let mut addrs: Vec<(UnifiedAddress, DiversifierIndex)> = vec![];

let request = self
.viewing_key
.to_unified_incoming_viewing_key()
.to_address_request()
.and_then(|ua_request| ua_request.intersect(&UnifiedAddressRequest::all().unwrap()))
.ok_or(AddressGenerationError::ShieldedReceiverRequired)?;

let (mut current_addr, mut current_idx) = self.default_address(request)?;
addrs.push((current_addr.clone(), current_idx));

let (highest_addr, _) = self.current_address()?;

while current_addr != highest_addr {
current_idx.increment().map_err(|_| {
Error::AddressGeneration(AddressGenerationError::DiversifierSpaceExhausted)
})?;
(current_addr, current_idx) = self.uivk().find_address(current_idx, request)?;
addrs.push((current_addr.clone(), current_idx));
}
addrs
.into_iter()
.filter_map(|(addr, idx)| addr.transparent().map(|taddr| (*taddr, idx)))
.map(|(taddr, idx)| {
let metadata = TransparentAddressMetadata::new(
Scope::External.into(),
NonHardenedChildIndex::from_index(
DiversifierIndex::from(idx).try_into().map_err(|_| {
Error::CorruptedData(
"Unable to get diversifier for transparent address.".to_string(),
)
})?,
)
.ok_or_else(|| {
Error::CorruptedData(
"Unexpected hardened index for transparent address.".to_string(),
)
})?,
);
Ok((taddr, Some(metadata)))
})
.collect()
// TODO: Handle legacy???
}

pub(crate) fn account_id(&self) -> AccountId {
self.account_id
}
Expand Down
3 changes: 2 additions & 1 deletion zcash_client_memory/src/wallet_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,10 @@ impl<P: consensus::Parameters> WalletRead for MemoryWalletDb<P> {
#[cfg(feature = "transparent-inputs")]
fn get_transparent_receivers(
&self,
_account: Self::AccountId,
account_id: Self::AccountId,
) -> Result<HashMap<TransparentAddress, Option<TransparentAddressMetadata>>, Self::Error> {
tracing::debug!("get_transparent_receivers");

Ok(HashMap::new())
}

Expand Down

0 comments on commit 566616b

Please sign in to comment.