Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce segwit v0 script validity when creating address. #1021

Merged
merged 1 commit into from Jun 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/util/address.rs
Expand Up @@ -672,6 +672,12 @@ impl Address {

/// Constructs an [`Address`] from an output script (`scriptPubkey`).
pub fn from_script(script: &script::Script, network: Network) -> Option<Address> {
Copy link
Contributor

@dpc dpc May 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might not be to be done in this PR, but if we have multiple ways of "not turning script into an address", maybe we should make it enum Error { ... list all the ways things can go wrong ... }, so the caller has something better to show to the user than just "didn't turn into address".

Copy link
Contributor Author

@nlanson nlanson May 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Address::from_script() and a few other functions should return Result<_, Error> instead of Option<> but this is for another PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1022 just to not forget.

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,
Expand Down Expand Up @@ -1398,4 +1404,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);
}
}