refring/monero-rpc-php

一个用于使用Monero守护进程RPC和钱包RPC API的现代强类型库

0.7.3 2024-02-21 12:10 UTC

README

Latest Stable Version Tests PHPStan codecov PHP Version Require

使用现代PHP编写的Monero守护进程和钱包RPC客户端库。

功能

  • 实现了Monero钱包和守护进程RPC方法
  • 支持钱包和守护进程RPC服务器的认证
  • 完全强类型和启用strict_types
  • 最小依赖
  • PSR-18兼容,因此可以使用不同的HTTP客户端库

安装

您可以使用Composer安装此包,在此期间最低稳定性必须设置为dev

composer require refring/monero-rpc-php

当您的项目尚未提供HTTP客户端时,您也应该需要它。

可以使用不同的HTTP客户端

guzzle

composer require guzzlehttp/guzzle
其他HTTP客户端

symfony http客户端

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

设置

创建客户端

对于钱包RPC客户端

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

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

守护进程RPC客户端

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

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

使用认证

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

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

通过代理连接

配置代理具体取决于HTTP客户端库。

以下是一个使用socks5代理的Symfony Http Client示例

$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();

注入记录器

客户端构建器还支持注入记录器和/或HTTP客户端

$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();

使用

创建钱包和账户

// 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);

测试

该项目具有单元测试和集成测试,单元测试可以使用composer test:unit运行

要运行集成测试,您需要dockerdocker compose,或者您可以在自己的机器上运行monerodmonero-wallet-rpc

如果您已安装docker stack,请转到tests文件夹并运行docker compose up。注意,守护进程将在端口18081上运行,而monero-wallet-rpc将在端口18083上运行。

之后,运行composer test:integration以运行集成测试。

路线图

  • 更多集成测试
  • 改进文档并添加示例

贡献

请参阅CONTRIBUTING.md

变更日志

请参阅CHANGELOG.md

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

致谢

  • monero-rpc-rs - 本项目的一些部分受到了该项目的启发。
  • monero-php - 多年来为PHP生态系统提供Monero库,感谢您!
  • Monero - 感谢所有参与的人!