|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// Copyright © 2017 Trust Wallet. |
| 4 | + |
| 5 | +use crate::t_address::{TAddress, ZcashPublicKeyHasher}; |
| 6 | +use bech32::FromBase32; |
| 7 | +use core::fmt; |
| 8 | +use std::str::FromStr; |
| 9 | +use tw_bech32_address::bech32_prefix::Bech32Prefix; |
| 10 | +use tw_coin_entry::coin_context::CoinContext; |
| 11 | +use tw_coin_entry::coin_entry::CoinAddress; |
| 12 | +use tw_coin_entry::error::prelude::{AddressError, AddressResult, SigningResult}; |
| 13 | +use tw_hash::hasher::StaticHasher; |
| 14 | +use tw_hash::H160; |
| 15 | +use tw_keypair::ecdsa; |
| 16 | +use tw_memory::Data; |
| 17 | +use tw_utxo::script::standard_script::conditions; |
| 18 | +use tw_utxo::script::Script; |
| 19 | + |
| 20 | +pub const BECH32_VARIANT: bech32::Variant = bech32::Variant::Bech32m; |
| 21 | + |
| 22 | +/// A TEX Address, also called a Transparent-Source-Only Address, is a Bech32m reencoding of a transparent Zcash P2PKH address. |
| 23 | +pub struct TexAddress { |
| 24 | + hrp: String, |
| 25 | + bytes: H160, |
| 26 | + address_str: String, |
| 27 | +} |
| 28 | + |
| 29 | +impl TexAddress { |
| 30 | + pub fn new(hrp: String, bytes: H160) -> AddressResult<TexAddress> { |
| 31 | + let address_str = Self::fmt_internal(&hrp, &bytes)?; |
| 32 | + Ok(TexAddress { |
| 33 | + hrp, |
| 34 | + bytes, |
| 35 | + address_str, |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn with_public_key( |
| 40 | + hrp: String, |
| 41 | + public_key: &ecdsa::secp256k1::PublicKey, |
| 42 | + ) -> AddressResult<Self> { |
| 43 | + let public_key_hash = ZcashPublicKeyHasher::hash(public_key.compressed().as_slice()); |
| 44 | + let bytes = |
| 45 | + H160::try_from(public_key_hash.as_slice()).map_err(|_| AddressError::InvalidInput)?; |
| 46 | + TexAddress::new(hrp, bytes) |
| 47 | + } |
| 48 | + |
| 49 | + pub fn from_t_address_with_hrp(t_address: &TAddress, hrp: String) -> AddressResult<TexAddress> { |
| 50 | + let bytes = t_address.payload(); |
| 51 | + TexAddress::new(hrp, bytes) |
| 52 | + } |
| 53 | + |
| 54 | + pub fn from_str_with_coin_and_prefix( |
| 55 | + coin: &dyn CoinContext, |
| 56 | + s: &str, |
| 57 | + prefix: Option<Bech32Prefix>, |
| 58 | + ) -> AddressResult<TexAddress> { |
| 59 | + let hrp = match prefix { |
| 60 | + Some(Bech32Prefix { hrp }) => hrp, |
| 61 | + None => coin.hrp().ok_or(AddressError::InvalidRegistry)?, |
| 62 | + }; |
| 63 | + TexAddress::from_str_checked(s, &hrp) |
| 64 | + } |
| 65 | + |
| 66 | + pub fn from_str_checked(s: &str, expected_hrp: &str) -> AddressResult<TexAddress> { |
| 67 | + let address = Self::from_str(s)?; |
| 68 | + if address.hrp != expected_hrp { |
| 69 | + return Err(AddressError::InvalidHrp); |
| 70 | + } |
| 71 | + Ok(address) |
| 72 | + } |
| 73 | + |
| 74 | + pub fn to_script_pubkey(&self) -> SigningResult<Script> { |
| 75 | + Ok(conditions::new_p2pkh(&self.bytes)) |
| 76 | + } |
| 77 | + |
| 78 | + pub fn to_t_address(&self, p2pkh_prefix: u8) -> AddressResult<TAddress> { |
| 79 | + TAddress::new(p2pkh_prefix, self.bytes.as_slice()) |
| 80 | + } |
| 81 | + |
| 82 | + fn fmt_internal(hrp: &str, bytes: &H160) -> AddressResult<String> { |
| 83 | + const STRING_CAPACITY: usize = 100; |
| 84 | + |
| 85 | + let mut result_addr = String::with_capacity(STRING_CAPACITY); |
| 86 | + |
| 87 | + { |
| 88 | + let mut bech32_writer = |
| 89 | + bech32::Bech32Writer::new(hrp, BECH32_VARIANT, &mut result_addr) |
| 90 | + .map_err(|_| AddressError::FromBech32Error)?; |
| 91 | + bech32::ToBase32::write_base32(&bytes, &mut bech32_writer) |
| 92 | + .map_err(|_| AddressError::FromBech32Error)?; |
| 93 | + } |
| 94 | + |
| 95 | + Ok(result_addr) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl fmt::Display for TexAddress { |
| 100 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 101 | + write!(f, "{}", self.address_str) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl FromStr for TexAddress { |
| 106 | + type Err = AddressError; |
| 107 | + |
| 108 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 109 | + let (hrp, payload_u5, checksum_variant) = |
| 110 | + bech32::decode(s).map_err(|_| AddressError::FromBech32Error)?; |
| 111 | + |
| 112 | + if checksum_variant != BECH32_VARIANT { |
| 113 | + return Err(AddressError::FromBech32Error); |
| 114 | + } |
| 115 | + let payload = Data::from_base32(&payload_u5).map_err(|_| AddressError::FromBech32Error)?; |
| 116 | + let bytes = |
| 117 | + H160::try_from(payload.as_slice()).map_err(|_| AddressError::FromBech32Error)?; |
| 118 | + TexAddress::new(hrp, bytes) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl CoinAddress for TexAddress { |
| 123 | + fn data(&self) -> Data { |
| 124 | + self.bytes.to_vec() |
| 125 | + } |
| 126 | +} |
0 commit comments