| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://obyte.org/bank.json", |
| 5 | "getters": "{ |
| 6 | |
| 7 | $get_balance = ($address, $asset) => { |
| 8 | var['balance_' || $address || '_' || $asset] OTHERWISE 0 |
| 9 | }; |
| 10 | |
| 11 | $get_payment_messages = ($payments) => { |
| 12 | $payment_messages_by_asset = {}; |
| 13 | $buffer_recipients = []; |
| 14 | $payment_messages = []; |
| 15 | foreach($payments, 5, $payment => { |
| 16 | if (!$payment.amount) |
| 17 | return; |
| 18 | if ($payment.is_aa){ |
| 19 | delete($payment, 'is_aa'); |
| 20 | $buffer_recipients[] = $payment; |
| 21 | $address = this_address; |
| 22 | } |
| 23 | else |
| 24 | $address = $payment.address; |
| 25 | if ($payment_messages_by_asset[$payment.asset]) |
| 26 | $payment_messages_by_asset[$payment.asset].payload.outputs[] = {address: $address, amount: $payment.amount}; |
| 27 | else |
| 28 | $payment_messages_by_asset[$payment.asset] = { |
| 29 | app: 'payment', |
| 30 | payload: { |
| 31 | asset: $payment.asset, |
| 32 | outputs: [ |
| 33 | {address: $address, amount: $payment.amount} |
| 34 | ] |
| 35 | } |
| 36 | }; |
| 37 | }); |
| 38 | |
| 39 | foreach($payment_messages_by_asset, 5, $payment_message => { |
| 40 | $payment_messages[] = $payment_message; |
| 41 | }); |
| 42 | { |
| 43 | payment_messages: $payment_messages, |
| 44 | buffer_recipients: $buffer_recipients, |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | }", |
| 49 | "init": "{ |
| 50 | $withdrawal_fee = 2000; |
| 51 | |
| 52 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 53 | bounce("bad to-address"); |
| 54 | |
| 55 | $get_amount = ($amount, $balance) => { |
| 56 | if ($amount == 'all') |
| 57 | return $balance; |
| 58 | if (!is_integer($amount) OR $amount <= 0) |
| 59 | bounce("bad amount: " || $amount); |
| 60 | $amount |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | $get_owner = ($message_to_sign) => { |
| 65 | if (!trigger.data.owner) |
| 66 | return trigger.address; |
| 67 | if (!trigger.data.pubkey) |
| 68 | bounce("no pubkey"); |
| 69 | if (!trigger.data.signature) |
| 70 | bounce("no signature"); |
| 71 | if (!trigger.data.nonce) |
| 72 | bounce("no nonce"); |
| 73 | $pubkey_hash = sha256(trigger.data.pubkey); |
| 74 | if (!var['key_' || trigger.data.owner || '_' || $pubkey_hash]) |
| 75 | bounce("this key is not auhorized to sign for this owner"); |
| 76 | if (var['nonce_' || $pubkey_hash || '_' || trigger.data.nonce]) |
| 77 | bounce("this nonce was already used"); |
| 78 | $full_message_to_sign = $message_to_sign || " with nonce " || trigger.data.nonce; |
| 79 | if (!is_valid_sig($full_message_to_sign, trigger.data.pubkey, trigger.data.signature)) |
| 80 | bounce("invalid signature"); |
| 81 | trigger.data.owner |
| 82 | }; |
| 83 | |
| 84 | $write_nonce = () => { |
| 85 | if (trigger.data.owner) |
| 86 | var['nonce_' || sha256(trigger.data.pubkey) || '_' || trigger.data.nonce] = 1; |
| 87 | }; |
| 88 | }", |
| 89 | "messages": { |
| 90 | "cases": [ |
| 91 | { |
| 92 | "if": "{trigger.data.withdraw AND trigger.data.asset AND trigger.data.amount}", |
| 93 | "init": "{ |
| 94 | $owner = $get_owner("withdraw " || trigger.data.amount || " " || trigger.data.asset || " to " || (trigger.data.to OTHERWISE "self")); |
| 95 | $to = trigger.data.to OTHERWISE $owner; |
| 96 | |
| 97 | $base_amount = trigger.output[[asset=base]]; |
| 98 | $key = 'balance_' || $owner || '_' || trigger.data.asset; |
| 99 | $base_key = 'balance_' || $owner || '_base'; |
| 100 | $fee = (trigger.data.asset == 'base') ? $withdrawal_fee : 0; |
| 101 | $balance = var[$key]; |
| 102 | if (!$balance) |
| 103 | bounce("you have no balance in this asset"); |
| 104 | $amount = $get_amount(trigger.data.amount, $balance - $fee); |
| 105 | $required_amount = $amount + $fee; |
| 106 | if ($required_amount > $balance) |
| 107 | bounce("trying to withdraw more than you have: " || $required_amount || " > " || $balance); |
| 108 | if ($withdrawal_fee > var[$base_key] + $base_amount) |
| 109 | bounce("not enough bytes to pay the withdrawal fee, please add some bytes first"); |
| 110 | }", |
| 111 | "messages": [ |
| 112 | { |
| 113 | "app": "payment", |
| 114 | "payload": { |
| 115 | "asset": "{trigger.data.asset}", |
| 116 | "outputs": [ |
| 117 | { |
| 118 | "address": "{$to}", |
| 119 | "amount": "{$amount}" |
| 120 | } |
| 121 | ] |
| 122 | } |
| 123 | }, |
| 124 | { |
| 125 | "app": "state", |
| 126 | "state": "{ |
| 127 | var[$key] -= $amount; |
| 128 | var[$base_key] += - $withdrawal_fee + $base_amount; |
| 129 | $write_nonce(); |
| 130 | }" |
| 131 | } |
| 132 | ] |
| 133 | }, |
| 134 | { |
| 135 | "if": "{trigger.data.withdraw AND trigger.data.recipients}", |
| 136 | "init": "{ |
| 137 | $owner = trigger.address; |
| 138 | if (!is_array(trigger.data.recipients)) |
| 139 | bounce("recipients must be array"); |
| 140 | if (length(trigger.data.recipients) > 5) |
| 141 | bounce("too many recipients, max 5"); |
| 142 | $fee = $withdrawal_fee * length(trigger.data.recipients); |
| 143 | $totals = { base: $fee }; |
| 144 | foreach(trigger.data.recipients, 5, $recipient => { |
| 145 | if (!is_integer($recipient.amount) OR $recipient.amount < 0) |
| 146 | bounce("bad amount: " || $recipient.amount); |
| 147 | if (!is_valid_address($recipient.address)) |
| 148 | bounce("bad address: " || $recipient.address); |
| 149 | if ($recipient.is_aa) |
| 150 | bounce("is_aa not allowed here"); |
| 151 | $totals[$recipient.asset] = $totals[$recipient.asset] + $recipient.amount; |
| 152 | }); |
| 153 | foreach($totals, 5, ($asset, $total) => { |
| 154 | $balance = var['balance_' || $owner || '_' || $asset]; |
| 155 | if (!$balance) |
| 156 | bounce("you have no balance in " || $asset); |
| 157 | if ($total > $balance) |
| 158 | bounce("not enough balance in " || $asset); |
| 159 | }); |
| 160 | $res = $get_payment_messages(trigger.data.recipients); |
| 161 | $payment_messages = $res.payment_messages; |
| 162 | |
| 163 | $data = trigger.data; |
| 164 | delete($data, 'recipients'); |
| 165 | delete($data, 'withdraw'); |
| 166 | }", |
| 167 | "messages": [ |
| 168 | "{$payment_messages[0] OTHERWISE ''}", |
| 169 | "{$payment_messages[1] OTHERWISE ''}", |
| 170 | "{$payment_messages[2] OTHERWISE ''}", |
| 171 | "{$payment_messages[3] OTHERWISE ''}", |
| 172 | "{$payment_messages[4] OTHERWISE ''}", |
| 173 | { |
| 174 | "if": "{length($data) > 0}", |
| 175 | "app": "data", |
| 176 | "payload": "{$data}" |
| 177 | }, |
| 178 | { |
| 179 | "app": "state", |
| 180 | "state": "{ |
| 181 | foreach($totals, 5, ($asset, $total) => { |
| 182 | var['balance_' || $owner || '_' || $asset] -= $total; |
| 183 | }); |
| 184 | }" |
| 185 | } |
| 186 | ] |
| 187 | }, |
| 188 | { |
| 189 | "if": "{trigger.data.transfer AND trigger.data.to AND trigger.data.asset AND trigger.data.amount}", |
| 190 | "init": "{ |
| 191 | $owner = $get_owner("transfer " || trigger.data.amount || " " || trigger.data.asset || " to " || trigger.data.to); |
| 192 | |
| 193 | $base_amount = trigger.output[[asset=base]]; |
| 194 | $base_key = 'balance_' || $owner || '_base'; |
| 195 | |
| 196 | $key = 'balance_' || $owner || '_' || trigger.data.asset; |
| 197 | $balance = var[$key]; |
| 198 | if (!$balance) |
| 199 | bounce("you have no balance in this asset"); |
| 200 | |
| 201 | $amount = $get_amount(trigger.data.amount, $balance); |
| 202 | if ($amount > $balance) |
| 203 | bounce("trying to transfer more than you have: " || $balance); |
| 204 | }", |
| 205 | "messages": [ |
| 206 | { |
| 207 | "app": "state", |
| 208 | "state": "{ |
| 209 | var[$key] -= $amount; |
| 210 | var['balance_' || trigger.data.to || '_' || trigger.data.asset] += $amount; |
| 211 | var[$base_key] += $base_amount; |
| 212 | $write_nonce(); |
| 213 | }" |
| 214 | } |
| 215 | ] |
| 216 | }, |
| 217 | { |
| 218 | "if": "{trigger.data.authorize AND trigger.data.pubkey}", |
| 219 | "init": "{ |
| 220 | if (length(trigger.data.pubkey) > 1000) |
| 221 | bounce("pubkey is too long"); |
| 222 | }", |
| 223 | "messages": [ |
| 224 | { |
| 225 | "app": "state", |
| 226 | "state": "{ |
| 227 | var['key_' || trigger.address || '_' || sha256(trigger.data.pubkey)] = 1; |
| 228 | response['message'] = "authorized the key to sign transactions on your behalf"; |
| 229 | }" |
| 230 | } |
| 231 | ] |
| 232 | }, |
| 233 | { |
| 234 | "if": "{trigger.data.revoke AND trigger.data.pubkey}", |
| 235 | "init": "{ |
| 236 | if (length(trigger.data.pubkey) > 1000) |
| 237 | bounce("pubkey is too long"); |
| 238 | }", |
| 239 | "messages": [ |
| 240 | { |
| 241 | "app": "state", |
| 242 | "state": "{ |
| 243 | var['key_' || trigger.address || '_' || sha256(trigger.data.pubkey)] = false; |
| 244 | response['message'] = "revoked the key"; |
| 245 | }" |
| 246 | } |
| 247 | ] |
| 248 | }, |
| 249 | { |
| 250 | "if": "{trigger.data.recipients}", |
| 251 | "init": "{ |
| 252 | $totals = {}; |
| 253 | foreach(trigger.data.recipients, 10, $recipient => { |
| 254 | if (!is_integer($recipient.amount) OR $recipient.amount < 0) |
| 255 | bounce("bad amount: " || $recipient.amount); |
| 256 | if (!is_valid_address($recipient.address)) |
| 257 | bounce("bad address: " || $recipient.address); |
| 258 | if (!trigger.output[[asset=$recipient.asset]]) |
| 259 | bounce("nothing received in asset " || $recipient.asset); |
| 260 | $totals[$recipient.asset] = $totals[$recipient.asset] + $recipient.amount; |
| 261 | }); |
| 262 | foreach($totals, 10, ($asset, $total) => { |
| 263 | if ($total != trigger.output[[asset=$asset]]) |
| 264 | bounce("expected " || $total || " for asset " || $asset); |
| 265 | }); |
| 266 | |
| 267 | |
| 268 | }", |
| 269 | "messages": [ |
| 270 | { |
| 271 | "app": "state", |
| 272 | "state": "{ |
| 273 | foreach(trigger.data.recipients, 10, $recipient => { |
| 274 | $asset_key = 'balance_' || $recipient.address || '_' || $recipient.asset; |
| 275 | var[$asset_key] += $recipient.amount; |
| 276 | }); |
| 277 | }" |
| 278 | } |
| 279 | ] |
| 280 | }, |
| 281 | { |
| 282 | "messages": [ |
| 283 | { |
| 284 | "app": "state", |
| 285 | "state": "{ |
| 286 | $asset = trigger.output[[asset!=base]].asset; |
| 287 | if ($asset == 'ambiguous') |
| 288 | bounce('ambiguous asset'); |
| 289 | |
| 290 | $to = trigger.data.to OTHERWISE trigger.address; |
| 291 | |
| 292 | $base_amount = trigger.output[[asset=base]]; |
| 293 | $base_key = 'balance_' || $to || '_base'; |
| 294 | var[$base_key] += $base_amount; |
| 295 | $response_base = $base_amount || ' bytes\ |
| 296 | '; |
| 297 | |
| 298 | if ($asset != 'none'){ |
| 299 | $asset_key = 'balance_' || $to || '_' || $asset; |
| 300 | var[$asset_key] += trigger.output[[asset=$asset]]; |
| 301 | $response_asset = trigger.output[[asset=$asset]] || ' of ' || $asset || '\ |
| 302 | '; |
| 303 | } |
| 304 | response['message'] = 'accepted coins:\ |
| 305 | ' || ($response_base otherwise '') || ($response_asset otherwise ''); |
| 306 | }" |
| 307 | } |
| 308 | ] |
| 309 | } |
| 310 | ] |
| 311 | } |
| 312 | } |
| 313 | ] |