Skip to content

Commit efd971b

Browse files
authored
Merge pull request #127 from darosior/legacy_do_have_scriptcode
descriptors: define script_code() for legacy outputs
2 parents cda6b5e + 8582c17 commit efd971b

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
@@ -400,29 +400,27 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
400400
}
401401
}
402402

403-
/// Get the `scriptCode` as [defined by
404-
/// bip-0143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification),
405-
/// used to generate sighashes for spending Segwit outputs.
403+
/// Get the `scriptCode` of a transaction output.
406404
///
407-
/// # Errors
408-
/// - If the script descriptor type is non-segwit, as the script code is only defined for
409-
/// segregated witness transaction outputs.
410-
pub fn script_code(&self) -> Result<Script, Error> {
405+
/// The `scriptCode` is the Script of the previous transaction output being serialized in the
406+
/// sighash when evaluating a `CHECKSIG` & co. OP code.
407+
pub fn script_code(&self) -> Script {
411408
match *self {
412-
// Spending non-segwit outputs requires "legacy" sighash and don't use bip143's `scriptCode`.
413-
Descriptor::Bare(..)
414-
| Descriptor::Pk(..)
415-
| Descriptor::Pkh(..)
416-
| Descriptor::Sh(..) => Err(Error::BadDescriptor),
409+
// For "legacy" non-P2SH outputs, it is defined as the txo's scriptPubKey.
410+
Descriptor::Bare(..) | Descriptor::Pk(..) | Descriptor::Pkh(..) => self.script_pubkey(),
411+
// For "legacy" P2SH outputs, it is defined as the txo's redeemScript.
412+
Descriptor::Sh(ref d) => d.encode(),
413+
// For SegWit outputs, it is defined by bip-0143 (quoted below) and is different from
414+
// the previous txo's scriptPubKey.
417415
// The item 5:
418416
// - For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac`.
419417
Descriptor::Wpkh(ref pk) | Descriptor::ShWpkh(ref pk) => {
420418
let addr = bitcoin::Address::p2pkh(&pk.to_public_key(), bitcoin::Network::Bitcoin);
421-
Ok(addr.script_pubkey())
419+
addr.script_pubkey()
422420
}
423421
// - For P2WSH witness program, if the witnessScript does not contain any `OP_CODESEPARATOR`,
424422
// the `scriptCode` is the `witnessScript` serialized as scripts inside CTxOut.
425-
Descriptor::Wsh(ref d) | Descriptor::ShWsh(ref d) => Ok(d.encode()),
423+
Descriptor::Wsh(ref d) | Descriptor::ShWsh(ref d) => d.encode(),
426424
}
427425
}
428426
}
@@ -1031,10 +1029,7 @@ mod tests {
10311029
)
10321030
.unwrap();
10331031
assert_eq!(
1034-
*descriptor
1035-
.script_code()
1036-
.expect("script_code() on P2WPKH descriptor")
1037-
.as_bytes(),
1032+
*descriptor.script_code().as_bytes(),
10381033
Vec::<u8>::from_hex("76a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac").unwrap()[..]
10391034
);
10401035

@@ -1044,10 +1039,7 @@ mod tests {
10441039
)
10451040
.unwrap();
10461041
assert_eq!(
1047-
*descriptor
1048-
.script_code()
1049-
.expect("script_code() on P2SH-P2WPKH descriptor")
1050-
.as_bytes(),
1042+
*descriptor.script_code().as_bytes(),
10511043
Vec::<u8>::from_hex("76a91479091972186c449eb1ded22b78e40d009bdf008988ac").unwrap()[..]
10521044
);
10531045

@@ -1059,7 +1051,6 @@ mod tests {
10591051
assert_eq!(
10601052
*descriptor
10611053
.script_code()
1062-
.expect("script_code() on P2WSH descriptor")
10631054
.as_bytes(),
10641055
Vec::<u8>::from_hex("522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae").unwrap()[..]
10651056
);
@@ -1069,7 +1060,6 @@ mod tests {
10691060
assert_eq!(
10701061
*descriptor
10711062
.script_code()
1072-
.expect("script_code() on P2SH-P2WSH descriptor")
10731063
.as_bytes(),
10741064
Vec::<u8>::from_hex("522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae")
10751065
.unwrap()[..]

0 commit comments

Comments
 (0)