Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by yyforyongyu for Can someone explain how an HTLC is redeemed?

Check the Script wiki would be helpful. I once did this by hand, here's the process.

First, put the unlocking script on top of the locking script,

<remotehtlcsig> <payment_preimage># To remote node with revocation keyOP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUALOP_IF    OP_CHECKSIGOP_ELSE<remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL    OP_NOTIF        # To local node via HTLC-timeout transaction (timelocked).        OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG    OP_ELSE        # To remote node with preimage.        OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY        OP_CHECKSIG    OP_ENDIFOP_ENDIF

If it helps, put them vertically,

<remotehtlcsig><payment_preimage>OP_DUPOP_HASH160<RIPEMD160(SHA256(revocationpubkey))>OP_EQUALOP_IFOP_CHECKSIGOP_ELSE...

Second, run them by hand. Keep in mind that, the instructions are read from top to bottom(or left to right), the stack grows as follows,

  1. <remotehtlcsig>
  2. <remotehtlcsig> <payment_preimage>
  3. <remotehtlcsig> <payment_preimage> OP_DUP

OP_DUP, from wiki, duplicates the top stack item, so,

  1. <remotehtlcsig> <payment_preimage> <payment_preimage>
  2. <remotehtlcsig> <payment_preimage> <payment_preimage> OP_HASH160

OP_HASH160, from wiki, The input is hashed twice: first with SHA-256 and then with RIPEMD-160, so we hash,

  1. <remotehtlcsig> <payment_preimage> <payment_preimage_hash160>
  2. <remotehtlcsig> <payment_preimage> <payment_preimage_hash160> <RIPEMD160(SHA256(revocationpubkey))>
  3. <remotehtlcsig> <payment_preimage> <payment_preimage_hash160> <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL

OP_EQUAL, from wiki, Returns 1 if the inputs are exactly equal, 0 otherwise. We check that <payment_preimage_hash160> != <RIPEMD160(SHA256(revocationpubkey))> and return 0,

  1. <remotehtlcsig> <payment_preimage> 0
  2. <remotehtlcsig> <payment_preimage> OP_IF OP_CHECKSIG OP_ELSE

OP_IF, OP_ELSE, and OP_NOTIF are flow controls, from wiki, we will jump into the OP_ELSE clause, which is,

...OP_ELSE<remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL    OP_NOTIF        # To local node via HTLC-timeout transaction (timelocked).        OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG    OP_ELSE        # To remote node with preimage.        OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY        OP_CHECKSIG    OP_ENDIFOP_ENDIF

Now we keep working on the stack,

  1. <remotehtlcsig> <payment_preimage> <remote_htlcpubkey>
  2. <remotehtlcsig> <payment_preimage> <remote_htlcpubkey> OP_SWAPOP_SWAP, from wiki, the top two items on the stack are swapped, so we swap <payment_preimage> and <remote_htlcpubkey>,
  3. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage>
  4. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> OP_SIZE

OP_SIZE, from wiki, pushes the string length of the top element of the stack (without popping it).

  1. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> <size of payment_preimage>

Notice that the size of a valid <payment_preimage> is 32,

  1. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> <size of payment_preimage> 32
  2. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> <size of payment_preimage> 32 OP_EQUAL
  3. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> 1

We have a 1 here, so we jump into the OP_ELSE section in the whole OP_NOTIF ... OP_ENDIF, to remind you, the script left is,

...    OP_ELSE        # To remote node with preimage.        OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY        OP_CHECKSIG    OP_ENDIFOP_ENDIF
  1. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> 1 OP_NOTIF ... OP_ELSE

Once jumped into the OP_ELSE, the rest is straightforward, step by step, we have,

  1. <remotehtlcsig> <remote_htlcpubkey> <payment_preimage> OP_HASH160
  2. <remotehtlcsig> <remote_htlcpubkey> <hash160 of payment_preimage>
  3. <remotehtlcsig> <remote_htlcpubkey> <hash160 of payment_preimage> <RIPEMD160(payment_hash)>
  4. <remotehtlcsig> <remote_htlcpubkey> <hash160 of payment_preimage> <RIPEMD160(payment_hash)> OP_EQUALVERIFY
  5. <remotehtlcsig> <remote_htlcpubkey>
  6. <remotehtlcsig> <remote_htlcpubkey> OP_CHECKSIG

The final OP_CHECKSIG will check <remotehtlcsig> and <remote_htlcpubkey>, thus the HTLC will be spent via the unlocking script, <remotehtlcsig> <payment_preimage>.

The orders may not be strictly precise, but the general direction should be correct. Another tool is Bitcoin Script Debugger.And yes, as pointed out by Ugam Kamat, there are three ways to redeem a HTCL.

Happy Lightning!


Viewing all articles
Browse latest Browse all 3

Trending Articles