I want to generate P2WSH to study it. I read the spec
https://bitcoincore.org/en/segwit_wallet_dev/#creation-of-p2sh-p2wsh-address but I have an error during spend it. (redeemScript does not correspond to witnessScript)
I create a bash script to generate private and public key, and I will report only the part to create redeem script etc.
Public key
02aa31bcc43d302bd217761e3268b3fde67078a8ddcb157517f9c69633f5f9e214
Witness script
SCRIPT="76a9"$PBLENGTH$PBH"88AC"
printf $SCRIPT > witness_script.txt
> result: 76a9143a067efd9e4c79d0b8c94d81108fa21920ef896788AC
Create Script Hash, use sha256 once
ADDR_SHA=`printf $SCRIPT | xxd -r -p | openssl sha256| sed 's/^.* //'`
printf $ADDR_SHA > scripthash.txt
> result: bd73935d1af51d3e4490f158cc2585fdd8ac81cae898855faa8e26606a7e9934
Create redeem script (i.e. 0x0020{32-byte scripthash})
REDEEMSCRIPT="0020"$ADDR_SHA
printf $REDEEMSCRIPT > redeem_script.txt
> result: 0020bd73935d1af51d3e4490f158cc2585fdd8ac81cae898855faa8e26606a7e9934
Create SCRIPTPUBKEY (OP_HASH160 hash160(redeemScript) OP_EQUAL) where hash160 is Sha256 and ripemd160
ADDR_160=$(printf $(printf $REDEEMSCRIPT | xxd -r -p | openssl sha256| sed 's/^.* //') | xxd -r -p | openssl ripemd160 | sed 's/^.* //')
SCRIPTPUBKEY="A914"$ADDR_160"87"
printf $SCRIPTPUBKEY > scriptPubKey.txt
> result: A914bb18c1d4c6d65ca60f4dee268475c429f9896b6587
Create Address (regtest, prefix is C4)
ADDR=`printf $VERSION_PREFIX_ADDRESS$ADDR_160 | xxd -p -r | base58 -c`
printf $ADDR > address_p2sh-p2wsh.txt
> result: 2NAJW1K8Us8ymnzZvRJY4JPqKQ6VqRcMqd7
Then I create a transaction from this address to anther address. I have 50 bitcoin available.
REDEEMSCRIPT=`cat redeem_script.txt`
SCRIPTPUBKEY=`cat scriptPubKey.txt`
WITNESSCRIPT=`cat witness_script.txt`
TX_DATA=$(bitcoin-cli createrawtransaction '[{"txid":"'$TXID'","vout":'$VOUT'}]' '[{"'$ADDR_DEST'":'$AMOUNT'}]')
TX_SIGNED=$(bitcoin-cli signrawtransactionwithkey $TX_DATA '["'$PK'"]' '[{"txid":"'$TXID'","vout":'$VOUT',"scriptPubKey":"'$SCRIPTPUBKEY'","redeemScript":"'$REDEEMSCRIPT'","witnessScript":"'$WITNESSCRIPT'","amount":"'$TOTAL_UTXO_AMOUNT'"}]' | jq -r '.hex')
At that moment I have this error
redeemScript does not correspond to witnessScript
TX_DATA:
{
"txid": "b0a12356d1caa8bb6cfb1dd572c520742fc96d176762a10b8f669118744417c7",
"hash": "b0a12356d1caa8bb6cfb1dd572c520742fc96d176762a10b8f669118744417c7",
"version": 2,
"size": 85,
"vsize": 85,
"weight": 340,
"locktime": 0,
"vin": [
{
"txid": "4de43b38df24ff350ac9821637ea8996fa33d20aa99677b86918f577697a3e15",
"vout": 0,
"scriptSig": {
"asm": "",
"hex": ""
},
"sequence": 4294967295
}
],
"vout": [
{
"value": 49.99100000,
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 af886b3952c6b69e25721d6ffbf3d96c39ca4352 OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a914af886b3952c6b69e25721d6ffbf3d96c39ca435288ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"mwX5yAv3Q42VsebJwuk3Qzn2mikWBugF4g"
]
}
}
]
}
UTXO
$ bitcoin-cli getrawtransaction 4de43b38df24ff350ac9821637ea8996fa33d20aa99677b86918f577697a3e15 2
{
"txid": "4de43b38df24ff350ac9821637ea8996fa33d20aa99677b86918f577697a3e15",
"hash": "df9abe47f73aacbe0fd021868123c0a7e9ab923db9d8a4972f12bdb36d8e89d6",
"version": 2,
"size": 169,
"vsize": 142,
"weight": 568,
"locktime": 0,
"vin": [
{
"coinbase": "510101",
"sequence": 4294967295
}
],
"vout": [
{
"value": 50.00000000,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 bb18c1d4c6d65ca60f4dee268475c429f9896b65 OP_EQUAL",
"hex": "a914bb18c1d4c6d65ca60f4dee268475c429f9896b6587",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2NAJW1K8Us8ymnzZvRJY4JPqKQ6VqRcMqd7"
]
}
},
{
"value": 0.00000000,
"n": 1,
"scriptPubKey": {
"asm": "OP_RETURN aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
"hex": "6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
"type": "nulldata"
}
}
],
"hex": "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0200f2052a0100000017a914bb18c1d4c6d65ca60f4dee268475c429f9896b65870000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000",
"blockhash": "4fa868b42b4f935ff48ae10893f5dad5cb1a0fb5c1f1dd38b7cbe476aa3c0270",
"confirmations": 107,
"time": 1585666082,
"blocktime": 1585666082
}