| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $howto = "To store asset, choose a secret phrase (max length of 1024 char), pass it through a sha256 hasher. Send the funds with 'store = true' + 'hash_of_my_secret = <hash of secret phrase>. To withdraw use the hash and 'withdraw = true'"; |
| 9 | |
| 10 | $non_byte = trigger.output[[asset!=base]].asset; |
| 11 | $asset = ($non_byte != "none") ? $non_byte : "base"; |
| 12 | $amount = trigger.output[[asset=$asset]].amount; |
| 13 | }", |
| 14 | "messages": { |
| 15 | "cases": [ |
| 16 | { |
| 17 | "if": "{ !!trigger.data.store and !!trigger.data.hash_of_my_secret }", |
| 18 | "init": "{ |
| 19 | if (!!var[trigger.data.hash_of_my_secret]) |
| 20 | bounce ("This hash already exist, choose another secret phrase to hash"); |
| 21 | }", |
| 22 | "messages": [ |
| 23 | { |
| 24 | "app": "state", |
| 25 | "state": "{ |
| 26 | var[trigger.data.hash_of_my_secret] = $amount || " " || $asset; |
| 27 | response['message'] = "Safely stored, to withdraw later, use 'withdraw = true' + 'hash_of_my_secret = <hash of secret phrase> and wait for messages in payment"; |
| 28 | }" |
| 29 | } |
| 30 | ] |
| 31 | }, |
| 32 | { |
| 33 | "if": "{ !!trigger.data.withdraw and !!trigger.data.hash_of_my_secret }", |
| 34 | "init": "{ |
| 35 | if (!var[trigger.data.hash_of_my_secret]) |
| 36 | bounce ("This hash do not exist"); |
| 37 | if (!!var[trigger.data.hash_of_my_secret||"_withdrawer"]) |
| 38 | if (timestamp < var[trigger.data.hash_of_my_secret||"_Dead_line_timestamp"]) |
| 39 | bounce ("There is a withdraw request in progress"); |
| 40 | |
| 41 | $dead_line = timestamp + 60*60 ; |
| 42 | }", |
| 43 | "messages": [ |
| 44 | { |
| 45 | "app": "data", |
| 46 | "payload": { |
| 47 | "message": "{ "You have until "|| timestamp_to_string($dead_line, 'datetime') ||" to provide the secret key" }" |
| 48 | } |
| 49 | }, |
| 50 | { |
| 51 | "app": "payment", |
| 52 | "payload": { |
| 53 | "asset": "base", |
| 54 | "outputs": [ |
| 55 | { |
| 56 | "address": "{ trigger.initial_address }", |
| 57 | "amount": "{ 5 }" |
| 58 | } |
| 59 | ] |
| 60 | } |
| 61 | }, |
| 62 | { |
| 63 | "app": "state", |
| 64 | "state": "{ |
| 65 | if (!!var[trigger.data.hash_of_my_secret||"_withdrawer"]) |
| 66 | var[var[trigger.data.hash_of_my_secret||"_withdrawer"]] = false; |
| 67 | var[trigger.data.hash_of_my_secret||"_withdrawer"] = trigger.initial_address; |
| 68 | var[trigger.data.hash_of_my_secret||"_Dead_line_timestamp"] = $dead_line; |
| 69 | var[trigger.initial_address] = trigger.data.hash_of_my_secret; |
| 70 | response['message'] = "You have until "|| timestamp_to_string($dead_line, 'datetime') ||" to provide the secret key"; |
| 71 | }" |
| 72 | } |
| 73 | ] |
| 74 | }, |
| 75 | { |
| 76 | "if": "{ !!trigger.data.secret_phrase }", |
| 77 | "init": "{ |
| 78 | |
| 79 | if (!var[trigger.initial_address]) |
| 80 | bounce ("Attention!, I'm not ready to receive your secret phrase, don't send it or it can compromised"); |
| 81 | $hash = var[trigger.initial_address]; |
| 82 | |
| 83 | if (timestamp > var[$hash||"_Dead_line_timestamp"]) |
| 84 | bounce ("Too late! don't send you secret phrase!, use 'withdraw' + 'hash_of_my_secret' again"); |
| 85 | |
| 86 | if (sha256(trigger.data.secret_phrase) != $hash) |
| 87 | bounce ("The secret phrase hash does not match with the one previously given"||$hash||" "||sha256(trigger.data.secret_phrase)); |
| 88 | |
| 89 | $separator_position = index_of(var[$hash], " "); |
| 90 | $amount = substring(var[$hash], 0, $separator_position); |
| 91 | $asset = substring(var[$hash], $separator_position); |
| 92 | }", |
| 93 | "messages": [ |
| 94 | { |
| 95 | "app": "payment", |
| 96 | "payload": { |
| 97 | "asset": "{ $asset }", |
| 98 | "outputs": [ |
| 99 | { |
| 100 | "address": "{ trigger.initial_address }", |
| 101 | "amount": "{ $amount }" |
| 102 | } |
| 103 | ] |
| 104 | } |
| 105 | }, |
| 106 | { |
| 107 | "app": "state", |
| 108 | "state": "{ |
| 109 | |
| 110 | var[trigger.initial_address] = false; |
| 111 | var[trigger.initial_address||"_Dead_line_timestamp"] = false; |
| 112 | var[$hash||"_withdrawer"]=false; |
| 113 | var[$hash||"_Dead_line_timestamp"]=false; |
| 114 | response['message'] = "Funds sent"; |
| 115 | }" |
| 116 | } |
| 117 | ] |
| 118 | }, |
| 119 | { |
| 120 | "messages": [ |
| 121 | { |
| 122 | "app": "state", |
| 123 | "state": "{ bounce ($howto); }" |
| 124 | } |
| 125 | ] |
| 126 | } |
| 127 | ] |
| 128 | } |
| 129 | } |
| 130 | ] |