@@ -3,7 +3,9 @@ use core::fmt;
3
3
#[ cfg( feature = "std" ) ]
4
4
use std:: error:: Error ;
5
5
6
- use crate :: { kind:: * , AddressKind , Network , ZcashAddress } ;
6
+ use zcash_protocol:: consensus:: NetworkType ;
7
+
8
+ use crate :: { kind:: * , AddressKind , ZcashAddress } ;
7
9
8
10
/// An error indicating that an address type is not supported for conversion.
9
11
#[ derive( Debug ) ]
@@ -19,7 +21,10 @@ impl fmt::Display for UnsupportedAddress {
19
21
#[ derive( Debug ) ]
20
22
pub enum ConversionError < E > {
21
23
/// The address is for the wrong network.
22
- IncorrectNetwork { expected : Network , actual : Network } ,
24
+ IncorrectNetwork {
25
+ expected : NetworkType ,
26
+ actual : NetworkType ,
27
+ } ,
23
28
/// The address type is not supported by the target type.
24
29
Unsupported ( UnsupportedAddress ) ,
25
30
/// A conversion error returned by the target type.
@@ -60,15 +65,16 @@ impl<E: Error + 'static> Error for ConversionError<E> {
60
65
61
66
/// A helper trait for converting a [`ZcashAddress`] into a network-agnostic type.
62
67
///
63
- /// A blanket implementation of [`TryFromAddress`] is provided for `(Network , T)` where
68
+ /// A blanket implementation of [`TryFromAddress`] is provided for `(NetworkType , T)` where
64
69
/// `T: TryFromRawAddress`.
65
70
///
66
71
/// [`ZcashAddress`]: crate::ZcashAddress
67
72
///
68
73
/// # Examples
69
74
///
70
75
/// ```
71
- /// use zcash_address::{ConversionError, Network, TryFromRawAddress, UnsupportedAddress, ZcashAddress};
76
+ /// use zcash_address::{ConversionError, TryFromRawAddress, UnsupportedAddress, ZcashAddress};
77
+ /// use zcash_protocol::consensus::NetworkType;
72
78
///
73
79
/// #[derive(Debug, PartialEq)]
74
80
/// struct MySapling([u8; 43]);
@@ -90,15 +96,15 @@ impl<E: Error + 'static> Error for ConversionError<E> {
90
96
///
91
97
/// // You can use `ZcashAddress::convert_if_network` to get your type directly.
92
98
/// 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);
94
100
/// assert!(converted.is_ok());
95
101
/// assert_eq!(converted.unwrap(), MySapling([0; 43]));
96
102
///
97
103
/// // Using `ZcashAddress::convert` gives us the tuple `(network, converted_addr)`.
98
104
/// let addr: ZcashAddress = addr_string.parse().unwrap();
99
105
/// let converted = addr.convert::<(_, MySapling)>();
100
106
/// assert!(converted.is_ok());
101
- /// assert_eq!(converted.unwrap(), (Network ::Main, MySapling([0; 43])));
107
+ /// assert_eq!(converted.unwrap(), (NetworkType ::Main, MySapling([0; 43])));
102
108
///
103
109
/// // For an unsupported address type, we get an error.
104
110
/// let addr: ZcashAddress = "t1Hsc1LR8yKnbbe3twRp88p6vFfC5t7DLbs".parse().unwrap();
@@ -158,7 +164,8 @@ pub trait TryFromRawAddress: Sized {
158
164
/// # Examples
159
165
///
160
166
/// ```
161
- /// use zcash_address::{ConversionError, Network, TryFromAddress, UnsupportedAddress, ZcashAddress};
167
+ /// use zcash_address::{ConversionError, TryFromAddress, UnsupportedAddress, ZcashAddress};
168
+ /// use zcash_protocol::consensus::NetworkType;
162
169
///
163
170
/// #[derive(Debug)]
164
171
/// struct MySapling([u8; 43]);
@@ -171,7 +178,7 @@ pub trait TryFromRawAddress: Sized {
171
178
/// type Error = &'static str;
172
179
///
173
180
/// fn try_from_sapling(
174
- /// net: Network ,
181
+ /// net: NetworkType ,
175
182
/// data: [u8; 43],
176
183
/// ) -> Result<Self, ConversionError<Self::Error>> {
177
184
/// Ok(MySapling(data))
@@ -197,29 +204,32 @@ pub trait TryFromAddress: Sized {
197
204
/// [`Self::try_from_sapling`] as a valid Sapling address).
198
205
type Error ;
199
206
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 > > {
201
211
let _ = ( net, data) ;
202
212
Err ( ConversionError :: Unsupported ( UnsupportedAddress ( "Sprout" ) ) )
203
213
}
204
214
205
215
fn try_from_sapling (
206
- net : Network ,
216
+ net : NetworkType ,
207
217
data : [ u8 ; 43 ] ,
208
218
) -> Result < Self , ConversionError < Self :: Error > > {
209
219
let _ = ( net, data) ;
210
220
Err ( ConversionError :: Unsupported ( UnsupportedAddress ( "Sapling" ) ) )
211
221
}
212
222
213
223
fn try_from_unified (
214
- net : Network ,
224
+ net : NetworkType ,
215
225
data : unified:: Address ,
216
226
) -> Result < Self , ConversionError < Self :: Error > > {
217
227
let _ = ( net, data) ;
218
228
Err ( ConversionError :: Unsupported ( UnsupportedAddress ( "Unified" ) ) )
219
229
}
220
230
221
231
fn try_from_transparent_p2pkh (
222
- net : Network ,
232
+ net : NetworkType ,
223
233
data : [ u8 ; 20 ] ,
224
234
) -> Result < Self , ConversionError < Self :: Error > > {
225
235
let _ = ( net, data) ;
@@ -229,7 +239,7 @@ pub trait TryFromAddress: Sized {
229
239
}
230
240
231
241
fn try_from_transparent_p2sh (
232
- net : Network ,
242
+ net : NetworkType ,
233
243
data : [ u8 ; 20 ] ,
234
244
) -> Result < Self , ConversionError < Self :: Error > > {
235
245
let _ = ( net, data) ;
@@ -238,50 +248,59 @@ pub trait TryFromAddress: Sized {
238
248
) ) )
239
249
}
240
250
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 > > {
242
255
let _ = ( net, data) ;
243
256
Err ( ConversionError :: Unsupported ( UnsupportedAddress (
244
257
"transparent-source restricted P2PKH" ,
245
258
) ) )
246
259
}
247
260
}
248
261
249
- impl < T : TryFromRawAddress > TryFromAddress for ( Network , T ) {
262
+ impl < T : TryFromRawAddress > TryFromAddress for ( NetworkType , T ) {
250
263
type Error = T :: Error ;
251
264
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 > > {
253
269
T :: try_from_raw_sprout ( data) . map ( |addr| ( net, addr) )
254
270
}
255
271
256
272
fn try_from_sapling (
257
- net : Network ,
273
+ net : NetworkType ,
258
274
data : [ u8 ; 43 ] ,
259
275
) -> Result < Self , ConversionError < Self :: Error > > {
260
276
T :: try_from_raw_sapling ( data) . map ( |addr| ( net, addr) )
261
277
}
262
278
263
279
fn try_from_unified (
264
- net : Network ,
280
+ net : NetworkType ,
265
281
data : unified:: Address ,
266
282
) -> Result < Self , ConversionError < Self :: Error > > {
267
283
T :: try_from_raw_unified ( data) . map ( |addr| ( net, addr) )
268
284
}
269
285
270
286
fn try_from_transparent_p2pkh (
271
- net : Network ,
287
+ net : NetworkType ,
272
288
data : [ u8 ; 20 ] ,
273
289
) -> Result < Self , ConversionError < Self :: Error > > {
274
290
T :: try_from_raw_transparent_p2pkh ( data) . map ( |addr| ( net, addr) )
275
291
}
276
292
277
293
fn try_from_transparent_p2sh (
278
- net : Network ,
294
+ net : NetworkType ,
279
295
data : [ u8 ; 20 ] ,
280
296
) -> Result < Self , ConversionError < Self :: Error > > {
281
297
T :: try_from_raw_transparent_p2sh ( data) . map ( |addr| ( net, addr) )
282
298
}
283
299
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 > > {
285
304
T :: try_from_raw_tex ( data) . map ( |addr| ( net, addr) )
286
305
}
287
306
}
@@ -298,88 +317,89 @@ impl<T: TryFromRawAddress> TryFromAddress for (Network, T) {
298
317
/// # Examples
299
318
///
300
319
/// ```
301
- /// use zcash_address::{ToAddress, Network, ZcashAddress};
320
+ /// use zcash_address::{ToAddress, ZcashAddress};
321
+ /// use zcash_protocol::consensus::NetworkType;
302
322
///
303
323
/// #[derive(Debug)]
304
324
/// struct MySapling([u8; 43]);
305
325
///
306
326
/// impl MySapling {
307
327
/// /// Encodes this Sapling address for the given network.
308
- /// fn encode(&self, net: Network ) -> ZcashAddress {
328
+ /// fn encode(&self, net: NetworkType ) -> ZcashAddress {
309
329
/// ZcashAddress::from_sapling(net, self.0)
310
330
/// }
311
331
/// }
312
332
///
313
333
/// let addr = MySapling([0; 43]);
314
- /// let encoded = addr.encode(Network ::Main);
334
+ /// let encoded = addr.encode(NetworkType ::Main);
315
335
/// assert_eq!(
316
336
/// encoded.to_string(),
317
337
/// "zs1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpq6d8g",
318
338
/// );
319
339
/// ```
320
340
pub trait ToAddress : private:: Sealed {
321
- fn from_sprout ( net : Network , data : [ u8 ; 64 ] ) -> Self ;
341
+ fn from_sprout ( net : NetworkType , data : [ u8 ; 64 ] ) -> Self ;
322
342
323
- fn from_sapling ( net : Network , data : [ u8 ; 43 ] ) -> Self ;
343
+ fn from_sapling ( net : NetworkType , data : [ u8 ; 43 ] ) -> Self ;
324
344
325
- fn from_unified ( net : Network , data : unified:: Address ) -> Self ;
345
+ fn from_unified ( net : NetworkType , data : unified:: Address ) -> Self ;
326
346
327
- fn from_transparent_p2pkh ( net : Network , data : [ u8 ; 20 ] ) -> Self ;
347
+ fn from_transparent_p2pkh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self ;
328
348
329
- fn from_transparent_p2sh ( net : Network , data : [ u8 ; 20 ] ) -> Self ;
349
+ fn from_transparent_p2sh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self ;
330
350
331
- fn from_tex ( net : Network , data : [ u8 ; 20 ] ) -> Self ;
351
+ fn from_tex ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self ;
332
352
}
333
353
334
354
impl ToAddress for ZcashAddress {
335
- fn from_sprout ( net : Network , data : [ u8 ; 64 ] ) -> Self {
355
+ fn from_sprout ( net : NetworkType , data : [ u8 ; 64 ] ) -> Self {
336
356
ZcashAddress {
337
- net : if let Network :: Regtest = net {
338
- Network :: Test
357
+ net : if let NetworkType :: Regtest = net {
358
+ NetworkType :: Test
339
359
} else {
340
360
net
341
361
} ,
342
362
kind : AddressKind :: Sprout ( data) ,
343
363
}
344
364
}
345
365
346
- fn from_sapling ( net : Network , data : [ u8 ; 43 ] ) -> Self {
366
+ fn from_sapling ( net : NetworkType , data : [ u8 ; 43 ] ) -> Self {
347
367
ZcashAddress {
348
368
net,
349
369
kind : AddressKind :: Sapling ( data) ,
350
370
}
351
371
}
352
372
353
- fn from_unified ( net : Network , data : unified:: Address ) -> Self {
373
+ fn from_unified ( net : NetworkType , data : unified:: Address ) -> Self {
354
374
ZcashAddress {
355
375
net,
356
376
kind : AddressKind :: Unified ( data) ,
357
377
}
358
378
}
359
379
360
- fn from_transparent_p2pkh ( net : Network , data : [ u8 ; 20 ] ) -> Self {
380
+ fn from_transparent_p2pkh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self {
361
381
ZcashAddress {
362
- net : if let Network :: Regtest = net {
363
- Network :: Test
382
+ net : if let NetworkType :: Regtest = net {
383
+ NetworkType :: Test
364
384
} else {
365
385
net
366
386
} ,
367
387
kind : AddressKind :: P2pkh ( data) ,
368
388
}
369
389
}
370
390
371
- fn from_transparent_p2sh ( net : Network , data : [ u8 ; 20 ] ) -> Self {
391
+ fn from_transparent_p2sh ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self {
372
392
ZcashAddress {
373
- net : if let Network :: Regtest = net {
374
- Network :: Test
393
+ net : if let NetworkType :: Regtest = net {
394
+ NetworkType :: Test
375
395
} else {
376
396
net
377
397
} ,
378
398
kind : AddressKind :: P2sh ( data) ,
379
399
}
380
400
}
381
401
382
- fn from_tex ( net : Network , data : [ u8 ; 20 ] ) -> Self {
402
+ fn from_tex ( net : NetworkType , data : [ u8 ; 20 ] ) -> Self {
383
403
ZcashAddress {
384
404
net,
385
405
kind : AddressKind :: Tex ( data) ,
0 commit comments