Definition: [
"autonomous agent",
{
"init": "{
$owner = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; // could withdraw the dust is calculable., can set the safe to open/close and if close add some accesses.
// check accesses
if ((var["mode"] == "closed") and (!var["grant_"||trigger.address]) and (trigger.address != $owner))
bounce ("User not allowed !");
// Reconstitute the public key by concataining the 2 given parts
$puk = (trigger.data.puk1 AND trigger.data.puk2)? trigger.data.puk1||trigger.data.puk2 : "";
$b_is_puk_received = ($puk != "");
// Reconstitute the signature by concataining the 2 given parts
$signature = (trigger.data.s1 AND trigger.data.s2)? trigger.data.s1||trigger.data.s2 : "";
$b_is_signature_received = ($signature != "");
$asset = trigger.data.asset otherwise trigger.output[[asset=base]].asset;
$asset_nice_name = ($asset == 'base')? 'bytes' : $asset;
$drawer = $puk||'_'||$asset;
}",
"messages": {
"cases": [
{
"if": "{trigger.address == $owner}",
"messages": [
{
"app": "state",
"state": "{
if (trigger.data.open) var["mode"] = "open";
if (trigger.data.closed) var["mode"] = "closed";
if (trigger.data.grant) var ["grant_"||trigger.data.grant] = true;
if (trigger.data.remove) var ["grant_"||trigger.data.remove] = false;
}"
}
]
},
{
"if": "{($b_is_puk_received) AND !($b_is_signature_received or trigger.data.ad)}",
"messages": [
{
"app": "state",
"state": "{
var["drawer_"||$drawer] += trigger.output[[asset=$asset]];
response['message'] = var[$drawer]||' '||$asset_nice_name||' are safe under the puk '||$puk;
}"
}
]
},
{
"if": "{($b_is_puk_received) AND ($b_is_signature_received or trigger.data.ad) }",
"init": "{
$signature = trigger.data.s1||trigger.data.s2;
if (!var["drawer_"||$drawer]) bounce ("puk doesn\'t exists with asset!"||$asset_nice_name);
if (var["last_s_"||$puk] == $signature) bounce ("Cannot use 2 times the same signature in a row!");
if (!is_valid_sig(trigger.data.ad, "-----BEGIN PUBLIC KEY-----"||$puk||"-----END PUBLIC KEY-----", $signature)) bounce ("Wrong signature!");
$old_balance = var["drawer_"||$drawer];
$amount = trigger.data.am otherwise var["drawer_"||$drawer];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.data.ad}",
"amount": "{$amount}"
}
]
}
},
{
"app": "state",
"state": "{
$new_balance = $old_balance - $amount - 1000;
response['message'] = $amount||' of the '||$old_balance||' '||$asset_nice_name||' withdrawn from '||$puk||' to '|| trigger.data.ad;
if ($new_balance < 2000)
var["drawer_"||$drawer] = false;
else
var["last_s_"||$puk] = $signature;
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
bounce ("To store, set a 'puk1' and 'puk2' field to <the 2 half of the public key, for which you have the related private key> to safely store funds. To withdraw, set the same 'puk', as well as a 'ad' field to <the Obyte adresse where you want to receive the funds> and a 's1' and 's2' field to <the 2 half signature of a message containing the address 'a'>.");
}"
}
]
}
]
}
}
]