Skip to content

Commit 8582c17

Browse files
committed
descriptors: define script_code() for legacy outputs
This fixes the incorrect assumption made in 283676e that the `scriptCode` was defined in bip-0143. Reported-by: Andrew Poelstra <[email protected]> Signed-off-by: Antoine Poinsot <[email protected]>
1 parent a3370b7 commit 8582c17

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

src/descriptor/mod.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -393,29 +393,27 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
393393
}
394394
}
395395

396-
/// Get the `scriptCode` as [defined by
397-
/// bip-0143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification),
398-
/// used to generate sighashes for spending Segwit outputs.
396+
/// Get the `scriptCode` of a transaction output.
399397
///
400-
/// # Errors
401-
/// - If the script descriptor type is non-segwit, as the script code is only defined for
402-
/// segregated witness transaction outputs.
403-
pub fn script_code(&self) -> Result<Script, Error> {
398+
/// The `scriptCode` is the Script of the previous transaction output being serialized in the
399+
/// sighash when evaluating a `CHECKSIG` & co. OP code.
400+
pub fn script_code(&self) -> Script {
404401
match *self {
405-
// Spending non-segwit outputs requires "legacy" sighash and don't use bip143's `scriptCode`.
406-
Descriptor::Bare(..)
407-
| Descriptor::Pk(..)
408-
| Descriptor::Pkh(..)
409-
| Descriptor::Sh(..) => Err(Error::BadDescriptor),
402+
// For "legacy" non-P2SH outputs, it is defined as the txo's scriptPubKey.
403+
Descriptor::Bare(..) | Descriptor::Pk(..) | Descriptor::Pkh(..) => self.script_pubkey(),
404+
// For "legacy" P2SH outputs, it is defined as the txo's redeemScript.
405+
Descriptor::Sh(ref d) => d.encode(),
406+
// For SegWit outputs, it is defined by bip-0143 (quoted below) and is different from
407+
// the previous txo's scriptPubKey.
410408
// The item 5:
411409
// - For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac`.
412410
Descriptor::Wpkh(ref pk) | Descriptor::ShWpkh(ref pk) => {
413411
let addr = bitcoin::Address::p2pkh(&pk.to_public_key(), bitcoin::Network::Bitcoin);
414-
Ok(addr.script_pubkey())
412+
addr.script_pubkey()
415413
}
416414
// - For P2WSH witness program, if the witnessScript does not contain any `OP_CODESEPARATOR`,
417415
// the `scriptCode` is the `witnessScript` serialized as scripts inside CTxOut.
418-
Descriptor::Wsh(ref d) | Descriptor::ShWsh(ref d) => Ok(d.encode()),
416+
Descriptor::Wsh(ref d) | Descriptor::ShWsh(ref d) => d.encode(),
419417
}
420418
}
421419
}
@@ -1024,10 +1022,7 @@ mod tests {
10241022
)
10251023
.unwrap();
10261024
assert_eq!(
1027-
*descriptor
1028-
.script_code()
1029-
.expect("script_code() on P2WPKH descriptor")
1030-
.as_bytes(),
1025+
*descriptor.script_code().as_bytes(),
10311026
Vec::<u8>::from_hex("76a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac").unwrap()[..]
10321027
);
10331028

@@ -1037,10 +1032,7 @@ mod tests {
10371032
)
10381033
.unwrap();
10391034
assert_eq!(
1040-
*descriptor
1041-
.script_code()
1042-
.expect("script_code() on P2SH-P2WPKH descriptor")
1043-
.as_bytes(),
1035+
*descriptor.script_code().as_bytes(),
10441036
Vec::<u8>::from_hex("76a91479091972186c449eb1ded22b78e40d009bdf008988ac").unwrap()[..]
10451037
);
10461038

@@ -1052,7 +1044,6 @@ mod tests {
10521044
assert_eq!(
10531045
*descriptor
10541046
.script_code()
1055-
.expect("script_code() on P2WSH descriptor")
10561047
.as_bytes(),
10571048
Vec::<u8>::from_hex("522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae").unwrap()[..]
10581049
);
@@ -1062,7 +1053,6 @@ mod tests {
10621053
assert_eq!(
10631054
*descriptor
10641055
.script_code()
1065-
.expect("script_code() on P2SH-P2WSH descriptor")
10661056
.as_bytes(),
10671057
Vec::<u8>::from_hex("522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae")
10681058
.unwrap()[..]

0 commit comments

Comments
 (0)