Skip to main content

zcash_address/
constants.rs

1//! String-encoding prefixes for Zcash addresses and Unified containers.
2//!
3//! Each network's prefixes are in the [`mainnet`], [`testnet`], and [`regtest`]
4//! modules. The functions in this module select the prefix for a given
5//! [`NetworkType`].
6//!
7//! Prefixes for encodings of keys that this crate does not handle, such as Sapling
8//! extended keys, are defined in the `zcash_keys` crate.
9
10use zcash_protocol::consensus::NetworkType;
11
12/// String-encoding prefixes for the Zcash main network.
13pub mod mainnet {
14    /// The HRP for a Bech32-encoded mainnet Sapling payment address.
15    ///
16    /// Defined in the [Zcash Protocol Specification section 5.6.3.1][saplingpaymentaddrencoding].
17    ///
18    /// [saplingpaymentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#saplingpaymentaddrencoding
19    pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "zs";
20
21    /// The prefix for a Base58Check-encoded mainnet Sprout address.
22    ///
23    /// Defined in the [Zcash Protocol Specification section 5.6.2.1][sproutpaymentaddrencoding].
24    ///
25    /// [sproutpaymentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#sproutpaymentaddrencoding
26    pub const B58_SPROUT_ADDRESS_PREFIX: [u8; 2] = [0x16, 0x9a];
27
28    /// The prefix for a Base58Check-encoded mainnet transparent P2PKH address.
29    ///
30    /// Defined in the [Zcash Protocol Specification section 5.6.1.1][transparentaddrencoding].
31    ///
32    /// [transparentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#transparentaddrencoding
33    pub const B58_PUBKEY_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xb8];
34
35    /// The prefix for a Base58Check-encoded mainnet transparent P2SH address.
36    ///
37    /// Defined in the [Zcash Protocol Specification section 5.6.1.1][transparentaddrencoding].
38    ///
39    /// [transparentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#transparentaddrencoding
40    pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xbd];
41
42    /// The HRP for a Bech32m-encoded mainnet [ZIP 320] TEX address.
43    ///
44    /// [ZIP 320]: https://zips.z.cash/zip-0320
45    pub const HRP_TEX_ADDRESS: &str = "tex";
46
47    /// The HRP for a Bech32m-encoded mainnet Revision 0 Unified Address.
48    ///
49    /// Defined in [ZIP 316].
50    ///
51    /// [ZIP 316]: https://zips.z.cash/zip-0316
52    pub const HRP_UNIFIED_ADDRESS: &str = "u";
53
54    /// The HRP for a Bech32m-encoded mainnet Revision 0 Unified FVK.
55    ///
56    /// Defined in [ZIP 316].
57    ///
58    /// [ZIP 316]: https://zips.z.cash/zip-0316
59    pub const HRP_UNIFIED_FVK: &str = "uview";
60
61    /// The HRP for a Bech32m-encoded mainnet Revision 0 Unified IVK.
62    ///
63    /// Defined in [ZIP 316].
64    ///
65    /// [ZIP 316]: https://zips.z.cash/zip-0316
66    pub const HRP_UNIFIED_IVK: &str = "uivk";
67
68    /// The HRP for a Bech32m-encoded mainnet shielded-only Revision 2 Unified Address.
69    ///
70    /// Defined in [ZIP 316].
71    ///
72    /// [ZIP 316]: https://zips.z.cash/zip-0316
73    pub const HRP_UNIFIED_ADDRESS_R2: &str = "zu";
74
75    /// The HRP for a Bech32m-encoded mainnet transparent-including Revision 2 Unified
76    /// Address.
77    ///
78    /// Defined in [ZIP 316].
79    ///
80    /// [ZIP 316]: https://zips.z.cash/zip-0316
81    pub const HRP_UNIFIED_ADDRESS_R2_TI: &str = "tu";
82
83    /// The HRP for a Bech32m-encoded mainnet Revision 2 Unified FVK.
84    ///
85    /// Defined in [ZIP 316].
86    ///
87    /// [ZIP 316]: https://zips.z.cash/zip-0316
88    pub const HRP_UNIFIED_FVK_R2: &str = "uvf";
89
90    /// The HRP for a Bech32m-encoded mainnet Revision 2 Unified IVK.
91    ///
92    /// Defined in [ZIP 316].
93    ///
94    /// [ZIP 316]: https://zips.z.cash/zip-0316
95    pub const HRP_UNIFIED_IVK_R2: &str = "uvi";
96}
97
98/// String-encoding prefixes for the Zcash test network.
99pub mod testnet {
100    /// The HRP for a Bech32-encoded testnet Sapling payment address.
101    ///
102    /// Defined in the [Zcash Protocol Specification section 5.6.3.1][saplingpaymentaddrencoding].
103    ///
104    /// [saplingpaymentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#saplingpaymentaddrencoding
105    pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "ztestsapling";
106
107    /// The prefix for a Base58Check-encoded testnet Sprout address.
108    ///
109    /// Defined in the [Zcash Protocol Specification section 5.6.2.1][sproutpaymentaddrencoding].
110    ///
111    /// [sproutpaymentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#sproutpaymentaddrencoding
112    pub const B58_SPROUT_ADDRESS_PREFIX: [u8; 2] = [0x16, 0xb6];
113
114    /// The prefix for a Base58Check-encoded testnet transparent P2PKH address.
115    ///
116    /// Defined in the [Zcash Protocol Specification section 5.6.1.1][transparentaddrencoding].
117    ///
118    /// [transparentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#transparentaddrencoding
119    pub const B58_PUBKEY_ADDRESS_PREFIX: [u8; 2] = [0x1d, 0x25];
120
121    /// The prefix for a Base58Check-encoded testnet transparent P2SH address.
122    ///
123    /// Defined in the [Zcash Protocol Specification section 5.6.1.1][transparentaddrencoding].
124    ///
125    /// [transparentaddrencoding]: https://zips.z.cash/protocol/protocol.pdf#transparentaddrencoding
126    pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xba];
127
128    /// The HRP for a Bech32m-encoded testnet [ZIP 320] TEX address.
129    ///
130    /// [ZIP 320]: https://zips.z.cash/zip-0320
131    pub const HRP_TEX_ADDRESS: &str = "textest";
132
133    /// The HRP for a Bech32m-encoded testnet Revision 0 Unified Address.
134    ///
135    /// Defined in [ZIP 316].
136    ///
137    /// [ZIP 316]: https://zips.z.cash/zip-0316
138    pub const HRP_UNIFIED_ADDRESS: &str = "utest";
139
140    /// The HRP for a Bech32m-encoded testnet Revision 0 Unified FVK.
141    ///
142    /// Defined in [ZIP 316].
143    ///
144    /// [ZIP 316]: https://zips.z.cash/zip-0316
145    pub const HRP_UNIFIED_FVK: &str = "uviewtest";
146
147    /// The HRP for a Bech32m-encoded testnet Revision 0 Unified IVK.
148    ///
149    /// Defined in [ZIP 316].
150    ///
151    /// [ZIP 316]: https://zips.z.cash/zip-0316
152    pub const HRP_UNIFIED_IVK: &str = "uivktest";
153
154    /// The HRP for a Bech32m-encoded testnet shielded-only Revision 2 Unified Address.
155    ///
156    /// Defined in [ZIP 316].
157    ///
158    /// [ZIP 316]: https://zips.z.cash/zip-0316
159    pub const HRP_UNIFIED_ADDRESS_R2: &str = "zutest";
160
161    /// The HRP for a Bech32m-encoded testnet transparent-including Revision 2 Unified
162    /// Address.
163    ///
164    /// Defined in [ZIP 316].
165    ///
166    /// [ZIP 316]: https://zips.z.cash/zip-0316
167    pub const HRP_UNIFIED_ADDRESS_R2_TI: &str = "tutest";
168
169    /// The HRP for a Bech32m-encoded testnet Revision 2 Unified FVK.
170    ///
171    /// Defined in [ZIP 316].
172    ///
173    /// [ZIP 316]: https://zips.z.cash/zip-0316
174    pub const HRP_UNIFIED_FVK_R2: &str = "uvftest";
175
176    /// The HRP for a Bech32m-encoded testnet Revision 2 Unified IVK.
177    ///
178    /// Defined in [ZIP 316].
179    ///
180    /// [ZIP 316]: https://zips.z.cash/zip-0316
181    pub const HRP_UNIFIED_IVK_R2: &str = "uvitest";
182}
183
184/// String-encoding prefixes for a local regression-testing network.
185///
186/// The Base58Check prefixes are the same as on testnet, so a decoder cannot tell a
187/// regtest Base58Check address from a testnet one.
188pub mod regtest {
189    /// The HRP for a Bech32-encoded regtest Sapling payment address.
190    ///
191    /// It is defined in zcashd, and is not part of the Zcash Protocol Specification.
192    pub const HRP_SAPLING_PAYMENT_ADDRESS: &str = "zregtestsapling";
193
194    /// The prefix for a Base58Check-encoded regtest Sprout address.
195    ///
196    /// It is the same as [`super::testnet::B58_SPROUT_ADDRESS_PREFIX`].
197    pub const B58_SPROUT_ADDRESS_PREFIX: [u8; 2] = [0x16, 0xb6];
198
199    /// The prefix for a Base58Check-encoded regtest transparent P2PKH address.
200    ///
201    /// It is the same as [`super::testnet::B58_PUBKEY_ADDRESS_PREFIX`].
202    pub const B58_PUBKEY_ADDRESS_PREFIX: [u8; 2] = [0x1d, 0x25];
203
204    /// The prefix for a Base58Check-encoded regtest transparent P2SH address.
205    ///
206    /// It is the same as [`super::testnet::B58_SCRIPT_ADDRESS_PREFIX`].
207    pub const B58_SCRIPT_ADDRESS_PREFIX: [u8; 2] = [0x1c, 0xba];
208
209    /// The HRP for a Bech32m-encoded regtest [ZIP 320] TEX address.
210    ///
211    /// [ZIP 320]: https://zips.z.cash/zip-0320
212    pub const HRP_TEX_ADDRESS: &str = "texregtest";
213
214    /// The HRP for a Bech32m-encoded regtest Revision 0 Unified Address.
215    ///
216    /// Defined in [ZIP 316].
217    ///
218    /// [ZIP 316]: https://zips.z.cash/zip-0316
219    pub const HRP_UNIFIED_ADDRESS: &str = "uregtest";
220
221    /// The HRP for a Bech32m-encoded regtest Revision 0 Unified FVK.
222    ///
223    /// Defined in [ZIP 316].
224    ///
225    /// [ZIP 316]: https://zips.z.cash/zip-0316
226    pub const HRP_UNIFIED_FVK: &str = "uviewregtest";
227
228    /// The HRP for a Bech32m-encoded regtest Revision 0 Unified IVK.
229    ///
230    /// Defined in [ZIP 316].
231    ///
232    /// [ZIP 316]: https://zips.z.cash/zip-0316
233    pub const HRP_UNIFIED_IVK: &str = "uivkregtest";
234
235    /// The HRP for a Bech32m-encoded regtest shielded-only Revision 2 Unified Address.
236    ///
237    /// Defined in [ZIP 316].
238    ///
239    /// [ZIP 316]: https://zips.z.cash/zip-0316
240    pub const HRP_UNIFIED_ADDRESS_R2: &str = "zuregtest";
241
242    /// The HRP for a Bech32m-encoded regtest transparent-including Revision 2 Unified
243    /// Address.
244    ///
245    /// Defined in [ZIP 316].
246    ///
247    /// [ZIP 316]: https://zips.z.cash/zip-0316
248    pub const HRP_UNIFIED_ADDRESS_R2_TI: &str = "turegtest";
249
250    /// The HRP for a Bech32m-encoded regtest Revision 2 Unified FVK.
251    ///
252    /// Defined in [ZIP 316].
253    ///
254    /// [ZIP 316]: https://zips.z.cash/zip-0316
255    pub const HRP_UNIFIED_FVK_R2: &str = "uvfregtest";
256
257    /// The HRP for a Bech32m-encoded regtest Revision 2 Unified IVK.
258    ///
259    /// Defined in [ZIP 316].
260    ///
261    /// [ZIP 316]: https://zips.z.cash/zip-0316
262    pub const HRP_UNIFIED_IVK_R2: &str = "uviregtest";
263}
264
265/// Returns the HRP for a Bech32-encoded Sapling payment address on the given network.
266pub const fn hrp_sapling_payment_address(net: NetworkType) -> &'static str {
267    match net {
268        NetworkType::Main => mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
269        NetworkType::Test => testnet::HRP_SAPLING_PAYMENT_ADDRESS,
270        NetworkType::Regtest => regtest::HRP_SAPLING_PAYMENT_ADDRESS,
271    }
272}
273
274/// Returns the prefix for a Base58Check-encoded Sprout address on the given network.
275pub const fn b58_sprout_address_prefix(net: NetworkType) -> [u8; 2] {
276    match net {
277        NetworkType::Main => mainnet::B58_SPROUT_ADDRESS_PREFIX,
278        NetworkType::Test => testnet::B58_SPROUT_ADDRESS_PREFIX,
279        NetworkType::Regtest => regtest::B58_SPROUT_ADDRESS_PREFIX,
280    }
281}
282
283/// Returns the prefix for a Base58Check-encoded transparent P2PKH address on the given
284/// network.
285pub const fn b58_pubkey_address_prefix(net: NetworkType) -> [u8; 2] {
286    match net {
287        NetworkType::Main => mainnet::B58_PUBKEY_ADDRESS_PREFIX,
288        NetworkType::Test => testnet::B58_PUBKEY_ADDRESS_PREFIX,
289        NetworkType::Regtest => regtest::B58_PUBKEY_ADDRESS_PREFIX,
290    }
291}
292
293/// Returns the prefix for a Base58Check-encoded transparent P2SH address on the given
294/// network.
295pub const fn b58_script_address_prefix(net: NetworkType) -> [u8; 2] {
296    match net {
297        NetworkType::Main => mainnet::B58_SCRIPT_ADDRESS_PREFIX,
298        NetworkType::Test => testnet::B58_SCRIPT_ADDRESS_PREFIX,
299        NetworkType::Regtest => regtest::B58_SCRIPT_ADDRESS_PREFIX,
300    }
301}
302
303/// Returns the HRP for a Bech32m-encoded [ZIP 320] TEX address on the given network.
304///
305/// [ZIP 320]: https://zips.z.cash/zip-0320
306pub const fn hrp_tex_address(net: NetworkType) -> &'static str {
307    match net {
308        NetworkType::Main => mainnet::HRP_TEX_ADDRESS,
309        NetworkType::Test => testnet::HRP_TEX_ADDRESS,
310        NetworkType::Regtest => regtest::HRP_TEX_ADDRESS,
311    }
312}
313
314/// Returns the HRP for a Bech32m-encoded Revision 0 Unified Address on the given network.
315pub const fn hrp_unified_address(net: NetworkType) -> &'static str {
316    match net {
317        NetworkType::Main => mainnet::HRP_UNIFIED_ADDRESS,
318        NetworkType::Test => testnet::HRP_UNIFIED_ADDRESS,
319        NetworkType::Regtest => regtest::HRP_UNIFIED_ADDRESS,
320    }
321}
322
323/// Returns the HRP for a Bech32m-encoded Revision 0 Unified FVK on the given network.
324pub const fn hrp_unified_fvk(net: NetworkType) -> &'static str {
325    match net {
326        NetworkType::Main => mainnet::HRP_UNIFIED_FVK,
327        NetworkType::Test => testnet::HRP_UNIFIED_FVK,
328        NetworkType::Regtest => regtest::HRP_UNIFIED_FVK,
329    }
330}
331
332/// Returns the HRP for a Bech32m-encoded Revision 0 Unified IVK on the given network.
333pub const fn hrp_unified_ivk(net: NetworkType) -> &'static str {
334    match net {
335        NetworkType::Main => mainnet::HRP_UNIFIED_IVK,
336        NetworkType::Test => testnet::HRP_UNIFIED_IVK,
337        NetworkType::Regtest => regtest::HRP_UNIFIED_IVK,
338    }
339}
340
341/// Returns the HRP for a Bech32m-encoded shielded-only Revision 2 Unified Address on the
342/// given network.
343pub const fn hrp_unified_address_r2(net: NetworkType) -> &'static str {
344    match net {
345        NetworkType::Main => mainnet::HRP_UNIFIED_ADDRESS_R2,
346        NetworkType::Test => testnet::HRP_UNIFIED_ADDRESS_R2,
347        NetworkType::Regtest => regtest::HRP_UNIFIED_ADDRESS_R2,
348    }
349}
350
351/// Returns the HRP for a Bech32m-encoded transparent-including Revision 2 Unified Address
352/// on the given network.
353pub const fn hrp_unified_address_r2_ti(net: NetworkType) -> &'static str {
354    match net {
355        NetworkType::Main => mainnet::HRP_UNIFIED_ADDRESS_R2_TI,
356        NetworkType::Test => testnet::HRP_UNIFIED_ADDRESS_R2_TI,
357        NetworkType::Regtest => regtest::HRP_UNIFIED_ADDRESS_R2_TI,
358    }
359}
360
361/// Returns the HRP for a Bech32m-encoded Revision 2 Unified FVK on the given network.
362pub const fn hrp_unified_fvk_r2(net: NetworkType) -> &'static str {
363    match net {
364        NetworkType::Main => mainnet::HRP_UNIFIED_FVK_R2,
365        NetworkType::Test => testnet::HRP_UNIFIED_FVK_R2,
366        NetworkType::Regtest => regtest::HRP_UNIFIED_FVK_R2,
367    }
368}
369
370/// Returns the HRP for a Bech32m-encoded Revision 2 Unified IVK on the given network.
371pub const fn hrp_unified_ivk_r2(net: NetworkType) -> &'static str {
372    match net {
373        NetworkType::Main => mainnet::HRP_UNIFIED_IVK_R2,
374        NetworkType::Test => testnet::HRP_UNIFIED_IVK_R2,
375        NetworkType::Regtest => regtest::HRP_UNIFIED_IVK_R2,
376    }
377}
378
379#[cfg(test)]
380mod tests {
381    use zcash_protocol::consensus::{NetworkConstants, NetworkType};
382
383    const NETWORKS: [NetworkType; 3] = [NetworkType::Main, NetworkType::Test, NetworkType::Regtest];
384
385    /// The `zcash_protocol` copies of these prefixes must not drift from the definitions
386    /// here while both exist.
387    #[test]
388    #[allow(deprecated)]
389    fn zcash_protocol_copies_match() {
390        for net in NETWORKS {
391            assert_eq!(
392                super::hrp_sapling_payment_address(net),
393                net.hrp_sapling_payment_address()
394            );
395            assert_eq!(
396                super::b58_sprout_address_prefix(net),
397                net.b58_sprout_address_prefix()
398            );
399            assert_eq!(
400                super::b58_pubkey_address_prefix(net),
401                net.b58_pubkey_address_prefix()
402            );
403            assert_eq!(
404                super::b58_script_address_prefix(net),
405                net.b58_script_address_prefix()
406            );
407            assert_eq!(super::hrp_tex_address(net), net.hrp_tex_address());
408            assert_eq!(super::hrp_unified_address(net), net.hrp_unified_address());
409            assert_eq!(super::hrp_unified_fvk(net), net.hrp_unified_fvk());
410            assert_eq!(super::hrp_unified_ivk(net), net.hrp_unified_ivk());
411        }
412    }
413}