mirror of
https://github.com/Qortal/piratewallet-light-cli.git
synced 2025-07-30 03:41:28 +00:00
transparent address prefix based on chain
This commit is contained in:
@@ -5,10 +5,6 @@ use zcash_primitives::primitives::PaymentAddress;
|
|||||||
use zcash_client_backend::encoding::{decode_payment_address, decode_transparent_address};
|
use zcash_client_backend::encoding::{decode_payment_address, decode_transparent_address};
|
||||||
use zcash_primitives::legacy::TransparentAddress;
|
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.
|
/// An address that funds can be sent to.
|
||||||
pub enum RecipientAddress {
|
pub enum RecipientAddress {
|
||||||
Shielded(PaymentAddress<Bls12>),
|
Shielded(PaymentAddress<Bls12>),
|
||||||
@@ -28,7 +24,7 @@ impl From<TransparentAddress> for RecipientAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RecipientAddress {
|
impl RecipientAddress {
|
||||||
pub fn from_str(s: &str, hrp_sapling_address: &str) -> Option<Self> {
|
pub fn from_str(s: &str, hrp_sapling_address: &str, b58_pubkey_address: [u8; 2], b58_script_address: [u8; 2]) -> Option<Self> {
|
||||||
// Try to match a sapling z address
|
// Try to match a sapling z address
|
||||||
if let Some(pa) = match decode_payment_address(hrp_sapling_address, s) {
|
if let Some(pa) = match decode_payment_address(hrp_sapling_address, s) {
|
||||||
Ok(ret) => ret,
|
Ok(ret) => ret,
|
||||||
@@ -37,7 +33,7 @@ impl RecipientAddress {
|
|||||||
{
|
{
|
||||||
Some(RecipientAddress::Shielded(pa)) // Matched a shielded address
|
Some(RecipientAddress::Shielded(pa)) // Matched a shielded address
|
||||||
} else if let Some(addr) = match decode_transparent_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,
|
Ok(ret) => ret,
|
||||||
Err(_) => None
|
Err(_) => None
|
||||||
}
|
}
|
||||||
|
@@ -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 {
|
pub struct LightClient {
|
||||||
@@ -212,7 +230,7 @@ impl LightClient {
|
|||||||
|
|
||||||
// Collect t addresses
|
// Collect t addresses
|
||||||
let t_addresses = self.wallet.tkeys.iter().map( |sk| {
|
let t_addresses = self.wallet.tkeys.iter().map( |sk| {
|
||||||
LightWallet::address_from_sk(&sk)
|
self.wallet.address_from_sk(&sk)
|
||||||
}).collect::<Vec<String>>();
|
}).collect::<Vec<String>>();
|
||||||
|
|
||||||
object!{
|
object!{
|
||||||
@@ -234,7 +252,7 @@ impl LightClient {
|
|||||||
|
|
||||||
// Collect t addresses
|
// Collect t addresses
|
||||||
let t_addresses = self.wallet.tkeys.iter().map( |sk| {
|
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
|
// Get the balance for this address
|
||||||
let balance = self.wallet.tbalance(Some(address.clone()));
|
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
|
// We'll also fetch all the txids that our transparent addresses are involved with
|
||||||
// TODO: Use for all t addresses
|
// 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();
|
let wallet = self.wallet.clone();
|
||||||
self.fetch_transparent_txids(address, last_scanned_height, end_height,
|
self.fetch_transparent_txids(address, last_scanned_height, end_height,
|
||||||
move |tx_bytes: &[u8], height: u64 | {
|
move |tx_bytes: &[u8], height: u64 | {
|
||||||
|
@@ -14,7 +14,6 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
|||||||
use pairing::bls12_381::{Bls12};
|
use pairing::bls12_381::{Bls12};
|
||||||
|
|
||||||
use zcash_client_backend::{
|
use zcash_client_backend::{
|
||||||
constants::testnet::{B58_PUBKEY_ADDRESS_PREFIX,},
|
|
||||||
encoding::encode_payment_address,
|
encoding::encode_payment_address,
|
||||||
proto::compact_formats::CompactBlock, welding_rig::scan_block,
|
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 secp = secp256k1::Secp256k1::new();
|
||||||
let pk = secp256k1::PublicKey::from_secret_key(&secp, &sk);
|
let pk = secp256k1::PublicKey::from_secret_key(&secp, &sk);
|
||||||
|
|
||||||
@@ -364,13 +363,13 @@ impl LightWallet {
|
|||||||
hash160.input(Sha256::digest(&pk.serialize()[..].to_vec()));
|
hash160.input(Sha256::digest(&pk.serialize()[..].to_vec()));
|
||||||
|
|
||||||
// TODO: The taddr version prefix needs to be different for testnet and mainnet
|
// 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<TransparentAddress>) -> Option<String> {
|
pub fn address_from_pubkeyhash(&self, ta: Option<TransparentAddress>) -> Option<String> {
|
||||||
match ta {
|
match ta {
|
||||||
Some(TransparentAddress::PublicKey(hash)) => {
|
Some(TransparentAddress::PublicKey(hash)) => {
|
||||||
Some(hash.to_base58check(&B58_PUBKEY_ADDRESS_PREFIX, &[]))
|
Some(hash.to_base58check(&self.config.base58_pubkey_address(), &[]))
|
||||||
},
|
},
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
@@ -478,7 +477,7 @@ impl LightWallet {
|
|||||||
info!("Already have {}:{}", utxo.txid, utxo.output_index);
|
info!("Already have {}:{}", utxo.txid, utxo.output_index);
|
||||||
}
|
}
|
||||||
None => {
|
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() {
|
if address.is_none() {
|
||||||
println!("Couldn't determine address for output!");
|
println!("Couldn't determine address for output!");
|
||||||
}
|
}
|
||||||
@@ -795,7 +794,10 @@ impl LightWallet {
|
|||||||
let extfvk = &self.extfvks[0];
|
let extfvk = &self.extfvks[0];
|
||||||
let ovk = extfvk.fvk.ovk;
|
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,
|
Some(to) => to,
|
||||||
None => {
|
None => {
|
||||||
eprintln!("Invalid recipient address");
|
eprintln!("Invalid recipient address");
|
||||||
|
Reference in New Issue
Block a user