boomuo / php-electrum-api
Electrum JSONRPC-API的PHP包装器
dev-master
2023-05-23 01:24 UTC
Requires
- php: ^8.0.2
- laminas/laminas-hydrator: ^4.13.0
This package is auto-updated.
Last update: 2024-09-10 22:46:39 UTC
README
php-electrum-api - Electrum库
Licence: GPL-3.0
Author: Pascal Krason <p.krason@padr.io>
Language: PHP 5.6-7.1
请注意,这个库尚未完成,但可以用于生产。到目前为止,我只实现了最常见的API调用。如果您认为我遗漏了什么,只需创建一个问题或分叉项目。
设置Electrum
首先,您需要设置一个新的Electrum钱包。根据您的操作系统,在Electrum下载页面上遵循说明。安装成功后,您需要通过键入来设置rpc端口
electrum setconfig rpcport 7777
electrum setconfig rpcuser "username"
electrum setconfig rpcpassword "password"
然后我们可以创建一个默认的钱包,别忘了记录下您生成的种子,如果您想将来恢复它,这是必要的。
electrum create
现在我们可以继续以守护进程模式启动Electrum。
electrum daemon start
由于某些新版本的Electrum要求您在启动时手动加载钱包。
electrum daemon load_wallet
要求
在PHP端没有太多要求,您只需要至少PHP 5.6和安装了curl扩展。然后您可以通过Composer继续操作,它将为您完成其他所有工作。
安装
首先您需要安装Composer,在您完成此操作后,您可以继续操作。
composer require boomuo/php-electrum-api
然后您可以直接包含自动加载器并开始使用库。
// Include composer autoloader require_once 'vendor/autoloader.php';
示例
基本示例
一个非常基本的用法示例。每个API调用都有自己的请求对象。您只需创建一个并执行它。
$method = new \Electrum\Request\Method\Version(); try { $response = $method->execute(); } catch(\Exception $exception) { die($exception->getMessage()); } $response->getVersion();
创建新钱包
创建默认钱包
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $wallet = new \Electrum\Request\Method\Wallet\CreateWallet($client); $response = $wallet->execute();
此代码类似于以下命令
$ electrum create
您也可以创建更多具有自定义名称的钱包,指定新钱包的标志。
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $wallet = new \Electrum\Request\Method\Wallet\CreateWallet($client); $response = $wallet->execute(['wallet_path' => '~/.electrum/wallets/your_wallet']);
此代码类似于以下命令
$ electrum create -w ~/.electrum/wallets/your_wallet
响应将是
[ 'seed' => 'wallet seed', 'path' => 'path where wallet file is stored', 'msg' => 'Please keep your seed in a safe place; if you lose it, you will not be able to restore your wallet.', ];
加载钱包
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $load_wallet = new \Electrum\Request\Method\Wallet\LoadWallet($client); $load_wallet->execute(['wallet_path' => '~/.electrum/wallets/your_wallet']);
列出钱包
获取所有加载的钱包列表
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $list_wallets = new \Electrum\Request\Method\Wallet\ListWallets($client); $list_wallets->execute();
获取新地址
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $wallet = new \Electrum\Request\Method\Payment\AddRequest($client); $tx = $wallet->execute(); echo $tx->getAddress();
为钱包创建新地址
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $newAddress = new \Electrum\Request\Method\Address\CreateNewAddress($client); $newAddress->execute(['wallet' => '~/.electrum/wallets/your_wallet']);
进行新付款
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'user', 'password'); $method = new \Electrum\Request\Method\Payment\PayTo($client); $method->setDestination('BTC4ddress1234'); //Destination parameter is the address where we'll send the btc $method->setAmount(1); //send 1 BTC = 10k usd $tx = $method->execute(); //$tx returns the transaction ID of the payment, this is still not sent to the blockchain /** * @param array ['password' => '<password>'] * If the Electrum wallet is encrypted with a password use the following execute method instead * The previous one will return an error of "Password required" */ //$tx = $method->execute(['password' => 'myPass123']); // $broadcast = new Electrum\Request\Method\Payment\Broadcast($client); $broadcast->setTransaction($tx); $result = $broadcast->execute(); //broadcasts payment to the blockchain echo $result; A payment has been made
自定义客户端配置
每个请求/方法都接受一个Electrum\Client
实例作为参数,该实例替换默认实例。如果需要设置自定义配置参数(如另一个主机名或端口),则自定义实例可能很有用。
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'username', 'password'); $method = new \Electrum\Request\Method\Version($client); try { $response = $method->execute(); } catch (\Exception $exception) { die($exception->getMessage()); } $response->getVersion();
高级异常处理
处理异常很简单。您可以捕获两种类型的异常,这表明是请求还是响应错误。
$method = new \Electrum\Request\Method\Version(); try { $response = $method->execute(); } catch (\Electrum\Request\Exception\BadRequestException $exception) { die(sprintf( 'Failed to send request: %s', $exception->getMessage() )); } catch(\Electrum\Response\Exception\BadResponseException $exception) { die(sprintf( 'Electrum-Client failed to respond correctly: %s', $exception->getMessage() )); }