Skip to content

Commit

Permalink
zcash_protocol: Add unified address/fvk/ivk HRPs retrieval methods to…
Browse files Browse the repository at this point in the history
… `NetworkConstants` trait.
  • Loading branch information
nuttycom committed Dec 30, 2024
1 parent 6ead8f8 commit 5d7a185
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 10 deletions.
8 changes: 4 additions & 4 deletions components/zcash_address/src/kind/unified/address.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use zcash_protocol::PoolType;
use zcash_protocol::{constants, PoolType};

use super::{private::SealedItem, ParseError, Typecode};

Expand Down Expand Up @@ -136,17 +136,17 @@ impl super::private::SealedContainer for Address {
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const MAINNET: &'static str = "u";
const MAINNET: &'static str = constants::mainnet::HRP_UNIFIED_ADDRESS;

/// The HRP for a Bech32m-encoded testnet Unified Address.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const TESTNET: &'static str = "utest";
const TESTNET: &'static str = constants::testnet::HRP_UNIFIED_ADDRESS;

/// The HRP for a Bech32m-encoded regtest Unified Address.
const REGTEST: &'static str = "uregtest";
const REGTEST: &'static str = constants::regtest::HRP_UNIFIED_ADDRESS;

fn from_inner(receivers: Vec<Self::Item>) -> Self {
Self(receivers)
Expand Down
11 changes: 8 additions & 3 deletions components/zcash_address/src/kind/unified/fvk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};
use zcash_protocol::constants;

use super::{
private::{SealedContainer, SealedItem},
Expand Down Expand Up @@ -128,17 +129,21 @@ impl SealedContainer for Ufvk {
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const MAINNET: &'static str = "uview";
const MAINNET: &'static str = constants::mainnet::HRP_UNIFIED_FVK;

/// The HRP for a Bech32m-encoded testnet Unified FVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const TESTNET: &'static str = "uviewtest";
const TESTNET: &'static str = constants::testnet::HRP_UNIFIED_FVK;

/// The HRP for a Bech32m-encoded regtest Unified FVK.
const REGTEST: &'static str = "uviewregtest";
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const REGTEST: &'static str = constants::regtest::HRP_UNIFIED_FVK;

fn from_inner(fvks: Vec<Self::Item>) -> Self {
Self(fvks)
Expand Down
7 changes: 4 additions & 3 deletions components/zcash_address/src/kind/unified/ivk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::vec::Vec;
use zcash_protocol::constants;
use core::convert::{TryFrom, TryInto};

use super::{
Expand Down Expand Up @@ -133,17 +134,17 @@ impl SealedContainer for Uivk {
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const MAINNET: &'static str = "uivk";
const MAINNET: &'static str = constants::mainnet::HRP_UNIFIED_IVK;

/// The HRP for a Bech32m-encoded testnet Unified IVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
const TESTNET: &'static str = "uivktest";
const TESTNET: &'static str = constants::testnet::HRP_UNIFIED_IVK;

/// The HRP for a Bech32m-encoded regtest Unified IVK.
const REGTEST: &'static str = "uivkregtest";
const REGTEST: &'static str = constants::regtest::HRP_UNIFIED_IVK;

fn from_inner(ivks: Vec<Self::Item>) -> Self {
Self(ivks)
Expand Down
6 changes: 6 additions & 0 deletions components/zcash_protocol/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]

### Changed
- `zcash_protocol::consensus::NetworkConstants` has added methods:
- `hrp_unified_address`
- `hrp_unified_fvk`
- `hrp_unified_ivk`

## [0.4.3] - 2024-12-16
### Added
- `zcash_protocol::TxId` (moved from `zcash_primitives::transaction`).
Expand Down
57 changes: 57 additions & 0 deletions components/zcash_protocol/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ pub trait NetworkConstants: Clone {
///
/// [ZIP 320]: https://zips.z.cash/zip-0320
fn hrp_tex_address(&self) -> &'static str;

/// The HRP for a Bech32m-encoded mainnet Unified Address.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
fn hrp_unified_address(&self) -> &'static str;

/// The HRP for a Bech32m-encoded mainnet Unified FVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
fn hrp_unified_fvk(&self) -> &'static str;

/// The HRP for a Bech32m-encoded mainnet Unified IVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
fn hrp_unified_ivk(&self) -> &'static str;
}

/// The enumeration of known Zcash network types.
Expand Down Expand Up @@ -272,6 +293,30 @@ impl NetworkConstants for NetworkType {
NetworkType::Regtest => regtest::HRP_TEX_ADDRESS,
}
}

fn hrp_unified_address(&self) -> &'static str {
match self {
NetworkType::Main => mainnet::HRP_UNIFIED_ADDRESS,
NetworkType::Test => testnet::HRP_UNIFIED_ADDRESS,
NetworkType::Regtest => regtest::HRP_UNIFIED_ADDRESS,
}
}

fn hrp_unified_fvk(&self) -> &'static str {
match self {
NetworkType::Main => mainnet::HRP_UNIFIED_FVK,
NetworkType::Test => testnet::HRP_UNIFIED_FVK,
NetworkType::Regtest => regtest::HRP_UNIFIED_FVK,
}
}

fn hrp_unified_ivk(&self) -> &'static str {
match self {
NetworkType::Main => mainnet::HRP_UNIFIED_IVK,
NetworkType::Test => testnet::HRP_UNIFIED_IVK,
NetworkType::Regtest => regtest::HRP_UNIFIED_IVK,
}
}
}

/// Zcash consensus parameters.
Expand Down Expand Up @@ -322,6 +367,18 @@ impl<P: Parameters> NetworkConstants for P {
fn hrp_tex_address(&self) -> &'static str {
self.network_type().hrp_tex_address()
}

fn hrp_unified_address(&self) -> &'static str {
self.network_type().hrp_unified_address()
}

fn hrp_unified_fvk(&self) -> &'static str {
self.network_type().hrp_unified_fvk()
}

fn hrp_unified_ivk(&self) -> &'static str {
self.network_type().hrp_unified_ivk()
}
}

/// Marker struct for the production network.
Expand Down
21 changes: 21 additions & 0 deletions components/zcash_protocol/src/constants/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,24 @@ pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xbd];
///
/// [ZIP 320]: https://zips.z.cash/zip-0320
pub const HRP_TEX_ADDRESS: &str = "tex";

/// The HRP for a Bech32m-encoded mainnet Unified Address.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_ADDRESS: &str = "u";

/// The HRP for a Bech32m-encoded mainnet Unified FVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_FVK: &str = "uview";

/// The HRP for a Bech32m-encoded mainnet Unified IVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_IVK: &str = "uivk";
13 changes: 13 additions & 0 deletions components/zcash_protocol/src/constants/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xba];
///
/// [ZIP 320]: https://zips.z.cash/zip-0320
pub const HRP_TEX_ADDRESS: &str = "texregtest";

/// The HRP for a Bech32m-encoded regtest Unified Address.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_ADDRESS: &str = "uregtest";

/// The HRP for a Bech32m-encoded regtest Unified FVK.
pub const HRP_UNIFIED_FVK: &str = "uviewregtest";

/// The HRP for a Bech32m-encoded regtest Unified IVK.
pub const HRP_UNIFIED_IVK: &str = "uivkregtest";
21 changes: 21 additions & 0 deletions components/zcash_protocol/src/constants/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,24 @@ pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xba];
///
/// [ZIP 320]: https://zips.z.cash/zip-0320
pub const HRP_TEX_ADDRESS: &str = "textest";

/// The HRP for a Bech32m-encoded testnet Unified Address.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_ADDRESS: &str = "utest";

/// The HRP for a Bech32m-encoded testnet Unified FVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_FVK: &str = "uviewtest";

/// The HRP for a Bech32m-encoded testnet Unified IVK.
///
/// Defined in [ZIP 316][zip-0316].
///
/// [zip-0316]: https://zips.z.cash/zip-0316
pub const HRP_UNIFIED_IVK: &str = "uivktest";

0 comments on commit 5d7a185

Please sign in to comment.