tmwclaxton / laravel-bitcoin-module
Laravel Bitcoin 模块
v1.0.3
2024-06-25 14:27 UTC
Requires
- php: ^8.2
- ext-decimal: *
- guzzlehttp/guzzle: ^7.2
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.15
README
Laravel Bitcoin 模块 是一个用于处理加密货币比特币的 Laravel 扩展包。您可以使用它创建描述符钱包、生成地址、跟踪当前余额、收集交易历史、组织网站上的支付接受,并自动化出款。
您可以通过邮件与我联系,以帮助将支付接受集成到您的项目中。
示例
创建描述符钱包
$name = 'my-wallet'; $password = 'password for encrypt wallet files'; $title = 'My First Wallet'; $node = Bitcoin::createNode('localhost', 'LocalHost', '127.0.0.1'); $wallet = Bitcoin::createWallet($node, $name, $password, $title);
使用描述符导入描述符钱包
$name = 'my-wallet'; $password = 'password for encrypt wallet files'; $descriptions = json_decode('DESCRIPTORS JSON', true); $title = 'My First Wallet'; $node = Bitcoin::createNode('localhost', 'LocalHost', '127.0.0.1'); $wallet = Bitcoin::importWallet($node, $name, $descriptions, $password, $title);
创建地址
$wallet = BitcoinWallet::firstOrFail(); $title = 'My address title'; $address = Bitcoin::createAddress($wallet, AddressType::BECH32, $title);
验证地址
$address = '....'; $node = BitcoinNode::firstOrFail(); $addressType = Bitcoin::validateAddress($node, $address); if( $addressType === null ) { die('Address is not valid!'); } var_dump($addressType); // Enum value of AddressType
从钱包发送所有 BTC
$wallet = BitcoinWallet::firstOrFail(); $address = 'to_address'; $txid = Bitcoin::sendAll($wallet, $address); echo 'TXID: '.$txid;
从钱包发送 BTC
$wallet = BitcoinWallet::firstOrFail(); $address = 'to_address'; $amount = 0.001; $txid = Bitcoin::send($wallet, $address, $amount); echo 'TXID: '.$txid;
安装
您可以通过 composer 安装此包
composer require mollsoft/laravel-bitcoin-module
之后,您可以使用以下命令运行安装程序
php artisan bitcoin:install
并运行迁移
php artisan migrate
在 app 目录中注册 Service Provider 和 Facade,编辑 config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([ ..., \Mollsoft\LaravelBitcoinModule\BitcoinServiceProvider::class, ])->toArray(), 'aliases' => Facade::defaultAliases()->merge([ ..., 'Bitcoin' => \Mollsoft\LaravelBitcoinModule\Facades\Bitcoin::class, ])->toArray(),
在文件 app/Console/Kernel
中方法 schedule(Schedule $schedule)
添加
$schedule->command('bitcoin:sync')
->everyMinute()
->runInBackground();
命令
扫描交易并更新余额
> php artisan bitcoin:sync
扫描交易并更新钱包的余额
> php artisan bitcoin:sync-wallet {wallet_id}
WebHook
您可以设置一个 WebHook,当检测到新的 BTC 存款时将被调用。
在文件 config/bitcoin.php 中,您可以设置参数
'webhook_handler' => \Mollsoft\LaravelBitcoinModule\WebhookHandlers\EmptyWebhookHandler::class,
示例 WebHook 处理程序
class EmptyWebhookHandler implements WebhookHandlerInterface { public function handle(BitcoinWallet $wallet, BitcoinAddress $address, BitcoinDeposit $transaction): void { Log::error('Bitcoin Wallet '.$wallet->name.' new transaction '.$transaction->txid.' for address '.$address->address); } }
要求
本版本支持以下版本的 PHP。
- PHP 8.2 及以下版本
- PHP 扩展:Decimal。