3
3
How does relative timelock work? I mean, I know the theoretical description, but how to use nSequence concretely ? Can you do me a practical example with 2 sample transactions one of which locked with nSequence from another?
3
3
How does relative timelock work? I mean, I know the theoretical description, but how to use nSequence concretely ? Can you do me a practical example with 2 sample transactions one of which locked with nSequence from another?
5
In order to use a relative time lock, you need to provide the requirements in the scriptPubKey to which the Bitcoin is sent.
scriptPubKey for escrow with 30 day timeout:
IF
2 <Alice's pubkey> <Bob's pubkey> <Escrow's pubkey> 3 CHECKMULTISIG
ELSE
"30d" CHECKSEQUENCEVERIFY DROP
<Alice's pubkey> CHECKSIG
ENDIF
Then, in order to spend it before 30 days, the scriptSig that satisfies the first conditional statement (multisig script) must be provided, i.e.:
scriptSig: 0 <signature1> <signature2>
Or after 30 days, alice can provide:
scriptSig: <signature>
See BIP112
Note that in order to set a relative locktime: the tx must have the following properties:
0x400000) if it is a lock-time type, unset for block height typeFor 30 days, I believe it would be as follows:
30 * 24 * 60 * 60 = 2592000 seconds
2592000 / 512 = 5062.5 ~= 5063 or 0x13C7
sequence = 0x13C7 | 0x400000 = 0x4013C7 or 4199367
nSequence = 0xC7134000 (little endian)
The sequence is the very last 4 bytes of the transaction, see https://en.bitcoin.it/wiki/Transaction
Thank you, very clear. Do u know where to add nSequence parameter inside a transaction? – Bruce Wayne – 2019-01-15T08:50:52.063
1
The sequence or locktime is the very last 4 bytes of the transaction, see https://en.bitcoin.it/wiki/Transaction
– JBaczuk – 2019-01-15T12:54:24.833@JBaczuk - Has the impact of little endian encoding been taken into consideration after performing BIP 68 bit encoding? – skaht – 2019-01-15T19:24:38.713
@skaht right, you'd need to convert to little endian before putting it in the transaction – JBaczuk – 2019-01-15T21:19:34.627
1The
nSequencefield is not the same as thenLockTimefield. ThenSequencefield is per-input, and inputs can have different values set. See General format (inside a block) of each input of a transaction - Txin in the bitcoin wiki link. – arubi – 2019-01-16T08:54:47.127Right, thanks I was mixing the two terms, I'll try to clarify – JBaczuk – 2019-01-16T13:03:26.013
nSequence is at the TX input level. Was referring to each TX output CSV opcode locktime needing to be BIP 68 encoded and converted to little endian format. nLockTime is associated with absolute time locks. – skaht – 2019-01-17T03:57:41.617
TX version number must be set to 2 or greater per https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki#summary.
– skaht – 2019-01-17T15:53:59.780