
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@getalby/lightning-tools
Advanced tools
Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps
An npm package that provides useful and common tools and helpers to build lightning web applications.
npm install @getalby/lightning-tools
or
yarn add @getalby/lightning-tools
or for use without any build tools:
// lightning-tools now available at window.lightningTools
<script src="https://cdn.jsdelivr.net/npm/@getalby/lightning-tools@latest/dist/index.browser.js"></script>
This library relies on a global fetch()
function which will work in browsers and node v18 or newer. (In older versions you have to use a polyfill.)
The LightningAddress
class provides helpers to work with lightning addresses
import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("hello@getalby.com");
// fetch the LNURL data
await ln.fetch();
// get the LNURL-pay data:
console.log(ln.lnurlpData); // returns a [LNURLPayResponse](https://github.com/getAlby/js-lightning-tools/blob/master/src/types.ts#L1-L15)
// get the keysend data:
console.log(ln.keysendData);
import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("hello@getalby.com");
await ln.fetch();
// request an invoice for 1000 satoshis
// this returns a new `Invoice` class that can also be used to validate the payment
const invoice = await ln.requestInvoice({ satoshi: 1000 });
console.log(invoice.paymentRequest); // print the payment request
console.log(invoice.paymentHash); // print the payment hash
import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("hello@getalby.com");
await ln.fetch();
const invoice = await ln.requestInvoice({ satoshi: 1000 });
// if the LNURL providers supports LNURL-verify:
const paid = await invoice.verifyPayment(); // returns true of false
if (paid) {
console.log(invoice.preimage);
}
// if you have the preimage for example in a WebLN context
await window.webln.enable();
const response = await window.webln.sendPayment(invoice.paymentRequest);
const paid = invoice.validatePreimage(response.preimage); // returns true or false
if (paid) {
console.log("paid");
}
// or use the convenenice method:
await invoice.isPaid();
It is also possible to manually initialize the Invoice
const { Invoice } = require("alby-tools");
const invoice = new Invoice({ pr: pr, preimage: preimage });
await invoice.isPaid();
You can also attach additional metadata information like app name, version, name of the podcast which is boosted etc. to the keysend payment.
import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("hello@getalby.com");
await ln.fetch();
const boost = {
action: "boost",
value_msat: 21000,
value_msat_total: 21000,
app_name: "Podcastr",
app_version: "v2.1",
feedId: "21",
podcast: "random podcast",
episode: "1",
ts: 2121,
name: "Satoshi",
sender_name: "Alby",
};
await ln.boost(boost);
Nostr is a simple, open protocol that enables truly censorship-resistant and global value-for-value publishing on the web. Nostr integrates deeply with Lightning. more info
This librarys provides helpers to create zaps.
import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("hello@getalby.com");
await ln.fetch();
const response = await ln.zap({
satoshi: 1000,
comment: "Awesome post",
relays: ["wss://relay.damus.io"],
e: "44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245",
});
console.log(response.preimage); // print the preimage
For a full example see examples/zaps
Native zaps without a browser extension are possible by using a Nostr Wallet Connect WebLN provider.
L402 is a protocol standard based on the HTTP 402 Payment Required error code designed to support the use case of charging for services and authenticating users in distributed networks.
This library includes a fetchWithL402
function to consume L402 protected resources.
fetch()
function used to do the HTTP requestsendPayment()
defaults to globalThis.weblngetItem()
/setItem()
function as the browser's localStorage. By default a memory storage is used.import { fetchWithL402 } from "@getalby/lightning-tools";
// this will fetch the resouce and pay the invoice with window.webln.
// the tokens/preimage data will be stored in the browser's localStorage and used for any following request
await fetchWithL402(
"https://lsat-weather-api.getalby.repl.co/kigali",
{},
{ store: window.localStorage },
)
.then((res) => res.json())
.then(console.log);
import { fetchWithL402 } from "@getalby/lightning-tools";
import { webln } from "alby-js-sdk";
// use a NWC WebLN provide to do the payments
const nwc = new webln.NostrWebLNProvider({
nostrWalletConnectUrl: loadNWCUrl(),
});
// this will fetch the resouce and pay the invoice with a NWC webln object
await fetchWithL402(
"https://lsat-weather-api.getalby.repl.co/kigali",
{},
{ webln: nwc },
)
.then((res) => res.json())
.then(console.log);
import { l402 } from "@getalby/lightning-tools";
// do not store the tokens
await l402.fetchWithL402(
"https://lsat-weather-api.getalby.repl.co/kigali",
{},
{ store: new l402.storage.NoStorage() },
);
Helpers to convert sats values to fiat and fiat values to sats.
Returns the fiat value for a specified currrency of a satoshi amount
Returns the satoshi value for a specified amount (in the smallest denomination) and currency
Like getFiatValue
but returns a formatted string for a given locale using JavaScript's toLocaleString
await getFiatValue(satoshi: 2100, currency: 'eur');
await getSatoshiValue(amount: 100, currency: 'eur'); // for 1 EUR
await getFormattedFiatValue(satoshi: 2100, currency: 'usd', locale: 'en')
This library uses a proxy to simplify requests to lightning providers.
You can disable the proxy by explicitly setting the proxy to false when initializing a lightning address:
const lightningAddress = new LightningAddress("hello@getalby.com", {proxy: false});
This library relies on a global fetch object which will work in browsers and node v18.x or newer. In old version yoi can manually install a global fetch option or polyfill if needed.
For example:
import fetch from "cross-fetch"; // or "@inrupt/universal-fetch"
globalThis.fetch = fetch;
// or as a polyfill:
import "cross-fetch/polyfill";
yarn install
yarn run build
We are happy to help, please contact us or create an issue.
MIT
FAQs
Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps
The npm package @getalby/lightning-tools receives a total of 3,360 weekly downloads. As such, @getalby/lightning-tools popularity was classified as popular.
We found that @getalby/lightning-tools demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.