@getalby/lightning-tools 8.1.1 β 8.2.0
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 +78 -13
- package/dist/cjs/402/l402.cjs +1304 -2
- package/dist/cjs/402/l402.cjs.map +1 -1
- package/dist/cjs/402/mpp.cjs +1306 -5
- package/dist/cjs/402/mpp.cjs.map +1 -1
- package/dist/cjs/402/x402.cjs +46 -15
- package/dist/cjs/402/x402.cjs.map +1 -1
- package/dist/cjs/402.cjs +101 -10
- package/dist/cjs/402.cjs.map +1 -1
- package/dist/cjs/bip21.cjs +115 -0
- package/dist/cjs/bip21.cjs.map +1 -0
- package/dist/cjs/index.cjs +213 -10
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/402/l402.js +1304 -2
- package/dist/esm/402/l402.js.map +1 -1
- package/dist/esm/402/mpp.js +1306 -5
- package/dist/esm/402/mpp.js.map +1 -1
- package/dist/esm/402/x402.js +46 -15
- package/dist/esm/402/x402.js.map +1 -1
- package/dist/esm/402.js +98 -11
- package/dist/esm/402.js.map +1 -1
- package/dist/esm/bip21.js +112 -0
- package/dist/esm/bip21.js.map +1 -0
- package/dist/esm/index.js +208 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/lightning-tools.umd.js +2 -2
- package/dist/lightning-tools.umd.js.map +1 -1
- package/dist/types/402/l402.d.ts +49 -3
- package/dist/types/402/mpp.d.ts +54 -6
- package/dist/types/402/x402.d.ts +49 -3
- package/dist/types/402.d.ts +68 -17
- package/dist/types/bip21.d.ts +46 -0
- package/dist/types/index.d.ts +112 -17
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Before you start coding, look at example scenarios in our **[Developer Sandbox](
|
|
|
10
10
|
|
|
11
11
|
## π€ π β‘ For Developers using Agents / LLMs / Vibe Coding
|
|
12
12
|
|
|
13
|
-
Skip the rest of this README and use the [Alby Bitcoin Builder Skill](https://github.com/getAlby/builder-skill)
|
|
13
|
+
Skip the rest of this README and use the [Alby Bitcoin Builder Skill](https://github.com/getAlby/builder-skill) to build a bitcoin app or the [Alby Bitcoin Payment Skill](https://github.com/getAlby/payments-skill) to give your agent payment capabilities. It will handle the rest!
|
|
14
14
|
|
|
15
15
|
## Manual Installation
|
|
16
16
|
|
|
@@ -184,7 +184,8 @@ This library includes functions to consome those resources.
|
|
|
184
184
|
- url: the protected URL
|
|
185
185
|
- fetchArgs: arguments are passed to the underlying `fetch()` function used to do the HTTP request
|
|
186
186
|
- options:
|
|
187
|
-
- wallet: any object that implements `payInvoice({ invoice })` and returns `{ preimage }`. Used to pay L402, X402 and MPP invoices.
|
|
187
|
+
- wallet: any object that implements `payInvoice({ invoice })` and returns `{ preimage, fees_paid? }`. Used to pay L402, X402 and MPP invoices.
|
|
188
|
+
- credentials (optional): a credential from a previous paid request (`response.payment.credentials`). When provided it is applied to the request before it is sent so the server can authorize it without a new payment β see [Payment info & polling](#payment-info--polling).
|
|
188
189
|
|
|
189
190
|
##### Examples
|
|
190
191
|
|
|
@@ -196,16 +197,53 @@ const nwc = new NWCClient({
|
|
|
196
197
|
nostrWalletConnectUrl: "nostr+walletconnect://...",
|
|
197
198
|
});
|
|
198
199
|
|
|
199
|
-
await fetch402(
|
|
200
|
-
"https://example.com/protected-resource",
|
|
201
|
-
{},
|
|
202
|
-
{ wallet: nwc },
|
|
203
|
-
)
|
|
200
|
+
await fetch402("https://example.com/protected-resource", {}, { wallet: nwc })
|
|
204
201
|
.then((res) => res.json())
|
|
205
202
|
.then(console.log)
|
|
206
203
|
.finally(() => nwc.close());
|
|
207
204
|
```
|
|
208
205
|
|
|
206
|
+
#### Payment info & polling
|
|
207
|
+
|
|
208
|
+
All of the 402 fetch helpers (`fetch402`, `fetchWithL402`, `fetchWithX402`, `fetchWithMpp`) return a standard `fetch` `Response`. When a payment was made (or a supplied credential was reused) the response also carries a `payment` property:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
interface PaymentInfo {
|
|
212
|
+
paid: boolean; // whether a lightning payment was made for this request
|
|
213
|
+
amount: number; // amount of the paid invoice, in satoshis (0 when paid is false)
|
|
214
|
+
feesPaid?: number; // routing fees in millisatoshis, when reported by the wallet
|
|
215
|
+
preimage?: string; // payment preimage, when a payment was made
|
|
216
|
+
credentials: {
|
|
217
|
+
// reusable credential β pass back via options.credentials
|
|
218
|
+
header: string; // e.g. "Authorization" (L402/MPP) or "payment-signature" (x402)
|
|
219
|
+
value: string;
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
This lets you inspect what a request cost, and β by passing `credentials` back on a follow-up request β authorize subsequent calls without paying again (e.g. polling a long-running video/song generation job).
|
|
225
|
+
|
|
226
|
+
> **Important:** when you pass `credentials`, the helper reuses them and **never pays a second time**. If the server still responds with a `402` (e.g. the credential expired or the balance is depleted), that `402` response is returned to you as-is β the library will not silently pay another invoice. You decide what to do next: retry the same credential, or make a fresh unauthenticated request to pay again.
|
|
227
|
+
|
|
228
|
+
```js
|
|
229
|
+
// First request (no credentials): pays once and returns the content plus a reusable credential
|
|
230
|
+
const res = await fetch402(url, { method: "POST", body }, { wallet: nwc });
|
|
231
|
+
const job = await res.json();
|
|
232
|
+
console.info(`Paid ${res.payment.amount} sats`);
|
|
233
|
+
|
|
234
|
+
// Follow-up requests reuse the credential β these NEVER pay again
|
|
235
|
+
const pollRes = await fetch402(
|
|
236
|
+
`${url}/status/${job.id}`,
|
|
237
|
+
{},
|
|
238
|
+
{ wallet: nwc, credentials: res.payment.credentials },
|
|
239
|
+
);
|
|
240
|
+
if (pollRes.status === 402) {
|
|
241
|
+
// credential not (yet) accepted β retry the same credential later, do not re-pay
|
|
242
|
+
} else {
|
|
243
|
+
console.info(await pollRes.json());
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
209
247
|
#### L402
|
|
210
248
|
|
|
211
249
|
L402 is a protocol standard based on the HTTP 402 Payment Required error code
|
|
@@ -219,7 +257,8 @@ This library includes a `fetchWithL402` function to consume L402 protected resou
|
|
|
219
257
|
- url: the L402 protected URL
|
|
220
258
|
- fetchArgs: arguments are passed to the underlying `fetch()` function used to do the HTTP request
|
|
221
259
|
- options:
|
|
222
|
-
- wallet: any object (e.g. a NWC client) that implements `payInvoice({ invoice })` and returns `{ preimage }`. Used to pay the L402 invoice.
|
|
260
|
+
- wallet: any object (e.g. a NWC client) that implements `payInvoice({ invoice })` and returns `{ preimage, fees_paid? }`. Used to pay the L402 invoice.
|
|
261
|
+
- credentials (optional): a credential from a previous paid request β see [Payment info & polling](#payment-info--polling).
|
|
223
262
|
|
|
224
263
|
##### Examples
|
|
225
264
|
|
|
@@ -244,9 +283,9 @@ await fetchWithL402(
|
|
|
244
283
|
#### X402
|
|
245
284
|
|
|
246
285
|
Similar to L402 X402 is an open protocol for machine-to-machine payments built on the HTTP 402 Payment Required status code.
|
|
247
|
-
It enables APIs and resources to request payments inline, without prior registration or authentication.
|
|
286
|
+
It enables APIs and resources to request payments inline, without prior registration or authentication.
|
|
248
287
|
|
|
249
|
-
This library includes a `fetchWithX402` function to consume X402-protected resources that support the lightning network.
|
|
288
|
+
This library includes a `fetchWithX402` function to consume X402-protected resources that support the lightning network.
|
|
250
289
|
(Note: X402 works also with other coins and network. This library supports X402 resources that accept Bitcoin on the lightning network)
|
|
251
290
|
|
|
252
291
|
##### fetchWithX402(url: string, fetchArgs, options)
|
|
@@ -254,7 +293,8 @@ This library includes a `fetchWithX402` function to consume X402-protected resou
|
|
|
254
293
|
- url: the X402 protected URL
|
|
255
294
|
- fetchArgs: arguments are passed to the underlying `fetch()` function used to do the HTTP request
|
|
256
295
|
- options:
|
|
257
|
-
- wallet: any object (e.g. a NWC client) that implements `payInvoice({ invoice })` and returns `{ preimage }`. Used to pay the X402 invoice.
|
|
296
|
+
- wallet: any object (e.g. a NWC client) that implements `payInvoice({ invoice })` and returns `{ preimage, fees_paid? }`. Used to pay the X402 invoice.
|
|
297
|
+
- credentials (optional): a credential from a previous paid request β see [Payment info & polling](#payment-info--polling).
|
|
258
298
|
|
|
259
299
|
##### Examples
|
|
260
300
|
|
|
@@ -281,7 +321,7 @@ await fetchWithX402(
|
|
|
281
321
|
MPP is an open protocol for machine-to-machine payments built on the HTTP 402 Payment Required status code.
|
|
282
322
|
Charge for API requests, tool calls, or contentβagents and apps pay per request in the same HTTP call.
|
|
283
323
|
|
|
284
|
-
This library includes a `fetchWithMpp` function to consume MPP-protected resources that support the lightning network.
|
|
324
|
+
This library includes a `fetchWithMpp` function to consume MPP-protected resources that support the lightning network.
|
|
285
325
|
(Note: MPP works also with other payment methods. This library supports resources that accept Bitcoin on the lightning network)
|
|
286
326
|
|
|
287
327
|
##### fetchWithMpp(url: string, fetchArgs, options)
|
|
@@ -289,7 +329,8 @@ This library includes a `fetchWithMpp` function to consume MPP-protected resourc
|
|
|
289
329
|
- url: the MPP protected URL
|
|
290
330
|
- fetchArgs: arguments are passed to the underlying `fetch()` function used to do the HTTP request
|
|
291
331
|
- options:
|
|
292
|
-
- wallet: any object (e.g. a NWC client) that implements `payInvoice({ invoice })` and returns `{ preimage }`. Used to pay the
|
|
332
|
+
- wallet: any object (e.g. a NWC client) that implements `payInvoice({ invoice })` and returns `{ preimage, fees_paid? }`. Used to pay the MPP invoice.
|
|
333
|
+
- credentials (optional): a credential from a previous paid request β see [Payment info & polling](#payment-info--polling).
|
|
293
334
|
|
|
294
335
|
##### Examples
|
|
295
336
|
|
|
@@ -357,6 +398,30 @@ await fiat.getFormattedFiatValue({
|
|
|
357
398
|
});
|
|
358
399
|
```
|
|
359
400
|
|
|
401
|
+
### BIP21 (`bitcoin:` URIs)
|
|
402
|
+
|
|
403
|
+
Parse [BIP21](https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki) payment URIs, including the unified-QR `lightning=` fallback parameter.
|
|
404
|
+
|
|
405
|
+
```js
|
|
406
|
+
import { parseBip21 } from "@getalby/lightning-tools";
|
|
407
|
+
|
|
408
|
+
const result = parseBip21(
|
|
409
|
+
"bitcoin:bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4?amount=0.001&label=Donation&lightning=lnbc...",
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
if (result) {
|
|
413
|
+
result.address; // "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"
|
|
414
|
+
result.amount; // 0.001 (BTC)
|
|
415
|
+
result.amountSats; // 100000
|
|
416
|
+
result.label; // "Donation"
|
|
417
|
+
result.lightning; // BOLT11 fallback, if present
|
|
418
|
+
result.lno; // BOLT12 offer, if present
|
|
419
|
+
result.unknownRequiredParams; // reject the URI if non-empty (BIP21 `req-*` rule)
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
`parseBip21` returns `null` for inputs that don't start with the `bitcoin:` scheme. Address validation is intentionally out of scope β validate the returned `address` with your own check if needed.
|
|
424
|
+
|
|
360
425
|
### π€ Lightning Address Proxy
|
|
361
426
|
|
|
362
427
|
This library uses a [proxy](https://github.com/getAlby/lightning-address-details-proxy) to simplify requests to lightning providers.
|