Merge rust-bitcoin/rust-bitcoin#1021: Enforce segwit v0 script validity when creating address.

6c10d77ecb Address::from_script() - Check witness v0 program lengths. (Noah)

Pull request description:

  Adds a check in `Address::from_script()` that checks if segwit v0 scripts have a valid length.

  Fix: #995

ACKs for top commit:
  tcharding:
    ACK 6c10d77ecb
  sanket1729:
    ACK 6c10d77ecb. Left a comment can be addressed in separate PR.
  apoelstra:
    ACK 6c10d77ecb

Tree-SHA512: 32aebb13477958b1455c688f668aaa3d3af4db0a7936b9549bcd1d03bd0e16635b8471549d96f1e8d408d6501e8fb515df2eb86b17a08c3152774a5be78ae8b1
This commit is contained in:
Andrew Poelstra 2022-06-01 16:24:02 +00:00
commit 57eaf13c86
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 15 additions and 0 deletions

View File

@ -683,6 +683,12 @@ impl Address {
/// Constructs an [`Address`] from an output script (`scriptPubkey`).
pub fn from_script(script: &script::Script, network: Network) -> Option<Address> {
if script.is_witness_program() {
if script.witness_version() == Some(WitnessVersion::V0) && !(script.is_v0_p2wpkh() || script.is_v0_p2wsh()) {
return None
}
}
Some(Address {
payload: Payload::from_script(script)?,
network,
@ -1409,4 +1415,13 @@ mod tests {
let result = address.is_related_to_xonly_pubkey(&xonly_pubkey);
assert!(result);
}
#[test]
fn test_fail_address_from_script() {
let bad_p2wpkh = hex_script!("0014dbc5b0a8f9d4353b4b54c3db48846bb15abfec");
let bad_p2wsh = hex_script!("00202d4fa2eb233d008cc83206fa2f4f2e60199000f5b857a835e3172323385623");
assert_eq!(Address::from_script(&bad_p2wpkh, Network::Bitcoin), None);
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), None);
}
}