user64243: 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:

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


user38482: 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... 

user103206: 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

user67101: 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

64243: <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>
 38482: <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>
 103206: <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>
 67101: <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>
