2018-10-15 07:51:40 -07:00
|
|
|
//! Helper functions for managing light client key material.
|
2022-06-13 19:41:01 -07:00
|
|
|
use zcash_address::unified::{self, Container, Encoding};
|
2022-06-09 14:59:53 -07:00
|
|
|
use zcash_primitives::{
|
|
|
|
consensus,
|
2022-06-13 19:27:55 -07:00
|
|
|
sapling::keys as sapling_keys,
|
2022-06-09 14:59:53 -07:00
|
|
|
zip32::{AccountId, DiversifierIndex},
|
|
|
|
};
|
|
|
|
|
2022-06-27 13:23:33 -07:00
|
|
|
use crate::address::UnifiedAddress;
|
2022-06-09 14:59:53 -07:00
|
|
|
|
2022-01-24 17:02:25 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-06-09 14:59:53 -07:00
|
|
|
use zcash_primitives::legacy::keys::{self as legacy, IncomingViewingKey};
|
2018-10-15 07:51:40 -07:00
|
|
|
|
2022-01-20 19:01:33 -08:00
|
|
|
pub mod sapling {
|
2022-01-24 17:02:25 -08:00
|
|
|
use zcash_primitives::zip32::{AccountId, ChildIndex};
|
2022-01-21 17:20:12 -08:00
|
|
|
pub use zcash_primitives::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey};
|
2020-12-22 06:10:13 -08:00
|
|
|
|
2022-01-20 19:01:33 -08:00
|
|
|
/// Derives the ZIP 32 [`ExtendedSpendingKey`] for a given coin type and account from the
|
|
|
|
/// given seed.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `seed` is shorter than 32 bytes.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2022-01-24 17:02:25 -08:00
|
|
|
/// use zcash_primitives::{
|
|
|
|
/// constants::testnet::COIN_TYPE,
|
|
|
|
/// zip32::AccountId,
|
|
|
|
/// };
|
2022-01-20 19:01:33 -08:00
|
|
|
/// use zcash_client_backend::{
|
|
|
|
/// keys::sapling,
|
|
|
|
/// };
|
|
|
|
///
|
2022-02-10 08:47:42 -08:00
|
|
|
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId::from(0));
|
2022-01-20 19:01:33 -08:00
|
|
|
/// ```
|
|
|
|
/// [`ExtendedSpendingKey`]: zcash_primitives::zip32::ExtendedSpendingKey
|
|
|
|
pub fn spending_key(seed: &[u8], coin_type: u32, account: AccountId) -> ExtendedSpendingKey {
|
|
|
|
if seed.len() < 32 {
|
|
|
|
panic!("ZIP 32 seeds MUST be at least 32 bytes");
|
|
|
|
}
|
2021-10-04 13:09:02 -07:00
|
|
|
|
2022-01-20 19:01:33 -08:00
|
|
|
ExtendedSpendingKey::from_path(
|
2022-02-01 13:02:07 -08:00
|
|
|
&ExtendedSpendingKey::master(seed),
|
2022-01-20 19:01:33 -08:00
|
|
|
&[
|
|
|
|
ChildIndex::Hardened(32),
|
|
|
|
ChildIndex::Hardened(coin_type),
|
2022-02-10 08:47:42 -08:00
|
|
|
ChildIndex::Hardened(account.into()),
|
2022-01-20 19:01:33 -08:00
|
|
|
],
|
|
|
|
)
|
2019-09-17 10:03:58 -07:00
|
|
|
}
|
2018-10-15 07:51:40 -07:00
|
|
|
}
|
2019-09-17 15:58:14 -07:00
|
|
|
|
2022-06-09 14:59:53 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
fn to_transparent_child_index(j: DiversifierIndex) -> Option<u32> {
|
|
|
|
let (low_4_bytes, rest) = j.0.split_at(4);
|
|
|
|
let transparent_j = u32::from_le_bytes(low_4_bytes.try_into().unwrap());
|
|
|
|
if transparent_j > (0x7FFFFFFF) || rest.iter().any(|b| b != &0) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(transparent_j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 18:01:32 -08:00
|
|
|
#[derive(Debug)]
|
2022-02-01 10:37:43 -08:00
|
|
|
#[doc(hidden)]
|
2022-01-21 12:47:03 -08:00
|
|
|
pub enum DerivationError {
|
2022-08-24 07:56:58 -07:00
|
|
|
Orchard(orchard::zip32::Error),
|
2022-01-21 12:47:03 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
Transparent(hdwallet::error::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A set of viewing keys that are all associated with a single
|
|
|
|
/// ZIP-0032 account identifier.
|
|
|
|
#[derive(Clone, Debug)]
|
2022-02-01 10:37:43 -08:00
|
|
|
#[doc(hidden)]
|
2022-01-21 12:47:03 -08:00
|
|
|
pub struct UnifiedSpendingKey {
|
|
|
|
account: AccountId,
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-01-24 17:02:25 -08:00
|
|
|
transparent: legacy::AccountPrivKey,
|
2022-01-21 17:20:12 -08:00
|
|
|
sapling: sapling::ExtendedSpendingKey,
|
2022-08-24 07:56:58 -07:00
|
|
|
orchard: orchard::keys::SpendingKey,
|
2022-01-21 12:47:03 -08:00
|
|
|
}
|
|
|
|
|
2022-02-01 10:37:43 -08:00
|
|
|
#[doc(hidden)]
|
2022-01-21 12:47:03 -08:00
|
|
|
impl UnifiedSpendingKey {
|
|
|
|
pub fn from_seed<P: consensus::Parameters>(
|
|
|
|
params: &P,
|
|
|
|
seed: &[u8],
|
|
|
|
account: AccountId,
|
|
|
|
) -> Result<UnifiedSpendingKey, DerivationError> {
|
|
|
|
if seed.len() < 32 {
|
|
|
|
panic!("ZIP 32 seeds MUST be at least 32 bytes");
|
|
|
|
}
|
|
|
|
|
2022-08-24 07:56:58 -07:00
|
|
|
let orchard =
|
|
|
|
orchard::keys::SpendingKey::from_zip32_seed(seed, params.coin_type(), account.into())
|
|
|
|
.map_err(DerivationError::Orchard)?;
|
|
|
|
|
2022-01-21 12:47:03 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-01-24 17:02:25 -08:00
|
|
|
let transparent = legacy::AccountPrivKey::from_seed(params, seed, account)
|
2022-01-21 12:47:03 -08:00
|
|
|
.map_err(DerivationError::Transparent)?;
|
|
|
|
|
|
|
|
Ok(UnifiedSpendingKey {
|
|
|
|
account,
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
transparent,
|
|
|
|
sapling: sapling::spending_key(seed, params.coin_type(), account),
|
2022-08-24 07:56:58 -07:00
|
|
|
orchard,
|
2022-01-21 12:47:03 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_unified_full_viewing_key(&self) -> UnifiedFullViewingKey {
|
|
|
|
UnifiedFullViewingKey {
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
transparent: Some(self.transparent.to_account_pubkey()),
|
2022-06-13 19:27:55 -07:00
|
|
|
sapling: Some(sapling::ExtendedFullViewingKey::from(&self.sapling).into()),
|
2022-08-24 07:56:58 -07:00
|
|
|
orchard: Some((&self.orchard).into()),
|
2022-06-13 19:41:01 -07:00
|
|
|
unknown: vec![],
|
2022-01-21 12:47:03 -08:00
|
|
|
}
|
|
|
|
}
|
2022-01-21 17:20:12 -08:00
|
|
|
|
|
|
|
pub fn account(&self) -> AccountId {
|
|
|
|
self.account
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the transparent component of the unified key at the
|
|
|
|
/// BIP44 path `m/44'/<coin_type>'/<account>'`.
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-01-24 17:02:25 -08:00
|
|
|
pub fn transparent(&self) -> &legacy::AccountPrivKey {
|
2022-01-21 18:01:32 -08:00
|
|
|
&self.transparent
|
2022-01-21 17:20:12 -08:00
|
|
|
}
|
|
|
|
|
2022-08-24 07:56:58 -07:00
|
|
|
/// Returns the Sapling extended spending key component of this unified spending key.
|
2022-01-21 18:01:32 -08:00
|
|
|
pub fn sapling(&self) -> &sapling::ExtendedSpendingKey {
|
|
|
|
&self.sapling
|
2022-01-21 17:20:12 -08:00
|
|
|
}
|
2022-08-24 07:56:58 -07:00
|
|
|
|
|
|
|
/// Returns the Orchard spending key component of this unified spending key.
|
|
|
|
pub fn orchard(&self) -> &orchard::keys::SpendingKey {
|
|
|
|
&self.orchard
|
|
|
|
}
|
2022-01-21 12:47:03 -08:00
|
|
|
}
|
|
|
|
|
2021-10-04 13:09:02 -07:00
|
|
|
/// A set of viewing keys that are all associated with a single
|
|
|
|
/// ZIP-0032 account identifier.
|
|
|
|
#[derive(Clone, Debug)]
|
2022-02-01 10:37:43 -08:00
|
|
|
#[doc(hidden)]
|
2021-10-04 13:09:02 -07:00
|
|
|
pub struct UnifiedFullViewingKey {
|
2022-01-20 19:55:34 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-01-24 17:02:25 -08:00
|
|
|
transparent: Option<legacy::AccountPubKey>,
|
2022-06-13 19:27:55 -07:00
|
|
|
sapling: Option<sapling_keys::DiversifiableFullViewingKey>,
|
2022-06-13 19:41:01 -07:00
|
|
|
orchard: Option<orchard::keys::FullViewingKey>,
|
|
|
|
unknown: Vec<(u32, Vec<u8>)>,
|
2021-10-04 13:09:02 -07:00
|
|
|
}
|
|
|
|
|
2022-02-01 10:37:43 -08:00
|
|
|
#[doc(hidden)]
|
2021-10-04 13:09:02 -07:00
|
|
|
impl UnifiedFullViewingKey {
|
|
|
|
/// Construct a new unified full viewing key, if the required components are present.
|
|
|
|
pub fn new(
|
2022-01-24 17:02:25 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")] transparent: Option<legacy::AccountPubKey>,
|
2022-06-13 19:27:55 -07:00
|
|
|
sapling: Option<sapling_keys::DiversifiableFullViewingKey>,
|
2022-06-13 19:41:01 -07:00
|
|
|
orchard: Option<orchard::keys::FullViewingKey>,
|
2021-10-04 13:09:02 -07:00
|
|
|
) -> Option<UnifiedFullViewingKey> {
|
|
|
|
if sapling.is_none() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(UnifiedFullViewingKey {
|
2022-01-20 19:55:34 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2021-10-04 13:09:02 -07:00
|
|
|
transparent,
|
|
|
|
sapling,
|
2022-06-13 19:41:01 -07:00
|
|
|
orchard,
|
|
|
|
// We don't allow constructing new UFVKs with unknown items, but we store
|
|
|
|
// this to allow parsing such UFVKs.
|
|
|
|
unknown: vec![],
|
2021-10-04 13:09:02 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 19:41:01 -07:00
|
|
|
/// Parses a `UnifiedFullViewingKey` from its [ZIP 316] string encoding.
|
|
|
|
///
|
|
|
|
/// [ZIP 316]: https://zips.z.cash/zip-0316
|
2022-06-13 18:56:46 -07:00
|
|
|
pub fn decode<P: consensus::Parameters>(params: &P, encoding: &str) -> Result<Self, String> {
|
2022-06-13 19:41:01 -07:00
|
|
|
let (net, ufvk) = unified::Ufvk::decode(encoding).map_err(|e| e.to_string())?;
|
2022-06-27 13:23:33 -07:00
|
|
|
let expected_net = params.address_network().expect("Unrecognized network");
|
2022-06-13 19:41:01 -07:00
|
|
|
if net != expected_net {
|
|
|
|
return Err(format!(
|
|
|
|
"UFVK is for network {:?} but we expected {:?}",
|
|
|
|
net, expected_net,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut orchard = None;
|
|
|
|
let mut sapling = None;
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
let mut transparent = None;
|
|
|
|
|
|
|
|
// We can use as-parsed order here for efficiency, because we're breaking out the
|
|
|
|
// receivers we support from the unknown receivers.
|
|
|
|
let unknown = ufvk
|
|
|
|
.items_as_parsed()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|receiver| match receiver {
|
|
|
|
unified::Fvk::Orchard(data) => orchard::keys::FullViewingKey::from_bytes(data)
|
|
|
|
.ok_or("Invalid Orchard FVK in Unified FVK")
|
|
|
|
.map(|addr| {
|
|
|
|
orchard = Some(addr);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.transpose(),
|
|
|
|
unified::Fvk::Sapling(data) => {
|
|
|
|
sapling_keys::DiversifiableFullViewingKey::from_bytes(data)
|
|
|
|
.ok_or("Invalid Sapling FVK in Unified FVK")
|
|
|
|
.map(|pa| {
|
|
|
|
sapling = Some(pa);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
2022-06-10 15:53:52 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-06-13 19:41:01 -07:00
|
|
|
unified::Fvk::P2pkh(data) => legacy::AccountPubKey::deserialize(data)
|
|
|
|
.map_err(|_| "Invalid transparent FVK in Unified FVK")
|
|
|
|
.map(|tfvk| {
|
|
|
|
transparent = Some(tfvk);
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.transpose(),
|
|
|
|
#[cfg(not(feature = "transparent-inputs"))]
|
|
|
|
unified::Fvk::P2pkh(data) => {
|
|
|
|
Some(Ok((unified::Typecode::P2pkh.into(), data.to_vec())))
|
|
|
|
}
|
|
|
|
unified::Fvk::Unknown { typecode, data } => Some(Ok((*typecode, data.clone()))),
|
2022-06-10 15:53:52 -07:00
|
|
|
})
|
2022-06-13 19:41:01 -07:00
|
|
|
.collect::<Result<_, _>>()?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
transparent,
|
|
|
|
sapling,
|
|
|
|
orchard,
|
|
|
|
unknown,
|
|
|
|
})
|
2022-06-10 15:53:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the string encoding of this `UnifiedFullViewingKey` for the given network.
|
|
|
|
pub fn encode<P: consensus::Parameters>(&self, params: &P) -> String {
|
2022-06-13 19:41:01 -07:00
|
|
|
let items = std::iter::empty()
|
|
|
|
.chain(
|
|
|
|
self.orchard
|
|
|
|
.as_ref()
|
|
|
|
.map(|fvk| fvk.to_bytes())
|
|
|
|
.map(unified::Fvk::Orchard),
|
|
|
|
)
|
|
|
|
.chain(
|
|
|
|
self.sapling
|
|
|
|
.as_ref()
|
|
|
|
.map(|dfvk| dfvk.to_bytes())
|
|
|
|
.map(unified::Fvk::Sapling),
|
|
|
|
)
|
|
|
|
.chain(
|
|
|
|
self.unknown
|
|
|
|
.iter()
|
|
|
|
.map(|(typecode, data)| unified::Fvk::Unknown {
|
|
|
|
typecode: *typecode,
|
|
|
|
data: data.clone(),
|
|
|
|
}),
|
|
|
|
);
|
2022-06-10 15:53:52 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-06-13 19:41:01 -07:00
|
|
|
let items = items.chain(
|
|
|
|
self.transparent
|
|
|
|
.as_ref()
|
|
|
|
.map(|tfvk| tfvk.serialize().try_into().unwrap())
|
|
|
|
.map(unified::Fvk::P2pkh),
|
|
|
|
);
|
2022-06-10 15:53:52 -07:00
|
|
|
|
2022-06-13 19:41:01 -07:00
|
|
|
let ufvk = unified::Ufvk::try_from_items(items.collect())
|
|
|
|
.expect("UnifiedFullViewingKey should only be constructed safely");
|
2022-06-27 13:23:33 -07:00
|
|
|
ufvk.encode(¶ms.address_network().expect("Unrecognized network"))
|
2022-06-10 15:53:52 -07:00
|
|
|
}
|
|
|
|
|
2022-01-20 19:01:33 -08:00
|
|
|
/// Returns the transparent component of the unified key at the
|
|
|
|
/// BIP44 path `m/44'/<coin_type>'/<account>'`.
|
2022-02-01 10:37:43 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-01-24 17:02:25 -08:00
|
|
|
pub fn transparent(&self) -> Option<&legacy::AccountPubKey> {
|
2021-10-04 13:09:02 -07:00
|
|
|
self.transparent.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-06-13 19:27:55 -07:00
|
|
|
/// Returns the Sapling diversifiable full viewing key component of this unified key.
|
|
|
|
pub fn sapling(&self) -> Option<&sapling_keys::DiversifiableFullViewingKey> {
|
2021-10-04 13:09:02 -07:00
|
|
|
self.sapling.as_ref()
|
|
|
|
}
|
2022-06-09 14:59:53 -07:00
|
|
|
|
2022-08-24 07:56:58 -07:00
|
|
|
/// Returns the Orchard full viewing key component of this unified key.
|
|
|
|
pub fn orchard(&self) -> Option<&orchard::keys::FullViewingKey> {
|
|
|
|
self.orchard.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-06-09 14:59:53 -07:00
|
|
|
/// Attempts to derive the Unified Address for the given diversifier index.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the specified index does not produce a valid diversifier.
|
|
|
|
// TODO: Allow filtering down by receiver types?
|
|
|
|
pub fn address(&self, j: DiversifierIndex) -> Option<UnifiedAddress> {
|
|
|
|
let sapling = if let Some(extfvk) = self.sapling.as_ref() {
|
|
|
|
Some(extfvk.address(j)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
let transparent = if let Some(tfvk) = self.transparent.as_ref() {
|
|
|
|
match to_transparent_child_index(j) {
|
|
|
|
Some(transparent_j) => match tfvk
|
|
|
|
.derive_external_ivk()
|
|
|
|
.and_then(|tivk| tivk.derive_address(transparent_j))
|
|
|
|
{
|
|
|
|
Ok(taddr) => Some(taddr),
|
|
|
|
Err(_) => return None,
|
|
|
|
},
|
|
|
|
// Diversifier doesn't generate a valid transparent child index.
|
|
|
|
None => return None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
#[cfg(not(feature = "transparent-inputs"))]
|
|
|
|
let transparent = None;
|
|
|
|
|
|
|
|
UnifiedAddress::from_receivers(None, sapling, transparent)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches the diversifier space starting at diversifier index `j` for one which will
|
|
|
|
/// produce a valid diversifier, and return the Unified Address constructed using that
|
|
|
|
/// diversifier along with the index at which the valid diversifier was found.
|
|
|
|
///
|
|
|
|
/// Returns `None` if no valid diversifier exists
|
|
|
|
pub fn find_address(
|
|
|
|
&self,
|
|
|
|
mut j: DiversifierIndex,
|
|
|
|
) -> Option<(UnifiedAddress, DiversifierIndex)> {
|
|
|
|
// If we need to generate a transparent receiver, check that the user has not
|
|
|
|
// specified an invalid transparent child index, from which we can never search to
|
|
|
|
// find a valid index.
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
if self.transparent.is_some() && to_transparent_child_index(j).is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a working diversifier and construct the associated address.
|
|
|
|
loop {
|
|
|
|
let res = self.address(j);
|
|
|
|
if let Some(ua) = res {
|
|
|
|
break Some((ua, j));
|
|
|
|
}
|
|
|
|
if j.increment().is_err() {
|
|
|
|
break None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Unified Address corresponding to the smallest valid diversifier index,
|
|
|
|
/// along with that index.
|
|
|
|
pub fn default_address(&self) -> (UnifiedAddress, DiversifierIndex) {
|
|
|
|
self.find_address(DiversifierIndex::new())
|
|
|
|
.expect("UFVK should have at least one valid diversifier")
|
|
|
|
}
|
2021-10-04 13:09:02 -07:00
|
|
|
}
|
|
|
|
|
2019-09-17 15:58:14 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-06-10 15:53:52 -07:00
|
|
|
use super::{sapling, UnifiedFullViewingKey};
|
|
|
|
use zcash_primitives::{
|
|
|
|
consensus::MAIN_NETWORK,
|
|
|
|
zip32::{AccountId, ExtendedFullViewingKey},
|
|
|
|
};
|
2019-09-17 15:58:14 -07:00
|
|
|
|
2021-02-12 14:54:59 -08:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
use {
|
2022-01-24 17:02:25 -08:00
|
|
|
crate::encoding::AddressCodec,
|
2022-06-28 14:47:21 -07:00
|
|
|
zcash_primitives::legacy::{
|
|
|
|
self,
|
|
|
|
keys::{AccountPrivKey, IncomingViewingKey},
|
|
|
|
},
|
2021-02-12 14:54:59 -08:00
|
|
|
};
|
|
|
|
|
2021-10-04 13:09:02 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2021-03-31 18:04:38 -07:00
|
|
|
fn seed() -> Vec<u8> {
|
|
|
|
let seed_hex = "6ef5f84def6f4b9d38f466586a8380a38593bd47c8cda77f091856176da47f26b5bd1c8d097486e5635df5a66e820d28e1d73346f499801c86228d43f390304f";
|
|
|
|
hex::decode(&seed_hex).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-09-17 15:58:14 -07:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn spending_key_panics_on_short_seed() {
|
2022-02-10 08:47:42 -08:00
|
|
|
let _ = sapling::spending_key(&[0; 31][..], 0, AccountId::from(0));
|
2019-09-17 15:58:14 -07:00
|
|
|
}
|
2021-02-12 14:54:59 -08:00
|
|
|
|
2021-03-31 18:04:38 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
#[test]
|
|
|
|
fn pk_to_taddr() {
|
2022-02-10 08:47:42 -08:00
|
|
|
let taddr =
|
|
|
|
legacy::keys::AccountPrivKey::from_seed(&MAIN_NETWORK, &seed(), AccountId::from(0))
|
|
|
|
.unwrap()
|
|
|
|
.to_account_pubkey()
|
|
|
|
.derive_external_ivk()
|
|
|
|
.unwrap()
|
|
|
|
.derive_address(0)
|
|
|
|
.unwrap()
|
|
|
|
.encode(&MAIN_NETWORK);
|
2021-03-31 18:04:38 -07:00
|
|
|
assert_eq!(taddr, "t1PKtYdJJHhc3Pxowmznkg7vdTwnhEsCvR4".to_string());
|
|
|
|
}
|
2022-06-10 15:53:52 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ufvk_round_trip() {
|
|
|
|
let account = 0.into();
|
|
|
|
|
2022-06-13 19:41:01 -07:00
|
|
|
let orchard = {
|
|
|
|
let sk = orchard::keys::SpendingKey::from_zip32_seed(&[0; 32], 0, 0).unwrap();
|
|
|
|
Some(orchard::keys::FullViewingKey::from(&sk))
|
|
|
|
};
|
|
|
|
|
2022-06-10 15:53:52 -07:00
|
|
|
let sapling = {
|
|
|
|
let extsk = sapling::spending_key(&[0; 32], 0, account);
|
2022-06-13 19:27:55 -07:00
|
|
|
Some(ExtendedFullViewingKey::from(&extsk).into())
|
2022-06-10 15:53:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
2022-06-28 14:47:21 -07:00
|
|
|
let transparent = {
|
|
|
|
let privkey =
|
|
|
|
AccountPrivKey::from_seed(&MAIN_NETWORK, &[0; 32], AccountId::from(0)).unwrap();
|
|
|
|
Some(privkey.to_account_pubkey())
|
|
|
|
};
|
2022-06-10 15:53:52 -07:00
|
|
|
|
|
|
|
let ufvk = UnifiedFullViewingKey::new(
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
transparent,
|
|
|
|
sapling,
|
2022-06-13 19:41:01 -07:00
|
|
|
orchard,
|
2022-06-10 15:53:52 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-06-28 14:47:21 -07:00
|
|
|
let encoded = ufvk.encode(&MAIN_NETWORK);
|
|
|
|
|
|
|
|
// test encoded form against known values
|
|
|
|
let encoded_with_t = "uview1tg6rpjgju2s2j37gkgjq79qrh5lvzr6e0ed3n4sf4hu5qd35vmsh7avl80xa6mx7ryqce9hztwaqwrdthetpy4pc0kce25x453hwcmax02p80pg5savlg865sft9reat07c5vlactr6l2pxtlqtqunt2j9gmvr8spcuzf07af80h5qmut38h0gvcfa9k4rwujacwwca9vu8jev7wq6c725huv8qjmhss3hdj2vh8cfxhpqcm2qzc34msyrfxk5u6dqttt4vv2mr0aajreww5yufpk0gn4xkfm888467k7v6fmw7syqq6cceu078yw8xja502jxr0jgum43lhvpzmf7eu5dmnn6cr6f7p43yw8znzgxg598mllewnx076hljlvynhzwn5es94yrv65tdg3utuz2u3sras0wfcq4adxwdvlk387d22g3q98t5z74quw2fa4wed32escx8dwh4mw35t4jwf35xyfxnu83mk5s4kw2glkgsshmxk";
|
|
|
|
let _encoded_no_t = "uview12z384wdq76ceewlsu0esk7d97qnd23v2qnvhujxtcf2lsq8g4hwzpx44fwxssnm5tg8skyh4tnc8gydwxefnnm0hd0a6c6etmj0pp9jqkdsllkr70u8gpf7ndsfqcjlqn6dec3faumzqlqcmtjf8vp92h7kj38ph2786zx30hq2wru8ae3excdwc8w0z3t9fuw7mt7xy5sn6s4e45kwm0cjp70wytnensgdnev286t3vew3yuwt2hcz865y037k30e428dvgne37xvyeal2vu8yjnznphf9t2rw3gdp0hk5zwq00ws8f3l3j5n3qkqgsyzrwx4qzmgq0xwwk4vz2r6vtsykgz089jncvycmem3535zjwvvtvjw8v98y0d5ydwte575gjm7a7k";
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
assert_eq!(encoded, encoded_with_t);
|
|
|
|
#[cfg(not(feature = "transparent-inputs"))]
|
|
|
|
assert_eq!(encoded, _encoded_no_t);
|
|
|
|
|
|
|
|
let decoded = UnifiedFullViewingKey::decode(&MAIN_NETWORK, &encoded).unwrap();
|
|
|
|
let reencoded = decoded.encode(&MAIN_NETWORK);
|
|
|
|
assert_eq!(encoded, reencoded);
|
|
|
|
|
2022-06-10 15:53:52 -07:00
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
assert_eq!(
|
|
|
|
decoded.transparent.map(|t| t.serialize()),
|
2022-06-28 14:47:21 -07:00
|
|
|
ufvk.transparent.as_ref().map(|t| t.serialize()),
|
2022-06-10 15:53:52 -07:00
|
|
|
);
|
2022-06-13 19:27:55 -07:00
|
|
|
assert_eq!(
|
|
|
|
decoded.sapling.map(|s| s.to_bytes()),
|
|
|
|
ufvk.sapling.map(|s| s.to_bytes()),
|
|
|
|
);
|
2022-06-13 19:41:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
decoded.orchard.map(|o| o.to_bytes()),
|
|
|
|
ufvk.orchard.map(|o| o.to_bytes()),
|
|
|
|
);
|
2022-06-28 14:47:21 -07:00
|
|
|
|
|
|
|
let decoded_with_t = UnifiedFullViewingKey::decode(&MAIN_NETWORK, encoded_with_t).unwrap();
|
|
|
|
#[cfg(feature = "transparent-inputs")]
|
|
|
|
assert_eq!(
|
|
|
|
decoded_with_t.transparent.map(|t| t.serialize()),
|
|
|
|
ufvk.transparent.as_ref().map(|t| t.serialize()),
|
|
|
|
);
|
|
|
|
#[cfg(not(feature = "transparent-inputs"))]
|
|
|
|
assert_eq!(decoded_with_t.unknown.len(), 1);
|
2022-06-10 15:53:52 -07:00
|
|
|
}
|
2019-09-17 15:58:14 -07:00
|
|
|
}
|