Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate from misnamed ephemeral--->rejection addresses #1465

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions zingo-memo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum ParsedMemo {
/// the list of unified addresses
uas: Vec<UnifiedAddress>,
/// The ephemeral address indexes
ephemeral_address_indexes: Vec<u32>,
rejection_address_indexes: Vec<u32>,
},
}

Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn parse_zingo_memo(memo: [u8; 511]) -> io::Result<ParsedMemo> {
}),
1 => Ok(ParsedMemo::Version1 {
uas: Vector::read(&mut reader, |r| read_unified_address_from_raw_encoding(r))?,
ephemeral_address_indexes: Vector::read(&mut reader, |r| CompactSize::read_t(r))?,
rejection_address_indexes: Vector::read(&mut reader, |r| CompactSize::read_t(r))?,
}),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/blaze/fetch_taddr_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl FetchTaddrTransactions {
.iter()
.filter_map(|ua| ua.transparent())
.chain(
wc.transparent_child_ephemeral_addresses()
wc.get_rejection_addresses()
.iter()
.map(|(taddr, _metadata)| taddr),
)
Expand Down
4 changes: 2 additions & 2 deletions zingolib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ impl LightWallet {
let transaction_metadata_set = if wc.unified_key_store().is_spending_key() {
Arc::new(RwLock::new(TxMap::new_with_witness_trees(
wc.transparent_child_addresses().clone(),
wc.transparent_child_ephemeral_addresses().clone(),
wc.ephemeral_ivk().map_err(|e| {
wc.get_rejection_addresses().clone(),
wc.rejection_ivk().map_err(|e| {
Error::new(
ErrorKind::InvalidData,
format!("Error with transparent key: {e}"),
Expand Down
27 changes: 14 additions & 13 deletions zingolib/src/wallet/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ pub mod summaries {
Shield,
/// The recipient is the creator and is receiving at least 1 note with a TEXT memo
MemoToSelf,
/// The recipient is an ephemeral 320 address
Ephemeral320,
/// The recipient is an "ephemeral" 320 address
Rejection,
}

impl std::fmt::Display for ValueTransferKind {
Expand All @@ -804,7 +804,7 @@ pub mod summaries {
SelfSendValueTransfer::Basic => write!(f, "basic"),
SelfSendValueTransfer::Shield => write!(f, "shield"),
SelfSendValueTransfer::MemoToSelf => write!(f, "memo-to-self"),
SelfSendValueTransfer::Ephemeral320 => write!(f, "ephemeral-320-tex"),
SelfSendValueTransfer::Rejection => write!(f, "rejection"),
},
},
}
Expand Down Expand Up @@ -1994,28 +1994,29 @@ impl WalletZecPriceInfo {
}
}

/// Generate a new ephemeral transparent address,
/// Generate a new rejection address,
/// for use in a send to a TEX address.
pub fn new_persistent_ephemeral_address(
transparent_child_ephemeral_addresses: &append_only_vec::AppendOnlyVec<(
pub fn new_rejection_address(
rejection_addresses: &append_only_vec::AppendOnlyVec<(
TransparentAddress,
TransparentAddressMetadata,
)>,

transparent_ephemeral_ivk: &zcash_primitives::legacy::keys::EphemeralIvk,
rejection_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))
let (rejection_address, metadata) =
super::keys::unified::WalletCapability::get_rejection_address_by_index(
rejection_ivk,
rejection_addresses.len() as u32,
)?;
rejection_addresses.push((rejection_address, metadata.clone()));
Ok((rejection_address, metadata))
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use super::{

impl LightWallet {
/// Changes in version 30:
/// - New WalletCapability version (v4) which implements read/write for ephemeral addresses
/// - New WalletCapability version (v4) which implements read/write for rejection addresses
pub const fn serialized_version() -> u64 {
30
}
Expand Down
72 changes: 35 additions & 37 deletions zingolib/src/wallet/keys/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use zcash_client_backend::address::UnifiedAddress;
use zcash_client_backend::keys::{Era, UnifiedSpendingKey};
use zcash_client_backend::wallet::TransparentAddressMetadata;
use zcash_encoding::{CompactSize, Vector};
use zcash_keys::{
encoding::AddressCodec,
keys::{DerivationError, UnifiedFullViewingKey},
};
use zcash_keys::keys::{DerivationError, UnifiedFullViewingKey};
use zcash_primitives::consensus::{NetworkConstants, Parameters};
use zcash_primitives::legacy::{
keys::{AccountPubKey, IncomingViewingKey, NonHardenedChildIndex},
Expand All @@ -36,7 +33,7 @@ use crate::wallet::error::KeyError;
use crate::wallet::traits::{DomainWalletExt, ReadableWriteable, Recipient};
use crate::{
config::{ChainType, ZingoConfig},
wallet::data::new_persistent_ephemeral_address,
wallet::data::new_rejection_address,
};

use super::legacy::{generate_transparent_address_from_legacy_key, legacy_sks_to_usk, Capability};
Expand Down Expand Up @@ -234,8 +231,7 @@ pub struct WalletCapability {
transparent_child_addresses: Arc<append_only_vec::AppendOnlyVec<(usize, TransparentAddress)>>,
// TODO: read/write for ephmereral addresses
// TODO: Remove this field and exclusively use the TxMap field instead
transparent_child_ephemeral_addresses:
Arc<AppendOnlyVec<(TransparentAddress, TransparentAddressMetadata)>>,
rejection_addresses: Arc<AppendOnlyVec<(TransparentAddress, TransparentAddressMetadata)>>,
/// Cache of unified_addresses
unified_addresses: append_only_vec::AppendOnlyVec<UnifiedAddress>,
addresses_write_lock: AtomicBool,
Expand All @@ -245,7 +241,7 @@ impl Default for WalletCapability {
Self {
unified_key_store: UnifiedKeyStore::Empty,
transparent_child_addresses: Arc::new(AppendOnlyVec::new()),
transparent_child_ephemeral_addresses: Arc::new(AppendOnlyVec::new()),
rejection_addresses: Arc::new(AppendOnlyVec::new()),
unified_addresses: AppendOnlyVec::new(),
addresses_write_lock: AtomicBool::new(false),
}
Expand Down Expand Up @@ -584,16 +580,9 @@ impl WalletCapability {
.collect()
}

pub(crate) fn get_ephemeral_taddrs(&self, chain: &crate::config::ChainType) -> HashSet<String> {
self.transparent_child_ephemeral_addresses
.iter()
.map(|(transparent_address, _metadata)| transparent_address.encode(chain))
.collect()
}

pub(crate) fn get_taddrs(&self, chain: &crate::config::ChainType) -> HashSet<String> {
self.get_external_taddrs(chain)
.union(&self.get_ephemeral_taddrs(chain))
.union(&self.get_rejection_address_set(chain))
.cloned()
.collect()
}
Expand Down Expand Up @@ -642,13 +631,13 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
fn read<R: Read>(mut reader: R, input: ChainType) -> io::Result<Self> {
let version = Self::get_version(&mut reader)?;
let legacy_key: bool;
let ephemeral_addresses_len: u32;
let length_of_rejection_addresses: u32;

let wc = match version {
// in version 1, only spending keys are stored
1 => {
legacy_key = true;
ephemeral_addresses_len = 0;
length_of_rejection_addresses = 0;

// Create a temporary USK for address generation to load old wallets
// due to missing BIP0032 transparent extended private key data
Expand All @@ -667,7 +656,7 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
}
2 => {
legacy_key = true;
ephemeral_addresses_len = 0;
length_of_rejection_addresses = 0;

let orchard_capability = Capability::<
orchard::keys::FullViewingKey,
Expand Down Expand Up @@ -758,7 +747,7 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
}
3 => {
legacy_key = false;
ephemeral_addresses_len = 0;
length_of_rejection_addresses = 0;

Self {
unified_key_store: UnifiedKeyStore::read(&mut reader, input)?,
Expand All @@ -767,7 +756,7 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
}
4 => {
legacy_key = false;
ephemeral_addresses_len = reader.read_u32::<LittleEndian>()?;
length_of_rejection_addresses = reader.read_u32::<LittleEndian>()?;

Self {
unified_key_store: UnifiedKeyStore::read(&mut reader, input)?,
Expand All @@ -787,10 +776,10 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
}

for _ in 0..ephemeral_addresses_len {
new_persistent_ephemeral_address(
&wc.transparent_child_ephemeral_addresses,
&wc.ephemeral_ivk()
for _ in 0..length_of_rejection_addresses {
new_rejection_address(
&wc.rejection_addresses,
&wc.rejection_ivk()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?,
)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Expand All @@ -801,7 +790,7 @@ impl ReadableWriteable<ChainType, ChainType> for WalletCapability {

fn write<W: Write>(&self, mut writer: W, input: ChainType) -> io::Result<()> {
writer.write_u8(Self::VERSION)?;
writer.write_u32::<LittleEndian>(self.transparent_child_ephemeral_addresses.len() as u32)?;
writer.write_u32::<LittleEndian>(self.rejection_addresses.len() as u32)?;
self.unified_key_store().write(&mut writer, input)?;
Vector::write(
&mut writer,
Expand Down Expand Up @@ -908,12 +897,12 @@ impl Fvk<SaplingDomain> for sapling_crypto::zip32::DiversifiableFullViewingKey {
}
}
}
mod ephemeral {
use std::sync::Arc;
mod rejection {
use std::{collections::HashSet, sync::Arc};

use append_only_vec::AppendOnlyVec;
use zcash_client_backend::wallet::TransparentAddressMetadata;
use zcash_keys::keys::DerivationError;
use zcash_keys::{encoding::AddressCodec, keys::DerivationError};
use zcash_primitives::legacy::{
keys::{AccountPubKey, NonHardenedChildIndex, TransparentKeyScope},
TransparentAddress,
Expand All @@ -924,33 +913,42 @@ mod ephemeral {
use super::WalletCapability;

impl WalletCapability {
pub(crate) fn ephemeral_ivk(
pub(crate) fn rejection_ivk(
&self,
) -> Result<zcash_primitives::legacy::keys::EphemeralIvk, KeyError> {
AccountPubKey::try_from(self.unified_key_store())?
.derive_ephemeral_ivk()
.map_err(DerivationError::Transparent)
.map_err(KeyError::KeyDerivationError)
}
pub(crate) fn ephemeral_address(
ephemeral_ivk: &zcash_primitives::legacy::keys::EphemeralIvk,
ephemeral_address_index: u32,
pub(crate) fn get_rejection_address_by_index(
rejection_ivk: &zcash_primitives::legacy::keys::EphemeralIvk,
rejection_address_index: u32,
) -> Result<(TransparentAddress, TransparentAddressMetadata), KeyError> {
let address_index = NonHardenedChildIndex::from_index(ephemeral_address_index)
let address_index = NonHardenedChildIndex::from_index(rejection_address_index)
.ok_or(KeyError::InvalidNonHardenedChildIndex)?;
Ok((
ephemeral_ivk
rejection_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(
pub fn get_rejection_addresses(
&self,
) -> &Arc<AppendOnlyVec<(TransparentAddress, TransparentAddressMetadata)>> {
&self.transparent_child_ephemeral_addresses
&self.rejection_addresses
}
pub(crate) fn get_rejection_address_set(
&self,
chain: &crate::config::ChainType,
) -> HashSet<String> {
self.rejection_addresses
.iter()
.map(|(transparent_address, _metadata)| transparent_address.encode(chain))
.collect()
}
}
}
9 changes: 3 additions & 6 deletions zingolib/src/wallet/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ impl LightWallet {
&self,
request: TransactionRequest,
) -> Result<crate::data::proposal::ProportionalFeeProposal, ProposeSendError> {
let num_ephemeral_addresses = self
.transaction_context
.key
.transparent_child_ephemeral_addresses()
.len() as u32;
let memo = change_memo_from_transaction_request(&request, num_ephemeral_addresses);
let number_of_rejection_addresses =
self.transaction_context.key.get_rejection_addresses().len() as u32;
let memo = change_memo_from_transaction_request(&request, number_of_rejection_addresses);

let input_selector = build_default_giskit(Some(memo));
let mut tmamt = self
Expand Down
10 changes: 5 additions & 5 deletions zingolib/src/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ impl LightWallet {
// TODO: move to a more suitable place
pub(crate) fn change_memo_from_transaction_request(
request: &TransactionRequest,
mut num_ephemeral_addresses: u32,
mut number_of_rejection_addresses: u32,
) -> MemoBytes {
let mut recipient_uas = Vec::new();
let mut ephemeral_address_indexes = Vec::new();
let mut rejection_address_indexes = Vec::new();
for payment in request.payments().values() {
match payment.recipient_address().kind() {
AddressKind::Unified(ua) => {
Expand All @@ -201,16 +201,16 @@ pub(crate) fn change_memo_from_transaction_request(
}
}
AddressKind::Tex(_) => {
ephemeral_address_indexes.push(num_ephemeral_addresses);
rejection_address_indexes.push(number_of_rejection_addresses);

num_ephemeral_addresses += 1;
number_of_rejection_addresses += 1;
}
_ => (),
}
}
let uas_bytes = match create_wallet_internal_memo_version_1(
recipient_uas.as_slice(),
ephemeral_address_indexes.as_slice(),
rejection_address_indexes.as_slice(),
) {
Ok(bytes) => bytes,
Err(e) => {
Expand Down
Loading
Loading