From a3d2d1a184586eeb5a85efd2eace15ca9f388c7e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 23 May 2024 09:41:18 +1000 Subject: [PATCH 1/2] Make Address:p2sh_from_hash public We previously made this function Private and added a comment that doing so was somehow better to remove the footgun of hashing the wrong length script. However in hindsight this was a bad idea and users want the functionality. Make the `Address:p2sh_from_hash` public and document it as we do for `Address::p2sh`. --- bitcoin/src/address/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 89658144b..3883378fb 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -384,8 +384,13 @@ impl Address { Ok(Address::p2sh_from_hash(hash, network)) } - // This is intentionally not public so we enforce script length checks. - fn p2sh_from_hash(hash: ScriptHash, network: impl Into) -> Address { + /// Creates a pay to script hash P2SH address from a script hash. + /// + /// # Warning + /// + /// The `hash` pre-image (redeem script) must not exceed 520 bytes in length + /// otherwise outputs created from the returned address will be un-spendable. + pub fn p2sh_from_hash(hash: ScriptHash, network: impl Into) -> Address { Self(AddressInner::P2sh { hash, network: network.into() }, PhantomData) } From 3615410d215222ca5fdb431706007a36df5c797d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sat, 25 May 2024 07:54:22 +1000 Subject: [PATCH 2/2] api: Run just check-api --- api/bitcoin/all-features.txt | 1 + api/bitcoin/default-features.txt | 1 + api/bitcoin/no-features.txt | 1 + 3 files changed, 3 insertions(+) diff --git a/api/bitcoin/all-features.txt b/api/bitcoin/all-features.txt index 95a72b91d..7cc7e30f4 100644 --- a/api/bitcoin/all-features.txt +++ b/api/bitcoin/all-features.txt @@ -7119,6 +7119,7 @@ pub fn bitcoin::address::Address::is_spend_standard(&self) -> bool pub fn bitcoin::address::Address::matches_script_pubkey(&self, script: &bitcoin::blockdata::script::Script) -> bool pub fn bitcoin::address::Address::p2pkh(pk: impl core::convert::Into, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2sh(script: &bitcoin::blockdata::script::Script, network: impl core::convert::Into) -> core::result::Result +pub fn bitcoin::address::Address::p2sh_from_hash(hash: bitcoin::blockdata::script::ScriptHash, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2shwpkh(pk: &bitcoin::CompressedPublicKey, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2shwsh(script: &bitcoin::blockdata::script::Script, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2tr(secp: &secp256k1::Secp256k1, internal_key: bitcoin::key::UntweakedPublicKey, merkle_root: core::option::Option, hrp: impl core::convert::Into) -> bitcoin::address::Address diff --git a/api/bitcoin/default-features.txt b/api/bitcoin/default-features.txt index 1d4fab2bc..152f5460a 100644 --- a/api/bitcoin/default-features.txt +++ b/api/bitcoin/default-features.txt @@ -6787,6 +6787,7 @@ pub fn bitcoin::address::Address::is_spend_standard(&self) -> bool pub fn bitcoin::address::Address::matches_script_pubkey(&self, script: &bitcoin::blockdata::script::Script) -> bool pub fn bitcoin::address::Address::p2pkh(pk: impl core::convert::Into, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2sh(script: &bitcoin::blockdata::script::Script, network: impl core::convert::Into) -> core::result::Result +pub fn bitcoin::address::Address::p2sh_from_hash(hash: bitcoin::blockdata::script::ScriptHash, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2shwpkh(pk: &bitcoin::CompressedPublicKey, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2shwsh(script: &bitcoin::blockdata::script::Script, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2tr(secp: &secp256k1::Secp256k1, internal_key: bitcoin::key::UntweakedPublicKey, merkle_root: core::option::Option, hrp: impl core::convert::Into) -> bitcoin::address::Address diff --git a/api/bitcoin/no-features.txt b/api/bitcoin/no-features.txt index 3e1e22a3c..d9fe72690 100644 --- a/api/bitcoin/no-features.txt +++ b/api/bitcoin/no-features.txt @@ -6154,6 +6154,7 @@ pub fn bitcoin::address::Address::is_spend_standard(&self) -> bool pub fn bitcoin::address::Address::matches_script_pubkey(&self, script: &bitcoin::blockdata::script::Script) -> bool pub fn bitcoin::address::Address::p2pkh(pk: impl core::convert::Into, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2sh(script: &bitcoin::blockdata::script::Script, network: impl core::convert::Into) -> core::result::Result +pub fn bitcoin::address::Address::p2sh_from_hash(hash: bitcoin::blockdata::script::ScriptHash, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2shwpkh(pk: &bitcoin::CompressedPublicKey, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2shwsh(script: &bitcoin::blockdata::script::Script, network: impl core::convert::Into) -> bitcoin::address::Address pub fn bitcoin::address::Address::p2tr(secp: &secp256k1::Secp256k1, internal_key: bitcoin::key::UntweakedPublicKey, merkle_root: core::option::Option, hrp: impl core::convert::Into) -> bitcoin::address::Address