Skip to content

Commit c68ba8c

Browse files
committed
change homepage example to an HTLC
1 parent 361e9e5 commit c68ba8c

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

docs/index.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,56 @@ Multi-party spending policies, staged cooperation, and recovery paths are expres
3232

3333
</div>
3434

35-
``` rust title="pay to pubkey"
36-
fn main(pubkey: Pubkey, funds: Funds) -> Funds {
37-
let (witness = _, funds) = commit(
38-
Witness = ValidSignature(pubkey, funds),
39-
funds,
40-
);
41-
funds
35+
``` rust title="Hash Time-Locked Contract"
36+
/*
37+
* The recipient can spend the coins by providing the secret preimage of a hash.
38+
* The sender can cancel the transfer after a fixed block height.
39+
*
40+
* HTLCs enable two-way payment channels and multi-hop payments,
41+
* such as on the Lightning network.
42+
*/
43+
fn sha2(string: u256) -> u256 {
44+
let hasher: Ctx8 = jet::sha_256_ctx_8_init();
45+
let hasher: Ctx8 = jet::sha_256_ctx_8_add_32(hasher, string);
46+
jet::sha_256_ctx_8_finalize(hasher)
47+
}
48+
49+
fn checksig(pk: Pubkey, sig: Signature) {
50+
let msg: u256 = jet::sig_all_hash();
51+
jet::bip_0340_verify((pk, msg), sig);
52+
}
53+
54+
fn complete_spend(preimage: u256, recipient_sig: Signature) {
55+
let hash: u256 = sha2(preimage);
56+
let expected_hash: u256 = 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925;// (2)!
57+
assert!(jet::eq_256(hash, expected_hash));
58+
let recipient_pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798;// (3)!
59+
checksig(recipient_pk, recipient_sig);
60+
}
61+
62+
fn cancel_spend(sender_sig: Signature) {
63+
let timeout: Height = 1000;
64+
jet::check_lock_height(timeout);
65+
let sender_pk: Pubkey = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5;// (4)!
66+
checksig(sender_pk, sender_sig)
67+
}
68+
69+
fn main() {
70+
match witness::COMPLETE_OR_CANCEL {
71+
Left(preimage_sig: (u256, Signature)) => {
72+
let (preimage, recipient_sig): (u256, Signature) = preimage_sig;
73+
complete_spend(preimage, recipient_sig);
74+
},
75+
Right(sender_sig: Signature) => cancel_spend(sender_sig),
76+
}
4277
}// (1)!
4378
```
4479

4580
1. This compiles to Simplicity ready for on-chain execution. More involved scripts can execute [reverse Dutch auctions](https://delvingbitcoin.org/t/writing-simplicity-programs-with-simplicityhl/1900).
4681

82+
2. `sha2([0x00; 32])`
83+
84+
3. `1 * G`
85+
86+
4. `2 * G`
87+

0 commit comments

Comments
 (0)