<p>The HTLC isn't really a specific type of bitcoin transaction, it is more a specific output script with the required spend conditions. Thus to make an HTLC, you really need to create a custom script which the bitcoin core command line tools don't really accomplish. You could construct the script by hand or perhaps find another tool which allows more easy script editing, perhaps <a href="https://github.com/kallewoof/btcdeb" rel="nofollow noreferrer">btcdeb</a> by kallewoof.</p>

<p>A HTLC script format can be found in <a href="https://github.com/bitcoin/bips/blob/master/bip-0199.mediawiki" rel="nofollow noreferrer">BIP 199</a>. There was also a PR opened to bitcoin core to add HTLC support to the wallet, but the author became too busy to continue maintaining that PR and it was never merged. It may interest you, you can find it <a href="https://github.com/bitcoin/bitcoin/pull/7601" rel="nofollow noreferrer">here</a>.</p>

<p>Let's show an example. Take your value "secret", and hash it with sha256 (you could use hash160 instead also). Then create the following script:</p>

<pre><code>OP_IF
    OP_SHA256 hash_of_secret OP_EQUALVERIFY OP_DUP OP_HASH160 your_pubkey            
OP_ELSE
    timeout OP_CSV OP_DROP OP_DUP OP_HASH160 their_pubkey
OP_ENDIF
OP_EQUALVERIFY
OP_CHECKSIG
</code></pre>

<p>To spend, the first value on the stack is true or false to pick which branch of the if statement to evaluate. If you are the seller who has the preimage of the secret hash, you use the top branch and reveal the secret and sign the transaction to spend the output instantly.
Otherwise, if the deal falls through or whatever, they can redeem their funds again without revealing the secret after a certain amount of time. If the CSV with the timeout (you could use CLT instead) is successful, meaning the time has passed, they redeem it with just their signature on the transaction, and don't require the preimage of the hash.</p>

<p>It is relatively trivial to convert this script to an actual hex encoded script, simply by looking up the correct hex representation of each op_code and using the correct push opcodes for the length of each piece of data you are pushing (pubkeys, secret hash, etc)</p>

CC BY-SA 4.0
2018-05-07T10:23:13.327Z
I guess that is what I am after, how to create  a HTLC script by hand.
oshirowanen
CC BY-SA 4.0
2018-05-07T19:43:34.170Z
You don't have to create it; it already exists and is defined in BIP 199.
Pieter Wuille
CC BY-SA 4.0
2018-05-08T09:04:06.377Z
@MeshCollider,  Regarding your last paragraph.  I have been trying to convert that script to an actual bex encoded script by looking up the correct hex representations of each op_code, but so far I have been unsuccessful.  Could you please give me an example (testnet) hex so I can work backwards from it?
oshirowanen
CC BY-SA 4.0
2018-05-08T13:20:06.910Z
@oshirowanen what do you mean unsuccessful, what issue have you encountered?
meshcollider
CC BY-SA 4.0
2018-05-07T09:30:34.477Z
2018-05-08T02:42:10.053Z
2018-05-08T02:42:10.053Z
meshcollider
<p>I haven’t played the whole HTLC thing through, but was starting easy: try to create a redeem script, and the condition shall be, that a hash is checked with „equal verify“. Only play on testnet/regtest! My example below is on unixoide systems, at the command line with bitcoin in regtest mode. I have done this with "bitcoin_cli -version": v0.15.1
Attention, there has been changes in v0.16.</p>

<p>To begin, for the ScriptSig I simply ignore signatures, and just put the secret on the stack, followed by an OP_TRUE to fulfill the IF STATEMENT later on.</p>

<p>For the RedeemScript the following logic is used:</p>

<pre><code>#  OP_IF
#   a9 14 &lt;hash_lock&gt; 87
#  OP_ELSE
#   76 a9 14 &lt;pubkey_hash&gt; 88 ac
#  OP_ENDIF
</code></pre>

<p>Which ends up in a pseudo serialized way a bit like this:</p>

<pre><code># 63 a914&lt;hash_lock&gt;88 67 76a914&lt;pubkey_hash&gt;88ac 68
</code></pre>

<p>And in my case into this redeem script: <code>63a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68</code></p>

<p>The secret shall be:</p>

<pre><code>#   426974636f696e5f72756c657321 
#     = ascii hex(Bitcoin_rules!)
#   SHA256(4269...7321) = bc6cee57412cfe8c26cbef4676d3af3182e2897ba482a8ce05ee407aebe76699
#   RIPEMD160(SHA256(4269...7321) = 8103b0df9ad75e2b774f43d6e7e71eeaa2c73efb
</code></pre>

<p>And here are all the variables I used to have:</p>

<pre><code>b_cli="bitcoin-cli -regtest"
RedeemScriptHash=361801286b4be137f6a468ff8a34cfceb071763c
RedeemScriptAddress=2MxBFEWKRPBy96BCxmuZuXkz5CfivDg8e1a
Spend_TX_Value=9.99995000
Src_Address=mqE7Xm8vXYMx2T7qkCk5ggYuxwo8PHGT7k
Src_PrivKey=cT2KDnyWU7UoFdwVBgv682HAAPAVzpiHZ9YY54YRgFvUmXFr6L4n
Src_PubKey=02dae48354460f84dde299dbf35504056ae6cc500e2ef32b805cda1fdb1924a420
Src_PubKeyScript=76a9146a81e587585e58b07dce293a089894a0f8a61b8488ac
Tgt_Address=mtad6HurkyVUtZUWvWHuF2DQ6nQ7bodz19
Tgt_PubKeyScript=76a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac
UTXO_ScriptPK=a914361801286b4be137f6a468ff8a34cfceb071763c87
UTXO_TXID=6a8764163bc493073c3f05f9ad37cb9fdfb8136d19263c8e4ae38d8e00cdf16b
UTXO_VOUT=0
</code></pre>

<p>I then created a funding transaction of 10 BTC to the redeem script address with this command, and generate a block: </p>

<pre><code>$b_cli sendtoaddress $RedeemScriptAddress 10
$b_cli generate 1
</code></pre>

<h3>CREATING TX</h3>

<pre><code>$b_cli createrawtransaction
'''[{\"txid\":\"'$UTXO_TXID'\",\"vout\":'$UTXO_VOUT'}]'''
'''{\"'$Tgt_Address'\":'$Spend_TX_Value'}''' 
</code></pre>

<p>02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a0000000000ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000</p>

<p>(gosh! these escape sequences drove me mad in the shell script ...)</p>

<h3>SIGNING TX</h3>

<pre><code>$b_cli signrawtransaction $RAW_TX '''[{"txid": "'$UTXO_TXID'","vout": '$UTXO_VOUT',"scriptPubKey": "'$UTXO_ScriptPK'","redeemScript": "'$RedeemScript'"}]''' '''["'$Src_PrivKey'"]'''

{
  "hex": "02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a00000000343363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000",
  "complete": false,
  "errors": [
    {
      "txid": "6a8764163bc493073c3f05f9ad37cb9fdfb8136d19263c8e4ae38d8e00cdf16b",
      "vout": 0,
      "witness": [
      ],
      "scriptSig": "3363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68",
      "sequence": 4294967295,
      "error": "Invalid OP_IF construction"
    }
  ]
}
BITCOIND Signed_TX:
</code></pre>

<p>02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a00000000343363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000</p>

<p>Note that bitcoin_cli returned an error with an <strong>invalid OP_IF statement</strong> </p>

<p>The returned BITCOIND Signed_TX can be decomposed this way:</p>

<pre><code>Version:       02000000
V_INs:         01
TX_ID:         6bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a
TX_Outpoint:   00000000
Len subscript: 34 
Len script:    33
  OP_IF:       63
               a914 8103b0df9ad75e2b774f43d6e7e71eeaa2c73efb87
  OP_ELSE:     67
               76a914 6a81e587585e58b07dce293a089894a0f8a61b84 88ac
  OP_ENDIF:    68
Sequence:      ffffffff
TX_OUT …:      0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac
LockTime:      00000000
</code></pre>

<p>Now fiddling with the length values, and bringing in the secret, I end up with:</p>

<h3>PREPARING SigScript</h3>

<blockquote>
  <p>02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a00000000440E426974636f696e5f72756c657321513363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000</p>
</blockquote>

<pre><code>Version:       02000000
V_INs:         01
TX_ID:         6bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a
TX_Outpoint:   00000000
Len subscript: 44
The secret:    0E426974636f696e5f72756c657321     = ascii hex(Bitcoin_rules!)
  OP_TRUE:     51 
Len script:    33
  OP_IF:       63
               a914 8103b0df9ad75e2b774f43d6e7e71eeaa2c73efb87
  OP_ELSE:     67
               76a914 6a81e587585e58b07dce293a089894a0f8a61b84 88ac
  OP_ENDIF:    68
Sequence:      ffffffff
TX_OUT …:      0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac
LockTime:      00000000
</code></pre>

<h3>SENDING TX</h3>

<pre><code>$b_cli sendrawtransaction $to_send_TX
</code></pre>

<p>afa97d910badda64e8e2498d822387f8aa75c1efad7eb78caa06e67a0103bb0f</p>

<pre><code>bitcoin-cli -regtest getrawmempool
</code></pre>

<p>[
  "afa97d910badda64e8e2498d822387f8aa75c1efad7eb78caa06e67a0103bb0f"
]</p>

<pre><code>bitcoin-cli -regtest generate 1
</code></pre>

<p>[
  "5a5ecf0f67074eff7ce10d633fee99256f1094657354ab5e832d5412157782b0"
]</p>

<p>And then verify results with:</p>

<pre><code>$b_cli listunspent | grep $RedeemScriptAddress
$b_cli getreceivedbyaddress $Tgt_Address
</code></pre>

<p>Hint: 
The biggest „issue“ is to create the final tx before sending it. There you need to play with the length of the parts of the scriptsigs. Therefor I created some universally usable procedures to convert between hex and decimal in my .profile:</p>

<pre><code>d2h() 
{ 
  echo "obase=16;$1" | bc
}

h2d() 
{ 
  echo "ibase=16;$1" | bc
}

len() 
{ 
  printf "$1" | wc -c
}

lenx() 
{ 
  my_len=$( len $1)
  d2h "$my_len / 2"
}
</code></pre>

<p>I hope this helps to see, how a manually constructed smart contract can be realized at the command line step by step. I used two important references, especially the answer sections:</p>

<p><a href="https://bitcoin.stackexchange.com/questions/3374">How to redeem a basic Tx?</a></p>

<p><a href="https://bitcoin.stackexchange.com/questions/32305">How does the ECDSA verification algorithm work during transaction?</a></p>

CC BY-SA 4.0
2018-05-09T18:24:50.607Z
2018-05-09T18:37:09.783Z
2018-05-09T18:37:09.783Z
pebwindkraft
<p>Is it possible to manually create a HTLC using the bitcoin-qt command line?</p>

<p>I know how to manually creating createrawtransaction/sendrawtransaction using the bitcoin-qt clients command line, and wanted to know how to manually create a HTLC.</p>

<p>When  I say "manually", I mean, really getting your hands dirty, like using notepad to construct the contract, putting the values into bitcoin-cli, to convert it into a hash, ready for it to be sent using sendrawtransaction.</p>

<bitcoin-core><raw-transaction><atomic-swap><hash-time-locked-contract>
HTLC (Hash Time Lock Contract) using bitcoin-qt
oshirowanen
# HTLC (Hash Time Lock Contract) using bitcoin-qt

(Asked by: username_0 on 2018-05-04)

Is it possible to manually create a HTLC using the bitcoin-qt command line?

I know how to manually creating createrawtransaction/sendrawtransaction using the bitcoin-qt clients command line, and wanted to know how to manually create a HTLC.

When I say "manually", I mean, really getting your hands dirty, like using notepad to construct the contract, putting the values into bitcoin-cli, to convert it into a hash, ready for it to be sent using sendrawtransaction.

**Tags:** bitcoin-core, raw-transaction, atomic-swap, hash-time-locked-contract


---

## 2 answers

### Answer by username_3 on 2018-05-09. Score: 4 | Accepted answer

I haven’t played the whole HTLC thing through, but was starting easy: try to create a redeem script, and the condition shall be, that a hash is checked with „equal verify“. Only play on testnet/regtest! My example below is on unixoide systems, at the command line with bitcoin in regtest mode. I have done this with "bitcoin_cli -version": v0.15.1
Attention, there has been changes in v0.16.
To begin, for the ScriptSig I simply ignore signatures, and just put the secret on the stack, followed by an OP_TRUE to fulfill the IF STATEMENT later on.
For the RedeemScript the following logic is used:
#  OP_IF
#   a9 14  87
#  OP_ELSE
#   76 a9 14  88 ac
#  OP_ENDIF

Which ends up in a pseudo serialized way a bit like this:
# 63 a91488 67 76a91488ac 68

And in my case into this redeem script: 63a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68
The secret shall be:
#   426974636f696e5f72756c657321 
#     = ascii hex(Bitcoin_rules!)
#   SHA256(4269...7321) = bc6cee57412cfe8c26cbef4676d3af3182e2897ba482a8ce05ee407aebe76699
#   RIPEMD160(SHA256(4269...7321) = 8103b0df9ad75e2b774f43d6e7e71eeaa2c73efb

And here are all the variables I used to have:
b_cli="bitcoin-cli -regtest"
RedeemScriptHash=361801286b4be137f6a468ff8a34cfceb071763c
RedeemScriptAddress=2MxBFEWKRPBy96BCxmuZuXkz5CfivDg8e1a
Spend_TX_Value=9.99995000
Src_Address=mqE7Xm8vXYMx2T7qkCk5ggYuxwo8PHGT7k
Src_PrivKey=cT2KDnyWU7UoFdwVBgv682HAAPAVzpiHZ9YY54YRgFvUmXFr6L4n
Src_PubKey=02dae48354460f84dde299dbf35504056ae6cc500e2ef32b805cda1fdb1924a420
Src_PubKeyScript=76a9146a81e587585e58b07dce293a089894a0f8a61b8488ac
Tgt_Address=mtad6HurkyVUtZUWvWHuF2DQ6nQ7bodz19
Tgt_PubKeyScript=76a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac
UTXO_ScriptPK=a914361801286b4be137f6a468ff8a34cfceb071763c87
UTXO_TXID=6a8764163bc493073c3f05f9ad37cb9fdfb8136d19263c8e4ae38d8e00cdf16b
UTXO_VOUT=0

I then created a funding transaction of 10 BTC to the redeem script address with this command, and generate a block: 
$b_cli sendtoaddress $RedeemScriptAddress 10
$b_cli generate 1

CREATING TX
$b_cli createrawtransaction
'''[{\"txid\":\"'$UTXO_TXID'\",\"vout\":'$UTXO_VOUT'}]'''
'''{\"'$Tgt_Address'\":'$Spend_TX_Value'}''' 

02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a0000000000ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000
(gosh! these escape sequences drove me mad in the shell script ...)
SIGNING TX
$b_cli signrawtransaction $RAW_TX '''[{"txid": "'$UTXO_TXID'","vout": '$UTXO_VOUT',"scriptPubKey": "'$UTXO_ScriptPK'","redeemScript": "'$RedeemScript'"}]''' '''["'$Src_PrivKey'"]'''

{
  "hex": "02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a00000000343363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000",
  "complete": false,
  "errors": [
    {
      "txid": "6a8764163bc493073c3f05f9ad37cb9fdfb8136d19263c8e4ae38d8e00cdf16b",
      "vout": 0,
      "witness": [
      ],
      "scriptSig": "3363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68",
      "sequence": 4294967295,
      "error": "Invalid OP_IF construction"
    }
  ]
}
BITCOIND Signed_TX:

02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a00000000343363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000
Note that bitcoin_cli returned an error with an invalid OP_IF statement 
The returned BITCOIND Signed_TX can be decomposed this way:
Version:       02000000
V_INs:         01
TX_ID:         6bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a
TX_Outpoint:   00000000
Len subscript: 34 
Len script:    33
  OP_IF:       63
               a914 8103b0df9ad75e2b774f43d6e7e71eeaa2c73efb87
  OP_ELSE:     67
               76a914 6a81e587585e58b07dce293a089894a0f8a61b84 88ac
  OP_ENDIF:    68
Sequence:      ffffffff
TX_OUT …:      0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac
LockTime:      00000000

Now fiddling with the length values, and bringing in the secret, I end up with:
PREPARING SigScript

02000000016bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a00000000440E426974636f696e5f72756c657321513363a9148103b0df9ad75e2b774f43d6e7e71eeaa2c73efb876776a9146a81e587585e58b07dce293a089894a0f8a61b8488ac68ffffffff0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac00000000

Version:       02000000
V_INs:         01
TX_ID:         6bf1cd008e8de34a8e3c26196d13b8df9fcb37adf9053f3c0793c43b1664876a
TX_Outpoint:   00000000
Len subscript: 44
The secret:    0E426974636f696e5f72756c657321     = ascii hex(Bitcoin_rules!)
  OP_TRUE:     51 
Len script:    33
  OP_IF:       63
               a914 8103b0df9ad75e2b774f43d6e7e71eeaa2c73efb87
  OP_ELSE:     67
               76a914 6a81e587585e58b07dce293a089894a0f8a61b84 88ac
  OP_ENDIF:    68
Sequence:      ffffffff
TX_OUT …:      0178b69a3b000000001976a9148f4b44f4975751d7cf6a797e0818c353afbd3bb388ac
LockTime:      00000000

SENDING TX
$b_cli sendrawtransaction $to_send_TX

afa97d910badda64e8e2498d822387f8aa75c1efad7eb78caa06e67a0103bb0f
bitcoin-cli -regtest getrawmempool

[
  "afa97d910badda64e8e2498d822387f8aa75c1efad7eb78caa06e67a0103bb0f"
]
bitcoin-cli -regtest generate 1

[
  "5a5ecf0f67074eff7ce10d633fee99256f1094657354ab5e832d5412157782b0"
]
And then verify results with:
$b_cli listunspent | grep $RedeemScriptAddress
$b_cli getreceivedbyaddress $Tgt_Address

Hint: 
The biggest „issue“ is to create the final tx before sending it. There you need to play with the length of the parts of the scriptsigs. Therefor I created some universally usable procedures to convert between hex and decimal in my .profile:
d2h() 
{ 
  echo "obase=16;$1" | bc
}

h2d() 
{ 
  echo "ibase=16;$1" | bc
}

len() 
{ 
  printf "$1" | wc -c
}

lenx() 
{ 
  my_len=$( len $1)
  d2h "$my_len / 2"
}

I hope this helps to see, how a manually constructed smart contract can be realized at the command line step by step. I used two important references, especially the answer sections:
How to redeem a basic Tx?
How does the ECDSA verification algorithm work during transaction?


---

### Answer by username_1 on 2018-05-07. Score: 6

The HTLC isn't really a specific type of bitcoin transaction, it is more a specific output script with the required spend conditions. Thus to make an HTLC, you really need to create a custom script which the bitcoin core command line tools don't really accomplish. You could construct the script by hand or perhaps find another tool which allows more easy script editing, perhaps btcdeb by kallewoof.
A HTLC script format can be found in BIP 199. There was also a PR opened to bitcoin core to add HTLC support to the wallet, but the author became too busy to continue maintaining that PR and it was never merged. It may interest you, you can find it here.
Let's show an example. Take your value "secret", and hash it with sha256 (you could use hash160 instead also). Then create the following script:
OP_IF
    OP_SHA256 hash_of_secret OP_EQUALVERIFY OP_DUP OP_HASH160 your_pubkey            
OP_ELSE
    timeout OP_CSV OP_DROP OP_DUP OP_HASH160 their_pubkey
OP_ENDIF
OP_EQUALVERIFY
OP_CHECKSIG

To spend, the first value on the stack is true or false to pick which branch of the if statement to evaluate. If you are the seller who has the preimage of the secret hash, you use the top branch and reveal the secret and sign the transaction to spend the output instantly.
Otherwise, if the deal falls through or whatever, they can redeem their funds again without revealing the secret after a certain amount of time. If the CSV with the timeout (you could use CLT instead) is successful, meaning the time has passed, they redeem it with just their signature on the transaction, and don't require the preimage of the hash.
It is relatively trivial to convert this script to an actual hex encoded script, simply by looking up the correct hex representation of each op_code and using the correct push opcodes for the length of each piece of data you are pushing (pubkeys, secret hash, etc)

> **Comments:**
> **username_0**: I guess that is what I am after, how to create  a HTLC script by hand.  
> **username_2**: You don't have to create it; it already exists and is defined in BIP 199.  
> **username_0**: @MeshCollider,  Regarding your last paragraph.  I have been trying to convert that script to an actual bex encoded script by looking up the correct hex representations of each op_code, but so far I have been unsuccessful.  Could you please give me an example (testnet) hex so I can work backwards from it?  
> **username_1**: @username_0 what do you mean unsuccessful, what issue have you encountered?  

---

