From 0d463ec19e47644bb675abde342aa11515cdba5c Mon Sep 17 00:00:00 2001 From: Leonardo Comandini Date: Wed, 27 Oct 2021 17:58:19 +0200 Subject: [PATCH] tests: improve coverage for P2tr and AddressType The new AddressType test shows addresses that are valid but have no type. If in the future some of those get a type or become invalid (either voluntarily or due to a regression), this will highlight it. --- src/util/address.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/util/address.rs b/src/util/address.rs index 055b2e96..ce59a9a7 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -962,6 +962,26 @@ mod tests { roundtrips(&addr); } + #[test] + fn test_address_type() { + let addresses = [ + ("1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY", Some(AddressType::P2pkh)), + ("33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k", Some(AddressType::P2sh)), + ("bc1qvzvkjn4q3nszqxrv3nraga2r822xjty3ykvkuw", Some(AddressType::P2wpkh)), + ("bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej", Some(AddressType::P2wsh)), + ("bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr", Some(AddressType::P2tr)), + // Related to future extensions, addresses are valid but have no type + // segwit v1 and len != 32 + ("bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y", None), + // segwit v2 + ("bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs", None), + ]; + for (address, expected_type) in &addresses { + let addr = Address::from_str(&address).unwrap(); + assert_eq!(&addr.address_type(), expected_type); + } + } + #[test] fn test_bip173_350_vectors() { // Test vectors valid under both BIP-173 and BIP-350 @@ -1179,5 +1199,7 @@ mod tests { let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap(); let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin); assert_eq!(address.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr"); + assert_eq!(address.address_type(), Some(AddressType::P2tr)); + roundtrips(&address); } }