Skip to content

Commit

Permalink
fix new_ephemeral_address -> new_persistant_ephemeral_address, now wr…
Browse files Browse the repository at this point in the history
…aps ephemeral_address, both made callable from more scopes
  • Loading branch information
AloeareV committed Oct 16, 2024
1 parent 8ee9533 commit caa4697
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 74 deletions.
30 changes: 27 additions & 3 deletions zingolib/src/wallet/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ use incrementalmerkletree::{Hashable, Position};
use json::JsonValue;
use prost::Message;

use std::convert::TryFrom;
use std::io::{self, Read, Write};
use zcash_client_backend::proto::compact_formats::CompactBlock;
use std::{convert::TryFrom, sync::Arc};
use zcash_client_backend::{
proto::compact_formats::CompactBlock, wallet::TransparentAddressMetadata,
};

use zcash_encoding::{Optional, Vector};

use zcash_primitives::memo::MemoBytes;
use zcash_primitives::merkle_tree::{read_commitment_tree, write_commitment_tree};
use zcash_primitives::{legacy::TransparentAddress, memo::MemoBytes};
use zcash_primitives::{memo::Memo, transaction::TxId};

pub use crate::wallet::transaction_record::TransactionRecord; // TODO: is this necessary? can we import this directly where its used?
Expand Down Expand Up @@ -2007,6 +2009,28 @@ fn read_write_empty_sapling_tree() {
)
}

/// Generate a new ephemeral transparent address,
/// for use in a send to a TEX address.
pub fn new_persistant_ephemeral_address(
transparent_child_ephemeral_addresses: Arc<
append_only_vec::AppendOnlyVec<(TransparentAddress, TransparentAddressMetadata)>,
>,
transparent_ephemeral_ivk: &zcash_primitives::legacy::keys::EphemeralIvk,
) -> Result<
(
zcash_primitives::legacy::TransparentAddress,
zcash_client_backend::wallet::TransparentAddressMetadata,
),
super::error::KeyError,
> {
let (ephemeral_address, metadata) = super::keys::unified::WalletCapability::ephemeral_address(
transparent_ephemeral_ivk,
transparent_child_ephemeral_addresses.len() as u32,
)?;
transparent_child_ephemeral_addresses.push((ephemeral_address, metadata.clone()));
Ok((ephemeral_address, metadata))
}

#[cfg(any(test, feature = "test-elevation"))]
pub(crate) mod mocks {
use zcash_primitives::memo::Memo;
Expand Down
31 changes: 20 additions & 11 deletions zingolib/src/wallet/keys/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ use zcash_primitives::legacy::{
};
use zcash_primitives::zip32::{AccountId, DiversifierIndex};

use crate::config::{ChainType, ZingoConfig};
use crate::wallet::error::KeyError;
use crate::wallet::traits::{DomainWalletExt, ReadableWriteable, Recipient};
use crate::{
config::{ChainType, ZingoConfig},
wallet::data::new_persistant_ephemeral_address,
};

use super::legacy::{generate_transparent_address_from_legacy_key, legacy_sks_to_usk, Capability};
use super::ToBase58Check;
Expand Down Expand Up @@ -775,8 +778,12 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
}

for _ in 0..ephemeral_addresses_len {
wc.new_ephemeral_address()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
new_persistant_ephemeral_address(
wc.transparent_child_ephemeral_addresses.clone(),
&wc.ephemeral_ivk()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?,
)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
}

Ok(wc)
Expand Down Expand Up @@ -898,7 +905,7 @@ mod ephemeral {
use zcash_client_backend::wallet::TransparentAddressMetadata;
use zcash_keys::keys::DerivationError;
use zcash_primitives::legacy::{
keys::{AccountPubKey, NonHardenedChildIndex},
keys::{AccountPubKey, NonHardenedChildIndex, TransparentKeyScope},
TransparentAddress,
};

Expand All @@ -916,16 +923,18 @@ mod ephemeral {
.map_err(KeyError::KeyDerivationError)
}
pub(crate) fn ephemeral_address(
&self,
ephemeral_ivk: &zcash_primitives::legacy::keys::EphemeralIvk,
ephemeral_address_index: u32,
) -> Result<TransparentAddress, KeyError> {
let eph_ivk = self.ephemeral_ivk()?;
) -> Result<(TransparentAddress, TransparentAddressMetadata), KeyError> {
let address_index = NonHardenedChildIndex::from_index(ephemeral_address_index)
.ok_or(KeyError::InvalidNonHardenedChildIndex)?;
eph_ivk
.derive_ephemeral_address(address_index)
.map_err(DerivationError::Transparent)
.map_err(KeyError::KeyDerivationError)
Ok((
ephemeral_ivk
.derive_ephemeral_address(address_index)
.map_err(DerivationError::Transparent)
.map_err(KeyError::KeyDerivationError)?,
TransparentAddressMetadata::new(TransparentKeyScope::EPHEMERAL, address_index),
))
}
/// TODO: Add Doc Comment Here!
pub fn transparent_child_ephemeral_addresses(
Expand Down
6 changes: 2 additions & 4 deletions zingolib/src/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use log::error;
use zcash_address::AddressKind;
use zcash_client_backend::proposal::Proposal;
use zcash_keys::keys::UnifiedSpendingKey;
use zcash_proofs::prover::LocalTxProver;

use std::cmp;
Expand Down Expand Up @@ -141,7 +140,7 @@ impl LightWallet {

match proposal.steps().len() {
1 => {
self.create_transaction_helper(sapling_prover, unified_spend_key, proposal)
self.create_transaction_helper(sapling_prover, proposal)
.await
}
2 if proposal.steps()[1]
Expand All @@ -152,7 +151,7 @@ impl LightWallet {
matches!(payment.recipient_address().kind(), AddressKind::Tex(_))
}) =>
{
self.create_transaction_helper(sapling_prover, unified_spend_key, proposal)
self.create_transaction_helper(sapling_prover, proposal)
.await
}

Expand All @@ -163,7 +162,6 @@ impl LightWallet {
async fn create_transaction_helper<NoteRef>(
&self,
sapling_prover: LocalTxProver,
unified_spend_key: UnifiedSpendingKey,
proposal: &Proposal<zcash_primitives::transaction::fees::zip317::FeeRule, NoteRef>,
) -> Result<(), BuildTransactionError> {
zcash_client_backend::data_api::wallet::create_proposed_transactions(
Expand Down
16 changes: 9 additions & 7 deletions zingolib/src/wallet/transaction_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,7 @@ mod decrypt_transaction {
use zcash_client_backend::wallet::TransparentAddressMetadata;
use zcash_keys::address::UnifiedAddress;
use zcash_primitives::{
legacy::{
keys::{NonHardenedChildIndex, TransparentKeyScope},
TransparentAddress,
},
legacy::keys::{NonHardenedChildIndex, TransparentKeyScope},
transaction::TxId,
};
use zingo_memo::ParsedMemo;
Expand Down Expand Up @@ -663,9 +660,14 @@ mod decrypt_transaction {
ephemeral_address_indexes: Vec<u32>,
) -> Result<(), InvalidMemoError> {
for ephemeral_address_index in ephemeral_address_indexes {
let ephemeral_address = self
.key
.ephemeral_address(ephemeral_address_index)
let (ephemeral_address, _metadata) =
crate::wallet::keys::unified::WalletCapability::ephemeral_address(
&self
.key
.ephemeral_ivk()
.map_err(InvalidMemoError::InvalidEphemeralIndex)?,
ephemeral_address_index,
)
.map_err(InvalidMemoError::InvalidEphemeralIndex)?;
let current_keys = &mut self.key.transparent_child_ephemeral_addresses();
let total_keys = current_keys.len();
Expand Down
44 changes: 1 addition & 43 deletions zingolib/src/wallet/tx_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use spending_data::SpendingData;
use std::{fmt::Debug, sync::Arc};
use thiserror::Error;
use zcash_client_backend::wallet::TransparentAddressMetadata;
use zcash_primitives::legacy::{
keys::{self, EphemeralIvk},
TransparentAddress,
};
use zcash_primitives::legacy::{keys::EphemeralIvk, TransparentAddress};

/// HashMap of all transactions in a wallet, keyed by txid.
/// Note that the parent is expected to hold a RwLock, so we will assume that all accesses to
Expand Down Expand Up @@ -90,45 +87,6 @@ impl TxMap {
self.transaction_records_by_id.clear();
self.witness_trees_mut().map(WitnessTrees::clear);
}
/// Generate a new ephemeral transparent address,
/// for use in a send to a TEX address.
pub fn new_ephemeral_address(
&self,
) -> Result<
(
zcash_primitives::legacy::TransparentAddress,
zcash_client_backend::wallet::TransparentAddressMetadata,
),
String,
> {
let child_index = keys::NonHardenedChildIndex::from_index(
self.transparent_child_ephemeral_addresses.len() as u32,
)
.ok_or_else(|| String::from("Ephemeral index overflow"))?;
let t_addr = self
.spending_data()
.as_ref()
.ok_or_else(|| String::from("Ephemeral addresses are only generated at spend time"))?
.transparent_ephemeral_ivk()
.derive_ephemeral_address(child_index)
.map_err(|e| e.to_string())?;
self.transparent_child_ephemeral_addresses.push((
t_addr,
TransparentAddressMetadata::new(
keys::TransparentKeyScope::EPHEMERAL,
keys::NonHardenedChildIndex::from_index(
self.transparent_child_ephemeral_addresses.len() as u32,
)
.expect("ephemeral index overflow"),
),
));
Ok(self
.transparent_child_ephemeral_addresses
.iter()
.last()
.expect("we just generated an address, this is known to be non-empty")
.clone())
}
}
#[cfg(test)]
impl TxMap {
Expand Down
20 changes: 14 additions & 6 deletions zingolib/src/wallet/tx_map/trait_walletwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,19 @@ impl WalletWrite for TxMap {
)>,
Self::Error,
> {
iter::repeat_with(|| {
self.new_ephemeral_address()
.map_err(TxMapTraitError::TexSendError)
})
.take(n)
.collect()
self.spending_data()
.map(|spending_data| {
iter::repeat_with(|| {
crate::wallet::data::new_persistant_ephemeral_address(
self.transparent_child_ephemeral_addresses.clone(),
spending_data.transparent_ephemeral_ivk(),
)
.map_err(TxMapTraitError::TexSendError)
})
.take(n)
.collect::<Result<_, _>>()
})
.transpose()?
.ok_or(TxMapTraitError::NoSpendCapability)
}
}

0 comments on commit caa4697

Please sign in to comment.