@@ -393,29 +393,27 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
393
393
}
394
394
}
395
395
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.
399
397
///
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 {
404
401
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.
410
408
// The item 5:
411
409
// - For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac`.
412
410
Descriptor :: Wpkh ( ref pk) | Descriptor :: ShWpkh ( ref pk) => {
413
411
let addr = bitcoin:: Address :: p2pkh ( & pk. to_public_key ( ) , bitcoin:: Network :: Bitcoin ) ;
414
- Ok ( addr. script_pubkey ( ) )
412
+ addr. script_pubkey ( )
415
413
}
416
414
// - For P2WSH witness program, if the witnessScript does not contain any `OP_CODESEPARATOR`,
417
415
// 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 ( ) ,
419
417
}
420
418
}
421
419
}
@@ -1024,10 +1022,7 @@ mod tests {
1024
1022
)
1025
1023
. unwrap ( ) ;
1026
1024
assert_eq ! (
1027
- * descriptor
1028
- . script_code( )
1029
- . expect( "script_code() on P2WPKH descriptor" )
1030
- . as_bytes( ) ,
1025
+ * descriptor. script_code( ) . as_bytes( ) ,
1031
1026
Vec :: <u8 >:: from_hex( "76a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac" ) . unwrap( ) [ ..]
1032
1027
) ;
1033
1028
@@ -1037,10 +1032,7 @@ mod tests {
1037
1032
)
1038
1033
. unwrap ( ) ;
1039
1034
assert_eq ! (
1040
- * descriptor
1041
- . script_code( )
1042
- . expect( "script_code() on P2SH-P2WPKH descriptor" )
1043
- . as_bytes( ) ,
1035
+ * descriptor. script_code( ) . as_bytes( ) ,
1044
1036
Vec :: <u8 >:: from_hex( "76a91479091972186c449eb1ded22b78e40d009bdf008988ac" ) . unwrap( ) [ ..]
1045
1037
) ;
1046
1038
@@ -1052,7 +1044,6 @@ mod tests {
1052
1044
assert_eq ! (
1053
1045
* descriptor
1054
1046
. script_code( )
1055
- . expect( "script_code() on P2WSH descriptor" )
1056
1047
. as_bytes( ) ,
1057
1048
Vec :: <u8 >:: from_hex( "522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae" ) . unwrap( ) [ ..]
1058
1049
) ;
@@ -1062,7 +1053,6 @@ mod tests {
1062
1053
assert_eq ! (
1063
1054
* descriptor
1064
1055
. script_code( )
1065
- . expect( "script_code() on P2SH-P2WSH descriptor" )
1066
1056
. as_bytes( ) ,
1067
1057
Vec :: <u8 >:: from_hex( "522103789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd2103dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a6162652ae" )
1068
1058
. unwrap( ) [ ..]
0 commit comments