Merge rust-bitcoin/rust-bitcoin#683: tests: improve coverage for P2tr and AddressType

0d463ec19e tests: improve coverage for P2tr and AddressType (Leonardo Comandini)

Pull request description:

  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.

ACKs for top commit:
  dr-orlovsky:
    utACK 0d463ec19e
  sanket1729:
    ACK 0d463ec19e

Tree-SHA512: 9e062a1807173638cb62a61a2e8ea5be8324449a8944c356073e8bd9f53941dea369c65a35dfa0019bd8323eaa5dd26a9907c1823522fef9a524e919728973a6
This commit is contained in:
sanket1729 2021-11-16 08:48:04 -08:00
commit e66a94fa40
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
1 changed files with 22 additions and 0 deletions

View File

@ -961,6 +961,26 @@ mod tests {
roundtrips(&addr); 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] #[test]
fn test_bip173_350_vectors() { fn test_bip173_350_vectors() {
// Test vectors valid under both BIP-173 and BIP-350 // Test vectors valid under both BIP-173 and BIP-350
@ -1178,5 +1198,7 @@ mod tests {
let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap(); let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap();
let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin); let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin);
assert_eq!(address.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr"); assert_eq!(address.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr");
assert_eq!(address.address_type(), Some(AddressType::P2tr));
roundtrips(&address);
} }
} }