| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $aa_name = "SAAFE"; |
| 6 | $owner = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 7 | |
| 8 | |
| 9 | $mode = var["mode"] otherwise "open"; |
| 10 | |
| 11 | |
| 12 | |
| 13 | $puk = (trigger.data.puk1 AND trigger.data.puk2)? trigger.data.puk1||trigger.data.puk2 : ""; |
| 14 | $b_is_puk_received = ($puk != ""); |
| 15 | |
| 16 | |
| 17 | $signature = (trigger.data.s1 AND trigger.data.s2)? trigger.data.s1||trigger.data.s2 : ""; |
| 18 | $b_is_signature_received = ($signature != ""); |
| 19 | |
| 20 | $asset = trigger.data.asset otherwise trigger.output[[asset=base]].asset; |
| 21 | $asset_nice_name = ($asset == 'base')? 'bytes' : $asset; |
| 22 | $drawer = $puk||'_'||$asset; |
| 23 | }", |
| 24 | "messages": { |
| 25 | "cases": [ |
| 26 | { |
| 27 | "if": "{trigger.address == $owner}", |
| 28 | "messages": [ |
| 29 | { |
| 30 | "app": "state", |
| 31 | "state": "{ |
| 32 | if (trigger.data.open) var["mode"] = "open"; |
| 33 | if (trigger.data.closed) var["mode"] = "closed"; |
| 34 | if (trigger.data.grant) var ["grant_"||trigger.data.grant] = true; |
| 35 | if (trigger.data.remove) var ["grant_"||trigger.data.remove] = false; |
| 36 | }" |
| 37 | } |
| 38 | ] |
| 39 | }, |
| 40 | { |
| 41 | "if": "{($b_is_puk_received) AND !($b_is_signature_received or trigger.data.ad)}", |
| 42 | "messages": [ |
| 43 | { |
| 44 | "app": "state", |
| 45 | "state": "{ |
| 46 | var["drawer_"||$drawer] += trigger.output[[asset=$asset]]; |
| 47 | response['message'] = var[$drawer]||' '||$asset_nice_name||' are safe under the puk '||$puk; |
| 48 | }" |
| 49 | } |
| 50 | ] |
| 51 | }, |
| 52 | { |
| 53 | "if": "{($b_is_puk_received) AND ($b_is_signature_received or trigger.data.ad) }", |
| 54 | "init": "{ |
| 55 | |
| 56 | if (($mode == "closed") and (!var["grant_"||trigger.address]) and (trigger.address != $owner)) |
| 57 | bounce ("User not allowed to withdraw!"); |
| 58 | |
| 59 | $signature = trigger.data.s1||trigger.data.s2; |
| 60 | if (!var["drawer_"||$drawer]) bounce ("puk doesn\'t exists with asset!"||$asset_nice_name); |
| 61 | if (var["last_s_"||$puk] == $signature) bounce ("Cannot use 2 times the same signature in a row!"); |
| 62 | |
| 63 | if (!is_valid_sig(trigger.data.ad, "-----BEGIN PUBLIC KEY-----"||$puk||"-----END PUBLIC KEY-----", $signature)) bounce ("Wrong signature!"); |
| 64 | |
| 65 | |
| 66 | $old_balance = var["drawer_"||$drawer]; |
| 67 | $amount = trigger.data.am otherwise var["drawer_"||$drawer]; |
| 68 | }", |
| 69 | "messages": [ |
| 70 | { |
| 71 | "app": "payment", |
| 72 | "payload": { |
| 73 | "asset": "{$asset}", |
| 74 | "outputs": [ |
| 75 | { |
| 76 | "address": "{trigger.data.ad}", |
| 77 | "amount": "{$amount}" |
| 78 | } |
| 79 | ] |
| 80 | } |
| 81 | }, |
| 82 | { |
| 83 | "app": "state", |
| 84 | "state": "{ |
| 85 | $new_balance = $old_balance - $amount - 1000; |
| 86 | response['message'] = $amount||' of the '||$old_balance||' '||$asset_nice_name||' withdrawn from '||$puk||' to '|| trigger.data.ad; |
| 87 | if ($new_balance < 2000) |
| 88 | var["drawer_"||$drawer] = false; |
| 89 | else |
| 90 | var["last_s_"||$puk] = $signature; |
| 91 | }" |
| 92 | } |
| 93 | ] |
| 94 | }, |
| 95 | { |
| 96 | "messages": [ |
| 97 | { |
| 98 | "app": "state", |
| 99 | "state": "{ |
| 100 | bounce ("To store, set a 'puk1' and 'puk2' field to <the 2 half of the public key, for which you have the related private key> to safely store funds. To withdraw, set the same 'puk', as well as a 'ad' field to <the Obyte adresse where you want to receive the funds> and a 's1' and 's2' field to <the 2 half signature of a message containing the address 'a'>."); |
| 101 | }" |
| 102 | } |
| 103 | ] |
| 104 | } |
| 105 | ] |
| 106 | } |
| 107 | } |
| 108 | ] |