Skip to content

Commit

Permalink
zcash_address: Use zcash_protocol::consensus::NetworkType
Browse files Browse the repository at this point in the history
This inverts the dependency relationship between `zcash_protocol` and
`zcash_address`, permitting the network constants (primarily the HRPs)
defined in `zcash_protocol` to be used directly in `zcash_address`
instead of being duplicated.
  • Loading branch information
nuttycom committed Feb 5, 2024
1 parent 1264182 commit 234522f
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 384 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions components/zcash_address/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Removed
- `zcash_address::kind::p2pkh` - use constants from `zcash_protocol` instead.
- `zcash_address::kind::p2sh` - use constants from `zcash_protocol` instead.
- `zcash_address::kind::sapling` - use constants from `zcash_protocol` instead.
- `zcash_address::kind::sprout` - use constants from `zcash_protocol` instead.

## [0.3.1] - 2024-01-12
### Fixed
- Stubs for `zcash_address::convert` traits that are created by `rust-analyzer`
Expand Down
4 changes: 3 additions & 1 deletion components/zcash_address/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ rustdoc-args = ["--cfg", "docsrs"]
bech32 = "0.9"
bs58 = { version = "0.5", features = ["check"] }
f4jumble = { version = "0.1", path = "../f4jumble" }
zcash_encoding = { version = "0.2", path = "../zcash_encoding" }
zcash_protocol.workspace = true
zcash_encoding.workspace = true

[dev-dependencies]
assert_matches = "1.3.0"
proptest = "1"

[features]
test-dependencies = []
regtest = ["zcash_protocol/local-consensus"]

[lib]
bench = false
65 changes: 26 additions & 39 deletions components/zcash_address/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{convert::TryInto, error::Error, fmt, str::FromStr};

use bech32::{self, FromBase32, ToBase32, Variant};
use zcash_protocol::consensus::{NetworkConstants, NetworkType};
use zcash_protocol::constants::{mainnet, regtest, testnet};

use crate::kind::unified::Encoding;
use crate::{kind::*, AddressKind, Network, ZcashAddress};
use crate::{kind::*, AddressKind, ZcashAddress};

/// An error while attempting to parse a string as a Zcash address.
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -68,9 +70,9 @@ impl FromStr for ZcashAddress {
let data = Vec::<u8>::from_base32(&data).map_err(|_| ParseError::InvalidEncoding)?;

let net = match hrp.as_str() {
sapling::MAINNET => Network::Main,
sapling::TESTNET => Network::Test,
sapling::REGTEST => Network::Regtest,
mainnet::HRP_SAPLING_PAYMENT_ADDRESS => NetworkType::Main,
testnet::HRP_SAPLING_PAYMENT_ADDRESS => NetworkType::Test,
regtest::HRP_SAPLING_PAYMENT_ADDRESS => NetworkType::Regtest,
// We will not define new Bech32 address encodings.
_ => {
return Err(ParseError::NotZcash);
Expand All @@ -87,18 +89,26 @@ impl FromStr for ZcashAddress {
// The rest use Base58Check.
if let Ok(decoded) = bs58::decode(s).with_check(None).into_vec() {
let net = match decoded[..2].try_into().unwrap() {
sprout::MAINNET | p2pkh::MAINNET | p2sh::MAINNET => Network::Main,
sprout::TESTNET | p2pkh::TESTNET | p2sh::TESTNET => Network::Test,
mainnet::B58_PUBKEY_ADDRESS_PREFIX
| mainnet::B58_SCRIPT_ADDRESS_PREFIX
| mainnet::B58_SPROUT_ADDRESS_PREFIX => NetworkType::Main,
testnet::B58_PUBKEY_ADDRESS_PREFIX
| testnet::B58_SCRIPT_ADDRESS_PREFIX
| testnet::B58_SPROUT_ADDRESS_PREFIX => NetworkType::Test,
// We will not define new Base58Check address encodings.
_ => return Err(ParseError::NotZcash),
};

return match decoded[..2].try_into().unwrap() {
sprout::MAINNET | sprout::TESTNET => {
mainnet::B58_SPROUT_ADDRESS_PREFIX | testnet::B58_SPROUT_ADDRESS_PREFIX => {
decoded[2..].try_into().map(AddressKind::Sprout)
}
p2pkh::MAINNET | p2pkh::TESTNET => decoded[2..].try_into().map(AddressKind::P2pkh),
p2sh::MAINNET | p2sh::TESTNET => decoded[2..].try_into().map(AddressKind::P2sh),
mainnet::B58_PUBKEY_ADDRESS_PREFIX | testnet::B58_PUBKEY_ADDRESS_PREFIX => {
decoded[2..].try_into().map(AddressKind::P2pkh)
}
mainnet::B58_SCRIPT_ADDRESS_PREFIX | testnet::B58_SCRIPT_ADDRESS_PREFIX => {
decoded[2..].try_into().map(AddressKind::P2sh)
}
_ => unreachable!(),
}
.map_err(|_| ParseError::InvalidEncoding)
Expand All @@ -124,36 +134,13 @@ fn encode_b58(prefix: [u8; 2], data: &[u8]) -> String {
impl fmt::Display for ZcashAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let encoded = match &self.kind {
AddressKind::Sprout(data) => encode_b58(
match self.net {
Network::Main => sprout::MAINNET,
Network::Test | Network::Regtest => sprout::TESTNET,
},
data,
),
AddressKind::Sapling(data) => encode_bech32(
match self.net {
Network::Main => sapling::MAINNET,
Network::Test => sapling::TESTNET,
Network::Regtest => sapling::REGTEST,
},
data,
),
AddressKind::Sprout(data) => encode_b58(self.net.b58_sprout_address_prefix(), data),
AddressKind::Sapling(data) => {
encode_bech32(self.net.hrp_sapling_payment_address(), data)
}
AddressKind::Unified(addr) => addr.encode(&self.net),
AddressKind::P2pkh(data) => encode_b58(
match self.net {
Network::Main => p2pkh::MAINNET,
Network::Test | Network::Regtest => p2pkh::TESTNET,
},
data,
),
AddressKind::P2sh(data) => encode_b58(
match self.net {
Network::Main => p2sh::MAINNET,
Network::Test | Network::Regtest => p2sh::TESTNET,
},
data,
),
AddressKind::P2pkh(data) => encode_b58(self.net.b58_pubkey_address_prefix(), data),
AddressKind::P2sh(data) => encode_b58(self.net.b58_script_address_prefix(), data),
};
write!(f, "{}", encoded)
}
Expand All @@ -162,7 +149,7 @@ impl fmt::Display for ZcashAddress {
#[cfg(test)]
mod tests {
use super::*;
use crate::kind::unified;
use crate::{kind::unified, Network};

fn encoding(encoded: &str, decoded: ZcashAddress) {
assert_eq!(decoded.to_string(), encoded);
Expand Down
6 changes: 0 additions & 6 deletions components/zcash_address/src/kind.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
pub mod unified;

pub(crate) mod sapling;
pub(crate) mod sprout;

pub(crate) mod p2pkh;
pub(crate) mod p2sh;
5 changes: 0 additions & 5 deletions components/zcash_address/src/kind/p2pkh.rs

This file was deleted.

5 changes: 0 additions & 5 deletions components/zcash_address/src/kind/p2sh.rs

This file was deleted.

20 changes: 0 additions & 20 deletions components/zcash_address/src/kind/sapling.rs

This file was deleted.

13 changes: 0 additions & 13 deletions components/zcash_address/src/kind/sprout.rs

This file was deleted.

15 changes: 1 addition & 14 deletions components/zcash_address/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub use convert::{
};
pub use encoding::ParseError;
pub use kind::unified;
pub use zcash_protocol::consensus::NetworkType as Network;

/// A Zcash address.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -149,20 +150,6 @@ pub struct ZcashAddress {
kind: AddressKind,
}

/// The Zcash network for which an address is encoded.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Network {
/// Zcash Mainnet.
Main,
/// Zcash Testnet.
Test,
/// Private integration / regression testing, used in `zcashd`.
///
/// For some address types there is no distinction between test and regtest encodings;
/// those will always be parsed as `Network::Test`.
Regtest,
}

/// Known kinds of Zcash addresses.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum AddressKind {
Expand Down
9 changes: 9 additions & 0 deletions components/zcash_protocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ The entries below are relative to the `zcash_primitives` crate as of the tag
- `zcash_protocol::value::MAX_BALANCE` has been added to replace previous
instances where `zcash_protocol::value::MAX_MONEY` was used as a signed
value.
- `zcash_protocol::consensus::NetworkConstants` has been extracted from
the `Parameters` trait in the same module.
- `zcash_protocol::consensus::NetworkType`
- `zcash_protocol::consensus::Parameters::b58_sprout_address_prefix`
- `zcash_protocol::constants::{mainnet, testnet}::B58_SPROUT_ADDRESS_PREFIX`

### Moved
- `zcash_primitives::transcation::components::amount` has been moved to
Expand All @@ -35,3 +40,7 @@ The entries below are relative to the `zcash_primitives` crate as of the tag
been renamed to `zcash_protocol::value::Zatoshis`
- `zcash_primitives::transcation::components::amount::Amount` has been renamed
to `zcash_protocol::value::ZatBalance`
- `zcash_protocol::consensus::Parameters` has been split into two traits, with
the `NetworkConstants` trait providing all network constant accessors. Also,
the `address_network` method has been replaced with a new `network_type`
method that serves the same purpose.
2 changes: 0 additions & 2 deletions components/zcash_protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
zcash_address.workspace = true

# - Logging and metrics
memuse.workspace = true

Expand Down
Loading

0 comments on commit 234522f

Please sign in to comment.