@getalby/lightning-tools 6.1.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  An npm package that provides useful and common tools and helpers to build lightning web applications.
8
8
 
9
+ Before you start coding, look at example scenarios in our **[Developer Sandbox](https://sandbox.albylabs.com/)**
10
+
9
11
  ## 🤖 🚀 ⚡ For Developers using Agents / LLMs / Vibe Coding
10
12
 
11
13
  Skip the rest of this README and use the [Alby Bitcoin Payments Agent Skill](https://github.com/getAlby/alby-agent-skill) instead. It will handle the rest!
@@ -179,7 +181,7 @@ This library includes a `fetchWithL402` function to consume L402 protected resou
179
181
  - url: the L402 protected URL
180
182
  - fetchArgs: arguments are passed to the underlying `fetch()` function used to do the HTTP request
181
183
  - options:
182
- - webln: the webln object used to call `sendPayment()` defaults to globalThis.webln
184
+ - wallet: any object that implements `sendPayment(paymentRequest)` and returns `{ preimage }`. Used to pay the L402 invoice.
183
185
  - 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.
184
186
  - headerKey: defaults to L402 but if you need to consume an old LSAT API set this to LSAT
185
187
 
@@ -188,12 +190,12 @@ This library includes a `fetchWithL402` function to consume L402 protected resou
188
190
  ```js
189
191
  import { fetchWithL402 } from "@getalby/lightning-tools/l402";
190
192
 
191
- // this will fetch the resource and pay the invoice with window.webln.
193
+ // pass a wallet that implements sendPayment()
192
194
  // the tokens/preimage data will be stored in the browser's localStorage and used for any following request
193
195
  await fetchWithL402(
194
196
  "https://lsat-weather-api.getalby.repl.co/kigali",
195
197
  {},
196
- { store: window.localStorage },
198
+ { wallet: myWallet, store: window.localStorage },
197
199
  )
198
200
  .then((res) => res.json())
199
201
  .then(console.log);
@@ -203,16 +205,16 @@ await fetchWithL402(
203
205
  import { fetchWithL402 } from "@getalby/lightning-tools/l402";
204
206
  import { NostrWebLNProvider } from "@getalby/sdk";
205
207
 
206
- // use a NWC WebLN provide to do the payments
208
+ // use a NWC provider as the wallet to do the payments
207
209
  const nwc = new NostrWebLNProvider({
208
210
  nostrWalletConnectUrl: loadNWCUrl(),
209
211
  });
210
212
 
211
- // this will fetch the resource and pay the invoice with a NWC webln object
213
+ // this will fetch the resource and pay the invoice using the NWC wallet
212
214
  await fetchWithL402(
213
215
  "https://lsat-weather-api.getalby.repl.co/kigali",
214
216
  {},
215
- { webln: nwc },
217
+ { wallet: nwc },
216
218
  )
217
219
  .then((res) => res.json())
218
220
  .then(console.log);
@@ -1693,17 +1693,21 @@ const parseL402 = (input) => {
1693
1693
  }
1694
1694
  return keyValuePairs;
1695
1695
  };
1696
+ const makeAuthenticateHeader = (args) => {
1697
+ const key = args.key || "L402";
1698
+ return `${key} macaroon="${args.macaroon}", invoice="${args.invoice}"`;
1699
+ };
1696
1700
 
1697
1701
  const memoryStorage = new MemoryStorage();
1698
- const HEADER_KEY = "L402"; // we have to update this to L402 at some point
1702
+ const HEADER_KEY = "L402";
1699
1703
  const fetchWithL402 = async (url, fetchArgs, options) => {
1700
1704
  if (!options) {
1701
1705
  options = {};
1702
1706
  }
1703
1707
  const headerKey = options.headerKey || HEADER_KEY;
1704
- const webln = options.webln || globalThis.webln;
1705
- if (!webln) {
1706
- throw new Error("WebLN is missing");
1708
+ const wallet = options.wallet;
1709
+ if (!wallet) {
1710
+ throw new Error("wallet is missing");
1707
1711
  }
1708
1712
  const store = options.store || memoryStorage;
1709
1713
  if (!fetchArgs) {
@@ -1730,8 +1734,7 @@ const fetchWithL402 = async (url, fetchArgs, options) => {
1730
1734
  const details = parseL402(header);
1731
1735
  const token = details.token || details.macaroon;
1732
1736
  const inv = details.invoice;
1733
- await webln.enable();
1734
- const invResp = await webln.sendPayment(inv);
1737
+ const invResp = await wallet.sendPayment(inv);
1735
1738
  store.setItem(url, JSON.stringify({
1736
1739
  token: token,
1737
1740
  preimage: invResp.preimage,
@@ -1808,6 +1811,7 @@ exports.getFormattedFiatValue = getFormattedFiatValue;
1808
1811
  exports.getSatoshiValue = getSatoshiValue;
1809
1812
  exports.isUrl = isUrl;
1810
1813
  exports.isValidAmount = isValidAmount;
1814
+ exports.makeAuthenticateHeader = makeAuthenticateHeader;
1811
1815
  exports.parseKeysendResponse = parseKeysendResponse;
1812
1816
  exports.parseL402 = parseL402;
1813
1817
  exports.parseLnUrlPayResponse = parseLnUrlPayResponse;