refring/monero-rpc-php

A modern fully strong-typed library for using the Monero daemon rpc and wallet rpc APIs

0.7.3 2024-02-21 12:10 UTC

README

Latest Stable Version Tests PHPStan codecov PHP Version Require

Monero daemon and wallet RPC client library written in modern PHP.

Features

  • Implements Monero wallet and daemon rpc methods
  • Support authentication for the wallet and daemon rpc servers
  • Fully strongly typed and strict_types enabled
  • Minimal dependencies
  • PSR-18 compatible, so different http client libraries can be used

Installation

You can install the package with Composer, at this this time minimum-stability has to be set to dev:

composer require refring/monero-rpc-php

When your project does not have a http client available yet, you should require one as well.

Different http clients can be used:

guzzle

composer require guzzlehttp/guzzle
Other http clients

symfony http client

composer require symfony/http-client psr/http-client nyholm/psr7

buzz

composer require kriswallsmith/buzz nyholm/psr7

php-http/curl-client

composer php-http/curl-client

Setup

Creating a client

For the wallet rpc client:

$walletClient = (new \RefRing\MoneroRpcPhp\ClientBuilder('http://127.0.0.1:18081/json_rpc'))
    ->buildWalletClient();

echo $walletClient->getVersion()->version;

Daemon rpc client:

$daemonClient = (new \RefRing\MoneroRpcPhp\ClientBuilder('http://127.0.0.1:18081/json_rpc'))
    ->buildDaemonClient();

echo $daemonClient->getVersion()->version;

Using authentication

$daemonClient = (new \RefRing\MoneroRpcPhp\ClientBuilder('http://127.0.0.1:18081/json_rpc'))
    ->withAuthentication('foo', 'bar')
    ->buildDaemonClient();

echo $daemonClient->getVersion()->version;

Connecting through a proxy

Configuring a proxy is specific to the http client library.

Below is a Symfony Http Client example for a socks5 proxy:

$httpClient = new Psr18Client(new CurlHttpClient([
    'http_version' => '2.0',
    'proxy' => 'socks5://username:password@127.0.0.1:9999',
]));

$daemonClient = (new \RefRing\MoneroRpcPhp\ClientBuilder('http://examplenode/json_rpc'))
    ->withHttpClient($httpClient)
    ->buildDaemonClient();

Injecting a logger

The client builder also supports injecting a logger and/or a http client:

$httpClient = new \Symfony\Component\HttpClient\Psr18Client();
$logger = new \Psr\Log\NullLogger();

$daemonClient = (new \RefRing\MoneroRpcPhp\ClientBuilder('http://127.0.0.1:18081/json_rpc'))    
    ->withHttpClient($httpClient)
    ->withLogger($logger)
    ->buildDaemonClient();

Usage

Creating a wallet and account

// Try to create a wallet, or open it when it already exists
try{
    $walletClient->createWallet('testwallet', 'English', 'password');
} catch (WalletExistsException $e) {
    $walletClient->openWallet('testwallet', 'password');
} catch(MoneroRpcException $e) {
    echo 'An error occured: '.$e->getMessage();
}

$baseAddressData = $walletClient->getAddress();
$subAddressData = $walletClient->createAddress();

printf("BaseAddress: %s\nSubAddress: %s\n", $baseAddressData->address, $subAddressData->address);

// Create another account
$newAccountData = $walletClient->createAccount('another account');
$newAccountSubAddress = $walletClient->createAddress($newAccountData->accountIndex);

printf("Account %d\nBaseAddress: %s\nSubAddress: %s", $newAccountData->accountIndex, $newAccountData->address, $newAccountSubAddress->address);

Testing

The project has unit tests and integration tests, the unit tests can be run using composer test:unit

To run the integration tests, you'll need docker and docker compose or you could run monerod and monero-wallet-rpc on your own.

If you have the docker stack installed, go to the tests folder and run docker compose up. Note that the daemon will run on port 18081 and monero-wallet-rpc will run on port 18083.

After that, run composer test:integration to run the integration tests.

Roadmap

  • More integration tests
  • Improve documentation and add examples

Contributing

See CONTRIBUTING.md

Changelog

See CHANGELOG.md

License

The MIT License (MIT). Please see License File for more information.

Acknowledgments

  • monero-rpc-rs - Parts of this project served as inspiration.
  • monero-php - Thanks for providing the php ecosystem with a Monero library during all these years!
  • Monero - Thanks to everybody involved!