basic cryptonote wallet rpc class/converter

allow to read balance and do queries via the console

Note: The db is not ready to handle both wallet and daemons ip/ports
      I assume the wallet is on local host and the daemon on another machine(ip).
      For now, they should use the same port (on 2 different ips so).
This commit is contained in:
Tanguy Pruvot 2016-09-11 19:56:21 +02:00
parent 06c377bb3c
commit 95fe244984
3 changed files with 460 additions and 0 deletions

View file

@ -3,4 +3,5 @@
require_once('easybitcoin.php');
require_once('json-rpc.php');
require_once('ethereum.php');
require_once('xmr-rpc.php');
require_once('wallet-rpc.php');

View file

@ -6,10 +6,12 @@ class WalletRPC {
public $type = 'Bitcoin';
protected $rpc;
protected $rpc_wallet;
// cache
protected $account;
protected $accounts;
protected $coin;
protected $info;
protected $height = 0;
@ -30,6 +32,13 @@ class WalletRPC {
$this->account = empty($coin->account) ? $coin->master_wallet : $coin->account;
$this->rpc = new Ethereum($coin->rpchost, $coin->rpcport);
break;
case 'XMR':
$this->type = 'CryptoNote';
$this->rpc = new CryptoRPC($coin->rpchost, $coin->rpcport, $coin->rpcuser, $coin->rpcpasswd);
// for now, assume the wallet in on localhost, and daemon set in the db
$this->rpc_wallet = new CryptoRPC("127.0.0.1", $coin->rpcport, $coin->rpcuser, $coin->rpcpasswd);
$this->coin = $coin;
break;
default:
$this->type = 'Bitcoin';
$this->rpc = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport, $url);
@ -134,6 +143,170 @@ class WalletRPC {
}
}
// XMR & BBR
else if ($this->type == 'CryptoNote')
{
// convert some rpc methods used by yiimp (and handle wallet rpc, very limited)
switch ($method) {
case "getinfo":
$res = $this->rpc->getinfo();
$res["blocks"] = arraySafeVal($res,"height");
$res["connections"] = arraySafeVal($res,"white_peerlist_size");
$balances = $this->rpc_wallet->getbalance();
$res["balance"] = arraySafeVal($balances,"unlocked_balance") / 1e12;
$res["pending"] = arraySafeVal($balances,"balance",0) / 1e12 - $res["balance"];
$ver = arraySafeVal($res,"mi");
$res["version"] = (int) sprintf("%02d%02d%02d%02d", // BBR
arraySafeVal($ver,"ver_major"), arraySafeVal($ver,"ver_minor"), arraySafeVal($ver,"ver_revision"), arraySafeVal($ver,"build_no"));
$this->error = $this->rpc_wallet->error.$this->rpc->error;
unset($res["mi"]);
break;
case "getmininginfo":
$res = $this->rpc->getinfo();
$res["networkhps"] = arraySafeVal($res,"current_network_hashrate_50");
unset($res["current_network_hashrate_50"]);
unset($res["current_network_hashrate_350"]);
unset($res["mi"]);
unset($res["grey_peerlist_size"]);
unset($res["white_peerlist_size"]);
unset($res["incoming_connections_count"]);
unset($res["outgoing_connections_count"]);
unset($res["max_net_seen_height"]);
unset($res["synchronization_start_height"]);
unset($res["transactions_cnt_per_day"]);
unset($res["transactions_volume_per_day"]);
unset($res["mi"]);
$data = $this->rpc->getlastblockheader();
$header = arraySafeVal($data,"block_header");
$res["reward"] = (double) arraySafeVal($header,"reward") / 1e12;
$this->error = $this->rpc->error;
break;
case "getnetworkinfo":
$res = $this->rpc->getinfo();
$res["connections"] = arraySafeVal($res,"white_peerlist_size");
$res["networkhps"] = arraySafeVal($res,"current_network_hashrate_50");
unset($res["current_network_hashrate_50"]);
unset($res["current_network_hashrate_350"]);
unset($res["mi"]);
$this->error = $this->rpc->error;
break;
case 'getaccountaddress':
$res = $this->rpc_wallet->getaddress();
$res = objSafeVal($res, "address");
$this->error = $this->rpc_wallet->error;
break;
case 'getblocktemplate':
$gbt_params = array(
"wallet_address" => $this->coin->master_wallet,
"reserve_size" => 8, // extra data
"alias_details" => null,
);
$res = $this->rpc->getblocktemplate($gbt_params);
$data = $this->rpc->getlastblockheader();
$header = arraySafeVal($data,"block_header");
$res["coinbase"] = (double) arraySafeVal($header,"reward") / 1e4;
$res["reward"] = (double) arraySafeVal($header,"reward") / 1e12;
$this->error = $this->rpc->error;
break;
case "getbalance":
$res = $this->rpc_wallet->getbalance();
$this->error = $this->rpc_wallet->error;
$res = arraySafeVal($res,"unlocked_balance") / 1e12;
break;
case "getbalances":
$res = $this->rpc_wallet->getbalance();
$this->error = $this->rpc_wallet->error;
break;
case 'listtransactions':
$res = $this->rpc_wallet->get_bulk_payments();
$this->error = $this->rpc_wallet->error;
break;
case 'getaddress':
$res = $this->rpc_wallet->getaddress();
$this->error = $this->rpc_wallet->error;
break;
case 'get_bulk_payments':
$res = $this->rpc_wallet->get_bulk_payments();
$this->error = $this->rpc_wallet->error;
break;
case 'get_payments':
$named_params = array(
"payment_id"=>arraySafeVal($params, 0)
);
$res = $this->rpc_wallet->get_payments($named_params);
$this->error = $this->rpc_wallet->error;
break;
case 'get_transfers': // deprecated ?
$res = $this->rpc_wallet->get_transfers();
$this->error = $this->rpc_wallet->error;
break;
case 'incoming_transfers': // deprecated ?
$res = $this->rpc_wallet->incoming_transfers();
$this->error = $this->rpc_wallet->error;
break;
case 'sendtoaddress':
// 3rd param is "payment id"
$destination = array(
"address"=>arraySafeVal($params, 0),
"amount"=> (double) arraySafeVal($params, 1) * 1e12,
);
$named_params = array(
"mixin"=>0,
"destinations" => array((object)$destination),
"payment_id" => arraySafeVal($params, 2),
);
$res = $this->rpc_wallet->transfer($named_params);
$this->error = $this->rpc_wallet->error;
break;
case 'sendmany':
$destinations = array();
foreach ($params as $dest) {
foreach ($dest as $addr => $amount) {
$data = array("amount" => (double) $amount * 1e12, "address"=>$addr);
$destinations[] = (object) $data;
}
}
$named_params = array(
"mixin"=>arraySafeVal($params, 0, 0),
"destinations"=>$destinations,
);
$res = $this->rpc_wallet->transfer($named_params);
$this->error = $this->rpc_wallet->error;
break;
case 'transfer':
case 'transfer_original':
$destination = array(
"address"=> arraySafeVal($params, 1),
"amount"=> (double) arraySafeVal($params, 2, 0) * 1e12,
// also: "fee" "unlock_time"
);
$destinations = array();
$destinations[] = (object)$destination;
$named_params = array(
"mixin" => arraySafeVal($params, 0, 0),
"destinations" => $destinations,
"payment_id" => arraySafeVal($params, 3),
);
$res = $this->rpc_wallet->transfer($named_params);
$this->error = $this->rpc_wallet->error;
break;
case 'reset':
$res = $this->rpc_wallet->reset();
$this->error = $this->rpc_wallet->error;
break;
case 'store':
$res = $this->rpc_wallet->store();
$this->error = $this->rpc_wallet->error;
break;
default:
// default to daemon
$res = $this->rpc->__call($method,$params);
$this->error = $this->rpc->error;
}
return $res;
}
// Bitcoin RPC
$res = $this->rpc->__call($method,$params);
$this->error = $this->rpc->error;

View file

@ -0,0 +1,286 @@
<?php
/*
if (!function_exists('debuglog')) {
function debuglog($x) { echo "$x\n"; }
}
*/
class CryptoRPC
{
// Configuration options
private $username;
private $password;
private $proto;
private $host;
private $port;
private $url;
// Information and debugging
public $status;
public $error;
public $raw_response;
public $response;
private $id = 0;
function __construct($host='localhost', $port=18081, $username='', $password='')
{
$this->proto = 'http';
$this->host = $host;
$this->port = $port;
$this->url = 'json_rpc';
$this->username = $username;
$this->password = $password;
}
function __call($method, $params=array())
{
$this->status = null;
$this->error = null;
$this->raw_response = null;
$this->response = null;
switch ($method) {
case 'getheight':
case 'getinfo':
case 'gettransactions':
case 'start_mining':
case 'stop_mining':
return $this->rpcget($method, $params);
case 'sendrawtransaction':
return $this->rpcpost($method, $params);
// binary stuff
case 'getblocks':
case 'get_o_indexes':
case 'getrandom_outs':
case 'get_tx_pool':
case 'set_maintainers_info':
case 'check_keyimages':
return $this->rpcget($method.'.bin', $params);
// queries with named params
case 'getblocktemplate':
case 'get_payments':
if (count($params) == 1) {
// __call put all params in array $params
$pop = array_shift($params);
if (is_object($pop) || is_array($pop)) {
$params = (object) $pop;
}
}
break;
case 'transfer':
case 'transfer_original':
if (is_string($params[0])) { // assume json
//debuglog("params: ".$params[0]);
$params = array(json_decode($params[0]));
}
else if (is_array($params) && count($params) == 1) {
// __call put all params in array $params
$pop = array_shift($params);
if (is_object($pop) || is_array($pop)) {
$params = (object) $pop;
}
}
break;
}
//debuglog(json_encode($params));
$data = array();
$data['method'] = $method;
$data['params'] = $params;
$data['id'] = $this->id++;
$data['jsonrpc'] = '2.0';
// Build the cURL session, to check later {$this->username}:{$this->password}@
$curl = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$this->url}");
$options = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
);
curl_setopt_array($curl, $options);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
//debuglog(json_encode($data));
// Execute the request and decode to an array
$this->raw_response = curl_exec($curl);
$this->response = json_decode($this->raw_response, TRUE);
// If the status is not 200, something is wrong
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// If there was no error, this will be an empty string
$curl_error = curl_error($curl);
curl_close($curl);
if (!empty($curl_error)) {
$this->error = $curl_error;
}
if (isset($this->response['error']) && $this->response['error']) {
$this->error = strtolower($this->response['error']['message']);
}
elseif ($this->status != 200) {
// If didn't return a nice error message, we need to make our own
switch ($this->status) {
case 400:
$this->error = 'HTTP_BAD_REQUEST';
break;
case 401:
$this->error = 'HTTP_UNAUTHORIZED';
break;
case 403:
$this->error = 'HTTP_FORBIDDEN';
break;
case 404:
$this->error = 'HTTP_NOT_FOUND';
break;
}
}
if ($this->error) {
return FALSE;
}
return $this->response['result'];
}
// these methods use other urls
function rpcget($url, $params=array())
{
$url = "{$this->proto}://{$this->host}:{$this->port}/{$url}";
if (!empty($params)) {
$url = "?ts=".time();
foreach ($params as $key => $val) {
$url .= '&'.urlencode($key).'='.urlencode($val);
}
}
$curl = curl_init($url);
$options = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_POST => false,
);
curl_setopt_array($curl, $options);
$this->raw_response = curl_exec($curl);
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// If there was no error, this will be an empty string
$curl_error = curl_error($curl);
curl_close($curl);
//debuglog($this->response);
if (!empty($curl_error)) {
$this->error = $curl_error;
}
if (isset($this->response['error']) && $this->response['error']) {
$this->error = strtolower($this->response['error']['message']);
}
elseif ($this->status != 200) {
// If didn't return a nice error message, we need to make our own
switch ($this->status) {
case 400:
$this->error = 'HTTP_BAD_REQUEST';
break;
case 401:
$this->error = 'HTTP_UNAUTHORIZED';
break;
case 403:
$this->error = 'HTTP_FORBIDDEN';
break;
case 404:
$this->error = 'HTTP_NOT_FOUND';
break;
}
} else {
// getinfo
$this->response = json_decode($this->raw_response, TRUE);
}
return $this->response;
}
// sendrawtransaction (untested yet)
function rpcpost($url, $params=array())
{
$curl = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$url}");
$options = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
);
curl_setopt_array($curl, $options);
$pop = array_pop($params);
if (is_object($pop) || is_array($pop)) {
$params = (object) $pop;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
$this->raw_response = curl_exec($curl);
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// If there was no error, this will be an empty string
$curl_error = curl_error($curl);
curl_close($curl);
if (!empty($curl_error)) {
$this->error = $curl_error;
}
if (isset($this->response['error']) && $this->response['error']) {
$this->error = strtolower($this->response['error']['message']);
}
elseif ($this->status != 200) {
// If didn't return a nice error message, we need to make our own
switch ($this->status) {
case 400:
$this->error = 'HTTP_BAD_REQUEST';
break;
case 401:
$this->error = 'HTTP_UNAUTHORIZED';
break;
case 403:
$this->error = 'HTTP_FORBIDDEN';
break;
case 404:
$this->error = 'HTTP_NOT_FOUND';
break;
}
} else {
// getinfo
$this->response = json_decode($this->raw_response, TRUE);
}
return $this->response;
}
}