[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$howto = "Send funds with 'store = true' 'hash_of_my_secret = <hash of secret phrase>.";
$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.
}",
"messages": {
"cases": [
{
"if": "{ !!trigger.data.store and !!trigger.data.hash_of_my_secret }",
"init": "{
if (!!var[trigger.data.hash_of_my_secret]) // already existing in state
bounce ("This hash already exist, choose another secret phrase to hash");
}",
"messages": [
{
"app": "state",
"state": "{
var[trigger.data.hash_of_my_secret] = $amount || " " || $asset;
response['message'] = "Safely stored, to withdraw later, use 'withdraw_from = <storage name>' and wait for messages in payment";
}"
}
]
},
{
"if": "{ !!trigger.data.withdraw and !!trigger.data.hash_of_my_secret }",
"init": "{
if (!var[trigger.data.hash_of_my_secret]) // not existing in state
bounce ("This hash do not exist");
if (!!var[trigger.data.hash_of_my_secret||"_withdrawer"])
if (timestamp < var[trigger.data.hash_of_my_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.address }",
"amount": "{ 5 }"
}
]
}
},
{
"app": "state",
"state": "{
if (!!var[trigger.data.hash_of_my_secret||"_withdrawer"]) // if outdated request exist
var[var[trigger.data.hash_of_my_secret||"_withdrawer"]] = false; // remove it
var[trigger.data.hash_of_my_secret||"_withdrawer"] = trigger.address;
var[trigger.data.hash_of_my_secret||"_Dead_line_timestamp"] = $dead_line;
var[trigger.address] = trigger.data.hash_of_my_secret;
response['message'] = "You have until "|| timestamp_to_string($dead_line, 'datetime') ||" to provide the secret key";
}"
}
]
},
{
"if": "{ !!trigger.data.secret_phrase }",
"init": "{
// waiting secret key from this address?
if (!var[trigger.address])
bounce ("Attention!, I'm not ready to receive your secret phrase, don't send it or it can compromised");
// check if it is not to late
if (timestamp> var[trigger.address||"_Dead_line_timestamp"])
bounce ("Too late! don't send you secret phrase!, use 'withdraw' + 'hash_of_my_secret' again");
// check that the secret phrase is valid:
$hash = var[trigger.address];
if (sha256(trigger.data.secret_phrase) != $hash)
bounce ("The secret phrase hash does not match with the one previously given");
// prepare asset and amount to withdraw
$separator_position = index_of(var[$hash], " ");
$amount = substring(var[$hash], 0, $separator_position);
$asset = substring(var[$hash], $separator_position);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{ $asset }",
"outputs": [
{
"address": "{ trigger.address }",
"amount": "{ $amount }"
}
]
}
},
{
"app": "state",
"state": "{
// delete all states
var[trigger.address] = false;
var[trigger.address||"_Dead_line_timestamp"] = false;
var[$hash]=false;
response['message'] = "Funds sent";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{ bounce ($howto); }"
}
]
}
]
}
}
]