1

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?

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.