<p>It looks like you're trying to create and spend from a P2WSH (Pay-to-Witness-Script-Hash) address with a time-lock condition.  I'll highlight where there might be issues or confusion, as well as how to resolve them.</p>
<h3>1. Script Construction</h3>
<p>You correctly generated a P2WSH script that uses <code>OP_CHECKLOCKTIMEVERIFY</code> (CLTV) to lock the coins until a certain time.</p>
<p>The script you're using is:</p>
<pre><code>048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
</code></pre>
<p>This translates to:</p>
<ul>
<li><code>8095fe63</code>: The locktime (timestamp for 1 March 2023 in little-endian format).</li>
<li><code>b175</code>: <code>OP_CHECKLOCKTIMEVERIFY</code> and <code>OP_DROP</code>.</li>
<li><code>76a914ad060529794593cac3e55a6eb26a0c6589d9681588ac</code>: P2PKH part of the script (<code>OP_DUP OP_HASH160 &lt;pubkeyhash&gt; OP_EQUALVERIFY OP_CHECKSIG</code>).</li>
</ul>
<p>This script seems correct for locking coins with a CLTV condition.</p>
<h3>2. Generating the Address</h3>
<p>You computed the correct witness script hash (P2WSH) for the locking script:</p>
<ul>
<li><code>5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb</code>.</li>
</ul>
<p>This P2WSH can be verified by the output of the <code>decodescript</code> command and the address you generated:</p>
<ul>
<li><code>bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr</code> is the correct P2WSH address.</li>
</ul>
<p>So, this part of the process looks correct.</p>
<h3>3. Sending Funds to the Address</h3>
<p>You successfully sent 1 BTC to the P2WSH address (<code>bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr</code>), and the transaction seems confirmed.</p>
<h3>4. Spending from the Time-Locked Address</h3>
<p>Here's where the complexity increases, as spending from a CLTV-locked P2WSH requires meeting specific conditions:</p>
<ol>
<li><strong>Locktime in Transaction:</strong> The <code>nLocktime</code> of the transaction must be set to a value <strong>greater than or equal to</strong> the locktime in the script (i.e., <code>1677628800</code>).</li>
<li><strong>Sequence Number:</strong> The input's sequence number must be less than <code>0xFFFFFFFF</code> (which you correctly set to <code>4294967293</code>), indicating that the input is allowed to be spent according to locktime rules.</li>
<li><strong>Witness Stack:</strong> To spend the P2WSH output, the witness stack must include:
<ul>
<li>The signature for the public key.</li>
<li>The public key.</li>
<li>The locking script (<code>048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac</code>).</li>
</ul>
</li>
</ol>
<h3>Key Areas to Check:</h3>
<ol>
<li><p><strong>Locktime Mismatch</strong>: The <code>nLocktime</code> in your raw transaction is set to <code>247</code>:</p>
<pre><code>02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f297e022500
</code></pre>
<p>However, the locktime in your script is <code>1677628800</code>, so you need to set the locktime of the transaction to <strong>at least <code>1677628800</code></strong> (March 1, 2023). Without doing this, the transaction will be invalid because the nLocktime doesn’t meet the script's requirement.</p>
</li>
<li><p><strong>Creating the Correct Witness Data</strong>: For spending from a P2WSH address, you need to provide the correct witness data:</p>
<ul>
<li>The first element is the unlocking signature.</li>
<li>The second element is the public key.</li>
<li>The third element is the actual script itself (which includes the <code>OP_CHECKLOCKTIMEVERIFY</code> part).</li>
</ul>
</li>
<li><p><strong>Raw Transaction Creation</strong>:</p>
<ul>
<li>When creating the raw transaction, make sure you include the witness program for the script and use the correct <code>nLocktime</code> in the transaction.</li>
</ul>
</li>
<li><p><strong>Redeem Script Confusion</strong>: You mention a redeem script (which is used in P2SH-P2WSH), but you're working directly with a P2WSH. If you were using P2SH-P2WSH, you would need a redeem script to &quot;reveal&quot; the P2WSH. Since you're using native P2WSH, you don't need a redeem script. The witness data directly includes the locking script.</p>
</li>
</ol>
<h3>Suggested Fixes:</h3>
<ol>
<li><p><strong>Set Correct nLocktime</strong>: When creating the spending transaction, set <code>nLocktime</code> to <code>1677628800</code> or a later block.</p>
<p>Example:</p>
<pre class="lang-bash prettyprint-override"><code>bitcoin-cli -regtest -named createrawtransaction inputs='[{&quot;txid&quot;:&quot;'$IN_TXID'&quot;, &quot;vout&quot;:'$IN_VOUT'}]' outputs='[{&quot;'$OUT_ADDR'&quot;:'$OUT_AMNT'}]' locktime=1677628800 replaceable=true
</code></pre>
</li>
<li><p><strong>Create Correct Witness</strong>:
Ensure the witness field contains the signature, public key, and locking script.</p>
</li>
<li><p><strong>Include Locking Script</strong>: When constructing the witness for the spending transaction, use the full locking script (<code>048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac</code>) as the last element.</p>
</li>
</ol>
<h3>Summary of Process:</h3>
<ol>
<li><strong>Locking Transaction</strong>: You correctly created a P2WSH address that locks coins with <code>OP_CHECKLOCKTIMEVERIFY</code>. The transaction that sent coins to this address is also correct.</li>
<li><strong>Spending Transaction</strong>: Ensure the spending transaction’s <code>nLocktime</code> matches or exceeds the locktime in the locking script. Construct the witness correctly when spending, including the signature, public key, and the locking script.</li>
</ol>

CC BY-SA 4.0
2024-09-05T19:46:22.650Z
2024-09-05T19:46:22.650Z
Al Nostru
<p>I'm attempting to create a P2SWH address with a script that locks the coins for some time. And then I want to spend those coins from that address.</p>
<p>I've read a lot, some of the most relevant:</p>
<ul>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki" rel="nofollow noreferrer">BIP173</a></li>
<li><a href="https://bitcoincore.org/en/segwit_wallet_dev/#creation-of-p2sh-p2wsh-address" rel="nofollow noreferrer">Segwit dev guide</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/81079/constructing-a-p2wsh-address-from-the-bip173-spec">Constructing a P2WSH address from the BIP173 spec</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/95023/generate-p2sh-p2wsh">Generate P2SH-P2WSH</a></li>
</ul>
<p>These are the steps I've taken. Where am I going wrong?</p>
<ol>
<li>Create an address to be locked</li>
</ol>
<pre><code>bitcoin-cli -regtest -getnewaddress
bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs
</code></pre>
<ol start="2">
<li>Get the pubkey</li>
</ol>
<pre><code>bitcoin-cli -regtest getaddressinfo bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs
{
  &quot;address&quot;: &quot;bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs&quot;,
  &quot;scriptPubKey&quot;: &quot;0014ad060529794593cac3e55a6eb26a0c6589d96815&quot;,
  &quot;ismine&quot;: true,
  &quot;solvable&quot;: true,
  &quot;desc&quot;: &quot;wpkh([10304ad5/84'/1'/0'/0/0]0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8)#52elxezw&quot;,
  &quot;parent_desc&quot;: &quot;wpkh([10304ad5/84'/1'/0']tpubDDr5yrdp9CqDy4qv2Waa1Z5aeQUzQh4Rxs84XyT8vHXTNKEaR6rDhTY56sSYC4vBdbKj1fHbwEPuimcu1yY3RSN8u1RvSREdyAUEDmfWzFv/0/*)#82xg44pv&quot;,
  &quot;iswatchonly&quot;: false,
  &quot;isscript&quot;: false,
  &quot;iswitness&quot;: true,
  &quot;witness_version&quot;: 0,
  &quot;witness_program&quot;: &quot;ad060529794593cac3e55a6eb26a0c6589d96815&quot;,
  &quot;pubkey&quot;: &quot;0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8&quot;,
  &quot;ischange&quot;: false,
  &quot;timestamp&quot;: 1679657480,
  &quot;hdkeypath&quot;: &quot;m/84'/1'/0'/0/0&quot;,
  &quot;hdseedid&quot;: &quot;0000000000000000000000000000000000000000&quot;,
  &quot;hdmasterfingerprint&quot;: &quot;10304ad5&quot;,
  &quot;labels&quot;: [
    &quot;&quot;
  ]
}
</code></pre>
<ol start="3">
<li>Convert pubkey into pubkeyhash</li>
</ol>
<pre><code>echo -n 0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8 | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
ad060529794593cac3e55a6eb26a0c6589d96815
</code></pre>
<ol start="4">
<li>Get little-endian hex of timestamp for 1 March 2023 (1677628800)</li>
</ol>
<pre><code>printf '%x\n' 1677628800 | fold -w2 | tac | tr -d &quot;\n&quot;
8095fe63
</code></pre>
<ol start="5">
<li>Create script using btcc from btcdeb</li>
</ol>
<pre><code>btcc 8095fe63 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 ad060529794593cac3e55a6eb26a0c6589d96815 OP_EQUALVERIFY OP_CHECKSIG
048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
</code></pre>
<ol start="6">
<li>Get ScriptHash</li>
</ol>
<pre><code>echo -n 048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac | xxd -r -p | openssl dgst -sha256
5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
</code></pre>
<ol start="7">
<li>Decode script to check and get P2WSH (or could compute)</li>
</ol>
<pre><code>bitcoin-cli -regtest decodescript 048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
{
  &quot;asm&quot;: &quot;1677628800 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 ad060529794593cac3e55a6eb26a0c6589d96815 OP_EQUALVERIFY OP_CHECKSIG&quot;,
  &quot;desc&quot;: &quot;raw(048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac)#snjtcwv8&quot;,
  &quot;type&quot;: &quot;nonstandard&quot;,
  &quot;p2sh&quot;: &quot;2N6zwcmxWbwkcVRwY275T8UXJ9mR9AtFaQo&quot;,
  &quot;segwit&quot;: {
    &quot;asm&quot;: &quot;0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb&quot;,
    &quot;desc&quot;: &quot;addr(bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr)#afsheh6p&quot;,
    &quot;hex&quot;: &quot;00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb&quot;,
    &quot;address&quot;: &quot;bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr&quot;,
    &quot;type&quot;: &quot;witness_v0_scripthash&quot;,
    &quot;p2sh-segwit&quot;: &quot;2N5EceqhW9HtQxRnL8qRCscz8Udv9P3bvwT&quot;
  }
}
</code></pre>
<ol start="8">
<li>Send some coins to address (and then generate some blocks to confirm txn)</li>
</ol>
<pre><code>bitcoin-cli -regtest -named sendtoaddress address=&quot;bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr&quot; amount=&quot;1.0&quot; fee_rate=1.0
3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c
</code></pre>
<p>Above txn decoded</p>
<pre><code>{
  &quot;txid&quot;: &quot;3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c&quot;,
  &quot;hash&quot;: &quot;30e06e1c9b41029638e2d4e8f3e7813193299840f2333d61a4ddf244201723cb&quot;,
  &quot;version&quot;: 2,
  &quot;size&quot;: 246,
  &quot;vsize&quot;: 165,
  &quot;weight&quot;: 657,
  &quot;locktime&quot;: 241,
  &quot;vin&quot;: [
    {
      &quot;txid&quot;: &quot;47495befacd402d9f73f6ba8148e434fa6bf1d1957b87524fef41d8fdea409e4&quot;,
      &quot;vout&quot;: 0,
      &quot;scriptSig&quot;: {
        &quot;asm&quot;: &quot;&quot;,
        &quot;hex&quot;: &quot;&quot;
      },
      &quot;txinwitness&quot;: [
        &quot;30440220181c3f5b2144f4e11eb8fc6b4a996e530774936fb2de21f5d85db42928a003f002205065e3ccbf29978545bdf9088de8100f851f04026766c23a37b519a7fc104a4201&quot;,
        &quot;022e6433df12239f3cce79c9260fa8394d53902f05d017e86ff430108cdfecc54e&quot;
      ],
      &quot;sequence&quot;: 4294967293
    }
  ],
  &quot;vout&quot;: [
    {
      &quot;value&quot;: 48.99999835,
      &quot;n&quot;: 0,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;1 063ae126ecdab401ee960d7431b0eff76c1c28986133f84d285c4ef988270552&quot;,
        &quot;desc&quot;: &quot;rawtr(063ae126ecdab401ee960d7431b0eff76c1c28986133f84d285c4ef988270552)#83pfucst&quot;,
        &quot;hex&quot;: &quot;5120063ae126ecdab401ee960d7431b0eff76c1c28986133f84d285c4ef988270552&quot;,
        &quot;address&quot;: &quot;bcrt1pqcawzfhvm26qrm5kp46rrv807akpc2ycvyelsnfgt380nzp8q4fqf67ehv&quot;,
        &quot;type&quot;: &quot;witness_v1_taproot&quot;
      }
    },
    {
      &quot;value&quot;: 1.00000000,
      &quot;n&quot;: 1,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb&quot;,
        &quot;desc&quot;: &quot;addr(bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr)#afsheh6p&quot;,
        &quot;hex&quot;: &quot;00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb&quot;,
        &quot;address&quot;: &quot;bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr&quot;,
        &quot;type&quot;: &quot;witness_v0_scripthash&quot;
      }
    }
  ]
}
</code></pre>
<ol start="9">
<li>Create a new address to send to and define some variables, then create raw txn</li>
</ol>
<pre><code>LOCKTIME=247
OUT_ADDR=bcrt1qmydu04snguq9lrtg9rmenu4dvx0zzlefdtpe8p
OUT_AMNT=&quot;0.998&quot;
IN_TXID=3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c
IN_VOUT=1

bitcoin-cli -regtest -named createrawtransaction inputs='[{&quot;txid&quot;:&quot;'$IN_TXID'&quot;, &quot;vout&quot;:'$IN_VOUT'}]' outputs='[{&quot;'$OUT_ADDR'&quot;:'$OUT_AMNT'}]' locktime=$LOCKTIME replaceable=true
02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f297e022500
</code></pre>
<ol start="10">
<li>Create scriptpubky: OP_HASH160 hash160(redeemScript) OP_EQUAL
redeemScript is <code>0200</code> + <code>scripthash</code> (I'm not too sure about this step...)</li>
</ol>
<pre><code>echo -n 00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
838403567aa049ada65f944d0a2a320d7b93e7ee
# combine rest of script hex
A914838403567aa049ada65f944d0a2a320d7b93e7ee87
</code></pre>
<ol start="11">
<li>More variables and attempt to sign raw txn</li>
</ol>
<pre><code>
TX_DATA=02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f29f7000000
WITNESSCRIPT=048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
SCRIPTHASH=5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
REDEEMSCRIPT=00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
SCRIPTPUBKEY=A914838403567aa049ada65f944d0a2a320d7b93e7ee87

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

error code: -22
error message:
Previous output scriptPubKey mismatch:
0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
vs:
OP_HASH160 838403567aa049ada65f944d0a2a320d7b93e7ee OP_EQUAL
</code></pre>
<p>Based on that error, it seems to be expecting the redeemscript, so I give that a try and get a different error:</p>
<pre><code>bitcoin-cli -regtest -named signrawtransactionwithwallet hexstring=$TX_DATA prevtxs='[{&quot;txid&quot;:&quot;'$IN_TXID'&quot;,&quot;vout&quot;:'$IN_VOUT',&quot;redeemScript&quot;:&quot;'$REDEEMSCRIPT'&quot;,&quot;scriptPubKey&quot;:&quot;'$REDEEMSCRIPT'&quot;,&quot;witnessScript&quot;:&quot;'$WITNESSCRIPT'&quot;,&quot;amount&quot;:&quot;'$OUT_AMNT'&quot;}]'
{
  &quot;hex&quot;: &quot;02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f29f7000000&quot;,
  &quot;complete&quot;: false,
  &quot;errors&quot;: [
    {
      &quot;txid&quot;: &quot;3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c&quot;,
      &quot;vout&quot;: 1,
      &quot;witness&quot;: [
      ],
      &quot;scriptSig&quot;: &quot;&quot;,
      &quot;sequence&quot;: 4294967293,
      &quot;error&quot;: &quot;Witness program was passed an empty witness&quot;
    }
  ]
}
</code></pre>
<p>Looking at the decoded hex of this incomplete txn, I see there is no witness data. Am I supposed to be supplying that? I'm probably getting something wrong along the way, but not sure what.</p>

<bitcoin-cli><raw-transaction><p2wsh><checklocktimeverify>
Create and spend P2WSH that locks coins with OP_CHECKLOCKTIMEVERIFY
wazint
# Create and spend P2WSH that locks coins with OP_CHECKLOCKTIMEVERIFY

(Asked by: username_0 on 2023-03-24)

I'm attempting to create a P2SWH address with a script that locks the coins for some time. And then I want to spend those coins from that address.

I've read a lot, some of the most relevant:

* [BIP173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)
* [Segwit dev guide](https://bitcoincore.org/en/segwit_wallet_dev/#creation-of-p2sh-p2wsh-address)
* [Constructing a P2WSH address from the BIP173 spec](https://bitcoin.stackexchange.com/questions/81079/constructing-a-p2wsh-address-from-the-bip173-spec)
* [Generate P2SH-P2WSH](https://bitcoin.stackexchange.com/questions/95023/generate-p2sh-p2wsh)

These are the steps I've taken. Where am I going wrong?

1. Create an address to be locked

```
bitcoin-cli -regtest -getnewaddress
bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs

```

2. Get the pubkey

```
bitcoin-cli -regtest getaddressinfo bcrt1q45rq22tegkfu4sl9tfhty6svvkyaj6q4wu43fs
{
  "address": "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": [
    ""
  ]
}

```

3. Convert pubkey into pubkeyhash

```
echo -n 0340469e0bd5cca35c04bcee20e203f8fe601b69d73793b493fe92900e620b67d8 | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
ad060529794593cac3e55a6eb26a0c6589d96815

```

4. Get little-endian hex of timestamp for 1 March 2023 (1677628800)

```
printf '%x\n' 1677628800 | fold -w2 | tac | tr -d "\n"
8095fe63

```

5. Create script using btcc from btcdeb

```
btcc 8095fe63 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 ad060529794593cac3e55a6eb26a0c6589d96815 OP_EQUALVERIFY OP_CHECKSIG
048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac

```

6. Get ScriptHash

```
echo -n 048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac | xxd -r -p | openssl dgst -sha256
5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb

```

7. Decode script to check and get P2WSH (or could compute)

```
bitcoin-cli -regtest decodescript 048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac
{
  "asm": "1677628800 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 ad060529794593cac3e55a6eb26a0c6589d96815 OP_EQUALVERIFY OP_CHECKSIG",
  "desc": "raw(048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac)#snjtcwv8",
  "type": "nonstandard",
  "p2sh": "2N6zwcmxWbwkcVRwY275T8UXJ9mR9AtFaQo",
  "segwit": {
    "asm": "0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb",
    "desc": "addr(bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr)#afsheh6p",
    "hex": "00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb",
    "address": "bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr",
    "type": "witness_v0_scripthash",
    "p2sh-segwit": "2N5EceqhW9HtQxRnL8qRCscz8Udv9P3bvwT"
  }
}

```

8. Send some coins to address (and then generate some blocks to confirm txn)

```
bitcoin-cli -regtest -named sendtoaddress address="bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr" amount="1.0" fee_rate=1.0
3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c

```

Above txn decoded

```
{
  "txid": "3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c",
  "hash": "30e06e1c9b41029638e2d4e8f3e7813193299840f2333d61a4ddf244201723cb",
  "version": 2,
  "size": 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"
      }
    }
  ]
}

```

9. Create a new address to send to and define some variables, then create raw 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

```

10. Create scriptpubky: OP\_HASH160 hash160(redeemScript) OP\_EQUAL
    redeemScript is `0200` + `scripthash` (I'm not too sure about this step...)

```
echo -n 00205ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -rmd160
838403567aa049ada65f944d0a2a320d7b93e7ee
# combine rest of script hex
A914838403567aa049ada65f944d0a2a320d7b93e7ee87

```

11. More variables and attempt to sign raw 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:
Previous output scriptPubKey mismatch:
0 5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb
vs:
OP_HASH160 838403567aa049ada65f944d0a2a320d7b93e7ee OP_EQUAL

```

Based on that error, it seems to be expecting the redeemscript, so I give that a try and get a different 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",
  "complete": false,
  "errors": [
    {
      "txid": "3259e33b721664a5aadae575a6870d1c26750cad789925f7df0282ac1a76f75c",
      "vout": 1,
      "witness": [
      ],
      "scriptSig": "",
      "sequence": 4294967293,
      "error": "Witness program was passed an empty witness"
    }
  ]
}

```

Looking at the decoded hex of this incomplete txn, I see there is no witness data. Am I supposed to be supplying that? I'm probably getting something wrong along the way, but not sure what.

**Tags:** bitcoin-cli, raw-transaction, p2wsh, checklocktimeverify


---

## 1 answers

### Answer by username_1 on 2024-09-05. Score: 0

It looks like you're trying to create and spend from a P2WSH (Pay-to-Witness-Script-Hash) address with a time-lock condition. I'll highlight where there might be issues or confusion, as well as how to resolve them.

### 1. Script Construction

You correctly generated a P2WSH script that uses `OP_CHECKLOCKTIMEVERIFY` (CLTV) to lock the coins until a certain time.

The script you're using is:

```
048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac

```

This translates to:

* `8095fe63`: The locktime (timestamp for 1 March 2023 in little-endian format).
* `b175`: `OP_CHECKLOCKTIMEVERIFY` and `OP_DROP`.
* `76a914ad060529794593cac3e55a6eb26a0c6589d9681588ac`: P2PKH part of the script (`OP_DUP OP_HASH160 <pubkeyhash> OP_EQUALVERIFY OP_CHECKSIG`).

This script seems correct for locking coins with a CLTV condition.

### 2. Generating the Address

You computed the correct witness script hash (P2WSH) for the locking script:

* `5ef75077ffb6e6d533f4245d1ece3c6034ea315db818906dfb49a643e27068fb`.

This P2WSH can be verified by the output of the `decodescript` command and the address you generated:

* `bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr` is the correct P2WSH address.

So, this part of the process looks correct.

### 3. Sending Funds to the Address

You successfully sent 1 BTC to the P2WSH address (`bcrt1qtmm4qallkmnd2vl5y3w3an3uvq6w5v2ahqvfqm0mfxny8cnsdrashv8fsr`), and the transaction seems confirmed.

### 4. Spending from the Time-Locked Address

Here's where the complexity increases, as spending from a CLTV-locked P2WSH requires meeting specific conditions:

1. **Locktime in Transaction:** The `nLocktime` of the transaction must be set to a value **greater than or equal to** the locktime in the script (i.e., `1677628800`).
2. **Sequence Number:** The input's sequence number must be less than `0xFFFFFFFF` (which you correctly set to `4294967293`), indicating that the input is allowed to be spent according to locktime rules.
3. **Witness Stack:** To spend the P2WSH output, the witness stack must include:
   * The signature for the public key.
   * The public key.
   * The locking script (`048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac`).

### Key Areas to Check:

1. **Locktime Mismatch**: The `nLocktime` in your raw transaction is set to `247`:

   ```
   02000000015cf7761aac8202dff7259978ad0c75261c0d87a675e5daaaa56416723be359320100000000fdffffff01c0d3f20500000000160014d91bc7d61347005f8d6828f799f2ad619e217f297e022500

   ```

   However, the locktime in your script is `1677628800`, so you need to set the locktime of the transaction to **at least `1677628800`** (March 1, 2023). Without doing this, the transaction will be invalid because the nLocktime doesn’t meet the script's requirement.
2. **Creating the Correct Witness Data**: For spending from a P2WSH address, you need to provide the correct witness data:

   * The first element is the unlocking signature.
   * The second element is the public key.
   * The third element is the actual script itself (which includes the `OP_CHECKLOCKTIMEVERIFY` part).
3. **Raw Transaction Creation**:

   * When creating the raw transaction, make sure you include the witness program for the script and use the correct `nLocktime` in the transaction.
4. **Redeem Script Confusion**: You mention a redeem script (which is used in P2SH-P2WSH), but you're working directly with a P2WSH. If you were using P2SH-P2WSH, you would need a redeem script to "reveal" the P2WSH. Since you're using native P2WSH, you don't need a redeem script. The witness data directly includes the locking script.

### Suggested Fixes:

1. **Set Correct nLocktime**: When creating the spending transaction, set `nLocktime` to `1677628800` or a later block.

   Example:

   ```bash
   bitcoin-cli -regtest -named createrawtransaction inputs='[{"txid":"'$IN_TXID'", "vout":'$IN_VOUT'}]' outputs='[{"'$OUT_ADDR'":'$OUT_AMNT'}]' locktime=1677628800 replaceable=true

   ```
2. **Create Correct Witness**:
   Ensure the witness field contains the signature, public key, and locking script.
3. **Include Locking Script**: When constructing the witness for the spending transaction, use the full locking script (`048095fe63b17576a914ad060529794593cac3e55a6eb26a0c6589d9681588ac`) as the last element.

### Summary of Process:

1. **Locking Transaction**: You correctly created a P2WSH address that locks coins with `OP_CHECKLOCKTIMEVERIFY`. The transaction that sent coins to this address is also correct.
2. **Spending Transaction**: Ensure the spending transaction’s `nLocktime` matches or exceeds the locktime in the locking script. Construct the witness correctly when spending, including the signature, public key, and the locking script.


---

