mollsoft/laravel-bitcoin-module

v1.0.7 2023-12-14 14:45 UTC

This package is auto-updated.

Last update: 2024-10-14 16:42:35 UTC


README

Pest Laravel Expectations

Latest Version on Packagist Php Version Php Version Total Downloads

Website Telegram

Laravel Bitcoin Module is a Laravel package for work with cryptocurrency Bitcoin. You can create descriptor wallets, generate addresses, track current balances, collect transaction history, organize payment acceptance on your website, and automate outgoing transfers.

You can contact me for help in integrating payment acceptance into your project.

Examples

Create Descriptor Wallet:

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

Import Descriptor Wallet using descriptors:

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

Create address:

$wallet = BitcoinWallet::firstOrFail();
$title = 'My address title';

$address = Bitcoin::createAddress($wallet, AddressType::BECH32, $title);

Validate address:

$address = '....';

$node = BitcoinNode::firstOrFail();
$addressType = Bitcoin::validateAddress($node, $address);
if( $addressType === null ) {
    die('Address is not valid!');
} 

var_dump($addressType); // Enum value of AddressType

Send all BTC from wallet:

$wallet = BitcoinWallet::firstOrFail();
$address = 'to_address';

$txid = Bitcoin::sendAll($wallet, $address);

echo 'TXID: '.$txid;

Send BTC from wallet:

$wallet = BitcoinWallet::firstOrFail();
$address = 'to_address';
$amount = 0.001;

$txid = Bitcoin::send($wallet, $address, $amount);

echo 'TXID: '.$txid;

Installation

You can install the package via composer:

composer require mollsoft/laravel-bitcoin-module

After you can run installer using command:

php artisan bitcoin:install

And run migrations:

php artisan migrate

Register Service Provider and Facade in app, edit config/app.php:

'providers' => ServiceProvider::defaultProviders()->merge([
    ...,
    \Mollsoft\LaravelBitcoinModule\BitcoinServiceProvider::class,
])->toArray(),

'aliases' => Facade::defaultAliases()->merge([
    ...,
    'Bitcoin' => \Mollsoft\LaravelBitcoinModule\Facades\Bitcoin::class,
])->toArray(),

In file app/Console/Kernel in method schedule(Schedule $schedule) add

$schedule->command('bitcoin:sync')
    ->everyMinute()
    ->runInBackground();

Commands

Scan transactions and update balances:

> php artisan bitcoin:sync

Scan transactions and update balances for wallet:

> php artisan bitcoin:sync-wallet {wallet_id}

WebHook

You can set up a WebHook that will be called when a new incoming BTC deposit is detected.

In file config/bitcoin.php you can set param:

'webhook_handler' => \Mollsoft\LaravelBitcoinModule\WebhookHandlers\EmptyWebhookHandler::class,

Example WebHook handler:

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

Requirements

The following versions of PHP are supported by this version.

  • PHP 8.2 and older
  • PHP Extensions: Decimal.