|
1 | 1 | use std::{error::Error, fmt};
|
2 | 2 |
|
3 |
| -use crate::{kind::*, Network}; |
| 3 | +use crate::{kind::*, AddressKind, Network, ZcashAddress}; |
4 | 4 |
|
5 | 5 | /// An address type is not supported for conversion.
|
6 | 6 | #[derive(Debug)]
|
@@ -74,3 +74,102 @@ pub trait FromAddress: Sized {
|
74 | 74 | Err(UnsupportedAddress("transparent P2SH"))
|
75 | 75 | }
|
76 | 76 | }
|
| 77 | + |
| 78 | +/// A helper trait for converting another type into a [`ZcashAddress`]. |
| 79 | +/// |
| 80 | +/// This trait is sealed and cannot be implemented for types outside this crate. Its |
| 81 | +/// purpose is to move these conversion functions out of the main `ZcashAddress` API |
| 82 | +/// documentation, as they are only required when creating addresses (rather than when |
| 83 | +/// parsing addresses, which is a more common occurrence). |
| 84 | +/// |
| 85 | +/// [`ZcashAddress`]: crate::ZcashAddress |
| 86 | +/// |
| 87 | +/// # Examples |
| 88 | +/// |
| 89 | +/// ``` |
| 90 | +/// use zcash_address::{ToAddress, Network, ZcashAddress}; |
| 91 | +/// |
| 92 | +/// #[derive(Debug)] |
| 93 | +/// struct MySapling([u8; 43]); |
| 94 | +/// |
| 95 | +/// impl MySapling { |
| 96 | +/// /// Encodes this Sapling address for the given network. |
| 97 | +/// fn encode(&self, net: Network) -> ZcashAddress { |
| 98 | +/// ZcashAddress::from_sapling(net, self.0) |
| 99 | +/// } |
| 100 | +/// } |
| 101 | +/// |
| 102 | +/// let addr = MySapling([0; 43]); |
| 103 | +/// let encoded = addr.encode(Network::Main); |
| 104 | +/// assert_eq!( |
| 105 | +/// encoded.to_string(), |
| 106 | +/// "zs1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpq6d8g", |
| 107 | +/// ); |
| 108 | +/// ``` |
| 109 | +pub trait ToAddress: private::Sealed { |
| 110 | + fn from_sprout(net: Network, data: sprout::Data) -> Self; |
| 111 | + |
| 112 | + fn from_sapling(net: Network, data: sapling::Data) -> Self; |
| 113 | + |
| 114 | + fn from_orchard(net: Network, data: orchard::Data) -> Self; |
| 115 | + |
| 116 | + fn from_transparent_p2pkh(net: Network, data: p2pkh::Data) -> Self; |
| 117 | + |
| 118 | + fn from_transparent_p2sh(net: Network, data: p2sh::Data) -> Self; |
| 119 | +} |
| 120 | + |
| 121 | +impl ToAddress for ZcashAddress { |
| 122 | + fn from_sprout(net: Network, data: sprout::Data) -> Self { |
| 123 | + ZcashAddress { |
| 124 | + net: if let Network::Regtest = net { |
| 125 | + Network::Test |
| 126 | + } else { |
| 127 | + net |
| 128 | + }, |
| 129 | + kind: AddressKind::Sprout(data), |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + fn from_sapling(net: Network, data: sapling::Data) -> Self { |
| 134 | + ZcashAddress { |
| 135 | + net, |
| 136 | + kind: AddressKind::Sapling(data), |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + fn from_orchard(net: Network, data: orchard::Data) -> Self { |
| 141 | + ZcashAddress { |
| 142 | + net, |
| 143 | + kind: AddressKind::Orchard(data), |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + fn from_transparent_p2pkh(net: Network, data: p2pkh::Data) -> Self { |
| 148 | + ZcashAddress { |
| 149 | + net: if let Network::Regtest = net { |
| 150 | + Network::Test |
| 151 | + } else { |
| 152 | + net |
| 153 | + }, |
| 154 | + kind: AddressKind::P2pkh(data), |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + fn from_transparent_p2sh(net: Network, data: p2sh::Data) -> Self { |
| 159 | + ZcashAddress { |
| 160 | + net: if let Network::Regtest = net { |
| 161 | + Network::Test |
| 162 | + } else { |
| 163 | + net |
| 164 | + }, |
| 165 | + kind: AddressKind::P2sh(data), |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +mod private { |
| 171 | + use crate::ZcashAddress; |
| 172 | + |
| 173 | + pub trait Sealed {} |
| 174 | + impl Sealed for ZcashAddress {} |
| 175 | +} |
0 commit comments