Add newtypes for transparent keys at the account & external levels.

This updates UnifiedFullViewingKey to conform to ZIP 316, and
adds types that facilitate this support. These types should likely
be factored out from `zcash_client_backend` into `zcash_primitives`
along with the remainder of the existing unified address support.
This commit is contained in:
Kris Nuttycombe 2022-01-20 20:01:33 -07:00
parent 37e6d3a2bc
commit ffc4d0cefb
6 changed files with 271 additions and 243 deletions

View File

@ -22,7 +22,7 @@ use crate::{
};
#[cfg(feature = "transparent-inputs")]
use crate::keys::derive_transparent_address_from_secret_key;
use crate::keys::transparent;
/// Scans a [`Transaction`] for any information that can be decrypted by the accounts in
/// the wallet, and saves it to the wallet.
@ -103,7 +103,7 @@ where
/// };
/// use zcash_proofs::prover::LocalTxProver;
/// use zcash_client_backend::{
/// keys::spending_key,
/// keys::sapling,
/// data_api::wallet::create_spend_to_address,
/// wallet::{AccountId, OvkPolicy},
/// };
@ -128,7 +128,7 @@ where
/// };
///
/// let account = AccountId(0);
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, account);
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, account);
/// let to = extsk.default_address().1.into();
///
/// let data_file = NamedTempFile::new().unwrap();
@ -384,7 +384,7 @@ pub fn shield_transparent_funds<E, N, P, D, R>(
wallet_db: &mut D,
params: &P,
prover: impl TxProver,
sk: &secp256k1::SecretKey,
sk: &transparent::ExternalPrivKey,
extfvk: &ExtendedFullViewingKey,
account: AccountId,
memo: &MemoBytes,
@ -406,10 +406,12 @@ where
.get_target_and_anchor_heights(min_confirmations)
.and_then(|x| x.ok_or_else(|| Error::ScanRequired.into()))?;
// derive the corresponding t-address
let taddr = derive_transparent_address_from_secret_key(sk);
// derive the t-address for the extpubkey at child index 0
let taddr = sk.to_external_pubkey().to_address();
// derive own shielded address from the provided extended spending key
// TODO: this should become the internal change address derived from
// the wallet's UFVK
let z_address = extfvk.default_address().1;
let ovk = extfvk.fvk.ovk;
@ -432,7 +434,7 @@ where
for utxo in &utxos {
builder
.add_transparent_input(*sk, utxo.outpoint.clone(), utxo.txout.clone())
.add_transparent_input(*sk.secret_key(), utxo.outpoint.clone(), utxo.txout.clone())
.map_err(Error::Builder)?;
}

View File

@ -103,11 +103,11 @@ impl<P: consensus::Parameters> AddressCodec<P> for TransparentAddress {
/// };
/// use zcash_client_backend::{
/// encoding::encode_extended_spending_key,
/// keys::spending_key,
/// keys::sapling,
/// wallet::AccountId,
/// };
///
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let encoded = encode_extended_spending_key(HRP_SAPLING_EXTENDED_SPENDING_KEY, &extsk);
/// ```
/// [`ExtendedSpendingKey`]: zcash_primitives::zip32::ExtendedSpendingKey
@ -135,12 +135,12 @@ pub fn decode_extended_spending_key(
/// };
/// use zcash_client_backend::{
/// encoding::encode_extended_full_viewing_key,
/// keys::spending_key,
/// keys::sapling,
/// wallet::AccountId,
/// };
/// use zcash_primitives::zip32::ExtendedFullViewingKey;
///
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let extfvk = ExtendedFullViewingKey::from(&extsk);
/// let encoded = encode_extended_full_viewing_key(HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, &extfvk);
/// ```

View File

@ -1,169 +1,199 @@
//! Helper functions for managing light client key material.
use zcash_primitives::zip32::{ChildIndex, ExtendedSpendingKey};
use crate::wallet::AccountId;
use zcash_primitives::{legacy::TransparentAddress, zip32::ExtendedFullViewingKey};
pub mod sapling {
pub use zcash_primitives::zip32::ExtendedFullViewingKey;
use zcash_primitives::zip32::{ChildIndex, ExtendedSpendingKey};
#[cfg(feature = "transparent-inputs")]
use {
bs58::{self, decode::Error as Bs58Error},
hdwallet::{ExtendedPrivKey, ExtendedPubKey, KeyIndex},
secp256k1::{key::PublicKey, key::SecretKey, Secp256k1},
sha2::{Digest, Sha256},
zcash_primitives::consensus,
};
use crate::wallet::AccountId;
/// 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
///
/// ```
/// use zcash_primitives::{constants::testnet::COIN_TYPE};
/// use zcash_client_backend::{
/// keys::spending_key,
/// wallet::AccountId,
/// };
///
/// let extsk = spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// ```
/// [`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");
/// 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
///
/// ```
/// use zcash_primitives::{constants::testnet::COIN_TYPE};
/// use zcash_client_backend::{
/// keys::sapling,
/// wallet::AccountId,
/// };
///
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// ```
/// [`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");
}
ExtendedSpendingKey::from_path(
&ExtendedSpendingKey::master(&seed),
&[
ChildIndex::Hardened(32),
ChildIndex::Hardened(coin_type),
ChildIndex::Hardened(account.0),
],
)
}
ExtendedSpendingKey::from_path(
&ExtendedSpendingKey::master(&seed),
&[
ChildIndex::Hardened(32),
ChildIndex::Hardened(coin_type),
ChildIndex::Hardened(account.0),
],
)
}
#[cfg(feature = "transparent-inputs")]
pub fn derive_transparent_address_from_secret_key(
secret_key: &secp256k1::key::SecretKey,
) -> TransparentAddress {
let secp = Secp256k1::new();
let pk = PublicKey::from_secret_key(&secp, secret_key);
derive_transparent_address_from_public_key(&pk)
}
pub mod transparent {
use bs58::{self, decode::Error as Bs58Error};
use hdwallet::{ExtendedPrivKey, ExtendedPubKey, KeyIndex};
use secp256k1::key::SecretKey;
use sha2::{Digest, Sha256};
#[cfg(feature = "transparent-inputs")]
pub fn derive_transparent_address_from_public_key(
public_key: &secp256k1::key::PublicKey,
) -> TransparentAddress {
let mut hash160 = ripemd::Ripemd160::new();
hash160.update(Sha256::digest(&public_key.serialize()));
TransparentAddress::PublicKey(*hash160.finalize().as_ref())
}
use crate::wallet::AccountId;
use zcash_primitives::{consensus, legacy::TransparentAddress};
#[cfg(feature = "transparent-inputs")]
pub fn derive_secret_key_from_seed<P: consensus::Parameters>(
params: &P,
seed: &[u8],
account: AccountId,
index: u32,
) -> Result<SecretKey, hdwallet::error::Error> {
let private_key =
derive_extended_private_key_from_seed(params, seed, account, index)?.private_key;
Ok(private_key)
}
/// A type representing a BIP-44 private key at the account path level
/// `m/44'/<coin_type>'/<account>'
#[derive(Clone, Debug)]
pub struct AccountPrivKey(ExtendedPrivKey);
#[cfg(feature = "transparent-inputs")]
pub fn derive_public_key_from_seed<P: consensus::Parameters>(
params: &P,
seed: &[u8],
account: AccountId,
index: u32,
) -> Result<PublicKey, hdwallet::error::Error> {
let private_key = derive_extended_private_key_from_seed(params, seed, account, index)?;
let pub_key = ExtendedPubKey::from_private_key(&private_key);
Ok(pub_key.public_key)
}
impl AccountPrivKey {
/// Perform derivation of the extended private key for the BIP-44 path:
/// `m/44'/<coin_type>'/<account>'
///
/// This produces the extended private key for the external (non-change)
/// address at the specified index for the provided account.
pub fn from_seed<P: consensus::Parameters>(
params: &P,
seed: &[u8],
account: AccountId,
) -> Result<AccountPrivKey, hdwallet::error::Error> {
ExtendedPrivKey::with_seed(&seed)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(44)?)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(params.coin_type())?)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(account.0)?)
.map(AccountPrivKey)
}
/// Perform derivation of the extended private key for the BIP-44 path:
/// `m/44'/<coin_type>'/<account>'
///
/// This produces the extended private key for the external (non-change)
/// address at the specified index for the provided account.
#[cfg(feature = "transparent-inputs")]
pub fn derive_extended_private_key_from_seed<P: consensus::Parameters>(
params: &P,
seed: &[u8],
account: AccountId,
index: u32,
) -> Result<ExtendedPrivKey, hdwallet::error::Error> {
let pk = ExtendedPrivKey::with_seed(&seed)?;
let private_key = pk
.derive_private_key(KeyIndex::hardened_from_normalize_index(44)?)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(params.coin_type())?)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(account.0)?)?
.derive_private_key(KeyIndex::Normal(0))?
.derive_private_key(KeyIndex::Normal(index))?;
Ok(private_key)
}
pub fn to_account_pubkey(&self) -> AccountPubKey {
AccountPubKey(ExtendedPubKey::from_private_key(&self.0))
}
/// Wallet Import Format encoded transparent private key.
#[cfg(feature = "transparent-inputs")]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Wif(pub String);
#[cfg(feature = "transparent-inputs")]
#[derive(Debug)]
pub enum WifError {
Base58(Bs58Error),
InvalidLeadByte(u8),
InvalidTrailingByte(u8),
Secp256k1(secp256k1::Error),
}
#[cfg(feature = "transparent-inputs")]
impl Wif {
/// Encode the provided secret key in Wallet Import Format.
pub fn from_secret_key<P: consensus::Parameters>(
params: &P,
sk: &SecretKey,
compressed: bool,
) -> Self {
let secret_key = sk.as_ref();
let mut wif = [0u8; 34];
wif[0] = params.wif_lead_byte();
wif[1..33].copy_from_slice(secret_key);
if compressed {
wif[33] = 0x01;
Wif(bs58::encode(&wif[..]).with_check().into_string())
} else {
Wif(bs58::encode(&wif[..]).with_check().into_string())
/// Derive BIP-44 private key at the external child path
/// `m/44'/<coin_type>'/<account>'/0/<child_index>
pub fn derive_external_secret_key(
&self,
child_index: u32,
) -> Result<ExternalPrivKey, hdwallet::error::Error> {
self.0
.derive_private_key(KeyIndex::Normal(0))?
.derive_private_key(KeyIndex::Normal(child_index))
.map(ExternalPrivKey)
}
}
pub fn to_secret_key<P: consensus::Parameters>(
&self,
params: &P,
) -> Result<SecretKey, WifError> {
bs58::decode(&self.0)
.with_check(None)
.into_vec()
.map_err(WifError::Base58)
.and_then(|decoded| {
if decoded[0] != params.wif_lead_byte() {
Err(WifError::InvalidLeadByte(decoded[0]))
} else if decoded[33] != 0x01 {
Err(WifError::InvalidTrailingByte(decoded[33]))
} else {
SecretKey::from_slice(&decoded[1..33]).map_err(WifError::Secp256k1)
}
})
/// A type representing a BIP-44 public key at the account path level
/// `m/44'/<coin_type>'/<account>'
#[derive(Clone, Debug)]
pub struct AccountPubKey(ExtendedPubKey);
impl AccountPubKey {
pub fn to_external_pubkey(
&self,
child_index: u32,
) -> Result<ExternalPubKey, hdwallet::error::Error> {
self.0
.derive_public_key(KeyIndex::Normal(0))?
.derive_public_key(KeyIndex::Normal(child_index))
.map(ExternalPubKey)
}
}
/// A type representing a private key at the BIP-44 external child
/// level `m/44'/<coin_type>'/<account>'/0/<child_index>
#[derive(Clone, Debug)]
pub struct ExternalPrivKey(ExtendedPrivKey);
impl ExternalPrivKey {
pub fn to_external_pubkey(&self) -> ExternalPubKey {
ExternalPubKey(ExtendedPubKey::from_private_key(&self.0))
}
pub fn secret_key(&self) -> &secp256k1::key::SecretKey {
&self.0.private_key
}
}
pub(crate) fn pubkey_to_address(pubkey: &secp256k1::key::PublicKey) -> TransparentAddress {
let mut hash160 = ripemd::Ripemd160::new();
hash160.update(Sha256::digest(pubkey.serialize()));
TransparentAddress::PublicKey(*hash160.finalize().as_ref())
}
/// A type representing a public key at the BIP-44 external child
/// level `m/44'/<coin_type>'/<account>'/0/<child_index>
#[derive(Clone, Debug)]
pub struct ExternalPubKey(ExtendedPubKey);
impl ExternalPubKey {
pub fn to_address(&self) -> TransparentAddress {
pubkey_to_address(&self.0.public_key)
}
pub fn public_key(&self) -> &secp256k1::key::PublicKey {
&self.0.public_key
}
}
/// Wallet Import Format encoded transparent private key.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Wif(pub String);
#[derive(Debug)]
pub enum WifError {
Base58(Bs58Error),
InvalidLeadByte(u8),
InvalidTrailingByte(u8),
Secp256k1(secp256k1::Error),
}
impl Wif {
/// Encode the provided secret key in Wallet Import Format.
pub fn from_secret_key<P: consensus::Parameters>(
params: &P,
sk: &SecretKey,
compressed: bool,
) -> Self {
let secret_key = sk.as_ref();
let mut wif = [0u8; 34];
wif[0] = params.wif_lead_byte();
wif[1..33].copy_from_slice(secret_key);
if compressed {
wif[33] = 0x01;
Wif(bs58::encode(&wif[..]).with_check().into_string())
} else {
Wif(bs58::encode(&wif[..]).with_check().into_string())
}
}
pub fn to_secret_key<P: consensus::Parameters>(
&self,
params: &P,
) -> Result<SecretKey, WifError> {
bs58::decode(&self.0)
.with_check(None)
.into_vec()
.map_err(WifError::Base58)
.and_then(|decoded| {
if decoded[0] != params.wif_lead_byte() {
Err(WifError::InvalidLeadByte(decoded[0]))
} else if decoded[33] != 0x01 {
Err(WifError::InvalidTrailingByte(decoded[33]))
} else {
SecretKey::from_slice(&decoded[1..33]).map_err(WifError::Secp256k1)
}
})
}
}
}
@ -172,16 +202,16 @@ impl Wif {
#[derive(Clone, Debug)]
pub struct UnifiedFullViewingKey {
account: AccountId,
transparent: Option<TransparentAddress>,
sapling: Option<ExtendedFullViewingKey>,
transparent: Option<transparent::AccountPubKey>,
sapling: Option<sapling::ExtendedFullViewingKey>,
}
impl UnifiedFullViewingKey {
/// Construct a new unified full viewing key, if the required components are present.
pub fn new(
account: AccountId,
transparent: Option<TransparentAddress>,
sapling: Option<ExtendedFullViewingKey>,
transparent: Option<transparent::AccountPubKey>,
sapling: Option<sapling::ExtendedFullViewingKey>,
) -> Option<UnifiedFullViewingKey> {
if sapling.is_none() {
None
@ -200,34 +230,27 @@ impl UnifiedFullViewingKey {
self.account
}
/// Returns the transparent component of the unified key.
// TODO: make this the pubkey rather than the address to
// permit child derivation
pub fn transparent(&self) -> Option<&TransparentAddress> {
/// Returns the transparent component of the unified key at the
/// BIP44 path `m/44'/<coin_type>'/<account>'`.
pub fn transparent(&self) -> Option<&transparent::AccountPubKey> {
self.transparent.as_ref()
}
/// Returns the Sapling extended full viewing key component of this
/// unified key.
pub fn sapling(&self) -> Option<&ExtendedFullViewingKey> {
pub fn sapling(&self) -> Option<&sapling::ExtendedFullViewingKey> {
self.sapling.as_ref()
}
}
#[cfg(test)]
mod tests {
use super::spending_key;
use super::sapling;
use crate::wallet::AccountId;
#[cfg(feature = "transparent-inputs")]
use {
super::{
derive_public_key_from_seed, derive_secret_key_from_seed,
derive_transparent_address_from_public_key, derive_transparent_address_from_secret_key,
Wif,
},
crate::encoding::AddressCodec,
secp256k1::key::SecretKey,
super::transparent, crate::encoding::AddressCodec, secp256k1::key::SecretKey,
zcash_primitives::consensus::MAIN_NETWORK,
};
@ -240,42 +263,44 @@ mod tests {
#[test]
#[should_panic]
fn spending_key_panics_on_short_seed() {
let _ = spending_key(&[0; 31][..], 0, AccountId(0));
let _ = sapling::spending_key(&[0; 31][..], 0, AccountId(0));
}
#[cfg(feature = "transparent-inputs")]
#[test]
fn sk_to_wif() {
let sk = derive_secret_key_from_seed(&MAIN_NETWORK, &seed(), AccountId(0), 0).unwrap();
let wif = Wif::from_secret_key(&MAIN_NETWORK, &sk, true).0;
let sk = transparent::AccountPrivKey::from_seed(&MAIN_NETWORK, &seed(), AccountId(0))
.unwrap()
.derive_external_secret_key(0)
.unwrap();
let wif = transparent::Wif::from_secret_key(&MAIN_NETWORK, &sk.secret_key(), true).0;
assert_eq!(
wif,
"L4BvDC33yLjMRxipZvdiUmdYeRfZmR8viziwsVwe72zJdGbiJPv2".to_string()
);
}
#[cfg(feature = "transparent-inputs")]
#[test]
fn sk_to_taddr() {
let sk = derive_secret_key_from_seed(&MAIN_NETWORK, &seed(), AccountId(0), 0).unwrap();
let taddr = derive_transparent_address_from_secret_key(&sk).encode(&MAIN_NETWORK);
assert_eq!(taddr, "t1PKtYdJJHhc3Pxowmznkg7vdTwnhEsCvR4".to_string());
}
#[cfg(feature = "transparent-inputs")]
#[test]
fn sk_wif_to_taddr() {
let sk_wif = Wif("L4BvDC33yLjMRxipZvdiUmdYeRfZmR8viziwsVwe72zJdGbiJPv2".to_string());
let sk_wif =
transparent::Wif("L4BvDC33yLjMRxipZvdiUmdYeRfZmR8viziwsVwe72zJdGbiJPv2".to_string());
let sk: SecretKey = (&sk_wif).to_secret_key(&MAIN_NETWORK).expect("invalid wif");
let taddr = derive_transparent_address_from_secret_key(&sk).encode(&MAIN_NETWORK);
let secp = secp256k1::Secp256k1::new();
let pubkey = secp256k1::key::PublicKey::from_secret_key(&secp, &sk);
let taddr = transparent::pubkey_to_address(&pubkey).encode(&MAIN_NETWORK);
assert_eq!(taddr, "t1PKtYdJJHhc3Pxowmznkg7vdTwnhEsCvR4".to_string());
}
#[cfg(feature = "transparent-inputs")]
#[test]
fn pk_from_seed() {
let pk = derive_public_key_from_seed(&MAIN_NETWORK, &seed(), AccountId(0), 0).unwrap();
let hex_value = hex::encode(&pk.serialize());
let pk = transparent::AccountPrivKey::from_seed(&MAIN_NETWORK, &seed(), AccountId(0))
.unwrap()
.derive_external_secret_key(0)
.unwrap()
.to_external_pubkey();
let hex_value = hex::encode(&pk.public_key().serialize());
assert_eq!(
hex_value,
"03b1d7fb28d17c125b504d06b1530097e0a3c76ada184237e3bc0925041230a5af".to_string()
@ -285,8 +310,12 @@ mod tests {
#[cfg(feature = "transparent-inputs")]
#[test]
fn pk_to_taddr() {
let pk = derive_public_key_from_seed(&MAIN_NETWORK, &seed(), AccountId(0), 0).unwrap();
let taddr = derive_transparent_address_from_public_key(&pk).encode(&MAIN_NETWORK);
let pk = transparent::AccountPrivKey::from_seed(&MAIN_NETWORK, &seed(), AccountId(0))
.unwrap()
.derive_external_secret_key(0)
.unwrap()
.to_external_pubkey();
let taddr = pk.to_address().encode(&MAIN_NETWORK);
assert_eq!(taddr, "t1PKtYdJJHhc3Pxowmznkg7vdTwnhEsCvR4".to_string());
}
}

View File

@ -702,14 +702,12 @@ mod tests {
use rusqlite::params;
use zcash_client_backend::{
keys::{spending_key, UnifiedFullViewingKey},
keys::{sapling, UnifiedFullViewingKey},
proto::compact_formats::{CompactBlock, CompactOutput, CompactSpend, CompactTx},
};
#[cfg(feature = "transparent-inputs")]
use zcash_client_backend::keys::{
derive_secret_key_from_seed, derive_transparent_address_from_secret_key,
};
use zcash_client_backend::keys::transparent;
use zcash_primitives::{
block::BlockHash,
@ -758,22 +756,23 @@ mod tests {
) -> (ExtendedFullViewingKey, Option<TransparentAddress>) {
let seed = [0u8; 32];
let extsk = spending_key(&seed, network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&seed, network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let taddr = {
let tsk = derive_secret_key_from_seed(&network(), &seed, AccountId(0), 0).unwrap();
Some(derive_transparent_address_from_secret_key(&tsk))
};
let tkey = Some(
transparent::AccountPrivKey::from_seed(&network(), &seed, AccountId(0))
.unwrap()
.to_account_pubkey()
);
#[cfg(not(feature = "transparent-inputs"))]
let taddr = None;
let tkey = None;
let ufvk =
UnifiedFullViewingKey::new(AccountId(0), taddr.clone(), Some(extfvk.clone())).unwrap();
UnifiedFullViewingKey::new(AccountId(0), tkey.clone(), Some(extfvk.clone())).unwrap();
init_accounts_table(db_data, &[ufvk]).unwrap();
(extfvk, taddr)
(extfvk, tkey.map(|k| k.to_external_pubkey(0).unwrap().to_address()))
}
/// Create a fake CompactBlock at the given height, containing a single output paying

View File

@ -146,7 +146,7 @@ pub fn init_wallet_db<P>(wdb: &WalletDb<P>) -> Result<(), rusqlite::Error> {
///
/// use zcash_client_backend::{
/// keys::{
/// spending_key,
/// sapling,
/// UnifiedFullViewingKey
/// },
/// wallet::AccountId,
@ -163,7 +163,7 @@ pub fn init_wallet_db<P>(wdb: &WalletDb<P>) -> Result<(), rusqlite::Error> {
///
/// let seed = [0u8; 32]; // insecure; replace with a strong random seed
/// let account = AccountId(0);
/// let extsk = spending_key(&seed, Network::TestNetwork.coin_type(), account);
/// let extsk = sapling::spending_key(&seed, Network::TestNetwork.coin_type(), account);
/// let extfvk = ExtendedFullViewingKey::from(&extsk);
/// let ufvk = UnifiedFullViewingKey::new(account, None, Some(extfvk)).unwrap();
/// init_accounts_table(&db_data, &[ufvk]).unwrap();
@ -194,7 +194,11 @@ pub fn init_accounts_table<P: consensus::Parameters>(
let address_str: Option<String> = key
.sapling()
.map(|extfvk| address_from_extfvk(&wdb.params, extfvk));
let taddress_str: Option<String> = key.transparent().map(|taddr| taddr.encode(&wdb.params));
let taddress_str: Option<String> = key.transparent().and_then(|k| {
k.to_external_pubkey(0)
.ok()
.map(|k| k.to_address().encode(&wdb.params))
});
wdb.conn.execute(
"INSERT INTO accounts (account, extfvk, address, transparent_address)
@ -269,12 +273,10 @@ pub fn init_blocks_table<P>(
mod tests {
use tempfile::NamedTempFile;
use zcash_client_backend::keys::{spending_key, UnifiedFullViewingKey};
use zcash_client_backend::keys::{sapling, UnifiedFullViewingKey};
#[cfg(feature = "transparent-inputs")]
use zcash_client_backend::keys::{
derive_secret_key_from_seed, derive_transparent_address_from_secret_key,
};
use zcash_client_backend::keys::transparent;
use zcash_primitives::{
block::BlockHash,
@ -304,21 +306,19 @@ mod tests {
let account = AccountId(0);
// First call with data should initialise the accounts table
let extsk = spending_key(&seed, network().coin_type(), account);
let extsk = sapling::spending_key(&seed, network().coin_type(), account);
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = {
let tsk = derive_secret_key_from_seed(&network(), &seed, account, 0).unwrap();
UnifiedFullViewingKey::new(
account,
Some(derive_transparent_address_from_secret_key(&tsk)),
Some(extfvk),
)
.unwrap()
};
let tkey = Some(
transparent::AccountPrivKey::from_seed(&network(), &seed, account)
.unwrap()
.to_account_pubkey(),
);
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(account, None, Some(extfvk)).unwrap();
let tkey = None;
let ufvk = UnifiedFullViewingKey::new(account, tkey, Some(extfvk)).unwrap();
init_accounts_table(&db_data, &[ufvk.clone()]).unwrap();
@ -363,7 +363,7 @@ mod tests {
let seed = [0u8; 32];
// Add an account to the wallet
let extsk = spending_key(&seed, network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&seed, network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk)).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();

View File

@ -164,14 +164,12 @@ mod tests {
use zcash_client_backend::{
data_api::{chain::scan_cached_blocks, wallet::create_spend_to_address, WalletRead},
keys::{spending_key, UnifiedFullViewingKey},
keys::{sapling, UnifiedFullViewingKey},
wallet::OvkPolicy,
};
#[cfg(feature = "transparent-inputs")]
use zcash_client_backend::keys::{
derive_secret_key_from_seed, derive_transparent_address_from_secret_key,
};
use zcash_client_backend::keys::transparent;
use crate::{
chain::init::init_cache_database,
@ -199,27 +197,27 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add two accounts to the wallet
let extsk0 = spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk1 = spending_key(&[1u8; 32], network().coin_type(), AccountId(1));
let extsk0 = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk1 = sapling::spending_key(&[1u8; 32], network().coin_type(), AccountId(1));
let extfvk0 = ExtendedFullViewingKey::from(&extsk0);
let extfvk1 = ExtendedFullViewingKey::from(&extsk1);
#[cfg(feature = "transparent-inputs")]
let ufvks = {
let tsk0 =
derive_secret_key_from_seed(&network(), &[0u8; 32], AccountId(0), 0).unwrap();
let tsk1 =
derive_secret_key_from_seed(&network(), &[1u8; 32], AccountId(1), 0).unwrap();
let tsk0 = transparent::AccountPrivKey::from_seed(&network(), &[0u8; 32], AccountId(0))
.unwrap();
let tsk1 = transparent::AccountPrivKey::from_seed(&network(), &[1u8; 32], AccountId(1))
.unwrap();
[
UnifiedFullViewingKey::new(
AccountId(0),
Some(derive_transparent_address_from_secret_key(&tsk0)),
Some(tsk0.to_account_pubkey()),
Some(extfvk0),
)
.unwrap(),
UnifiedFullViewingKey::new(
AccountId(1),
Some(derive_transparent_address_from_secret_key(&tsk1)),
Some(tsk1.to_account_pubkey()),
Some(extfvk1),
)
.unwrap(),
@ -276,7 +274,7 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk)).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
@ -316,7 +314,7 @@ mod tests {
.unwrap();
// Add an account to the wallet
let extsk = spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk)).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
@ -358,7 +356,7 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
@ -498,7 +496,7 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
@ -624,7 +622,7 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = spending_key(&[0u8; 32], network.coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network.coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
@ -731,7 +729,7 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();