Hashed Timelocked Contract

Hashed Timelock Contract (HTLC) as proposed in the Lightning Network white paper.
The receiver can include a 0 or 1 in the scriptSig to choose to enter through if or else branch.

Use case:
Alice wants to send money to Chuck via the Lightning Network. Bob is a trustless payment hub on the network.
Chuck chooses an arbitrary secret S and sends hash160(S) to Alice.
Alice sends money to the HTLC address.

Bob signs with his key 2 a transaction spending the HTLC address to a timelocked refund address controlled by Alice.
Alice signs with her key 1 a transaction spending the HTLC address to pay Bob.
Bob can collect the payment before the expiry of the timelock T1 if and only if he knows S.

A similar HTCL address is created by Bob and Chuck with a timelock T2 < T1. Bob funds the HTLC address.
A similar set of transactions are signed by Bob and Chuck.
Alice does not broadcast her refund transaction because doing so would prompt Bob to do the same with his refund transaction, closing the payment channel immediately.
Chuck reveals S on the blockchain when he collects his payment. Knowing S, Bob can collect his payment, completing the protocol.
If Chuck fails to collect his payment before T2, Bob can collect his refund. If Bob fails to collect his payment before T1, Alice can collect her refund.

HASH160 <hash160(S)> EQUALVERIFY
2 <AlicePubkey1> <BobPubkey1>
2 <AlicePubkey2> <BobPubkey2>

scriptSig (knowing S): 0 <Alice signature 1> <Bob signature 1> <S> TRUE
scriptSig (ignoring S): 0 <Alice signature 2> <Bob signature 2> FALSE


hash160(S): ee9eb446302cbaaeded21f6a50d7ffb6c240023d

Alice Public Key 1: 02BE332AE534CC30FB84BA64817A748DBC9A9C9021463A645F5B3CF2AB4AEB0284

Bob Public Key 1: 039DD14C371FBB1BCA9860942D14ED32897CF4ABF8312A6446EBF716774769441B

Alice Public Key 2: 02808F45C3B1DEF4C9917D80ABD94D1DA271069C1CE227F8FA6D57E7D5FA836838

Bob Public Key 2: 03C875B4E83368088CB5C9EA1244F16679A091277F7C3BA771E83673DA3A839BD8

Hashed Timelocked Contract Address: 3LZgKZspe411v9vNGFddNMQZqGdzeTvd2Q