BOLT 11 Lightning Network invoice encoding/decoding library
npm install @node-lightning/invoice

A Node.js invoice encoding/decoding library for the Lightning Network. This library is BOLT #11 compliant.
Decode an invoice
``javascript`
let invoice = require("@node-lightning/invoice");
let input =
"lnbc2500u1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsxqzpuaztrnwngzn3kdzw5hydlzf03qdgm2hdq27cqv3agm2awhz5se903vruatfhq77w3ls4evs3ch9zw97j25emudupq63nyw24cg27h2rspfj9srp";
let result = invoice.decode(input);
/*
Invoice {
network: 'bc',
valueMsat: 250000000,
valueSat: 250000,
timestamp: 1496314658,
paymentHash:
desc: '1 cup coffee',
shortDesc: '1 cup coffee',
expiry: 60,
fields:
[
{ type: 1, value:
{ type: 13, value: '1 cup coffee' },
{ type: 6, value: 60 }
],
unknownFields: [],
signature: {
r:
s:
recoveryFlag: 1
},
pubkey:
hashData:
usedSigRecovery: true
}
*/
Encode an invoice
`javascript
let invoice = require("@node-lightning/invoice");
let privKey = Buffer.from(
"e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734",
"hex",
);
let invoice = new Invoice();
invoice.network = "bc";
invoice.valueSat = 250000;
invoice.timestamp = 1496314658;
invoice.paymentHash = "0001020304050607080900010203040506070809000102030405060708090102";
invoice.shortDesc = "1 cup coffee";
invoice.expiry = 60;
result = invoice.encode(invoice, privKey);
console.log(result);
/*
lnbc2500u1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsxqzpuaztrnwngzn3kdzw5hydlzf03qdgm2hdq27cqv3agm2awhz5se903vruatfhq77w3ls4evs3ch9zw97j25emudupq63nyw24cg27h2rspfj9srp
*/
`
#### .decode(string): Invoice
Decodes a bech32 encoded lightning invoice. Exceptions are thrown for invalid invoices.
#### .encode(Invoice): string
Encodes an invoice into a bech32 encode lightning invoice.
#### Class - Invoice
Represents a payment invoice that contains the following properties
- network: string - network prefix (Bitcoin Mainnet bc, Bitcoin Testnet tb, Bitcoin Regression crt, Bitcoin Simnet smvalueSat: String
- - value in satoshivalueMsat: String
- - value in millisatoshitimestamp: Int
- - timestamp of the invoicefields: Array
- - raw fields that are known in BOLT 11unknownFields: Array
- - raw fields that are unknown in BOLT 11signature: Object
- - signature in the format: { r: Buffer(32), s: Buffer(32), recoveryFlag: Int } that was used to sign the invoicepubkey: Object
- - pubkey in the format: { x: Buffer(32), y: Buffer(32) } that was recovered from the signature or provided in an n fieldhashData: Buffer(32)
- - SHA256 of the data that was signedexpiry: Int
- - expiry time in seconds, defaults to 3600 (per BOLT 11)paymentHash: Buffer(32)
- - SHA256 of the payment_preimage provided in return for paymentdesc: String
- - automatically sets the description correctlyshortDesc: String
- - short descriptionhashDesc: Buffer(var)
- - hash of the long descriptionpayeeNode: Buffer(33)
- : optional pubkey of the payee nodeminFinalCltvExpiry: Int
- : min_final_cltv_expiry to use for the last node, defaults to 9 (per BOLT 11)fallbackAddresses: Array
- : list of on-chain addresses to fall back if payment fails in the format { version: Int, address: Buffer(var) }, supports version 0, 17, 18 addressesroutes: Array
- : list of routes that should be used in the format { pubkey: Buffer(33), short_channel_id: Buffer(8), fee_base_msat: Int, fee_proportional_millionths: Int, cltv_expiry_delta: Int }addFallbackAddress(address: string)
- adds a P2PKH or P2SH address in base58check or bech32 encoding.addRoute(route: [Route])
- adds a new private route with each sub-route being of the form { pubkey: Buffer(33), short_channel_id: Buffer(8), fee_base_msat: Int, fee_proportional_millionths: Int, cltv_expiry_delta: Int }signature
- the signature for the invoice in the format { r: Buffer(32), s: Buffer(32), recoveryFlag: Int }pubkey: Buffer(33)
- the compressed public key recovered from the signature, or the pubkey from payeeNode when supplied.hashData: Buffer(32)
- the 256-bit hash of the data used to generate the signature signature.usedSigRecovery: boolean` indicates if signature recovery was used on the invoice, which indicates that an external comparison of the recovered pubkey must be performed against a known pubkey.
-
Refer to the LN Tools contributing guide.