| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | |
| 9 | $INSTRUCTIONS = "You need a public/private key pair. You can use the same key for different assets. The public key should always be provided as many part as needed ('public_key_part1=<part>',...). To store asset, you have to send the funds with the clear message 'store' ('message = store') and the corresponding signature in many parts as needed 'signature_part1=<part1>, ...). To withdraw, provide 'message = <withdraw address>', the associated signature and the nature of the asset if not bytes ('asset=<asset id>')."; |
| 10 | |
| 11 | |
| 12 | $public_key = trigger.data.public_key1 otherwise bounce ("Need public key parts!") || trigger.data.public_key2 otherwise "" || trigger.data.public_key3 otherwise "" || trigger.data.public_key4 otherwise ""; |
| 13 | |
| 14 | |
| 15 | $message = trigger.data.message otherwise bounce ("Need the 'message=store or withdraw address'!"); |
| 16 | |
| 17 | |
| 18 | $signature = trigger.data.signature_part1 otherwise bounce ("Need signature parts!") || trigger.data.signature_part2 otherwise "" || trigger.data.signature_part3 otherwise "" ||trigger.data.signature_part4 otherwise ""; |
| 19 | |
| 20 | |
| 21 | $received_none_base_asset = trigger.output[[asset!=base]].asset; |
| 22 | $received_asset = ($received_none_base_asset != "none")? $received_none_base_asset : "base"; |
| 23 | $asset = trigger.data.asset otherwise $received_asset; |
| 24 | $received_amount = trigger.output[[asset=$asset]].amount; |
| 25 | |
| 26 | |
| 27 | $public_key_hash = sha256($public_key); |
| 28 | $signature_hash = sha256($signature); |
| 29 | $drawer_key = "drawer_"||sha256($public_key_hash||$asset); |
| 30 | $drawer_owner_key = $drawer_key||"_owner"; |
| 31 | $drawer_amount_key = $drawer_key||"_amount"; |
| 32 | $drawer_asset_key = $drawer_key||"_asset"; |
| 33 | $last_signature_key = "last_signature_hash_"||$public_key_hash; |
| 34 | }", |
| 35 | "messages": { |
| 36 | "cases": [ |
| 37 | { |
| 38 | "if": "{ $message == "store" }", |
| 39 | "init": "{ |
| 40 | if (!is_valid_sig("store", $public_key, $signature)) |
| 41 | bounce ("Wrong signature, the fund cannot be safely store because the signature entered is note recognized as a sign message containing the word 'store' please check the documation!"); |
| 42 | }", |
| 43 | "messages": [ |
| 44 | { |
| 45 | "app": "state", |
| 46 | "state": "{ |
| 47 | if (!!trigger.data.private) |
| 48 | if (!var[$drawer_asset_key]) |
| 49 | var[$drawer_owner_key] = trigger.address; |
| 50 | var[$drawer_amount_key] += $received_amount; |
| 51 | var[$drawer_asset_key] = $asset; |
| 52 | response['message'] = "Safely stored ^^"; |
| 53 | }" |
| 54 | } |
| 55 | ] |
| 56 | }, |
| 57 | { |
| 58 | "if": "{ !!$message }", |
| 59 | "init": "{ |
| 60 | |
| 61 | if (!is_valid_address($message)) |
| 62 | bounce ("Invalid address or keyword ('store') in 'message'!"); |
| 63 | |
| 64 | |
| 65 | if (!var[$drawer_asset_key]) |
| 66 | bounce ("public_key doesn\'t exists with this asset!"); |
| 67 | |
| 68 | |
| 69 | if (!!var[$drawer_owner_key]) |
| 70 | if (var[$drawer_owner_key] != trigger.address) |
| 71 | bounce ("You are not the owner of this private drawer!"); |
| 72 | |
| 73 | |
| 74 | if (!!var[$last_signature_key]) |
| 75 | if (var[$last_signature_key] == $signature_hash) |
| 76 | bounce ("Cannot use 2 times the same signature in a row!"); |
| 77 | |
| 78 | |
| 79 | $withdraw_address = $message; |
| 80 | if (!is_valid_sig($withdraw_address, $public_key, $signature)) |
| 81 | bounce ("The 'withdraw address' signed is not the same as the one that was asked to use ! or this signature was not created with private key linked to "||$public_key||" !"); |
| 82 | |
| 83 | |
| 84 | $balance_before_withdraw = var[$drawer_amount_key]; |
| 85 | $withdraw_amount = trigger.data.withdraw_amount otherwise $balance_before_withdraw; |
| 86 | }", |
| 87 | "messages": [ |
| 88 | { |
| 89 | "app": "payment", |
| 90 | "payload": { |
| 91 | "asset": "{$asset}", |
| 92 | "outputs": [ |
| 93 | { |
| 94 | "address": "{ $withdraw_address }", |
| 95 | "amount": "{ $withdraw_amount }" |
| 96 | } |
| 97 | ] |
| 98 | } |
| 99 | }, |
| 100 | { |
| 101 | "app": "state", |
| 102 | "state": "{ |
| 103 | $balance_after_withdraw = $balance_before_withdraw - $withdraw_amount; |
| 104 | |
| 105 | if ($balance_after_withdraw == 0) |
| 106 | { |
| 107 | var[$drawer_amount_key] = false; |
| 108 | var[$drawer_asset_key] = false; |
| 109 | var[$drawer_owner] = false; |
| 110 | response['message'] = "Successful withdraw, the drawer is now empty ^^"; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | var[$drawer_amount_key] = $balance_after_withdraw; |
| 115 | var[$last_signature_key] = $signature_hash; |
| 116 | response['message'] = "successful withdraw ^^"; |
| 117 | } |
| 118 | }" |
| 119 | } |
| 120 | ] |
| 121 | }, |
| 122 | { |
| 123 | "messages": [ |
| 124 | { |
| 125 | "app": "state", |
| 126 | "state": "{ bounce ($INSTRUCTIONS); }" |
| 127 | } |
| 128 | ] |
| 129 | } |
| 130 | ] |
| 131 | } |
| 132 | } |
| 133 | ] |