Multi-signature Nested Segwit 2 of 4
To follow along this tutorial
|
Let’s create a 2 of 4 multi-signature with an embedded Segwit P2SH-P2WSH transaction.
Creating and Funding the P2SH
const bitcoin = require('bitcoinjs-lib')
const { alice, bob, carol, dave } = require('./wallets.json')
const network = bitcoin.networks.regtest
const keyPairAlice1 = bitcoin.ECPair.fromWIF(alice[1].wif, network)
const keyPairBob1 = bitcoin.ECPair.fromWIF(bob[1].wif, network)
const keyPairCarol1 = bitcoin.ECPair.fromWIF(carol[1].wif, network)
const keyPairDave1 = bitcoin.ECPair.fromWIF(dave[1].wif, network)
const keyPairAlice2 = bitcoin.ECPair.fromWIF(alice[2].wif, network)
const p2wpkhAlice2 = bitcoin.payments.p2wpkh({pubkey: keyPairAlice2.publicKey, network})
p2ms
payment method.const p2ms = bitcoin.payments.p2ms({
m: 2, pubkeys: [
keyPairAlice1.publicKey,
keyPairBob1.publicKey,
keyPairCarol1.publicKey,
keyPairDave1.publicKey], network})
console.log('Script:')
console.log(p2ms.output.toString('hex'))
decodescript SCRIPT
02 03745c9aceb84dcdeddf2c3cdc1edb0b0b5af2f9bf85612d73fa6394758eaee35d 027efbabf425077cdbceb73f6681c7ebe2ade74a65ea57ebcf0c42364d3822c590 023a11cfcedb993ff2e7523f92e359c4454072a66d42e8b74b4b27a8a1258abddd 02e9d617f38f8c3ab9a6bde36ce991bafb295d7adba457699f8620c8160ec9e87a 04 OP_CHECKMULTISIG
p2sh
method with the p2wsh
object.const p2wsh = bitcoin.payments.p2wsh({redeem: p2ms, network})
const p2sh = bitcoin.payments.p2sh({redeem: p2wsh, network})
console.log('P2SH address:')
console.log(p2sh.address)
sendtoaddress 2N4LnN5rp8JAmqE3LBVQhYEQg83piAF15sX 1
gettransaction TX_ID
Find the output index (or vout) under | .
Preparing the spending transaction
Now let’s prepare the spending transaction by setting input and output and having two people (private keys) to sign the transaction. Here alice_1 and bob_1 will redeem the P2SH-P2WSH multi-signature and send the funds to alice_2 P2WPKH address.
const txb = new bitcoin.TransactionBuilder(network)
txb.addInput('TX_ID', TX_VOUT)
txb.addOutput(p2wpkhAlice2.address, 999e5)
// txb.sign(index, keyPair, redeemScript, sign.hashType, value, witnessScript)
txb.sign(0, keyPairAlice1, p2sh.redeem.output, null, 1e8, p2wsh.redeem.output)
txb.sign(0, keyPairBob1, p2sh.redeem.output, null, 1e8, p2wsh.redeem.output)
Note that, because we are doing a P2SH-P2WSH, we need to provide the locking script as the redeemScript third parameter, the same script as the witnessScript sixth parameter, as well as the input value. |
const tx = txb.build()
console.log('Transaction hexadecimal:')
console.log(tx.toHex())
decoderawtransaction TX_HEX
Broadcasting the transaction
sendrawtransaction TX_HEX
getrawtransaction TX_ID true
Observations
We can see that the scriptSig is a special unlocking script that contains the version byte 00
followed by a 32-bytes witness program. This script (asm) has to match the HASH160 contained in the P2SH UTXO script we are spending.
$ bx bitcoin160 '00205b07dcc35fc2b29db80be059e495c88f5b7609c1e3d888c14240678f00217b3d'
79b67d4c7bff512939e90e170ee9b969eb1203a8
or
bitcoin.crypto.hash160(Buffer.from('00205b07dcc35fc2b29db80be059e495c88f5b7609c1e3d888c14240678f00217b3d', 'hex')).toString('hex')
After checking hash equality, the script interpreter recognize that it is actually a Segwit transaction thanks to the version byte and triggers execution of the witness data. The witness, located in the txinwitness
field contains
-
an empty string that will convert to a dummy but mandatory
00
value due to a bug inOP_CHECKMULTISIG
-
Alice_1 and bob_1 signatures
-
and our witness script