public static function request($params = array()) { if (empty($params)) { $params['description'] = gio::input('Transaction Description'); $params['amount'] = gio::input("Amount", "integer"); $params['merchant'] = account::address(); $params['client'] = gio::input("Enter the client's account address"); $params['order id'] = md5(uniqid(rand(), true)); $params['time'] = time(); } $dest = Tools::address($params['client']); if (!$dest) { $status = 0; $res = "The client address is invalid"; } else { $m = GsonCrypt::sign(Gmsg::create($params)); if (!$m) { $status = 0; $res = "Unable to sign message to " . $params['client']; } else { $m = Gmsg::create(Gmsg::Prepare($m, "paymentrequest", config::$bankId)); $net = new Gnet(); $r = $net->send($m); unset($net); if (!$r) { $status = 0; $res = "Unable to send the message"; } else { $v = GsonCrypt::verify($r, config::$bankId); if (!$v) { $status = 0; $res = "Unable to verify response from bank"; } else { $v = Gmsg::extract($v); if (!$v) { $status = 0; $res = "Unable to understand the response"; } else { $status = $v['status']; $res = $v['response']; } } } } } return array($status, $res); }
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 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; } } } } } }
function main($mainmsg) { gio::output(config::$walletMessage); $net = new Gnet(); $inerr = 0; while (true) { $inmsg = array(); if (account::isCreated()) { $inmsg["d"] = "Destroy Account"; $dmsg = $mainmsg + $inmsg; } else { $inmsg["c"] = "Create Wallet Account [Merchant/Client]"; $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("Your wallet address is: {$a}"); break; case 2: $m = transaction::request(); gio::output($m[1]); break; case 3: transaction::grant(); break; case 4: $n = account::balance($coins); gio::output("Total value of eCash units: {$n}"); 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 5: $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, $f); break; case 6: transaction::clearallrequests(); break; case 7: account::merckey(gio::input("Enter the name of the file to write to your desktop")); break; case 8: $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::ecash($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 "c": 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 = null; 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); }
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 configserver() { $dint = config::$networkInterface; $dport = config::$walletPort; $rdint = config::$remoteAddress; $rdport = config::$remotePort; $int = 0; $port = 0; $rint = 0; $rport = 0; while (true) { $int = gio::input("Enter the interface ip address to listen on [Default: {$dint}]"); if (empty($int)) { $int = "{$dint}"; } if (filter_var($int, FILTER_VALIDATE_IP)) { break; } $int = 0; } while ($int && true) { $port = intval(gio::input("Enter the port address to listen on [Default: {$dport}]")); if (empty($port)) { $port = $dport; } if ($port > 0 && $port < 65536) { break; } $port = 0; } while ($port && true) { $rint = gio::input("Enter the interface ip address of the SRA [Default: {$rdint}]"); if (empty($rint)) { $rint = "{$rdint}"; } if (filter_var($rint, FILTER_VALIDATE_IP)) { break; } $rint = 0; } while ($port && $rint && true) { $rport = intval(gio::input("Enter the port of the SRA to connect to [Default: {$rdport}]]")); if (empty($rport)) { $rport = $rdport; } if ($rport > 0 && $rport < 65536) { break; } $rport = 0; } if ($int && $port && $rint && $rport) { if (gio::savetofile(serialize(array($int, $port, $rint, $rport)), config::$walCfgFile)) { return true; } } return false; }
public static function keygen(&$userid, $info = false) { $userid = !$userid ? config::$accountId : $userid; if (!$userid) { return false; } $dn = is_array($info) ? $info : array("countryName" => strtoupper(gio::input("Country code", "string")), "stateOrProvinceName" => strtoupper(gio::input("State code", "string")), "localityName" => gio::input("City", "string"), "organizationName" => gio::input("Your Name/Your Company Name in Full", "string"), "organizationalUnitName" => 'Digicoin', "commonName" => config::$bankId, "emailAddress" => gio::input("Contact Email Address", "string")); $privkeypass = config::$privateKeyPassword; if (!self::cryptoInstalled()) { gio::log("... Could not generate cryptographic keys for {$userid} ...", E_USER_ERROR); return false; } gio::log("Generating cryptographic keys for {$userid}...", VERBOSE); try { $privkey = @openssl_pkey_new(self::$keyOpts); $privateKey = ""; $csr = @openssl_csr_new($dn, $privkey, self::$keyOpts); if ($csr) { openssl_csr_export_to_file($csr, self::getcert($userid)); openssl_pkey_export($privkey, $privatekey, $privkeypass, self::$keyOpts); gio::savetofile($privatekey, self::getkey($userid, true), config::$privateKeyFileMode); gio::savetofile($userid, config::$accountIdFile); config::$accountId = $userid; } else { return false; } } catch (Exception $e) { gio::log("Error while generating cryptographic keys for {$userid}: " . $e->message, E_USER_ERROR); return false; } gio::log("... Done generating cryptographic keys for {$userid}", VERBOSE); return true; }
public static function configsvc() { $cfg[0] = gio::input("Enter the interface to run on [127.0.0.1]"); if (empty($cfg[0])) { $cfg[0] = "127.0.0.1"; } $cfg[1] = gio::input("Enter the port to run on [11000]"); if (empty($cfg[1])) { $cfg[1] = 11000; } gio::savetofile(serialize($cfg), config::$svcCfgFile); }
public static function grant() { $net = new Gnet(); $m['account'] = config::$accountId; $m = GsonCrypt::sign(Gmsg::create($m)); $m = Gmsg::create(Gmsg::Prepare($m, "pullrequests", config::$bankId)); $net = new Gnet(); $r = $net->send($m); unset($net); if (!$r) { $status = 0; $res = "Unable to send the message"; } else { $v = GsonCrypt::verify($r, config::$bankId); if (!$v) { $status = 0; $res = "Unable to verify response from bank"; } else { $v = Gmsg::extract($v); if (!$v) { $status = 0; $res = "Unable to understand the response"; } else { $status = $v['status']; $res = $v['response']; } } } if (!$status) { gio::output($res); } else { $res = Gmsg::extract($res); if (!is_array($res)) { $status = 0; gio::output("Invalid response from bank"); } else { if (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::getcoins($amount); if (!$coins) { gio::output("Isufficient balance"); break; } $req['coins'] = $coins; $r = self::sendnotification($id, $req); $status = $r[0]; gio::output($r[1]); if ($status) { account::spentcoins($coins); } if ($status || !$status && !gio::confirm("Do you want to try again")) { break; } } gio::output(); break; } } } } } } } }