@moonpay/platform-sdk-core 0.0.0 → 0.2.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/index.js ADDED
@@ -0,0 +1,1568 @@
1
+ // src/api/client-context.ts
2
+ function createClientContext(options = {}) {
3
+ const { apiBaseUrl = "https://api.moonpay.com" } = options;
4
+ let _accessToken = "";
5
+ let _clientToken = "";
6
+ return {
7
+ get accessToken() {
8
+ return _accessToken;
9
+ },
10
+ get clientToken() {
11
+ return _clientToken;
12
+ },
13
+ setAccessToken(token) {
14
+ _accessToken = token;
15
+ },
16
+ setClientToken(token) {
17
+ _clientToken = token;
18
+ },
19
+ fetch(url, options2) {
20
+ const headers = {
21
+ "Content-Type": "application/json",
22
+ ...options2?.headers
23
+ };
24
+ if (_accessToken) {
25
+ headers.Authorization = `Bearer ${_accessToken}`;
26
+ }
27
+ const resolvedUrl = url.startsWith("http") ? url : `${apiBaseUrl}${url}`;
28
+ return fetch(resolvedUrl, { ...options2, headers });
29
+ }
30
+ };
31
+ }
32
+
33
+ // src/api/fetch-wrapper.ts
34
+ import { err, ok } from "@moonpay/platform-protocol";
35
+ async function apiFetch(ctx, path, options) {
36
+ const response = await ctx.fetch(path, options);
37
+ const data = await response.json();
38
+ if (!response.ok) {
39
+ const error = data;
40
+ return err({
41
+ code: error.code ?? "unknown_error",
42
+ message: error.message ?? "An unknown error occurred",
43
+ errors: error.errors
44
+ });
45
+ }
46
+ return ok(data);
47
+ }
48
+
49
+ // src/api/get-quote.ts
50
+ async function getQuote(ctx, params) {
51
+ return apiFetch(ctx, "/platform/v1/quotes/buy", {
52
+ method: "POST",
53
+ body: JSON.stringify(params)
54
+ });
55
+ }
56
+
57
+ // src/api/get-transactions.ts
58
+ async function listTransactions(ctx, params = {}) {
59
+ const searchParams = new URLSearchParams();
60
+ if (params.startDate)
61
+ searchParams.set("startDate", params.startDate);
62
+ if (params.endDate)
63
+ searchParams.set("endDate", params.endDate);
64
+ if (params.cursor)
65
+ searchParams.set("cursor", params.cursor);
66
+ if (params.limit != null)
67
+ searchParams.set("limit", String(params.limit));
68
+ const query = searchParams.toString();
69
+ const path = `/platform/v1/transactions${query ? `?${query}` : ""}`;
70
+ return apiFetch(ctx, path);
71
+ }
72
+ async function getTransaction(ctx, id) {
73
+ return apiFetch(
74
+ ctx,
75
+ `/platform/v1/transactions/${encodeURIComponent(id)}`
76
+ );
77
+ }
78
+
79
+ // src/api/payment-methods.ts
80
+ import {
81
+ err as err2,
82
+ ok as ok2
83
+ } from "@moonpay/platform-protocol";
84
+ async function getPaymentMethods(ctx) {
85
+ return apiFetch(ctx, "/platform/v1/payment-methods");
86
+ }
87
+ async function deletePaymentMethod(ctx, paymentMethodId) {
88
+ const response = await ctx.fetch(
89
+ `/platform/v1/payment-methods/${encodeURIComponent(paymentMethodId)}`,
90
+ { method: "DELETE" }
91
+ );
92
+ if (!response.ok) {
93
+ const data = await response.json().catch(() => ({}));
94
+ return err2({
95
+ code: data.code ?? "unknown_error",
96
+ message: data.message ?? "Failed to delete payment method"
97
+ });
98
+ }
99
+ return ok2(void 0);
100
+ }
101
+
102
+ // src/client.ts
103
+ function createClientCore(options) {
104
+ const ctx = createClientContext({
105
+ apiBaseUrl: options.apiBaseUrl
106
+ });
107
+ return {
108
+ get context() {
109
+ return ctx;
110
+ },
111
+ get frameBaseUrl() {
112
+ return options.frameBaseUrl;
113
+ },
114
+ createTransport: options.createTransport,
115
+ getQuote: (params) => getQuote(ctx, params),
116
+ getPaymentMethods: () => getPaymentMethods(ctx),
117
+ listTransactions: (params) => listTransactions(ctx, params),
118
+ getTransaction: (id) => getTransaction(ctx, id),
119
+ deletePaymentMethod: (id) => deletePaymentMethod(ctx, id)
120
+ };
121
+ }
122
+
123
+ // ../../node_modules/.bun/@noble+ciphers@1.2.0/node_modules/@noble/ciphers/esm/_assert.js
124
+ function isBytes(a) {
125
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
126
+ }
127
+ function abytes(b, ...lengths) {
128
+ if (!isBytes(b))
129
+ throw new Error("Uint8Array expected");
130
+ if (lengths.length > 0 && !lengths.includes(b.length))
131
+ throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
132
+ }
133
+ function aexists(instance, checkFinished = true) {
134
+ if (instance.destroyed)
135
+ throw new Error("Hash instance has been destroyed");
136
+ if (checkFinished && instance.finished)
137
+ throw new Error("Hash#digest() has already been called");
138
+ }
139
+ function aoutput(out, instance) {
140
+ abytes(out);
141
+ const min = instance.outputLen;
142
+ if (out.length < min) {
143
+ throw new Error("digestInto() expects output buffer of length at least " + min);
144
+ }
145
+ }
146
+
147
+ // ../../node_modules/.bun/@noble+ciphers@1.2.0/node_modules/@noble/ciphers/esm/utils.js
148
+ var u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
149
+ var u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
150
+ var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
151
+ var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
152
+ if (!isLE)
153
+ throw new Error("Non little-endian hardware is not supported");
154
+ function utf8ToBytes(str) {
155
+ if (typeof str !== "string")
156
+ throw new Error("string expected");
157
+ return new Uint8Array(new TextEncoder().encode(str));
158
+ }
159
+ function toBytes(data) {
160
+ if (typeof data === "string")
161
+ data = utf8ToBytes(data);
162
+ else if (isBytes(data))
163
+ data = copyBytes(data);
164
+ else
165
+ throw new Error("Uint8Array expected, got " + typeof data);
166
+ return data;
167
+ }
168
+ function equalBytes(a, b) {
169
+ if (a.length !== b.length)
170
+ return false;
171
+ let diff = 0;
172
+ for (let i = 0; i < a.length; i++)
173
+ diff |= a[i] ^ b[i];
174
+ return diff === 0;
175
+ }
176
+ var wrapCipher = /* @__NO_SIDE_EFFECTS__ */ (params, constructor) => {
177
+ function wrappedCipher(key, ...args) {
178
+ abytes(key);
179
+ if (params.nonceLength !== void 0) {
180
+ const nonce = args[0];
181
+ if (!nonce)
182
+ throw new Error("nonce / iv required");
183
+ if (params.varSizeNonce)
184
+ abytes(nonce);
185
+ else
186
+ abytes(nonce, params.nonceLength);
187
+ }
188
+ const tagl = params.tagLength;
189
+ if (tagl && args[1] !== void 0) {
190
+ abytes(args[1]);
191
+ }
192
+ const cipher = constructor(key, ...args);
193
+ const checkOutput = (fnLength, output) => {
194
+ if (output !== void 0) {
195
+ if (fnLength !== 2)
196
+ throw new Error("cipher output not supported");
197
+ abytes(output);
198
+ }
199
+ };
200
+ let called = false;
201
+ const wrCipher = {
202
+ encrypt(data, output) {
203
+ if (called)
204
+ throw new Error("cannot encrypt() twice with same key + nonce");
205
+ called = true;
206
+ abytes(data);
207
+ checkOutput(cipher.encrypt.length, output);
208
+ return cipher.encrypt(data, output);
209
+ },
210
+ decrypt(data, output) {
211
+ abytes(data);
212
+ if (tagl && data.length < tagl)
213
+ throw new Error("invalid ciphertext length: smaller than tagLength=" + tagl);
214
+ checkOutput(cipher.decrypt.length, output);
215
+ return cipher.decrypt(data, output);
216
+ }
217
+ };
218
+ return wrCipher;
219
+ }
220
+ Object.assign(wrappedCipher, params);
221
+ return wrappedCipher;
222
+ };
223
+ function getOutput(expectedLength, out, onlyAligned = true) {
224
+ if (out === void 0)
225
+ return new Uint8Array(expectedLength);
226
+ if (out.length !== expectedLength)
227
+ throw new Error("invalid output length, expected " + expectedLength + ", got: " + out.length);
228
+ if (onlyAligned && !isAligned32(out))
229
+ throw new Error("invalid output, must be aligned");
230
+ return out;
231
+ }
232
+ function setBigUint64(view, byteOffset, value, isLE2) {
233
+ if (typeof view.setBigUint64 === "function")
234
+ return view.setBigUint64(byteOffset, value, isLE2);
235
+ const _32n = BigInt(32);
236
+ const _u32_max = BigInt(4294967295);
237
+ const wh = Number(value >> _32n & _u32_max);
238
+ const wl = Number(value & _u32_max);
239
+ const h = isLE2 ? 4 : 0;
240
+ const l = isLE2 ? 0 : 4;
241
+ view.setUint32(byteOffset + h, wh, isLE2);
242
+ view.setUint32(byteOffset + l, wl, isLE2);
243
+ }
244
+ function isAligned32(bytes) {
245
+ return bytes.byteOffset % 4 === 0;
246
+ }
247
+ function copyBytes(bytes) {
248
+ return Uint8Array.from(bytes);
249
+ }
250
+ function clean(...arrays) {
251
+ for (let i = 0; i < arrays.length; i++) {
252
+ arrays[i].fill(0);
253
+ }
254
+ }
255
+
256
+ // ../../node_modules/.bun/@noble+ciphers@1.2.0/node_modules/@noble/ciphers/esm/_polyval.js
257
+ var BLOCK_SIZE = 16;
258
+ var ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
259
+ var ZEROS32 = u32(ZEROS16);
260
+ var POLY = 225;
261
+ var mul2 = (s0, s1, s2, s3) => {
262
+ const hiBit = s3 & 1;
263
+ return {
264
+ s3: s2 << 31 | s3 >>> 1,
265
+ s2: s1 << 31 | s2 >>> 1,
266
+ s1: s0 << 31 | s1 >>> 1,
267
+ s0: s0 >>> 1 ^ POLY << 24 & -(hiBit & 1)
268
+ // reduce % poly
269
+ };
270
+ };
271
+ var swapLE = (n) => (n >>> 0 & 255) << 24 | (n >>> 8 & 255) << 16 | (n >>> 16 & 255) << 8 | n >>> 24 & 255 | 0;
272
+ function _toGHASHKey(k) {
273
+ k.reverse();
274
+ const hiBit = k[15] & 1;
275
+ let carry = 0;
276
+ for (let i = 0; i < k.length; i++) {
277
+ const t = k[i];
278
+ k[i] = t >>> 1 | carry;
279
+ carry = (t & 1) << 7;
280
+ }
281
+ k[0] ^= -hiBit & 225;
282
+ return k;
283
+ }
284
+ var estimateWindow = (bytes) => {
285
+ if (bytes > 64 * 1024)
286
+ return 8;
287
+ if (bytes > 1024)
288
+ return 4;
289
+ return 2;
290
+ };
291
+ var GHASH = class {
292
+ // We select bits per window adaptively based on expectedLength
293
+ constructor(key, expectedLength) {
294
+ this.blockLen = BLOCK_SIZE;
295
+ this.outputLen = BLOCK_SIZE;
296
+ this.s0 = 0;
297
+ this.s1 = 0;
298
+ this.s2 = 0;
299
+ this.s3 = 0;
300
+ this.finished = false;
301
+ key = toBytes(key);
302
+ abytes(key, 16);
303
+ const kView = createView(key);
304
+ let k0 = kView.getUint32(0, false);
305
+ let k1 = kView.getUint32(4, false);
306
+ let k2 = kView.getUint32(8, false);
307
+ let k3 = kView.getUint32(12, false);
308
+ const doubles = [];
309
+ for (let i = 0; i < 128; i++) {
310
+ doubles.push({ s0: swapLE(k0), s1: swapLE(k1), s2: swapLE(k2), s3: swapLE(k3) });
311
+ ({ s0: k0, s1: k1, s2: k2, s3: k3 } = mul2(k0, k1, k2, k3));
312
+ }
313
+ const W = estimateWindow(expectedLength || 1024);
314
+ if (![1, 2, 4, 8].includes(W))
315
+ throw new Error("ghash: invalid window size, expected 2, 4 or 8");
316
+ this.W = W;
317
+ const bits = 128;
318
+ const windows = bits / W;
319
+ const windowSize = this.windowSize = 2 ** W;
320
+ const items = [];
321
+ for (let w = 0; w < windows; w++) {
322
+ for (let byte = 0; byte < windowSize; byte++) {
323
+ let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
324
+ for (let j = 0; j < W; j++) {
325
+ const bit = byte >>> W - j - 1 & 1;
326
+ if (!bit)
327
+ continue;
328
+ const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j];
329
+ s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
330
+ }
331
+ items.push({ s0, s1, s2, s3 });
332
+ }
333
+ }
334
+ this.t = items;
335
+ }
336
+ _updateBlock(s0, s1, s2, s3) {
337
+ s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
338
+ const { W, t, windowSize } = this;
339
+ let o0 = 0, o1 = 0, o2 = 0, o3 = 0;
340
+ const mask = (1 << W) - 1;
341
+ let w = 0;
342
+ for (const num of [s0, s1, s2, s3]) {
343
+ for (let bytePos = 0; bytePos < 4; bytePos++) {
344
+ const byte = num >>> 8 * bytePos & 255;
345
+ for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
346
+ const bit = byte >>> W * bitPos & mask;
347
+ const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit];
348
+ o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
349
+ w += 1;
350
+ }
351
+ }
352
+ }
353
+ this.s0 = o0;
354
+ this.s1 = o1;
355
+ this.s2 = o2;
356
+ this.s3 = o3;
357
+ }
358
+ update(data) {
359
+ data = toBytes(data);
360
+ aexists(this);
361
+ const b32 = u32(data);
362
+ const blocks = Math.floor(data.length / BLOCK_SIZE);
363
+ const left = data.length % BLOCK_SIZE;
364
+ for (let i = 0; i < blocks; i++) {
365
+ this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
366
+ }
367
+ if (left) {
368
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
369
+ this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
370
+ clean(ZEROS32);
371
+ }
372
+ return this;
373
+ }
374
+ destroy() {
375
+ const { t } = this;
376
+ for (const elm of t) {
377
+ elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
378
+ }
379
+ }
380
+ digestInto(out) {
381
+ aexists(this);
382
+ aoutput(out, this);
383
+ this.finished = true;
384
+ const { s0, s1, s2, s3 } = this;
385
+ const o32 = u32(out);
386
+ o32[0] = s0;
387
+ o32[1] = s1;
388
+ o32[2] = s2;
389
+ o32[3] = s3;
390
+ return out;
391
+ }
392
+ digest() {
393
+ const res = new Uint8Array(BLOCK_SIZE);
394
+ this.digestInto(res);
395
+ this.destroy();
396
+ return res;
397
+ }
398
+ };
399
+ var Polyval = class extends GHASH {
400
+ constructor(key, expectedLength) {
401
+ key = toBytes(key);
402
+ const ghKey = _toGHASHKey(copyBytes(key));
403
+ super(ghKey, expectedLength);
404
+ clean(ghKey);
405
+ }
406
+ update(data) {
407
+ data = toBytes(data);
408
+ aexists(this);
409
+ const b32 = u32(data);
410
+ const left = data.length % BLOCK_SIZE;
411
+ const blocks = Math.floor(data.length / BLOCK_SIZE);
412
+ for (let i = 0; i < blocks; i++) {
413
+ this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
414
+ }
415
+ if (left) {
416
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
417
+ this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
418
+ clean(ZEROS32);
419
+ }
420
+ return this;
421
+ }
422
+ digestInto(out) {
423
+ aexists(this);
424
+ aoutput(out, this);
425
+ this.finished = true;
426
+ const { s0, s1, s2, s3 } = this;
427
+ const o32 = u32(out);
428
+ o32[0] = s0;
429
+ o32[1] = s1;
430
+ o32[2] = s2;
431
+ o32[3] = s3;
432
+ return out.reverse();
433
+ }
434
+ };
435
+ function wrapConstructorWithKey(hashCons) {
436
+ const hashC = (msg, key) => hashCons(key, msg.length).update(toBytes(msg)).digest();
437
+ const tmp = hashCons(new Uint8Array(16), 0);
438
+ hashC.outputLen = tmp.outputLen;
439
+ hashC.blockLen = tmp.blockLen;
440
+ hashC.create = (key, expectedLength) => hashCons(key, expectedLength);
441
+ return hashC;
442
+ }
443
+ var ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength));
444
+ var polyval = wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength));
445
+
446
+ // ../../node_modules/.bun/@noble+ciphers@1.2.0/node_modules/@noble/ciphers/esm/aes.js
447
+ var BLOCK_SIZE2 = 16;
448
+ var BLOCK_SIZE32 = 4;
449
+ var EMPTY_BLOCK = /* @__PURE__ */ new Uint8Array(BLOCK_SIZE2);
450
+ var POLY2 = 283;
451
+ function mul22(n) {
452
+ return n << 1 ^ POLY2 & -(n >> 7);
453
+ }
454
+ function mul(a, b) {
455
+ let res = 0;
456
+ for (; b > 0; b >>= 1) {
457
+ res ^= a & -(b & 1);
458
+ a = mul22(a);
459
+ }
460
+ return res;
461
+ }
462
+ var sbox = /* @__PURE__ */ (() => {
463
+ const t = new Uint8Array(256);
464
+ for (let i = 0, x = 1; i < 256; i++, x ^= mul22(x))
465
+ t[i] = x;
466
+ const box = new Uint8Array(256);
467
+ box[0] = 99;
468
+ for (let i = 0; i < 255; i++) {
469
+ let x = t[255 - i];
470
+ x |= x << 8;
471
+ box[t[i]] = (x ^ x >> 4 ^ x >> 5 ^ x >> 6 ^ x >> 7 ^ 99) & 255;
472
+ }
473
+ clean(t);
474
+ return box;
475
+ })();
476
+ var rotr32_8 = (n) => n << 24 | n >>> 8;
477
+ var rotl32_8 = (n) => n << 8 | n >>> 24;
478
+ function genTtable(sbox2, fn) {
479
+ if (sbox2.length !== 256)
480
+ throw new Error("Wrong sbox length");
481
+ const T0 = new Uint32Array(256).map((_, j) => fn(sbox2[j]));
482
+ const T1 = T0.map(rotl32_8);
483
+ const T2 = T1.map(rotl32_8);
484
+ const T3 = T2.map(rotl32_8);
485
+ const T01 = new Uint32Array(256 * 256);
486
+ const T23 = new Uint32Array(256 * 256);
487
+ const sbox22 = new Uint16Array(256 * 256);
488
+ for (let i = 0; i < 256; i++) {
489
+ for (let j = 0; j < 256; j++) {
490
+ const idx = i * 256 + j;
491
+ T01[idx] = T0[i] ^ T1[j];
492
+ T23[idx] = T2[i] ^ T3[j];
493
+ sbox22[idx] = sbox2[i] << 8 | sbox2[j];
494
+ }
495
+ }
496
+ return { sbox: sbox2, sbox2: sbox22, T0, T1, T2, T3, T01, T23 };
497
+ }
498
+ var tableEncoding = /* @__PURE__ */ genTtable(sbox, (s) => mul(s, 3) << 24 | s << 16 | s << 8 | mul(s, 2));
499
+ var xPowers = /* @__PURE__ */ (() => {
500
+ const p = new Uint8Array(16);
501
+ for (let i = 0, x = 1; i < 16; i++, x = mul22(x))
502
+ p[i] = x;
503
+ return p;
504
+ })();
505
+ function expandKeyLE(key) {
506
+ abytes(key);
507
+ const len = key.length;
508
+ if (![16, 24, 32].includes(len))
509
+ throw new Error("aes: invalid key size, should be 16, 24 or 32, got " + len);
510
+ const { sbox2 } = tableEncoding;
511
+ const toClean = [];
512
+ if (!isAligned32(key))
513
+ toClean.push(key = copyBytes(key));
514
+ const k32 = u32(key);
515
+ const Nk = k32.length;
516
+ const subByte = (n) => applySbox(sbox2, n, n, n, n);
517
+ const xk = new Uint32Array(len + 28);
518
+ xk.set(k32);
519
+ for (let i = Nk; i < xk.length; i++) {
520
+ let t = xk[i - 1];
521
+ if (i % Nk === 0)
522
+ t = subByte(rotr32_8(t)) ^ xPowers[i / Nk - 1];
523
+ else if (Nk > 6 && i % Nk === 4)
524
+ t = subByte(t);
525
+ xk[i] = xk[i - Nk] ^ t;
526
+ }
527
+ clean(...toClean);
528
+ return xk;
529
+ }
530
+ function apply0123(T01, T23, s0, s1, s2, s3) {
531
+ return T01[s0 << 8 & 65280 | s1 >>> 8 & 255] ^ T23[s2 >>> 8 & 65280 | s3 >>> 24 & 255];
532
+ }
533
+ function applySbox(sbox2, s0, s1, s2, s3) {
534
+ return sbox2[s0 & 255 | s1 & 65280] | sbox2[s2 >>> 16 & 255 | s3 >>> 16 & 65280] << 16;
535
+ }
536
+ function encrypt(xk, s0, s1, s2, s3) {
537
+ const { sbox2, T01, T23 } = tableEncoding;
538
+ let k = 0;
539
+ s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
540
+ const rounds = xk.length / 4 - 2;
541
+ for (let i = 0; i < rounds; i++) {
542
+ const t02 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
543
+ const t12 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
544
+ const t22 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
545
+ const t32 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
546
+ s0 = t02, s1 = t12, s2 = t22, s3 = t32;
547
+ }
548
+ const t0 = xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3);
549
+ const t1 = xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0);
550
+ const t2 = xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1);
551
+ const t3 = xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2);
552
+ return { s0: t0, s1: t1, s2: t2, s3: t3 };
553
+ }
554
+ function ctr32(xk, isLE2, nonce, src, dst) {
555
+ abytes(nonce, BLOCK_SIZE2);
556
+ abytes(src);
557
+ dst = getOutput(src.length, dst);
558
+ const ctr = nonce;
559
+ const c32 = u32(ctr);
560
+ const view = createView(ctr);
561
+ const src32 = u32(src);
562
+ const dst32 = u32(dst);
563
+ const ctrPos = isLE2 ? 0 : 12;
564
+ const srcLen = src.length;
565
+ let ctrNum = view.getUint32(ctrPos, isLE2);
566
+ let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
567
+ for (let i = 0; i + 4 <= src32.length; i += 4) {
568
+ dst32[i + 0] = src32[i + 0] ^ s0;
569
+ dst32[i + 1] = src32[i + 1] ^ s1;
570
+ dst32[i + 2] = src32[i + 2] ^ s2;
571
+ dst32[i + 3] = src32[i + 3] ^ s3;
572
+ ctrNum = ctrNum + 1 >>> 0;
573
+ view.setUint32(ctrPos, ctrNum, isLE2);
574
+ ({ s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
575
+ }
576
+ const start = BLOCK_SIZE2 * Math.floor(src32.length / BLOCK_SIZE32);
577
+ if (start < srcLen) {
578
+ const b32 = new Uint32Array([s0, s1, s2, s3]);
579
+ const buf = u8(b32);
580
+ for (let i = start, pos = 0; i < srcLen; i++, pos++)
581
+ dst[i] = src[i] ^ buf[pos];
582
+ clean(b32);
583
+ }
584
+ return dst;
585
+ }
586
+ function computeTag(fn, isLE2, key, data, AAD) {
587
+ const aadLength = AAD == null ? 0 : AAD.length;
588
+ const h = fn.create(key, data.length + aadLength);
589
+ if (AAD)
590
+ h.update(AAD);
591
+ h.update(data);
592
+ const num = new Uint8Array(16);
593
+ const view = createView(num);
594
+ if (AAD)
595
+ setBigUint64(view, 0, BigInt(aadLength * 8), isLE2);
596
+ setBigUint64(view, 8, BigInt(data.length * 8), isLE2);
597
+ h.update(num);
598
+ const res = h.digest();
599
+ clean(num);
600
+ return res;
601
+ }
602
+ var gcm = /* @__PURE__ */ wrapCipher({ blockSize: 16, nonceLength: 12, tagLength: 16, varSizeNonce: true }, function aesgcm(key, nonce, AAD) {
603
+ if (nonce.length < 8)
604
+ throw new Error("aes/gcm: invalid nonce length");
605
+ const tagLength = 16;
606
+ function _computeTag(authKey, tagMask, data) {
607
+ const tag = computeTag(ghash, false, authKey, data, AAD);
608
+ for (let i = 0; i < tagMask.length; i++)
609
+ tag[i] ^= tagMask[i];
610
+ return tag;
611
+ }
612
+ function deriveKeys() {
613
+ const xk = expandKeyLE(key);
614
+ const authKey = EMPTY_BLOCK.slice();
615
+ const counter = EMPTY_BLOCK.slice();
616
+ ctr32(xk, false, counter, counter, authKey);
617
+ if (nonce.length === 12) {
618
+ counter.set(nonce);
619
+ } else {
620
+ const nonceLen = EMPTY_BLOCK.slice();
621
+ const view = createView(nonceLen);
622
+ setBigUint64(view, 8, BigInt(nonce.length * 8), false);
623
+ const g = ghash.create(authKey).update(nonce).update(nonceLen);
624
+ g.digestInto(counter);
625
+ g.destroy();
626
+ }
627
+ const tagMask = ctr32(xk, false, counter, EMPTY_BLOCK);
628
+ return { xk, authKey, counter, tagMask };
629
+ }
630
+ return {
631
+ encrypt(plaintext) {
632
+ const { xk, authKey, counter, tagMask } = deriveKeys();
633
+ const out = new Uint8Array(plaintext.length + tagLength);
634
+ const toClean = [xk, authKey, counter, tagMask];
635
+ if (!isAligned32(plaintext))
636
+ toClean.push(plaintext = copyBytes(plaintext));
637
+ ctr32(xk, false, counter, plaintext, out.subarray(0, plaintext.length));
638
+ const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
639
+ toClean.push(tag);
640
+ out.set(tag, plaintext.length);
641
+ clean(...toClean);
642
+ return out;
643
+ },
644
+ decrypt(ciphertext) {
645
+ const { xk, authKey, counter, tagMask } = deriveKeys();
646
+ const toClean = [xk, authKey, tagMask, counter];
647
+ if (!isAligned32(ciphertext))
648
+ toClean.push(ciphertext = copyBytes(ciphertext));
649
+ const data = ciphertext.subarray(0, -tagLength);
650
+ const passedTag = ciphertext.subarray(-tagLength);
651
+ const tag = _computeTag(authKey, tagMask, data);
652
+ toClean.push(tag);
653
+ if (!equalBytes(tag, passedTag))
654
+ throw new Error("aes/gcm: invalid ghash tag");
655
+ const out = ctr32(xk, false, counter, data);
656
+ clean(...toClean);
657
+ return out;
658
+ }
659
+ };
660
+ });
661
+
662
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/_assert.js
663
+ function anumber(n) {
664
+ if (!Number.isSafeInteger(n) || n < 0)
665
+ throw new Error("positive integer expected, got " + n);
666
+ }
667
+ function isBytes2(a) {
668
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
669
+ }
670
+ function abytes2(b, ...lengths) {
671
+ if (!isBytes2(b))
672
+ throw new Error("Uint8Array expected");
673
+ if (lengths.length > 0 && !lengths.includes(b.length))
674
+ throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
675
+ }
676
+ function ahash(h) {
677
+ if (typeof h !== "function" || typeof h.create !== "function")
678
+ throw new Error("Hash should be wrapped by utils.wrapConstructor");
679
+ anumber(h.outputLen);
680
+ anumber(h.blockLen);
681
+ }
682
+ function aexists2(instance, checkFinished = true) {
683
+ if (instance.destroyed)
684
+ throw new Error("Hash instance has been destroyed");
685
+ if (checkFinished && instance.finished)
686
+ throw new Error("Hash#digest() has already been called");
687
+ }
688
+ function aoutput2(out, instance) {
689
+ abytes2(out);
690
+ const min = instance.outputLen;
691
+ if (out.length < min) {
692
+ throw new Error("digestInto() expects output buffer of length at least " + min);
693
+ }
694
+ }
695
+
696
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/cryptoNode.js
697
+ import * as nc from "crypto";
698
+ var crypto = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : nc && typeof nc === "object" && "randomBytes" in nc ? nc : void 0;
699
+
700
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/utils.js
701
+ var createView2 = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
702
+ var rotr = (word, shift) => word << 32 - shift | word >>> shift;
703
+ function utf8ToBytes2(str) {
704
+ if (typeof str !== "string")
705
+ throw new Error("utf8ToBytes expected string, got " + typeof str);
706
+ return new Uint8Array(new TextEncoder().encode(str));
707
+ }
708
+ function toBytes2(data) {
709
+ if (typeof data === "string")
710
+ data = utf8ToBytes2(data);
711
+ abytes2(data);
712
+ return data;
713
+ }
714
+ var Hash = class {
715
+ // Safe version that clones internal state
716
+ clone() {
717
+ return this._cloneInto();
718
+ }
719
+ };
720
+ function wrapConstructor(hashCons) {
721
+ const hashC = (msg) => hashCons().update(toBytes2(msg)).digest();
722
+ const tmp = hashCons();
723
+ hashC.outputLen = tmp.outputLen;
724
+ hashC.blockLen = tmp.blockLen;
725
+ hashC.create = () => hashCons();
726
+ return hashC;
727
+ }
728
+ function randomBytes(bytesLength = 32) {
729
+ if (crypto && typeof crypto.getRandomValues === "function") {
730
+ return crypto.getRandomValues(new Uint8Array(bytesLength));
731
+ }
732
+ if (crypto && typeof crypto.randomBytes === "function") {
733
+ return crypto.randomBytes(bytesLength);
734
+ }
735
+ throw new Error("crypto.getRandomValues must be defined");
736
+ }
737
+
738
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/_md.js
739
+ function setBigUint642(view, byteOffset, value, isLE2) {
740
+ if (typeof view.setBigUint64 === "function")
741
+ return view.setBigUint64(byteOffset, value, isLE2);
742
+ const _32n = BigInt(32);
743
+ const _u32_max = BigInt(4294967295);
744
+ const wh = Number(value >> _32n & _u32_max);
745
+ const wl = Number(value & _u32_max);
746
+ const h = isLE2 ? 4 : 0;
747
+ const l = isLE2 ? 0 : 4;
748
+ view.setUint32(byteOffset + h, wh, isLE2);
749
+ view.setUint32(byteOffset + l, wl, isLE2);
750
+ }
751
+ var Chi = (a, b, c) => a & b ^ ~a & c;
752
+ var Maj = (a, b, c) => a & b ^ a & c ^ b & c;
753
+ var HashMD = class extends Hash {
754
+ constructor(blockLen, outputLen, padOffset, isLE2) {
755
+ super();
756
+ this.blockLen = blockLen;
757
+ this.outputLen = outputLen;
758
+ this.padOffset = padOffset;
759
+ this.isLE = isLE2;
760
+ this.finished = false;
761
+ this.length = 0;
762
+ this.pos = 0;
763
+ this.destroyed = false;
764
+ this.buffer = new Uint8Array(blockLen);
765
+ this.view = createView2(this.buffer);
766
+ }
767
+ update(data) {
768
+ aexists2(this);
769
+ const { view, buffer, blockLen } = this;
770
+ data = toBytes2(data);
771
+ const len = data.length;
772
+ for (let pos = 0; pos < len; ) {
773
+ const take = Math.min(blockLen - this.pos, len - pos);
774
+ if (take === blockLen) {
775
+ const dataView = createView2(data);
776
+ for (; blockLen <= len - pos; pos += blockLen)
777
+ this.process(dataView, pos);
778
+ continue;
779
+ }
780
+ buffer.set(data.subarray(pos, pos + take), this.pos);
781
+ this.pos += take;
782
+ pos += take;
783
+ if (this.pos === blockLen) {
784
+ this.process(view, 0);
785
+ this.pos = 0;
786
+ }
787
+ }
788
+ this.length += data.length;
789
+ this.roundClean();
790
+ return this;
791
+ }
792
+ digestInto(out) {
793
+ aexists2(this);
794
+ aoutput2(out, this);
795
+ this.finished = true;
796
+ const { buffer, view, blockLen, isLE: isLE2 } = this;
797
+ let { pos } = this;
798
+ buffer[pos++] = 128;
799
+ this.buffer.subarray(pos).fill(0);
800
+ if (this.padOffset > blockLen - pos) {
801
+ this.process(view, 0);
802
+ pos = 0;
803
+ }
804
+ for (let i = pos; i < blockLen; i++)
805
+ buffer[i] = 0;
806
+ setBigUint642(view, blockLen - 8, BigInt(this.length * 8), isLE2);
807
+ this.process(view, 0);
808
+ const oview = createView2(out);
809
+ const len = this.outputLen;
810
+ if (len % 4)
811
+ throw new Error("_sha2: outputLen should be aligned to 32bit");
812
+ const outLen = len / 4;
813
+ const state = this.get();
814
+ if (outLen > state.length)
815
+ throw new Error("_sha2: outputLen bigger than state");
816
+ for (let i = 0; i < outLen; i++)
817
+ oview.setUint32(4 * i, state[i], isLE2);
818
+ }
819
+ digest() {
820
+ const { buffer, outputLen } = this;
821
+ this.digestInto(buffer);
822
+ const res = buffer.slice(0, outputLen);
823
+ this.destroy();
824
+ return res;
825
+ }
826
+ _cloneInto(to) {
827
+ to || (to = new this.constructor());
828
+ to.set(...this.get());
829
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
830
+ to.length = length;
831
+ to.pos = pos;
832
+ to.finished = finished;
833
+ to.destroyed = destroyed;
834
+ if (length % blockLen)
835
+ to.buffer.set(buffer);
836
+ return to;
837
+ }
838
+ };
839
+
840
+ // ../../node_modules/.bun/@noble+curves@1.8.0/node_modules/@noble/curves/esm/abstract/utils.js
841
+ var _0n = /* @__PURE__ */ BigInt(0);
842
+ function isBytes3(a) {
843
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
844
+ }
845
+ function abytes3(item) {
846
+ if (!isBytes3(item))
847
+ throw new Error("Uint8Array expected");
848
+ }
849
+ var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
850
+ function bytesToHex(bytes) {
851
+ abytes3(bytes);
852
+ let hex = "";
853
+ for (let i = 0; i < bytes.length; i++) {
854
+ hex += hexes[bytes[i]];
855
+ }
856
+ return hex;
857
+ }
858
+ function hexToNumber(hex) {
859
+ if (typeof hex !== "string")
860
+ throw new Error("hex string expected, got " + typeof hex);
861
+ return hex === "" ? _0n : BigInt("0x" + hex);
862
+ }
863
+ var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
864
+ function asciiToBase16(ch) {
865
+ if (ch >= asciis._0 && ch <= asciis._9)
866
+ return ch - asciis._0;
867
+ if (ch >= asciis.A && ch <= asciis.F)
868
+ return ch - (asciis.A - 10);
869
+ if (ch >= asciis.a && ch <= asciis.f)
870
+ return ch - (asciis.a - 10);
871
+ return;
872
+ }
873
+ function hexToBytes(hex) {
874
+ if (typeof hex !== "string")
875
+ throw new Error("hex string expected, got " + typeof hex);
876
+ const hl = hex.length;
877
+ const al = hl / 2;
878
+ if (hl % 2)
879
+ throw new Error("hex string expected, got unpadded hex of length " + hl);
880
+ const array = new Uint8Array(al);
881
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
882
+ const n1 = asciiToBase16(hex.charCodeAt(hi));
883
+ const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
884
+ if (n1 === void 0 || n2 === void 0) {
885
+ const char = hex[hi] + hex[hi + 1];
886
+ throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
887
+ }
888
+ array[ai] = n1 * 16 + n2;
889
+ }
890
+ return array;
891
+ }
892
+ function bytesToNumberLE(bytes) {
893
+ abytes3(bytes);
894
+ return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
895
+ }
896
+ function numberToBytesBE(n, len) {
897
+ return hexToBytes(n.toString(16).padStart(len * 2, "0"));
898
+ }
899
+ function numberToBytesLE(n, len) {
900
+ return numberToBytesBE(n, len).reverse();
901
+ }
902
+ function ensureBytes(title, hex, expectedLength) {
903
+ let res;
904
+ if (typeof hex === "string") {
905
+ try {
906
+ res = hexToBytes(hex);
907
+ } catch (e) {
908
+ throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
909
+ }
910
+ } else if (isBytes3(hex)) {
911
+ res = Uint8Array.from(hex);
912
+ } else {
913
+ throw new Error(title + " must be hex string or Uint8Array");
914
+ }
915
+ const len = res.length;
916
+ if (typeof expectedLength === "number" && len !== expectedLength)
917
+ throw new Error(title + " of length " + expectedLength + " expected, got " + len);
918
+ return res;
919
+ }
920
+ var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
921
+ function inRange(n, min, max) {
922
+ return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
923
+ }
924
+ function aInRange(title, n, min, max) {
925
+ if (!inRange(n, min, max))
926
+ throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
927
+ }
928
+ var validatorFns = {
929
+ bigint: (val) => typeof val === "bigint",
930
+ function: (val) => typeof val === "function",
931
+ boolean: (val) => typeof val === "boolean",
932
+ string: (val) => typeof val === "string",
933
+ stringOrUint8Array: (val) => typeof val === "string" || isBytes3(val),
934
+ isSafeInteger: (val) => Number.isSafeInteger(val),
935
+ array: (val) => Array.isArray(val),
936
+ field: (val, object) => object.Fp.isValid(val),
937
+ hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
938
+ };
939
+ function validateObject(object, validators, optValidators = {}) {
940
+ const checkField = (fieldName, type, isOptional) => {
941
+ const checkVal = validatorFns[type];
942
+ if (typeof checkVal !== "function")
943
+ throw new Error("invalid validator function");
944
+ const val = object[fieldName];
945
+ if (isOptional && val === void 0)
946
+ return;
947
+ if (!checkVal(val, object)) {
948
+ throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val);
949
+ }
950
+ };
951
+ for (const [fieldName, type] of Object.entries(validators))
952
+ checkField(fieldName, type, false);
953
+ for (const [fieldName, type] of Object.entries(optValidators))
954
+ checkField(fieldName, type, true);
955
+ return object;
956
+ }
957
+
958
+ // ../../node_modules/.bun/@noble+curves@1.8.0/node_modules/@noble/curves/esm/abstract/modular.js
959
+ var _0n2 = BigInt(0);
960
+ var _1n = BigInt(1);
961
+ function mod(a, b) {
962
+ const result = a % b;
963
+ return result >= _0n2 ? result : b + result;
964
+ }
965
+ function pow(num, power, modulo) {
966
+ if (power < _0n2)
967
+ throw new Error("invalid exponent, negatives unsupported");
968
+ if (modulo <= _0n2)
969
+ throw new Error("invalid modulus");
970
+ if (modulo === _1n)
971
+ return _0n2;
972
+ let res = _1n;
973
+ while (power > _0n2) {
974
+ if (power & _1n)
975
+ res = res * num % modulo;
976
+ num = num * num % modulo;
977
+ power >>= _1n;
978
+ }
979
+ return res;
980
+ }
981
+ function pow2(x, power, modulo) {
982
+ let res = x;
983
+ while (power-- > _0n2) {
984
+ res *= res;
985
+ res %= modulo;
986
+ }
987
+ return res;
988
+ }
989
+
990
+ // ../../node_modules/.bun/@noble+curves@1.8.0/node_modules/@noble/curves/esm/abstract/montgomery.js
991
+ var _0n3 = BigInt(0);
992
+ var _1n2 = BigInt(1);
993
+ function validateOpts(curve) {
994
+ validateObject(curve, {
995
+ a: "bigint"
996
+ }, {
997
+ montgomeryBits: "isSafeInteger",
998
+ nByteLength: "isSafeInteger",
999
+ adjustScalarBytes: "function",
1000
+ domain: "function",
1001
+ powPminus2: "function",
1002
+ Gu: "bigint"
1003
+ });
1004
+ return Object.freeze({ ...curve });
1005
+ }
1006
+ function montgomery(curveDef) {
1007
+ const CURVE = validateOpts(curveDef);
1008
+ const { P } = CURVE;
1009
+ const modP = (n) => mod(n, P);
1010
+ const montgomeryBits = CURVE.montgomeryBits;
1011
+ const montgomeryBytes = Math.ceil(montgomeryBits / 8);
1012
+ const fieldLen = CURVE.nByteLength;
1013
+ const adjustScalarBytes2 = CURVE.adjustScalarBytes || ((bytes) => bytes);
1014
+ const powPminus2 = CURVE.powPminus2 || ((x) => pow(x, P - BigInt(2), P));
1015
+ function cswap(swap, x_2, x_3) {
1016
+ const dummy = modP(swap * (x_2 - x_3));
1017
+ x_2 = modP(x_2 - dummy);
1018
+ x_3 = modP(x_3 + dummy);
1019
+ return [x_2, x_3];
1020
+ }
1021
+ const a24 = (CURVE.a - BigInt(2)) / BigInt(4);
1022
+ function montgomeryLadder(u, scalar) {
1023
+ aInRange("u", u, _0n3, P);
1024
+ aInRange("scalar", scalar, _0n3, P);
1025
+ const k = scalar;
1026
+ const x_1 = u;
1027
+ let x_2 = _1n2;
1028
+ let z_2 = _0n3;
1029
+ let x_3 = u;
1030
+ let z_3 = _1n2;
1031
+ let swap = _0n3;
1032
+ let sw;
1033
+ for (let t = BigInt(montgomeryBits - 1); t >= _0n3; t--) {
1034
+ const k_t = k >> t & _1n2;
1035
+ swap ^= k_t;
1036
+ sw = cswap(swap, x_2, x_3);
1037
+ x_2 = sw[0];
1038
+ x_3 = sw[1];
1039
+ sw = cswap(swap, z_2, z_3);
1040
+ z_2 = sw[0];
1041
+ z_3 = sw[1];
1042
+ swap = k_t;
1043
+ const A = x_2 + z_2;
1044
+ const AA = modP(A * A);
1045
+ const B = x_2 - z_2;
1046
+ const BB = modP(B * B);
1047
+ const E = AA - BB;
1048
+ const C = x_3 + z_3;
1049
+ const D = x_3 - z_3;
1050
+ const DA = modP(D * A);
1051
+ const CB = modP(C * B);
1052
+ const dacb = DA + CB;
1053
+ const da_cb = DA - CB;
1054
+ x_3 = modP(dacb * dacb);
1055
+ z_3 = modP(x_1 * modP(da_cb * da_cb));
1056
+ x_2 = modP(AA * BB);
1057
+ z_2 = modP(E * (AA + modP(a24 * E)));
1058
+ }
1059
+ sw = cswap(swap, x_2, x_3);
1060
+ x_2 = sw[0];
1061
+ x_3 = sw[1];
1062
+ sw = cswap(swap, z_2, z_3);
1063
+ z_2 = sw[0];
1064
+ z_3 = sw[1];
1065
+ const z2 = powPminus2(z_2);
1066
+ return modP(x_2 * z2);
1067
+ }
1068
+ function encodeUCoordinate(u) {
1069
+ return numberToBytesLE(modP(u), montgomeryBytes);
1070
+ }
1071
+ function decodeUCoordinate(uEnc) {
1072
+ const u = ensureBytes("u coordinate", uEnc, montgomeryBytes);
1073
+ if (fieldLen === 32)
1074
+ u[31] &= 127;
1075
+ return bytesToNumberLE(u);
1076
+ }
1077
+ function decodeScalar(n) {
1078
+ const bytes = ensureBytes("scalar", n);
1079
+ const len = bytes.length;
1080
+ if (len !== montgomeryBytes && len !== fieldLen) {
1081
+ let valid = "" + montgomeryBytes + " or " + fieldLen;
1082
+ throw new Error("invalid scalar, expected " + valid + " bytes, got " + len);
1083
+ }
1084
+ return bytesToNumberLE(adjustScalarBytes2(bytes));
1085
+ }
1086
+ function scalarMult(scalar, u) {
1087
+ const pointU = decodeUCoordinate(u);
1088
+ const _scalar = decodeScalar(scalar);
1089
+ const pu = montgomeryLadder(pointU, _scalar);
1090
+ if (pu === _0n3)
1091
+ throw new Error("invalid private or public key received");
1092
+ return encodeUCoordinate(pu);
1093
+ }
1094
+ const GuBytes = encodeUCoordinate(CURVE.Gu);
1095
+ function scalarMultBase(scalar) {
1096
+ return scalarMult(scalar, GuBytes);
1097
+ }
1098
+ return {
1099
+ scalarMult,
1100
+ scalarMultBase,
1101
+ getSharedSecret: (privateKey, publicKey) => scalarMult(privateKey, publicKey),
1102
+ getPublicKey: (privateKey) => scalarMultBase(privateKey),
1103
+ utils: { randomPrivateKey: () => CURVE.randomBytes(CURVE.nByteLength) },
1104
+ GuBytes
1105
+ };
1106
+ }
1107
+
1108
+ // ../../node_modules/.bun/@noble+curves@1.8.0/node_modules/@noble/curves/esm/ed25519.js
1109
+ var ED25519_P = BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819949");
1110
+ var _0n4 = BigInt(0);
1111
+ var _1n3 = BigInt(1);
1112
+ var _2n = BigInt(2);
1113
+ var _3n = BigInt(3);
1114
+ var _5n = BigInt(5);
1115
+ var _8n = BigInt(8);
1116
+ function ed25519_pow_2_252_3(x) {
1117
+ const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
1118
+ const P = ED25519_P;
1119
+ const x2 = x * x % P;
1120
+ const b2 = x2 * x % P;
1121
+ const b4 = pow2(b2, _2n, P) * b2 % P;
1122
+ const b5 = pow2(b4, _1n3, P) * x % P;
1123
+ const b10 = pow2(b5, _5n, P) * b5 % P;
1124
+ const b20 = pow2(b10, _10n, P) * b10 % P;
1125
+ const b40 = pow2(b20, _20n, P) * b20 % P;
1126
+ const b80 = pow2(b40, _40n, P) * b40 % P;
1127
+ const b160 = pow2(b80, _80n, P) * b80 % P;
1128
+ const b240 = pow2(b160, _80n, P) * b80 % P;
1129
+ const b250 = pow2(b240, _10n, P) * b10 % P;
1130
+ const pow_p_5_8 = pow2(b250, _2n, P) * x % P;
1131
+ return { pow_p_5_8, b2 };
1132
+ }
1133
+ function adjustScalarBytes(bytes) {
1134
+ bytes[0] &= 248;
1135
+ bytes[31] &= 127;
1136
+ bytes[31] |= 64;
1137
+ return bytes;
1138
+ }
1139
+ var x25519 = /* @__PURE__ */ (() => montgomery({
1140
+ P: ED25519_P,
1141
+ a: BigInt(486662),
1142
+ montgomeryBits: 255,
1143
+ // n is 253 bits
1144
+ nByteLength: 32,
1145
+ Gu: BigInt(9),
1146
+ powPminus2: (x) => {
1147
+ const P = ED25519_P;
1148
+ const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
1149
+ return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
1150
+ },
1151
+ adjustScalarBytes,
1152
+ randomBytes
1153
+ }))();
1154
+
1155
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/hmac.js
1156
+ var HMAC = class extends Hash {
1157
+ constructor(hash, _key) {
1158
+ super();
1159
+ this.finished = false;
1160
+ this.destroyed = false;
1161
+ ahash(hash);
1162
+ const key = toBytes2(_key);
1163
+ this.iHash = hash.create();
1164
+ if (typeof this.iHash.update !== "function")
1165
+ throw new Error("Expected instance of class which extends utils.Hash");
1166
+ this.blockLen = this.iHash.blockLen;
1167
+ this.outputLen = this.iHash.outputLen;
1168
+ const blockLen = this.blockLen;
1169
+ const pad = new Uint8Array(blockLen);
1170
+ pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
1171
+ for (let i = 0; i < pad.length; i++)
1172
+ pad[i] ^= 54;
1173
+ this.iHash.update(pad);
1174
+ this.oHash = hash.create();
1175
+ for (let i = 0; i < pad.length; i++)
1176
+ pad[i] ^= 54 ^ 92;
1177
+ this.oHash.update(pad);
1178
+ pad.fill(0);
1179
+ }
1180
+ update(buf) {
1181
+ aexists2(this);
1182
+ this.iHash.update(buf);
1183
+ return this;
1184
+ }
1185
+ digestInto(out) {
1186
+ aexists2(this);
1187
+ abytes2(out, this.outputLen);
1188
+ this.finished = true;
1189
+ this.iHash.digestInto(out);
1190
+ this.oHash.update(out);
1191
+ this.oHash.digestInto(out);
1192
+ this.destroy();
1193
+ }
1194
+ digest() {
1195
+ const out = new Uint8Array(this.oHash.outputLen);
1196
+ this.digestInto(out);
1197
+ return out;
1198
+ }
1199
+ _cloneInto(to) {
1200
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
1201
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
1202
+ to = to;
1203
+ to.finished = finished;
1204
+ to.destroyed = destroyed;
1205
+ to.blockLen = blockLen;
1206
+ to.outputLen = outputLen;
1207
+ to.oHash = oHash._cloneInto(to.oHash);
1208
+ to.iHash = iHash._cloneInto(to.iHash);
1209
+ return to;
1210
+ }
1211
+ destroy() {
1212
+ this.destroyed = true;
1213
+ this.oHash.destroy();
1214
+ this.iHash.destroy();
1215
+ }
1216
+ };
1217
+ var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
1218
+ hmac.create = (hash, key) => new HMAC(hash, key);
1219
+
1220
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/hkdf.js
1221
+ function extract(hash, ikm, salt) {
1222
+ ahash(hash);
1223
+ if (salt === void 0)
1224
+ salt = new Uint8Array(hash.outputLen);
1225
+ return hmac(hash, toBytes2(salt), toBytes2(ikm));
1226
+ }
1227
+ var HKDF_COUNTER = /* @__PURE__ */ new Uint8Array([0]);
1228
+ var EMPTY_BUFFER = /* @__PURE__ */ new Uint8Array();
1229
+ function expand(hash, prk, info, length = 32) {
1230
+ ahash(hash);
1231
+ anumber(length);
1232
+ if (length > 255 * hash.outputLen)
1233
+ throw new Error("Length should be <= 255*HashLen");
1234
+ const blocks = Math.ceil(length / hash.outputLen);
1235
+ if (info === void 0)
1236
+ info = EMPTY_BUFFER;
1237
+ const okm = new Uint8Array(blocks * hash.outputLen);
1238
+ const HMAC2 = hmac.create(hash, prk);
1239
+ const HMACTmp = HMAC2._cloneInto();
1240
+ const T = new Uint8Array(HMAC2.outputLen);
1241
+ for (let counter = 0; counter < blocks; counter++) {
1242
+ HKDF_COUNTER[0] = counter + 1;
1243
+ HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T).update(info).update(HKDF_COUNTER).digestInto(T);
1244
+ okm.set(T, hash.outputLen * counter);
1245
+ HMAC2._cloneInto(HMACTmp);
1246
+ }
1247
+ HMAC2.destroy();
1248
+ HMACTmp.destroy();
1249
+ T.fill(0);
1250
+ HKDF_COUNTER.fill(0);
1251
+ return okm.slice(0, length);
1252
+ }
1253
+ var hkdf = (hash, ikm, salt, info, length) => expand(hash, extract(hash, ikm, salt), info, length);
1254
+
1255
+ // ../../node_modules/.bun/@noble+hashes@1.7.0/node_modules/@noble/hashes/esm/sha256.js
1256
+ var SHA256_K = /* @__PURE__ */ new Uint32Array([
1257
+ 1116352408,
1258
+ 1899447441,
1259
+ 3049323471,
1260
+ 3921009573,
1261
+ 961987163,
1262
+ 1508970993,
1263
+ 2453635748,
1264
+ 2870763221,
1265
+ 3624381080,
1266
+ 310598401,
1267
+ 607225278,
1268
+ 1426881987,
1269
+ 1925078388,
1270
+ 2162078206,
1271
+ 2614888103,
1272
+ 3248222580,
1273
+ 3835390401,
1274
+ 4022224774,
1275
+ 264347078,
1276
+ 604807628,
1277
+ 770255983,
1278
+ 1249150122,
1279
+ 1555081692,
1280
+ 1996064986,
1281
+ 2554220882,
1282
+ 2821834349,
1283
+ 2952996808,
1284
+ 3210313671,
1285
+ 3336571891,
1286
+ 3584528711,
1287
+ 113926993,
1288
+ 338241895,
1289
+ 666307205,
1290
+ 773529912,
1291
+ 1294757372,
1292
+ 1396182291,
1293
+ 1695183700,
1294
+ 1986661051,
1295
+ 2177026350,
1296
+ 2456956037,
1297
+ 2730485921,
1298
+ 2820302411,
1299
+ 3259730800,
1300
+ 3345764771,
1301
+ 3516065817,
1302
+ 3600352804,
1303
+ 4094571909,
1304
+ 275423344,
1305
+ 430227734,
1306
+ 506948616,
1307
+ 659060556,
1308
+ 883997877,
1309
+ 958139571,
1310
+ 1322822218,
1311
+ 1537002063,
1312
+ 1747873779,
1313
+ 1955562222,
1314
+ 2024104815,
1315
+ 2227730452,
1316
+ 2361852424,
1317
+ 2428436474,
1318
+ 2756734187,
1319
+ 3204031479,
1320
+ 3329325298
1321
+ ]);
1322
+ var SHA256_IV = /* @__PURE__ */ new Uint32Array([
1323
+ 1779033703,
1324
+ 3144134277,
1325
+ 1013904242,
1326
+ 2773480762,
1327
+ 1359893119,
1328
+ 2600822924,
1329
+ 528734635,
1330
+ 1541459225
1331
+ ]);
1332
+ var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
1333
+ var SHA256 = class extends HashMD {
1334
+ constructor() {
1335
+ super(64, 32, 8, false);
1336
+ this.A = SHA256_IV[0] | 0;
1337
+ this.B = SHA256_IV[1] | 0;
1338
+ this.C = SHA256_IV[2] | 0;
1339
+ this.D = SHA256_IV[3] | 0;
1340
+ this.E = SHA256_IV[4] | 0;
1341
+ this.F = SHA256_IV[5] | 0;
1342
+ this.G = SHA256_IV[6] | 0;
1343
+ this.H = SHA256_IV[7] | 0;
1344
+ }
1345
+ get() {
1346
+ const { A, B, C, D, E, F, G, H } = this;
1347
+ return [A, B, C, D, E, F, G, H];
1348
+ }
1349
+ // prettier-ignore
1350
+ set(A, B, C, D, E, F, G, H) {
1351
+ this.A = A | 0;
1352
+ this.B = B | 0;
1353
+ this.C = C | 0;
1354
+ this.D = D | 0;
1355
+ this.E = E | 0;
1356
+ this.F = F | 0;
1357
+ this.G = G | 0;
1358
+ this.H = H | 0;
1359
+ }
1360
+ process(view, offset) {
1361
+ for (let i = 0; i < 16; i++, offset += 4)
1362
+ SHA256_W[i] = view.getUint32(offset, false);
1363
+ for (let i = 16; i < 64; i++) {
1364
+ const W15 = SHA256_W[i - 15];
1365
+ const W2 = SHA256_W[i - 2];
1366
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
1367
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
1368
+ SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
1369
+ }
1370
+ let { A, B, C, D, E, F, G, H } = this;
1371
+ for (let i = 0; i < 64; i++) {
1372
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
1373
+ const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
1374
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
1375
+ const T2 = sigma0 + Maj(A, B, C) | 0;
1376
+ H = G;
1377
+ G = F;
1378
+ F = E;
1379
+ E = D + T1 | 0;
1380
+ D = C;
1381
+ C = B;
1382
+ B = A;
1383
+ A = T1 + T2 | 0;
1384
+ }
1385
+ A = A + this.A | 0;
1386
+ B = B + this.B | 0;
1387
+ C = C + this.C | 0;
1388
+ D = D + this.D | 0;
1389
+ E = E + this.E | 0;
1390
+ F = F + this.F | 0;
1391
+ G = G + this.G | 0;
1392
+ H = H + this.H | 0;
1393
+ this.set(A, B, C, D, E, F, G, H);
1394
+ }
1395
+ roundClean() {
1396
+ SHA256_W.fill(0);
1397
+ }
1398
+ destroy() {
1399
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
1400
+ this.buffer.fill(0);
1401
+ }
1402
+ };
1403
+ var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
1404
+
1405
+ // src/crypto/decrypt.ts
1406
+ function hexToBytes2(hex) {
1407
+ const cleanHex = hex.replace(/^0x|[\s-]/g, "");
1408
+ if (!/^[0-9a-fA-F]+$/.test(cleanHex) || cleanHex.length % 2 !== 0) {
1409
+ throw new Error("Invalid hex string");
1410
+ }
1411
+ return new Uint8Array(cleanHex.match(/.{1,2}/g)?.map((byte) => Number.parseInt(byte, 16)) ?? []);
1412
+ }
1413
+ function decryptCredentials(encryptedPayload, privateKeyHex) {
1414
+ const decoded = atob(encryptedPayload);
1415
+ const data = JSON.parse(decoded);
1416
+ const recipientPrivateKey = hexToBytes2(privateKeyHex);
1417
+ const ephemeralPublicKey = hexToBytes2(data.ephemeralPublicKey);
1418
+ const iv = hexToBytes2(data.iv);
1419
+ const ciphertext = hexToBytes2(data.ciphertext);
1420
+ const sharedSecret = x25519.getSharedSecret(recipientPrivateKey, ephemeralPublicKey);
1421
+ const encryptionKey = hkdf(sha256, sharedSecret, void 0, void 0, 32);
1422
+ const cipher = gcm(encryptionKey, iv);
1423
+ const plainTextBytes = cipher.decrypt(ciphertext);
1424
+ const plainText = new TextDecoder().decode(plainTextBytes);
1425
+ return JSON.parse(plainText);
1426
+ }
1427
+
1428
+ // src/crypto/key-exchange.ts
1429
+ function generateKeyPair() {
1430
+ const privateKey = x25519.utils.randomPrivateKey();
1431
+ const publicKey = x25519.getPublicKey(privateKey);
1432
+ return {
1433
+ privateKey: bytesToHex(privateKey),
1434
+ publicKey: bytesToHex(publicKey)
1435
+ };
1436
+ }
1437
+
1438
+ // src/frames/frame-orchestrator.ts
1439
+ import {
1440
+ HANDSHAKE_ACK_KIND,
1441
+ HANDSHAKE_REQUEST_KIND
1442
+ } from "@moonpay/platform-protocol";
1443
+ function createFrameOrchestrator(options) {
1444
+ const { transport, channelId, url, container, hidden, handshakeTimeout = 15e3 } = options;
1445
+ return new Promise((resolve, reject) => {
1446
+ const messageHandlers = [];
1447
+ let handshakeComplete = false;
1448
+ const timer = setTimeout(() => {
1449
+ if (!handshakeComplete) {
1450
+ transport.dispose();
1451
+ reject(new Error("Frame handshake timed out"));
1452
+ }
1453
+ }, handshakeTimeout);
1454
+ const unsubHandshake = transport.onMessage((msg) => {
1455
+ if (msg.meta?.channelId !== channelId)
1456
+ return;
1457
+ if (!handshakeComplete && msg.kind === HANDSHAKE_REQUEST_KIND) {
1458
+ handshakeComplete = true;
1459
+ clearTimeout(timer);
1460
+ transport.sendMessage({
1461
+ version: 2,
1462
+ meta: { channelId },
1463
+ kind: HANDSHAKE_ACK_KIND
1464
+ });
1465
+ resolve(handle);
1466
+ return;
1467
+ }
1468
+ if (handshakeComplete) {
1469
+ for (const handler of messageHandlers) {
1470
+ handler(msg);
1471
+ }
1472
+ }
1473
+ });
1474
+ const handle = {
1475
+ sendMessage(message) {
1476
+ transport.sendMessage(message);
1477
+ },
1478
+ onMessage(handler) {
1479
+ messageHandlers.push(handler);
1480
+ return () => {
1481
+ const idx = messageHandlers.indexOf(handler);
1482
+ if (idx >= 0)
1483
+ messageHandlers.splice(idx, 1);
1484
+ };
1485
+ },
1486
+ dispose() {
1487
+ clearTimeout(timer);
1488
+ unsubHandshake();
1489
+ messageHandlers.length = 0;
1490
+ transport.dispose();
1491
+ }
1492
+ };
1493
+ transport.create(url, { container, hidden });
1494
+ });
1495
+ }
1496
+
1497
+ // src/frames/url-builder.ts
1498
+ var FRAME_PATHS = {
1499
+ connect: "/platform/v1/connect",
1500
+ checkConnection: "/platform/v1/check-connection",
1501
+ applePay: "/platform/v1/apple-pay",
1502
+ googlePay: "/platform/v1/google-pay",
1503
+ widget: "/platform/v1/widget",
1504
+ buy: "/platform/v1/buy",
1505
+ buyButton: "/platform/v1/buy-button",
1506
+ challenge: "/platform/v1/challenge",
1507
+ addCard: "/platform/v1/add-card",
1508
+ reset: "/platform/v1/reset"
1509
+ };
1510
+ var DEFAULT_FRAME_BASE_URL = "https://platform.moonpay.com";
1511
+ function stringifyQueryValue(value) {
1512
+ if (value == null)
1513
+ return null;
1514
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1515
+ return String(value);
1516
+ }
1517
+ if (typeof value === "object") {
1518
+ return JSON.stringify(value);
1519
+ }
1520
+ return null;
1521
+ }
1522
+ function buildFrameUrl(path, query = {}, options = {}) {
1523
+ const base = options.frameBaseUrl ?? DEFAULT_FRAME_BASE_URL;
1524
+ const url = new URL(`${base}${path}`);
1525
+ for (const [key, value] of Object.entries(query)) {
1526
+ const encoded = stringifyQueryValue(value);
1527
+ if (encoded != null) {
1528
+ url.searchParams.set(key, encoded);
1529
+ }
1530
+ }
1531
+ return url.href;
1532
+ }
1533
+ export {
1534
+ FRAME_PATHS,
1535
+ apiFetch,
1536
+ buildFrameUrl,
1537
+ createClientContext,
1538
+ createClientCore,
1539
+ createFrameOrchestrator,
1540
+ decryptCredentials,
1541
+ deletePaymentMethod,
1542
+ generateKeyPair,
1543
+ getPaymentMethods,
1544
+ getQuote,
1545
+ getTransaction,
1546
+ listTransactions
1547
+ };
1548
+ /*! Bundled license information:
1549
+
1550
+ @noble/ciphers/esm/utils.js:
1551
+ (*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
1552
+
1553
+ @noble/hashes/esm/utils.js:
1554
+ (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1555
+
1556
+ @noble/curves/esm/abstract/utils.js:
1557
+ (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1558
+
1559
+ @noble/curves/esm/abstract/modular.js:
1560
+ (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1561
+
1562
+ @noble/curves/esm/abstract/montgomery.js:
1563
+ (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1564
+
1565
+ @noble/curves/esm/ed25519.js:
1566
+ (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
1567
+ */
1568
+ //# sourceMappingURL=index.js.map