@getalby/lightning-tools 8.0.0 → 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.
@@ -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;
@@ -664,528 +971,234 @@ function requireBolt11 () {
664
971
  // doesn't have anything, there's a good chance the last letter of the
665
972
  // coin type got captured by the third group, so just re-regex without
666
973
  // the number.
667
- let prefixMatches = prefix.match(/^ln(\S+?)(\d*)([a-zA-Z]?)$/);
668
- if (prefixMatches && !prefixMatches[2])
669
- prefixMatches = prefix.match(/^ln(\S+)$/);
670
- if (!prefixMatches) {
671
- throw new Error('Not a proper lightning payment request')
672
- }
673
-
674
- // "ln" section
675
- sections.push({
676
- name: 'lightning_network',
677
- letters: 'ln'
678
- });
679
-
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
- };
974
+ let prefixMatches = prefix.match(/^ln(\S+?)(\d*)([a-zA-Z]?)$/);
975
+ if (prefixMatches && !prefixMatches[2])
976
+ prefixMatches = prefix.match(/^ln(\S+)$/);
977
+ if (!prefixMatches) {
978
+ throw new Error('Not a proper lightning payment request')
979
+ }
882
980
 
883
- function bytes(b, ...lengths) {
884
- if (!(b instanceof Uint8Array))
885
- throw new Error('Expected Uint8Array');
886
- if (lengths.length > 0 && !lengths.includes(b.length))
887
- throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
888
- }
889
- function exists(instance, checkFinished = true) {
890
- if (instance.destroyed)
891
- throw new Error('Hash instance has been destroyed');
892
- if (checkFinished && instance.finished)
893
- throw new Error('Hash#digest() has already been called');
894
- }
895
- function output(out, instance) {
896
- bytes(out);
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
- }
981
+ // "ln" section
982
+ sections.push({
983
+ name: 'lightning_network',
984
+ letters: 'ln'
985
+ });
902
986
 
903
- /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
904
- // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
905
- // node.js versions earlier than v19 don't declare it in global scope.
906
- // For node.js, package.json#exports field mapping rewrites import
907
- // from `crypto` to `cryptoNode`, which imports native module.
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
- }
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
+ });
969
1026
 
970
- // Polyfill for Safari 14
971
- function setBigUint64(view, byteOffset, value, isLE) {
972
- if (typeof view.setBigUint64 === 'function')
973
- return view.setBigUint64(byteOffset, value, isLE);
974
- const _32n = BigInt(32);
975
- const _u32_max = BigInt(0xffffffff);
976
- const wh = Number((value >> _32n) & _u32_max);
977
- const wl = Number(value & _u32_max);
978
- const h = isLE ? 4 : 0;
979
- const l = isLE ? 0 : 4;
980
- view.setUint32(byteOffset + h, wh, isLE);
981
- view.setUint32(byteOffset + l, wl, isLE);
982
- }
983
- // Base SHA2 class (RFC 6234)
984
- class SHA2 extends Hash {
985
- constructor(blockLen, outputLen, padOffset, isLE) {
986
- super();
987
- this.blockLen = blockLen;
988
- this.outputLen = outputLen;
989
- this.padOffset = padOffset;
990
- this.isLE = isLE;
991
- this.finished = false;
992
- this.length = 0;
993
- this.pos = 0;
994
- this.destroyed = false;
995
- this.buffer = new Uint8Array(blockLen);
996
- this.view = createView(this.buffer);
997
- }
998
- update(data) {
999
- exists(this);
1000
- const { view, buffer, blockLen } = this;
1001
- data = toBytes(data);
1002
- const len = data.length;
1003
- for (let pos = 0; pos < len;) {
1004
- const take = Math.min(blockLen - this.pos, len - pos);
1005
- // Fast path: we have at least one block in input, cast it to view and process
1006
- if (take === blockLen) {
1007
- const dataView = createView(data);
1008
- for (; blockLen <= len - pos; pos += blockLen)
1009
- this.process(dataView, pos);
1010
- continue;
1011
- }
1012
- buffer.set(data.subarray(pos, pos + take), this.pos);
1013
- this.pos += take;
1014
- pos += take;
1015
- if (this.pos === blockLen) {
1016
- this.process(view, 0);
1017
- this.pos = 0;
1018
- }
1019
- }
1020
- this.length += data.length;
1021
- this.roundClean();
1022
- return this;
1023
- }
1024
- digestInto(out) {
1025
- exists(this);
1026
- output(out, this);
1027
- this.finished = true;
1028
- // Padding
1029
- // We can avoid allocation of buffer for padding completely if it
1030
- // was previously not allocated here. But it won't change performance.
1031
- const { buffer, view, blockLen, isLE } = this;
1032
- let { pos } = this;
1033
- // append the bit '1' to the message
1034
- buffer[pos++] = 0b10000000;
1035
- this.buffer.subarray(pos).fill(0);
1036
- // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
1037
- if (this.padOffset > blockLen - pos) {
1038
- this.process(view, 0);
1039
- pos = 0;
1040
- }
1041
- // Pad until full block byte with zeros
1042
- for (let i = pos; i < blockLen; i++)
1043
- buffer[i] = 0;
1044
- // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
1045
- // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
1046
- // So we just write lowest 64 bits of that value.
1047
- setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
1048
- this.process(view, 0);
1049
- const oview = createView(out);
1050
- const len = this.outputLen;
1051
- // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
1052
- if (len % 4)
1053
- throw new Error('_sha2: outputLen should be aligned to 32bit');
1054
- const outLen = len / 4;
1055
- const state = this.get();
1056
- if (outLen > state.length)
1057
- throw new Error('_sha2: outputLen bigger than state');
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
- }
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
+ }
1041
+
1042
+ // "1" separator
1043
+ sections.push({
1044
+ name: 'separator',
1045
+ letters: '1'
1046
+ });
1047
+
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
- // SHA2-256 need to try 2^128 hashes to execute birthday attack.
1083
- // BTC network is doing 2^67 hashes/sec as per early 2023.
1084
- // Choice: a ? b : c
1085
- const Chi = (a, b, c) => (a & b) ^ (~a & c);
1086
- // Majority function, true if any two inpust is true
1087
- const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
1088
- // Round constants:
1089
- // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
1090
- // prettier-ignore
1091
- const SHA256_K = /* @__PURE__ */ new Uint32Array([
1092
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
1093
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
1094
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
1095
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
1096
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
1097
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
1098
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
1099
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
1100
- ]);
1101
- // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
1102
- // prettier-ignore
1103
- const IV = /* @__PURE__ */ new Uint32Array([
1104
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
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
- // Compression function main loop, 64 rounds
1150
- let { A, B, C, D, E, F, G, H } = this;
1151
- for (let i = 0; i < 64; i++) {
1152
- const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
1153
- const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
1154
- const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
1155
- const T2 = (sigma0 + Maj(A, B, C)) | 0;
1156
- H = G;
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
- // Add the compressed chunk to the current hash value
1166
- A = (A + this.A) | 0;
1167
- B = (B + this.B) | 0;
1168
- C = (C + this.C) | 0;
1169
- D = (D + this.D) | 0;
1170
- E = (E + this.E) | 0;
1171
- F = (F + this.F) | 0;
1172
- G = (G + this.G) | 0;
1173
- H = (H + this.H) | 0;
1174
- this.set(A, B, C, D, E, F, G, H);
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
- roundClean() {
1177
- SHA256_W.fill(0);
1185
+ catch {
1186
+ return null;
1178
1187
  }
1179
- destroy() {
1180
- this.set(0, 0, 0, 0, 0, 0, 0, 0);
1181
- this.buffer.fill(0);
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
- try {
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 {