@@ -3,7 +3,9 @@ use core::fmt;
33#[ cfg( feature = "std" ) ]
44use std:: error:: Error ;
55
6- use crate :: { kind:: * , AddressKind , Network , ZcashAddress } ;
6+ use zcash_protocol:: consensus:: NetworkType ;
7+
8+ use crate :: { kind:: * , AddressKind , ZcashAddress } ;
79
810/// An error indicating that an address type is not supported for conversion.
911#[ derive( Debug ) ]
@@ -19,7 +21,10 @@ impl fmt::Display for UnsupportedAddress {
1921#[ derive( Debug ) ]
2022pub enum ConversionError < E > {
2123 /// The address is for the wrong network.
22- IncorrectNetwork { expected : Network , actual : Network } ,
24+ IncorrectNetwork {
25+ expected : NetworkType ,
26+ actual : NetworkType ,
27+ } ,
2328 /// The address type is not supported by the target type.
2429 Unsupported ( UnsupportedAddress ) ,
2530 /// A conversion error returned by the target type.
@@ -60,15 +65,16 @@ impl<E: Error + 'static> Error for ConversionError<E> {
6065
6166/// A helper trait for converting a [`ZcashAddress`] into a network-agnostic type.
6267///
63- /// A blanket implementation of [`TryFromAddress`] is provided for `(Network , T)` where
68+ /// A blanket implementation of [`TryFromAddress`] is provided for `(NetworkType , T)` where
6469/// `T: TryFromRawAddress`.
6570///
6671/// [`ZcashAddress`]: crate::ZcashAddress
6772///
6873/// # Examples
6974///
7075/// ```
71- /// use zcash_address::{ConversionError, Network, TryFromRawAddress, UnsupportedAddress, ZcashAddress};
76+ /// use zcash_address::{ConversionError, TryFromRawAddress, UnsupportedAddress, ZcashAddress};
77+ /// use zcash_protocol::consensus::NetworkType;
7278///
7379/// #[derive(Debug, PartialEq)]
7480/// struct MySapling([u8; 43]);
@@ -90,15 +96,15 @@ impl<E: Error + 'static> Error for ConversionError<E> {
9096///
9197/// // You can use `ZcashAddress::convert_if_network` to get your type directly.
9298/// let addr: ZcashAddress = addr_string.parse().unwrap();
93- /// let converted = addr.convert_if_network::<MySapling>(Network ::Main);
99+ /// let converted = addr.convert_if_network::<MySapling>(NetworkType ::Main);
94100/// assert!(converted.is_ok());
95101/// assert_eq!(converted.unwrap(), MySapling([0; 43]));
96102///
97103/// // Using `ZcashAddress::convert` gives us the tuple `(network, converted_addr)`.
98104/// let addr: ZcashAddress = addr_string.parse().unwrap();
99105/// let converted = addr.convert::<(_, MySapling)>();
100106/// assert!(converted.is_ok());
101- /// assert_eq!(converted.unwrap(), (Network ::Main, MySapling([0; 43])));
107+ /// assert_eq!(converted.unwrap(), (NetworkType ::Main, MySapling([0; 43])));
102108///
103109/// // For an unsupported address type, we get an error.
104110/// let addr: ZcashAddress = "t1Hsc1LR8yKnbbe3twRp88p6vFfC5t7DLbs".parse().unwrap();
@@ -158,7 +164,8 @@ pub trait TryFromRawAddress: Sized {
158164/// # Examples
159165///
160166/// ```
161- /// use zcash_address::{ConversionError, Network, TryFromAddress, UnsupportedAddress, ZcashAddress};
167+ /// use zcash_address::{ConversionError, TryFromAddress, UnsupportedAddress, ZcashAddress};
168+ /// use zcash_protocol::consensus::NetworkType;
162169///
163170/// #[derive(Debug)]
164171/// struct MySapling([u8; 43]);
@@ -171,7 +178,7 @@ pub trait TryFromRawAddress: Sized {
171178/// type Error = &'static str;
172179///
173180/// fn try_from_sapling(
174- /// net: Network ,
181+ /// net: NetworkType ,
175182/// data: [u8; 43],
176183/// ) -> Result<Self, ConversionError<Self::Error>> {
177184/// Ok(MySapling(data))
@@ -197,29 +204,32 @@ pub trait TryFromAddress: Sized {
197204 /// [`Self::try_from_sapling`] as a valid Sapling address).
198205 type Error ;
199206
200- fn try_from_sprout ( net : Network , data : [ u8 ; 64 ] ) -> Result < Self , ConversionError < Self :: Error > > {
207+ fn try_from_sprout (
208+ net : NetworkType ,
209+ data : [ u8 ; 64 ] ,
210+ ) -> Result < Self , ConversionError < Self :: Error > > {
201211 let _ = ( net, data) ;
202212 Err ( ConversionError :: Unsupported ( UnsupportedAddress ( "Sprout" ) ) )
203213 }
204214
205215 fn try_from_sapling (
206- net : Network ,
216+ net : NetworkType ,
207217 data : [ u8 ; 43 ] ,
208218 ) -> Result < Self , ConversionError < Self :: Error > > {
209219 let _ = ( net, data) ;
210220 Err ( ConversionError :: Unsupported ( UnsupportedAddress ( "Sapling" ) ) )
211221 }
212222
213223 fn try_from_unified (
214- net : Network ,
224+ net : NetworkType ,
215225 data : unified:: Address ,
216226 ) -> Result < Self , ConversionError < Self :: Error > > {
217227 let _ = ( net, data) ;
218228 Err ( ConversionError :: Unsupported ( UnsupportedAddress ( "Unified" ) ) )
219229 }
220230
221231 fn try_from_transparent_p2pkh (
222- net : Network ,
232+ net : NetworkType ,
223233 data : [ u8 ; 20 ] ,
224234 ) -> Result < Self , ConversionError < Self :: Error > > {
225235 let _ = ( net, data) ;
@@ -229,7 +239,7 @@ pub trait TryFromAddress: Sized {
229239 }
230240
231241 fn try_from_transparent_p2sh (
232- net : Network ,
242+ net : NetworkType ,
233243 data : [ u8 ; 20 ] ,
234244 ) -> Result < Self , ConversionError < Self :: Error > > {
235245 let _ = ( net, data) ;
@@ -238,50 +248,59 @@ pub trait TryFromAddress: Sized {
238248 ) ) )
239249 }
240250
241- fn try_from_tex ( net : Network , data : [ u8 ; 20 ] ) -> Result < Self , ConversionError < Self :: Error > > {
251+ fn try_from_tex (
252+ net : NetworkType ,
253+ data : [ u8 ; 20 ] ,
254+ ) -> Result < Self , ConversionError < Self :: Error > > {
242255 let _ = ( net, data) ;
243256 Err ( ConversionError :: Unsupported ( UnsupportedAddress (
244257 "transparent-source restricted P2PKH" ,
245258 ) ) )
246259 }
247260}
248261
249- impl < T : TryFromRawAddress > TryFromAddress for ( Network , T ) {
262+ impl < T : TryFromRawAddress > TryFromAddress for ( NetworkType , T ) {
250263 type Error = T :: Error ;
251264
252- fn try_from_sprout ( net : Network , data : [ u8 ; 64 ] ) -> Result < Self , ConversionError < Self :: Error > > {
265+ fn try_from_sprout (
266+ net : NetworkType ,
267+ data : [ u8 ; 64 ] ,
268+ ) -> Result < Self , ConversionError < Self :: Error > > {
253269 T :: try_from_raw_sprout ( data) . map ( |addr| ( net, addr) )
254270 }
255271
256272 fn try_from_sapling (
257- net : Network ,
273+ net : NetworkType ,
258274 data : [ u8 ; 43 ] ,
259275 ) -> Result < Self , ConversionError < Self :: Error > > {
260276 T :: try_from_raw_sapling ( data) . map ( |addr| ( net, addr) )
261277 }
262278
263279 fn try_from_unified (
264- net : Network ,
280+ net : NetworkType ,
265281 data : unified:: Address ,
266282 ) -> Result < Self , ConversionError < Self :: Error > > {
267283 T :: try_from_raw_unified ( data) . map ( |addr| ( net, addr) )
268284 }
269285
270286 fn try_from_transparent_p2pkh (
271- net : Network ,
287+ net : NetworkType ,
272288 data : [ u8 ; 20 ] ,
273289 ) -> Result < Self , ConversionError < Self :: Error > > {
274290 T :: try_from_raw_transparent_p2pkh ( data) . map ( |addr| ( net, addr) )
275291 }
276292
277293 fn try_from_transparent_p2sh (
278- net : Network ,
294+ net : NetworkType ,
279295 data : [ u8 ; 20 ] ,
280296 ) -> Result < Self , ConversionError < Self :: Error > > {
281297 T :: try_from_raw_transparent_p2sh ( data) . map ( |addr| ( net, addr) )
282298 }
283299
284- fn try_from_tex ( net : Network , data : [ u8 ; 20 ] ) -> Result < Self , ConversionError < Self :: Error > > {
300+ fn try_from_tex (
301+ net : NetworkType ,
302+ data : [ u8 ; 20 ] ,
303+ ) -> Result < Self , ConversionError < Self :: Error > > {
285304 T :: try_from_raw_tex ( data) . map ( |addr| ( net, addr) )
286305 }
287306}
@@ -298,88 +317,89 @@ impl<T: TryFromRawAddress> TryFromAddress for (Network, T) {
298317/// # Examples
299318///
300319/// ```
301- /// use zcash_address::{ToAddress, Network, ZcashAddress};
320+ /// use zcash_address::{ToAddress, ZcashAddress};
321+ /// use zcash_protocol::consensus::NetworkType;
302322///
303323/// #[derive(Debug)]
304324/// struct MySapling([u8; 43]);
305325///
306326/// impl MySapling {
307327/// /// Encodes this Sapling address for the given network.
308- /// fn encode(&self, net: Network ) -> ZcashAddress {
328+ /// fn encode(&self, net: NetworkType ) -> ZcashAddress {
309329/// ZcashAddress::from_sapling(net, self.0)
310330/// }
311331/// }
312332///
313333/// let addr = MySapling([0; 43]);
314- /// let encoded = addr.encode(Network ::Main);
334+ /// let encoded = addr.encode(NetworkType ::Main);
315335/// assert_eq!(
316336/// encoded.to_string(),
317337/// "zs1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpq6d8g",
318338/// );
319339/// ```
320340pub trait ToAddress : private:: Sealed {
321- fn from_sprout ( net : Network , data : [ u8 ; 64 ] ) -> Self ;
341+ fn from_sprout ( net : NetworkType , data : [ u8 ; 64 ] ) -> Self ;
322342
323- fn from_sapling ( net : Network , data : [ u8 ; 43 ] ) -> Self ;
343+ fn from_sapling ( net : NetworkType , data : [ u8 ; 43 ] ) -> Self ;
324344
325- fn from_unified ( net : Network , data : unified:: Address ) -> Self ;
345+ fn from_unified ( net : NetworkType , data : unified:: Address ) -> Self ;
326346
327- fn from_transparent_p2pkh ( net : Network , data : [ u8 ; 20 ] ) -> Self ;
347+ fn from_transparent_p2pkh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self ;
328348
329- fn from_transparent_p2sh ( net : Network , data : [ u8 ; 20 ] ) -> Self ;
349+ fn from_transparent_p2sh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self ;
330350
331- fn from_tex ( net : Network , data : [ u8 ; 20 ] ) -> Self ;
351+ fn from_tex ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self ;
332352}
333353
334354impl ToAddress for ZcashAddress {
335- fn from_sprout ( net : Network , data : [ u8 ; 64 ] ) -> Self {
355+ fn from_sprout ( net : NetworkType , data : [ u8 ; 64 ] ) -> Self {
336356 ZcashAddress {
337- net : if let Network :: Regtest = net {
338- Network :: Test
357+ net : if let NetworkType :: Regtest = net {
358+ NetworkType :: Test
339359 } else {
340360 net
341361 } ,
342362 kind : AddressKind :: Sprout ( data) ,
343363 }
344364 }
345365
346- fn from_sapling ( net : Network , data : [ u8 ; 43 ] ) -> Self {
366+ fn from_sapling ( net : NetworkType , data : [ u8 ; 43 ] ) -> Self {
347367 ZcashAddress {
348368 net,
349369 kind : AddressKind :: Sapling ( data) ,
350370 }
351371 }
352372
353- fn from_unified ( net : Network , data : unified:: Address ) -> Self {
373+ fn from_unified ( net : NetworkType , data : unified:: Address ) -> Self {
354374 ZcashAddress {
355375 net,
356376 kind : AddressKind :: Unified ( data) ,
357377 }
358378 }
359379
360- fn from_transparent_p2pkh ( net : Network , data : [ u8 ; 20 ] ) -> Self {
380+ fn from_transparent_p2pkh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self {
361381 ZcashAddress {
362- net : if let Network :: Regtest = net {
363- Network :: Test
382+ net : if let NetworkType :: Regtest = net {
383+ NetworkType :: Test
364384 } else {
365385 net
366386 } ,
367387 kind : AddressKind :: P2pkh ( data) ,
368388 }
369389 }
370390
371- fn from_transparent_p2sh ( net : Network , data : [ u8 ; 20 ] ) -> Self {
391+ fn from_transparent_p2sh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self {
372392 ZcashAddress {
373- net : if let Network :: Regtest = net {
374- Network :: Test
393+ net : if let NetworkType :: Regtest = net {
394+ NetworkType :: Test
375395 } else {
376396 net
377397 } ,
378398 kind : AddressKind :: P2sh ( data) ,
379399 }
380400 }
381401
382- fn from_tex ( net : Network , data : [ u8 ; 20 ] ) -> Self {
402+ fn from_tex ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self {
383403 ZcashAddress {
384404 net,
385405 kind : AddressKind :: Tex ( data) ,
0 commit comments