<p>In bitcoin-js you could do something like this</p>

<pre><code>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
]);
</code></pre>

<p>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</p>

<p><a href="https://github.com/rubensayshi/counterparty-p2sh-demo/blob/master/atomic-swap.js#L163-L185" rel="nofollow noreferrer">https://github.com/rubensayshi/counterparty-p2sh-demo/blob/master/atomic-swap.js#L163-L185</a></p>

CC BY-SA 3.0
2018-01-10T10:50:22.910Z
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
CC BY-SA 3.0
2017-12-19T06:19:02.130Z
2017-12-19T06:19:02.130Z
MandelDuck
<p>following the link <a href="https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki</a>:</p>

<p>1.) the timeDelay in your example represents two possibilities:</p>

<pre><code>// There are two kinds of nSequence: lock-by-blockheight
// and lock-by-blocktime, distinguished by whether
// nSequenceMasked &lt; CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG.
</code></pre>

<p>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 :-)</p>

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

CC BY-SA 3.0
2017-11-11T21:13:14.753Z
2017-11-11T21:13:14.753Z
pebwindkraft
<p>To answer the question:
<strong>What does the timeDelay value represent? How can I e.g. lock the funds for 2 blocks?</strong></p>

<p>Please see the following example using bitcoinjs:</p>

<p>let bip68 = require('bip68')</p>

<p>bip68.encode({ blocks: 54 })
// => 0x00000036</p>

<p>bip68.encode({ blocks: 200 })
// => 0x000000c8
Also, see the link below:
<em><a href="https://github.com/bitcoinjs/bip68" rel="nofollow noreferrer">https://github.com/bitcoinjs/bip68</a></em></p>

CC BY-SA 4.0
2020-06-02T02:41:41.510Z
2020-06-02T02:41:41.510Z
Ken Huang
<p>I'm working on a tool for timelocked contract generation. My redeem script looks like so:</p>

<pre><code>OP_HASH160 &lt;revocationHash&gt; OP_EQUAL
OP_IF
    &lt;bobPubKey&gt;
OP_ELSE
    &lt;timeDelay&gt; OP_CHECKSEQUENCEVERIFY OP_DROP
    &lt;alicePubKey&gt;
OP_ENDIF
OP_CHECKSIG
</code></pre>

<p>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.</p>

<p>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:</p>

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

<script><contracts><bitcoinjs>
Creating an OP_CSV transaction with bitcoinjs
Colin Atkinson
# Creating an OP_CSV transaction with bitcoinjs

(Asked by: username_0 on 2017-11-10)

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?

**Tags:** script, contracts, bitcoinjs


---

## 3 answers

### Answer by username_1 on 2017-12-19. Score: 3 | Accepted answer

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>

> **Comments:**
> **username_1**: 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, `  

---

### Answer by username_2 on 2017-11-11. Score: 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...


---

### Answer by username_3 on 2020-06-02. Score: 1

To answer the question:
**What does the timeDelay value represent? How can I e.g. lock the funds for 2 blocks?**

Please see the following example using bitcoinjs:

let bip68 = require('bip68')

bip68.encode({ blocks: 54 })
// => 0x00000036

bip68.encode({ blocks: 200 })
// => 0x000000c8
Also, see the link below:
*<https://github.com/bitcoinjs/bip68>*


---

