@getalby/lightning-tools 8.1.0 → 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.
@@ -1,5 +1,1287 @@
1
1
  'use strict';
2
2
 
3
+ function bytes(b, ...lengths) {
4
+ if (!(b instanceof Uint8Array))
5
+ throw new Error('Expected Uint8Array');
6
+ if (lengths.length > 0 && !lengths.includes(b.length))
7
+ throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
8
+ }
9
+ function exists(instance, checkFinished = true) {
10
+ if (instance.destroyed)
11
+ throw new Error('Hash instance has been destroyed');
12
+ if (checkFinished && instance.finished)
13
+ throw new Error('Hash#digest() has already been called');
14
+ }
15
+ function output(out, instance) {
16
+ bytes(out);
17
+ const min = instance.outputLen;
18
+ if (out.length < min) {
19
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
20
+ }
21
+ }
22
+
23
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
24
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
25
+ // node.js versions earlier than v19 don't declare it in global scope.
26
+ // For node.js, package.json#exports field mapping rewrites import
27
+ // from `crypto` to `cryptoNode`, which imports native module.
28
+ // Makes the utils un-importable in browsers without a bundler.
29
+ // Once node.js 18 is deprecated, we can just drop the import.
30
+ const u8a = (a) => a instanceof Uint8Array;
31
+ // Cast array to view
32
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
33
+ // The rotate right (circular right shift) operation for uint32
34
+ const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
35
+ // big-endian hardware is rare. Just in case someone still decides to run hashes:
36
+ // early-throw an error because we don't support BE yet.
37
+ const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
38
+ if (!isLE)
39
+ throw new Error('Non little-endian hardware is not supported');
40
+ const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
41
+ /**
42
+ * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
43
+ */
44
+ function bytesToHex(bytes) {
45
+ if (!u8a(bytes))
46
+ throw new Error('Uint8Array expected');
47
+ // pre-caching improves the speed 6x
48
+ let hex = '';
49
+ for (let i = 0; i < bytes.length; i++) {
50
+ hex += hexes[bytes[i]];
51
+ }
52
+ return hex;
53
+ }
54
+ /**
55
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
56
+ */
57
+ function utf8ToBytes(str) {
58
+ if (typeof str !== 'string')
59
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
60
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
61
+ }
62
+ /**
63
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
64
+ * Warning: when Uint8Array is passed, it would NOT get copied.
65
+ * Keep in mind for future mutable operations.
66
+ */
67
+ function toBytes(data) {
68
+ if (typeof data === 'string')
69
+ data = utf8ToBytes(data);
70
+ if (!u8a(data))
71
+ throw new Error(`expected Uint8Array, got ${typeof data}`);
72
+ return data;
73
+ }
74
+ // For runtime check if class implements interface
75
+ class Hash {
76
+ // Safe version that clones internal state
77
+ clone() {
78
+ return this._cloneInto();
79
+ }
80
+ }
81
+ function wrapConstructor(hashCons) {
82
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
83
+ const tmp = hashCons();
84
+ hashC.outputLen = tmp.outputLen;
85
+ hashC.blockLen = tmp.blockLen;
86
+ hashC.create = () => hashCons();
87
+ return hashC;
88
+ }
89
+
90
+ // Polyfill for Safari 14
91
+ function setBigUint64(view, byteOffset, value, isLE) {
92
+ if (typeof view.setBigUint64 === 'function')
93
+ return view.setBigUint64(byteOffset, value, isLE);
94
+ const _32n = BigInt(32);
95
+ const _u32_max = BigInt(0xffffffff);
96
+ const wh = Number((value >> _32n) & _u32_max);
97
+ const wl = Number(value & _u32_max);
98
+ const h = isLE ? 4 : 0;
99
+ const l = isLE ? 0 : 4;
100
+ view.setUint32(byteOffset + h, wh, isLE);
101
+ view.setUint32(byteOffset + l, wl, isLE);
102
+ }
103
+ // Base SHA2 class (RFC 6234)
104
+ class SHA2 extends Hash {
105
+ constructor(blockLen, outputLen, padOffset, isLE) {
106
+ super();
107
+ this.blockLen = blockLen;
108
+ this.outputLen = outputLen;
109
+ this.padOffset = padOffset;
110
+ this.isLE = isLE;
111
+ this.finished = false;
112
+ this.length = 0;
113
+ this.pos = 0;
114
+ this.destroyed = false;
115
+ this.buffer = new Uint8Array(blockLen);
116
+ this.view = createView(this.buffer);
117
+ }
118
+ update(data) {
119
+ exists(this);
120
+ const { view, buffer, blockLen } = this;
121
+ data = toBytes(data);
122
+ const len = data.length;
123
+ for (let pos = 0; pos < len;) {
124
+ const take = Math.min(blockLen - this.pos, len - pos);
125
+ // Fast path: we have at least one block in input, cast it to view and process
126
+ if (take === blockLen) {
127
+ const dataView = createView(data);
128
+ for (; blockLen <= len - pos; pos += blockLen)
129
+ this.process(dataView, pos);
130
+ continue;
131
+ }
132
+ buffer.set(data.subarray(pos, pos + take), this.pos);
133
+ this.pos += take;
134
+ pos += take;
135
+ if (this.pos === blockLen) {
136
+ this.process(view, 0);
137
+ this.pos = 0;
138
+ }
139
+ }
140
+ this.length += data.length;
141
+ this.roundClean();
142
+ return this;
143
+ }
144
+ digestInto(out) {
145
+ exists(this);
146
+ output(out, this);
147
+ this.finished = true;
148
+ // Padding
149
+ // We can avoid allocation of buffer for padding completely if it
150
+ // was previously not allocated here. But it won't change performance.
151
+ const { buffer, view, blockLen, isLE } = this;
152
+ let { pos } = this;
153
+ // append the bit '1' to the message
154
+ buffer[pos++] = 0b10000000;
155
+ this.buffer.subarray(pos).fill(0);
156
+ // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
157
+ if (this.padOffset > blockLen - pos) {
158
+ this.process(view, 0);
159
+ pos = 0;
160
+ }
161
+ // Pad until full block byte with zeros
162
+ for (let i = pos; i < blockLen; i++)
163
+ buffer[i] = 0;
164
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
165
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
166
+ // So we just write lowest 64 bits of that value.
167
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
168
+ this.process(view, 0);
169
+ const oview = createView(out);
170
+ const len = this.outputLen;
171
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
172
+ if (len % 4)
173
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
174
+ const outLen = len / 4;
175
+ const state = this.get();
176
+ if (outLen > state.length)
177
+ throw new Error('_sha2: outputLen bigger than state');
178
+ for (let i = 0; i < outLen; i++)
179
+ oview.setUint32(4 * i, state[i], isLE);
180
+ }
181
+ digest() {
182
+ const { buffer, outputLen } = this;
183
+ this.digestInto(buffer);
184
+ const res = buffer.slice(0, outputLen);
185
+ this.destroy();
186
+ return res;
187
+ }
188
+ _cloneInto(to) {
189
+ to || (to = new this.constructor());
190
+ to.set(...this.get());
191
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
192
+ to.length = length;
193
+ to.pos = pos;
194
+ to.finished = finished;
195
+ to.destroyed = destroyed;
196
+ if (length % blockLen)
197
+ to.buffer.set(buffer);
198
+ return to;
199
+ }
200
+ }
201
+
202
+ // SHA2-256 need to try 2^128 hashes to execute birthday attack.
203
+ // BTC network is doing 2^67 hashes/sec as per early 2023.
204
+ // Choice: a ? b : c
205
+ const Chi = (a, b, c) => (a & b) ^ (~a & c);
206
+ // Majority function, true if any two inpust is true
207
+ const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
208
+ // Round constants:
209
+ // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
210
+ // prettier-ignore
211
+ const SHA256_K = /* @__PURE__ */ new Uint32Array([
212
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
213
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
214
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
215
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
216
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
217
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
218
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
219
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
220
+ ]);
221
+ // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
222
+ // prettier-ignore
223
+ const IV = /* @__PURE__ */ new Uint32Array([
224
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
225
+ ]);
226
+ // Temporary buffer, not used to store anything between runs
227
+ // Named this way because it matches specification.
228
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
229
+ class SHA256 extends SHA2 {
230
+ constructor() {
231
+ super(64, 32, 8, false);
232
+ // We cannot use array here since array allows indexing by variable
233
+ // which means optimizer/compiler cannot use registers.
234
+ this.A = IV[0] | 0;
235
+ this.B = IV[1] | 0;
236
+ this.C = IV[2] | 0;
237
+ this.D = IV[3] | 0;
238
+ this.E = IV[4] | 0;
239
+ this.F = IV[5] | 0;
240
+ this.G = IV[6] | 0;
241
+ this.H = IV[7] | 0;
242
+ }
243
+ get() {
244
+ const { A, B, C, D, E, F, G, H } = this;
245
+ return [A, B, C, D, E, F, G, H];
246
+ }
247
+ // prettier-ignore
248
+ set(A, B, C, D, E, F, G, H) {
249
+ this.A = A | 0;
250
+ this.B = B | 0;
251
+ this.C = C | 0;
252
+ this.D = D | 0;
253
+ this.E = E | 0;
254
+ this.F = F | 0;
255
+ this.G = G | 0;
256
+ this.H = H | 0;
257
+ }
258
+ process(view, offset) {
259
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
260
+ for (let i = 0; i < 16; i++, offset += 4)
261
+ SHA256_W[i] = view.getUint32(offset, false);
262
+ for (let i = 16; i < 64; i++) {
263
+ const W15 = SHA256_W[i - 15];
264
+ const W2 = SHA256_W[i - 2];
265
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
266
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
267
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
268
+ }
269
+ // Compression function main loop, 64 rounds
270
+ let { A, B, C, D, E, F, G, H } = this;
271
+ for (let i = 0; i < 64; i++) {
272
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
273
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
274
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
275
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
276
+ H = G;
277
+ G = F;
278
+ F = E;
279
+ E = (D + T1) | 0;
280
+ D = C;
281
+ C = B;
282
+ B = A;
283
+ A = (T1 + T2) | 0;
284
+ }
285
+ // Add the compressed chunk to the current hash value
286
+ A = (A + this.A) | 0;
287
+ B = (B + this.B) | 0;
288
+ C = (C + this.C) | 0;
289
+ D = (D + this.D) | 0;
290
+ E = (E + this.E) | 0;
291
+ F = (F + this.F) | 0;
292
+ G = (G + this.G) | 0;
293
+ H = (H + this.H) | 0;
294
+ this.set(A, B, C, D, E, F, G, H);
295
+ }
296
+ roundClean() {
297
+ SHA256_W.fill(0);
298
+ }
299
+ destroy() {
300
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
301
+ this.buffer.fill(0);
302
+ }
303
+ }
304
+ /**
305
+ * SHA2-256 hash function
306
+ * @param message - data that would be hashed
307
+ */
308
+ const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
309
+
310
+ var lib = {};
311
+
312
+ var hasRequiredLib;
313
+
314
+ function requireLib () {
315
+ if (hasRequiredLib) return lib;
316
+ hasRequiredLib = 1;
317
+ (function (exports) {
318
+ /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
319
+ Object.defineProperty(exports, "__esModule", { value: true });
320
+ exports.bytes = exports.stringToBytes = exports.str = exports.bytesToString = exports.hex = exports.utf8 = exports.bech32m = exports.bech32 = exports.base58check = exports.base58xmr = exports.base58xrp = exports.base58flickr = exports.base58 = exports.base64url = exports.base64 = exports.base32crockford = exports.base32hex = exports.base32 = exports.base16 = exports.utils = exports.assertNumber = void 0;
321
+ function assertNumber(n) {
322
+ if (!Number.isSafeInteger(n))
323
+ throw new Error(`Wrong integer: ${n}`);
324
+ }
325
+ exports.assertNumber = assertNumber;
326
+ function chain(...args) {
327
+ const wrap = (a, b) => (c) => a(b(c));
328
+ const encode = Array.from(args)
329
+ .reverse()
330
+ .reduce((acc, i) => (acc ? wrap(acc, i.encode) : i.encode), undefined);
331
+ const decode = args.reduce((acc, i) => (acc ? wrap(acc, i.decode) : i.decode), undefined);
332
+ return { encode, decode };
333
+ }
334
+ function alphabet(alphabet) {
335
+ return {
336
+ encode: (digits) => {
337
+ if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number'))
338
+ throw new Error('alphabet.encode input should be an array of numbers');
339
+ return digits.map((i) => {
340
+ assertNumber(i);
341
+ if (i < 0 || i >= alphabet.length)
342
+ throw new Error(`Digit index outside alphabet: ${i} (alphabet: ${alphabet.length})`);
343
+ return alphabet[i];
344
+ });
345
+ },
346
+ decode: (input) => {
347
+ if (!Array.isArray(input) || (input.length && typeof input[0] !== 'string'))
348
+ throw new Error('alphabet.decode input should be array of strings');
349
+ return input.map((letter) => {
350
+ if (typeof letter !== 'string')
351
+ throw new Error(`alphabet.decode: not string element=${letter}`);
352
+ const index = alphabet.indexOf(letter);
353
+ if (index === -1)
354
+ throw new Error(`Unknown letter: "${letter}". Allowed: ${alphabet}`);
355
+ return index;
356
+ });
357
+ },
358
+ };
359
+ }
360
+ function join(separator = '') {
361
+ if (typeof separator !== 'string')
362
+ throw new Error('join separator should be string');
363
+ return {
364
+ encode: (from) => {
365
+ if (!Array.isArray(from) || (from.length && typeof from[0] !== 'string'))
366
+ throw new Error('join.encode input should be array of strings');
367
+ for (let i of from)
368
+ if (typeof i !== 'string')
369
+ throw new Error(`join.encode: non-string input=${i}`);
370
+ return from.join(separator);
371
+ },
372
+ decode: (to) => {
373
+ if (typeof to !== 'string')
374
+ throw new Error('join.decode input should be string');
375
+ return to.split(separator);
376
+ },
377
+ };
378
+ }
379
+ function padding(bits, chr = '=') {
380
+ assertNumber(bits);
381
+ if (typeof chr !== 'string')
382
+ throw new Error('padding chr should be string');
383
+ return {
384
+ encode(data) {
385
+ if (!Array.isArray(data) || (data.length && typeof data[0] !== 'string'))
386
+ throw new Error('padding.encode input should be array of strings');
387
+ for (let i of data)
388
+ if (typeof i !== 'string')
389
+ throw new Error(`padding.encode: non-string input=${i}`);
390
+ while ((data.length * bits) % 8)
391
+ data.push(chr);
392
+ return data;
393
+ },
394
+ decode(input) {
395
+ if (!Array.isArray(input) || (input.length && typeof input[0] !== 'string'))
396
+ throw new Error('padding.encode input should be array of strings');
397
+ for (let i of input)
398
+ if (typeof i !== 'string')
399
+ throw new Error(`padding.decode: non-string input=${i}`);
400
+ let end = input.length;
401
+ if ((end * bits) % 8)
402
+ throw new Error('Invalid padding: string should have whole number of bytes');
403
+ for (; end > 0 && input[end - 1] === chr; end--) {
404
+ if (!(((end - 1) * bits) % 8))
405
+ throw new Error('Invalid padding: string has too much padding');
406
+ }
407
+ return input.slice(0, end);
408
+ },
409
+ };
410
+ }
411
+ function normalize(fn) {
412
+ if (typeof fn !== 'function')
413
+ throw new Error('normalize fn should be function');
414
+ return { encode: (from) => from, decode: (to) => fn(to) };
415
+ }
416
+ function convertRadix(data, from, to) {
417
+ if (from < 2)
418
+ throw new Error(`convertRadix: wrong from=${from}, base cannot be less than 2`);
419
+ if (to < 2)
420
+ throw new Error(`convertRadix: wrong to=${to}, base cannot be less than 2`);
421
+ if (!Array.isArray(data))
422
+ throw new Error('convertRadix: data should be array');
423
+ if (!data.length)
424
+ return [];
425
+ let pos = 0;
426
+ const res = [];
427
+ const digits = Array.from(data);
428
+ digits.forEach((d) => {
429
+ assertNumber(d);
430
+ if (d < 0 || d >= from)
431
+ throw new Error(`Wrong integer: ${d}`);
432
+ });
433
+ while (true) {
434
+ let carry = 0;
435
+ let done = true;
436
+ for (let i = pos; i < digits.length; i++) {
437
+ const digit = digits[i];
438
+ const digitBase = from * carry + digit;
439
+ if (!Number.isSafeInteger(digitBase) ||
440
+ (from * carry) / from !== carry ||
441
+ digitBase - digit !== from * carry) {
442
+ throw new Error('convertRadix: carry overflow');
443
+ }
444
+ carry = digitBase % to;
445
+ digits[i] = Math.floor(digitBase / to);
446
+ if (!Number.isSafeInteger(digits[i]) || digits[i] * to + carry !== digitBase)
447
+ throw new Error('convertRadix: carry overflow');
448
+ if (!done)
449
+ continue;
450
+ else if (!digits[i])
451
+ pos = i;
452
+ else
453
+ done = false;
454
+ }
455
+ res.push(carry);
456
+ if (done)
457
+ break;
458
+ }
459
+ for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
460
+ res.push(0);
461
+ return res.reverse();
462
+ }
463
+ const gcd = (a, b) => (!b ? a : gcd(b, a % b));
464
+ const radix2carry = (from, to) => from + (to - gcd(from, to));
465
+ function convertRadix2(data, from, to, padding) {
466
+ if (!Array.isArray(data))
467
+ throw new Error('convertRadix2: data should be array');
468
+ if (from <= 0 || from > 32)
469
+ throw new Error(`convertRadix2: wrong from=${from}`);
470
+ if (to <= 0 || to > 32)
471
+ throw new Error(`convertRadix2: wrong to=${to}`);
472
+ if (radix2carry(from, to) > 32) {
473
+ throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${radix2carry(from, to)}`);
474
+ }
475
+ let carry = 0;
476
+ let pos = 0;
477
+ const mask = 2 ** to - 1;
478
+ const res = [];
479
+ for (const n of data) {
480
+ assertNumber(n);
481
+ if (n >= 2 ** from)
482
+ throw new Error(`convertRadix2: invalid data word=${n} from=${from}`);
483
+ carry = (carry << from) | n;
484
+ if (pos + from > 32)
485
+ throw new Error(`convertRadix2: carry overflow pos=${pos} from=${from}`);
486
+ pos += from;
487
+ for (; pos >= to; pos -= to)
488
+ res.push(((carry >> (pos - to)) & mask) >>> 0);
489
+ carry &= 2 ** pos - 1;
490
+ }
491
+ carry = (carry << (to - pos)) & mask;
492
+ if (!padding && pos >= from)
493
+ throw new Error('Excess padding');
494
+ if (!padding && carry)
495
+ throw new Error(`Non-zero padding: ${carry}`);
496
+ if (padding && pos > 0)
497
+ res.push(carry >>> 0);
498
+ return res;
499
+ }
500
+ function radix(num) {
501
+ assertNumber(num);
502
+ return {
503
+ encode: (bytes) => {
504
+ if (!(bytes instanceof Uint8Array))
505
+ throw new Error('radix.encode input should be Uint8Array');
506
+ return convertRadix(Array.from(bytes), 2 ** 8, num);
507
+ },
508
+ decode: (digits) => {
509
+ if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number'))
510
+ throw new Error('radix.decode input should be array of strings');
511
+ return Uint8Array.from(convertRadix(digits, num, 2 ** 8));
512
+ },
513
+ };
514
+ }
515
+ function radix2(bits, revPadding = false) {
516
+ assertNumber(bits);
517
+ if (bits <= 0 || bits > 32)
518
+ throw new Error('radix2: bits should be in (0..32]');
519
+ if (radix2carry(8, bits) > 32 || radix2carry(bits, 8) > 32)
520
+ throw new Error('radix2: carry overflow');
521
+ return {
522
+ encode: (bytes) => {
523
+ if (!(bytes instanceof Uint8Array))
524
+ throw new Error('radix2.encode input should be Uint8Array');
525
+ return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
526
+ },
527
+ decode: (digits) => {
528
+ if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number'))
529
+ throw new Error('radix2.decode input should be array of strings');
530
+ return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding));
531
+ },
532
+ };
533
+ }
534
+ function unsafeWrapper(fn) {
535
+ if (typeof fn !== 'function')
536
+ throw new Error('unsafeWrapper fn should be function');
537
+ return function (...args) {
538
+ try {
539
+ return fn.apply(null, args);
540
+ }
541
+ catch (e) { }
542
+ };
543
+ }
544
+ function checksum(len, fn) {
545
+ assertNumber(len);
546
+ if (typeof fn !== 'function')
547
+ throw new Error('checksum fn should be function');
548
+ return {
549
+ encode(data) {
550
+ if (!(data instanceof Uint8Array))
551
+ throw new Error('checksum.encode: input should be Uint8Array');
552
+ const checksum = fn(data).slice(0, len);
553
+ const res = new Uint8Array(data.length + len);
554
+ res.set(data);
555
+ res.set(checksum, data.length);
556
+ return res;
557
+ },
558
+ decode(data) {
559
+ if (!(data instanceof Uint8Array))
560
+ throw new Error('checksum.decode: input should be Uint8Array');
561
+ const payload = data.slice(0, -len);
562
+ const newChecksum = fn(payload).slice(0, len);
563
+ const oldChecksum = data.slice(-len);
564
+ for (let i = 0; i < len; i++)
565
+ if (newChecksum[i] !== oldChecksum[i])
566
+ throw new Error('Invalid checksum');
567
+ return payload;
568
+ },
569
+ };
570
+ }
571
+ exports.utils = { alphabet, chain, checksum, radix, radix2, join, padding };
572
+ exports.base16 = chain(radix2(4), alphabet('0123456789ABCDEF'), join(''));
573
+ exports.base32 = chain(radix2(5), alphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'), padding(5), join(''));
574
+ exports.base32hex = chain(radix2(5), alphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV'), padding(5), join(''));
575
+ exports.base32crockford = chain(radix2(5), alphabet('0123456789ABCDEFGHJKMNPQRSTVWXYZ'), join(''), normalize((s) => s.toUpperCase().replace(/O/g, '0').replace(/[IL]/g, '1')));
576
+ exports.base64 = chain(radix2(6), alphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'), padding(6), join(''));
577
+ exports.base64url = chain(radix2(6), alphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'), padding(6), join(''));
578
+ const genBase58 = (abc) => chain(radix(58), alphabet(abc), join(''));
579
+ exports.base58 = genBase58('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
580
+ exports.base58flickr = genBase58('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ');
581
+ exports.base58xrp = genBase58('rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz');
582
+ const XMR_BLOCK_LEN = [0, 2, 3, 5, 6, 7, 9, 10, 11];
583
+ exports.base58xmr = {
584
+ encode(data) {
585
+ let res = '';
586
+ for (let i = 0; i < data.length; i += 8) {
587
+ const block = data.subarray(i, i + 8);
588
+ res += exports.base58.encode(block).padStart(XMR_BLOCK_LEN[block.length], '1');
589
+ }
590
+ return res;
591
+ },
592
+ decode(str) {
593
+ let res = [];
594
+ for (let i = 0; i < str.length; i += 11) {
595
+ const slice = str.slice(i, i + 11);
596
+ const blockLen = XMR_BLOCK_LEN.indexOf(slice.length);
597
+ const block = exports.base58.decode(slice);
598
+ for (let j = 0; j < block.length - blockLen; j++) {
599
+ if (block[j] !== 0)
600
+ throw new Error('base58xmr: wrong padding');
601
+ }
602
+ res = res.concat(Array.from(block.slice(block.length - blockLen)));
603
+ }
604
+ return Uint8Array.from(res);
605
+ },
606
+ };
607
+ const base58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), exports.base58);
608
+ exports.base58check = base58check;
609
+ const BECH_ALPHABET = chain(alphabet('qpzry9x8gf2tvdw0s3jn54khce6mua7l'), join(''));
610
+ const POLYMOD_GENERATORS = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
611
+ function bech32Polymod(pre) {
612
+ const b = pre >> 25;
613
+ let chk = (pre & 0x1ffffff) << 5;
614
+ for (let i = 0; i < POLYMOD_GENERATORS.length; i++) {
615
+ if (((b >> i) & 1) === 1)
616
+ chk ^= POLYMOD_GENERATORS[i];
617
+ }
618
+ return chk;
619
+ }
620
+ function bechChecksum(prefix, words, encodingConst = 1) {
621
+ const len = prefix.length;
622
+ let chk = 1;
623
+ for (let i = 0; i < len; i++) {
624
+ const c = prefix.charCodeAt(i);
625
+ if (c < 33 || c > 126)
626
+ throw new Error(`Invalid prefix (${prefix})`);
627
+ chk = bech32Polymod(chk) ^ (c >> 5);
628
+ }
629
+ chk = bech32Polymod(chk);
630
+ for (let i = 0; i < len; i++)
631
+ chk = bech32Polymod(chk) ^ (prefix.charCodeAt(i) & 0x1f);
632
+ for (let v of words)
633
+ chk = bech32Polymod(chk) ^ v;
634
+ for (let i = 0; i < 6; i++)
635
+ chk = bech32Polymod(chk);
636
+ chk ^= encodingConst;
637
+ return BECH_ALPHABET.encode(convertRadix2([chk % 2 ** 30], 30, 5, false));
638
+ }
639
+ function genBech32(encoding) {
640
+ const ENCODING_CONST = encoding === 'bech32' ? 1 : 0x2bc830a3;
641
+ const _words = radix2(5);
642
+ const fromWords = _words.decode;
643
+ const toWords = _words.encode;
644
+ const fromWordsUnsafe = unsafeWrapper(fromWords);
645
+ function encode(prefix, words, limit = 90) {
646
+ if (typeof prefix !== 'string')
647
+ throw new Error(`bech32.encode prefix should be string, not ${typeof prefix}`);
648
+ if (!Array.isArray(words) || (words.length && typeof words[0] !== 'number'))
649
+ throw new Error(`bech32.encode words should be array of numbers, not ${typeof words}`);
650
+ const actualLength = prefix.length + 7 + words.length;
651
+ if (limit !== false && actualLength > limit)
652
+ throw new TypeError(`Length ${actualLength} exceeds limit ${limit}`);
653
+ prefix = prefix.toLowerCase();
654
+ return `${prefix}1${BECH_ALPHABET.encode(words)}${bechChecksum(prefix, words, ENCODING_CONST)}`;
655
+ }
656
+ function decode(str, limit = 90) {
657
+ if (typeof str !== 'string')
658
+ throw new Error(`bech32.decode input should be string, not ${typeof str}`);
659
+ if (str.length < 8 || (limit !== false && str.length > limit))
660
+ throw new TypeError(`Wrong string length: ${str.length} (${str}). Expected (8..${limit})`);
661
+ const lowered = str.toLowerCase();
662
+ if (str !== lowered && str !== str.toUpperCase())
663
+ throw new Error(`String must be lowercase or uppercase`);
664
+ str = lowered;
665
+ const sepIndex = str.lastIndexOf('1');
666
+ if (sepIndex === 0 || sepIndex === -1)
667
+ throw new Error(`Letter "1" must be present between prefix and data only`);
668
+ const prefix = str.slice(0, sepIndex);
669
+ const _words = str.slice(sepIndex + 1);
670
+ if (_words.length < 6)
671
+ throw new Error('Data must be at least 6 characters long');
672
+ const words = BECH_ALPHABET.decode(_words).slice(0, -6);
673
+ const sum = bechChecksum(prefix, words, ENCODING_CONST);
674
+ if (!_words.endsWith(sum))
675
+ throw new Error(`Invalid checksum in ${str}: expected "${sum}"`);
676
+ return { prefix, words };
677
+ }
678
+ const decodeUnsafe = unsafeWrapper(decode);
679
+ function decodeToBytes(str) {
680
+ const { prefix, words } = decode(str, false);
681
+ return { prefix, words, bytes: fromWords(words) };
682
+ }
683
+ return { encode, decode, decodeToBytes, decodeUnsafe, fromWords, fromWordsUnsafe, toWords };
684
+ }
685
+ exports.bech32 = genBech32('bech32');
686
+ exports.bech32m = genBech32('bech32m');
687
+ exports.utf8 = {
688
+ encode: (data) => new TextDecoder().decode(data),
689
+ decode: (str) => new TextEncoder().encode(str),
690
+ };
691
+ exports.hex = chain(radix2(4), alphabet('0123456789abcdef'), join(''), normalize((s) => {
692
+ if (typeof s !== 'string' || s.length % 2)
693
+ throw new TypeError(`hex.decode: expected string, got ${typeof s} with length ${s.length}`);
694
+ return s.toLowerCase();
695
+ }));
696
+ const CODERS = {
697
+ utf8: exports.utf8, hex: exports.hex, base16: exports.base16, base32: exports.base32, base64: exports.base64, base64url: exports.base64url, base58: exports.base58, base58xmr: exports.base58xmr
698
+ };
699
+ const coderTypeError = `Invalid encoding type. Available types: ${Object.keys(CODERS).join(', ')}`;
700
+ const bytesToString = (type, bytes) => {
701
+ if (typeof type !== 'string' || !CODERS.hasOwnProperty(type))
702
+ throw new TypeError(coderTypeError);
703
+ if (!(bytes instanceof Uint8Array))
704
+ throw new TypeError('bytesToString() expects Uint8Array');
705
+ return CODERS[type].encode(bytes);
706
+ };
707
+ exports.bytesToString = bytesToString;
708
+ exports.str = exports.bytesToString;
709
+ const stringToBytes = (type, str) => {
710
+ if (!CODERS.hasOwnProperty(type))
711
+ throw new TypeError(coderTypeError);
712
+ if (typeof str !== 'string')
713
+ throw new TypeError('stringToBytes() expects string');
714
+ return CODERS[type].decode(str);
715
+ };
716
+ exports.stringToBytes = stringToBytes;
717
+ exports.bytes = exports.stringToBytes;
718
+ } (lib));
719
+ return lib;
720
+ }
721
+
722
+ var bolt11;
723
+ var hasRequiredBolt11;
724
+
725
+ function requireBolt11 () {
726
+ if (hasRequiredBolt11) return bolt11;
727
+ hasRequiredBolt11 = 1;
728
+ const {bech32, hex, utf8} = requireLib();
729
+
730
+ // defaults for encode; default timestamp is current time at call
731
+ const DEFAULTNETWORK = {
732
+ // default network is bitcoin
733
+ bech32: 'bc',
734
+ pubKeyHash: 0x00,
735
+ scriptHash: 0x05,
736
+ validWitnessVersions: [0]
737
+ };
738
+ const TESTNETWORK = {
739
+ bech32: 'tb',
740
+ pubKeyHash: 0x6f,
741
+ scriptHash: 0xc4,
742
+ validWitnessVersions: [0]
743
+ };
744
+ const SIGNETNETWORK = {
745
+ bech32: 'tbs',
746
+ pubKeyHash: 0x6f,
747
+ scriptHash: 0xc4,
748
+ validWitnessVersions: [0]
749
+ };
750
+ const REGTESTNETWORK = {
751
+ bech32: 'bcrt',
752
+ pubKeyHash: 0x6f,
753
+ scriptHash: 0xc4,
754
+ validWitnessVersions: [0]
755
+ };
756
+ const SIMNETWORK = {
757
+ bech32: 'sb',
758
+ pubKeyHash: 0x3f,
759
+ scriptHash: 0x7b,
760
+ validWitnessVersions: [0]
761
+ };
762
+
763
+ const FEATUREBIT_ORDER = [
764
+ 'option_data_loss_protect',
765
+ 'initial_routing_sync',
766
+ 'option_upfront_shutdown_script',
767
+ 'gossip_queries',
768
+ 'var_onion_optin',
769
+ 'gossip_queries_ex',
770
+ 'option_static_remotekey',
771
+ 'payment_secret',
772
+ 'basic_mpp',
773
+ 'option_support_large_channel'
774
+ ];
775
+
776
+ const DIVISORS = {
777
+ m: BigInt(1e3),
778
+ u: BigInt(1e6),
779
+ n: BigInt(1e9),
780
+ p: BigInt(1e12)
781
+ };
782
+
783
+ const MAX_MILLISATS = BigInt('2100000000000000000');
784
+
785
+ const MILLISATS_PER_BTC = BigInt(1e11);
786
+
787
+ const TAGCODES = {
788
+ payment_hash: 1,
789
+ payment_secret: 16,
790
+ description: 13,
791
+ payee: 19,
792
+ description_hash: 23, // commit to longer descriptions (used by lnurl-pay)
793
+ expiry: 6, // default: 3600 (1 hour)
794
+ min_final_cltv_expiry: 24, // default: 9
795
+ fallback_address: 9,
796
+ route_hint: 3, // for extra routing info (private etc.)
797
+ feature_bits: 5,
798
+ metadata: 27
799
+ };
800
+
801
+ // reverse the keys and values of TAGCODES and insert into TAGNAMES
802
+ const TAGNAMES = {};
803
+ for (let i = 0, keys = Object.keys(TAGCODES); i < keys.length; i++) {
804
+ const currentName = keys[i];
805
+ const currentCode = TAGCODES[keys[i]].toString();
806
+ TAGNAMES[currentCode] = currentName;
807
+ }
808
+
809
+ const TAGPARSERS = {
810
+ 1: words => hex.encode(bech32.fromWordsUnsafe(words)), // 256 bits
811
+ 16: words => hex.encode(bech32.fromWordsUnsafe(words)), // 256 bits
812
+ 13: words => utf8.encode(bech32.fromWordsUnsafe(words)), // string variable length
813
+ 19: words => hex.encode(bech32.fromWordsUnsafe(words)), // 264 bits
814
+ 23: words => hex.encode(bech32.fromWordsUnsafe(words)), // 256 bits
815
+ 27: words => hex.encode(bech32.fromWordsUnsafe(words)), // variable
816
+ 6: wordsToIntBE, // default: 3600 (1 hour)
817
+ 24: wordsToIntBE, // default: 9
818
+ 3: routingInfoParser, // for extra routing info (private etc.)
819
+ 5: featureBitsParser // keep feature bits as array of 5 bit words
820
+ };
821
+
822
+ function getUnknownParser(tagCode) {
823
+ return words => ({
824
+ tagCode: parseInt(tagCode),
825
+ words: bech32.encode('unknown', words, Number.MAX_SAFE_INTEGER)
826
+ })
827
+ }
828
+
829
+ function wordsToIntBE(words) {
830
+ return words.reverse().reduce((total, item, index) => {
831
+ return total + item * Math.pow(32, index)
832
+ }, 0)
833
+ }
834
+
835
+ // first convert from words to buffer, trimming padding where necessary
836
+ // parse in 51 byte chunks. See encoder for details.
837
+ function routingInfoParser(words) {
838
+ const routes = [];
839
+ let pubkey,
840
+ shortChannelId,
841
+ feeBaseMSats,
842
+ feeProportionalMillionths,
843
+ cltvExpiryDelta;
844
+ let routesBuffer = bech32.fromWordsUnsafe(words);
845
+ while (routesBuffer.length > 0) {
846
+ pubkey = hex.encode(routesBuffer.slice(0, 33)); // 33 bytes
847
+ shortChannelId = hex.encode(routesBuffer.slice(33, 41)); // 8 bytes
848
+ feeBaseMSats = parseInt(hex.encode(routesBuffer.slice(41, 45)), 16); // 4 bytes
849
+ feeProportionalMillionths = parseInt(
850
+ hex.encode(routesBuffer.slice(45, 49)),
851
+ 16
852
+ ); // 4 bytes
853
+ cltvExpiryDelta = parseInt(hex.encode(routesBuffer.slice(49, 51)), 16); // 2 bytes
854
+
855
+ routesBuffer = routesBuffer.slice(51);
856
+
857
+ routes.push({
858
+ pubkey,
859
+ short_channel_id: shortChannelId,
860
+ fee_base_msat: feeBaseMSats,
861
+ fee_proportional_millionths: feeProportionalMillionths,
862
+ cltv_expiry_delta: cltvExpiryDelta
863
+ });
864
+ }
865
+ return routes
866
+ }
867
+
868
+ function featureBitsParser(words) {
869
+ const bools = words
870
+ .slice()
871
+ .reverse()
872
+ .map(word => [
873
+ !!(word & 0b1),
874
+ !!(word & 0b10),
875
+ !!(word & 0b100),
876
+ !!(word & 0b1000),
877
+ !!(word & 0b10000)
878
+ ])
879
+ .reduce((finalArr, itemArr) => finalArr.concat(itemArr), []);
880
+ while (bools.length < FEATUREBIT_ORDER.length * 2) {
881
+ bools.push(false);
882
+ }
883
+
884
+ const featureBits = {};
885
+
886
+ FEATUREBIT_ORDER.forEach((featureName, index) => {
887
+ let status;
888
+ if (bools[index * 2]) {
889
+ status = 'required';
890
+ } else if (bools[index * 2 + 1]) {
891
+ status = 'supported';
892
+ } else {
893
+ status = 'unsupported';
894
+ }
895
+ featureBits[featureName] = status;
896
+ });
897
+
898
+ const extraBits = bools.slice(FEATUREBIT_ORDER.length * 2);
899
+ featureBits.extra_bits = {
900
+ start_bit: FEATUREBIT_ORDER.length * 2,
901
+ bits: extraBits,
902
+ has_required: extraBits.reduce(
903
+ (result, bit, index) =>
904
+ index % 2 !== 0 ? result || false : result || bit,
905
+ false
906
+ )
907
+ };
908
+
909
+ return featureBits
910
+ }
911
+
912
+ function hrpToMillisat(hrpString, outputString) {
913
+ let divisor, value;
914
+ if (hrpString.slice(-1).match(/^[munp]$/)) {
915
+ divisor = hrpString.slice(-1);
916
+ value = hrpString.slice(0, -1);
917
+ } else if (hrpString.slice(-1).match(/^[^munp0-9]$/)) {
918
+ throw new Error('Not a valid multiplier for the amount')
919
+ } else {
920
+ value = hrpString;
921
+ }
922
+
923
+ if (!value.match(/^\d+$/))
924
+ throw new Error('Not a valid human readable amount')
925
+
926
+ const valueBN = BigInt(value);
927
+
928
+ const millisatoshisBN = divisor
929
+ ? (valueBN * MILLISATS_PER_BTC) / DIVISORS[divisor]
930
+ : valueBN * MILLISATS_PER_BTC;
931
+
932
+ if (
933
+ (divisor === 'p' && !(valueBN % BigInt(10) === BigInt(0))) ||
934
+ millisatoshisBN > MAX_MILLISATS
935
+ ) {
936
+ throw new Error('Amount is outside of valid range')
937
+ }
938
+
939
+ return outputString ? millisatoshisBN.toString() : millisatoshisBN
940
+ }
941
+
942
+ // decode will only have extra comments that aren't covered in encode comments.
943
+ // also if anything is hard to read I'll comment.
944
+ function decode(paymentRequest, network) {
945
+ if (typeof paymentRequest !== 'string')
946
+ throw new Error('Lightning Payment Request must be string')
947
+ if (paymentRequest.slice(0, 2).toLowerCase() !== 'ln')
948
+ throw new Error('Not a proper lightning payment request')
949
+
950
+ const sections = [];
951
+ const decoded = bech32.decode(paymentRequest, Number.MAX_SAFE_INTEGER);
952
+ paymentRequest = paymentRequest.toLowerCase();
953
+ const prefix = decoded.prefix;
954
+ let words = decoded.words;
955
+ let letters = paymentRequest.slice(prefix.length + 1);
956
+ let sigWords = words.slice(-104);
957
+ words = words.slice(0, -104);
958
+
959
+ // Without reverse lookups, can't say that the multipier at the end must
960
+ // have a number before it, so instead we parse, and if the second group
961
+ // doesn't have anything, there's a good chance the last letter of the
962
+ // coin type got captured by the third group, so just re-regex without
963
+ // the number.
964
+ let prefixMatches = prefix.match(/^ln(\S+?)(\d*)([a-zA-Z]?)$/);
965
+ if (prefixMatches && !prefixMatches[2])
966
+ prefixMatches = prefix.match(/^ln(\S+)$/);
967
+ if (!prefixMatches) {
968
+ throw new Error('Not a proper lightning payment request')
969
+ }
970
+
971
+ // "ln" section
972
+ sections.push({
973
+ name: 'lightning_network',
974
+ letters: 'ln'
975
+ });
976
+
977
+ // "bc" section
978
+ const bech32Prefix = prefixMatches[1];
979
+ let coinNetwork;
980
+ if (!network) {
981
+ switch (bech32Prefix) {
982
+ case DEFAULTNETWORK.bech32:
983
+ coinNetwork = DEFAULTNETWORK;
984
+ break
985
+ case TESTNETWORK.bech32:
986
+ coinNetwork = TESTNETWORK;
987
+ break
988
+ case SIGNETNETWORK.bech32:
989
+ coinNetwork = SIGNETNETWORK;
990
+ break
991
+ case REGTESTNETWORK.bech32:
992
+ coinNetwork = REGTESTNETWORK;
993
+ break
994
+ case SIMNETWORK.bech32:
995
+ coinNetwork = SIMNETWORK;
996
+ break
997
+ }
998
+ } else {
999
+ if (
1000
+ network.bech32 === undefined ||
1001
+ network.pubKeyHash === undefined ||
1002
+ network.scriptHash === undefined ||
1003
+ !Array.isArray(network.validWitnessVersions)
1004
+ )
1005
+ throw new Error('Invalid network')
1006
+ coinNetwork = network;
1007
+ }
1008
+ if (!coinNetwork || coinNetwork.bech32 !== bech32Prefix) {
1009
+ throw new Error('Unknown coin bech32 prefix')
1010
+ }
1011
+ sections.push({
1012
+ name: 'coin_network',
1013
+ letters: bech32Prefix,
1014
+ value: coinNetwork
1015
+ });
1016
+
1017
+ // amount section
1018
+ const value = prefixMatches[2];
1019
+ let millisatoshis;
1020
+ if (value) {
1021
+ const divisor = prefixMatches[3];
1022
+ millisatoshis = hrpToMillisat(value + divisor, true);
1023
+ sections.push({
1024
+ name: 'amount',
1025
+ letters: prefixMatches[2] + prefixMatches[3],
1026
+ value: millisatoshis
1027
+ });
1028
+ } else {
1029
+ millisatoshis = null;
1030
+ }
1031
+
1032
+ // "1" separator
1033
+ sections.push({
1034
+ name: 'separator',
1035
+ letters: '1'
1036
+ });
1037
+
1038
+ // timestamp
1039
+ const timestamp = wordsToIntBE(words.slice(0, 7));
1040
+ words = words.slice(7); // trim off the left 7 words
1041
+ sections.push({
1042
+ name: 'timestamp',
1043
+ letters: letters.slice(0, 7),
1044
+ value: timestamp
1045
+ });
1046
+ letters = letters.slice(7);
1047
+
1048
+ let tagName, parser, tagLength, tagWords;
1049
+ // we have no tag count to go on, so just keep hacking off words
1050
+ // until we have none.
1051
+ while (words.length > 0) {
1052
+ const tagCode = words[0].toString();
1053
+ tagName = TAGNAMES[tagCode] || 'unknown_tag';
1054
+ parser = TAGPARSERS[tagCode] || getUnknownParser(tagCode);
1055
+ words = words.slice(1);
1056
+
1057
+ tagLength = wordsToIntBE(words.slice(0, 2));
1058
+ words = words.slice(2);
1059
+
1060
+ tagWords = words.slice(0, tagLength);
1061
+ words = words.slice(tagLength);
1062
+
1063
+ sections.push({
1064
+ name: tagName,
1065
+ tag: letters[0],
1066
+ letters: letters.slice(0, 1 + 2 + tagLength),
1067
+ value: parser(tagWords) // see: parsers for more comments
1068
+ });
1069
+ letters = letters.slice(1 + 2 + tagLength);
1070
+ }
1071
+
1072
+ // signature
1073
+ sections.push({
1074
+ name: 'signature',
1075
+ letters: letters.slice(0, 104),
1076
+ value: hex.encode(bech32.fromWordsUnsafe(sigWords))
1077
+ });
1078
+ letters = letters.slice(104);
1079
+
1080
+ // checksum
1081
+ sections.push({
1082
+ name: 'checksum',
1083
+ letters: letters
1084
+ });
1085
+
1086
+ let result = {
1087
+ paymentRequest,
1088
+ sections,
1089
+
1090
+ get expiry() {
1091
+ let exp = sections.find(s => s.name === 'expiry');
1092
+ if (exp) return getValue('timestamp') + exp.value
1093
+ },
1094
+
1095
+ get route_hints() {
1096
+ return sections.filter(s => s.name === 'route_hint').map(s => s.value)
1097
+ }
1098
+ };
1099
+
1100
+ for (let name in TAGCODES) {
1101
+ if (name === 'route_hint') {
1102
+ // route hints can be multiple, so this won't work for them
1103
+ continue
1104
+ }
1105
+
1106
+ Object.defineProperty(result, name, {
1107
+ get() {
1108
+ return getValue(name)
1109
+ }
1110
+ });
1111
+ }
1112
+
1113
+ return result
1114
+
1115
+ function getValue(name) {
1116
+ let section = sections.find(s => s.name === name);
1117
+ return section ? section.value : undefined
1118
+ }
1119
+ }
1120
+
1121
+ bolt11 = {
1122
+ decode,
1123
+ hrpToMillisat
1124
+ };
1125
+ return bolt11;
1126
+ }
1127
+
1128
+ var bolt11Exports = requireBolt11();
1129
+
1130
+ // from https://stackoverflow.com/a/50868276
1131
+ const fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
1132
+ const decodeInvoice = (paymentRequest) => {
1133
+ if (!paymentRequest)
1134
+ return null;
1135
+ try {
1136
+ const decoded = bolt11Exports.decode(paymentRequest);
1137
+ if (!decoded || !decoded.sections)
1138
+ return null;
1139
+ const hashTag = decoded.sections.find((value) => value.name === "payment_hash");
1140
+ if (hashTag?.name !== "payment_hash" || !hashTag.value)
1141
+ return null;
1142
+ const paymentHash = hashTag.value;
1143
+ let satoshi = 0;
1144
+ let millisatoshi = 0;
1145
+ let amountRaw = "0";
1146
+ const amountTag = decoded.sections.find((value) => value.name === "amount");
1147
+ if (amountTag?.name === "amount" && amountTag.value) {
1148
+ amountRaw = amountTag.value;
1149
+ millisatoshi = parseInt(amountTag.value);
1150
+ satoshi = parseInt(amountTag.value) / 1000; // millisats
1151
+ }
1152
+ const timestampTag = decoded.sections.find((value) => value.name === "timestamp");
1153
+ if (timestampTag?.name !== "timestamp" || !timestampTag.value)
1154
+ return null;
1155
+ const timestamp = timestampTag.value;
1156
+ let expiry;
1157
+ const expiryTag = decoded.sections.find((value) => value.name === "expiry");
1158
+ if (expiryTag?.name === "expiry") {
1159
+ expiry = expiryTag.value;
1160
+ }
1161
+ const descriptionTag = decoded.sections.find((value) => value.name === "description");
1162
+ const description = descriptionTag?.name === "description"
1163
+ ? descriptionTag?.value
1164
+ : undefined;
1165
+ return {
1166
+ paymentHash,
1167
+ satoshi,
1168
+ millisatoshi,
1169
+ amountRaw,
1170
+ timestamp,
1171
+ expiry,
1172
+ description,
1173
+ };
1174
+ }
1175
+ catch {
1176
+ return null;
1177
+ }
1178
+ };
1179
+ function validatePreimage(preimage, paymentHash) {
1180
+ try {
1181
+ if (!/^[0-9a-fA-F]{64}$/.test(preimage))
1182
+ return false;
1183
+ if (!/^[0-9a-fA-F]{64}$/.test(paymentHash))
1184
+ return false;
1185
+ const preimageHash = bytesToHex(sha256(fromHexString(preimage)));
1186
+ return paymentHash === preimageHash;
1187
+ }
1188
+ catch {
1189
+ return false;
1190
+ }
1191
+ }
1192
+
1193
+ class Invoice {
1194
+ constructor(args) {
1195
+ this.paymentRequest = args.pr;
1196
+ if (!this.paymentRequest) {
1197
+ throw new Error("Invalid payment request");
1198
+ }
1199
+ const decodedInvoice = decodeInvoice(this.paymentRequest);
1200
+ if (!decodedInvoice) {
1201
+ throw new Error("Failed to decode payment request");
1202
+ }
1203
+ this.paymentHash = decodedInvoice.paymentHash;
1204
+ this.satoshi = decodedInvoice.satoshi;
1205
+ this.millisatoshi = decodedInvoice.millisatoshi;
1206
+ this.amountRaw = decodedInvoice.amountRaw;
1207
+ this.timestamp = decodedInvoice.timestamp;
1208
+ this.expiry = decodedInvoice.expiry;
1209
+ this.createdDate = new Date(this.timestamp * 1000);
1210
+ this.expiryDate = this.expiry
1211
+ ? new Date((this.timestamp + this.expiry) * 1000)
1212
+ : undefined;
1213
+ this.description = decodedInvoice.description ?? null;
1214
+ this.verify = args.verify ?? null;
1215
+ this.preimage = args.preimage ?? null;
1216
+ this.successAction = args.successAction ?? null;
1217
+ }
1218
+ async isPaid() {
1219
+ if (this.preimage)
1220
+ return this.validatePreimage(this.preimage);
1221
+ else if (this.verify) {
1222
+ return await this.verifyPayment();
1223
+ }
1224
+ else {
1225
+ throw new Error("Could not verify payment");
1226
+ }
1227
+ }
1228
+ validatePreimage(preimage) {
1229
+ if (!preimage || !this.paymentHash)
1230
+ return false;
1231
+ return validatePreimage(preimage, this.paymentHash);
1232
+ }
1233
+ async verifyPayment() {
1234
+ try {
1235
+ if (!this.verify) {
1236
+ throw new Error("LNURL verify not available");
1237
+ }
1238
+ const response = await fetch(this.verify);
1239
+ if (!response.ok) {
1240
+ throw new Error(`Verification request failed: ${response.status} ${response.statusText}`);
1241
+ }
1242
+ const json = await response.json();
1243
+ if (json.preimage) {
1244
+ this.preimage = json.preimage;
1245
+ }
1246
+ return json.settled;
1247
+ }
1248
+ catch (error) {
1249
+ console.error("Failed to check LNURL-verify", error);
1250
+ return false;
1251
+ }
1252
+ }
1253
+ hasExpired() {
1254
+ const { expiryDate } = this;
1255
+ if (expiryDate) {
1256
+ return expiryDate.getTime() < Date.now();
1257
+ }
1258
+ return false;
1259
+ }
1260
+ }
1261
+
1262
+ /** Apply a previously-obtained credential to the outgoing request headers. */
1263
+ const applyCredentials = (headers, credentials) => {
1264
+ headers.set(credentials.header, credentials.value);
1265
+ };
1266
+ /** Attach payment metadata to a response and return it (typed). */
1267
+ const attachPayment = (response, payment) => {
1268
+ if (payment) {
1269
+ response.payment = payment;
1270
+ }
1271
+ return response;
1272
+ };
1273
+ /** Payment metadata describing a request authorized with a reused credential. */
1274
+ const reusedCredentialPayment = (credentials) => credentials ? { paid: false, amount: 0, credentials } : undefined;
1275
+ /** Satoshi amount of a BOLT11 invoice (0 when it cannot be decoded). */
1276
+ const getInvoiceAmount = (invoice) => {
1277
+ try {
1278
+ return new Invoice({ pr: invoice }).satoshi;
1279
+ }
1280
+ catch (_) {
1281
+ return 0;
1282
+ }
1283
+ };
1284
+
3
1285
  /**
4
1286
  * Client: parse "www-authenticate" header from server response
5
1287
  * @param input
@@ -39,6 +1321,9 @@ const handleL402Payment = async (l402Header, url, fetchArgs, headers, wallet) =>
39
1321
  const details = parseL402(l402Header);
40
1322
  const token = details.token || details.macaroon;
41
1323
  const invoice = details.invoice;
1324
+ // Preserve the scheme the server challenged with (L402 or LSAT) so the
1325
+ // retry's Authorization header matches what the server expects.
1326
+ const scheme = /^\s*LSAT\b/i.test(l402Header) ? "LSAT" : "L402";
42
1327
  if (!token) {
43
1328
  throw new Error("L402: missing token/macaroon in WWW-Authenticate header");
44
1329
  }
@@ -46,8 +1331,16 @@ const handleL402Payment = async (l402Header, url, fetchArgs, headers, wallet) =>
46
1331
  throw new Error("L402: missing invoice in WWW-Authenticate header");
47
1332
  }
48
1333
  const invResp = await wallet.payInvoice({ invoice });
49
- headers.set("Authorization", `L402 ${token}:${invResp.preimage}`);
50
- return fetch(url, fetchArgs);
1334
+ const value = `${scheme} ${token}:${invResp.preimage}`;
1335
+ headers.set("Authorization", value);
1336
+ const response = await fetch(url, fetchArgs);
1337
+ return attachPayment(response, {
1338
+ paid: true,
1339
+ amount: getInvoiceAmount(invoice),
1340
+ feesPaid: invResp.fees_paid,
1341
+ preimage: invResp.preimage,
1342
+ credentials: { header: "Authorization", value },
1343
+ });
51
1344
  };
52
1345
  const fetchWithL402 = async (url, fetchArgs, options) => {
53
1346
  const wallet = options.wallet;
@@ -61,6 +1354,15 @@ const fetchWithL402 = async (url, fetchArgs, options) => {
61
1354
  fetchArgs.mode = "cors";
62
1355
  const headers = new Headers(fetchArgs.headers ?? undefined);
63
1356
  fetchArgs.headers = headers;
1357
+ // If the caller supplied a credential, we MUST use it and never pay again —
1358
+ // even if the server still responds with a 402. Re-paying here is the exact
1359
+ // double-charge this API exists to prevent; the caller decides what to do
1360
+ // with a rejected credential (retry after settlement, top up, etc.).
1361
+ if (options.credentials) {
1362
+ applyCredentials(headers, options.credentials);
1363
+ const reusedResp = await fetch(url, fetchArgs);
1364
+ return attachPayment(reusedResp, reusedCredentialPayment(options.credentials));
1365
+ }
64
1366
  const initResp = await fetch(url, fetchArgs);
65
1367
  const header = initResp.headers.get("www-authenticate");
66
1368
  if (!header) {