Skip to content

Commit

Permalink
Merge pull request zcash#1149 from nuttycom/rename_taddr_variants
Browse files Browse the repository at this point in the history
zcash_primitives: Rename `TransparentAddress` variants.
  • Loading branch information
str4d authored Feb 6, 2024
2 parents 9b7f13c + 2360609 commit 45df8b7
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions zcash_client_sqlite/src/wallet/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ pub(crate) mod tests {
let fee_rule = StandardFeeRule::PreZip313;

// TODO: generate_next_block_from_tx does not currently support transparent outputs.
let to = TransparentAddress::PublicKey([7; 20]).into();
let to = TransparentAddress::PublicKeyHash([7; 20]).into();
let min_confirmations = NonZeroU32::new(1).unwrap();
let proposal = st
.propose_standard_transfer::<Infallible>(
Expand Down Expand Up @@ -1216,7 +1216,7 @@ pub(crate) mod tests {
let fee_rule = StandardFeeRule::PreZip313;

// TODO: generate_next_block_from_tx does not currently support transparent outputs.
let to = TransparentAddress::PublicKey([7; 20]).into();
let to = TransparentAddress::PublicKeyHash([7; 20]).into();
let min_confirmations = NonZeroU32::new(1).unwrap();
let proposal = st
.propose_standard_transfer::<Infallible>(
Expand Down
2 changes: 1 addition & 1 deletion zcash_extensions/src/transparent/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ mod tests {

builder_c
.add_transparent_output(
&TransparentAddress::PublicKey([0; 20]),
&TransparentAddress::PublicKeyHash([0; 20]),
(value_xfr - fee_rule.fixed_fee()).unwrap(),
)
.unwrap();
Expand Down
18 changes: 10 additions & 8 deletions zcash_keys/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ impl TryFrom<unified::Address> for UnifiedAddress {
})
.transpose(),
unified::Receiver::P2pkh(data) => {
transparent = Some(TransparentAddress::PublicKey(*data));
transparent = Some(TransparentAddress::PublicKeyHash(*data));
None
}
unified::Receiver::P2sh(data) => {
transparent = Some(TransparentAddress::Script(*data));
transparent = Some(TransparentAddress::ScriptHash(*data));
None
}
unified::Receiver::Unknown { typecode, data } => {
Expand Down Expand Up @@ -173,8 +173,8 @@ impl UnifiedAddress {
data: data.clone(),
})
.chain(self.transparent.as_ref().map(|taddr| match taddr {
TransparentAddress::PublicKey(data) => unified::Receiver::P2pkh(*data),
TransparentAddress::Script(data) => unified::Receiver::P2sh(*data),
TransparentAddress::PublicKeyHash(data) => unified::Receiver::P2pkh(*data),
TransparentAddress::ScriptHash(data) => unified::Receiver::P2sh(*data),
}))
.chain(
self.sapling
Expand Down Expand Up @@ -241,11 +241,11 @@ impl TryFromRawAddress for Address {
fn try_from_raw_transparent_p2pkh(
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
Ok(TransparentAddress::PublicKey(data).into())
Ok(TransparentAddress::PublicKeyHash(data).into())
}

fn try_from_raw_transparent_p2sh(data: [u8; 20]) -> Result<Self, ConversionError<Self::Error>> {
Ok(TransparentAddress::Script(data).into())
Ok(TransparentAddress::ScriptHash(data).into())
}
}

Expand All @@ -262,10 +262,12 @@ impl Address {
match self {
Address::Sapling(pa) => ZcashAddress::from_sapling(net, pa.to_bytes()),
Address::Transparent(addr) => match addr {
TransparentAddress::PublicKey(data) => {
TransparentAddress::PublicKeyHash(data) => {
ZcashAddress::from_transparent_p2pkh(net, *data)
}
TransparentAddress::Script(data) => ZcashAddress::from_transparent_p2sh(net, *data),
TransparentAddress::ScriptHash(data) => {
ZcashAddress::from_transparent_p2sh(net, *data)
}
},
Address::Unified(ua) => ua.to_address(net),
}
Expand Down
16 changes: 8 additions & 8 deletions zcash_keys/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub fn decode_payment_address(
/// encode_transparent_address(
/// &TEST_NETWORK.b58_pubkey_address_prefix(),
/// &TEST_NETWORK.b58_script_address_prefix(),
/// &TransparentAddress::PublicKey([0; 20]),
/// &TransparentAddress::PublicKeyHash([0; 20]),
/// ),
/// "tm9iMLAuYMzJ6jtFLcA7rzUmfreGuKvr7Ma",
/// );
Expand All @@ -357,7 +357,7 @@ pub fn decode_payment_address(
/// encode_transparent_address(
/// &TEST_NETWORK.b58_pubkey_address_prefix(),
/// &TEST_NETWORK.b58_script_address_prefix(),
/// &TransparentAddress::Script([0; 20]),
/// &TransparentAddress::ScriptHash([0; 20]),
/// ),
/// "t26YoyZ1iPgiMEWL4zGUm74eVWfhyDMXzY2",
/// );
Expand All @@ -369,13 +369,13 @@ pub fn encode_transparent_address(
addr: &TransparentAddress,
) -> String {
let decoded = match addr {
TransparentAddress::PublicKey(key_id) => {
TransparentAddress::PublicKeyHash(key_id) => {
let mut decoded = vec![0; pubkey_version.len() + 20];
decoded[..pubkey_version.len()].copy_from_slice(pubkey_version);
decoded[pubkey_version.len()..].copy_from_slice(key_id);
decoded
}
TransparentAddress::Script(script_id) => {
TransparentAddress::ScriptHash(script_id) => {
let mut decoded = vec![0; script_version.len() + 20];
decoded[..script_version.len()].copy_from_slice(script_version);
decoded[script_version.len()..].copy_from_slice(script_id);
Expand Down Expand Up @@ -418,7 +418,7 @@ pub fn encode_transparent_address_p<P: consensus::Parameters>(
/// &TEST_NETWORK.b58_script_address_prefix(),
/// "tm9iMLAuYMzJ6jtFLcA7rzUmfreGuKvr7Ma",
/// ),
/// Ok(Some(TransparentAddress::PublicKey([0; 20]))),
/// Ok(Some(TransparentAddress::PublicKeyHash([0; 20]))),
/// );
///
/// assert_eq!(
Expand All @@ -427,7 +427,7 @@ pub fn encode_transparent_address_p<P: consensus::Parameters>(
/// &TEST_NETWORK.b58_script_address_prefix(),
/// "t26YoyZ1iPgiMEWL4zGUm74eVWfhyDMXzY2",
/// ),
/// Ok(Some(TransparentAddress::Script([0; 20]))),
/// Ok(Some(TransparentAddress::ScriptHash([0; 20]))),
/// );
/// ```
/// [`TransparentAddress`]: zcash_primitives::legacy::TransparentAddress
Expand All @@ -441,12 +441,12 @@ pub fn decode_transparent_address(
decoded[pubkey_version.len()..]
.try_into()
.ok()
.map(TransparentAddress::PublicKey)
.map(TransparentAddress::PublicKeyHash)
} else if decoded.starts_with(script_version) {
decoded[script_version.len()..]
.try_into()
.ok()
.map(TransparentAddress::Script)
.map(TransparentAddress::ScriptHash)
} else {
None
}
Expand Down
3 changes: 3 additions & 0 deletions zcash_primitives/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ and this library adheres to Rust's notion of
defaults to `constants::regtest::` for everything else.

### Changed
- `zcash_primitives::legacy::TransparentAddress` variants have changed:
- `TransparentAddress::PublicKey` has been renamed to `PublicKeyHash`
- `TransparentAddress::Script` has been renamed to `ScriptHash`
- `zcash_primitives::transaction`:
- `builder::Builder` now has a generic parameter for the type of progress
notifier, which needs to implement `sapling::builder::ProverProgress` in
Expand Down
12 changes: 6 additions & 6 deletions zcash_primitives/src/constants/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// [SLIP 44]: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
pub const COIN_TYPE: u32 = 133;

/// The HRP for a Bech32-encoded mainnet [`ExtendedSpendingKey`].
/// The HRP for a Bech32-encoded mainnet Sapling [`ExtendedSpendingKey`].
///
/// Defined in [ZIP 32].
///
Expand All @@ -21,20 +21,20 @@ pub const HRP_SAPLING_EXTENDED_SPENDING_KEY: &str = "secret-extended-key-main";
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
pub const HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY: &str = "zxviews";

/// The HRP for a Bech32-encoded mainnet [`PaymentAddress`].
/// The HRP for a Bech32-encoded mainnet Sapling [`PaymentAddress`].
///
/// Defined in section 5.6.4 of the [Zcash Protocol Specification].
///
/// [`PaymentAddress`]: crate::sapling::PaymentAddress
/// [Zcash Protocol Specification]: https://github.com/zcash/zips/blob/master/protocol/protocol.pdf
pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "zs";

/// The prefix for a Base58Check-encoded mainnet [`TransparentAddress::PublicKey`].
/// The prefix for a Base58Check-encoded mainnet [`PublicKeyHash`].
///
/// [`TransparentAddress::PublicKey`]: crate::legacy::TransparentAddress::PublicKey
/// [`PublicKeyHash`]: crate::legacy::TransparentAddress::PublicKeyHash
pub const B58_PUBKEY_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xb8];

/// The prefix for a Base58Check-encoded mainnet [`TransparentAddress::Script`].
/// The prefix for a Base58Check-encoded mainnet [`ScriptHash`].
///
/// [`TransparentAddress::Script`]: crate::legacy::TransparentAddress::Script
/// [`ScriptHash`]: crate::legacy::TransparentAddress::ScriptHash
pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xbd];
14 changes: 7 additions & 7 deletions zcash_primitives/src/constants/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@
/// The regtest cointype reuses the testnet cointype
pub const COIN_TYPE: u32 = 1;

/// The HRP for a Bech32-encoded regtest [`ExtendedSpendingKey`].
/// The HRP for a Bech32-encoded regtest Sapling [`ExtendedSpendingKey`].
///
/// It is defined in [the `zcashd` codebase].
///
/// [`ExtendedSpendingKey`]: crate::sapling::zip32::ExtendedSpendingKey
/// [the `zcashd` codebase]: <https://github.com/zcash/zcash/blob/128d863fb8be39ee294fda397c1ce3ba3b889cb2/src/chainparams.cpp#L496>
pub const HRP_SAPLING_EXTENDED_SPENDING_KEY: &str = "secret-extended-key-regtest";

/// The HRP for a Bech32-encoded regtest [`ExtendedFullViewingKey`].
/// The HRP for a Bech32-encoded regtest Sapling [`ExtendedFullViewingKey`].
///
/// It is defined in [the `zcashd` codebase].
///
/// [`ExtendedFullViewingKey`]: crate::sapling::zip32::ExtendedFullViewingKey
/// [the `zcashd` codebase]: <https://github.com/zcash/zcash/blob/128d863fb8be39ee294fda397c1ce3ba3b889cb2/src/chainparams.cpp#L494>
pub const HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY: &str = "zxviewregtestsapling";

/// The HRP for a Bech32-encoded regtest [`PaymentAddress`].
/// The HRP for a Bech32-encoded regtest Sapling [`PaymentAddress`].
///
/// It is defined in [the `zcashd` codebase].
///
/// [`PaymentAddress`]: crate::sapling::PaymentAddress
/// [the `zcashd` codebase]: <https://github.com/zcash/zcash/blob/128d863fb8be39ee294fda397c1ce3ba3b889cb2/src/chainparams.cpp#L493>
pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "zregtestsapling";

/// The prefix for a Base58Check-encoded regtest [`TransparentAddress::PublicKey`].
/// The prefix for a Base58Check-encoded regtest transparent [`PublicKeyHash`].
/// Same as the testnet prefix.
///
/// [`TransparentAddress::PublicKey`]: crate::legacy::TransparentAddress::PublicKey
/// [`PublicKeyHash`]: crate::legacy::TransparentAddress::PublicKeyHash
pub const B58_PUBKEY_ADDRESS_PREFIX: [u8; 2] = [0x1d, 0x25];

/// The prefix for a Base58Check-encoded regtest [`TransparentAddress::Script`].
/// The prefix for a Base58Check-encoded regtest transparent [`ScriptHash`].
/// Same as the testnet prefix.
///
/// [`TransparentAddress::Script`]: crate::legacy::TransparentAddress::Script
/// [`ScriptHash`]: crate::legacy::TransparentAddress::ScriptHash
pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xba];
14 changes: 7 additions & 7 deletions zcash_primitives/src/constants/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
/// [SLIP 44]: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
pub const COIN_TYPE: u32 = 1;

/// The HRP for a Bech32-encoded testnet [`ExtendedSpendingKey`].
/// The HRP for a Bech32-encoded testnet Sapling [`ExtendedSpendingKey`].
///
/// Defined in [ZIP 32].
///
/// [`ExtendedSpendingKey`]: crate::sapling::zip32::ExtendedSpendingKey
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
pub const HRP_SAPLING_EXTENDED_SPENDING_KEY: &str = "secret-extended-key-test";

/// The HRP for a Bech32-encoded testnet [`ExtendedFullViewingKey`].
/// The HRP for a Bech32-encoded testnet Sapling [`ExtendedFullViewingKey`].
///
/// Defined in [ZIP 32].
///
/// [`ExtendedFullViewingKey`]: crate::sapling::zip32::ExtendedFullViewingKey
/// [ZIP 32]: https://github.com/zcash/zips/blob/master/zip-0032.rst
pub const HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY: &str = "zxviewtestsapling";

/// The HRP for a Bech32-encoded testnet [`PaymentAddress`].
/// The HRP for a Bech32-encoded testnet Sapling [`PaymentAddress`].
///
/// Defined in section 5.6.4 of the [Zcash Protocol Specification].
///
/// [`PaymentAddress`]: crate::sapling::PaymentAddress
/// [Zcash Protocol Specification]: https://github.com/zcash/zips/blob/master/protocol/protocol.pdf
pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "ztestsapling";

/// The prefix for a Base58Check-encoded testnet [`TransparentAddress::PublicKey`].
/// The prefix for a Base58Check-encoded testnet transparent [`PublicKeyHash`].
///
/// [`TransparentAddress::PublicKey`]: crate::legacy::TransparentAddress::PublicKey
/// [`PublicKeyHash`]: crate::legacy::TransparentAddress::PublicKeyHash
pub const B58_PUBKEY_ADDRESS_PREFIX: [u8; 2] = [0x1d, 0x25];

/// The prefix for a Base58Check-encoded testnet [`TransparentAddress::Script`].
/// The prefix for a Base58Check-encoded testnet transparent [`ScriptHash`].
///
/// [`TransparentAddress::Script`]: crate::legacy::TransparentAddress::Script
/// [`ScriptHash`]: crate::legacy::TransparentAddress::ScriptHash
pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xba];
18 changes: 9 additions & 9 deletions zcash_primitives/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,14 @@ impl Script {
{
let mut hash = [0; 20];
hash.copy_from_slice(&self.0[3..23]);
Some(TransparentAddress::PublicKey(hash))
Some(TransparentAddress::PublicKeyHash(hash))
} else if self.0.len() == 23
&& self.0[0..2] == [OpCode::Hash160 as u8, 0x14]
&& self.0[22] == OpCode::Equal as u8
{
let mut hash = [0; 20];
hash.copy_from_slice(&self.0[2..22]);
Some(TransparentAddress::Script(hash))
Some(TransparentAddress::ScriptHash(hash))
} else {
None
}
Expand Down Expand Up @@ -377,15 +377,15 @@ impl Shl<&[u8]> for Script {
/// A transparent address corresponding to either a public key or a `Script`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TransparentAddress {
PublicKey([u8; 20]), // TODO: Rename to PublicKeyHash
Script([u8; 20]), // TODO: Rename to ScriptHash
PublicKeyHash([u8; 20]),
ScriptHash([u8; 20]),
}

impl TransparentAddress {
/// Generate the `scriptPubKey` corresponding to this address.
pub fn script(&self) -> Script {
match self {
TransparentAddress::PublicKey(key_id) => {
TransparentAddress::PublicKeyHash(key_id) => {
// P2PKH script
Script::default()
<< OpCode::Dup
Expand All @@ -394,7 +394,7 @@ impl TransparentAddress {
<< OpCode::EqualVerify
<< OpCode::CheckSig
}
TransparentAddress::Script(script_id) => {
TransparentAddress::ScriptHash(script_id) => {
// P2SH script
Script::default() << OpCode::Hash160 << &script_id[..] << OpCode::Equal
}
Expand All @@ -410,7 +410,7 @@ pub mod testing {

prop_compose! {
pub fn arb_transparent_addr()(v in proptest::array::uniform20(any::<u8>())) -> TransparentAddress {
TransparentAddress::PublicKey(v)
TransparentAddress::PublicKeyHash(v)
}
}
}
Expand Down Expand Up @@ -461,7 +461,7 @@ mod tests {

#[test]
fn p2pkh() {
let addr = TransparentAddress::PublicKey([4; 20]);
let addr = TransparentAddress::PublicKeyHash([4; 20]);
assert_eq!(
&addr.script().0,
&[
Expand All @@ -474,7 +474,7 @@ mod tests {

#[test]
fn p2sh() {
let addr = TransparentAddress::Script([7; 20]);
let addr = TransparentAddress::ScriptHash([7; 20]);
assert_eq!(
&addr.script().0,
&[
Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/src/legacy/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl AccountPubKey {
/// Derives the P2PKH transparent address corresponding to the given pubkey.
#[deprecated(note = "This function will be removed from the public API in an upcoming refactor.")]
pub fn pubkey_to_address(pubkey: &secp256k1::PublicKey) -> TransparentAddress {
TransparentAddress::PublicKey(
TransparentAddress::PublicKeyHash(
*ripemd::Ripemd160::digest(Sha256::digest(pubkey.serialize())).as_ref(),
)
}
Expand Down
Loading

0 comments on commit 45df8b7

Please sign in to comment.