@getalby/lightning-tools 8.0.0 → 8.1.1
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/dist/cjs/402/l402.cjs +102 -0
- package/dist/cjs/402/l402.cjs.map +1 -1
- package/dist/cjs/402/x402.cjs +539 -509
- package/dist/cjs/402/x402.cjs.map +1 -1
- package/dist/cjs/402.cjs +157 -16
- package/dist/cjs/402.cjs.map +1 -1
- package/dist/cjs/bolt11.cjs +526 -518
- package/dist/cjs/bolt11.cjs.map +1 -1
- package/dist/cjs/index.cjs +624 -482
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/lnurl.cjs +14 -7
- package/dist/cjs/lnurl.cjs.map +1 -1
- package/dist/esm/402/l402.js +98 -1
- package/dist/esm/402/l402.js.map +1 -1
- package/dist/esm/402/x402.js +539 -510
- package/dist/esm/402/x402.js.map +1 -1
- package/dist/esm/402.js +152 -17
- package/dist/esm/402.js.map +1 -1
- package/dist/esm/bolt11.js +526 -519
- package/dist/esm/bolt11.js.map +1 -1
- package/dist/esm/index.js +618 -483
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lnurl.js +14 -7
- package/dist/esm/lnurl.js.map +1 -1
- package/dist/lightning-tools.umd.js +3 -3
- package/dist/lightning-tools.umd.js.map +1 -1
- package/dist/types/402/l402.d.ts +39 -1
- package/dist/types/402/x402.d.ts +20 -1
- package/dist/types/402.d.ts +58 -2
- package/dist/types/bolt11.d.ts +2 -1
- package/dist/types/index.d.ts +59 -2
- package/package.json +1 -1
package/dist/esm/402/x402.js
CHANGED
|
@@ -10,6 +10,313 @@ const buildX402PaymentSignature = (scheme, network, invoice, requirements) => {
|
|
|
10
10
|
return btoa(unescape(encodeURIComponent(json)));
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
function bytes(b, ...lengths) {
|
|
14
|
+
if (!(b instanceof Uint8Array))
|
|
15
|
+
throw new Error('Expected Uint8Array');
|
|
16
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
17
|
+
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
|
|
18
|
+
}
|
|
19
|
+
function exists(instance, checkFinished = true) {
|
|
20
|
+
if (instance.destroyed)
|
|
21
|
+
throw new Error('Hash instance has been destroyed');
|
|
22
|
+
if (checkFinished && instance.finished)
|
|
23
|
+
throw new Error('Hash#digest() has already been called');
|
|
24
|
+
}
|
|
25
|
+
function output(out, instance) {
|
|
26
|
+
bytes(out);
|
|
27
|
+
const min = instance.outputLen;
|
|
28
|
+
if (out.length < min) {
|
|
29
|
+
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
34
|
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
35
|
+
// node.js versions earlier than v19 don't declare it in global scope.
|
|
36
|
+
// For node.js, package.json#exports field mapping rewrites import
|
|
37
|
+
// from `crypto` to `cryptoNode`, which imports native module.
|
|
38
|
+
// Makes the utils un-importable in browsers without a bundler.
|
|
39
|
+
// Once node.js 18 is deprecated, we can just drop the import.
|
|
40
|
+
const u8a = (a) => a instanceof Uint8Array;
|
|
41
|
+
// Cast array to view
|
|
42
|
+
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
43
|
+
// The rotate right (circular right shift) operation for uint32
|
|
44
|
+
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
|
45
|
+
// big-endian hardware is rare. Just in case someone still decides to run hashes:
|
|
46
|
+
// early-throw an error because we don't support BE yet.
|
|
47
|
+
const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
|
48
|
+
if (!isLE)
|
|
49
|
+
throw new Error('Non little-endian hardware is not supported');
|
|
50
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
51
|
+
/**
|
|
52
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
53
|
+
*/
|
|
54
|
+
function bytesToHex(bytes) {
|
|
55
|
+
if (!u8a(bytes))
|
|
56
|
+
throw new Error('Uint8Array expected');
|
|
57
|
+
// pre-caching improves the speed 6x
|
|
58
|
+
let hex = '';
|
|
59
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
60
|
+
hex += hexes[bytes[i]];
|
|
61
|
+
}
|
|
62
|
+
return hex;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
66
|
+
*/
|
|
67
|
+
function utf8ToBytes(str) {
|
|
68
|
+
if (typeof str !== 'string')
|
|
69
|
+
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
70
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
74
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
75
|
+
* Keep in mind for future mutable operations.
|
|
76
|
+
*/
|
|
77
|
+
function toBytes(data) {
|
|
78
|
+
if (typeof data === 'string')
|
|
79
|
+
data = utf8ToBytes(data);
|
|
80
|
+
if (!u8a(data))
|
|
81
|
+
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
82
|
+
return data;
|
|
83
|
+
}
|
|
84
|
+
// For runtime check if class implements interface
|
|
85
|
+
class Hash {
|
|
86
|
+
// Safe version that clones internal state
|
|
87
|
+
clone() {
|
|
88
|
+
return this._cloneInto();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function wrapConstructor(hashCons) {
|
|
92
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
93
|
+
const tmp = hashCons();
|
|
94
|
+
hashC.outputLen = tmp.outputLen;
|
|
95
|
+
hashC.blockLen = tmp.blockLen;
|
|
96
|
+
hashC.create = () => hashCons();
|
|
97
|
+
return hashC;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Polyfill for Safari 14
|
|
101
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
102
|
+
if (typeof view.setBigUint64 === 'function')
|
|
103
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
104
|
+
const _32n = BigInt(32);
|
|
105
|
+
const _u32_max = BigInt(0xffffffff);
|
|
106
|
+
const wh = Number((value >> _32n) & _u32_max);
|
|
107
|
+
const wl = Number(value & _u32_max);
|
|
108
|
+
const h = isLE ? 4 : 0;
|
|
109
|
+
const l = isLE ? 0 : 4;
|
|
110
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
111
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
112
|
+
}
|
|
113
|
+
// Base SHA2 class (RFC 6234)
|
|
114
|
+
class SHA2 extends Hash {
|
|
115
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
116
|
+
super();
|
|
117
|
+
this.blockLen = blockLen;
|
|
118
|
+
this.outputLen = outputLen;
|
|
119
|
+
this.padOffset = padOffset;
|
|
120
|
+
this.isLE = isLE;
|
|
121
|
+
this.finished = false;
|
|
122
|
+
this.length = 0;
|
|
123
|
+
this.pos = 0;
|
|
124
|
+
this.destroyed = false;
|
|
125
|
+
this.buffer = new Uint8Array(blockLen);
|
|
126
|
+
this.view = createView(this.buffer);
|
|
127
|
+
}
|
|
128
|
+
update(data) {
|
|
129
|
+
exists(this);
|
|
130
|
+
const { view, buffer, blockLen } = this;
|
|
131
|
+
data = toBytes(data);
|
|
132
|
+
const len = data.length;
|
|
133
|
+
for (let pos = 0; pos < len;) {
|
|
134
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
135
|
+
// Fast path: we have at least one block in input, cast it to view and process
|
|
136
|
+
if (take === blockLen) {
|
|
137
|
+
const dataView = createView(data);
|
|
138
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
139
|
+
this.process(dataView, pos);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
143
|
+
this.pos += take;
|
|
144
|
+
pos += take;
|
|
145
|
+
if (this.pos === blockLen) {
|
|
146
|
+
this.process(view, 0);
|
|
147
|
+
this.pos = 0;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
this.length += data.length;
|
|
151
|
+
this.roundClean();
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
digestInto(out) {
|
|
155
|
+
exists(this);
|
|
156
|
+
output(out, this);
|
|
157
|
+
this.finished = true;
|
|
158
|
+
// Padding
|
|
159
|
+
// We can avoid allocation of buffer for padding completely if it
|
|
160
|
+
// was previously not allocated here. But it won't change performance.
|
|
161
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
162
|
+
let { pos } = this;
|
|
163
|
+
// append the bit '1' to the message
|
|
164
|
+
buffer[pos++] = 0b10000000;
|
|
165
|
+
this.buffer.subarray(pos).fill(0);
|
|
166
|
+
// we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
|
|
167
|
+
if (this.padOffset > blockLen - pos) {
|
|
168
|
+
this.process(view, 0);
|
|
169
|
+
pos = 0;
|
|
170
|
+
}
|
|
171
|
+
// Pad until full block byte with zeros
|
|
172
|
+
for (let i = pos; i < blockLen; i++)
|
|
173
|
+
buffer[i] = 0;
|
|
174
|
+
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
|
175
|
+
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
|
176
|
+
// So we just write lowest 64 bits of that value.
|
|
177
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
178
|
+
this.process(view, 0);
|
|
179
|
+
const oview = createView(out);
|
|
180
|
+
const len = this.outputLen;
|
|
181
|
+
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
|
182
|
+
if (len % 4)
|
|
183
|
+
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
184
|
+
const outLen = len / 4;
|
|
185
|
+
const state = this.get();
|
|
186
|
+
if (outLen > state.length)
|
|
187
|
+
throw new Error('_sha2: outputLen bigger than state');
|
|
188
|
+
for (let i = 0; i < outLen; i++)
|
|
189
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
190
|
+
}
|
|
191
|
+
digest() {
|
|
192
|
+
const { buffer, outputLen } = this;
|
|
193
|
+
this.digestInto(buffer);
|
|
194
|
+
const res = buffer.slice(0, outputLen);
|
|
195
|
+
this.destroy();
|
|
196
|
+
return res;
|
|
197
|
+
}
|
|
198
|
+
_cloneInto(to) {
|
|
199
|
+
to || (to = new this.constructor());
|
|
200
|
+
to.set(...this.get());
|
|
201
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
202
|
+
to.length = length;
|
|
203
|
+
to.pos = pos;
|
|
204
|
+
to.finished = finished;
|
|
205
|
+
to.destroyed = destroyed;
|
|
206
|
+
if (length % blockLen)
|
|
207
|
+
to.buffer.set(buffer);
|
|
208
|
+
return to;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// SHA2-256 need to try 2^128 hashes to execute birthday attack.
|
|
213
|
+
// BTC network is doing 2^67 hashes/sec as per early 2023.
|
|
214
|
+
// Choice: a ? b : c
|
|
215
|
+
const Chi = (a, b, c) => (a & b) ^ (~a & c);
|
|
216
|
+
// Majority function, true if any two inpust is true
|
|
217
|
+
const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
|
|
218
|
+
// Round constants:
|
|
219
|
+
// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
|
|
220
|
+
// prettier-ignore
|
|
221
|
+
const SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
222
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
223
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
224
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
225
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
226
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
227
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
228
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
229
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
230
|
+
]);
|
|
231
|
+
// Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
|
|
232
|
+
// prettier-ignore
|
|
233
|
+
const IV = /* @__PURE__ */ new Uint32Array([
|
|
234
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
235
|
+
]);
|
|
236
|
+
// Temporary buffer, not used to store anything between runs
|
|
237
|
+
// Named this way because it matches specification.
|
|
238
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
239
|
+
class SHA256 extends SHA2 {
|
|
240
|
+
constructor() {
|
|
241
|
+
super(64, 32, 8, false);
|
|
242
|
+
// We cannot use array here since array allows indexing by variable
|
|
243
|
+
// which means optimizer/compiler cannot use registers.
|
|
244
|
+
this.A = IV[0] | 0;
|
|
245
|
+
this.B = IV[1] | 0;
|
|
246
|
+
this.C = IV[2] | 0;
|
|
247
|
+
this.D = IV[3] | 0;
|
|
248
|
+
this.E = IV[4] | 0;
|
|
249
|
+
this.F = IV[5] | 0;
|
|
250
|
+
this.G = IV[6] | 0;
|
|
251
|
+
this.H = IV[7] | 0;
|
|
252
|
+
}
|
|
253
|
+
get() {
|
|
254
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
255
|
+
return [A, B, C, D, E, F, G, H];
|
|
256
|
+
}
|
|
257
|
+
// prettier-ignore
|
|
258
|
+
set(A, B, C, D, E, F, G, H) {
|
|
259
|
+
this.A = A | 0;
|
|
260
|
+
this.B = B | 0;
|
|
261
|
+
this.C = C | 0;
|
|
262
|
+
this.D = D | 0;
|
|
263
|
+
this.E = E | 0;
|
|
264
|
+
this.F = F | 0;
|
|
265
|
+
this.G = G | 0;
|
|
266
|
+
this.H = H | 0;
|
|
267
|
+
}
|
|
268
|
+
process(view, offset) {
|
|
269
|
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
270
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
271
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
272
|
+
for (let i = 16; i < 64; i++) {
|
|
273
|
+
const W15 = SHA256_W[i - 15];
|
|
274
|
+
const W2 = SHA256_W[i - 2];
|
|
275
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
276
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
277
|
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
278
|
+
}
|
|
279
|
+
// Compression function main loop, 64 rounds
|
|
280
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
281
|
+
for (let i = 0; i < 64; i++) {
|
|
282
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
283
|
+
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
284
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
285
|
+
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
|
286
|
+
H = G;
|
|
287
|
+
G = F;
|
|
288
|
+
F = E;
|
|
289
|
+
E = (D + T1) | 0;
|
|
290
|
+
D = C;
|
|
291
|
+
C = B;
|
|
292
|
+
B = A;
|
|
293
|
+
A = (T1 + T2) | 0;
|
|
294
|
+
}
|
|
295
|
+
// Add the compressed chunk to the current hash value
|
|
296
|
+
A = (A + this.A) | 0;
|
|
297
|
+
B = (B + this.B) | 0;
|
|
298
|
+
C = (C + this.C) | 0;
|
|
299
|
+
D = (D + this.D) | 0;
|
|
300
|
+
E = (E + this.E) | 0;
|
|
301
|
+
F = (F + this.F) | 0;
|
|
302
|
+
G = (G + this.G) | 0;
|
|
303
|
+
H = (H + this.H) | 0;
|
|
304
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
305
|
+
}
|
|
306
|
+
roundClean() {
|
|
307
|
+
SHA256_W.fill(0);
|
|
308
|
+
}
|
|
309
|
+
destroy() {
|
|
310
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
311
|
+
this.buffer.fill(0);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* SHA2-256 hash function
|
|
316
|
+
* @param message - data that would be hashed
|
|
317
|
+
*/
|
|
318
|
+
const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
319
|
+
|
|
13
320
|
var lib = {};
|
|
14
321
|
|
|
15
322
|
var hasRequiredLib;
|
|
@@ -677,515 +984,221 @@ function requireBolt11 () {
|
|
|
677
984
|
letters: 'ln'
|
|
678
985
|
});
|
|
679
986
|
|
|
680
|
-
// "bc" section
|
|
681
|
-
const bech32Prefix = prefixMatches[1];
|
|
682
|
-
let coinNetwork;
|
|
683
|
-
if (!network) {
|
|
684
|
-
switch (bech32Prefix) {
|
|
685
|
-
case DEFAULTNETWORK.bech32:
|
|
686
|
-
coinNetwork = DEFAULTNETWORK;
|
|
687
|
-
break
|
|
688
|
-
case TESTNETWORK.bech32:
|
|
689
|
-
coinNetwork = TESTNETWORK;
|
|
690
|
-
break
|
|
691
|
-
case SIGNETNETWORK.bech32:
|
|
692
|
-
coinNetwork = SIGNETNETWORK;
|
|
693
|
-
break
|
|
694
|
-
case REGTESTNETWORK.bech32:
|
|
695
|
-
coinNetwork = REGTESTNETWORK;
|
|
696
|
-
break
|
|
697
|
-
case SIMNETWORK.bech32:
|
|
698
|
-
coinNetwork = SIMNETWORK;
|
|
699
|
-
break
|
|
700
|
-
}
|
|
701
|
-
} else {
|
|
702
|
-
if (
|
|
703
|
-
network.bech32 === undefined ||
|
|
704
|
-
network.pubKeyHash === undefined ||
|
|
705
|
-
network.scriptHash === undefined ||
|
|
706
|
-
!Array.isArray(network.validWitnessVersions)
|
|
707
|
-
)
|
|
708
|
-
throw new Error('Invalid network')
|
|
709
|
-
coinNetwork = network;
|
|
710
|
-
}
|
|
711
|
-
if (!coinNetwork || coinNetwork.bech32 !== bech32Prefix) {
|
|
712
|
-
throw new Error('Unknown coin bech32 prefix')
|
|
713
|
-
}
|
|
714
|
-
sections.push({
|
|
715
|
-
name: 'coin_network',
|
|
716
|
-
letters: bech32Prefix,
|
|
717
|
-
value: coinNetwork
|
|
718
|
-
});
|
|
719
|
-
|
|
720
|
-
// amount section
|
|
721
|
-
const value = prefixMatches[2];
|
|
722
|
-
let millisatoshis;
|
|
723
|
-
if (value) {
|
|
724
|
-
const divisor = prefixMatches[3];
|
|
725
|
-
millisatoshis = hrpToMillisat(value + divisor, true);
|
|
726
|
-
sections.push({
|
|
727
|
-
name: 'amount',
|
|
728
|
-
letters: prefixMatches[2] + prefixMatches[3],
|
|
729
|
-
value: millisatoshis
|
|
730
|
-
});
|
|
731
|
-
} else {
|
|
732
|
-
millisatoshis = null;
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
// "1" separator
|
|
736
|
-
sections.push({
|
|
737
|
-
name: 'separator',
|
|
738
|
-
letters: '1'
|
|
739
|
-
});
|
|
740
|
-
|
|
741
|
-
// timestamp
|
|
742
|
-
const timestamp = wordsToIntBE(words.slice(0, 7));
|
|
743
|
-
words = words.slice(7); // trim off the left 7 words
|
|
744
|
-
sections.push({
|
|
745
|
-
name: 'timestamp',
|
|
746
|
-
letters: letters.slice(0, 7),
|
|
747
|
-
value: timestamp
|
|
748
|
-
});
|
|
749
|
-
letters = letters.slice(7);
|
|
750
|
-
|
|
751
|
-
let tagName, parser, tagLength, tagWords;
|
|
752
|
-
// we have no tag count to go on, so just keep hacking off words
|
|
753
|
-
// until we have none.
|
|
754
|
-
while (words.length > 0) {
|
|
755
|
-
const tagCode = words[0].toString();
|
|
756
|
-
tagName = TAGNAMES[tagCode] || 'unknown_tag';
|
|
757
|
-
parser = TAGPARSERS[tagCode] || getUnknownParser(tagCode);
|
|
758
|
-
words = words.slice(1);
|
|
759
|
-
|
|
760
|
-
tagLength = wordsToIntBE(words.slice(0, 2));
|
|
761
|
-
words = words.slice(2);
|
|
762
|
-
|
|
763
|
-
tagWords = words.slice(0, tagLength);
|
|
764
|
-
words = words.slice(tagLength);
|
|
765
|
-
|
|
766
|
-
sections.push({
|
|
767
|
-
name: tagName,
|
|
768
|
-
tag: letters[0],
|
|
769
|
-
letters: letters.slice(0, 1 + 2 + tagLength),
|
|
770
|
-
value: parser(tagWords) // see: parsers for more comments
|
|
771
|
-
});
|
|
772
|
-
letters = letters.slice(1 + 2 + tagLength);
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
// signature
|
|
776
|
-
sections.push({
|
|
777
|
-
name: 'signature',
|
|
778
|
-
letters: letters.slice(0, 104),
|
|
779
|
-
value: hex.encode(bech32.fromWordsUnsafe(sigWords))
|
|
780
|
-
});
|
|
781
|
-
letters = letters.slice(104);
|
|
782
|
-
|
|
783
|
-
// checksum
|
|
784
|
-
sections.push({
|
|
785
|
-
name: 'checksum',
|
|
786
|
-
letters: letters
|
|
787
|
-
});
|
|
788
|
-
|
|
789
|
-
let result = {
|
|
790
|
-
paymentRequest,
|
|
791
|
-
sections,
|
|
792
|
-
|
|
793
|
-
get expiry() {
|
|
794
|
-
let exp = sections.find(s => s.name === 'expiry');
|
|
795
|
-
if (exp) return getValue('timestamp') + exp.value
|
|
796
|
-
},
|
|
797
|
-
|
|
798
|
-
get route_hints() {
|
|
799
|
-
return sections.filter(s => s.name === 'route_hint').map(s => s.value)
|
|
800
|
-
}
|
|
801
|
-
};
|
|
802
|
-
|
|
803
|
-
for (let name in TAGCODES) {
|
|
804
|
-
if (name === 'route_hint') {
|
|
805
|
-
// route hints can be multiple, so this won't work for them
|
|
806
|
-
continue
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
Object.defineProperty(result, name, {
|
|
810
|
-
get() {
|
|
811
|
-
return getValue(name)
|
|
812
|
-
}
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
return result
|
|
817
|
-
|
|
818
|
-
function getValue(name) {
|
|
819
|
-
let section = sections.find(s => s.name === name);
|
|
820
|
-
return section ? section.value : undefined
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
bolt11 = {
|
|
825
|
-
decode,
|
|
826
|
-
hrpToMillisat
|
|
827
|
-
};
|
|
828
|
-
return bolt11;
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
var bolt11Exports = requireBolt11();
|
|
832
|
-
|
|
833
|
-
// from https://stackoverflow.com/a/50868276
|
|
834
|
-
const fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
835
|
-
const decodeInvoice = (paymentRequest) => {
|
|
836
|
-
if (!paymentRequest)
|
|
837
|
-
return null;
|
|
838
|
-
try {
|
|
839
|
-
const decoded = bolt11Exports.decode(paymentRequest);
|
|
840
|
-
if (!decoded || !decoded.sections)
|
|
841
|
-
return null;
|
|
842
|
-
const hashTag = decoded.sections.find((value) => value.name === "payment_hash");
|
|
843
|
-
if (hashTag?.name !== "payment_hash" || !hashTag.value)
|
|
844
|
-
return null;
|
|
845
|
-
const paymentHash = hashTag.value;
|
|
846
|
-
let satoshi = 0;
|
|
847
|
-
let millisatoshi = 0;
|
|
848
|
-
let amountRaw = "0";
|
|
849
|
-
const amountTag = decoded.sections.find((value) => value.name === "amount");
|
|
850
|
-
if (amountTag?.name === "amount" && amountTag.value) {
|
|
851
|
-
amountRaw = amountTag.value;
|
|
852
|
-
millisatoshi = parseInt(amountTag.value);
|
|
853
|
-
satoshi = parseInt(amountTag.value) / 1000; // millisats
|
|
854
|
-
}
|
|
855
|
-
const timestampTag = decoded.sections.find((value) => value.name === "timestamp");
|
|
856
|
-
if (timestampTag?.name !== "timestamp" || !timestampTag.value)
|
|
857
|
-
return null;
|
|
858
|
-
const timestamp = timestampTag.value;
|
|
859
|
-
let expiry;
|
|
860
|
-
const expiryTag = decoded.sections.find((value) => value.name === "expiry");
|
|
861
|
-
if (expiryTag?.name === "expiry") {
|
|
862
|
-
expiry = expiryTag.value;
|
|
863
|
-
}
|
|
864
|
-
const descriptionTag = decoded.sections.find((value) => value.name === "description");
|
|
865
|
-
const description = descriptionTag?.name === "description"
|
|
866
|
-
? descriptionTag?.value
|
|
867
|
-
: undefined;
|
|
868
|
-
return {
|
|
869
|
-
paymentHash,
|
|
870
|
-
satoshi,
|
|
871
|
-
millisatoshi,
|
|
872
|
-
amountRaw,
|
|
873
|
-
timestamp,
|
|
874
|
-
expiry,
|
|
875
|
-
description,
|
|
876
|
-
};
|
|
877
|
-
}
|
|
878
|
-
catch {
|
|
879
|
-
return null;
|
|
880
|
-
}
|
|
881
|
-
};
|
|
987
|
+
// "bc" section
|
|
988
|
+
const bech32Prefix = prefixMatches[1];
|
|
989
|
+
let coinNetwork;
|
|
990
|
+
if (!network) {
|
|
991
|
+
switch (bech32Prefix) {
|
|
992
|
+
case DEFAULTNETWORK.bech32:
|
|
993
|
+
coinNetwork = DEFAULTNETWORK;
|
|
994
|
+
break
|
|
995
|
+
case TESTNETWORK.bech32:
|
|
996
|
+
coinNetwork = TESTNETWORK;
|
|
997
|
+
break
|
|
998
|
+
case SIGNETNETWORK.bech32:
|
|
999
|
+
coinNetwork = SIGNETNETWORK;
|
|
1000
|
+
break
|
|
1001
|
+
case REGTESTNETWORK.bech32:
|
|
1002
|
+
coinNetwork = REGTESTNETWORK;
|
|
1003
|
+
break
|
|
1004
|
+
case SIMNETWORK.bech32:
|
|
1005
|
+
coinNetwork = SIMNETWORK;
|
|
1006
|
+
break
|
|
1007
|
+
}
|
|
1008
|
+
} else {
|
|
1009
|
+
if (
|
|
1010
|
+
network.bech32 === undefined ||
|
|
1011
|
+
network.pubKeyHash === undefined ||
|
|
1012
|
+
network.scriptHash === undefined ||
|
|
1013
|
+
!Array.isArray(network.validWitnessVersions)
|
|
1014
|
+
)
|
|
1015
|
+
throw new Error('Invalid network')
|
|
1016
|
+
coinNetwork = network;
|
|
1017
|
+
}
|
|
1018
|
+
if (!coinNetwork || coinNetwork.bech32 !== bech32Prefix) {
|
|
1019
|
+
throw new Error('Unknown coin bech32 prefix')
|
|
1020
|
+
}
|
|
1021
|
+
sections.push({
|
|
1022
|
+
name: 'coin_network',
|
|
1023
|
+
letters: bech32Prefix,
|
|
1024
|
+
value: coinNetwork
|
|
1025
|
+
});
|
|
882
1026
|
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
const min = instance.outputLen;
|
|
898
|
-
if (out.length < min) {
|
|
899
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
900
|
-
}
|
|
901
|
-
}
|
|
1027
|
+
// amount section
|
|
1028
|
+
const value = prefixMatches[2];
|
|
1029
|
+
let millisatoshis;
|
|
1030
|
+
if (value) {
|
|
1031
|
+
const divisor = prefixMatches[3];
|
|
1032
|
+
millisatoshis = hrpToMillisat(value + divisor, true);
|
|
1033
|
+
sections.push({
|
|
1034
|
+
name: 'amount',
|
|
1035
|
+
letters: prefixMatches[2] + prefixMatches[3],
|
|
1036
|
+
value: millisatoshis
|
|
1037
|
+
});
|
|
1038
|
+
} else {
|
|
1039
|
+
millisatoshis = null;
|
|
1040
|
+
}
|
|
902
1041
|
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
// Makes the utils un-importable in browsers without a bundler.
|
|
909
|
-
// Once node.js 18 is deprecated, we can just drop the import.
|
|
910
|
-
const u8a = (a) => a instanceof Uint8Array;
|
|
911
|
-
// Cast array to view
|
|
912
|
-
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
913
|
-
// The rotate right (circular right shift) operation for uint32
|
|
914
|
-
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
|
915
|
-
// big-endian hardware is rare. Just in case someone still decides to run hashes:
|
|
916
|
-
// early-throw an error because we don't support BE yet.
|
|
917
|
-
const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
|
918
|
-
if (!isLE)
|
|
919
|
-
throw new Error('Non little-endian hardware is not supported');
|
|
920
|
-
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
921
|
-
/**
|
|
922
|
-
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
923
|
-
*/
|
|
924
|
-
function bytesToHex(bytes) {
|
|
925
|
-
if (!u8a(bytes))
|
|
926
|
-
throw new Error('Uint8Array expected');
|
|
927
|
-
// pre-caching improves the speed 6x
|
|
928
|
-
let hex = '';
|
|
929
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
930
|
-
hex += hexes[bytes[i]];
|
|
931
|
-
}
|
|
932
|
-
return hex;
|
|
933
|
-
}
|
|
934
|
-
/**
|
|
935
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
936
|
-
*/
|
|
937
|
-
function utf8ToBytes(str) {
|
|
938
|
-
if (typeof str !== 'string')
|
|
939
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
940
|
-
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
941
|
-
}
|
|
942
|
-
/**
|
|
943
|
-
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
944
|
-
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
945
|
-
* Keep in mind for future mutable operations.
|
|
946
|
-
*/
|
|
947
|
-
function toBytes(data) {
|
|
948
|
-
if (typeof data === 'string')
|
|
949
|
-
data = utf8ToBytes(data);
|
|
950
|
-
if (!u8a(data))
|
|
951
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
952
|
-
return data;
|
|
953
|
-
}
|
|
954
|
-
// For runtime check if class implements interface
|
|
955
|
-
class Hash {
|
|
956
|
-
// Safe version that clones internal state
|
|
957
|
-
clone() {
|
|
958
|
-
return this._cloneInto();
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
function wrapConstructor(hashCons) {
|
|
962
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
963
|
-
const tmp = hashCons();
|
|
964
|
-
hashC.outputLen = tmp.outputLen;
|
|
965
|
-
hashC.blockLen = tmp.blockLen;
|
|
966
|
-
hashC.create = () => hashCons();
|
|
967
|
-
return hashC;
|
|
968
|
-
}
|
|
1042
|
+
// "1" separator
|
|
1043
|
+
sections.push({
|
|
1044
|
+
name: 'separator',
|
|
1045
|
+
letters: '1'
|
|
1046
|
+
});
|
|
969
1047
|
|
|
970
|
-
//
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
for (let i = 0; i < outLen; i++)
|
|
1059
|
-
oview.setUint32(4 * i, state[i], isLE);
|
|
1060
|
-
}
|
|
1061
|
-
digest() {
|
|
1062
|
-
const { buffer, outputLen } = this;
|
|
1063
|
-
this.digestInto(buffer);
|
|
1064
|
-
const res = buffer.slice(0, outputLen);
|
|
1065
|
-
this.destroy();
|
|
1066
|
-
return res;
|
|
1067
|
-
}
|
|
1068
|
-
_cloneInto(to) {
|
|
1069
|
-
to || (to = new this.constructor());
|
|
1070
|
-
to.set(...this.get());
|
|
1071
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
1072
|
-
to.length = length;
|
|
1073
|
-
to.pos = pos;
|
|
1074
|
-
to.finished = finished;
|
|
1075
|
-
to.destroyed = destroyed;
|
|
1076
|
-
if (length % blockLen)
|
|
1077
|
-
to.buffer.set(buffer);
|
|
1078
|
-
return to;
|
|
1079
|
-
}
|
|
1048
|
+
// timestamp
|
|
1049
|
+
const timestamp = wordsToIntBE(words.slice(0, 7));
|
|
1050
|
+
words = words.slice(7); // trim off the left 7 words
|
|
1051
|
+
sections.push({
|
|
1052
|
+
name: 'timestamp',
|
|
1053
|
+
letters: letters.slice(0, 7),
|
|
1054
|
+
value: timestamp
|
|
1055
|
+
});
|
|
1056
|
+
letters = letters.slice(7);
|
|
1057
|
+
|
|
1058
|
+
let tagName, parser, tagLength, tagWords;
|
|
1059
|
+
// we have no tag count to go on, so just keep hacking off words
|
|
1060
|
+
// until we have none.
|
|
1061
|
+
while (words.length > 0) {
|
|
1062
|
+
const tagCode = words[0].toString();
|
|
1063
|
+
tagName = TAGNAMES[tagCode] || 'unknown_tag';
|
|
1064
|
+
parser = TAGPARSERS[tagCode] || getUnknownParser(tagCode);
|
|
1065
|
+
words = words.slice(1);
|
|
1066
|
+
|
|
1067
|
+
tagLength = wordsToIntBE(words.slice(0, 2));
|
|
1068
|
+
words = words.slice(2);
|
|
1069
|
+
|
|
1070
|
+
tagWords = words.slice(0, tagLength);
|
|
1071
|
+
words = words.slice(tagLength);
|
|
1072
|
+
|
|
1073
|
+
sections.push({
|
|
1074
|
+
name: tagName,
|
|
1075
|
+
tag: letters[0],
|
|
1076
|
+
letters: letters.slice(0, 1 + 2 + tagLength),
|
|
1077
|
+
value: parser(tagWords) // see: parsers for more comments
|
|
1078
|
+
});
|
|
1079
|
+
letters = letters.slice(1 + 2 + tagLength);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
// signature
|
|
1083
|
+
sections.push({
|
|
1084
|
+
name: 'signature',
|
|
1085
|
+
letters: letters.slice(0, 104),
|
|
1086
|
+
value: hex.encode(bech32.fromWordsUnsafe(sigWords))
|
|
1087
|
+
});
|
|
1088
|
+
letters = letters.slice(104);
|
|
1089
|
+
|
|
1090
|
+
// checksum
|
|
1091
|
+
sections.push({
|
|
1092
|
+
name: 'checksum',
|
|
1093
|
+
letters: letters
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
let result = {
|
|
1097
|
+
paymentRequest,
|
|
1098
|
+
sections,
|
|
1099
|
+
|
|
1100
|
+
get expiry() {
|
|
1101
|
+
let exp = sections.find(s => s.name === 'expiry');
|
|
1102
|
+
if (exp) return getValue('timestamp') + exp.value
|
|
1103
|
+
},
|
|
1104
|
+
|
|
1105
|
+
get route_hints() {
|
|
1106
|
+
return sections.filter(s => s.name === 'route_hint').map(s => s.value)
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1110
|
+
for (let name in TAGCODES) {
|
|
1111
|
+
if (name === 'route_hint') {
|
|
1112
|
+
// route hints can be multiple, so this won't work for them
|
|
1113
|
+
continue
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
Object.defineProperty(result, name, {
|
|
1117
|
+
get() {
|
|
1118
|
+
return getValue(name)
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
return result
|
|
1124
|
+
|
|
1125
|
+
function getValue(name) {
|
|
1126
|
+
let section = sections.find(s => s.name === name);
|
|
1127
|
+
return section ? section.value : undefined
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
bolt11 = {
|
|
1132
|
+
decode,
|
|
1133
|
+
hrpToMillisat
|
|
1134
|
+
};
|
|
1135
|
+
return bolt11;
|
|
1080
1136
|
}
|
|
1081
1137
|
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
//
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
]);
|
|
1106
|
-
// Temporary buffer, not used to store anything between runs
|
|
1107
|
-
// Named this way because it matches specification.
|
|
1108
|
-
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1109
|
-
class SHA256 extends SHA2 {
|
|
1110
|
-
constructor() {
|
|
1111
|
-
super(64, 32, 8, false);
|
|
1112
|
-
// We cannot use array here since array allows indexing by variable
|
|
1113
|
-
// which means optimizer/compiler cannot use registers.
|
|
1114
|
-
this.A = IV[0] | 0;
|
|
1115
|
-
this.B = IV[1] | 0;
|
|
1116
|
-
this.C = IV[2] | 0;
|
|
1117
|
-
this.D = IV[3] | 0;
|
|
1118
|
-
this.E = IV[4] | 0;
|
|
1119
|
-
this.F = IV[5] | 0;
|
|
1120
|
-
this.G = IV[6] | 0;
|
|
1121
|
-
this.H = IV[7] | 0;
|
|
1122
|
-
}
|
|
1123
|
-
get() {
|
|
1124
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
1125
|
-
return [A, B, C, D, E, F, G, H];
|
|
1126
|
-
}
|
|
1127
|
-
// prettier-ignore
|
|
1128
|
-
set(A, B, C, D, E, F, G, H) {
|
|
1129
|
-
this.A = A | 0;
|
|
1130
|
-
this.B = B | 0;
|
|
1131
|
-
this.C = C | 0;
|
|
1132
|
-
this.D = D | 0;
|
|
1133
|
-
this.E = E | 0;
|
|
1134
|
-
this.F = F | 0;
|
|
1135
|
-
this.G = G | 0;
|
|
1136
|
-
this.H = H | 0;
|
|
1137
|
-
}
|
|
1138
|
-
process(view, offset) {
|
|
1139
|
-
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
1140
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
1141
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
1142
|
-
for (let i = 16; i < 64; i++) {
|
|
1143
|
-
const W15 = SHA256_W[i - 15];
|
|
1144
|
-
const W2 = SHA256_W[i - 2];
|
|
1145
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
1146
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
1147
|
-
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
1138
|
+
var bolt11Exports = requireBolt11();
|
|
1139
|
+
|
|
1140
|
+
// from https://stackoverflow.com/a/50868276
|
|
1141
|
+
const fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
1142
|
+
const decodeInvoice = (paymentRequest) => {
|
|
1143
|
+
if (!paymentRequest)
|
|
1144
|
+
return null;
|
|
1145
|
+
try {
|
|
1146
|
+
const decoded = bolt11Exports.decode(paymentRequest);
|
|
1147
|
+
if (!decoded || !decoded.sections)
|
|
1148
|
+
return null;
|
|
1149
|
+
const hashTag = decoded.sections.find((value) => value.name === "payment_hash");
|
|
1150
|
+
if (hashTag?.name !== "payment_hash" || !hashTag.value)
|
|
1151
|
+
return null;
|
|
1152
|
+
const paymentHash = hashTag.value;
|
|
1153
|
+
let satoshi = 0;
|
|
1154
|
+
let millisatoshi = 0;
|
|
1155
|
+
let amountRaw = "0";
|
|
1156
|
+
const amountTag = decoded.sections.find((value) => value.name === "amount");
|
|
1157
|
+
if (amountTag?.name === "amount" && amountTag.value) {
|
|
1158
|
+
amountRaw = amountTag.value;
|
|
1159
|
+
millisatoshi = parseInt(amountTag.value);
|
|
1160
|
+
satoshi = parseInt(amountTag.value) / 1000; // millisats
|
|
1148
1161
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
G = F;
|
|
1158
|
-
F = E;
|
|
1159
|
-
E = (D + T1) | 0;
|
|
1160
|
-
D = C;
|
|
1161
|
-
C = B;
|
|
1162
|
-
B = A;
|
|
1163
|
-
A = (T1 + T2) | 0;
|
|
1162
|
+
const timestampTag = decoded.sections.find((value) => value.name === "timestamp");
|
|
1163
|
+
if (timestampTag?.name !== "timestamp" || !timestampTag.value)
|
|
1164
|
+
return null;
|
|
1165
|
+
const timestamp = timestampTag.value;
|
|
1166
|
+
let expiry;
|
|
1167
|
+
const expiryTag = decoded.sections.find((value) => value.name === "expiry");
|
|
1168
|
+
if (expiryTag?.name === "expiry") {
|
|
1169
|
+
expiry = expiryTag.value;
|
|
1164
1170
|
}
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1171
|
+
const descriptionTag = decoded.sections.find((value) => value.name === "description");
|
|
1172
|
+
const description = descriptionTag?.name === "description"
|
|
1173
|
+
? descriptionTag?.value
|
|
1174
|
+
: undefined;
|
|
1175
|
+
return {
|
|
1176
|
+
paymentHash,
|
|
1177
|
+
satoshi,
|
|
1178
|
+
millisatoshi,
|
|
1179
|
+
amountRaw,
|
|
1180
|
+
timestamp,
|
|
1181
|
+
expiry,
|
|
1182
|
+
description,
|
|
1183
|
+
};
|
|
1175
1184
|
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1185
|
+
catch {
|
|
1186
|
+
return null;
|
|
1178
1187
|
}
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1188
|
+
};
|
|
1189
|
+
function validatePreimage(preimage, paymentHash) {
|
|
1190
|
+
try {
|
|
1191
|
+
if (!/^[0-9a-fA-F]{64}$/.test(preimage))
|
|
1192
|
+
return false;
|
|
1193
|
+
if (!/^[0-9a-fA-F]{64}$/.test(paymentHash))
|
|
1194
|
+
return false;
|
|
1195
|
+
const preimageHash = bytesToHex(sha256(fromHexString(preimage)));
|
|
1196
|
+
return paymentHash === preimageHash;
|
|
1197
|
+
}
|
|
1198
|
+
catch {
|
|
1199
|
+
return false;
|
|
1182
1200
|
}
|
|
1183
1201
|
}
|
|
1184
|
-
/**
|
|
1185
|
-
* SHA2-256 hash function
|
|
1186
|
-
* @param message - data that would be hashed
|
|
1187
|
-
*/
|
|
1188
|
-
const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
1189
1202
|
|
|
1190
1203
|
class Invoice {
|
|
1191
1204
|
constructor(args) {
|
|
@@ -1225,13 +1238,7 @@ class Invoice {
|
|
|
1225
1238
|
validatePreimage(preimage) {
|
|
1226
1239
|
if (!preimage || !this.paymentHash)
|
|
1227
1240
|
return false;
|
|
1228
|
-
|
|
1229
|
-
const preimageHash = bytesToHex(sha256(fromHexString(preimage)));
|
|
1230
|
-
return this.paymentHash === preimageHash;
|
|
1231
|
-
}
|
|
1232
|
-
catch {
|
|
1233
|
-
return false;
|
|
1234
|
-
}
|
|
1241
|
+
return validatePreimage(preimage, this.paymentHash);
|
|
1235
1242
|
}
|
|
1236
1243
|
async verifyPayment() {
|
|
1237
1244
|
try {
|
|
@@ -1262,7 +1269,7 @@ class Invoice {
|
|
|
1262
1269
|
}
|
|
1263
1270
|
}
|
|
1264
1271
|
|
|
1265
|
-
const
|
|
1272
|
+
const decodeX402Header = (x402Header) => {
|
|
1266
1273
|
let parsed;
|
|
1267
1274
|
try {
|
|
1268
1275
|
parsed = JSON.parse(decodeURIComponent(escape(atob(x402Header))));
|
|
@@ -1273,9 +1280,31 @@ const handleX402Payment = async (x402Header, url, fetchArgs, headers, wallet) =>
|
|
|
1273
1280
|
if (!Array.isArray(parsed.accepts) || parsed.accepts.length === 0) {
|
|
1274
1281
|
throw new Error("x402: PAYMENT-REQUIRED header contains no payment options");
|
|
1275
1282
|
}
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1283
|
+
return { accepts: parsed.accepts };
|
|
1284
|
+
};
|
|
1285
|
+
/**
|
|
1286
|
+
* Probe a PAYMENT-REQUIRED header for a lightning-payable offer without
|
|
1287
|
+
* throwing. Returns the matching requirements, or null if the header has no
|
|
1288
|
+
* lightning entry (e.g. USDC-only endpoints) or is malformed. Used by the
|
|
1289
|
+
* top-level fetch402 dispatcher to decide whether to attempt payment or hand
|
|
1290
|
+
* the 402 back to the caller.
|
|
1291
|
+
*/
|
|
1292
|
+
const findX402LightningRequirements = (x402Header) => {
|
|
1293
|
+
let accepts;
|
|
1294
|
+
try {
|
|
1295
|
+
({ accepts } = decodeX402Header(x402Header));
|
|
1296
|
+
}
|
|
1297
|
+
catch (_) {
|
|
1298
|
+
return null;
|
|
1299
|
+
}
|
|
1300
|
+
const requirements = accepts.find((e) => e?.extra?.paymentMethod === "lightning");
|
|
1301
|
+
if (!requirements?.extra?.invoice)
|
|
1302
|
+
return null;
|
|
1303
|
+
return requirements;
|
|
1304
|
+
};
|
|
1305
|
+
const handleX402Payment = async (x402Header, url, fetchArgs, headers, wallet) => {
|
|
1306
|
+
const { accepts } = decodeX402Header(x402Header);
|
|
1307
|
+
const requirements = accepts.find((e) => e?.extra?.paymentMethod === "lightning");
|
|
1279
1308
|
if (!requirements) {
|
|
1280
1309
|
throw new Error("x402: unsupported x402 network, only Bitcoin lightning network is supported.");
|
|
1281
1310
|
}
|
|
@@ -1307,5 +1336,5 @@ const fetchWithX402 = async (url, fetchArgs, options) => {
|
|
|
1307
1336
|
return handleX402Payment(header, url, fetchArgs, headers, wallet);
|
|
1308
1337
|
};
|
|
1309
1338
|
|
|
1310
|
-
export { fetchWithX402 };
|
|
1339
|
+
export { fetchWithX402, findX402LightningRequirements };
|
|
1311
1340
|
//# sourceMappingURL=x402.js.map
|