@getalby/lightning-tools 7.0.2 → 8.1.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 +108 -28
- package/dist/cjs/402/l402.cjs +157 -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 +1320 -0
- package/dist/cjs/402/x402.cjs.map +1 -0
- package/dist/cjs/402.cjs +1694 -0
- package/dist/cjs/402.cjs.map +1 -0
- package/dist/cjs/bolt11.cjs +534 -518
- package/dist/cjs/bolt11.cjs.map +1 -1
- package/dist/cjs/index.cjs +811 -453
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/lnurl.cjs +22 -7
- package/dist/cjs/lnurl.cjs.map +1 -1
- package/dist/esm/402/l402.js +150 -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 +1318 -0
- package/dist/esm/402/x402.js.map +1 -0
- package/dist/esm/402.js +1683 -0
- package/dist/esm/402.js.map +1 -0
- package/dist/esm/bolt11.js +534 -519
- package/dist/esm/bolt11.js.map +1 -1
- package/dist/esm/index.js +803 -451
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lnurl.js +22 -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 +51 -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 +78 -0
- package/dist/types/bolt11.d.ts +6 -1
- package/dist/types/index.d.ts +76 -28
- package/dist/types/lnurl.d.ts +2 -0
- package/package.json +20 -5
- package/dist/cjs/l402.cjs +0 -93
- package/dist/cjs/l402.cjs.map +0 -1
- package/dist/esm/l402.js +0 -87
- package/dist/esm/l402.js.map +0 -1
- package/dist/types/l402.d.ts +0 -35
package/dist/cjs/bolt11.cjs
CHANGED
|
@@ -1,5 +1,312 @@
|
|
|
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
|
+
|
|
3
310
|
var lib = {};
|
|
4
311
|
|
|
5
312
|
var hasRequiredLib;
|
|
@@ -649,527 +956,239 @@ function requireBolt11 () {
|
|
|
649
956
|
let sigWords = words.slice(-104);
|
|
650
957
|
words = words.slice(0, -104);
|
|
651
958
|
|
|
652
|
-
// Without reverse lookups, can't say that the multipier at the end must
|
|
653
|
-
// have a number before it, so instead we parse, and if the second group
|
|
654
|
-
// doesn't have anything, there's a good chance the last letter of the
|
|
655
|
-
// coin type got captured by the third group, so just re-regex without
|
|
656
|
-
// the number.
|
|
657
|
-
let prefixMatches = prefix.match(/^ln(\S+?)(\d*)([a-zA-Z]?)$/);
|
|
658
|
-
if (prefixMatches && !prefixMatches[2])
|
|
659
|
-
prefixMatches = prefix.match(/^ln(\S+)$/);
|
|
660
|
-
if (!prefixMatches) {
|
|
661
|
-
throw new Error('Not a proper lightning payment request')
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
// "ln" section
|
|
665
|
-
sections.push({
|
|
666
|
-
name: 'lightning_network',
|
|
667
|
-
letters: 'ln'
|
|
668
|
-
});
|
|
669
|
-
|
|
670
|
-
// "bc" section
|
|
671
|
-
const bech32Prefix = prefixMatches[1];
|
|
672
|
-
let coinNetwork;
|
|
673
|
-
if (!network) {
|
|
674
|
-
switch (bech32Prefix) {
|
|
675
|
-
case DEFAULTNETWORK.bech32:
|
|
676
|
-
coinNetwork = DEFAULTNETWORK;
|
|
677
|
-
break
|
|
678
|
-
case TESTNETWORK.bech32:
|
|
679
|
-
coinNetwork = TESTNETWORK;
|
|
680
|
-
break
|
|
681
|
-
case SIGNETNETWORK.bech32:
|
|
682
|
-
coinNetwork = SIGNETNETWORK;
|
|
683
|
-
break
|
|
684
|
-
case REGTESTNETWORK.bech32:
|
|
685
|
-
coinNetwork = REGTESTNETWORK;
|
|
686
|
-
break
|
|
687
|
-
case SIMNETWORK.bech32:
|
|
688
|
-
coinNetwork = SIMNETWORK;
|
|
689
|
-
break
|
|
690
|
-
}
|
|
691
|
-
} else {
|
|
692
|
-
if (
|
|
693
|
-
network.bech32 === undefined ||
|
|
694
|
-
network.pubKeyHash === undefined ||
|
|
695
|
-
network.scriptHash === undefined ||
|
|
696
|
-
!Array.isArray(network.validWitnessVersions)
|
|
697
|
-
)
|
|
698
|
-
throw new Error('Invalid network')
|
|
699
|
-
coinNetwork = network;
|
|
700
|
-
}
|
|
701
|
-
if (!coinNetwork || coinNetwork.bech32 !== bech32Prefix) {
|
|
702
|
-
throw new Error('Unknown coin bech32 prefix')
|
|
703
|
-
}
|
|
704
|
-
sections.push({
|
|
705
|
-
name: 'coin_network',
|
|
706
|
-
letters: bech32Prefix,
|
|
707
|
-
value: coinNetwork
|
|
708
|
-
});
|
|
709
|
-
|
|
710
|
-
// amount section
|
|
711
|
-
const value = prefixMatches[2];
|
|
712
|
-
let millisatoshis;
|
|
713
|
-
if (value) {
|
|
714
|
-
const divisor = prefixMatches[3];
|
|
715
|
-
millisatoshis = hrpToMillisat(value + divisor, true);
|
|
716
|
-
sections.push({
|
|
717
|
-
name: 'amount',
|
|
718
|
-
letters: prefixMatches[2] + prefixMatches[3],
|
|
719
|
-
value: millisatoshis
|
|
720
|
-
});
|
|
721
|
-
} else {
|
|
722
|
-
millisatoshis = null;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
// "1" separator
|
|
726
|
-
sections.push({
|
|
727
|
-
name: 'separator',
|
|
728
|
-
letters: '1'
|
|
729
|
-
});
|
|
730
|
-
|
|
731
|
-
// timestamp
|
|
732
|
-
const timestamp = wordsToIntBE(words.slice(0, 7));
|
|
733
|
-
words = words.slice(7); // trim off the left 7 words
|
|
734
|
-
sections.push({
|
|
735
|
-
name: 'timestamp',
|
|
736
|
-
letters: letters.slice(0, 7),
|
|
737
|
-
value: timestamp
|
|
738
|
-
});
|
|
739
|
-
letters = letters.slice(7);
|
|
740
|
-
|
|
741
|
-
let tagName, parser, tagLength, tagWords;
|
|
742
|
-
// we have no tag count to go on, so just keep hacking off words
|
|
743
|
-
// until we have none.
|
|
744
|
-
while (words.length > 0) {
|
|
745
|
-
const tagCode = words[0].toString();
|
|
746
|
-
tagName = TAGNAMES[tagCode] || 'unknown_tag';
|
|
747
|
-
parser = TAGPARSERS[tagCode] || getUnknownParser(tagCode);
|
|
748
|
-
words = words.slice(1);
|
|
749
|
-
|
|
750
|
-
tagLength = wordsToIntBE(words.slice(0, 2));
|
|
751
|
-
words = words.slice(2);
|
|
752
|
-
|
|
753
|
-
tagWords = words.slice(0, tagLength);
|
|
754
|
-
words = words.slice(tagLength);
|
|
755
|
-
|
|
756
|
-
sections.push({
|
|
757
|
-
name: tagName,
|
|
758
|
-
tag: letters[0],
|
|
759
|
-
letters: letters.slice(0, 1 + 2 + tagLength),
|
|
760
|
-
value: parser(tagWords) // see: parsers for more comments
|
|
761
|
-
});
|
|
762
|
-
letters = letters.slice(1 + 2 + tagLength);
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
// signature
|
|
766
|
-
sections.push({
|
|
767
|
-
name: 'signature',
|
|
768
|
-
letters: letters.slice(0, 104),
|
|
769
|
-
value: hex.encode(bech32.fromWordsUnsafe(sigWords))
|
|
770
|
-
});
|
|
771
|
-
letters = letters.slice(104);
|
|
772
|
-
|
|
773
|
-
// checksum
|
|
774
|
-
sections.push({
|
|
775
|
-
name: 'checksum',
|
|
776
|
-
letters: letters
|
|
777
|
-
});
|
|
778
|
-
|
|
779
|
-
let result = {
|
|
780
|
-
paymentRequest,
|
|
781
|
-
sections,
|
|
782
|
-
|
|
783
|
-
get expiry() {
|
|
784
|
-
let exp = sections.find(s => s.name === 'expiry');
|
|
785
|
-
if (exp) return getValue('timestamp') + exp.value
|
|
786
|
-
},
|
|
787
|
-
|
|
788
|
-
get route_hints() {
|
|
789
|
-
return sections.filter(s => s.name === 'route_hint').map(s => s.value)
|
|
790
|
-
}
|
|
791
|
-
};
|
|
792
|
-
|
|
793
|
-
for (let name in TAGCODES) {
|
|
794
|
-
if (name === 'route_hint') {
|
|
795
|
-
// route hints can be multiple, so this won't work for them
|
|
796
|
-
continue
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
Object.defineProperty(result, name, {
|
|
800
|
-
get() {
|
|
801
|
-
return getValue(name)
|
|
802
|
-
}
|
|
803
|
-
});
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
return result
|
|
807
|
-
|
|
808
|
-
function getValue(name) {
|
|
809
|
-
let section = sections.find(s => s.name === name);
|
|
810
|
-
return section ? section.value : undefined
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
bolt11 = {
|
|
815
|
-
decode,
|
|
816
|
-
hrpToMillisat
|
|
817
|
-
};
|
|
818
|
-
return bolt11;
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
var bolt11Exports = requireBolt11();
|
|
822
|
-
|
|
823
|
-
// from https://stackoverflow.com/a/50868276
|
|
824
|
-
const fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
825
|
-
const decodeInvoice = (paymentRequest) => {
|
|
826
|
-
if (!paymentRequest)
|
|
827
|
-
return null;
|
|
828
|
-
try {
|
|
829
|
-
const decoded = bolt11Exports.decode(paymentRequest);
|
|
830
|
-
if (!decoded || !decoded.sections)
|
|
831
|
-
return null;
|
|
832
|
-
const hashTag = decoded.sections.find((value) => value.name === "payment_hash");
|
|
833
|
-
if (hashTag?.name !== "payment_hash" || !hashTag.value)
|
|
834
|
-
return null;
|
|
835
|
-
const paymentHash = hashTag.value;
|
|
836
|
-
let satoshi = 0;
|
|
837
|
-
const amountTag = decoded.sections.find((value) => value.name === "amount");
|
|
838
|
-
if (amountTag?.name === "amount" && amountTag.value) {
|
|
839
|
-
satoshi = parseInt(amountTag.value) / 1000; // millisats
|
|
840
|
-
}
|
|
841
|
-
const timestampTag = decoded.sections.find((value) => value.name === "timestamp");
|
|
842
|
-
if (timestampTag?.name !== "timestamp" || !timestampTag.value)
|
|
843
|
-
return null;
|
|
844
|
-
const timestamp = timestampTag.value;
|
|
845
|
-
let expiry;
|
|
846
|
-
const expiryTag = decoded.sections.find((value) => value.name === "expiry");
|
|
847
|
-
if (expiryTag?.name === "expiry") {
|
|
848
|
-
expiry = expiryTag.value;
|
|
849
|
-
}
|
|
850
|
-
const descriptionTag = decoded.sections.find((value) => value.name === "description");
|
|
851
|
-
const description = descriptionTag?.name === "description"
|
|
852
|
-
? descriptionTag?.value
|
|
853
|
-
: undefined;
|
|
854
|
-
return {
|
|
855
|
-
paymentHash,
|
|
856
|
-
satoshi,
|
|
857
|
-
timestamp,
|
|
858
|
-
expiry,
|
|
859
|
-
description,
|
|
860
|
-
};
|
|
861
|
-
}
|
|
862
|
-
catch {
|
|
863
|
-
return null;
|
|
864
|
-
}
|
|
865
|
-
};
|
|
866
|
-
|
|
867
|
-
function bytes(b, ...lengths) {
|
|
868
|
-
if (!(b instanceof Uint8Array))
|
|
869
|
-
throw new Error('Expected Uint8Array');
|
|
870
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
871
|
-
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
|
|
872
|
-
}
|
|
873
|
-
function exists(instance, checkFinished = true) {
|
|
874
|
-
if (instance.destroyed)
|
|
875
|
-
throw new Error('Hash instance has been destroyed');
|
|
876
|
-
if (checkFinished && instance.finished)
|
|
877
|
-
throw new Error('Hash#digest() has already been called');
|
|
878
|
-
}
|
|
879
|
-
function output(out, instance) {
|
|
880
|
-
bytes(out);
|
|
881
|
-
const min = instance.outputLen;
|
|
882
|
-
if (out.length < min) {
|
|
883
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
884
|
-
}
|
|
885
|
-
}
|
|
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
|
+
}
|
|
886
970
|
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
// Makes the utils un-importable in browsers without a bundler.
|
|
893
|
-
// Once node.js 18 is deprecated, we can just drop the import.
|
|
894
|
-
const u8a = (a) => a instanceof Uint8Array;
|
|
895
|
-
// Cast array to view
|
|
896
|
-
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
897
|
-
// The rotate right (circular right shift) operation for uint32
|
|
898
|
-
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
|
899
|
-
// big-endian hardware is rare. Just in case someone still decides to run hashes:
|
|
900
|
-
// early-throw an error because we don't support BE yet.
|
|
901
|
-
const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
|
902
|
-
if (!isLE)
|
|
903
|
-
throw new Error('Non little-endian hardware is not supported');
|
|
904
|
-
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
905
|
-
/**
|
|
906
|
-
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
907
|
-
*/
|
|
908
|
-
function bytesToHex(bytes) {
|
|
909
|
-
if (!u8a(bytes))
|
|
910
|
-
throw new Error('Uint8Array expected');
|
|
911
|
-
// pre-caching improves the speed 6x
|
|
912
|
-
let hex = '';
|
|
913
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
914
|
-
hex += hexes[bytes[i]];
|
|
915
|
-
}
|
|
916
|
-
return hex;
|
|
917
|
-
}
|
|
918
|
-
/**
|
|
919
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
920
|
-
*/
|
|
921
|
-
function utf8ToBytes(str) {
|
|
922
|
-
if (typeof str !== 'string')
|
|
923
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
924
|
-
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
925
|
-
}
|
|
926
|
-
/**
|
|
927
|
-
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
928
|
-
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
929
|
-
* Keep in mind for future mutable operations.
|
|
930
|
-
*/
|
|
931
|
-
function toBytes(data) {
|
|
932
|
-
if (typeof data === 'string')
|
|
933
|
-
data = utf8ToBytes(data);
|
|
934
|
-
if (!u8a(data))
|
|
935
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
936
|
-
return data;
|
|
937
|
-
}
|
|
938
|
-
// For runtime check if class implements interface
|
|
939
|
-
class Hash {
|
|
940
|
-
// Safe version that clones internal state
|
|
941
|
-
clone() {
|
|
942
|
-
return this._cloneInto();
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
function wrapConstructor(hashCons) {
|
|
946
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
947
|
-
const tmp = hashCons();
|
|
948
|
-
hashC.outputLen = tmp.outputLen;
|
|
949
|
-
hashC.blockLen = tmp.blockLen;
|
|
950
|
-
hashC.create = () => hashCons();
|
|
951
|
-
return hashC;
|
|
952
|
-
}
|
|
971
|
+
// "ln" section
|
|
972
|
+
sections.push({
|
|
973
|
+
name: 'lightning_network',
|
|
974
|
+
letters: 'ln'
|
|
975
|
+
});
|
|
953
976
|
|
|
954
|
-
//
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
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
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
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;
|
|
1064
1126
|
}
|
|
1065
1127
|
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
//
|
|
1069
|
-
const
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
]);
|
|
1090
|
-
// Temporary buffer, not used to store anything between runs
|
|
1091
|
-
// Named this way because it matches specification.
|
|
1092
|
-
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1093
|
-
class SHA256 extends SHA2 {
|
|
1094
|
-
constructor() {
|
|
1095
|
-
super(64, 32, 8, false);
|
|
1096
|
-
// We cannot use array here since array allows indexing by variable
|
|
1097
|
-
// which means optimizer/compiler cannot use registers.
|
|
1098
|
-
this.A = IV[0] | 0;
|
|
1099
|
-
this.B = IV[1] | 0;
|
|
1100
|
-
this.C = IV[2] | 0;
|
|
1101
|
-
this.D = IV[3] | 0;
|
|
1102
|
-
this.E = IV[4] | 0;
|
|
1103
|
-
this.F = IV[5] | 0;
|
|
1104
|
-
this.G = IV[6] | 0;
|
|
1105
|
-
this.H = IV[7] | 0;
|
|
1106
|
-
}
|
|
1107
|
-
get() {
|
|
1108
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
1109
|
-
return [A, B, C, D, E, F, G, H];
|
|
1110
|
-
}
|
|
1111
|
-
// prettier-ignore
|
|
1112
|
-
set(A, B, C, D, E, F, G, H) {
|
|
1113
|
-
this.A = A | 0;
|
|
1114
|
-
this.B = B | 0;
|
|
1115
|
-
this.C = C | 0;
|
|
1116
|
-
this.D = D | 0;
|
|
1117
|
-
this.E = E | 0;
|
|
1118
|
-
this.F = F | 0;
|
|
1119
|
-
this.G = G | 0;
|
|
1120
|
-
this.H = H | 0;
|
|
1121
|
-
}
|
|
1122
|
-
process(view, offset) {
|
|
1123
|
-
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
1124
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
1125
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
1126
|
-
for (let i = 16; i < 64; i++) {
|
|
1127
|
-
const W15 = SHA256_W[i - 15];
|
|
1128
|
-
const W2 = SHA256_W[i - 2];
|
|
1129
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
1130
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
1131
|
-
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
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
|
|
1132
1151
|
}
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
G = F;
|
|
1142
|
-
F = E;
|
|
1143
|
-
E = (D + T1) | 0;
|
|
1144
|
-
D = C;
|
|
1145
|
-
C = B;
|
|
1146
|
-
B = A;
|
|
1147
|
-
A = (T1 + T2) | 0;
|
|
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;
|
|
1148
1160
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
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
|
+
};
|
|
1159
1174
|
}
|
|
1160
|
-
|
|
1161
|
-
|
|
1175
|
+
catch {
|
|
1176
|
+
return null;
|
|
1162
1177
|
}
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
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;
|
|
1166
1190
|
}
|
|
1167
1191
|
}
|
|
1168
|
-
/**
|
|
1169
|
-
* SHA2-256 hash function
|
|
1170
|
-
* @param message - data that would be hashed
|
|
1171
|
-
*/
|
|
1172
|
-
const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
1173
1192
|
|
|
1174
1193
|
class Invoice {
|
|
1175
1194
|
constructor(args) {
|
|
@@ -1183,6 +1202,8 @@ class Invoice {
|
|
|
1183
1202
|
}
|
|
1184
1203
|
this.paymentHash = decodedInvoice.paymentHash;
|
|
1185
1204
|
this.satoshi = decodedInvoice.satoshi;
|
|
1205
|
+
this.millisatoshi = decodedInvoice.millisatoshi;
|
|
1206
|
+
this.amountRaw = decodedInvoice.amountRaw;
|
|
1186
1207
|
this.timestamp = decodedInvoice.timestamp;
|
|
1187
1208
|
this.expiry = decodedInvoice.expiry;
|
|
1188
1209
|
this.createdDate = new Date(this.timestamp * 1000);
|
|
@@ -1207,13 +1228,7 @@ class Invoice {
|
|
|
1207
1228
|
validatePreimage(preimage) {
|
|
1208
1229
|
if (!preimage || !this.paymentHash)
|
|
1209
1230
|
return false;
|
|
1210
|
-
|
|
1211
|
-
const preimageHash = bytesToHex(sha256(fromHexString(preimage)));
|
|
1212
|
-
return this.paymentHash === preimageHash;
|
|
1213
|
-
}
|
|
1214
|
-
catch {
|
|
1215
|
-
return false;
|
|
1216
|
-
}
|
|
1231
|
+
return validatePreimage(preimage, this.paymentHash);
|
|
1217
1232
|
}
|
|
1218
1233
|
async verifyPayment() {
|
|
1219
1234
|
try {
|
|
@@ -1247,4 +1262,5 @@ class Invoice {
|
|
|
1247
1262
|
exports.Invoice = Invoice;
|
|
1248
1263
|
exports.decodeInvoice = decodeInvoice;
|
|
1249
1264
|
exports.fromHexString = fromHexString;
|
|
1265
|
+
exports.validatePreimage = validatePreimage;
|
|
1250
1266
|
//# sourceMappingURL=bolt11.cjs.map
|