Newer
Older
beardedwarrior
committed
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
/**
* Class jsonRPCClient
* JSON 2.0 RPC Client for cryptocurrency wallet
*/
class jsonRPCClient implements Contracts\WalletManager
beardedwarrior
committed
/** @var string */
beardedwarrior
committed
/** @var string */
private $password = 'test2';
/** @var string */
private $url = 'http://127.0.0.1:28080/json_rpc';
beardedwarrior
committed
/** @var Client|null */
private $client;
beardedwarrior
committed
* JsonRPCClient constructor.
beardedwarrior
committed
* @param null $client
public function __construct($options, $client = null)
beardedwarrior
committed
{
$this->username = $options['username'] ?? $this->username;
$this->password = $options['password'] ?? $this->password;
$this->url = $options['url'] ?? $this->url;
beardedwarrior
committed
if (empty($client)) {
$client = new Client([
'base_uri' => $this->url,
'headers' => [
'Content-Type' => 'application/json',
]
beardedwarrior
committed
]);
}
beardedwarrior
committed
$this->client = $client;
}
beardedwarrior
committed
* Gets the balance
*
* @return int the overall value after inputs unlock
beardedwarrior
committed
public function balance() : int
{
$response = $this->request('get_balance');
return $response['balance'];
}
beardedwarrior
committed
* Gets the unlocked balance
*
* @return int the spendable balance
beardedwarrior
committed
public function unlockedBalance() : int
{
$response = $this->request('get_balance');
return $response['unlocked_balance'];
}
beardedwarrior
committed
* Gets the primary address
beardedwarrior
committed
* @return string wallets primary address
beardedwarrior
committed
public function address() : string
beardedwarrior
committed
$response = $this->request('get_address');
return $response['address'];
beardedwarrior
committed
* Gets the current block height
beardedwarrior
committed
* @return int block height
beardedwarrior
committed
public function blockHeight() : int
beardedwarrior
committed
$response = $this->request('get_height');
return $response['height'];
beardedwarrior
committed
* Creates a new integrated address
beardedwarrior
committed
* @return array ['integrated_address', 'payment_id']
beardedwarrior
committed
public function createIntegratedAddress() : array
beardedwarrior
committed
$response = $this->request('make_integrated_address');
return $response;
beardedwarrior
committed
* Gets any incoming transactions
beardedwarrior
committed
* @return array
beardedwarrior
committed
public function incomingTransfers() : array
beardedwarrior
committed
$response = $this->request('get_transfers', ['pool' => true, 'in' => true]);
return $response;
beardedwarrior
committed
* Checks for any payments made to the paymentIds
beardedwarrior
committed
* @param array $paymentIds list of payment ids to be searched for
* @param int $minHeight the lowest block the search should start with
beardedwarrior
committed
* @return array payments received since min block height with a payment id provided
beardedwarrior
committed
public function payments($paymentIds, $minHeight) : array
beardedwarrior
committed
$response = $this->request('get_bulk_payments', ['payment_ids' => $paymentIds, 'min_block_height' => $minHeight]);
beardedwarrior
committed
return $response;
beardedwarrior
committed
/**
* creates a uri for easier wallet parsing
*
* @param string $address address comprising of primary, sub or integrated address
* @param string $paymentId payment id when not using integrated addresses
* @param int $amount atomic amount requested
*
* @return string the uri string which can be used to generate a QR code
*/
public function createUri($address, $paymentId = null, $amount = null) : string
beardedwarrior
committed
$response = $this->request('make_uri', ['address' => $address, 'amount' => $amount, 'payment_id' => $paymentId]);
beardedwarrior
committed
return $response['uri'];
/**
* creates a random 64 char payment id
*
* @return string
*/
public function generatePaymentId(): string
{
return bin2hex(openssl_random_pseudo_bytes(32));
}
beardedwarrior
committed
* Sets up the request data body
beardedwarrior
committed
* @param string $method name of the rpc command
* @param array $params associative array of variables being passed to the method
beardedwarrior
committed
* @return false|string will return a json string or false
beardedwarrior
committed
private function preparePayload($method, $params)
beardedwarrior
committed
'id' => '0',
'method' => $method,
'params' => $params,
beardedwarrior
committed
return json_encode($payload);
beardedwarrior
committed
* @param string $method name of the rpc command
* @param array $params associative array of variables being passed to the method
*
* @return mixed the rpc query result
beardedwarrior
committed
* @throws \RuntimeException
beardedwarrior
committed
protected function request(string $method, array $params = [])
beardedwarrior
committed
$payload = $this->preparePayload($method, $params);
try {
$response = $this->client->request('POST', '',[
'auth' => [$this->username, $this->password, 'digest'],
'body' => $payload,
'headers' => [
'Content-Type' => 'application/json',
]
]);
$body = $response->getBody();
} catch (GuzzleException $exception) {
Log::error($exception);
throw new \RuntimeException('Connection to node unsuccessful');
beardedwarrior
committed
$result = json_decode((string) $body, true);
if (isset($result['error'])) {
beardedwarrior
committed
throw new \RuntimeException($result['error']['message']);
beardedwarrior
committed
return $result['result'];