Merge pull request #647 from nuttycom/update_ua_test_vectors

Update unified address test vectors
This commit is contained in:
Kris Nuttycombe 2022-10-12 10:56:26 -06:00 committed by GitHub
commit c3b6ef28c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 1256 additions and 786 deletions

View File

@ -24,5 +24,8 @@ zcash_encoding = { version = "0.1", path = "../zcash_encoding" }
assert_matches = "1.3.0"
proptest = "1"
[features]
test-dependencies = []
[lib]
bench = false

View File

@ -91,8 +91,8 @@ impl super::Container for Address {
}
}
#[cfg(test)]
pub(crate) mod test_vectors;
#[cfg(any(test, feature = "test-dependencies"))]
pub mod test_vectors;
#[cfg(test)]
mod tests {

View File

@ -2,8 +2,8 @@ mod convert;
mod encoding;
mod kind;
#[cfg(test)]
mod test_vectors;
#[cfg(any(test, feature = "test-dependencies"))]
pub mod test_vectors;
pub use convert::{
ConversionError, ToAddress, TryFromAddress, TryFromRawAddress, UnsupportedAddress,

View File

@ -1,43 +1,50 @@
use std::iter;
/// Export test vectors for reuse by implementers of address parsing libraries.
#[cfg(feature = "test-dependencies")]
pub use crate::unified::address::test_vectors::TEST_VECTORS as UNIFIED;
use crate::{
unified::{
self,
address::{test_vectors::test_vectors, Receiver},
#[cfg(test)]
use {
crate::{
unified::{
self,
address::{test_vectors::TEST_VECTORS, Receiver},
},
Network, ToAddress, ZcashAddress,
},
Network, ToAddress, ZcashAddress,
std::iter,
};
#[test]
fn unified() {
for tv in test_vectors() {
for tv in TEST_VECTORS {
// Double-check test vectors match requirements:
// - Only one of P2PKH and P2SH.
assert!(tv.p2pkh_bytes.is_none() || tv.p2sh_bytes.is_none());
// - At least one shielded receiver.
assert!(tv.sapling_raw_addr.is_some() || tv.orchard_raw_addr.is_some());
let addr_string = String::from_utf8(tv.unified_addr.to_vec()).unwrap();
let unknown_tc = tv.unknown_typecode;
let unknown_bytes = tv.unknown_bytes;
let receivers = iter::empty()
.chain(tv.p2pkh_bytes.map(Receiver::P2pkh))
.chain(tv.p2sh_bytes.map(Receiver::P2sh))
.chain(tv.sapling_raw_addr.map(Receiver::Sapling))
.chain(tv.orchard_raw_addr.map(Receiver::Orchard))
.chain(tv.unknown_bytes.map(|data| Receiver::Unknown {
typecode: unknown_tc,
data,
.chain(unknown_tc.and_then(|typecode| {
unknown_bytes.map(move |data| Receiver::Unknown {
typecode,
data: data.to_vec(),
})
}))
.collect();
let expected_addr = ZcashAddress::from_unified(Network::Main, unified::Address(receivers));
// Test parsing
let addr: ZcashAddress = addr_string.parse().unwrap();
let addr: ZcashAddress = tv.unified_addr.parse().unwrap();
assert_eq!(addr, expected_addr);
// Test serialization
assert_eq!(expected_addr.to_string(), addr_string);
assert_eq!(expected_addr.to_string(), tv.unified_addr.to_string());
}
}

View File

@ -54,6 +54,7 @@ proptest = "1.0.0"
rand_xorshift = "0.3"
tempfile = "3.1.0"
zcash_proofs = { version = "0.7", path = "../zcash_proofs" }
zcash_address = { version = "0.1", path = "../components/zcash_address", features = ["test-dependencies"] }
[features]
transparent-inputs = ["ripemd", "hdwallet", "sha2", "secp256k1", "zcash_primitives/transparent-inputs"]

View File

@ -225,6 +225,7 @@ impl RecipientAddress {
#[cfg(test)]
mod tests {
use zcash_address::test_vectors;
use zcash_primitives::{consensus::MAIN_NETWORK, zip32::ExtendedFullViewingKey};
use super::{RecipientAddress, UnifiedAddress};
@ -255,4 +256,32 @@ mod tests {
Some(addr)
);
}
#[test]
fn ua_parsing() {
for tv in test_vectors::UNIFIED {
match RecipientAddress::decode(&MAIN_NETWORK, tv.unified_addr) {
Some(RecipientAddress::Unified(ua)) => {
assert_eq!(
ua.transparent().is_some(),
tv.p2pkh_bytes.is_some() || tv.p2sh_bytes.is_some()
);
assert_eq!(ua.sapling().is_some(), tv.sapling_raw_addr.is_some());
assert_eq!(ua.orchard().is_some(), tv.orchard_raw_addr.is_some());
}
Some(_) => {
panic!(
"{} did not decode to a unified address value.",
tv.unified_addr
);
}
None => {
panic!(
"Failed to decode unified address from test vector: {}",
tv.unified_addr
);
}
}
}
}
}

View File

@ -591,10 +591,14 @@ mod tests {
#[cfg(feature = "transparent-inputs")]
use {
crate::encoding::AddressCodec,
zcash_primitives::legacy::{
self,
keys::{AccountPrivKey, IncomingViewingKey},
crate::{address::RecipientAddress, encoding::AddressCodec},
zcash_address::test_vectors,
zcash_primitives::{
legacy::{
self,
keys::{AccountPrivKey, IncomingViewingKey},
},
zip32::DiversifierIndex,
},
};
@ -699,6 +703,50 @@ mod tests {
assert_eq!(decoded_with_t.unknown.len(), 1);
}
#[test]
#[cfg(feature = "transparent-inputs")]
fn ufvk_derivation() {
for tv in test_vectors::UNIFIED {
let usk = UnifiedSpendingKey::from_seed(
&MAIN_NETWORK,
&tv.root_seed,
AccountId::from(tv.account),
)
.expect("seed produced a valid unified spending key");
let d_idx = DiversifierIndex::from(tv.diversifier_index);
let ufvk = usk.to_unified_full_viewing_key();
// The test vectors contain some diversifier indices that do not generate
// valid Sapling addresses, so skip those.
if ufvk.sapling().unwrap().address(d_idx).is_none() {
continue;
}
let ua = ufvk.address(d_idx).unwrap_or_else(|| panic!("diversifier index {} should have produced a valid unified address for account {}",
tv.diversifier_index, tv.account));
match RecipientAddress::decode(&MAIN_NETWORK, tv.unified_addr) {
Some(RecipientAddress::Unified(tvua)) => {
// We always derive transparent and Sapling receivers, but not
// every value in the test vectors has these present.
if tvua.transparent().is_some() {
assert_eq!(tvua.transparent(), ua.transparent());
}
if tvua.sapling().is_some() {
assert_eq!(tvua.sapling(), ua.sapling());
}
}
_other => {
panic!(
"{} did not decode to a valid unified address",
tv.unified_addr
);
}
}
}
}
proptest! {
#[test]
#[cfg(feature = "unstable")]

View File

@ -37,6 +37,7 @@ regex = "1.4"
tempfile = "3"
zcash_proofs = { version = "0.7", path = "../zcash_proofs" }
zcash_primitives = { version = "0.7", path = "../zcash_primitives", features = ["test-dependencies"] }
zcash_address = { version = "0.1", path = "../components/zcash_address", features = ["test-dependencies"] }
[features]
mainnet = []

View File

@ -293,6 +293,8 @@ mod tests {
use std::collections::HashMap;
use tempfile::NamedTempFile;
use zcash_address::test_vectors;
use zcash_client_backend::{
address::RecipientAddress,
encoding::{encode_extended_full_viewing_key, encode_payment_address},
@ -301,16 +303,19 @@ mod tests {
use zcash_primitives::{
block::BlockHash,
consensus::{BlockHeight, BranchId, Parameters},
consensus::{BlockHeight, BranchId, Network, Parameters},
transaction::{TransactionData, TxVersion},
zip32::sapling::{DiversifiableFullViewingKey, ExtendedFullViewingKey},
zip32::{
sapling::{DiversifiableFullViewingKey, ExtendedFullViewingKey},
DiversifierIndex,
},
};
use crate::{
error::SqliteClientError,
tests::{self, network},
wallet::get_address,
AccountId, WalletDb,
wallet::{self, get_address},
AccountId, WalletDb, WalletWrite,
};
use super::{init_accounts_table, init_blocks_table, init_wallet_db};
@ -1071,7 +1076,7 @@ mod tests {
fn init_accounts_table_stores_correct_address() {
let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data, Some(Secret::new(vec![]))).unwrap();
init_wallet_db(&mut db_data, None).unwrap();
let seed = [0u8; 32];
@ -1087,4 +1092,40 @@ mod tests {
let pa = get_address(&db_data, AccountId::from(0)).unwrap();
assert_eq!(pa.unwrap(), expected_address);
}
#[test]
#[cfg(feature = "transparent-inputs")]
fn account_produces_expected_ua_sequence() {
let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), Network::MainNetwork).unwrap();
init_wallet_db(&mut db_data, None).unwrap();
let mut ops = db_data.get_update_ops().unwrap();
let seed = test_vectors::UNIFIED[0].root_seed;
let (account, _usk) = ops.create_account(&Secret::new(seed.to_vec())).unwrap();
assert_eq!(account, AccountId::from(0u32));
for tv in &test_vectors::UNIFIED[..3] {
if let Some(RecipientAddress::Unified(tvua)) =
RecipientAddress::decode(&Network::MainNetwork, tv.unified_addr)
{
let (ua, di) = wallet::get_current_address(&db_data, account)
.unwrap()
.expect("create_account generated the first address");
assert_eq!(DiversifierIndex::from(tv.diversifier_index), di);
assert_eq!(tvua.transparent(), ua.transparent());
assert_eq!(tvua.sapling(), ua.sapling());
assert_eq!(tv.unified_addr, ua.encode(&Network::MainNetwork));
ops.get_next_available_address(account)
.unwrap()
.expect("get_next_available_address generated an address");
} else {
panic!(
"{} did not decode to a valid unified address",
tv.unified_addr
);
}
}
}
}