예제 #1
0
 public static function spentcoins($coins, $account)
 {
     if (!is_array($coins) || $account != config::$accountId && !account::exists($account)) {
         return false;
     }
     if (!is_array($coins)) {
         return false;
     }
     $dest = strtolower($account) == strtolower(config::$accountId) ? config::$walfile : $account;
     $old = storage::load($dest);
     if (!$old) {
         return false;
     }
     foreach ($coins as $id => $coin) {
         $val = $coin['value'];
         if (array_key_exists($id, $old[$val])) {
             unset($old[$val][$id]);
         }
     }
     $ret = storage::save($old, $dest);
     $ret = $ret ? 1 : 0;
     return $ret;
 }
예제 #2
0
 public static function log(&$in)
 {
     list($oid, $t) = each($in);
     if (!is_array($t) || !isset($t['order id'])) {
         $t = $in;
         $oid = $t['order id'];
     }
     if (isset($t['coins'])) {
         unset($t['coins']);
     }
     if (isset($t['change'])) {
         unset($t['change']);
     }
     $acc = Tools::address($t['client']);
     $acm = Tools::address($t['merchant']);
     $ac = $acc['account'];
     $am = $acm['account'];
     $tr[$oid] = $t;
     if (account::exists($ac) || $ac == config::$accountId) {
         $oldc = storage::load("{$ac}.transaction");
         if (!$oldc) {
             $oldc = array();
         }
         $oldc[$oid] = $tr[$oid];
         storage::save($oldc, "{$ac}.transaction");
     }
     if (account::exists($am) || $am == config::$accountId) {
         $oldm = storage::load("{$am}.transaction");
         if (!$oldm) {
             $oldm = array();
         }
         $oldm[$oid] = $tr[$oid];
         storage::save($oldm, "{$am}.transaction");
     }
 }
예제 #3
0
 public static function process($msg)
 {
     $status = 1;
     $sender = "";
     $res = "";
     $umsg = GsonCrypt::unseal($msg);
     if (!$umsg) {
         $ex = Gmsg::extract($msg);
         if ($ex && is_array($ex)) {
             $umsg = $msg;
         } else {
             $status = 0;
             $res = "Unable to decode the message";
         }
     }
     if ($umsg) {
         $parts = self::extract($umsg);
         $action = $parts["op"];
         $mess = $parts["msg"];
         $sender = $parts["sender"];
         $recipient = $parts["recipient"];
         if ($recipient && !account::exists($recipient)) {
             $status = 0;
             $res = "The recipient account {$recipient} does not reside here";
             $rply = Gmsg::create(array("status" => $status, "response" => $res));
         } else {
             switch ($action) {
                 case "mrequest":
                     $r = transaction::request($mess['m'], $sender, $mess['k']);
                     $rply = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     break;
                 case "mstatus":
                     $r = transaction::mercorder($mess['m'], $sender, $mess['k']);
                     $rply = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     break;
                 case "statusrequest":
                     $r = transaction::status($mess, $sender);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "paymentrequest":
                     $r = transaction::request($mess, $sender);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "pullrequests":
                     $r = transaction::pullrequests($mess, $sender);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "pullcoins":
                     $r = account::pullcoins($mess, $sender);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "clearrequests":
                     $r = transaction::clearrequests($mess, $sender);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "notification":
                     $r = transaction::notification($mess, $sender);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "acknowledgement":
                     $r = transaction::acknowledgement($mess, config::$SRA);
                     $m = Gmsg::create(array("status" => $r[0], "response" => $r[1]));
                     $rply = GsonCrypt::sign($m);
                     break;
                 case "deposit":
                     $r = account::deposit($mess, $recipient);
                     if (!$r) {
                         $status = 0;
                         $res = "Deposit failed";
                     } else {
                         $res = "Deposit was successful";
                     }
                     break;
                 case "revokecert":
                     $net = new Gnet();
                     $rply = $net->send("{$mess}", true);
                     $net = null;
                     break;
                 case "signcert":
                     $net = new Gnet();
                     $rply = $net->send("{$mess}", true);
                     $net = null;
                     break;
                 case "register":
                     $k = GsonCrypt::getcert();
                     if (is_readable($k)) {
                         $res = gio::readfile($k);
                         if (!$res) {
                             $status = 0;
                         }
                     }
                     $rply = Gmsg::create(array("status" => $status, "cert" => $res, "name" => config::$accountId, "account" => account::makenew()));
                     break;
                 case "create":
                     $status = gio::savetofile($mess, GsonCrypt::getkey("{$sender}"));
                     $res = $status ? "successful" : "failed";
                     $rply = Gmsg::create(array("status" => $status, "response" => $res));
                     break;
                 case "remove":
                     $res = "";
                     $ret = array("status" => $status, "response" => $res);
                     $rply = self::create($ret);
                     $rply = GsonCrypt::seal("{$rply}", "{$sender}");
                     unlink(GsonCrypt::getkey($sender));
                     break;
                 case "exchangecert":
                     $status = 0;
                     if (!file_exists(GsonCrypt::getcert("{$sender}"))) {
                         $status = gio::saverawfile($mess, GsonCrypt::getcert("{$sender}"));
                     }
                     $k = GsonCrypt::getcert();
                     if ($status && is_readable($k)) {
                         $res = gio::readfile($k);
                         if (!$res) {
                             $status = 0;
                         }
                     }
                     $rply = Gmsg::create(array("status" => $status, "cert" => $res));
                     break;
                 case "reverb":
                     $res = $mess;
                     break;
                 default:
                     $status = 0;
                     $res = "Invalid Operation!";
             }
         }
     }
     if (!isset($rply)) {
         $ret = array("status" => $status, "response" => $res);
         $rply = self::create($ret);
         $rply = $sender ? GsonCrypt::seal("{$rply}", "{$sender}") : "{$rply}";
     }
     return $rply;
 }