zcash_address/
lib.rs

1//! *Parser for all defined Zcash address types.*
2//!
3//! This crate implements address parsing as a two-phase process, built around the opaque
4//! [`ZcashAddress`] type.
5//!
6//! - [`ZcashAddress`] can be parsed from, and encoded to, strings.
7//! - [`ZcashAddress::convert`] or [`ZcashAddress::convert_if_network`] can be used to
8//!   convert a parsed address into custom types that implement the [`TryFromAddress`] trait.
9//! - Custom types can be converted into a [`ZcashAddress`] via its implementation of the
10//!   [`ToAddress`] trait.
11//!
12//! ```text
13//!         s.parse()              .convert()
14//!         -------->              --------->
15//! Strings           ZcashAddress            Custom types
16//!         <--------              <---------
17//!         .encode()              ToAddress
18//! ```
19//!
20//! It is important to note that this crate does not depend on any of the Zcash protocol
21//! crates (e.g. `sapling-crypto` or `orchard`). This crate has minimal dependencies by
22//! design; it focuses solely on parsing, handling those concerns for you, while exposing
23//! APIs that enable you to convert the parsed data into the Rust types you want to use.
24//!
25//! # Using this crate
26//!
27//! ## I just need to validate Zcash addresses
28//!
29//! ```
30//! # use zcash_address::ZcashAddress;
31//! fn is_valid_zcash_address(addr_string: &str) -> bool {
32//!     addr_string.parse::<ZcashAddress>().is_ok()
33//! }
34//! ```
35//!
36//! ## I want to parse Zcash addresses in a Rust wallet app that uses the `zcash_primitives` transaction builder
37//!
38//! Use `zcash_client_backend::address::RecipientAddress`, which implements the traits in
39//! this crate to parse address strings into protocol types that work with the transaction
40//! builder in the `zcash_primitives` crate (as well as the wallet functionality in the
41//! `zcash_client_backend` crate itself).
42//!
43//! > We intend to refactor the key and address types from the `zcash_client_backend` and
44//! > `zcash_primitives` crates into a separate crate focused on dealing with Zcash key
45//! > material. That crate will then be what you should use.
46//!
47//! ## I want to parse Unified Addresses
48//!
49//! See the [`unified::Address`] documentation for examples.
50//!
51//! While the [`unified::Address`] type does have parsing methods, you should still parse
52//! your address strings with [`ZcashAddress`] and then convert; this will ensure that for
53//! other Zcash address types you get a [`ConversionError::Unsupported`], which is a
54//! better error for your users.
55//!
56//! ## I want to parse mainnet Zcash addresses in a language that supports C FFI
57//!
58//! As an example, you could use static functions to create the address types in the
59//! target language from the parsed data.
60//!
61//! ```
62//! use std::ffi::{CStr, c_char, c_void};
63//! use std::ptr;
64//!
65//! use zcash_address::{ConversionError, TryFromAddress, ZcashAddress};
66//! use zcash_protocol::consensus::NetworkType;
67//!
68//! // Functions that return a pointer to a heap-allocated address of the given kind in
69//! // the target language. These should be augmented to return any relevant errors.
70//! extern {
71//!     fn addr_from_sapling(data: *const u8) -> *mut c_void;
72//!     fn addr_from_transparent_p2pkh(data: *const u8) -> *mut c_void;
73//! }
74//!
75//! struct ParsedAddress(*mut c_void);
76//!
77//! impl TryFromAddress for ParsedAddress {
78//!     type Error = &'static str;
79//!
80//!     fn try_from_sapling(
81//!         _net: NetworkType,
82//!         data: [u8; 43],
83//!     ) -> Result<Self, ConversionError<Self::Error>> {
84//!         let parsed = unsafe { addr_from_sapling(data[..].as_ptr()) };
85//!         if parsed.is_null() {
86//!             Err("Reason for the failure".into())
87//!         } else {
88//!             Ok(Self(parsed))
89//!         }
90//!     }
91//!
92//!     fn try_from_transparent_p2pkh(
93//!         _net: NetworkType,
94//!         data: [u8; 20],
95//!     ) -> Result<Self, ConversionError<Self::Error>> {
96//!         let parsed = unsafe { addr_from_transparent_p2pkh(data[..].as_ptr()) };
97//!         if parsed.is_null() {
98//!             Err("Reason for the failure".into())
99//!         } else {
100//!             Ok(Self(parsed))
101//!         }
102//!     }
103//! }
104//!
105//! pub extern "C" fn parse_zcash_address(encoded: *const c_char) -> *mut c_void {
106//!     let encoded = unsafe { CStr::from_ptr(encoded) }.to_str().expect("valid");
107//!
108//!     let addr = match ZcashAddress::try_from_encoded(encoded) {
109//!         Ok(addr) => addr,
110//!         Err(e) => {
111//!             // This was either an invalid address encoding, or not a Zcash address.
112//!             // You should pass this error back across the FFI.
113//!             return ptr::null_mut();
114//!         }
115//!     };
116//!
117//!     match addr.convert_if_network::<ParsedAddress>(NetworkType::Main) {
118//!         Ok(parsed) => parsed.0,
119//!         Err(e) => {
120//!             // We didn't implement all of the methods of `TryFromAddress`, so if an
121//!             // address with one of those kinds is parsed, it will result in an error
122//!             // here that should be passed back across the FFI.
123//!             ptr::null_mut()
124//!         }
125//!     }
126//! }
127//! ```
128
129#![no_std]
130#![cfg_attr(docsrs, feature(doc_cfg))]
131#![cfg_attr(docsrs, feature(doc_auto_cfg))]
132// Catch documentation errors caused by code changes.
133#![deny(rustdoc::broken_intra_doc_links)]
134
135#[macro_use]
136extern crate alloc;
137
138#[cfg(feature = "std")]
139extern crate std;
140
141use alloc::string::String;
142
143mod convert;
144mod encoding;
145mod kind;
146
147#[cfg(any(test, feature = "test-dependencies"))]
148pub mod test_vectors;
149
150pub use convert::{ConversionError, Converter, ToAddress, TryFromAddress, UnsupportedAddress};
151pub use encoding::ParseError;
152pub use kind::unified;
153use kind::unified::Receiver;
154
155#[deprecated(note = "use ::zcash_protocol::consensus::NetworkType instead")]
156pub type Network = zcash_protocol::consensus::NetworkType;
157
158use zcash_protocol::{consensus::NetworkType, PoolType};
159
160/// A Zcash address.
161#[derive(Clone, Debug, PartialEq, Eq, Hash)]
162pub struct ZcashAddress {
163    net: NetworkType,
164    kind: AddressKind,
165}
166
167/// Known kinds of Zcash addresses.
168#[derive(Clone, Debug, PartialEq, Eq, Hash)]
169enum AddressKind {
170    Sprout([u8; 64]),
171    Sapling([u8; 43]),
172    Unified(unified::Address),
173    P2pkh([u8; 20]),
174    P2sh([u8; 20]),
175    Tex([u8; 20]),
176}
177
178impl ZcashAddress {
179    /// Encodes this Zcash address in its canonical string representation.
180    ///
181    /// This provides the encoded string representation of the address as defined by the
182    /// [Zcash protocol specification](https://zips.z.cash/protocol/protocol.pdf) and/or
183    /// [ZIP 316](https://zips.z.cash/zip-0316). The [`Display` implementation] can also
184    /// be used to produce this encoding using [`address.to_string()`].
185    ///
186    /// [`Display` implementation]: core::fmt::Display
187    /// [`address.to_string()`]: alloc::string::ToString
188    pub fn encode(&self) -> String {
189        format!("{}", self)
190    }
191
192    /// Attempts to parse the given string as a Zcash address.
193    ///
194    /// This simply calls [`s.parse()`], leveraging the [`FromStr` implementation].
195    ///
196    /// [`s.parse()`]: str::parse
197    /// [`FromStr` implementation]: ZcashAddress#impl-FromStr
198    ///
199    /// # Errors
200    ///
201    /// - If the parser can detect that the string _must_ contain an address encoding used
202    ///   by Zcash, [`ParseError::InvalidEncoding`] will be returned if any subsequent
203    ///   part of that encoding is invalid.
204    ///
205    /// - In all other cases, [`ParseError::NotZcash`] will be returned on failure.
206    ///
207    /// # Examples
208    ///
209    /// ```
210    /// use zcash_address::ZcashAddress;
211    ///
212    /// let encoded = "zs1z7rejlpsa98s2rrrfkwmaxu53e4ue0ulcrw0h4x5g8jl04tak0d3mm47vdtahatqrlkngh9slya";
213    /// let addr = ZcashAddress::try_from_encoded(&encoded);
214    /// assert_eq!(encoded.parse(), addr);
215    /// ```
216    pub fn try_from_encoded(s: &str) -> Result<Self, ParseError> {
217        s.parse()
218    }
219
220    /// Converts this address into another type.
221    ///
222    /// `convert` can convert into any type that implements the [`TryFromAddress`] trait.
223    /// This enables `ZcashAddress` to be used as a common parsing and serialization
224    /// interface for Zcash addresses, while delegating operations on those addresses
225    /// (such as constructing transactions) to downstream crates.
226    ///
227    /// If you want to get the encoded string for this address, use the [`encode`]
228    /// method or the [`Display` implementation] via [`address.to_string()`] instead.
229    ///
230    /// [`encode`]: Self::encode
231    /// [`Display` implementation]: core::fmt::Display
232    /// [`address.to_string()`]: alloc::string::ToString
233    pub fn convert<T: TryFromAddress>(self) -> Result<T, ConversionError<T::Error>> {
234        match self.kind {
235            AddressKind::Sprout(data) => T::try_from_sprout(self.net, data),
236            AddressKind::Sapling(data) => T::try_from_sapling(self.net, data),
237            AddressKind::Unified(data) => T::try_from_unified(self.net, data),
238            AddressKind::P2pkh(data) => T::try_from_transparent_p2pkh(self.net, data),
239            AddressKind::P2sh(data) => T::try_from_transparent_p2sh(self.net, data),
240            AddressKind::Tex(data) => T::try_from_tex(self.net, data),
241        }
242    }
243
244    /// Converts this address into another type, if it matches the expected network.
245    ///
246    /// `convert_if_network` can convert into any type that implements the [`TryFromAddress`]
247    /// trait. This enables `ZcashAddress` to be used as a common parsing and serialization
248    /// interface for Zcash addresses, while delegating operations on those addresses (such as
249    /// constructing transactions) to downstream crates.
250    ///
251    /// If you want to get the encoded string for this address, use the [`encode`]
252    /// method or the [`Display` implementation] via [`address.to_string()`] instead.
253    ///
254    /// [`encode`]: Self::encode
255    /// [`Display` implementation]: core::fmt::Display
256    /// [`address.to_string()`]: alloc::string::ToString
257    pub fn convert_if_network<T: TryFromAddress>(
258        self,
259        net: NetworkType,
260    ) -> Result<T, ConversionError<T::Error>> {
261        let network_matches = self.net == net;
262        // The Sprout and transparent address encodings use the same prefix for testnet
263        // and regtest, so we need to allow parsing testnet addresses as regtest.
264        let regtest_exception =
265            network_matches || (self.net == NetworkType::Test && net == NetworkType::Regtest);
266
267        match self.kind {
268            AddressKind::Sprout(data) if regtest_exception => T::try_from_sprout(net, data),
269            AddressKind::Sapling(data) if network_matches => T::try_from_sapling(net, data),
270            AddressKind::Unified(data) if network_matches => T::try_from_unified(net, data),
271            AddressKind::P2pkh(data) if regtest_exception => {
272                T::try_from_transparent_p2pkh(net, data)
273            }
274            AddressKind::P2sh(data) if regtest_exception => T::try_from_transparent_p2sh(net, data),
275            AddressKind::Tex(data) if network_matches => T::try_from_tex(net, data),
276            _ => Err(ConversionError::IncorrectNetwork {
277                expected: net,
278                actual: self.net,
279            }),
280        }
281    }
282
283    /// Converts this address into another type using the specified converter.
284    ///
285    /// `convert` can convert into any type `T` for which an implementation of the [`Converter<T>`]
286    /// trait exists. This enables conversion of [`ZcashAddress`] values into other types to rely
287    /// on additional context.
288    pub fn convert_with<T, C: Converter<T>>(
289        self,
290        converter: C,
291    ) -> Result<T, ConversionError<C::Error>> {
292        match self.kind {
293            AddressKind::Sprout(data) => converter.convert_sprout(self.net, data),
294            AddressKind::Sapling(data) => converter.convert_sapling(self.net, data),
295            AddressKind::Unified(data) => converter.convert_unified(self.net, data),
296            AddressKind::P2pkh(data) => converter.convert_transparent_p2pkh(self.net, data),
297            AddressKind::P2sh(data) => converter.convert_transparent_p2sh(self.net, data),
298            AddressKind::Tex(data) => converter.convert_tex(self.net, data),
299        }
300    }
301
302    /// Returns whether this address has the ability to receive transfers of the given pool type.
303    pub fn can_receive_as(&self, pool_type: PoolType) -> bool {
304        use AddressKind::*;
305        match &self.kind {
306            Sprout(_) => false,
307            Sapling(_) => pool_type == PoolType::SAPLING,
308            Unified(addr) => addr.has_receiver_of_type(pool_type),
309            P2pkh(_) | P2sh(_) | Tex(_) => pool_type == PoolType::TRANSPARENT,
310        }
311    }
312
313    /// Returns whether this address can receive a memo.
314    pub fn can_receive_memo(&self) -> bool {
315        use AddressKind::*;
316        match &self.kind {
317            Sprout(_) | Sapling(_) => true,
318            Unified(addr) => addr.can_receive_memo(),
319            P2pkh(_) | P2sh(_) | Tex(_) => false,
320        }
321    }
322
323    /// Returns whether or not this address contains or corresponds to the given unified address
324    /// receiver.
325    pub fn matches_receiver(&self, receiver: &Receiver) -> bool {
326        match (&self.kind, receiver) {
327            (AddressKind::Unified(ua), r) => ua.contains_receiver(r),
328            (AddressKind::Sapling(d), Receiver::Sapling(r)) => r == d,
329            (AddressKind::P2pkh(d), Receiver::P2pkh(r)) => r == d,
330            (AddressKind::Tex(d), Receiver::P2pkh(r)) => r == d,
331            (AddressKind::P2sh(d), Receiver::P2sh(r)) => r == d,
332            _ => false,
333        }
334    }
335}
336
337#[cfg(feature = "test-dependencies")]
338pub mod testing {
339    use core::convert::TryInto;
340
341    use proptest::{array::uniform20, collection::vec, prelude::any, prop_compose, prop_oneof};
342
343    use crate::{unified::address::testing::arb_unified_address, AddressKind, ZcashAddress};
344    use zcash_protocol::consensus::NetworkType;
345
346    prop_compose! {
347        fn arb_sprout_addr_kind()(
348            r_bytes in vec(any::<u8>(), 64)
349        ) -> AddressKind {
350            AddressKind::Sprout(r_bytes.try_into().unwrap())
351        }
352    }
353
354    prop_compose! {
355        fn arb_sapling_addr_kind()(
356            r_bytes in vec(any::<u8>(), 43)
357        ) -> AddressKind {
358            AddressKind::Sapling(r_bytes.try_into().unwrap())
359        }
360    }
361
362    prop_compose! {
363        fn arb_p2pkh_addr_kind()(
364            r_bytes in uniform20(any::<u8>())
365        ) -> AddressKind {
366            AddressKind::P2pkh(r_bytes)
367        }
368    }
369
370    prop_compose! {
371        fn arb_p2sh_addr_kind()(
372            r_bytes in uniform20(any::<u8>())
373        ) -> AddressKind {
374            AddressKind::P2sh(r_bytes)
375        }
376    }
377
378    prop_compose! {
379        fn arb_unified_addr_kind()(
380            uaddr in arb_unified_address()
381        ) -> AddressKind {
382            AddressKind::Unified(uaddr)
383        }
384    }
385
386    prop_compose! {
387        fn arb_tex_addr_kind()(
388            r_bytes in uniform20(any::<u8>())
389        ) -> AddressKind {
390            AddressKind::Tex(r_bytes)
391        }
392    }
393
394    prop_compose! {
395        /// Create an arbitrary, structurally-valid `ZcashAddress` value.
396        ///
397        /// Note that the data contained in the generated address does _not_ necessarily correspond
398        /// to a valid address according to the Zcash protocol; binary data in the resulting value
399        /// is entirely random.
400        pub fn arb_address(net: NetworkType)(
401            kind in prop_oneof!(
402                arb_sprout_addr_kind(),
403                arb_sapling_addr_kind(),
404                arb_p2pkh_addr_kind(),
405                arb_p2sh_addr_kind(),
406                arb_unified_addr_kind(),
407                arb_tex_addr_kind()
408            )
409        ) -> ZcashAddress {
410            ZcashAddress { net, kind }
411        }
412    }
413}