@@ -400,29 +400,27 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
400
400
}
401
401
}
402
402
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.
406
404
///
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 {
411
408
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.
417
415
// The item 5:
418
416
// - For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac`.
419
417
Descriptor :: Wpkh ( ref pk) | Descriptor :: ShWpkh ( ref pk) => {
420
418
let addr = bitcoin:: Address :: p2pkh ( & pk. to_public_key ( ) , bitcoin:: Network :: Bitcoin ) ;
421
- Ok ( addr. script_pubkey ( ) )
419
+ addr. script_pubkey ( )
422
420
}
423
421
// - For P2WSH witness program, if the witnessScript does not contain any `OP_CODESEPARATOR`,
424
422
// 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 ( ) ,
426
424
}
427
425
}
428
426
}
@@ -1031,10 +1029,7 @@ mod tests {
1031
1029
)
1032
1030
. unwrap ( ) ;
1033
1031
assert_eq ! (
1034
- * descriptor
1035
- . script_code( )
1036
- . expect( "script_code() on P2WPKH descriptor" )
1037
- . as_bytes( ) ,
1032
+ * descriptor. script_code( ) . as_bytes( ) ,
1038
1033
Vec :: <u8 >:: from_hex( "76a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac" ) . unwrap( ) [ ..]
1039
1034
) ;
1040
1035
@@ -1044,10 +1039,7 @@ mod tests {
1044
1039
)
1045
1040
. unwrap ( ) ;
1046
1041
assert_eq ! (
1047
- * descriptor
1048
- . script_code( )
1049
- . expect( "script_code() on P2SH-P2WPKH descriptor" )
1050
- . as_bytes( ) ,
1042
+ * descriptor. script_code( ) . as_bytes( ) ,
1051
1043
Vec :: <u8 >:: from_hex( "76a91479091972186c449eb1ded22b78e40d009bdf008988ac" ) . unwrap( ) [ ..]
1052
1044
) ;
1053
1045
@@ -1059,7 +1051,6 @@ mod tests {
1059
1051
assert_eq ! (
1060
1052
* descriptor
1061
1053
. script_code( )
1062
- . expect( "script_code() on P2WSH descriptor" )
1063
1054
. as_bytes( ) ,
1064
1055
Vec :: <u8 >:: from_hex( "522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae" ) . unwrap( ) [ ..]
1065
1056
) ;
@@ -1069,7 +1060,6 @@ mod tests {
1069
1060
assert_eq ! (
1070
1061
* descriptor
1071
1062
. script_code( )
1072
- . expect( "script_code() on P2SH-P2WSH descriptor" )
1073
1063
. as_bytes( ) ,
1074
1064
Vec :: <u8 >:: from_hex( "522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae" )
1075
1065
. unwrap( ) [ ..]
0 commit comments