Merge rust-bitcoin/rust-bitcoin#1039: Fix incorrect argument passed to Error::InvalidSegwitV0ProgramLength and add test

24fdb53c9c Fix incorrect argument passed to Error::InvalidSegwitV0ProgramLength (eunoia_1729)

Pull request description:

  See also: #995, #1038

ACKs for top commit:
  sanket1729:
    utACK 24fdb53c9c. Nice catch
  apoelstra:
    ACK 24fdb53c9c
  tcharding:
    ACK 24fdb53c9c

Tree-SHA512: ced78b69054ec81431399a853291c7bad5b1a49d6683b1ac153a0f1449935bb5d75a31e3d86160602064530959a2ddc3c59a2a2ca268252c42a6805253ead9d0
This commit is contained in:
Andrew Poelstra 2022-06-15 13:25:16 +00:00
commit d363cd2ebc
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 3 additions and 1 deletions

View File

@ -401,7 +401,7 @@ impl Payload {
Payload::ScriptHash(ScriptHash::from_inner(hash_inner))
} else if script.is_witness_program() {
if script.witness_version() == Some(WitnessVersion::V0) && !(script.is_v0_p2wpkh() || script.is_v0_p2wsh()) {
return Err(Error::InvalidSegwitV0ProgramLength(script.len()));
return Err(Error::InvalidSegwitV0ProgramLength(script.len() - 2));
}
Payload::WitnessProgram {
@ -1430,9 +1430,11 @@ mod tests {
fn test_fail_address_from_script() {
let bad_p2wpkh = hex_script!("0014dbc5b0a8f9d4353b4b54c3db48846bb15abfec");
let bad_p2wsh = hex_script!("00202d4fa2eb233d008cc83206fa2f4f2e60199000f5b857a835e3172323385623");
let invalid_segwitv0_script = hex_script!("001161458e330389cd0437ee9fe3641d70cc18");
let expected = Err(Error::UnrecognizedScript);
assert_eq!(Address::from_script(&bad_p2wpkh, Network::Bitcoin), expected);
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), expected);
assert_eq!(Address::from_script(&invalid_segwitv0_script, Network::Bitcoin), Err(Error::InvalidSegwitV0ProgramLength(17)));
}
}