[
"autonomous agent",
{
"init": "{
// SAAFE, Safe Autonomous Agent Forwarding Estate
$owner = "
O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; // could withdraw the dust is calculable.
// 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_received != "";
// Reconstitute the signature by concataining the 2 given parts
$signature_received = (trigger.data.s1 AND trigger.data.s2)? trigger.data.s1||trigger.data.s2 : "";
$b_is_signature_received = $signature_received != "";
$asset = trigger.data.asset otherwise trigger.output[[asset=base]].asset;
$asset_nice_name = ($asset == 'base')? 'bytes' : $asset;
$key = $puk||'_'||$asset;
}",
"messages": {
"cases": [
{
"if": "{($b_is_puk_received) AND !($b_is_signature_received or trigger.data.ad)}",
"messages": [
{
"app": "state",
"state": "{
var[$key] += trigger.output[[asset=$asset]];
response['message'] = var[$key]||' '||$asset_nice_name||' are safe under the puk '||$puk;
}"
}
]
},
{
"if": "{($b_is_puk_received) AND ($b_is_signature_received or trigger.data.ad) }",
"init": "{
if (!var[$key]) bounce ("puk doesn\'t exists with asset "||$asset_nice_name);
$withdraw_hash= sha256($puk||trigger.data.ad);
if (var[$withdraw_hash]) bounce ("Withdraw address already used with this SAAFE, this is forbidden for safety reasons: the signature for this address has been publicly visible, so anyone could trigger a new withdraw to this address, it could be a problem if the first withdraw was not a full one and the address not one from the owner of the SAAFE.");
if (!is_valid_sig(trigger.data.ad, "-----BEGIN PUBLIC KEY-----"||$puk||"-----END PUBLIC KEY-----", trigger.data.s1||trigger.data.s2)) bounce ("Wrong signature!");
$old_balance = var[$key];
$amount = trigger.data.am otherwise var[$key];
}",
"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[$key] = false;
else
var[$withdraw_hash] = "used";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
bounce ("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 later, 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'>.");
}"
}
]
}
]
}
}
]