<p>Note: Although this answer is written for testnet, it is absolutely valid for the mainnet as well. The script will be similar, just the address encoding will be different (P2PKH start with 1 vs m for testnet as we use 0x00 as prefix in address base58check in mainnet).</p>

<blockquote>
  <p>I tried to make transaction with Locktime on bitcoin testnet. I am not able to broadcast it.</p>
</blockquote>

<p>When <code>nLocktime</code> is specified, you cannot broadcast the transaction until that <code>nLocktime</code> value is reached. In your case, when writing this answer, the testnet height was at 1567414 while <code>nLocktime</code> is set as 1567415. Other than that your transaction is correct from a script point of view.</p>

<blockquote>
  <p>And how to use CLTV and CSV?</p>
</blockquote>

<p>CLTV and CSV are script level timelocks, unlike <code>nLocktime</code> and <code>nSequence</code> which are transaction and input level timelocks. CLTV or CSV are specified in the output locking script, and when spending those outputs, the user must set the <code>nLocktime</code> or the <code>nSequence</code> value to satisfy the locking condition. </p>

<p><strong>CLTV</strong></p>

<p>Using your example, below will be the script for CLTV in the outputs and the hex of the outputs. The desired block height is 1567415 which is 0x17eab7 in hexadecimal and 0xb7ea17 in little-endian. <code>nLocktime</code> value should be equal or greater to this number in order to <strong>spend the outputs that are locked with CLTV</strong>. So, for this transaction to go through <code>nLocktime</code> value should be equal to or lower than the current block height which is equal to 0x0017eaae (1567406) or 0xaeea1700 in little-endian. However, do note that <strong>when spending this output that is locked with CLTV</strong> as an input in a transaction your <code>nsequence</code> should be between 0xf0000000 to 0xfffffffe. Using 0xffffffff disables timelock.</p>

<pre><code>"vout": [
            {
                "value": 0.01700000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "b7ea17 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 083b56fb8b320a6373d4738bb81496b096e8034c OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "03b7ea17b17576a914083b56fb8b320a6373d4738bb81496b096e8034c88ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "mgGUnb6BhgxzrjWau4X4WD9Lfp9YS4kMy7 "
                    ]
                }
            }
        ]
</code></pre>

<p>Using this output to your unsigned transaction hex, it gives the following script:</p>

<pre><code>Raw transaction before signing:
020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000001976a914a9156a64ea643e7c4db772c96c6a570712e0cc1988acfeffffff01a0f01900000000001f03b7ea17b17576a914083b56fb8b320a6373d4738bb81496b096e8034c88acaeea170001000000
</code></pre>

<p><strong>CSV</strong></p>

<p>CSV is a relative timelock, so the height of the blocks in relative sense is provided. For example, in your case the testnet height was at 1567406 while you wanted the transaction to be spent at height 1567415. So your relative height is 9 blocks. We are using <code>nLocktime</code> for the current transaction to be the current block height which is 0xaeea1700 in little-endian.</p>

<pre><code>"vout": [
            {
                "value": 0.01700000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "09 OP_CHECKSEQUENCEVERIFY OP_DROP OP_DUP OP_HASH160 083b56fb8b320a6373d4738bb81496b096e8034c OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "0109b27576a914083b56fb8b320a6373d4738bb81496b096e8034c88ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "mgGUnb6BhgxzrjWau4X4WD9Lfp9YS4kMy7 "
                    ]
                }
            }
        ]
</code></pre>

<p>Using this output script in the unsigned transaction hex you specified gives us:</p>

<pre><code>Raw transaction before signing:
020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000001976a914a9156a64ea643e7c4db772c96c6a570712e0cc1988acfeffffff01a0f01900000000001d0109b27576a914083b56fb8b320a6373d4738bb81496b096e8034c88acaeea170001000000
</code></pre>

<p>When spending this CSV locked outputs, you will need to keep a few things in mind: (1) <code>nSequence &lt;= 0xEFFFFFFF</code>; (2) <code>nSequence</code>'s 22nd bit determines if the relative lock-time is time-based or block based. This bit should be set to 0 since your CSV is locked in blocks; (3) The value of the relative locktime, is then interpreted from the first 16 bits of the <code>nSequence</code>.</p>

<p>Although I have shown the locking scripts in its raw form (using CLTV/CSV) in <code>scriptPubKey</code>, it is recommended to create a P2SH locking script out of this raw script. This not only ensures that you pay lower fees for making this transaction, but also ensures your privacy (nobody will know that they transaction is timelocked) until you spend that transaction.</p>

CC BY-SA 4.0
2019-07-04T11:51:25.847Z
2019-07-05T10:34:32.543Z
2019-07-05T10:34:32.543Z
Ugam Kamat
<p>I tried to make transaction with <code>nLocktime</code> on bitcoin testnet. I am not able to broadcast it. 
Can someone please help me to make a basic transaction with 1 input and 1 output, so that I can broadcast on the testnet. (p2pkh -> p2pkh)</p>

<p><strong>Usecase:</strong></p>

<pre><code>mvvz9TNZ37fFnUHXZPUJYV3MDutbkb9ipA -&gt; mgGUnb6BhgxzrjWau4X4WD9Lfp9YS4kMy7
Transfering: 0.017 BTC at block height 1567415 (now 1567406)
</code></pre>

<p>Raw transaction (before signing):</p>

<pre><code>020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000001976a914a9156a64ea643e7c4db772c96c6a570712e0cc1988acfeffffff01a0f01900000000001976a914083b56fb8b320a6373d4738bb81496b096e8034c88acb7ea170001000000
</code></pre>

<p>After Signature:</p>

<pre><code>020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000008a4730440220271919b389ed3b635522167d3e2a0201cd85d67941816af4c1638664054c078202203b93ddf929692c2a6d081ef422de9f375183fd595e8ca9e8135af86e47e47c580141043db79f05b69d34dfd3d8658a557ddb6b1a8ebca9070968b1e3833ca6b581d8a30073638dafca0bf6a5a2a08ff89ce91186fefa5a46d2a3349eb47904ab981114feffffff01a0f01900000000001976a914083b56fb8b320a6373d4738bb81496b096e8034c88acb7ea1700
</code></pre>

<p>How to use CLTV and CSV?</p>

<transactions><locktime><sequence><checklocktimeverify><checksequenceverify>
How to make a transaction with locktime on testnet?
Yash Madhwal
# How to make a transaction with locktime on testnet?

(Asked by: username_0 on 2019-07-04)

I tried to make transaction with `nLocktime` on bitcoin testnet. I am not able to broadcast it.
Can someone please help me to make a basic transaction with 1 input and 1 output, so that I can broadcast on the testnet. (p2pkh -> p2pkh)

**Usecase:**

```
mvvz9TNZ37fFnUHXZPUJYV3MDutbkb9ipA -> mgGUnb6BhgxzrjWau4X4WD9Lfp9YS4kMy7
Transfering: 0.017 BTC at block height 1567415 (now 1567406)

```

Raw transaction (before signing):

```
020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000001976a914a9156a64ea643e7c4db772c96c6a570712e0cc1988acfeffffff01a0f01900000000001976a914083b56fb8b320a6373d4738bb81496b096e8034c88acb7ea170001000000

```

After Signature:

```
020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000008a4730440220271919b389ed3b635522167d3e2a0201cd85d67941816af4c1638664054c078202203b93ddf929692c2a6d081ef422de9f375183fd595e8ca9e8135af86e47e47c580141043db79f05b69d34dfd3d8658a557ddb6b1a8ebca9070968b1e3833ca6b581d8a30073638dafca0bf6a5a2a08ff89ce91186fefa5a46d2a3349eb47904ab981114feffffff01a0f01900000000001976a914083b56fb8b320a6373d4738bb81496b096e8034c88acb7ea1700

```

How to use CLTV and CSV?

**Tags:** transactions, locktime, sequence, checklocktimeverify, checksequenceverify


---

## 1 answers

### Answer by username_1 on 2019-07-04. Score: 1 | Accepted answer

Note: Although this answer is written for testnet, it is absolutely valid for the mainnet as well. The script will be similar, just the address encoding will be different (P2PKH start with 1 vs m for testnet as we use 0x00 as prefix in address base58check in mainnet).

> I tried to make transaction with Locktime on bitcoin testnet. I am not able to broadcast it.

When `nLocktime` is specified, you cannot broadcast the transaction until that `nLocktime` value is reached. In your case, when writing this answer, the testnet height was at 1567414 while `nLocktime` is set as 1567415. Other than that your transaction is correct from a script point of view.

> And how to use CLTV and CSV?

CLTV and CSV are script level timelocks, unlike `nLocktime` and `nSequence` which are transaction and input level timelocks. CLTV or CSV are specified in the output locking script, and when spending those outputs, the user must set the `nLocktime` or the `nSequence` value to satisfy the locking condition.

**CLTV**

Using your example, below will be the script for CLTV in the outputs and the hex of the outputs. The desired block height is 1567415 which is 0x17eab7 in hexadecimal and 0xb7ea17 in little-endian. `nLocktime` value should be equal or greater to this number in order to **spend the outputs that are locked with CLTV**. So, for this transaction to go through `nLocktime` value should be equal to or lower than the current block height which is equal to 0x0017eaae (1567406) or 0xaeea1700 in little-endian. However, do note that **when spending this output that is locked with CLTV** as an input in a transaction your `nsequence` should be between 0xf0000000 to 0xfffffffe. Using 0xffffffff disables timelock.

```
"vout": [
            {
                "value": 0.01700000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "b7ea17 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 083b56fb8b320a6373d4738bb81496b096e8034c OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "03b7ea17b17576a914083b56fb8b320a6373d4738bb81496b096e8034c88ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "mgGUnb6BhgxzrjWau4X4WD9Lfp9YS4kMy7 "
                    ]
                }
            }
        ]

```

Using this output to your unsigned transaction hex, it gives the following script:

```
Raw transaction before signing:
020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000001976a914a9156a64ea643e7c4db772c96c6a570712e0cc1988acfeffffff01a0f01900000000001f03b7ea17b17576a914083b56fb8b320a6373d4738bb81496b096e8034c88acaeea170001000000

```

**CSV**

CSV is a relative timelock, so the height of the blocks in relative sense is provided. For example, in your case the testnet height was at 1567406 while you wanted the transaction to be spent at height 1567415. So your relative height is 9 blocks. We are using `nLocktime` for the current transaction to be the current block height which is 0xaeea1700 in little-endian.

```
"vout": [
            {
                "value": 0.01700000,
                "n": 0,
                "scriptPubKey": {
                    "asm": "09 OP_CHECKSEQUENCEVERIFY OP_DROP OP_DUP OP_HASH160 083b56fb8b320a6373d4738bb81496b096e8034c OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "0109b27576a914083b56fb8b320a6373d4738bb81496b096e8034c88ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash",
                    "addresses": [
                        "mgGUnb6BhgxzrjWau4X4WD9Lfp9YS4kMy7 "
                    ]
                }
            }
        ]

```

Using this output script in the unsigned transaction hex you specified gives us:

```
Raw transaction before signing:
020000000140c96a4c43a5db3c57be3fd92f491a5d40ccf00afe349c596bc35fac7bbee59a000000001976a914a9156a64ea643e7c4db772c96c6a570712e0cc1988acfeffffff01a0f01900000000001d0109b27576a914083b56fb8b320a6373d4738bb81496b096e8034c88acaeea170001000000

```

When spending this CSV locked outputs, you will need to keep a few things in mind: (1) `nSequence <= 0xEFFFFFFF`; (2) `nSequence`'s 22nd bit determines if the relative lock-time is time-based or block based. This bit should be set to 0 since your CSV is locked in blocks; (3) The value of the relative locktime, is then interpreted from the first 16 bits of the `nSequence`.

Although I have shown the locking scripts in its raw form (using CLTV/CSV) in `scriptPubKey`, it is recommended to create a P2SH locking script out of this raw script. This not only ensures that you pay lower fees for making this transaction, but also ensures your privacy (nobody will know that they transaction is timelocked) until you spend that transaction.


---

