Merge pull request #601 from LNP-BP/feat/bech32m-1

Bech32m adoption
This commit is contained in:
Andrew Poelstra 2021-06-18 21:14:12 +00:00 committed by GitHub
commit abff973e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 15 deletions

View File

@ -22,7 +22,7 @@ secp-lowmemory = ["secp256k1/lowmemory"]
secp-recovery = ["secp256k1/recovery"] secp-recovery = ["secp256k1/recovery"]
[dependencies] [dependencies]
bech32 = "0.7.3" bech32 = "0.8.0"
bitcoin_hashes = "0.9.6" bitcoin_hashes = "0.9.6"
secp256k1 = "0.20.2" secp256k1 = "0.20.2"

View File

@ -398,6 +398,7 @@ impl fmt::Display for Address {
Network::Testnet | Network::Signet => "tb", Network::Testnet | Network::Signet => "tb",
Network::Regtest => "bcrt", Network::Regtest => "bcrt",
}; };
let bech_ver = if ver.to_u8() > 0 { bech32::Variant::Bech32m } else { bech32::Variant::Bech32 };
let mut upper_writer; let mut upper_writer;
let writer = if fmt.alternate() { let writer = if fmt.alternate() {
upper_writer = UpperWriter(fmt); upper_writer = UpperWriter(fmt);
@ -405,7 +406,7 @@ impl fmt::Display for Address {
} else { } else {
fmt as &mut dyn fmt::Write fmt as &mut dyn fmt::Write
}; };
let mut bech32_writer = bech32::Bech32Writer::new(hrp, writer)?; let mut bech32_writer = bech32::Bech32Writer::new(hrp, bech_ver, writer)?;
bech32::WriteBase32::write_u5(&mut bech32_writer, ver)?; bech32::WriteBase32::write_u5(&mut bech32_writer, ver)?;
bech32::ToBase32::write_base32(&prog, &mut bech32_writer) bech32::ToBase32::write_base32(&prog, &mut bech32_writer)
} }
@ -448,7 +449,7 @@ impl FromStr for Address {
}; };
if let Some(network) = bech32_network { if let Some(network) = bech32_network {
// decode as bech32 // decode as bech32
let (_, payload) = bech32::decode(s)?; let (_, payload, variant) = bech32::decode(s)?;
if payload.is_empty() { if payload.is_empty() {
return Err(Error::EmptyBech32Payload); return Err(Error::EmptyBech32Payload);
} }
@ -472,6 +473,12 @@ impl FromStr for Address {
return Err(Error::InvalidSegwitV0ProgramLength(program.len())); return Err(Error::InvalidSegwitV0ProgramLength(program.len()));
} }
// Bech32 encoding check
if (version.to_u8() > 0 && variant != bech32::Variant::Bech32m) ||
(version.to_u8() == 0 && variant != bech32::Variant::Bech32) {
return Err(Error::InvalidWitnessVersion(version.to_u8()))
}
return Ok(Address { return Ok(Address {
payload: Payload::WitnessProgram { payload: Payload::WitnessProgram {
version: version, version: version,
@ -681,14 +688,17 @@ mod tests {
} }
#[test] #[test]
fn test_bip173_vectors() { fn test_bip173_350_vectors() {
// Test vectors valid under both BIP-173 and BIP-350
let valid_vectors = [ let valid_vectors = [
("BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", "0014751e76e8199196d454941c45d1b3a323f1433bd6"), ("BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", "0014751e76e8199196d454941c45d1b3a323f1433bd6"),
("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", "00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"), ("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", "00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262"),
("bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx", "5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"), ("bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y", "5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6"),
("BC1SW50QA3JX3S", "6002751e"), ("BC1SW50QGDZ25J", "6002751e"),
("bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj", "5210751e76e8199196d454941c45d1b3a323"), ("bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs", "5210751e76e8199196d454941c45d1b3a323"),
("tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", "0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"), ("tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", "0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"),
("tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c", "5120000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"),
("bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0", "512079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
]; ];
for vector in &valid_vectors { for vector in &valid_vectors {
let addr: Address = vector.0.parse().unwrap(); let addr: Address = vector.0.parse().unwrap();
@ -697,16 +707,59 @@ mod tests {
} }
let invalid_vectors = [ let invalid_vectors = [
"tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty", // 1. BIP-350 test vectors
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", // Invalid human-readable part
"BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2", "tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut",
"bc1rw5uspcuh", // Invalid checksums (Bech32 instead of Bech32m):
"bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90", "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd",
"tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf",
"BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL",
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh",
"tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47",
// Invalid character in checksum
"bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4",
// Invalid witness version
"BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R",
// Invalid program length (1 byte)
"bc1pw5dgrnzv",
// Invalid program length (41 bytes)
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav",
// Invalid program length for witness version 0 (per BIP141)
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P", "BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7", // Mixed case
"bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du", "tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq",
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv", // zero padding of more than 4 bits
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf",
// Non-zero padding in 8-to-5 conversion
"tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j",
// Empty data section
"bc1gmk9yu", "bc1gmk9yu",
// 2. BIP-173 test vectors
// Invalid human-readable part
"tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty",
// Invalid checksum
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5",
// Invalid witness version
"BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2",
// Invalid program length
"bc1rw5uspcuh",
// Invalid program length
"bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90",
// Invalid program length for witness version 0 (per BIP141)
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
// Mixed case
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7",
// zero padding of more than 4 bits
"bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du",
// Non-zero padding in 8-to-5 conversion
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv",
// Final test for empty data section is the same as above in BIP-350
// 3. BIP-173 valid test vectors obsolete by BIP-350
"bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx",
"BC1SW50QA3JX3S",
"bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj",
]; ];
for vector in &invalid_vectors { for vector in &invalid_vectors {
assert!(vector.parse::<Address>().is_err()); assert!(vector.parse::<Address>().is_err());