PHP code example of nova-carnivore / bolt11-php

1. Go to this page and download the library: Download nova-carnivore/bolt11-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nova-carnivore / bolt11-php example snippets


use Nova\Bitcoin\Bolt11\Decoder;

$invoice = Decoder::decode('lnbc2500u1pvjluez...');

$invoice->satoshis;          // 250000
$invoice->millisatoshis;     // '250000000'
$invoice->network;           // Network::Bitcoin
$invoice->timestamp;         // 1496314658
$invoice->payeeNodeKey;      // '03e7156ae33b...'

// Access tags
$invoice->getPaymentHash();   // hex string
$invoice->getDescription();   // 'string'
$invoice->getPaymentSecret(); // hex string
$invoice->isExpired();        // bool
$invoice->getTag('payment_hash'); // Tag object

use Nova\Bitcoin\Bolt11\Encoder;
use Nova\Bitcoin\Bolt11\Network;
use Nova\Bitcoin\Bolt11\Tag;

$unsigned = Encoder::encode(
    network: Network::Bitcoin,
    satoshis: 1000,
    tags: [
        Tag::paymentHash('0001020304050607...'),
        Tag::paymentSecret('1111111111111111...'),
        Tag::description('test payment'),
        Tag::expiry(3600),
    ],
);

use Nova\Bitcoin\Bolt11\Signer;

$signed = Signer::sign($unsigned, $privateKeyHex);

$signed->paymentRequest; // 'lnbc10u1...'
$signed->complete;       // true
$signed->payeeNodeKey;   // compressed public key

use Nova\Bitcoin\Bolt11\Helpers;

Helpers::satToHrp(250000);       // '2500u'
Helpers::hrpToSat('2500u');      // '250000'
Helpers::millisatToHrp('1000');  // '10n'
Helpers::hrpToMillisat('10n');   // '1000'

Tag::paymentHash(string $hex): Tag
Tag::paymentSecret(string $hex): Tag
Tag::description(string $text): Tag
Tag::descriptionHash(string $hex): Tag
Tag::payeeNodeKey(string $hex): Tag
Tag::expiry(int $seconds): Tag
Tag::minFinalCltvExpiry(int $blocks): Tag
Tag::fallbackAddress(int $code, string $addressHash): Tag
Tag::routeHint(array $hops): Tag
Tag::featureBits(FeatureBits $bits): Tag
Tag::metadata(string $hex): Tag

enum Network: string {
    case Bitcoin = 'bc';
    case Testnet = 'tb';
    case Signet  = 'tbs';
    case Regtest = 'bcrt';
}

enum TagType: int {
    case PaymentHash = 1;
    case PaymentSecret = 16;
    case Description = 13;
    // ... and more
}

enum Multiplier: string {
    case Milli = 'm';
    case Micro = 'u';
    case Nano  = 'n';
    case Pico  = 'p';
}

use Nova\Bitcoin\Bolt11\Exception\{
    Bolt11Exception,           // Base exception
    InvalidInvoiceException,   // Malformed invoice
    InvalidChecksumException,  // Bad bech32 checksum
    InvalidSignatureException, // Signature issues
    InvalidAmountException,    // Bad amount format
    UnsupportedNetworkException, // Unknown network
};

try {
    $invoice = Decoder::decode($paymentRequest);
} catch (InvalidChecksumException $e) {
    // Bad checksum
} catch (InvalidInvoiceException $e) {
    // Malformed invoice
} catch (Bolt11Exception $e) {
    // Any BOLT 11 error
}
bash
composer 
bash
# Install dependencies
composer install

# Run tests
vendor/bin/phpunit

# Static analysis
vendor/bin/phpstan analyse
vendor/bin/psalm

# Code style check
vendor/bin/php-cs-fixer fix --dry-run --diff

# Fix code style
vendor/bin/php-cs-fixer fix