Skip to content

Commit 7754209

Browse files
committed
Add Script conversion method p2wpkh_script_code
In order to sign a utxo that does a p2wpkh spend we need to create the script that can be used to create a sighash. In the libbitcoin docs this is referred to as the 'script code' [0]. The script is the same as a p2pkh script but the pubkey_hash is found in the scriptPubkey. Add a `Script` conversion method that checks if `self` is a v0 p2wpkh script and if so extracts the pubkey_hash and returns the required script. [0] https://github.com/libbitcoin/libbitcoin-system/wiki/P2WPKH-Transactions#spending-a-p2wpkh-output
1 parent 8efc9a1 commit 7754209

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/blockdata/script.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,25 @@ impl Script {
427427
Script::new_p2sh(&self.script_hash())
428428
}
429429

430+
/// Returns the script code used for spending a P2WPKH output if this script is a script pubkey
431+
/// for a P2WPKH output. The `scriptCode` is described in [BIP143].
432+
///
433+
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
434+
pub fn p2wpkh_script_code(&self) -> Option<Script> {
435+
if !self.is_v0_p2wpkh() {
436+
return None
437+
}
438+
let script = Builder::new()
439+
.push_opcode(opcodes::all::OP_DUP)
440+
.push_opcode(opcodes::all::OP_HASH160)
441+
.push_slice(&self[2..]) // The `self` script is 0x00, 0x14, <pubkey_hash>
442+
.push_opcode(opcodes::all::OP_EQUALVERIFY)
443+
.push_opcode(opcodes::all::OP_CHECKSIG)
444+
.into_script();
445+
446+
Some(script)
447+
}
448+
430449
/// Computes the P2WSH output corresponding to this witnessScript (aka the "witness redeem
431450
/// script").
432451
pub fn to_v0_p2wsh(&self) -> Script {

0 commit comments

Comments
 (0)