bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.65k stars 2.09k forks source link

LockTime test example does not properly set nSequence #1281

Closed educob closed 5 years ago

educob commented 5 years ago

Hello guys.

lockTime = bip65.encode({ utc: 1588583906 }) // 1588583906 => 2020-May (bitcoin halving)
txb.setLockTime(lockTime)

Whataever time in the future I set in lockTime the tx is always accepted by the bitcoin node.

Any idea why? As you can in a previous issue adding, OP_CHECKLOCKTIMEVERIFY to a script doesn't work either for me (in the opposite way, always rejecting the tx). Something is wrong with timing. I am running a regtest in my computer. I mine a block every 2 minutes (or manually as many as I want as once). What can be wrong so all the timing restrictions don't work?

Please help me. Thanks.

educob commented 5 years ago

I made it work by calling

txb.addInput(utxo_tx, utxo_n, bitcoin.Transaction.DEFAULT_SEQUENCE-1) Without specifying the sequence, the input sequence ends up being 0xffffffff and that means that locktime is ignored.

Unless I am doing something wrong, the test examples are wrong cause they don't specify the 3er argument when calling addInput.

junderw commented 5 years ago

https://github.com/bitcoinjs/bitcoinjs-lib/blob/bd0be2f3430e7a5a81b776129befc004b48635da/test/integration/cltv.js#L53

looks like 0xfffffffe to me...

Which test are you talking about?

educob commented 5 years ago

Now that all is clear I'll close the issue.

The library should modify the inputs sequence when setting tx's locktime or at least should give a warning that tx's locktime is not having any effect. After heavy reading and a lot of of hands on, this setting of the input sequence number is clear but a comment here and there in the examples would've been wonderful.

Thanks.