@getalby/lightning-tools 6.1.0 → 8.0.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 +111 -29
- package/dist/cjs/402/l402.cjs +55 -0
- package/dist/cjs/402/l402.cjs.map +1 -0
- package/dist/cjs/402/mpp.cjs +179 -0
- package/dist/cjs/402/mpp.cjs.map +1 -0
- package/dist/cjs/402/x402.cjs +1313 -0
- package/dist/cjs/402/x402.cjs.map +1 -0
- package/dist/cjs/402.cjs +1585 -0
- package/dist/cjs/402.cjs.map +1 -0
- package/dist/cjs/bolt11.cjs +8 -0
- package/dist/cjs/bolt11.cjs.map +1 -1
- package/dist/cjs/index.cjs +304 -52
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/lnurl.cjs +8 -0
- package/dist/cjs/lnurl.cjs.map +1 -1
- package/dist/esm/402/l402.js +53 -0
- package/dist/esm/402/l402.js.map +1 -0
- package/dist/esm/402/mpp.js +177 -0
- package/dist/esm/402/mpp.js.map +1 -0
- package/dist/esm/402/x402.js +1311 -0
- package/dist/esm/402/x402.js.map +1 -0
- package/dist/esm/402.js +1579 -0
- package/dist/esm/402.js.map +1 -0
- package/dist/esm/bolt11.js +8 -0
- package/dist/esm/bolt11.js.map +1 -1
- package/dist/esm/index.js +301 -50
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lnurl.js +8 -0
- package/dist/esm/lnurl.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 +13 -0
- package/dist/types/402/mpp.d.ts +26 -0
- package/dist/types/402/x402.d.ts +13 -0
- package/dist/types/402.d.ts +41 -0
- package/dist/types/bolt11.d.ts +4 -0
- package/dist/types/index.d.ts +40 -20
- package/dist/types/lnurl.d.ts +2 -0
- package/package.json +20 -5
- package/dist/cjs/l402.cjs +0 -89
- package/dist/cjs/l402.cjs.map +0 -1
- package/dist/esm/l402.js +0 -84
- package/dist/esm/l402.js.map +0 -1
- package/dist/types/l402.d.ts +0 -27
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface Wallet {
|
|
2
|
+
payInvoice(args: {
|
|
3
|
+
invoice: string;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
preimage: string;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const fetchWithL402: (url: string, fetchArgs: RequestInit, options: {
|
|
10
|
+
wallet: Wallet;
|
|
11
|
+
}) => Promise<Response>;
|
|
12
|
+
|
|
13
|
+
export { fetchWithL402 };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface Wallet {
|
|
2
|
+
payInvoice(args: {
|
|
3
|
+
invoice: string;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
preimage: string;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Fetch a resource protected by the draft-lightning-charge-00 payment
|
|
11
|
+
* authentication protocol.
|
|
12
|
+
*
|
|
13
|
+
* On a `402 Payment Required` response that carries a
|
|
14
|
+
* `WWW-Authenticate: Payment method="lightning" intent="charge" …` header
|
|
15
|
+
* the function pays the embedded BOLT11 invoice and retries with the
|
|
16
|
+
* resulting preimage as the credential.
|
|
17
|
+
*
|
|
18
|
+
* Note: lightning-charge uses consume-once challenge semantics – each
|
|
19
|
+
* challenge embeds a fresh invoice, so paid credentials cannot be reused.
|
|
20
|
+
* The `store` option is accepted for API consistency but is not used.
|
|
21
|
+
*/
|
|
22
|
+
declare const fetchWithMpp: (url: string, fetchArgs: RequestInit, options: {
|
|
23
|
+
wallet: Wallet;
|
|
24
|
+
}) => Promise<Response>;
|
|
25
|
+
|
|
26
|
+
export { fetchWithMpp };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface Wallet {
|
|
2
|
+
payInvoice(args: {
|
|
3
|
+
invoice: string;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
preimage: string;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare const fetchWithX402: (url: string, fetchArgs: RequestInit, options: {
|
|
10
|
+
wallet: Wallet;
|
|
11
|
+
}) => Promise<Response>;
|
|
12
|
+
|
|
13
|
+
export { fetchWithX402 };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
interface Wallet {
|
|
2
|
+
payInvoice(args: {
|
|
3
|
+
invoice: string;
|
|
4
|
+
}): Promise<{
|
|
5
|
+
preimage: string;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
declare function createGuardedWallet(wallet: Wallet, maxAmountSats: number): Wallet;
|
|
9
|
+
|
|
10
|
+
declare const fetch402: (url: string, fetchArgs: RequestInit, options: {
|
|
11
|
+
wallet: Wallet;
|
|
12
|
+
maxAmount?: number;
|
|
13
|
+
}) => Promise<Response>;
|
|
14
|
+
|
|
15
|
+
declare const fetchWithL402: (url: string, fetchArgs: RequestInit, options: {
|
|
16
|
+
wallet: Wallet;
|
|
17
|
+
}) => Promise<Response>;
|
|
18
|
+
|
|
19
|
+
declare const fetchWithX402: (url: string, fetchArgs: RequestInit, options: {
|
|
20
|
+
wallet: Wallet;
|
|
21
|
+
}) => Promise<Response>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Fetch a resource protected by the draft-lightning-charge-00 payment
|
|
25
|
+
* authentication protocol.
|
|
26
|
+
*
|
|
27
|
+
* On a `402 Payment Required` response that carries a
|
|
28
|
+
* `WWW-Authenticate: Payment method="lightning" intent="charge" …` header
|
|
29
|
+
* the function pays the embedded BOLT11 invoice and retries with the
|
|
30
|
+
* resulting preimage as the credential.
|
|
31
|
+
*
|
|
32
|
+
* Note: lightning-charge uses consume-once challenge semantics – each
|
|
33
|
+
* challenge embeds a fresh invoice, so paid credentials cannot be reused.
|
|
34
|
+
* The `store` option is accepted for API consistency but is not used.
|
|
35
|
+
*/
|
|
36
|
+
declare const fetchWithMpp: (url: string, fetchArgs: RequestInit, options: {
|
|
37
|
+
wallet: Wallet;
|
|
38
|
+
}) => Promise<Response>;
|
|
39
|
+
|
|
40
|
+
export { createGuardedWallet, fetch402, fetchWithL402, fetchWithMpp, fetchWithX402 };
|
|
41
|
+
export type { Wallet };
|
package/dist/types/bolt11.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ declare const fromHexString: (hexString: string) => Uint8Array<ArrayBuffer>;
|
|
|
17
17
|
type DecodedInvoice = {
|
|
18
18
|
paymentHash: string;
|
|
19
19
|
satoshi: number;
|
|
20
|
+
millisatoshi: number;
|
|
21
|
+
amountRaw: string;
|
|
20
22
|
timestamp: number;
|
|
21
23
|
expiry: number | undefined;
|
|
22
24
|
description: string | undefined;
|
|
@@ -29,6 +31,8 @@ declare class Invoice {
|
|
|
29
31
|
preimage: string | null;
|
|
30
32
|
verify: string | null;
|
|
31
33
|
satoshi: number;
|
|
34
|
+
millisatoshi: number;
|
|
35
|
+
amountRaw: string;
|
|
32
36
|
expiry: number | undefined;
|
|
33
37
|
timestamp: number;
|
|
34
38
|
createdDate: Date;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -20,6 +20,8 @@ declare const fromHexString: (hexString: string) => Uint8Array<ArrayBuffer>;
|
|
|
20
20
|
type DecodedInvoice = {
|
|
21
21
|
paymentHash: string;
|
|
22
22
|
satoshi: number;
|
|
23
|
+
millisatoshi: number;
|
|
24
|
+
amountRaw: string;
|
|
23
25
|
timestamp: number;
|
|
24
26
|
expiry: number | undefined;
|
|
25
27
|
description: string | undefined;
|
|
@@ -32,6 +34,8 @@ declare class Invoice {
|
|
|
32
34
|
preimage: string | null;
|
|
33
35
|
verify: string | null;
|
|
34
36
|
satoshi: number;
|
|
37
|
+
millisatoshi: number;
|
|
38
|
+
amountRaw: string;
|
|
35
39
|
expiry: number | undefined;
|
|
36
40
|
timestamp: number;
|
|
37
41
|
createdDate: Date;
|
|
@@ -235,27 +239,43 @@ declare class LightningAddress {
|
|
|
235
239
|
private parseNostrResponse;
|
|
236
240
|
}
|
|
237
241
|
|
|
238
|
-
interface
|
|
239
|
-
|
|
240
|
-
|
|
242
|
+
interface Wallet {
|
|
243
|
+
payInvoice(args: {
|
|
244
|
+
invoice: string;
|
|
245
|
+
}): Promise<{
|
|
246
|
+
preimage: string;
|
|
247
|
+
}>;
|
|
241
248
|
}
|
|
242
|
-
declare
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
declare class NoStorage implements KVStorage {
|
|
249
|
-
constructor(initial?: unknown);
|
|
250
|
-
getItem(key: string): null;
|
|
251
|
-
setItem(key: string, value: unknown): void;
|
|
252
|
-
}
|
|
253
|
-
declare const parseL402: (input: string) => Record<string, string>;
|
|
249
|
+
declare function createGuardedWallet(wallet: Wallet, maxAmountSats: number): Wallet;
|
|
250
|
+
|
|
251
|
+
declare const fetch402: (url: string, fetchArgs: RequestInit, options: {
|
|
252
|
+
wallet: Wallet;
|
|
253
|
+
maxAmount?: number;
|
|
254
|
+
}) => Promise<Response>;
|
|
254
255
|
|
|
255
256
|
declare const fetchWithL402: (url: string, fetchArgs: RequestInit, options: {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
wallet: Wallet;
|
|
258
|
+
}) => Promise<Response>;
|
|
259
|
+
|
|
260
|
+
declare const fetchWithX402: (url: string, fetchArgs: RequestInit, options: {
|
|
261
|
+
wallet: Wallet;
|
|
262
|
+
}) => Promise<Response>;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Fetch a resource protected by the draft-lightning-charge-00 payment
|
|
266
|
+
* authentication protocol.
|
|
267
|
+
*
|
|
268
|
+
* On a `402 Payment Required` response that carries a
|
|
269
|
+
* `WWW-Authenticate: Payment method="lightning" intent="charge" …` header
|
|
270
|
+
* the function pays the embedded BOLT11 invoice and retries with the
|
|
271
|
+
* resulting preimage as the credential.
|
|
272
|
+
*
|
|
273
|
+
* Note: lightning-charge uses consume-once challenge semantics – each
|
|
274
|
+
* challenge embeds a fresh invoice, so paid credentials cannot be reused.
|
|
275
|
+
* The `store` option is accepted for API consistency but is not used.
|
|
276
|
+
*/
|
|
277
|
+
declare const fetchWithMpp: (url: string, fetchArgs: RequestInit, options: {
|
|
278
|
+
wallet: Wallet;
|
|
259
279
|
}) => Promise<Response>;
|
|
260
280
|
|
|
261
281
|
interface FiatCurrency {
|
|
@@ -280,5 +300,5 @@ declare const getFormattedFiatValue: ({ satoshi, currency, locale, }: {
|
|
|
280
300
|
locale: string;
|
|
281
301
|
}) => Promise<string>;
|
|
282
302
|
|
|
283
|
-
export { DEFAULT_PROXY, Invoice, LN_ADDRESS_REGEX, LightningAddress,
|
|
284
|
-
export type { Boost, BoostArguments, BoostOptions, Event, FiatCurrency, InvoiceArgs,
|
|
303
|
+
export { DEFAULT_PROXY, Invoice, LN_ADDRESS_REGEX, LightningAddress, createGuardedWallet, decodeInvoice, fetch402, fetchWithL402, fetchWithMpp, fetchWithX402, fromHexString, generateZapEvent, getEventHash, getFiatBtcRate, getFiatCurrencies, getFiatValue, getFormattedFiatValue, getSatoshiValue, isUrl, isValidAmount, parseKeysendResponse, parseLnUrlPayResponse, parseNostrResponse, sendBoostagram, serializeEvent, validateEvent };
|
|
304
|
+
export type { Boost, BoostArguments, BoostOptions, Event, FiatCurrency, InvoiceArgs, KeySendRawData, KeysendResponse, LUD18PayerData, LUD18ServicePayerData, LnUrlPayResponse, LnUrlRawData, NostrProvider, NostrResponse, RequestInvoiceArgs, SuccessAction, Wallet, WeblnBoostParams, ZapArgs, ZapOptions };
|
package/dist/types/lnurl.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getalby/lightning-tools",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "Collection of helpful building blocks and tools to develop Bitcoin Lightning web apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": "https://github.com/getAlby/js-lightning-tools.git",
|
|
@@ -39,10 +39,25 @@
|
|
|
39
39
|
"require": "./dist/cjs/fiat.cjs",
|
|
40
40
|
"types": "./dist/types/fiat.d.ts"
|
|
41
41
|
},
|
|
42
|
-
"./
|
|
43
|
-
"import": "./dist/esm/
|
|
44
|
-
"require": "./dist/cjs/
|
|
45
|
-
"types": "./dist/types/
|
|
42
|
+
"./402": {
|
|
43
|
+
"import": "./dist/esm/402.js",
|
|
44
|
+
"require": "./dist/cjs/402.cjs",
|
|
45
|
+
"types": "./dist/types/402.d.ts"
|
|
46
|
+
},
|
|
47
|
+
"./402/l402": {
|
|
48
|
+
"import": "./dist/esm/402/l402.js",
|
|
49
|
+
"require": "./dist/cjs/402/l402.cjs",
|
|
50
|
+
"types": "./dist/types/402/l402.d.ts"
|
|
51
|
+
},
|
|
52
|
+
"./402/x402": {
|
|
53
|
+
"import": "./dist/esm/402/x402.js",
|
|
54
|
+
"require": "./dist/cjs/402/x402.cjs",
|
|
55
|
+
"types": "./dist/types/402/x402.d.ts"
|
|
56
|
+
},
|
|
57
|
+
"./402/mpp": {
|
|
58
|
+
"import": "./dist/esm/402/mpp.js",
|
|
59
|
+
"require": "./dist/cjs/402/mpp.cjs",
|
|
60
|
+
"types": "./dist/types/402/mpp.d.ts"
|
|
46
61
|
},
|
|
47
62
|
"./lnurl": {
|
|
48
63
|
"import": "./dist/esm/lnurl.js",
|
package/dist/cjs/l402.cjs
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
class MemoryStorage {
|
|
4
|
-
constructor(initial) {
|
|
5
|
-
this.storage = initial || {};
|
|
6
|
-
}
|
|
7
|
-
getItem(key) {
|
|
8
|
-
return this.storage[key];
|
|
9
|
-
}
|
|
10
|
-
setItem(key, value) {
|
|
11
|
-
this.storage[key] = value;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
class NoStorage {
|
|
15
|
-
constructor(initial) { }
|
|
16
|
-
getItem(key) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
setItem(key, value) { }
|
|
20
|
-
}
|
|
21
|
-
const parseL402 = (input) => {
|
|
22
|
-
// Remove the L402 and LSAT identifiers
|
|
23
|
-
const string = input.replace("L402", "").replace("LSAT", "").trim();
|
|
24
|
-
// Initialize an object to store the key-value pairs
|
|
25
|
-
const keyValuePairs = {};
|
|
26
|
-
// Regular expression to match key and (quoted or unquoted) value
|
|
27
|
-
const regex = /(\w+)=("([^"]*)"|'([^']*)'|([^,]*))/g;
|
|
28
|
-
let match;
|
|
29
|
-
// Use regex to find all key-value pairs
|
|
30
|
-
while ((match = regex.exec(string)) !== null) {
|
|
31
|
-
// Key is always match[1]
|
|
32
|
-
// Value is either match[3] (double-quoted), match[4] (single-quoted), or match[5] (unquoted)
|
|
33
|
-
keyValuePairs[match[1]] = match[3] || match[4] || match[5];
|
|
34
|
-
}
|
|
35
|
-
return keyValuePairs;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const memoryStorage = new MemoryStorage();
|
|
39
|
-
const HEADER_KEY = "L402"; // we have to update this to L402 at some point
|
|
40
|
-
const fetchWithL402 = async (url, fetchArgs, options) => {
|
|
41
|
-
if (!options) {
|
|
42
|
-
options = {};
|
|
43
|
-
}
|
|
44
|
-
const headerKey = options.headerKey || HEADER_KEY;
|
|
45
|
-
const webln = options.webln || globalThis.webln;
|
|
46
|
-
if (!webln) {
|
|
47
|
-
throw new Error("WebLN is missing");
|
|
48
|
-
}
|
|
49
|
-
const store = options.store || memoryStorage;
|
|
50
|
-
if (!fetchArgs) {
|
|
51
|
-
fetchArgs = {};
|
|
52
|
-
}
|
|
53
|
-
fetchArgs.cache = "no-store";
|
|
54
|
-
fetchArgs.mode = "cors";
|
|
55
|
-
if (!fetchArgs.headers) {
|
|
56
|
-
fetchArgs.headers = {};
|
|
57
|
-
}
|
|
58
|
-
const cachedL402Data = store.getItem(url);
|
|
59
|
-
if (cachedL402Data) {
|
|
60
|
-
const data = JSON.parse(cachedL402Data);
|
|
61
|
-
fetchArgs.headers["Authorization"] =
|
|
62
|
-
`${headerKey} ${data.token}:${data.preimage}`;
|
|
63
|
-
return await fetch(url, fetchArgs);
|
|
64
|
-
}
|
|
65
|
-
fetchArgs.headers["Accept-Authenticate"] = headerKey;
|
|
66
|
-
const initResp = await fetch(url, fetchArgs);
|
|
67
|
-
const header = initResp.headers.get("www-authenticate");
|
|
68
|
-
if (!header) {
|
|
69
|
-
return initResp;
|
|
70
|
-
}
|
|
71
|
-
const details = parseL402(header);
|
|
72
|
-
const token = details.token || details.macaroon;
|
|
73
|
-
const inv = details.invoice;
|
|
74
|
-
await webln.enable();
|
|
75
|
-
const invResp = await webln.sendPayment(inv);
|
|
76
|
-
store.setItem(url, JSON.stringify({
|
|
77
|
-
token: token,
|
|
78
|
-
preimage: invResp.preimage,
|
|
79
|
-
}));
|
|
80
|
-
fetchArgs.headers["Authorization"] =
|
|
81
|
-
`${headerKey} ${token}:${invResp.preimage}`;
|
|
82
|
-
return await fetch(url, fetchArgs);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
exports.MemoryStorage = MemoryStorage;
|
|
86
|
-
exports.NoStorage = NoStorage;
|
|
87
|
-
exports.fetchWithL402 = fetchWithL402;
|
|
88
|
-
exports.parseL402 = parseL402;
|
|
89
|
-
//# sourceMappingURL=l402.cjs.map
|
package/dist/cjs/l402.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"l402.cjs","sources":["../../src/l402/utils.ts","../../src/l402/l402.ts"],"sourcesContent":["export interface KVStorage {\n getItem(key: string): string | null;\n setItem(key: string, value: string): void;\n}\n\nexport class MemoryStorage implements KVStorage {\n storage;\n\n constructor(initial?: Record<string, unknown>) {\n this.storage = initial || {};\n }\n\n getItem(key: string) {\n return this.storage[key];\n }\n\n setItem(key: string, value: unknown) {\n this.storage[key] = value;\n }\n}\n\nexport class NoStorage implements KVStorage {\n constructor(initial?: unknown) {}\n\n getItem(key: string) {\n return null;\n }\n\n setItem(key: string, value: unknown) {}\n}\n\nexport const parseL402 = (input: string): Record<string, string> => {\n // Remove the L402 and LSAT identifiers\n const string = input.replace(\"L402\", \"\").replace(\"LSAT\", \"\").trim();\n\n // Initialize an object to store the key-value pairs\n const keyValuePairs = {};\n\n // Regular expression to match key and (quoted or unquoted) value\n const regex = /(\\w+)=(\"([^\"]*)\"|'([^']*)'|([^,]*))/g;\n let match;\n\n // Use regex to find all key-value pairs\n while ((match = regex.exec(string)) !== null) {\n // Key is always match[1]\n // Value is either match[3] (double-quoted), match[4] (single-quoted), or match[5] (unquoted)\n keyValuePairs[match[1]] = match[3] || match[4] || match[5];\n }\n\n return keyValuePairs;\n};\n","import { KVStorage, MemoryStorage, parseL402 } from \"./utils\";\nimport { WebLNProvider } from \"@webbtc/webln-types\";\n\nconst memoryStorage = new MemoryStorage();\n\nconst HEADER_KEY = \"L402\"; // we have to update this to L402 at some point\n\nexport const fetchWithL402 = async (\n url: string,\n fetchArgs: RequestInit,\n options: {\n headerKey?: string;\n webln?: WebLNProvider;\n store?: KVStorage;\n },\n) => {\n if (!options) {\n options = {};\n }\n const headerKey = options.headerKey || HEADER_KEY;\n const webln: WebLNProvider = options.webln || globalThis.webln;\n if (!webln) {\n throw new Error(\"WebLN is missing\");\n }\n const store = options.store || memoryStorage;\n if (!fetchArgs) {\n fetchArgs = {};\n }\n fetchArgs.cache = \"no-store\";\n fetchArgs.mode = \"cors\";\n if (!fetchArgs.headers) {\n fetchArgs.headers = {};\n }\n const cachedL402Data = store.getItem(url);\n if (cachedL402Data) {\n const data = JSON.parse(cachedL402Data);\n fetchArgs.headers[\"Authorization\"] =\n `${headerKey} ${data.token}:${data.preimage}`;\n return await fetch(url, fetchArgs);\n }\n\n fetchArgs.headers[\"Accept-Authenticate\"] = headerKey;\n const initResp = await fetch(url, fetchArgs);\n const header = initResp.headers.get(\"www-authenticate\");\n if (!header) {\n return initResp;\n }\n\n const details = parseL402(header);\n const token = details.token || details.macaroon;\n const inv = details.invoice;\n\n await webln.enable();\n const invResp = await webln.sendPayment(inv);\n\n store.setItem(\n url,\n JSON.stringify({\n token: token,\n preimage: invResp.preimage,\n }),\n );\n\n fetchArgs.headers[\"Authorization\"] =\n `${headerKey} ${token}:${invResp.preimage}`;\n return await fetch(url, fetchArgs);\n};\n"],"names":[],"mappings":";;MAKa,aAAa,CAAA;AAGxB,IAAA,WAAA,CAAY,OAAiC,EAAA;AAC3C,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,EAAE;IAC9B;AAEA,IAAA,OAAO,CAAC,GAAW,EAAA;AACjB,QAAA,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC;IAC1B;IAEA,OAAO,CAAC,GAAW,EAAE,KAAc,EAAA;AACjC,QAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK;IAC3B;AACD;MAEY,SAAS,CAAA;IACpB,WAAA,CAAY,OAAiB,IAAG;AAEhC,IAAA,OAAO,CAAC,GAAW,EAAA;AACjB,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,OAAO,CAAC,GAAW,EAAE,KAAc,IAAG;AACvC;AAEM,MAAM,SAAS,GAAG,CAAC,KAAa,KAA4B;;IAEjE,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE;;IAGnE,MAAM,aAAa,GAAG,EAAE;;IAGxB,MAAM,KAAK,GAAG,sCAAsC;AACpD,IAAA,IAAI,KAAK;;AAGT,IAAA,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE;;;QAG5C,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC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SAAS,CAAC;IAC5C,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;IACvD,IAAI,CAAC,MAAM,EAAE;AACX,QAAA,OAAO,QAAQ;IACjB;AAEA,IAAA,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC;IACjC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,QAAQ;AAC/C,IAAA,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO;AAE3B,IAAA,MAAM,KAAK,CAAC,MAAM,EAAE;IACpB,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC;IAE5C,KAAK,CAAC,OAAO,CACX,GAAG,EACH,IAAI,CAAC,SAAS,CAAC;AACb,QAAA,KAAK,EAAE,KAAK;QACZ,QAAQ,EAAE,OAAO,CAAC,QAAQ;AAC3B,KAAA,CAAC,CACH;AAED,IAAA,SAAS,CAAC,OAAO,CAAC,eAAe,CAAC;QAChC,CAAA,EAAG,SAAS,IAAI,KAAK,CAAA,CAAA,EAAI,OAAO,CAAC,QAAQ,EAAE;AAC7C,IAAA,OAAO,MAAM,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC;AACpC;;;;;;;"}
|
package/dist/esm/l402.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
class MemoryStorage {
|
|
2
|
-
constructor(initial) {
|
|
3
|
-
this.storage = initial || {};
|
|
4
|
-
}
|
|
5
|
-
getItem(key) {
|
|
6
|
-
return this.storage[key];
|
|
7
|
-
}
|
|
8
|
-
setItem(key, value) {
|
|
9
|
-
this.storage[key] = value;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
class NoStorage {
|
|
13
|
-
constructor(initial) { }
|
|
14
|
-
getItem(key) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
setItem(key, value) { }
|
|
18
|
-
}
|
|
19
|
-
const parseL402 = (input) => {
|
|
20
|
-
// Remove the L402 and LSAT identifiers
|
|
21
|
-
const string = input.replace("L402", "").replace("LSAT", "").trim();
|
|
22
|
-
// Initialize an object to store the key-value pairs
|
|
23
|
-
const keyValuePairs = {};
|
|
24
|
-
// Regular expression to match key and (quoted or unquoted) value
|
|
25
|
-
const regex = /(\w+)=("([^"]*)"|'([^']*)'|([^,]*))/g;
|
|
26
|
-
let match;
|
|
27
|
-
// Use regex to find all key-value pairs
|
|
28
|
-
while ((match = regex.exec(string)) !== null) {
|
|
29
|
-
// Key is always match[1]
|
|
30
|
-
// Value is either match[3] (double-quoted), match[4] (single-quoted), or match[5] (unquoted)
|
|
31
|
-
keyValuePairs[match[1]] = match[3] || match[4] || match[5];
|
|
32
|
-
}
|
|
33
|
-
return keyValuePairs;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const memoryStorage = new MemoryStorage();
|
|
37
|
-
const HEADER_KEY = "L402"; // we have to update this to L402 at some point
|
|
38
|
-
const fetchWithL402 = async (url, fetchArgs, options) => {
|
|
39
|
-
if (!options) {
|
|
40
|
-
options = {};
|
|
41
|
-
}
|
|
42
|
-
const headerKey = options.headerKey || HEADER_KEY;
|
|
43
|
-
const webln = options.webln || globalThis.webln;
|
|
44
|
-
if (!webln) {
|
|
45
|
-
throw new Error("WebLN is missing");
|
|
46
|
-
}
|
|
47
|
-
const store = options.store || memoryStorage;
|
|
48
|
-
if (!fetchArgs) {
|
|
49
|
-
fetchArgs = {};
|
|
50
|
-
}
|
|
51
|
-
fetchArgs.cache = "no-store";
|
|
52
|
-
fetchArgs.mode = "cors";
|
|
53
|
-
if (!fetchArgs.headers) {
|
|
54
|
-
fetchArgs.headers = {};
|
|
55
|
-
}
|
|
56
|
-
const cachedL402Data = store.getItem(url);
|
|
57
|
-
if (cachedL402Data) {
|
|
58
|
-
const data = JSON.parse(cachedL402Data);
|
|
59
|
-
fetchArgs.headers["Authorization"] =
|
|
60
|
-
`${headerKey} ${data.token}:${data.preimage}`;
|
|
61
|
-
return await fetch(url, fetchArgs);
|
|
62
|
-
}
|
|
63
|
-
fetchArgs.headers["Accept-Authenticate"] = headerKey;
|
|
64
|
-
const initResp = await fetch(url, fetchArgs);
|
|
65
|
-
const header = initResp.headers.get("www-authenticate");
|
|
66
|
-
if (!header) {
|
|
67
|
-
return initResp;
|
|
68
|
-
}
|
|
69
|
-
const details = parseL402(header);
|
|
70
|
-
const token = details.token || details.macaroon;
|
|
71
|
-
const inv = details.invoice;
|
|
72
|
-
await webln.enable();
|
|
73
|
-
const invResp = await webln.sendPayment(inv);
|
|
74
|
-
store.setItem(url, JSON.stringify({
|
|
75
|
-
token: token,
|
|
76
|
-
preimage: invResp.preimage,
|
|
77
|
-
}));
|
|
78
|
-
fetchArgs.headers["Authorization"] =
|
|
79
|
-
`${headerKey} ${token}:${invResp.preimage}`;
|
|
80
|
-
return await fetch(url, fetchArgs);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export { MemoryStorage, NoStorage, fetchWithL402, parseL402 };
|
|
84
|
-
//# sourceMappingURL=l402.js.map
|
package/dist/esm/l402.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"l402.js","sources":["../../src/l402/utils.ts","../../src/l402/l402.ts"],"sourcesContent":["export interface KVStorage {\n getItem(key: string): string | null;\n setItem(key: string, value: string): void;\n}\n\nexport class MemoryStorage implements KVStorage {\n storage;\n\n constructor(initial?: Record<string, unknown>) {\n this.storage = initial || {};\n }\n\n getItem(key: string) {\n return this.storage[key];\n }\n\n setItem(key: string, value: unknown) {\n this.storage[key] = value;\n }\n}\n\nexport class NoStorage implements KVStorage {\n constructor(initial?: unknown) {}\n\n getItem(key: string) {\n return null;\n }\n\n setItem(key: string, value: unknown) {}\n}\n\nexport const parseL402 = (input: string): Record<string, string> => {\n // Remove the L402 and LSAT identifiers\n const string = input.replace(\"L402\", \"\").replace(\"LSAT\", \"\").trim();\n\n // Initialize an object to store the key-value pairs\n const keyValuePairs = {};\n\n // Regular expression to match key and (quoted or unquoted) value\n const regex = /(\\w+)=(\"([^\"]*)\"|'([^']*)'|([^,]*))/g;\n let match;\n\n // Use regex to find all key-value pairs\n while ((match = regex.exec(string)) !== null) {\n // Key is always match[1]\n // Value is either match[3] (double-quoted), match[4] (single-quoted), or match[5] (unquoted)\n keyValuePairs[match[1]] = match[3] || match[4] || match[5];\n }\n\n return keyValuePairs;\n};\n","import { KVStorage, MemoryStorage, parseL402 } from \"./utils\";\nimport { WebLNProvider } from \"@webbtc/webln-types\";\n\nconst memoryStorage = new MemoryStorage();\n\nconst HEADER_KEY = \"L402\"; // we have to update this to L402 at some point\n\nexport const fetchWithL402 = async (\n url: string,\n fetchArgs: RequestInit,\n options: {\n headerKey?: string;\n webln?: WebLNProvider;\n store?: KVStorage;\n },\n) => {\n if (!options) {\n options = {};\n }\n const headerKey = options.headerKey || HEADER_KEY;\n const webln: WebLNProvider = options.webln || globalThis.webln;\n if (!webln) {\n throw new Error(\"WebLN is missing\");\n }\n const store = options.store || memoryStorage;\n if (!fetchArgs) {\n fetchArgs = {};\n }\n fetchArgs.cache = \"no-store\";\n fetchArgs.mode = \"cors\";\n if (!fetchArgs.headers) {\n fetchArgs.headers = {};\n }\n const cachedL402Data = store.getItem(url);\n if (cachedL402Data) {\n const data = JSON.parse(cachedL402Data);\n fetchArgs.headers[\"Authorization\"] =\n `${headerKey} ${data.token}:${data.preimage}`;\n return await fetch(url, fetchArgs);\n }\n\n fetchArgs.headers[\"Accept-Authenticate\"] = headerKey;\n const initResp = await fetch(url, fetchArgs);\n const header = initResp.headers.get(\"www-authenticate\");\n if (!header) {\n return initResp;\n }\n\n const details = parseL402(header);\n const token = details.token || details.macaroon;\n const inv = details.invoice;\n\n await webln.enable();\n const invResp = await webln.sendPayment(inv);\n\n store.setItem(\n url,\n JSON.stringify({\n token: token,\n preimage: invResp.preimage,\n }),\n );\n\n fetchArgs.headers[\"Authorization\"] =\n `${headerKey} ${token}:${invResp.preimage}`;\n return await fetch(url, fetch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IACjC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,QAAQ;AAC/C,IAAA,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO;AAE3B,IAAA,MAAM,KAAK,CAAC,MAAM,EAAE;IACpB,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC;IAE5C,KAAK,CAAC,OAAO,CACX,GAAG,EACH,IAAI,CAAC,SAAS,CAAC;AACb,QAAA,KAAK,EAAE,KAAK;QACZ,QAAQ,EAAE,OAAO,CAAC,QAAQ;AAC3B,KAAA,CAAC,CACH;AAED,IAAA,SAAS,CAAC,OAAO,CAAC,eAAe,CAAC;QAChC,CAAA,EAAG,SAAS,IAAI,KAAK,CAAA,CAAA,EAAI,OAAO,CAAC,QAAQ,EAAE;AAC7C,IAAA,OAAO,MAAM,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC;AACpC;;;;"}
|
package/dist/types/l402.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { WebLNProvider } from '@webbtc/webln-types';
|
|
2
|
-
|
|
3
|
-
interface KVStorage {
|
|
4
|
-
getItem(key: string): string | null;
|
|
5
|
-
setItem(key: string, value: string): void;
|
|
6
|
-
}
|
|
7
|
-
declare class MemoryStorage implements KVStorage {
|
|
8
|
-
storage: any;
|
|
9
|
-
constructor(initial?: Record<string, unknown>);
|
|
10
|
-
getItem(key: string): any;
|
|
11
|
-
setItem(key: string, value: unknown): void;
|
|
12
|
-
}
|
|
13
|
-
declare class NoStorage implements KVStorage {
|
|
14
|
-
constructor(initial?: unknown);
|
|
15
|
-
getItem(key: string): null;
|
|
16
|
-
setItem(key: string, value: unknown): void;
|
|
17
|
-
}
|
|
18
|
-
declare const parseL402: (input: string) => Record<string, string>;
|
|
19
|
-
|
|
20
|
-
declare const fetchWithL402: (url: string, fetchArgs: RequestInit, options: {
|
|
21
|
-
headerKey?: string;
|
|
22
|
-
webln?: WebLNProvider;
|
|
23
|
-
store?: KVStorage;
|
|
24
|
-
}) => Promise<Response>;
|
|
25
|
-
|
|
26
|
-
export { MemoryStorage, NoStorage, fetchWithL402, parseL402 };
|
|
27
|
-
export type { KVStorage };
|