Skip to content
Snippets Groups Projects
jsonRPCClient.php 5.61 KiB
Newer Older
  • Learn to ignore specific revisions
  • beardedwarrior's avatar
    beardedwarrior committed
    <?php
    
    namespace Monero;
    
    
    use GuzzleHttp\Client;
    use GuzzleHttp\Exception\GuzzleException;
    use Illuminate\Support\Facades\Log;
    
    beardedwarrior's avatar
    beardedwarrior committed
    
    /**
     * Class jsonRPCClient
     * JSON 2.0 RPC Client for cryptocurrency wallet
     */
    
    class jsonRPCClient implements Contracts\WalletManager
    
    beardedwarrior's avatar
    beardedwarrior committed
    {
    
    
        private $username = 'test2';
    
    beardedwarrior's avatar
    beardedwarrior committed
    
    
        private $password = 'test2';
    
        /** @var string  */
        private $url = 'http://127.0.0.1:28080/json_rpc';
    
    beardedwarrior's avatar
    beardedwarrior committed
    
        /**
    
         * @param array $options
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
        public function __construct($options, $client = null)
    
            $this->username = $options['username'] ?? $this->username;
            $this->password = $options['password'] ?? $this->password;
            $this->url = $options['url'] ?? $this->url;
    
    
                    'base_uri' => $this->url,
                    'headers' => [
                        'Content-Type' => 'application/json',
                    ]
    
    beardedwarrior's avatar
    beardedwarrior committed
    
        /**
    
         * Gets the balance
         *
         * @return int the overall value after inputs unlock
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
        public function balance() : int
        {
            $response = $this->request('get_balance');
            return $response['balance'];
        }
    
    beardedwarrior's avatar
    beardedwarrior committed
    
        /**
    
         * Gets the unlocked balance
         *
         * @return int the spendable balance
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
        public function unlockedBalance() : int
        {
            $response = $this->request('get_balance');
            return $response['unlocked_balance'];
        }
    
    beardedwarrior's avatar
    beardedwarrior committed
    
        /**
    
    beardedwarrior's avatar
    beardedwarrior committed
         *
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
    beardedwarrior's avatar
    beardedwarrior committed
        {
    
            $response = $this->request('get_address');
            return $response['address'];
    
    beardedwarrior's avatar
    beardedwarrior committed
        }
    
        /**
    
    beardedwarrior's avatar
    beardedwarrior committed
         *
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
    beardedwarrior's avatar
    beardedwarrior committed
        {
    
            $response = $this->request('get_height');
            return $response['height'];
    
    beardedwarrior's avatar
    beardedwarrior committed
        }
        /**
    
    beardedwarrior's avatar
    beardedwarrior committed
         *
    
         * @return array ['integrated_address', 'payment_id']
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
    beardedwarrior's avatar
    beardedwarrior committed
        {
    
            $response = $this->request('make_integrated_address');
            return $response;
    
    beardedwarrior's avatar
    beardedwarrior committed
         *
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
    beardedwarrior's avatar
    beardedwarrior committed
        {
    
            $response = $this->request('get_transfers', ['pool' => true, 'in' => true]);
    
            return $response;
    
    beardedwarrior's avatar
    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's avatar
    beardedwarrior committed
         *
    
         * @return array    payments received since min block height with a payment id provided
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
        public function payments($paymentIds, $minHeight) : array
    
    beardedwarrior's avatar
    beardedwarrior committed
        {
    
            $response = $this->request('get_bulk_payments', ['payment_ids' => $paymentIds, 'min_block_height' => $minHeight]);
    
    beardedwarrior's avatar
    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's avatar
    beardedwarrior committed
        {
    
            $response = $this->request('make_uri', ['address' => $address, 'amount' => $amount, 'payment_id' => $paymentId]);
    
    beardedwarrior's avatar
    beardedwarrior committed
    
    
        /**
         * creates a random 64 char payment id
         *
         * @return string
         */
        public function generatePaymentId(): string
        {
            return bin2hex(openssl_random_pseudo_bytes(32));
        }
    
    
    beardedwarrior's avatar
    beardedwarrior committed
        /**
    
    beardedwarrior's avatar
    beardedwarrior committed
         *
    
         * @param string    $method name of the rpc command
         * @param array     $params associative array of variables being passed to the method
    
    beardedwarrior's avatar
    beardedwarrior committed
         *
    
         * @return false|string will return a json string or false
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
    beardedwarrior's avatar
    beardedwarrior committed
        {
            $payload = [
                'jsonrpc' => '2.0',
    
    beardedwarrior's avatar
    beardedwarrior committed
            ];
    
         * Send off request to rpc server
    
    beardedwarrior's avatar
    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's avatar
    beardedwarrior committed
         *
    
    beardedwarrior's avatar
    beardedwarrior committed
         */
    
        protected function request(string $method, array $params = [])
    
    beardedwarrior's avatar
    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's avatar
    beardedwarrior committed
            }
    
            $result = json_decode((string) $body, true);
            if (isset($result['error'])) {
    
    beardedwarrior's avatar
    beardedwarrior committed
    
    
                throw new \RuntimeException($result['error']['message']);
    
    beardedwarrior's avatar
    beardedwarrior committed
            }