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