rgb-lninvoice-decoder
v1.0.5
Published
JavaScript library to parse RGB Lightning Network invoices compatible with rgb-lightning-node decodelninvoice API
Maintainers
Readme
RGB Lightning Invoice Decoder
A JavaScript library for parsing RGB Lightning Network invoices, compatible with the rgb-lightning-node decodelninvoice API.
Features
- ✅ Supports RGB Lightning Invoice parsing
- ✅ Compatible with
rgb-lightning-nodeAPI format - ✅ Supports multiple module formats (ES Modules, CommonJS, UMD, IIFE)
- ✅ Works in both Node.js and browser environments
- ✅ TypeScript type support (coming soon)
- ✅ Comprehensive test coverage
Installation
# Using yarn
yarn add rgb-lninvoice-decoder
# Using npm
npm install rgb-lninvoice-decoderUsage
Node.js (ES Modules)
// Using the primary function names
import { decodeRGBLNInvoice, isValidRGBLNInvoice } from 'rgb-lninvoice-decoder';
// Or using compatibility aliases
import { decodeLNInvoice, isValidLNInvoice } from 'rgb-lninvoice-decoder';
const invoice = "lnbcrt30u1p5zlun0...";
// Validate invoice
const isValid = isValidRGBLNInvoice(invoice);
console.log('Invoice is valid:', isValid);
// Decode invoice
const decoded = decodeRGBLNInvoice(invoice);
console.log('Decoded invoice:', decoded);Node.js (CommonJS)
const { decodeRGBLNInvoice, isValidRGBLNInvoice } = require('rgb-lninvoice-decoder');
const invoice = "lnbcrt30u1p5zlun0...";
// Validate invoice
const isValid = isValidRGBLNInvoice(invoice);
console.log('Invoice is valid:', isValid);
// Decode invoice
const decoded = decodeRGBLNInvoice(invoice);
console.log('Decoded invoice:', decoded);Browser (UMD)
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/lib/index.js"></script>
<script src="https://unpkg.com/rgb-lninvoice-decoder/dist/rgb-lninvoice-decoder.umd.js"></script>
</head>
<body>
<script>
const invoice = "lnbcrt30u1p5zlun0...";
// Validate invoice
const isValid = RGBLNInvoiceDecoder.isValidRGBLNInvoice(invoice);
console.log('Invoice is valid:', isValid);
// Decode invoice
const decoded = RGBLNInvoiceDecoder.decodeRGBLNInvoice(invoice);
console.log('Decoded invoice:', decoded);
</script>
</body>
</html>React/Vue/Other Frontend Frameworks
import { decodeRGBLNInvoice, isValidRGBLNInvoice } from 'rgb-lninvoice-decoder';
function MyComponent() {
const handleDecodeInvoice = (invoice) => {
try {
if (isValidRGBLNInvoice(invoice)) {
const decoded = decodeRGBLNInvoice(invoice);
console.log('Decoded:', decoded);
}
} catch (error) {
console.error('Error:', error.message);
}
};
return (
// Your component JSX
);
}API Reference
decodeRGBLNInvoice(invoice: string): Object
Parses an RGB Lightning Invoice and returns structured data.
Parameters:
invoice(string): The BOLT 11 Lightning Invoice string
Returns:
{
amt_msat: number, // Amount in millisatoshis
expiry_sec: number, // Expiry time in seconds
timestamp: number, // Timestamp of creation
asset_id: string | null, // RGB asset ID
asset_amount: number | null, // Quantity of the RGB asset
payment_hash: string, // Payment hash
payment_secret: string, // Payment secret
payee_pubkey: string, // Payee's public key
network: string // Network type
}isValidRGBLNInvoice(invoice: string): boolean
Validates the format of a Lightning Invoice.
Parameters:
invoice(string): The BOLT 11 Lightning Invoice string to validate
Returns:
boolean:trueif the invoice format is valid,falseotherwise
Development
Building the library
# Install dependencies
yarn install
# Build the library
yarn build
# Development mode (watches for file changes)
yarn devTesting
# Run tests
yarn test
# Test ES Modules example
node examples/node-esm.js
# Test CommonJS example
node examples/node-cjs.cjsBuild Output
After building, the dist/ directory will contain the following files:
rgb-lninvoice-decoder.mjs- ES Modules formatrgb-lninvoice-decoder.cjs- CommonJS formatrgb-lninvoice-decoder.umd.js- UMD format (for browsers)rgb-lninvoice-decoder.iife.js- IIFE format (for direct browser use)- Corresponding source map files
Dependencies
- bech32 - Bech32 encoding/decoding library
License
MIT License
Contribution
Feel free to open Issues and Pull Requests!
Changelog
v1.0.2
- Added compatibility exports:
decodeLNInvoiceandisValidLNInvoiceas aliases - Fixed issue with direct imports from dist directory
v1.0.1
- Added support for direct imports from dist directory
v1.0.0
- Initial release
- Support for RGB Lightning Invoice parsing
- Multi-module format support
- Node.js and browser compatibility
