Move ExternalPrivKey, ExternalPubKey to zcash_primitives.

This commit is contained in:
therealyingtong 2022-01-21 16:27:17 +08:00 committed by Kris Nuttycombe
parent 1f9b9fc147
commit a4c9f53a3a
6 changed files with 59 additions and 48 deletions

View File

@ -8,6 +8,7 @@ use zcash_primitives::{
components::{amount::DEFAULT_FEE, Amount},
Transaction,
},
transparent,
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
};
@ -21,9 +22,6 @@ use crate::{
zip321::{Payment, TransactionRequest},
};
#[cfg(feature = "transparent-inputs")]
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.
pub fn decrypt_and_store_transaction<N, E, P, D>(

View File

@ -48,10 +48,12 @@ pub mod transparent {
use bs58::{self, decode::Error as Bs58Error};
use hdwallet::{ExtendedPrivKey, ExtendedPubKey, KeyIndex};
use secp256k1::key::SecretKey;
use sha2::{Digest, Sha256};
use crate::wallet::AccountId;
use zcash_primitives::{consensus, legacy::TransparentAddress};
use zcash_primitives::{
consensus,
transparent::{ExternalPrivKey, ExternalPubKey},
};
/// A type representing a BIP-44 private key at the account path level
/// `m/44'/<coin_type>'/<account>'
@ -128,48 +130,6 @@ pub mod transparent {
}
}
/// 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 {
/// Returns the external public key corresponding to this private key
pub fn to_external_pubkey(&self) -> ExternalPubKey {
ExternalPubKey(ExtendedPubKey::from_private_key(&self.0))
}
/// Extracts the secp256k1 secret key component
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 {
/// Returns the transparent address corresponding to
/// this public key.
pub fn to_address(&self) -> TransparentAddress {
pubkey_to_address(&self.0.public_key)
}
/// Returns the secp256k1::key::PublicKey component of
/// this 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);
@ -383,7 +343,7 @@ mod tests {
let sk: SecretKey = (&sk_wif).to_secret_key(&MAIN_NETWORK).expect("invalid wif");
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);
let taddr = zcash_primitives::transparent::pubkey_to_address(&pubkey).encode(&MAIN_NETWORK);
assert_eq!(taddr, "t1PKtYdJJHhc3Pxowmznkg7vdTwnhEsCvR4".to_string());
}

View File

@ -23,6 +23,12 @@ use zcash_primitives::{
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct AccountId(pub u32);
impl From<u32> for AccountId {
fn from(id: u32) -> Self {
Self(id)
}
}
impl Default for AccountId {
fn default() -> Self {
AccountId(0)

View File

@ -28,6 +28,7 @@ equihash = { version = "0.1", path = "../components/equihash" }
ff = "0.11"
fpe = "0.5"
group = "0.11"
hdwallet = { version = "0.3.0", optional = true }
hex = "0.4"
incrementalmerkletree = "0.2"
jubjub = "0.8"

View File

@ -17,6 +17,7 @@ pub mod memo;
pub mod merkle_tree;
pub mod sapling;
pub mod transaction;
pub mod transparent;
pub mod zip32;
pub mod zip339;

View File

@ -0,0 +1,45 @@
use crate::legacy::TransparentAddress;
use hdwallet::{ExtendedPrivKey, ExtendedPubKey};
use sha2::{Digest, Sha256};
/// 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(pub ExtendedPrivKey);
impl ExternalPrivKey {
/// Returns the external public key corresponding to this private key
pub fn to_external_pubkey(&self) -> ExternalPubKey {
ExternalPubKey(ExtendedPubKey::from_private_key(&self.0))
}
/// Extracts the secp256k1 secret key component
pub fn secret_key(&self) -> &secp256k1::key::SecretKey {
&self.0.private_key
}
}
pub fn pubkey_to_address(pubkey: &secp256k1::key::PublicKey) -> TransparentAddress {
let mut hash160 = ripemd160::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(pub ExtendedPubKey);
impl ExternalPubKey {
/// Returns the transparent address corresponding to
/// this public key.
pub fn to_address(&self) -> TransparentAddress {
pubkey_to_address(&self.0.public_key)
}
/// Returns the secp256k1::key::PublicKey component of
/// this public key.
pub fn public_key(&self) -> &secp256k1::key::PublicKey {
&self.0.public_key
}
}