Sunday, November 24, 2024

bitcoin cli – Create and spend P2WSH that locks cash with OP_CHECKLOCKTIMEVERIFY

I am making an attempt to create a P2SWH tackle with a script that locks the cash for a while. After which I wish to spend these cash from that tackle.

I’ve learn quite a bit, a number of the most related:

These are the steps I’ve taken. The place am I going fallacious?

  1. Create an tackle to be locked
bitcoin-cli -regtest -getnewaddress
bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs
  1. Get the pubkey
bitcoin-cli -regtest getaddressinfo bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs
{
  "tackle": "bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs",
  "scriptPubKey": "0014ad060529794593cac3e55a6eb26a0c6589d96815",
  "ismine": true,
  "solvable": true,
  "desc": "wpkh([10304ad5/84'/1'/0'/0/0]0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8)#52elxezw",
  "parent_desc": "wpkh([10304ad5/84'/1'/0']tpubDDr5yrdp9CqDy4qv2Waa1Z5aeQUzQh4Rxs84XyT8vHXTNKEaR6rDhTY56sSYC4vBdbKj1fHbwEPuimcu1yY3RSN8u1RvSREdyAUEDmfWzFv/0/*)#82xg44pv",
  "iswatchonly": false,
  "isscript": false,
  "iswitness": true,
  "witness_version": 0,
  "witness_program": "ad060529794593cac3e55a6eb26a0c6589d96815",
  "pubkey": "0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8",
  "ischange": false,
  "timestamp": 1679657480,
  "hdkeypath": "m/84'/1'/0'/0/0",
  "hdseedid": "0000000000000000000000000000000000000000",
  "hdmasterfingerprint": "10304ad5",
  "labels": [
    ""
  ]
}
  1. Convert pubkey into pubkeyhash
echo -n 0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8 | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
ad060529794593cac3e55a6eb26a0c6589d96815
  1. Get little-endian hex of timestamp for 1 March 2023 (1677628800)
printf '%xn' 1677628800 | fold -w2 | tac | tr -d "n"
8095fe63
  1. Create script utilizing btcc from btcdeb
btcc 8095fe63 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 ad060529794593cac3e55a6eb26a0c6589d96815 OP_EQUALVERIFY OP_CHECKSIG
048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
  1. Get ScriptHash
echo -n 048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac | xxd -r -p | openssl dgst -sha256
5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
  1. Decode script to test and get P2WSH (or might compute)
bitcoin-cli -regtest decodescript 048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
{
  "asm": "1677628800 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 ad060529794593cac3e55a6eb26a0c6589d96815 OP_EQUALVERIFY OP_CHECKSIG",
  "desc": "uncooked(048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac)#snjtcwv8",
  "kind": "nonstandard",
  "p2sh": "2N6zwcmxWbwkcVRwY275T8UXJ9mR9AtFaQo",
  "segwit": {
    "asm": "0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb",
    "desc": "addr(bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr)#afsheh6p",
    "hex": "00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb",
    "tackle": "bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr",
    "kind": "witness_v0_scripthash",
    "p2sh-segwit": "2N5EceqhW9HtQxRnL8qRCscz8Udv9P3bvwT"
  }
}
  1. Ship some cash to handle (after which generate some blocks to substantiate txn)
bitcoin-cli -regtest -named sendtoaddress tackle="bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr" quantity="1.0" fee_rate=1.0
3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c

Above txn decoded

{
  "txid": "3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c",
  "hash": "30e06e1c9b41029638e2d4e8f3e7813193299840f2333d61a4ddf244201723cb",
  "model": 2,
  "dimension": 246,
  "vsize": 165,
  "weight": 657,
  "locktime": 241,
  "vin": [
    {
      "txid": "47495befacd402d9f73f6ba8148e434fa6bf1d1957b87524fef41d8fdea409e4",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "30440220181c3f5b2144f4e11eb8fc6b4a996e530774936fb2de21f5d85db42928a003f002205065e3ccbf29978545bdf9088de8100f851f04026766c23a37b519a7fc104a4201",
        "022e6433df12239f3cce79c9260fa8394d53902f05d017e86ff430108cdfecc54e"
      ],
      "sequence": 4294967293
    }
  ],
  "vout": [
    {
      "value": 48.99999835,
      "n": 0,
      "scriptPubKey": {
        "asm": "1 063ae126ecdab401ee960d7431b0eff76c1c28986133f84d285c4ef988270552",
        "desc": "rawtr(063ae126ecdab401ee960d7431b0eff76c1c28986133f84d285c4ef988270552)#83pfucst",
        "hex": "5120063ae126ecdab401ee960d7431b0eff76c1c28986133f84d285c4ef988270552",
        "address": "bcrt1pqcawzfhvm26qrm5kp46rrv807akpc2ycvyelsnfgt380nzp8q4fqf67ehv",
        "type": "witness_v1_taproot"
      }
    },
    {
      "value": 1.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb",
        "desc": "addr(bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr)#afsheh6p",
        "hex": "00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb",
        "address": "bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr",
        "type": "witness_v0_scripthash"
      }
    }
  ]
}
  1. Create a brand new tackle to ship to and outline some variables, then create uncooked txn
LOCKTIME=247
OUT_ADDR=bcrt1qmydu04snguq9lrtg9rmenu4dvx0zzlefdtpe8p
OUT_AMNT="0.998"
IN_TXID=3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c
IN_VOUT=1

bitcoin-cli -regtest -named createrawtransaction inputs="[{"txid":""$IN_TXID'", "vout":'$IN_VOUT'}]' outputs="[{""$OUT_ADDR'":'$OUT_AMNT'}]' locktime=$LOCKTIME replaceable=true
02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f297e022500
  1. Create scriptpubky: OP_HASH160 hash160(redeemScript) OP_EQUAL
    redeemScript is 0200 + scripthash (I am not too certain about this step…)
echo -n 00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
838403567aa049ada65f944d0a2a320d7b93e7ee
# mix remainder of script hex
A914838403567aa049ada65f944d0a2a320d7b93e7ee87
  1. Extra variables and try to signal uncooked txn

TX_DATA=02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f29f7000000
WITNESSCRIPT=048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
SCRIPTHASH=5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
REDEEMSCRIPT=00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
SCRIPTPUBKEY=A914838403567aa049ada65f944d0a2a320d7b93e7ee87

bitcoin-cli -regtest -named signrawtransactionwithwallet hexstring=$TX_DATA prevtxs="[{"txid":""$IN_TXID'","vout":'$IN_VOUT',"redeemScript":"'$REDEEMSCRIPT'","scriptPubKey":"'$SCRIPTPUBKEY'","witnessScript":"'$WITNESSCRIPT'","amount":"'$OUT_AMNT'"}]'

error code: -22
error message:
Earlier output scriptPubKey mismatch:
0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
vs:
OP_HASH160 838403567aa049ada65f944d0a2a320d7b93e7ee OP_EQUAL

Primarily based on that error, it appears to be anticipating the redeemscript, so I give {that a} try to get a distinct error:

bitcoin-cli -regtest -named signrawtransactionwithwallet hexstring=$TX_DATA prevtxs="[{"txid":""$IN_TXID'","vout":'$IN_VOUT',"redeemScript":"'$REDEEMSCRIPT'","scriptPubKey":"'$REDEEMSCRIPT'","witnessScript":"'$WITNESSCRIPT'","amount":"'$OUT_AMNT'"}]'
{
  "hex": "02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f29f7000000",
  "full": false,
  "errors": [
    {
      "txid": "3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c",
      "vout": 1,
      "witness": [
      ],
      "scriptSig": "",
      "sequence": 4294967293,
      "error": "Witness program was handed an empty witness"
    }
  ]
}

Wanting on the decoded hex of this incomplete txn, I see there is no such thing as a witness knowledge. Am I alleged to be supplying that? I am in all probability getting one thing fallacious alongside the best way, however unsure what.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles