[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
}",
"messages": {
"cases": [
{
"if": "{ !!trigger.data.store and !trigger.data.request_withdraw and !!trigger.data.hash_of_secret }",
"init": "{
if (!!var[trigger.data.hash_of_secret]) // already existing in state
bounce ("This hash already exist, choose another secret phrase and hash it");
}",
"messages": [
{
"app": "state",
"state": "{
$non_byte = trigger.output[[asset!=base]].asset; // return the asset if different than bytes
$asset = ($non_byte != "none") ? $non_byte : "base"; // the asset is the non-byte if existing
$amount = trigger.output[[asset=$asset]].amount; // get the amount of the asset.
var[trigger.data.hash_of_secret||"_amount"] = $amount;
var[trigger.data.hash_of_secret||"_asset"] = $asset;
response['message'] = "Safely stored, to withdraw later, use 'withdraw = true' + 'hash_of_my_secret = <hash of secret phrase> and wait for messages in payment";
}"
}
]
},
{
"if": "{ !!trigger.data.request_withdraw and !!trigger.data.hash_of_secret }",
"init": "{
if (!var[trigger.data.hash_of_secret||"_amount"]) // not existing in state
bounce ("This hash do not exist");
if (!!var[trigger.data.hash_of_secret||"_withdrawer"])
if (timestamp < var[trigger.data.hash_of_secret||"_Dead_line_timestamp"])
bounce ("There is a withdraw request in progress");
// determine a period in which this address can provide the secret phrase
$dead_line = timestamp + 60*60 ; // add 1 hour
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{ "You have until "|| timestamp_to_string($dead_line, 'datetime') ||" to provide the secret key" }"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ trigger.initial_address }",
"amount": "{ 5 }"
}
]
}
},
{
"app": "state",
"state": "{
if (!!var[trigger.data.hash_of_secret||"_withdrawer"]) // if outdated request exist
var[var[trigger.data.hash_of_secret||"_withdrawer"]] = false; // remove it
var[trigger.data.hash_of_secret||"_withdrawer"] = trigger.initial_address;
var[trigger.data.hash_of_secret||"_Dead_line_timestamp"] = $dead_line;
var[trigger.initial_address] = trigger.data.hash_of_secret; // initial to be able to work through other AA as well
response['message'] = "You have until "|| timestamp_to_string($dead_line, 'datetime') ||" to provide the secret key";
}"
}
]
},
{
"if": "{ !!trigger.data.proove_ownership and !!trigger.data.secret_phrase }",
"init": "{
// waiting secret key from this address?
if (!var[trigger.initial_address])
bounce ("Attention!, I'm not ready to receive your secret phrase, don't send it or it can be compromised");
$hash = var[trigger.initial_address];
// check if it is not too late (additionnal 6 minutes allowed for the transaction time)
if (timestamp > var[$hash||"_Dead_line_timestamp"] + 60 * 6)
bounce ("Too late! don't send you secret phrase!, use 'withdraw' + 'hash_of_my_secret' again");
// check that the secret phrase is valid:
if (sha256(trigger.data.secret_phrase) != $hash)
bounce ("The secret phrase hash does not match with the one previously given"||$hash||" "||sha256(trigger.data.secret_phrase));
// prepare asset and amount to withdraw
$separator_position = index_of(var[$hash], " ");
$withdraw_amount = substring(var[$hash], 0, $separator_position);
$withdraw_asset = substring(var[$hash], $separator_position);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{ var[$hash||"_asset"] }",
"outputs": [
{
"address": "{ trigger.initial_address }",
"amount": "{ var[$hash||"_amount"] }"
}
]
}
},
{
"app": "state",
"state": "{
response['message'] = $withdraw_amount || " Funds sent "|| $withdraw_asset;
// delete all states
var[trigger.initial_address] = false;
var[trigger.initial_address||"_Dead_line_timestamp"] = false;
var[$hash||"_withdrawer"]=false;
var[$hash||"_asset"]=false;
var[$hash||"_amount"]=false;
var[$hash||"_Dead_line_timestamp"]=false;
response['message'] = "Funds sent";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
// use cases instructions
if (!!trigger.data.store)
bounce ("Instruction: Add 'hash_of_secret = [(sha255 base64) hash]', to create hash, choose a secret phrase (max length of 1024 char) and pass it through a sha256 hasher that encode in base64.");
if (!!trigger.data.request_withdraw)
bounce ("Instruction: Add 'hash_of_secret = <hash of the secret phrase>.");
if (!!trigger.data.proove_ownership)
bounce ("Instruction: Add 'secret_phrase = <clear secret phrase>.");
// general purpose instructions
bounce ("Instruction: First choose what to do, 'store=true' to store any Obyte assets funds, 'request_withdraw=true' to initiate a withdraw or 'proove_ownership=true' then follow next instructions");
}"
}
]
}
]
}
}
]