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 hascert($userid = "") { if (empty($userid)) { $userid = config::$accountId; } $cert = self::getcert($userid); if (!file_exists($cert)) { $m = Gmsg::create(Gmsg::prepare("", "register", $userid)); $net = new Gnet(); $r = $net->send($m); unset($net); if (!$r) { } return false; $r = extract($r); if (!$r || !$r['status']) { return false; } if (!gio::saverawfile($r['cert'], $cert)) { return false; } } return true; }
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; } } }
} } $responsetime = date($dateformat); $_SESSION['shop2']['response'] = array("Order Id" => $_SESSION['shop2']['order id'], "Reference" => "", "Sent Time" => $timesent, "Response Time" => $responsetime, "Time Received" => "", "Time Completed" => "", "status" => $status, "Message" => "{$msg}", "Amount Valued" => $sum, "Amount Processed" => "", "From Account" => "{$address}", "To Account" => "{$merchantAddress}"); $_SESSION['shop2']['status'] = $status == 0 ? "successful" : "not successful"; $step = 3; } if (isset($_POST['step3'])) { $sum = $_SESSION['shop2']['sum']; $purchase = $_SESSION['shop2']['purchase']; $address = $_SESSION['shop2']['address']; //$response = $_SESSION['shop2']['response']; $mg['oid'] = $_SESSION['shop2']['order id']; $m['m'] = $mg; $m['k'] = $merchantKey; $net = new Gnet(); $my = Tools::address($merchantAddress); $m = Gmsg::create(Gmsg::prepare($m, "mstatus", $my['bank'])); $response['Order Id'] = $mg['oid']; $response['Order value'] = "{$sum} {$currency}"; $rr = $net->send($m); if (!$rr) { $status = 0; $msg = "Unable to connect to Merchant's bank to verify transaction's state"; } else { $rr = Gmsg::extract($rr); if (!$rr) { $status = 0; $msg = "Failed to communicate with Merchant's bank to verify transaction state of order"; } else { if ($rr['status']) {
function main($mainmsg) { gio::output(config::$walletMessage); if (account::isCreated()) { $serv = popen("service.ecsh", 'r'); } $net = new Gnet(); $inerr = 0; while (true) { $inmsg = array(); if (account::isCreated()) { $inmsg["d"] = "Destroy Account"; $dmsg = $mainmsg + $inmsg; } else { $inmsg["c"] = "Create Bank Wallet"; $dmsg = $inmsg; } $dmsg["x"] = "Exit"; if (!$inerr) { gio::output(); foreach ($dmsg as $k => $v) { $k = strtoupper($k); gio::output("{$k} - {$v}"); } gio::output(); $msg = "Enter your choice and press enter"; } $inerr = 0; $c = gio::input("{$msg}"); if (!array_key_exists($c, $dmsg)) { $c = null; } switch ($c) { case 1: $a = account::address(); gio::output("Bank's account address is: {$a}"); break; case 2: $n = account::coins(config::$accountId, $coins); gio::output("You have {$n} coins"); if ($n && gio::confirm("Do you wish to see their Id's")) { foreach ($coins as $val => $ccs) { foreach ($ccs as $i => $coin) { gio::output("Ecash ID: {$i}"); foreach ($coin as $id => $c) { if ($id == "token") { $c = md5($c); } if ($id == "hash") { $c = sha1($c); } if ($id == "mined") { $c = date(config::$timeFormat, $c); } gio::output("{$id}=>{$c}"); } gio::output(); } } } break; case 3: $m = transaction::prequest(); gio::output($m[1]); break; case 4: transaction::pgrant(); break; case 5: $acc = gio::input("Enter the wallet number [EMPTY for bank's self]"); $o = gio::input("Enter the order id [EMPTY for all]"); $f = null; if (gio::confirm("Do you want to create a file on your desktop")) { do { $f = gio::input("Enter the file name"); } while (!$f); } transaction::reports($o, $acc, $f); break; case 6: $m = transaction::clearrequests(null, null); gio::output($m[1]); break; case 7: account::merckey(gio::input("Enter the name of the file to write to your desktop")); break; case "c": $serv = account::create(); break; case "d": gio::output("This action will irreversibly destroy your wallet and its contents"); if (gio::confirm("Are you sure you want to destroy your account")) { account::destroy(); } break; case "x": @$net->send('shutdown'); $net = null; if ($serv) { $ret = pclose($serv); } break 2; default: $inerr = 1; $msg = "Retry"; } if (!$inerr) { gio::output("\n\n\n"); } } if (isset($ret) && $ret != 0) { gio::output("An error occured while exiting..."); } gio::output(config::$exitMessage); sleep(3); }
function main($mainmsg) { gio::output(config::$walletMessage); $serv = popen("service.ecsh", 'r'); $net = new Gnet(); $inerr = 0; while (true) { if (!$inerr) { gio::output(); foreach ($mainmsg as $k => $v) { gio::output("{$k} - {$v}"); } gio::output(); $msg = "Enter your choice and press enter"; } $inerr = 0; $c = gio::input("{$msg}"); switch ($c) { case 1: $tid = gio::input("Enter the transaction Id"); $st = transaction::status($tid); if (!$st) { gio::output("Transaction reference '{$tid}' not found"); } else { $rpt['order id'] = $st['order id']; $rpt['order amount'] = $st['amount']; $rpt['from'] = $st['client']; $rpt['to'] = $st['merchant']; $rpt['transaction time'] = date(config::$timeFormat, $st['acknowledgement']['completed']); $rpt['description'] = $st['description']; $rpt['transaction reference'] = $st['acknowledgement']['id']; $rpt['response code'] = $st['acknowledgement']['status']; $rpt['response message'] = $st['acknowledgement']['message']; $rpt['ecash value tendered'] = $st['acknowledgement']['amount']; $rpt['ecash tendered'] = $st['acknowledgement']['ecashids']; gio::display($rpt); if (gio::confirm("Do you want to save the report to a file")) { $dest = gio::input("Enter full path to the file"); gio::display($rpt, $x); if (!gio::saverawfile(join("\r\n", $x), $dest)) { gio::output("Could not save the report to {$dest}"); } else { gio::output("Report successfully saved to {$dest}"); } unset($x); } } break; case 2: $maxallowed = 1000; $v = gio::input("What value of eCash do you wish to mine", "integer"); $n = gio::input("How many eCashes do you wish to mine", "integer"); $c = mine::countcoins($null); unset($null); if ($n > $maxallowed || $c + $n > $maxallowed) { $rem = $maxallowed - $c; gio::output("You can not mine above {$maxallowed} eCashes!"); gio::output("You can mine {$rem} more eCashes!"); } else { $res = mine::coins($n, $v); if ($res) { gio::output("You have successfully mined {$n} eCashes."); gio::output("Mining process took " . Tools::arrtostr($res, ", ")); } else { gio::output("Mining operation failed!"); } } break; case 3: $n = mine::countcoins($coins); gio::output("A total of {$n} eCash value is available"); if ($n && gio::confirm("Do you wish to see their Id's")) { foreach ($coins as $val => $ccs) { foreach ($ccs as $i => $coin) { gio::output("Ecash ID: {$i}"); foreach ($coin as $id => $c) { if ($id == "token") { $c = md5($c); } if ($id == "hash") { $c = sha1($c); } if ($id == "mined") { $c = date(config::$timeFormat, $c); } gio::output("{$id}=>{$c}"); } gio::output(); } } } break; case 4: $to = gio::input("Enter the wallet address"); $amt = gio::input("Enter the amount to give"); $res = account::give($to, $amt); break; case "x": $net->connect(config::$serviceAddress, intval(config::$servicePort)); $net->send("shutdown"); if ($serv) { $ret = pclose($serv); } break 2; default: $inerr = 1; $msg = "Retry"; } if (!$inerr) { gio::output("\n\n\n"); } } if (isset($ret) && $ret != 0) { gio::output("An error occured while exiting..."); } gio::output(config::$exitMessage); sleep(3); }
public static function certexchange($addr) { $d = Tools::address($addr); if (file_exists(GsonCrypt::getcert($d['bank']))) { return true; } $c = GsonCrypt::getcert(null, true); $m = Gmsg::create(Gmsg::prepare($c, 'exchangecert', $d['bank'])); $net = new Gnet(); $r = $net->connect($d['address'], intval($d['port'])); if (!$r) { return false; } $r = $net->send($m); if (!$r) { return false; } $r = Gmsg::extract($r); if (!$r) { return false; } if ($r['status']) { $resp = gio::saverawfile($r['cert'], GsonCrypt::getcert($d['bank'])); } return isset($resp) ? $resp : false; }
public static function notification($msg, $account) { $m = GsonCrypt::verify($msg, $account); if (!$m) { return array(0, "SRA did not verify bank's message as authentic"); } $m = Gmsg::extract($m); if (!$m || !is_array($m)) { return array(0, "SRA did not extract bank's message correctly"); } list($id, $req) = each($m); $rtime = time(); $client = Tools::address($req['client']); $merchant = Tools::address($req['merchant']); $coins = storage::load($client['srakey']); $pcoins = $req['coins']; $errorcode = 0; $amount = 0; $least = 0; $most = 0; $leastcoin = array(); $mostcoin = array(); $ecashrejected = array(); if (mine::checkcoins($pcoins)) { foreach ($pcoins as $id => $coin) { $pcval = $coin['value']; if (!array_key_exists($id, $coins[$pcval])) { $errorcode = $errorcode ? $errorcode : 2; $tmsg = "Already spent eCash detected"; $ecids[] = $id; if ($errorcode == 2) { $rejectedecash[] = $id; } } else { $secret = GsonCrypt::unseal($coin['secret']); if ($coins[$pcval][$id]['secret'] != $secret) { $errorcode = $errorcode ? $errorcode : 3; $tmsg = "Invalid secret detected"; $ecids[] = $id; if ($errorcode == 3) { $rejectedecash[] = $id; } } else { $secrets[$id] = Tools::makesecrets(); $pcoins[$id]['secret'] = GsonCrypt::seal($secrets[$id], $merchant['srakey']); $pcoins[$id]['transactioncount'] = intval($pcoins[$id]['transactioncount']) + 1; $amount += $pcoins[$id]['value']; if ($pcoins[$id]['value'] > $most) { $most = $pcoins[$id]['value']; $mostcoin['id'] = $id; $mostcoin['coin'] = $pcoins[$id]; } if ($pcoins[$id]['value'] < $least || $least == 0) { $least = $pcoins[$id]['value']; $leastcoin['id'] = $id; $leastcoin['coin'] = $pcoins[$id]; } $ecids[] = $id; unset($coins[$pcval][$id]); } } } $ecids = join(", ", array_values($ecids)); } else { $ecids = join(", ", array_keys($pcoins)); $errorcode = 1; $tmsg = "Invalid/Counterfeit eCash detected"; } if (!$errorcode && $amount < $req['amount']) { $errorcode = 4; $tmsg = sprintf("Additional %d value is required to complete the transaction", $req['amount'] - $amount); } if (!$errorcode && $amount > $req['amount']) { $diff = $amount - $req['amount']; $rem = $leastcoin['coin']['value'] - $diff; $chg = self::splitvalue($diff, false); $dval = self::splitvalue($rem, false); if (!$chg || !$dval) { $errorcode = 5; $tmsg = "SRA Internal Error while splitting tokens."; } else { $mycoin = storage::load(); $chgcoins = self::getcoins($chg, $client['srakey'], $secrets, $mycoin); $dvalcoins = self::getcoins($dval, $merchant['srakey'], $secrets, $mycoin); if (!$chgcoins || !$dvalcoins) { $errorcode = 6; $tmsg = "SRA Internal Error while splitting tokens"; } else { $leastcoin['coin']['secret'] = ""; $mycoin[$leastcoin['coin']['value']][$leastcoin['id']] = $leastcoin['coin']; foreach ($chgcoins as $id => $coin) { $cval = $coin['value']; $coin['secret'] = $secrets[$id]; $coins[$cval][$id] = $coin; } unset($pcoins[$leastcoin['id']]); } } } foreach ($pcoins as $id => $coin) { $val = $coin['value']; if (isset($coins[$val][$id])) { unset($coins[$val][$id]); } } if (isset($dvalcoins)) { $pcoins += $dvalcoins; } $req['coins'] = $pcoins; $tmsg = $errorcode ? $tmsg : "pending"; $tid = md5(uniqid(rand(), true)); $dtime = time(); $req['acknowledgement'] = array("id" => "{$tid}", "completed" => $dtime, "status" => "{$errorcode}", "message" => "{$tmsg}", "amount" => $amount, "ecashids" => "{$ecids}"); //if(isset($rejectedecash)&&!empty($ecashrejected)) $req['acknowledgement']['rejectedecash'] = join(", ",$rejectedecash); if (strtolower($client['bank']) != strtolower($merchant['bank'])) { $m = GsonCrypt::sign(Gmsg::create($req)); $merchant['response'] = Gmsg::create(Gmsg::Prepare($m, "acknowledgement", $merchant['bank'])); $net = new Gnet(); $c = $net->connect($merchant['address'], intval($merchant['port'])); if (!$c) { return array(0, "Merchant's bank is unreachable"); } $r = $net->send($merchant['response']); if (!$r) { $req['acknowledgement']['status'] = 10; $req['acknowledgement']['message'] = "Invalid response from merchant's bank"; } $r = Gmsg::extract(GsonCrypt::verify($r, $merchant['bank'])); if (!$r) { $req['acknowledgement']['status'] = 11; $req['acknowledgement']['message'] = "Invalid response from merchant's bank"; } else { if ($r['status']) { $old = storage::load($merchant['srakey']); if (!$old) { $old = array(); } foreach ($pcoins as $id => $coin) { $val = $coin['value']; $old[$val][$id] = $coin; } storage::save($old, $merchant['srakey']); storage::save($coins, $client['srakey']); if (isset($mycoin)) { storage::save($mycoin); } $req['acknowledgement']['message'] =& $r['response']; } else { $req['acknowledgement']['status'] = 12; $req['acknowledgement']['message'] = $r['response']; } } } else { if (!$errorcode) { $old = storage::load($merchant['srakey']); if (!$old) { $old = array(); } foreach ($pcoins as $id => $coin) { $coin['secret'] = $secrets[$id]; $val = $coin['value']; $old[$val][$id] = $coin; } storage::save($old, $merchant['srakey']); storage::save($coins, $client['srakey']); if (isset($mycoin)) { storage::save($mycoin); } } $status = $req['acknowledgement']['status'] ? 0 : 1; if ($status) { $req['acknowledgement']['message'] = "Payment was successful"; } } $status = $req['acknowledgement']['status'] ? 0 : 1; if (!$status) { unset($req['coins']); unset($req['change']); if ($errorcode <= 3) { $req['acknowledgement']['amount'] = 0; } } if ($status && isset($chgcoins)) { $req['change'] = $chgcoins; } $resp = $req; self::log($resp); return array($status, $req); }
private static function register() { if (self::isRegistered()) { return true; } $ba = Tools::address(gio::input("Enter the bank's address")); if (!$ba) { return false; } config::$bankAddress = $ba['address']; config::$bankPort = intval($ba['port']); $net = new Gnet(); $r = $net->send(Gmsg::create(Gmsg::prepare("", "register", config::$bankId))); $net = null; if (!$r) { return false; } $m = Gmsg::extract($r); if (!$m['status']) { return false; } if (gio::saverawfile($m['cert'], GsonCrypt::getcert($m['name'])) && gio::savetofile($m['cert'], GsonCrypt::getkey($m['name']))) { if (gio::savetofile($m['name'], config::$bankIdFile) && gio::savetofile(serialize(array(config::$bankAddress, config::$bankPort)), config::$walCfgFile)) { config::$bankId = $m['name']; config::$accountId = $m['account']; return true; } else { self::deregister(); return false; } } return false; }
public static function status($oid) { $m = GsonCrypt::sign(Gmsg::create(array("oid" => $oid))); if (!$m) { $status = 0; $res = "Unable to sign status report for '{$oid}'"; } else { $m = Gmsg::create(Gmsg::Prepare($m, "statusrequest", config::$bankId)); $net = new Gnet(); $r = $net->send($m); unset($net); if (!$r) { $status = 0; $res = "Unable to send status report for '{$oid}'"; } else { $v = GsonCrypt::verify($r, config::$bankId); if (!$v) { $status = 0; $res = "Unable to verify response from bank for '{$oid}'"; } else { $v = Gmsg::extract($v); if (!$v) { $status = 0; $res = "Unable to understand the response in relation to '{$oid}'"; } 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; }