lightning-backends

Node.js module to integrate with various Lightning Network node software and service providers.
List of Backends
The following list includes all the Lightning Network node software and service providers which are supported:
Installation
Add to your application via npm:
npm install lightning-backends
Usage
const { checkBackend, prepareBackend } = require('lightning-backends');
const backend = 'lnd';
const config = {
hostname: '127.0.0.1:8080',
protocol: 'https',
cert: '/path/to/lnd/tls.cert',
macaroon: '/path/to/lnd/admin.macaroon',
};
const ln = prepareBackend(backend, config);
ln.payInvoice(invoice).then(result => {
console.log('payInvoice OK', { result });
}).catch(error => {
console.error('payInvoice FAILED:', { error });
});
ln.addInvoice(21000).then(result => {
console.log('addInvoice OK', { result });
}).catch(error => {
console.error('addInvoice FAILED:', { error });
});
ln.getInvoiceStatus(paymentHash).then(result => {
console.log('getInvoiceStatus OK', { result });
}).catch(error => {
console.error('getInvoiceStatus FAILED:', { error });
});
ln.getBalance().then(result => {
console.log('getBalance OK', { result });
}).catch(error => {
console.error('getBalance FAILED:', { error });
});
ln.openChannel(remoteId, localAmt, pushAmt, makePrivate).then(result => {
console.log('openChannel OK', { result });
}).catch(error => {
console.error('openChannel FAILED:', { error });
});
checkBackend(backend, config, { method: 'payInvoice' }).then(result => {
console.log('Backend configuration check', result.ok ? 'OK' : 'FAILED', { result });
});
Backend Configuration Options
Lightning Network Daemon (lnd):
- hostname - The host and port of the node's REST API. Examples:
127.0.0.1:8080
esdlkvxdkwxz6yqs6rquapg4xxt4pt4guj24k75pdnquo5nau135ugyd.onion
- protocol - "http" or "https" - Must be "https" unless an onion address is used.
- baseUrl - The full URL and path of the node's REST API. Can be used instead of the hostname and protocol options above. Examples:
https://127.0.0.1:8080/custom/path
http://esdlkvxdkwxz6yqs6rquapg4xxt4pt4guj24k75pdnquo5nau135ugyd.onion/custom/path
- cert - The TLS certificate of the lnd node. Examples:
/path/to/lnd/tls.cert - As a file path.
{ data: 'STRING_UTF8_ENCODED' } - As a string.
{ data: Buffer.from('STRING_UTF8_ENCODED', 'utf8') } - As a buffer.
- macaroon - The authentication macaroon to access the lnd node's REST API. Examples:
/path/to/lnd/admin.macaroon - As a file path.
{ data: 'STRING_HEX_ENCODED' } - As a string.
{ data: Buffer.from('STRING_HEX_ENCODED', 'hex') } - As a buffer.
- torSocksProxy - If hostname contains an onion address, the backend will try to connect to it using the the TOR socks proxy. Default:
Core Lightning (unix sock):
- unixSockPath - The absolute file path to the unix sock of c-lightning. Example:
/path/to/unix/sock/.lightning/lightning-rpc
Core Lightning (Sparko):
- baseUrl - Full URL and path of the Sparko plugin's HTTP-RPC API. Onion addresses are supported. Examples:
https://127.0.0.1:9737/rpc
http://esdlkvxdkwxz6yqs6rquapg4xxt4pt4guj24k75pdnquo5nau135ugyd.onion/rpc
- cert - The TLS certificate used by the Sparko plugin.
- accessKey - See
--sparko-keys= in your lightningd config.
LNBits:
- baseUrl - The URL of the LNBits instance. Example:
https://legend.lnbits.com
- adminKey - From an account page, open "API info" to view the "Admin key".
GetAlby:
- secret - Go to the GetAlby website, login to your account (link in top-right corner), then go to "Wallet" page (top-center), then scroll down until you see "Show your connection credentials". Click that button to reveal your lndhub secret. Copy and paste it here.
lndhub://login:password@https://ln.getalby.com
LndHub:
- secret - If using BlueWallet, go to wallet then "Export/Backup" to view the secret. Example:
lndhub://login:password@baseurl
OpenNode:
Custom Backend
It is possible to define your own custom backend to use with this module. To do so, create a new file and save it in your project:
const { LightningBackend } = require('lightning-backends/lib');
class Backend extends LightningBackend {
static name = 'custom';
constructor(options) {
super(Backend.name, options, {
defaultOptions: {
customOption1: 'a default value',
},
requiredOptions: ['customOption1'],
});
}
checkOptions(options) {
}
getNodeUri() {
const { customOption1 } = this.options;
return Promise.reject('Not implemented');
}
openChannel(remoteId, localAmt, pushAmt, makePrivate) {
return Promise.reject('Not implemented');
}
payInvoice(invoice) {
return Promise.reject('Not implemented');
}
addInvoice(amount, extra) {
return Promise.reject('Not implemented');
}
getInvoiceStatus(paymentHash) {
return Promise.reject('Not implemented');
}
};
module.exports = Backend;
Then to use your custom backend:
const { prepareBackend } = require('lightning-backends');
const backend = './backends/custom.js';
const config = {};
const ln = prepareBackend(backend, config);
ln.payInvoice(invoice).then(() => {
console.log('payInvoice OK', { result });
}).catch(error => {
console.error('payInvoice FAILED:', { error });
});
Tests
Run automated tests as follows:
npm test
Changelog
See CHANGELOG.md
License
This software is MIT licensed:
A short, permissive software license. Basically, you can do whatever you want as long as you include the original copyright and license notice in any copy of the software/source. There are many variations of this license in use.