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.
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
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.
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.
"asm": "b7ea17 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 083b56fb8b320a6373d4738bb81496b096e8034c OP_EQUALVERIFY OP_CHECKSIG",
Using this output to your unsigned transaction hex, it gives the following script:
Raw transaction before signing:
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.
"asm": "09 OP_CHECKSEQUENCEVERIFY OP_DROP OP_DUP OP_HASH160 083b56fb8b320a6373d4738bb81496b096e8034c OP_EQUALVERIFY OP_CHECKSIG",
Using this output script in the unsigned transaction hex you specified gives us:
Raw transaction before signing:
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
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.