uran1980/php-electrum-api

Electrum JSONRPC-API 的 PHP 封装

v1.7.6 2022-07-17 12:29 UTC

This package is auto-updated.

Last update: 2024-09-17 17:15:55 UTC


README

Packagist GitHub code size in bytes

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,完成此步骤后,您可以继续操作

composer require padrio/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()
    ));
}