public static function destroy() { $status = false; $net = new Gnet(); if (!self::isCreated()) { gio::output("No account found!"); } else { $m = Gmsg::create(Gmsg::prepare(config::$accountId, "revokecert", config::$SRA)); $m = GsonCrypt::seal($m, config::$SRA); $r = $net->send($m, true); if (!$r) { gio::output(); gio::output("Account Destruction Failed! Unable to connect to account server"); } else { $r = GsonCrypt::unseal($r); $r = Gmsg::extract($r); if ($r['status']) { self::rollback(); gio::output("Your account has been destroyed"); } else { gio::output($r['response']); } } } }
public static function give($to, $amt) { $ramt = $amt; $rem = mine::countcoins($coins); if ($rem < $amt) { gio::output("Not enough coins"); return false; } $paths = Tools::address($to); if (!$paths) { gio::output("The destination account is invalid"); return false; } $getcoins = array(); $c = 1; if (!is_array($coins)) { return false; } $vals = array_keys($coins); rsort($vals); foreach ($vals as $val) { if ($amt < $val) { continue; } if ($amt <= 0) { break; } $ch[$val] = floor($amt / $val); if ($ch[$val] > count($coins[$val])) { $ch[$val] = count($coins[$val]); } $amt -= $ch[$val] * $val; } foreach ($ch as $v => $n) { while ($n > 0 && (list($id, $coin) = each($coins[$v]))) { $getcoins[$id] = $coin; unset($coins[$v][$id]); $n--; } } foreach ($getcoins as $k => $v) { $secret[$k] = Tools::makesecrets(); $getcoins[$k]['secret'] = GsonCrypt::seal($secret[$k], $paths['srakey']); } $net = new Gnet(); if (!$net->connect($paths['address'], intval($paths['port']))) { gio::output("Unable to connect to the destination account"); return false; } else { $m = Gmsg::create(Gmsg::prepare($getcoins, "deposit", $paths['account'])); $m = GsonCrypt::seal($m, $paths['bank']); if (!$m) { gio::output("Unable to send!"); gio::output("POSSIBLE CAUSES:"); gio::output("The destination bank's certificate is not avaiable!"); gio::output("Account may not be registered with sra!"); gio::output("Account may have been deregistered with sra!"); return false; } $r = $net->send($m); $s = GsonCrypt::unseal($r); $r = $s ? Gmsg::extract($s) : Gmsg::extract($r); unset($net); if (!$r || !$r['status']) { gio::output("Deposit of {$ramt} coins to {$to} Failed!"); gio::output($r['response']); return false; } else { $old = storage::load($paths['srakey']); foreach ($getcoins as $id => $coin) { $getcoins[$id]['secret'] = $secret[$id]; $val = $coin['value']; $old[$val][$id] =& $getcoins[$id]; } storage::save($old, $paths['srakey']); storage::save($coins); gio::output("Deposit of {$ramt} coins to {$to} was successful"); return true; } } }
public static function process($msg) { $res = ""; $status = 1; $sender = ""; $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"]; $recipient = $parts["recipient"]; $sender = $parts["sender"]; if (isset($parts["bank"])) { $sender = $parts["bank"] . "_{$sender}"; } if (strtolower($recipient) != strtolower(config::$accountId)) { $status = 0; $res = config::$accountId . " is not the intended recipient [{$recipient}]"; $rply = Gmsg::create(array("status" => $status, "response" => $res)); } else { switch ($action) { case "notification": $r = transaction::notification($mess, $sender); $m = Gmsg::create(array("status" => $r[0], "response" => $r[1])); $rply = GsonCrypt::sign($m); break; case "revokecert": if (!$sender) { $status = 0; $res = "The sender is unknown"; } else { $res = ""; $ret = array("status" => $status, "response" => $res, "account" => $sender); $rply = self::create($ret); $rply = GsonCrypt::seal("{$rply}", "{$sender}"); @unlink(GsonCrypt::getkey($sender)); /* Buggy: Verify the sender first*/ } break; case "signcert": $k = GsonCrypt::getkey("{$sender}"); if (file_exists($k)) { $status = 2; $res = "This account already exist!"; } else { $res = GsonCrypt::signcert($sender, $mess); if (!$res) { $status = 0; $res = "An error occured while signing the certificate."; } } break; case "reverb": $res = $mess; break; default: $status = 0; $res = "Invalid Operation!"; } } } if (!isset($rply)) { $ret = array("status" => $status, "response" => $res, "account" => $sender); $rply = self::create($ret); $rply = $sender ? GsonCrypt::seal("{$rply}", "{$sender}") : "{$rply}"; } return $rply; }
public static function pgrant() { $acc = config::$accountId; $res = storage::load("{$acc}.request"); if (!$res || count($res) < 1) { gio::output("No pending payment requests"); } else { foreach ($res as $id => $req) { gio::output(); $disp = $req; $disp['time'] = date(config::$timeFormat, $disp['time']); gio::display($disp); gio::output(); if (gio::confirm("Do you want to grant this payment")) { while (true) { $amount = gio::input("Enter amount to confirm or c to cancel"); if ($amount == 'c' || $amount == 'C') { break; } if ($req['amount'] == $amount) { while (true) { $coins = account::getmycoins($amount); if (!$coins) { gio::output("Isufficient balance"); break; } $req['coins'] = $coins; foreach ($req['coins'] as $k => $v) { $req['coins'][$k]['secret'] = GsonCrypt::seal($req['coins'][$k]['secret'], config::$SRA); } $r = self::notification(array("{$id}" => $req), config::$accountId); $status = $r[0]; gio::output($r[1]); if ($status) { account::spentcoins($coins, config::$accountId); } if ($status || !$status && !gio::confirm("Do you want to try again")) { break; } } gio::output(); break; } } } } } }
public static function getcoins($vals, $owner, &$secrets, &$mycoin) { foreach ($vals as $v => $n) { foreach ($mycoin[$v] as $id => $coin) { if (!$n) { break; } if (empty($mycoin[$v])) { return false; } $getcoins[$id] = $coin; $secrets[$id] = Tools::makesecrets(); $getcoins[$id]['secret'] = GsonCrypt::seal($secrets[$id], $owner); $getcoins[$id]['transactioncount'] += 1; unset($mycoin[$v][$id]); $n--; } } return $getcoins; }
public static function destroy() { $status = false; $net = new Gnet(); if (!self::isCreated()) { gio::output("No account found!"); } else { $m = Gmsg::create(Gmsg::prepare(config::$accountId, "revokecert", config::$SRA)); $b = GsonCrypt::seal($m, config::$SRA); $bm = Gmsg::create(Gmsg::prepare($m, "revokecert", config::$bankId)); $r = $net->send($bm); if (!$r) { gio::output(); gio::output("Account Destruction Failed! Unable to connect to account server"); } else { $tr = GsonCrypt::unseal($r); if (!$tr) { $ex = Gmsg::extract($r); if (!$ex || !is_array($ex)) { gio::output(); gio::output("Account Destruction Failed! Server returned an invalid message"); } } $r = isset($ex) ? $ex : ($tr ? Gmsg::extract($tr) : null); if ($r['status']) { $m = Gmsg::create(Gmsg::prepare(config::$accountId, "remove", config::$bankId)); $rmv = $net->send($m); self::rollback(); if (gio::confirm(sprintf("Do you also want to deregister the bank '%s'", config::$bankId))) { self::deregister(); } gio::output("Your account has been destroyed"); } else { gio::output($r['response']); } } } }
private static function sendnotification($id, $req) { foreach ($req['coins'] as $k => $v) { $req['coins'][$k]['secret'] = GsonCrypt::seal($req['coins'][$k]['secret'], config::$SRA); } $m = GsonCrypt::sign(Gmsg::create(array("{$id}" => $req))); if (!$m) { $status = 0; $res = "Unable to sign payment request for '{$id}'"; } else { $m = Gmsg::create(Gmsg::Prepare($m, "notification", config::$bankId)); $net = new Gnet(); $r = $net->send($m); unset($net); if (!$r) { $status = 0; $res = "Unable to send grant payament for '{$id}'"; } else { $v = GsonCrypt::verify($r, config::$bankId); if (!$v) { $status = 0; $res = "Unable to verify response from bank for '{$id}'"; } else { $v = Gmsg::extract($v); if (!$v) { $status = 0; $res = "Unable to understand the response in relation to '{$id}'"; } else { $status = $v['status']; $res = $v['response']; } } } } return array($status, $res); }
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; }