Skip to content

Commit bdc4a66

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#1021: Enforce segwit v0 script validity when creating address.
6c10d77 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 6c10d77 sanket1729: ACK 6c10d77. Left a comment can be addressed in separate PR. apoelstra: ACK 6c10d77 Tree-SHA512: 32aebb13477958b1455c688f668aaa3d3af4db0a7936b9549bcd1d03bd0e16635b8471549d96f1e8d408d6501e8fb515df2eb86b17a08c3152774a5be78ae8b1
2 parents 67e583c + 054b821 commit bdc4a66

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/util/address.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,12 @@ impl Address {
683683

684684
/// Constructs an [`Address`] from an output script (`scriptPubkey`).
685685
pub fn from_script(script: &script::Script, network: Network) -> Option<Address> {
686+
if script.is_witness_program() {
687+
if script.witness_version() == Some(WitnessVersion::V0) && !(script.is_v0_p2wpkh() || script.is_v0_p2wsh()) {
688+
return None
689+
}
690+
}
691+
686692
Some(Address {
687693
payload: Payload::from_script(script)?,
688694
network,
@@ -1409,4 +1415,13 @@ mod tests {
14091415
let result = address.is_related_to_xonly_pubkey(&xonly_pubkey);
14101416
assert!(result);
14111417
}
1418+
1419+
#[test]
1420+
fn test_fail_address_from_script() {
1421+
let bad_p2wpkh = hex_script!("0014dbc5b0a8f9d4353b4b54c3db48846bb15abfec");
1422+
let bad_p2wsh = hex_script!("00202d4fa2eb233d008cc83206fa2f4f2e60199000f5b857a835e3172323385623");
1423+
1424+
assert_eq!(Address::from_script(&bad_p2wpkh, Network::Bitcoin), None);
1425+
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), None);
1426+
}
14121427
}

0 commit comments

Comments
 (0)