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