Creating an OP_CSV transaction with bitcoinjs

0

I'm working on a tool for timelocked contract generation. My redeem script looks like so:

OP_HASH160 <revocationHash> OP_EQUAL
OP_IF
    <bobPubKey>
OP_ELSE
    <timeDelay> OP_CHECKSEQUENCEVERIFY OP_DROP
    <alicePubKey>
OP_ENDIF
OP_CHECKSIG

The goal is that Alice can spend the funds after a time delay, while Bob can spend them at any time by proving knowledge of the hash preimage.

Spending through the revocation hash is easy enough. However, I'm having trouble constructing a transaction that spends based on the timelock is proving more difficult. Essentially, my questions are:

  1. What does the <timeDelay> value represent? How can I e.g. lock the funds for 2 blocks?
  2. From what I've read, the sequence number on the spend transaction needs to be set. But what should it be set to?
  3. How do I practically implement this using bitcoinjs-lib or another library?

Colin Atkinson

Posted 2017-11-10T21:35:20.397

Reputation: 101

Answers

3

In bitcoin-js you could do something like this

aliceToBobRedeemScript =  bitcoin.script.compile([,
bitcoin.opcodes.OP_IF,
  bitcoin.opcodes.OP_HASH160,
  aliceSecretHash,
  bitcoin.opcodes.OP_EQUALVERIFY,
  bobPrivKey.getPublicKeyBuffer(),
bitcoin.opcodes.OP_ELSE,
  bitcoin.script.number.encode(10),
  bitcoin.opcodes.OP_NOP3,
  bitcoin.opcodes.OP_DROP, 
  alicePrivKey.getPublicKeyBuffer(),
bitcoin.opcodes.OP_ENDIF,
bitcoin.opcodes.OP_CHECKSIG
]);

there is some sample code on an old atomicswap demo, it doesnt have csv but shows other things such as how to sign custom transactions etc

https://github.com/rubensayshi/counterparty-p2sh-demo/blob/master/atomic-swap.js#L163-L185

MandelDuck

Posted 2017-11-10T21:35:20.397

Reputation: 69

an update bitcoin-js also has a handy bip68 lib on the github https://github.com/bitcoinjs/bip68

so you could do something like this

var sequenceNumber = bip68.encode({ seconds: timeToLock }); bitcoin.script.number.encode(sequenceNumber), bitcoin.opcodes.OP_NOP3, bitcoin.opcodes.OP_DROP,

MandelDuck 2018-01-10T10:50:22.910

1

following the link https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki:

1.) the timeDelay in your example represents two possibilities:

// There are two kinds of nSequence: lock-by-blockheight
// and lock-by-blocktime, distinguished by whether
// nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG.

2.) when I get this correctly, it is BIP68 related. Originally nSequence should allow to modify a tx while being in the mempool, when value is less than 0xFFFFFFFF. With CSV, there is now a relative time after the point the tx enters into the blockchain. Spending the UTXO with CSV requires a certain number of blocks have elapsed. Whereas normal tx would set nSequence to 0xFFFFFFFF, using CSV requires it to be 0xFFFFFFFE or less. Further reading recommended :-)

3.) I have no idea, maybe other experts here know a library that supports CLTV and CSV...

pebwindkraft

Posted 2017-11-10T21:35:20.397

Reputation: 4 568