From 5d7a1857ee6edf9fdf3501583594a18515f60975 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Fri, 20 Dec 2024 13:49:51 -0700 Subject: [PATCH] zcash_protocol: Add unified address/fvk/ivk HRPs retrieval methods to `NetworkConstants` trait. --- .../zcash_address/src/kind/unified/address.rs | 8 +-- .../zcash_address/src/kind/unified/fvk.rs | 11 +++- .../zcash_address/src/kind/unified/ivk.rs | 7 ++- components/zcash_protocol/CHANGELOG.md | 6 ++ components/zcash_protocol/src/consensus.rs | 57 +++++++++++++++++++ .../zcash_protocol/src/constants/mainnet.rs | 21 +++++++ .../zcash_protocol/src/constants/regtest.rs | 13 +++++ .../zcash_protocol/src/constants/testnet.rs | 21 +++++++ 8 files changed, 134 insertions(+), 10 deletions(-) diff --git a/components/zcash_address/src/kind/unified/address.rs b/components/zcash_address/src/kind/unified/address.rs index 0a4423bf0c..41be7ab992 100644 --- a/components/zcash_address/src/kind/unified/address.rs +++ b/components/zcash_address/src/kind/unified/address.rs @@ -1,4 +1,4 @@ -use zcash_protocol::PoolType; +use zcash_protocol::{constants, PoolType}; use super::{private::SealedItem, ParseError, Typecode}; @@ -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 { Self(receivers) diff --git a/components/zcash_address/src/kind/unified/fvk.rs b/components/zcash_address/src/kind/unified/fvk.rs index 52655dc703..534d6c7834 100644 --- a/components/zcash_address/src/kind/unified/fvk.rs +++ b/components/zcash_address/src/kind/unified/fvk.rs @@ -1,5 +1,6 @@ use alloc::vec::Vec; use core::convert::{TryFrom, TryInto}; +use zcash_protocol::constants; use super::{ private::{SealedContainer, SealedItem}, @@ -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 { Self(fvks) diff --git a/components/zcash_address/src/kind/unified/ivk.rs b/components/zcash_address/src/kind/unified/ivk.rs index 138c44fb09..fa613faf3e 100644 --- a/components/zcash_address/src/kind/unified/ivk.rs +++ b/components/zcash_address/src/kind/unified/ivk.rs @@ -1,4 +1,5 @@ use alloc::vec::Vec; +use zcash_protocol::constants; use core::convert::{TryFrom, TryInto}; use super::{ @@ -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 { Self(ivks) diff --git a/components/zcash_protocol/CHANGELOG.md b/components/zcash_protocol/CHANGELOG.md index 79c2cba44a..8ebbfd656e 100644 --- a/components/zcash_protocol/CHANGELOG.md +++ b/components/zcash_protocol/CHANGELOG.md @@ -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`). diff --git a/components/zcash_protocol/src/consensus.rs b/components/zcash_protocol/src/consensus.rs index 007fbe6c63..6648089e97 100644 --- a/components/zcash_protocol/src/consensus.rs +++ b/components/zcash_protocol/src/consensus.rs @@ -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. @@ -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. @@ -322,6 +367,18 @@ impl 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. diff --git a/components/zcash_protocol/src/constants/mainnet.rs b/components/zcash_protocol/src/constants/mainnet.rs index 98c81caa25..ada1a42d16 100644 --- a/components/zcash_protocol/src/constants/mainnet.rs +++ b/components/zcash_protocol/src/constants/mainnet.rs @@ -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"; diff --git a/components/zcash_protocol/src/constants/regtest.rs b/components/zcash_protocol/src/constants/regtest.rs index 001baa7ea4..c78f9a2950 100644 --- a/components/zcash_protocol/src/constants/regtest.rs +++ b/components/zcash_protocol/src/constants/regtest.rs @@ -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"; diff --git a/components/zcash_protocol/src/constants/testnet.rs b/components/zcash_protocol/src/constants/testnet.rs index 023926546e..52e90a2561 100644 --- a/components/zcash_protocol/src/constants/testnet.rs +++ b/components/zcash_protocol/src/constants/testnet.rs @@ -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";