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,
<remotehtlcsig>
<remotehtlcsig> <payment_preimage>
<remotehtlcsig> <payment_preimage> OP_DUP
OP_DUP
, from wiki, duplicates the top stack item, so,
<remotehtlcsig> <payment_preimage> <payment_preimage>
<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,
<remotehtlcsig> <payment_preimage> <payment_preimage_hash160>
<remotehtlcsig> <payment_preimage> <payment_preimage_hash160> <RIPEMD160(SHA256(revocationpubkey))>
<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,
<remotehtlcsig> <payment_preimage> 0
<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,
<remotehtlcsig> <payment_preimage> <remote_htlcpubkey>
<remotehtlcsig> <payment_preimage> <remote_htlcpubkey> OP_SWAP
OP_SWAP
, from wiki, the top two items on the stack are swapped, so we swap<payment_preimage>
and<remote_htlcpubkey>
,<remotehtlcsig> <remote_htlcpubkey> <payment_preimage>
<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).
<remotehtlcsig> <remote_htlcpubkey> <payment_preimage> <size of payment_preimage>
Notice that the size of a valid <payment_preimage>
is 32,
<remotehtlcsig> <remote_htlcpubkey> <payment_preimage> <size of payment_preimage> 32
<remotehtlcsig> <remote_htlcpubkey> <payment_preimage> <size of payment_preimage> 32 OP_EQUAL
<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
<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,
<remotehtlcsig> <remote_htlcpubkey> <payment_preimage> OP_HASH160
<remotehtlcsig> <remote_htlcpubkey> <hash160 of payment_preimage>
<remotehtlcsig> <remote_htlcpubkey> <hash160 of payment_preimage> <RIPEMD160(payment_hash)>
<remotehtlcsig> <remote_htlcpubkey> <hash160 of payment_preimage> <RIPEMD160(payment_hash)> OP_EQUALVERIFY
<remotehtlcsig> <remote_htlcpubkey>
<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!