npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rolznz-test1

v5.0.3

Published

Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps

Downloads

1

Readme

Lightning Web SDK

An npm package that provides useful and common tools and helpers to build lightning web applications.

🚀 Quick Start

npm install @getalby/lightning-tools

or

yarn add @getalby/lightning-tools

or for use without any build tools:

<script type="module">
  import { LightningAddress } from "https://cdn.skypack.dev/@getalby/[email protected]";
  // or import { LightningAddress } from "https://cdn.jsdelivr.net/npm/@getalby/[email protected]/dist/index.modern.js";

  // use LightningAddress normally...
  (async () => {
    const ln = new LightningAddress("[email protected]");
    // fetch the LNURL data
    await ln.fetch();
    // get the LNURL-pay data:
    console.log(ln.lnurlpData);
  })();
</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.)

🤙 Usage

Lightning Address

The LightningAddress class provides helpers to work with lightning addresses

import { LightningAddress } from "@getalby/lightning-tools";

const ln = new LightningAddress("[email protected]");

// 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);

Get an invoice:

import { LightningAddress } from "@getalby/lightning-tools";

const ln = new LightningAddress("[email protected]");

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

Verify a payment

import { LightningAddress } from "@getalby/lightning-tools";
const ln = new LightningAddress("[email protected]");
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();

Boost a LN address:

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("[email protected]");
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);

Zapping a LN address on Nostr:

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("[email protected]");
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

Zapping a LN address on Nostr using Nostr Wallet Connect:

Native zaps without a browser extension are possible by using a Nostr Wallet Connect WebLN provider.

See examples/zaps-nwc

L402

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.

fetchWithL402(url: string, fetchArgs, options)

  • url: the L402 protected URL
  • fetchArgs: arguments are passed to the underlaying fetch() function used to do the HTTP request
  • options:
    • webln: the webln object used to call sendPayment() defaults to globalThis.webln
    • store: a key/value store object to persiste the l402 for each URL. The store must implement a getItem()/setItem() function as the browser's localStorage. By default a memory storage is used.
    • headerKey: defaults to L402 but if you need to consume an old LSAT API set this to LSAT
Examples
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() }
);

Basic invoice decoding

You can initialize an Invoice to decode a payment request.

const { Invoice } = require("alby-tools");

const invoice = new Invoice({ pr });

const { paymentHash, satoshi, description, createdDate, expiryDate } = invoice;

If you need more details about the invoice, use a dedicated BOLT11 decoding library.

💵 Fiat conversions

Helpers to convert sats values to fiat and fiat values to sats.

getFiatValue(satoshi: number, currency: string): number

Returns the fiat value for a specified currrency of a satoshi amount

getSatoshiValue(amount: number, currency: string): number

Returns the satoshi value for a specified amount (in the smallest denomination) and currency

getFormattedFiatValue(satoshi: number, currency: string, locale: string): string

Like getFiatValue but returns a formatted string for a given locale using JavaScript's toLocaleString

Examples

await getFiatValue(satoshi: 2100, currency: 'eur');
await getSatoshiValue(amount: 100, currency: 'eur'); // for 1 EUR
await getFormattedFiatValue(satoshi: 2100, currency: 'usd', locale: 'en')

🤖 Lightning Address Proxy

This library uses a proxy to simplify requests to lightning providers.

  • Many ln addresses don't support CORS, which means fetching the data directly in a browser environment will not always work.
  • Two requests are required to retrieve lnurlp and keysend data for a lightning address. The proxy will do these for you with a single request.

You can disable the proxy by explicitly setting the proxy to false when initializing a lightning address:

const lightningAddress = new LightningAddress("[email protected]", {proxy: false});

fetch() dependency

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";

🛠 Development

yarn install
yarn run build

Need help?

We are happy to help, please contact us or create an issue.

License

MIT