From 65f9655b407971faca2d00e681d1e57e0dde656c Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 19 Sep 2019 11:44:27 -0700 Subject: [PATCH] transparent address prefix based on chain --- src/address.rs | 8 ++------ src/lightclient.rs | 24 +++++++++++++++++++++--- src/lightwallet/mod.rs | 16 +++++++++------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/address.rs b/src/address.rs index 6e4df02..69f3b26 100644 --- a/src/address.rs +++ b/src/address.rs @@ -5,10 +5,6 @@ use zcash_primitives::primitives::PaymentAddress; use zcash_client_backend::encoding::{decode_payment_address, decode_transparent_address}; use zcash_primitives::legacy::TransparentAddress; -use zcash_client_backend::constants::testnet::{ - B58_PUBKEY_ADDRESS_PREFIX, B58_SCRIPT_ADDRESS_PREFIX, -}; - /// An address that funds can be sent to. pub enum RecipientAddress { Shielded(PaymentAddress), @@ -28,7 +24,7 @@ impl From for RecipientAddress { } impl RecipientAddress { - pub fn from_str(s: &str, hrp_sapling_address: &str) -> Option { + pub fn from_str(s: &str, hrp_sapling_address: &str, b58_pubkey_address: [u8; 2], b58_script_address: [u8; 2]) -> Option { // Try to match a sapling z address if let Some(pa) = match decode_payment_address(hrp_sapling_address, s) { Ok(ret) => ret, @@ -37,7 +33,7 @@ impl RecipientAddress { { Some(RecipientAddress::Shielded(pa)) // Matched a shielded address } else if let Some(addr) = match decode_transparent_address( - &B58_PUBKEY_ADDRESS_PREFIX, &B58_SCRIPT_ADDRESS_PREFIX, s) { + &b58_pubkey_address, &b58_script_address, s) { Ok(ret) => ret, Err(_) => None } diff --git a/src/lightclient.rs b/src/lightclient.rs index 8c905dd..23d9d71 100644 --- a/src/lightclient.rs +++ b/src/lightclient.rs @@ -123,6 +123,24 @@ impl LightClientConfig { } } + pub fn base58_pubkey_address(&self) -> [u8; 2] { + match &self.chain_name[..] { + "main" => mainnet::B58_PUBKEY_ADDRESS_PREFIX, + "test" => testnet::B58_PUBKEY_ADDRESS_PREFIX, + "regtest" => regtest::B58_PUBKEY_ADDRESS_PREFIX, + c => panic!("Unknown chain {}", c) + } + } + + + pub fn base58_script_address(&self) -> [u8; 2] { + match &self.chain_name[..] { + "main" => mainnet::B58_SCRIPT_ADDRESS_PREFIX, + "test" => testnet::B58_SCRIPT_ADDRESS_PREFIX, + "regtest" => regtest::B58_SCRIPT_ADDRESS_PREFIX, + c => panic!("Unknown chain {}", c) + } + } } pub struct LightClient { @@ -212,7 +230,7 @@ impl LightClient { // Collect t addresses let t_addresses = self.wallet.tkeys.iter().map( |sk| { - LightWallet::address_from_sk(&sk) + self.wallet.address_from_sk(&sk) }).collect::>(); object!{ @@ -234,7 +252,7 @@ impl LightClient { // Collect t addresses let t_addresses = self.wallet.tkeys.iter().map( |sk| { - let address = LightWallet::address_from_sk(&sk); + let address = self.wallet.address_from_sk(&sk); // Get the balance for this address let balance = self.wallet.tbalance(Some(address.clone())); @@ -547,7 +565,7 @@ impl LightClient { // We'll also fetch all the txids that our transparent addresses are involved with // TODO: Use for all t addresses - let address = LightWallet::address_from_sk(&self.wallet.tkeys[0]); + let address = self.wallet.address_from_sk(&self.wallet.tkeys[0]); let wallet = self.wallet.clone(); self.fetch_transparent_txids(address, last_scanned_height, end_height, move |tx_bytes: &[u8], height: u64 | { diff --git a/src/lightwallet/mod.rs b/src/lightwallet/mod.rs index a25faa5..d4919ce 100644 --- a/src/lightwallet/mod.rs +++ b/src/lightwallet/mod.rs @@ -14,7 +14,6 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use pairing::bls12_381::{Bls12}; use zcash_client_backend::{ - constants::testnet::{B58_PUBKEY_ADDRESS_PREFIX,}, encoding::encode_payment_address, proto::compact_formats::CompactBlock, welding_rig::scan_block, }; @@ -355,7 +354,7 @@ impl LightWallet { } } - pub fn address_from_sk(sk: &secp256k1::SecretKey) -> String { + pub fn address_from_sk(&self, sk: &secp256k1::SecretKey) -> String { let secp = secp256k1::Secp256k1::new(); let pk = secp256k1::PublicKey::from_secret_key(&secp, &sk); @@ -364,13 +363,13 @@ impl LightWallet { hash160.input(Sha256::digest(&pk.serialize()[..].to_vec())); // TODO: The taddr version prefix needs to be different for testnet and mainnet - hash160.result().to_base58check(&B58_PUBKEY_ADDRESS_PREFIX, &[]) + hash160.result().to_base58check(&self.config.base58_pubkey_address(), &[]) } - pub fn address_from_pubkeyhash(ta: Option) -> Option { + pub fn address_from_pubkeyhash(&self, ta: Option) -> Option { match ta { Some(TransparentAddress::PublicKey(hash)) => { - Some(hash.to_base58check(&B58_PUBKEY_ADDRESS_PREFIX, &[])) + Some(hash.to_base58check(&self.config.base58_pubkey_address(), &[])) }, _ => None } @@ -478,7 +477,7 @@ impl LightWallet { info!("Already have {}:{}", utxo.txid, utxo.output_index); } None => { - let address = LightWallet::address_from_pubkeyhash(vout.script_pubkey.address()); + let address = self.address_from_pubkeyhash(vout.script_pubkey.address()); if address.is_none() { println!("Couldn't determine address for output!"); } @@ -795,7 +794,10 @@ impl LightWallet { let extfvk = &self.extfvks[0]; let ovk = extfvk.fvk.ovk; - let to = match address::RecipientAddress::from_str(to, self.config.hrp_sapling_address()) { + let to = match address::RecipientAddress::from_str(to, + self.config.hrp_sapling_address(), + self.config.base58_pubkey_address(), + self.config.base58_script_address()) { Some(to) => to, None => { eprintln!("Invalid recipient address");