tapyrus 0.5.0

General purpose library for using and interoperating with Tapyrus.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Rust Bitcoin Library
// Written in 2014 by
//     Andrew Poelstra <apoelstra@wpsoftware.net>
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
// Changes for rust-tapyrus is licensed as below.
// Copyright (c) 2019 Chaintope Inc.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//

//! Addresses
//!
//! Support for ordinary base58 Bitcoin addresses and private keys
//!
//! # Example: creating a new address from a randomly-generated key pair
//!
//! ```rust
//!
//! use tapyrus::network::constants::Network;
//! use tapyrus::util::address::Address;
//! use tapyrus::util::key;
//! use tapyrus::secp256k1::Secp256k1;
//! use tapyrus::secp256k1::rand::thread_rng;
//!
//! // Generate random key pair
//! let s = Secp256k1::new();
//! let public_key = key::PublicKey {
//!     compressed: true,
//!     key: s.generate_keypair(&mut thread_rng()).1,
//! };
//!
//! // Generate pay-to-pubkey-hash address
//! let address = Address::p2pkh(&public_key, Network::Prod);
//! ```

use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use crate::hashes::Hash;
use crate::hashes::hex::FromHex;

use crate::hash_types::{PubkeyHash, ScriptHash};
use crate::blockdata::opcodes;
use crate::blockdata::script::{Builder, ColorIdentifier, Script};
use crate::network::constants::Network;
use crate::util::base58;
use crate::util::key;

/// Address error.
#[derive(Debug, PartialEq)]
pub enum Error {
    /// Base58 encoding error
    Base58(base58::Error),
    /// An uncompressed pubkey was used where it is not allowed.
    UncompressedPubkey,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Base58(ref e) => write!(f, "base58: {}", e),
            Error::UncompressedPubkey => write!(f,
                "an uncompressed pubkey was used where it is not allowed",
            ),
        }
    }
}

#[allow(deprecated)]
impl ::std::error::Error for Error {
    fn cause(&self) -> Option<&dyn ::std::error::Error> {
        match *self {
            Error::Base58(ref e) => Some(e),
            _ => None,
        }
    }

    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }
}

#[doc(hidden)]
impl From<base58::Error> for Error {
    fn from(e: base58::Error) -> Error {
        Error::Base58(e)
    }
}

/// The different types of addresses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AddressType {
    /// pay-to-pubkey-hash
    P2pkh,
    /// pay-to-script-hash
    P2sh,
    /// colored-pay-to-pubkey-hash
    Cp2pkh,
    /// colored-pay-to-script-hash
    Cp2sh,
}

impl fmt::Display for AddressType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match *self {
            AddressType::P2pkh => "p2pkh",
            AddressType::P2sh => "p2sh",
            AddressType::Cp2pkh => "cp2pkh",
            AddressType::Cp2sh => "cp2sh",
        })
    }
}

impl FromStr for AddressType {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "p2pkh" => Ok(AddressType::P2pkh),
            "p2sh" => Ok(AddressType::P2sh),
            "cp2pkh" => Ok(AddressType::Cp2pkh),
            "cp2sh" => Ok(AddressType::Cp2sh),
            _ => Err(()),
        }
    }
}

/// The method used to produce an address
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Payload {
    /// pay-to-pkhash address
    PubkeyHash(PubkeyHash),
    /// P2SH address
    ScriptHash(ScriptHash),
    /// CP2PKH address
    ColoredPubkeyHash(ColorIdentifier, PubkeyHash),
    /// CP2SH address
    ColoredScriptHash(ColorIdentifier, ScriptHash),
}

impl Payload {
    /// Get a [Payload] from an output script (scriptPubkey).
    pub fn from_script(script: &Script) -> Option<Payload> {
        Some(if script.is_p2pkh() {
            Payload::PubkeyHash(PubkeyHash::from_slice(&script.as_bytes()[3..23]).unwrap())
        } else if script.is_p2sh() {
            Payload::ScriptHash(ScriptHash::from_slice(&script.as_bytes()[2..22]).unwrap())
        } else if script.is_cp2pkh() {
            let bytes = script.as_bytes();
            let color_id = ColorIdentifier::from_slice(&bytes[1..34]).unwrap();
            Payload::ColoredPubkeyHash(color_id, PubkeyHash::from_slice(&bytes[38..58]).unwrap())
        } else if script.is_cp2sh() {
            let bytes = script.as_bytes();
            let color_id = ColorIdentifier::from_slice(&bytes[1..34]).unwrap();
            Payload::ColoredScriptHash(color_id, ScriptHash::from_slice(&bytes[37..57]).unwrap())
        } else {
            return None;
        })
    }

    /// Generates a script pubkey spending to this [Payload].
    pub fn script_pubkey(&self) -> Script {
        match *self {
            Payload::PubkeyHash(ref hash) => Builder::new()
                .push_opcode(opcodes::all::OP_DUP)
                .push_opcode(opcodes::all::OP_HASH160)
                .push_slice(&hash[..])
                .push_opcode(opcodes::all::OP_EQUALVERIFY)
                .push_opcode(opcodes::all::OP_CHECKSIG),
            Payload::ScriptHash(ref hash) => Builder::new()
                .push_opcode(opcodes::all::OP_HASH160)
                .push_slice(&hash[..])
                .push_opcode(opcodes::all::OP_EQUAL),
            Payload::ColoredPubkeyHash(ref color_id, ref hash) => Builder::new()
                .push_slice(&Vec::from_hex(&format!("{}", color_id)).unwrap())
                .push_opcode(opcodes::all::OP_COLOR)
                .push_opcode(opcodes::all::OP_DUP)
                .push_opcode(opcodes::all::OP_HASH160)
                .push_slice(&hash[..])
                .push_opcode(opcodes::all::OP_EQUALVERIFY)
                .push_opcode(opcodes::all::OP_CHECKSIG),
            Payload::ColoredScriptHash(ref color_id, ref hash) => Builder::new()
                .push_slice(&Vec::from_hex(&format!("{}", color_id)).unwrap())
                .push_opcode(opcodes::all::OP_COLOR)
                .push_opcode(opcodes::all::OP_HASH160)
                .push_slice(&hash[..])
                .push_opcode(opcodes::all::OP_EQUAL),
        }
        .into_script()
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// A Bitcoin address
pub struct Address {
    /// The type of the address
    pub payload: Payload,
    /// The network on which this address is usable
    pub network: Network,
}
serde_string_impl!(Address, "a Bitcoin address");

impl Address {
    /// Creates a pay to (compressed) public key hash address from a public key
    /// This is the preferred non-witness type address
    #[inline]
    pub fn p2pkh(pk: &key::PublicKey, network: Network) -> Address {
        let mut hash_engine = PubkeyHash::engine();
        pk.write_into(&mut hash_engine);

        Address {
            network,
            payload: Payload::PubkeyHash(PubkeyHash::from_engine(hash_engine)),
        }
    }

    /// Creates a pay to script hash P2SH address from a script
    /// This address type was introduced with BIP16 and is the popular type to implement multi-sig these days.
    #[inline]
    pub fn p2sh(script: &Script, network: Network) -> Address {
        Address {
            network,
            payload: Payload::ScriptHash(ScriptHash::hash(&script[..])),
        }
    }

    /// Creates a pay to (compressed) public key hash address from a public key
    /// This is the preferred non-witness type address
    #[inline]
    pub fn cp2pkh(color_id: &ColorIdentifier, pk: &key::PublicKey, network: Network) -> Address {
        let mut hash_engine = PubkeyHash::engine();
        pk.write_into(&mut hash_engine);

        Address {
            network,
            payload: Payload::ColoredPubkeyHash(color_id.clone(), PubkeyHash::from_engine(hash_engine)),
        }
    }

    /// Creates a pay to script hash P2SH address from a script
    /// This address type was introduced with BIP16 and is the popular type to implement multi-sig these days.
    #[inline]
    pub fn cp2sh(color_id: &ColorIdentifier, script: &Script, network: Network) -> Address {
        Address {
            network,
            payload: Payload::ColoredScriptHash(color_id.clone(), ScriptHash::hash(&script[..])),
        }
    }

    /// Get the address type of the address.
    /// None if unknown or non-standard.
    pub fn address_type(&self) -> Option<AddressType> {
        match self.payload {
            Payload::PubkeyHash(_) => Some(AddressType::P2pkh),
            Payload::ScriptHash(_) => Some(AddressType::P2sh),
            Payload::ColoredPubkeyHash(_, _)=> Some(AddressType::Cp2pkh),
            Payload::ColoredScriptHash(_, _)=> Some(AddressType::Cp2sh),
        }
    }

    /// Check whether or not the address is following Bitcoin
    /// standardness rules.
    ///
    /// Segwit addresses with unassigned witness versions or non-standard
    /// program sizes are considered non-standard.
    pub fn is_standard(&self) -> bool {
        self.address_type().is_some()
    }

    /// Get an [Address] from an output script (scriptPubkey).
    pub fn from_script(script: &Script, network: Network) -> Option<Address> {
        Some(Address {
            payload: Payload::from_script(script)?,
            network,
        })
    }

    /// Generates a script pubkey spending to this address
    pub fn script_pubkey(&self) -> Script {
        self.payload.script_pubkey()
    }
}

impl Display for Address {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        match self.payload {
            Payload::PubkeyHash(ref hash) => {
                let mut prefixed = [0; 21];
                prefixed[0] = match self.network {
                    Network::Prod => 0,
                    Network::Dev => 111,
                };
                prefixed[1..].copy_from_slice(&hash[..]);
                base58::check_encode_slice_to_fmt(fmt, &prefixed[..])
            }
            Payload::ScriptHash(ref hash) => {
                let mut prefixed = [0; 21];
                prefixed[0] = match self.network {
                    Network::Prod => 5,
                    Network::Dev => 196,
                };
                prefixed[1..].copy_from_slice(&hash[..]);
                base58::check_encode_slice_to_fmt(fmt, &prefixed[..])
            }
            Payload::ColoredPubkeyHash(ref color_id, ref hash) => {
                let mut prefixed = [0; 54];
                prefixed[0] = match self.network {
                    Network::Prod => 1,
                    Network::Dev => 112,
                };
                prefixed[1..34].copy_from_slice(&Vec::from_hex(&format!("{}", color_id)).unwrap());
                prefixed[34..].copy_from_slice(&hash[..]);
                base58::check_encode_slice_to_fmt(fmt, &prefixed[..])
            }
            Payload::ColoredScriptHash(ref color_id, ref hash) => {
                let mut prefixed = [0; 54];
                prefixed[0] = match self.network {
                    Network::Prod => 6,
                    Network::Dev => 197,
                };
                prefixed[1..34].copy_from_slice(&Vec::from_hex(&format!("{}", color_id)).unwrap());
                prefixed[34..].copy_from_slice(&hash[..]);
                base58::check_encode_slice_to_fmt(fmt, &prefixed[..])
            }
        }
    }
}

impl FromStr for Address {
    type Err = Error;

    fn from_str(s: &str) -> Result<Address, Error> {
        // Base58
        if s.len() > 80 {
            return Err(Error::Base58(base58::Error::InvalidLength(s.len() * 11 / 15)));
        }
        let data = base58::from_check(s)?;

        let (network, payload) = match data.len() {
            21 => {
                match data[0] {
                    0 => (
                        Network::Prod,
                        Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()),
                    ),
                    5 => (
                        Network::Prod,
                        Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()),
                    ),
                    111 => (
                        Network::Dev,
                        Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()),
                    ),
                    196 => (
                        Network::Dev,
                        Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()),
                    ),
                    x => return Err(Error::Base58(base58::Error::InvalidVersion(vec![x]))),
                }
            }
            54 => {
                let color_id = ColorIdentifier::from_slice(&data[1..34])
                    .map_err(|_| Error::Base58(base58::Error::InvalidLength(data.len())))?;
                match data[0] {
                    1 => (
                        Network::Prod,
                        Payload::ColoredPubkeyHash(color_id, PubkeyHash::from_slice(&data[34..]).unwrap()),
                    ),
                    6 => (
                        Network::Prod,
                        Payload::ColoredScriptHash(color_id, ScriptHash::from_slice(&data[34..]).unwrap()),
                    ),
                    112 => (
                        Network::Dev,
                        Payload::ColoredPubkeyHash(color_id, PubkeyHash::from_slice(&data[34..]).unwrap()),
                    ),
                    197 => (
                        Network::Dev,
                        Payload::ColoredScriptHash(color_id, ScriptHash::from_slice(&data[34..]).unwrap()),
                    ),
                    x => return Err(Error::Base58(base58::Error::InvalidVersion(vec![x]))),
                }
            }
            _ => return Err(Error::Base58(base58::Error::InvalidLength(data.len())))
        };

        Ok(Address { network, payload })
    }
}

impl ::std::fmt::Debug for Address {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{}", self)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use std::string::ToString;

    use crate::hashes::hex::FromHex;

    use crate::blockdata::script::Script;
    use crate::network::constants::Network::{Prod, Dev};
    use crate::util::key::PublicKey;

    use super::*;

    macro_rules! hex (($hex:expr_2021) => (Vec::from_hex($hex).unwrap()));
    macro_rules! hex_key (($hex:expr_2021) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
    macro_rules! hex_script (($hex:expr_2021) => (Script::from(hex!($hex))));
    macro_rules! hex_pubkeyhash (($hex:expr_2021) => (PubkeyHash::from_hex(&$hex).unwrap()));
    macro_rules! hex_scripthash (($hex:expr_2021) => (ScriptHash::from_hex(&$hex).unwrap()));

    fn roundtrips(addr: &Address) {
        assert_eq!(
            Address::from_str(&addr.to_string()).unwrap(),
            *addr,
            "string round-trip failed for {}",
            addr,
        );
        assert_eq!(
            Address::from_script(&addr.script_pubkey(), addr.network).as_ref(),
            Some(addr),
            "script round-trip failed for {}",
            addr,
        );
        //TODO: add serde roundtrip after no-strason PR
    }

    #[test]
    fn test_address_type() {
        let p2pkh = Address {
            network: Prod,
            payload: Payload::PubkeyHash(hex_pubkeyhash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };
        assert_eq!(format!("{}", p2pkh.address_type().unwrap()), "p2pkh");
        assert_eq!(AddressType::from_str("p2pkh").unwrap(), AddressType::P2pkh);

        let p2sh = Address {
            network: Prod,
            payload: Payload::ScriptHash(hex_scripthash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };
        assert_eq!(format!("{}", p2sh.address_type().unwrap()), "p2sh");
        assert_eq!(AddressType::from_str("p2sh").unwrap(), AddressType::P2sh);

        let color_id = ColorIdentifier::from_hex("c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0e").unwrap();

        let cp2pkh = Address {
            network: Prod,
            payload: Payload::ColoredPubkeyHash(color_id.clone(), hex_pubkeyhash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };
        assert_eq!(format!("{}", cp2pkh.address_type().unwrap()), "cp2pkh");
        assert_eq!(AddressType::from_str("cp2pkh").unwrap(), AddressType::Cp2pkh);

        let cp2sh = Address {
            network: Prod,
            payload: Payload::ColoredScriptHash(color_id.clone(), hex_scripthash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };
        assert_eq!(format!("{}", cp2sh.address_type().unwrap()), "cp2sh");
        assert_eq!(AddressType::from_str("cp2sh").unwrap(), AddressType::Cp2sh);
    }

    #[test]
    fn test_p2pkh_address_58() {
        let addr = Address {
            network: Prod,
            payload: Payload::PubkeyHash(hex_pubkeyhash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };

        assert_eq!(
            addr.script_pubkey(),
            hex_script!("76a914162c5ea71c0b23f5b9022ef047c4a86470a5b07088ac")
        );
        assert_eq!(&addr.to_string(), "132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM");
        assert_eq!(addr.address_type(), Some(AddressType::P2pkh));
        roundtrips(&addr);
    }

    #[test]
    fn test_p2pkh_from_key() {
        let key = hex_key!("048d5141948c1702e8c95f438815794b87f706a8d4cd2bffad1dc1570971032c9b6042a0431ded2478b5c9cf2d81c124a5e57347a3c63ef0e7716cf54d613ba183");
        let addr = Address::p2pkh(&key, Prod);
        assert_eq!(&addr.to_string(), "1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY");

        let key = hex_key!(&"03df154ebfcf29d29cc10d5c2565018bce2d9edbab267c31d2caf44a63056cf99f");
        let addr = Address::p2pkh(&key, Dev);
        assert_eq!(&addr.to_string(), "mqkhEMH6NCeYjFybv7pvFC22MFeaNT9AQC");
        assert_eq!(addr.address_type(), Some(AddressType::P2pkh));
        roundtrips(&addr);
    }

    #[test]
    fn test_p2sh_address_58() {
        let addr = Address {
            network: Prod,
            payload: Payload::ScriptHash(hex_scripthash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };

        assert_eq!(
            addr.script_pubkey(),
            hex_script!("a914162c5ea71c0b23f5b9022ef047c4a86470a5b07087")
        );
        assert_eq!(&addr.to_string(), "33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k");
        assert_eq!(addr.address_type(), Some(AddressType::P2sh));
        roundtrips(&addr);
    }

    #[test]
    fn test_p2sh_parse() {
        let script = hex_script!("552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae");
        let addr = Address::p2sh(&script, Dev);

        assert_eq!(&addr.to_string(), "2N3zXjbwdTcPsJiy8sUK9FhWJhqQCxA8Jjr");
        assert_eq!(addr.address_type(), Some(AddressType::P2sh));
        roundtrips(&addr);
    }

    #[test]
    fn test_cp2pkh_address() {
        let color_id = ColorIdentifier::from_hex("c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0e").unwrap();

        let addr = Address {
            network: Prod,
            payload: Payload::ColoredPubkeyHash(color_id, hex_pubkeyhash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };

        assert_eq!(
            addr.script_pubkey(),
            hex_script!("21c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0ebc76a914162c5ea71c0b23f5b9022ef047c4a86470a5b07088ac")
        );
        assert_eq!(&addr.to_string(), "vxgHyieT46FQMYAvmNM6BsmwNNXMTPk2C6StiVD3Cmy45ge5gftf5f417CehuVWVo5KfBbgtGyLn9j");
        assert_eq!(addr.address_type(), Some(AddressType::Cp2pkh));
        roundtrips(&addr);
    }

    #[test]
    fn test_cp2pkh_from_key() {
        let key = hex_key!("048d5141948c1702e8c95f438815794b87f706a8d4cd2bffad1dc1570971032c9b6042a0431ded2478b5c9cf2d81c124a5e57347a3c63ef0e7716cf54d613ba183");
        let color_id = ColorIdentifier::from_hex("c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0e").unwrap();
        let addr = Address::cp2pkh(&color_id, &key, Prod);
        assert_eq!(&addr.to_string(), "vxgHyieT46FQMYAvmNM6BsmwNNXMTPk2C6StiVD3Cmy463vKtag2nkNBtGeqUNzBuXWtgUctod91ag");

        let key = hex_key!(&"03df154ebfcf29d29cc10d5c2565018bce2d9edbab267c31d2caf44a63056cf99f");
        let addr = Address::cp2pkh(&color_id, &key, Dev);
        assert_eq!(&addr.to_string(), "22VZyRTDaMem4DcgBgRZgbo7PZm45gXSMWzHrYiYE9j1qVUiHVfNFhik8yY4YzpArPc3Hufwe5oeWgmg");
        assert_eq!(addr.address_type(), Some(AddressType::Cp2pkh));
        roundtrips(&addr);
    }

    #[test]
    fn test_cp2sh_address() {
        let color_id = ColorIdentifier::from_hex("c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0e").unwrap();

        let addr = Address {
            network: Prod,
            payload: Payload::ColoredScriptHash(color_id, hex_scripthash!("162c5ea71c0b23f5b9022ef047c4a86470a5b070")),
        };

        assert_eq!(
            addr.script_pubkey(),
            hex_script!("21c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0ebca914162c5ea71c0b23f5b9022ef047c4a86470a5b07087")
        );
        assert_eq!(&addr.to_string(), "4Zxhb33iSoydtcKzWc7hpoRjtJh2W9otwDeqP5PbZHGoq7m3fwysg7ec2TJ2RRuheF8PgQqrGKDjdij");
        assert_eq!(addr.address_type(), Some(AddressType::Cp2sh));
        roundtrips(&addr);
    }

    #[test]
    #[cfg(feature = "serde")]
    fn test_json_serialize() {
        use serde_json;

        // P2PKH
        let addr = Address::from_str("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM").unwrap();
        let json = serde_json::to_value(&addr).unwrap();
        assert_eq!(
            json,
            serde_json::Value::String("132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM".to_owned())
        );
        let into: Address = serde_json::from_value(json).unwrap();
        assert_eq!(addr.to_string(), into.to_string());
        assert_eq!(
            into.script_pubkey(),
            hex_script!("76a914162c5ea71c0b23f5b9022ef047c4a86470a5b07088ac")
        );

        // P2SH
        let addr = Address::from_str("33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k").unwrap();
        let json = serde_json::to_value(&addr).unwrap();
        assert_eq!(
            json,
            serde_json::Value::String("33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k".to_owned())
        );
        let into: Address = serde_json::from_value(json).unwrap();
        assert_eq!(addr.to_string(), into.to_string());
        assert_eq!(
            into.script_pubkey(),
            hex_script!("a914162c5ea71c0b23f5b9022ef047c4a86470a5b07087")
        );

        // CP2PKH
        let addr = Address::from_str("vxgHyieT46FQMYAvmNM6BsmwNNXMTPk2C6StiVD3Cmy45ge5gftf5f417CehuVWVo5KfBbgtGyLn9j").unwrap();
        let json = serde_json::to_value(&addr).unwrap();
        assert_eq!(
            json,
            serde_json::Value::String("vxgHyieT46FQMYAvmNM6BsmwNNXMTPk2C6StiVD3Cmy45ge5gftf5f417CehuVWVo5KfBbgtGyLn9j".to_owned())
        );
        let into: Address = serde_json::from_value(json).unwrap();
        assert_eq!(addr.to_string(), into.to_string());
        assert_eq!(
            into.script_pubkey(),
            hex_script!("21c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0ebc76a914162c5ea71c0b23f5b9022ef047c4a86470a5b07088ac")
        );

        // CP2SH
        let addr = Address::from_str("4Zxhb33iSoydtcKzWc7hpoRjtJh2W9otwDeqP5PbZHGoq7m3fwysg7ec2TJ2RRuheF8PgQqrGKDjdij").unwrap();
        let json = serde_json::to_value(&addr).unwrap();
        assert_eq!(
            json,
            serde_json::Value::String("4Zxhb33iSoydtcKzWc7hpoRjtJh2W9otwDeqP5PbZHGoq7m3fwysg7ec2TJ2RRuheF8PgQqrGKDjdij".to_owned())
        );
        let into: Address = serde_json::from_value(json).unwrap();
        assert_eq!(addr.to_string(), into.to_string());
        assert_eq!(
            into.script_pubkey(),
            hex_script!("21c36db65fd59fd356f6729140571b5bcd6bb3b83492a16e1bf0a3884442fc3c8a0ebca914162c5ea71c0b23f5b9022ef047c4a86470a5b07087")
        );
    }

}