Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples #83

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let b = {
assert!(jet::eq_32(b, 7));
```

Take a look at the [example programs](https://github.com/BlockstreamResearch/simfony/tree/master/example_progs).
Take a look at the [example programs](https://github.com/BlockstreamResearch/simfony/tree/master/examples).

## MSRV

Expand Down
8 changes: 0 additions & 8 deletions example_progs/add.simf

This file was deleted.

18 changes: 0 additions & 18 deletions example_progs/array.simf

This file was deleted.

12 changes: 0 additions & 12 deletions example_progs/checksigfromstackverify.simf

This file was deleted.

10 changes: 0 additions & 10 deletions example_progs/checksigfromstackverify.wit

This file was deleted.

27 changes: 0 additions & 27 deletions example_progs/function.simf

This file was deleted.

22 changes: 0 additions & 22 deletions example_progs/list.simf

This file was deleted.

17 changes: 0 additions & 17 deletions example_progs/match.simf

This file was deleted.

11 changes: 0 additions & 11 deletions example_progs/nesting.simf

This file was deleted.

11 changes: 0 additions & 11 deletions example_progs/option.simf

This file was deleted.

6 changes: 0 additions & 6 deletions example_progs/panic.simf

This file was deleted.

10 changes: 0 additions & 10 deletions example_progs/recursive-covenant.simf

This file was deleted.

18 changes: 0 additions & 18 deletions example_progs/scopes.simf

This file was deleted.

81 changes: 0 additions & 81 deletions example_progs/sighash_all.simf

This file was deleted.

16 changes: 0 additions & 16 deletions example_progs/tuple.simf

This file was deleted.

8 changes: 0 additions & 8 deletions example_progs/unwrap.simf

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions examples/escrow_with_delay.simf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* ESCROW WITH DELAY
*
* An escrow agent can approve the movement of coins in cooperation with the
* sender or the recipient. The escrow agent cannot steal the coins for himself.
* The sender can refund her coins after a timeout.
*
* https://docs.ivylang.org/bitcoin/language/ExampleContracts.html#escrowwithdelay
*/
fn not(bit: bool) -> bool {
<u1>::into(jet::complement_1(<bool>::into(bit)))
}

fn checksig(pk: Pubkey, sig: Signature) {
let msg: u256 = jet::sig_all_hash();
jet::bip_0340_verify((pk, msg), sig);
}

fn checksig_add(counter: u8, pk: Pubkey, maybe_sig: Option<Signature>) -> u8 {
match maybe_sig {
Some(sig: Signature) => {
checksig(pk, sig);
let (carry, new_counter): (bool, u8) = jet::increment_8(counter);
assert!(not(carry));
new_counter
}
None => counter,
}
}

fn check2of3multisig(pks: [Pubkey; 3], maybe_sigs: [Option<Signature>; 3]) {
let [pk1, pk2, pk3]: [Pubkey; 3] = pks;
let [sig1, sig2, sig3]: [Option<Signature>; 3] = maybe_sigs;

let counter1: u8 = checksig_add(0, pk1, sig1);
let counter2: u8 = checksig_add(counter1, pk2, sig2);
let counter3: u8 = checksig_add(counter2, pk3, sig3);

let threshold: u8 = 2;
assert!(jet::eq_8(counter3, threshold));
}

fn transfer_spend(maybe_sigs: [Option<Signature>; 3]) {
let sender_pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G
let recipient_pk: Pubkey = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5; // 2 * G
let escrow_pk: Pubkey = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9; // 3 * G
check2of3multisig([sender_pk, recipient_pk, escrow_pk], maybe_sigs);
}

fn timeout_spend(sender_sig: Signature) {
let sender_pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G
checksig(sender_pk, sender_sig);
let timeout: Distance = 1000;
jet::check_lock_distance(timeout);
}

fn main() {
let transfer_or_timeout: Either<[Option<Signature>; 3], Signature> = witness("transfer_or_timeout");
match transfer_or_timeout {
Left(maybe_sigs: [Option<Signature>; 3]) => transfer_spend(maybe_sigs),
Right(sender_sig: Signature) => timeout_spend(sender_sig),
}
}
6 changes: 6 additions & 0 deletions examples/escrow_with_delay.timeout.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"transfer_or_timeout": {
"value": "Right([0xed, 0xb6, 0x86, 0x50, 0x94, 0x26, 0x0f, 0x85, 0x58, 0x72, 0x82, 0x33, 0xaa, 0xe0, 0x17, 0xdd, 0x09, 0x69, 0xa2, 0xaf, 0xe5, 0xf0, 0x8c, 0x28, 0x2e, 0x1a, 0xb6, 0x59, 0xbf, 0x24, 0x62, 0x68, 0x4c, 0x99, 0xa6, 0x4a, 0x2a, 0x57, 0x24, 0x63, 0x58, 0xa0, 0xd6, 0x32, 0x67, 0x17, 0x78, 0xd0, 0x16, 0xe6, 0xdf, 0x73, 0x81, 0x29, 0x3d, 0xd5, 0xbb, 0x9f, 0x09, 0x99, 0xd3, 0x86, 0x40, 0xd4])",
"type": "Either<[Option<[u8; 64]>; 3], [u8; 64]>"
}
}
File renamed without changes.
Loading
Loading