2017-06-29 14:43:39 +00:00
|
|
|
<?php
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-06-29 14:43:39 +00:00
|
|
|
/**
|
2017-07-17 04:36:45 +00:00
|
|
|
* library.php
|
|
|
|
*
|
|
|
|
* Written using the JSON RPC specification -
|
|
|
|
* http://json-rpc.org/wiki/specification
|
|
|
|
*
|
|
|
|
* @author Kacper Rowinski <krowinski@implix.com>
|
|
|
|
* http://implix.com
|
|
|
|
* Modified to work with monero-rpc wallet by Serhack and cryptochangements
|
2017-06-29 14:43:39 +00:00
|
|
|
*/
|
2017-07-17 21:25:34 +00:00
|
|
|
class Monero_Library
|
2017-07-17 04:36:45 +00:00
|
|
|
{
|
2017-07-24 02:13:33 +00:00
|
|
|
protected $url = null, $is_debug = false, $parameters_structure = 'array';
|
2017-07-17 04:36:45 +00:00
|
|
|
protected $curl_options = array(
|
|
|
|
CURLOPT_CONNECTTIMEOUT => 8,
|
|
|
|
CURLOPT_TIMEOUT => 8
|
|
|
|
);
|
2017-12-28 00:41:36 +00:00
|
|
|
protected $host;
|
|
|
|
protected $port;
|
2017-07-17 04:36:45 +00:00
|
|
|
private $httpErrors = array(
|
|
|
|
400 => '400 Bad Request',
|
|
|
|
401 => '401 Unauthorized',
|
|
|
|
403 => '403 Forbidden',
|
|
|
|
404 => '404 Not Found',
|
|
|
|
405 => '405 Method Not Allowed',
|
|
|
|
406 => '406 Not Acceptable',
|
|
|
|
408 => '408 Request Timeout',
|
|
|
|
500 => '500 Internal Server Error',
|
|
|
|
502 => '502 Bad Gateway',
|
|
|
|
503 => '503 Service Unavailable'
|
|
|
|
);
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-12-28 00:41:36 +00:00
|
|
|
public function __construct($pHost, $pPort)
|
2017-07-17 04:36:45 +00:00
|
|
|
{
|
2017-09-06 11:49:27 +00:00
|
|
|
$this->validate(false === extension_loaded('curl'), 'The curl extension must be loaded to use this class!');
|
|
|
|
$this->validate(false === extension_loaded('json'), 'The json extension must be loaded to use this class!');
|
2017-12-28 00:41:36 +00:00
|
|
|
|
|
|
|
$this->host = $pHost;
|
|
|
|
$this->port = $pPort;
|
|
|
|
$this->url = $pHost . ':' . $pPort . '/json_rpc';
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
public function validate($pFailed, $pErrMsg)
|
2017-07-17 04:36:45 +00:00
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
if ($pFailed) {
|
|
|
|
echo $pErrMsg;
|
|
|
|
}
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 04:36:45 +00:00
|
|
|
public function setDebug($pIsDebug)
|
|
|
|
{
|
|
|
|
$this->is_debug = !empty($pIsDebug);
|
|
|
|
return $this;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
/* public function setParametersStructure($pParametersStructure)
|
|
|
|
{
|
|
|
|
if (in_array($pParametersStructure, array('array', 'object')))
|
|
|
|
{
|
|
|
|
$this->parameters_structure = $pParametersStructure;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new UnexpectedValueException('Invalid parameters structure type.');
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
} */
|
|
|
|
|
2017-07-17 04:36:45 +00:00
|
|
|
public function setCurlOptions($pOptionsArray)
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
if (is_array($pOptionsArray)) {
|
2017-07-17 04:36:45 +00:00
|
|
|
$this->curl_options = $pOptionsArray + $this->curl_options;
|
2017-09-21 22:11:47 +00:00
|
|
|
} else {
|
2017-09-14 19:07:53 +00:00
|
|
|
echo 'Invalid options type.';
|
2017-07-17 04:36:45 +00:00
|
|
|
}
|
|
|
|
return $this;
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
public function _print($json)
|
|
|
|
{
|
|
|
|
$json_encoded = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
|
echo $json_encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function address()
|
|
|
|
{
|
|
|
|
$address = $this->_run('getaddress');
|
|
|
|
return $address;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function _run($method, $params = null)
|
|
|
|
{
|
|
|
|
$result = $this->request($method, $params);
|
|
|
|
return $result; //the result is returned as an array
|
|
|
|
}
|
|
|
|
|
|
|
|
private function request($pMethod, $pParams)
|
2017-07-17 04:36:45 +00:00
|
|
|
{
|
|
|
|
static $requestId = 0;
|
|
|
|
// generating uniuqe id per process
|
|
|
|
$requestId++;
|
|
|
|
// check if given params are correct
|
|
|
|
$this->validate(false === is_scalar($pMethod), 'Method name has no scalar value');
|
2017-09-21 22:11:47 +00:00
|
|
|
// $this->validate(false === is_array($pParams), 'Params must be given as array');
|
2017-07-17 04:36:45 +00:00
|
|
|
// send params as an object or an array
|
|
|
|
//$pParams = ($this->parameters_structure == 'object') ? $pParams[0] : array_values($pParams);
|
|
|
|
// Request (method invocation)
|
|
|
|
$request = json_encode(array('jsonrpc' => '2.0', 'method' => $pMethod, 'params' => $pParams, 'id' => $requestId));
|
|
|
|
// if is_debug mode is true then add url and request to is_debug
|
|
|
|
$this->debug('Url: ' . $this->url . "\r\n", false);
|
|
|
|
$this->debug('Request: ' . $request . "\r\n", false);
|
|
|
|
$responseMessage = $this->getResponse($request);
|
|
|
|
// if is_debug mode is true then add response to is_debug and display it
|
|
|
|
$this->debug('Response: ' . $responseMessage . "\r\n", true);
|
|
|
|
// decode and create array ( can be object, just set to false )
|
|
|
|
$responseDecoded = json_decode($responseMessage, true);
|
|
|
|
// check if decoding json generated any errors
|
|
|
|
$jsonErrorMsg = $this->getJsonLastErrorMsg();
|
2017-09-21 22:11:47 +00:00
|
|
|
$this->validate(!is_null($jsonErrorMsg), $jsonErrorMsg . ': ' . $responseMessage);
|
2017-07-17 04:36:45 +00:00
|
|
|
// check if response is correct
|
|
|
|
$this->validate(empty($responseDecoded['id']), 'Invalid response data structure: ' . $responseMessage);
|
|
|
|
$this->validate($responseDecoded['id'] != $requestId, 'Request id: ' . $requestId . ' is different from Response id: ' . $responseDecoded['id']);
|
2017-09-21 22:11:47 +00:00
|
|
|
if (isset($responseDecoded['error'])) {
|
2017-07-17 04:36:45 +00:00
|
|
|
$errorMessage = 'Request have return error: ' . $responseDecoded['error']['message'] . '; ' . "\n" .
|
|
|
|
'Request: ' . $request . '; ';
|
2017-09-21 22:11:47 +00:00
|
|
|
if (isset($responseDecoded['error']['data'])) {
|
2017-07-17 04:36:45 +00:00
|
|
|
$errorMessage .= "\n" . 'Error data: ' . $responseDecoded['error']['data'];
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
$this->validate(!is_null($responseDecoded['error']), $errorMessage);
|
2017-07-17 04:36:45 +00:00
|
|
|
}
|
|
|
|
return $responseDecoded['result'];
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
protected function debug($pAdd, $pShow = false)
|
|
|
|
{
|
|
|
|
static $debug, $startTime;
|
|
|
|
// is_debug off return
|
|
|
|
if (false === $this->is_debug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// add
|
|
|
|
$debug .= $pAdd;
|
|
|
|
// get starttime
|
|
|
|
$startTime = empty($startTime) ? array_sum(explode(' ', microtime())) : $startTime;
|
|
|
|
if (true === $pShow and !empty($debug)) {
|
|
|
|
// get endtime
|
|
|
|
$endTime = array_sum(explode(' ', microtime()));
|
|
|
|
// performance summary
|
|
|
|
$debug .= 'Request time: ' . round($endTime - $startTime, 3) . ' s Memory usage: ' . round(memory_get_usage() / 1024) . " kb\r\n";
|
|
|
|
echo nl2br($debug);
|
|
|
|
// send output immediately
|
|
|
|
flush();
|
|
|
|
// clean static
|
|
|
|
$debug = $startTime = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:36:45 +00:00
|
|
|
protected function & getResponse(&$pRequest)
|
|
|
|
{
|
|
|
|
// do the actual connection
|
|
|
|
$ch = curl_init();
|
2017-09-21 22:11:47 +00:00
|
|
|
if (!$ch) {
|
2017-07-17 04:36:45 +00:00
|
|
|
throw new RuntimeException('Could\'t initialize a cURL session');
|
|
|
|
}
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->url);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $pRequest);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
|
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 04:36:45 +00:00
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
2017-09-21 22:11:47 +00:00
|
|
|
if (!curl_setopt_array($ch, $this->curl_options)) {
|
2017-07-17 04:36:45 +00:00
|
|
|
throw new RuntimeException('Error while setting curl options');
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-07-17 04:36:45 +00:00
|
|
|
// send the request
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
// check http status code
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
2017-09-21 22:11:47 +00:00
|
|
|
if (isset($this->httpErrors[$httpCode])) {
|
2017-09-14 19:07:53 +00:00
|
|
|
echo 'Response Http Error - ' . $this->httpErrors[$httpCode];
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-07-17 04:36:45 +00:00
|
|
|
// check for curl error
|
2017-09-21 22:11:47 +00:00
|
|
|
if (0 < curl_errno($ch)) {
|
2017-12-28 00:41:36 +00:00
|
|
|
echo '[ERROR] Failed to connect to monero-wallet-rpc at ' . $this->host . ' port '. $this->port .'</br>';
|
2017-07-17 04:36:45 +00:00
|
|
|
}
|
|
|
|
// close the connection
|
|
|
|
curl_close($ch);
|
|
|
|
return $response;
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
//prints result as json
|
|
|
|
|
2017-07-17 04:36:45 +00:00
|
|
|
function getJsonLastErrorMsg()
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
if (!function_exists('json_last_error_msg')) {
|
2017-07-17 04:36:45 +00:00
|
|
|
function json_last_error_msg()
|
|
|
|
{
|
|
|
|
static $errors = array(
|
2017-09-21 22:11:47 +00:00
|
|
|
JSON_ERROR_NONE => 'No error',
|
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
|
2017-07-17 04:36:45 +00:00
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
|
2017-09-21 22:11:47 +00:00
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
|
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error',
|
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
|
2017-07-17 04:36:45 +00:00
|
|
|
);
|
|
|
|
$error = json_last_error();
|
|
|
|
return array_key_exists($error, $errors) ? $errors[$error] : 'Unknown error (' . $error . ')';
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 04:36:45 +00:00
|
|
|
// Fix PHP 5.2 error caused by missing json_last_error function
|
2017-09-21 22:11:47 +00:00
|
|
|
if (function_exists('json_last_error')) {
|
2017-07-17 04:36:45 +00:00
|
|
|
return json_last_error() ? json_last_error_msg() : null;
|
2017-09-21 22:11:47 +00:00
|
|
|
} else {
|
2017-07-17 04:36:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-06-29 14:43:39 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
/*
|
|
|
|
* The following functions can all be called to interact with the monero rpc wallet
|
|
|
|
* They will majority of them will return the result as an array
|
|
|
|
* Example: $daemon->address(); where $daemon is an instance of this class, will return the wallet address as string within an array
|
|
|
|
*/
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function getbalance()
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
$balance = $this->_run('getbalance');
|
|
|
|
return $balance;
|
2017-07-17 21:25:34 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function getheight()
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
$height = $this->_run('getheight');
|
|
|
|
return $height;
|
2017-07-17 21:25:34 +00:00
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function incoming_transfer($type)
|
|
|
|
{
|
|
|
|
$incoming_parameters = array('transfer_type' => $type);
|
|
|
|
$incoming_transfers = $this->_run('incoming_transfers', $incoming_parameters);
|
|
|
|
return $incoming_transfers;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
public function get_transfers($input_type, $input_value)
|
|
|
|
{
|
2017-07-17 21:25:34 +00:00
|
|
|
$get_parameters = array($input_type => $input_value);
|
|
|
|
$get_transfers = $this->_run('get_transfers', $get_parameters);
|
|
|
|
return $get_transfers;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function view_key()
|
|
|
|
{
|
|
|
|
$query_key = array('key_type' => 'view_key');
|
|
|
|
$query_key_method = $this->_run('query_key', $query_key);
|
|
|
|
return $query_key_method;
|
2017-09-21 22:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function make_integrated_address($payment_id)
|
|
|
|
{
|
|
|
|
$integrate_address_parameters = array('payment_id' => $payment_id);
|
|
|
|
$integrate_address_method = $this->_run('make_integrated_address', $integrate_address_parameters);
|
|
|
|
return $integrate_address_method;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
|
|
|
/* A payment id can be passed as a string
|
|
|
|
A random payment id will be generatd if one is not given */
|
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function split_integrated_address($integrated_address)
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
if (!isset($integrated_address)) {
|
2017-07-17 21:25:34 +00:00
|
|
|
echo "Error: Integrated_Address mustn't be null";
|
2017-09-21 22:11:47 +00:00
|
|
|
} else {
|
|
|
|
$split_params = array('integrated_address' => $integrated_address);
|
|
|
|
$split_methods = $this->_run('split_integrated_address', $split_params);
|
|
|
|
return $split_methods;
|
2017-07-17 21:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function make_uri($address, $amount, $recipient_name = null, $description = null)
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
// If I pass 1, it will be 0.0000001 xmr. Then
|
2017-07-17 21:25:34 +00:00
|
|
|
$new_amount = $amount * 100000000;
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
$uri_params = array('address' => $address, 'amount' => $new_amount, 'payment_id' => '', 'recipient_name' => $recipient_name, 'tx_description' => $description);
|
|
|
|
$uri = $this->_run('make_uri', $uri_params);
|
|
|
|
return $uri;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function parse_uri($uri)
|
|
|
|
{
|
|
|
|
$uri_parameters = array('uri' => $uri);
|
|
|
|
$parsed_uri = $this->_run('parse_uri', $uri_parameters);
|
|
|
|
return $parsed_uri;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function transfer($amount, $address, $mixin = 4)
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
$new_amount = $amount * 1000000000000;
|
2017-07-17 21:25:34 +00:00
|
|
|
$destinations = array('amount' => $new_amount, 'address' => $address);
|
|
|
|
$transfer_parameters = array('destinations' => array($destinations), 'mixin' => $mixin, 'get_tx_key' => true, 'unlock_time' => 0, 'payment_id' => '');
|
|
|
|
$transfer_method = $this->_run('transfer', $transfer_parameters);
|
|
|
|
return $transfer_method;
|
|
|
|
}
|
2017-09-21 22:11:47 +00:00
|
|
|
|
2017-07-17 21:25:34 +00:00
|
|
|
public function get_payments($payment_id)
|
|
|
|
{
|
2017-09-21 22:11:47 +00:00
|
|
|
$get_payments_parameters = array('payment_id' => $payment_id);
|
|
|
|
$get_payments = $this->_run('get_payments', $get_payments_parameters);
|
|
|
|
return $get_payments;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_bulk_payments($payment_id, $min_block_height)
|
|
|
|
{
|
|
|
|
$get_bulk_payments_parameters = array('payment_id' => $payment_id, 'min_block_height' => $min_block_height);
|
|
|
|
$get_bulk_payments = $this->_run('get_bulk_payments', $get_bulk_payments_parameters);
|
|
|
|
return $get_bulk_payments;
|
|
|
|
}
|
|
|
|
}
|