digiid-ts 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,97 +1,2079 @@
1
- import { randomBytes as f } from "crypto";
2
- import * as m from "bitcoinjs-message";
3
- class e extends Error {
4
- constructor(r) {
5
- super(r), this.name = "DigiIDError";
1
+ var Te = Object.defineProperty;
2
+ var Me = (e, t, r) => t in e ? Te(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var m = (e, t, r) => Me(e, typeof t != "symbol" ? t + "" : t, r);
4
+ import { randomBytes as Ye } from "crypto";
5
+ class C extends Error {
6
+ constructor(t) {
7
+ super(t), this.name = "DigiIDError";
6
8
  }
7
9
  }
8
- async function U(t, r, i) {
9
- const c = `DigiByte Signed Message:
10
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
11
+ function Vt(e) {
12
+ return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array";
13
+ }
14
+ function ut(e, t = "") {
15
+ if (!Number.isSafeInteger(e) || e < 0) {
16
+ const r = t && `"${t}" `;
17
+ throw new Error(`${r}expected integer >= 0, got ${e}`);
18
+ }
19
+ }
20
+ function N(e, t, r = "") {
21
+ const n = Vt(e), s = e == null ? void 0 : e.length, i = t !== void 0;
22
+ if (!n || i && s !== t) {
23
+ const o = r && `"${r}" `, c = i ? ` of length ${t}` : "", f = n ? `length=${s}` : `type=${typeof e}`;
24
+ throw new Error(o + "expected Uint8Array" + c + ", got " + f);
25
+ }
26
+ return e;
27
+ }
28
+ function ae(e) {
29
+ if (typeof e != "function" || typeof e.create != "function")
30
+ throw new Error("Hash must wrapped by utils.createHasher");
31
+ ut(e.outputLen), ut(e.blockLen);
32
+ }
33
+ function Bt(e, t = !0) {
34
+ if (e.destroyed)
35
+ throw new Error("Hash instance has been destroyed");
36
+ if (t && e.finished)
37
+ throw new Error("Hash#digest() has already been called");
38
+ }
39
+ function je(e, t) {
40
+ N(e, void 0, "digestInto() output");
41
+ const r = t.outputLen;
42
+ if (e.length < r)
43
+ throw new Error('"digestInto() output" expected to be of length >=' + r);
44
+ }
45
+ function vt(...e) {
46
+ for (let t = 0; t < e.length; t++)
47
+ e[t].fill(0);
48
+ }
49
+ function Ot(e) {
50
+ return new DataView(e.buffer, e.byteOffset, e.byteLength);
51
+ }
52
+ function P(e, t) {
53
+ return e << 32 - t | e >>> t;
54
+ }
55
+ const ue = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", Ke = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0"));
56
+ function Lt(e) {
57
+ if (N(e), ue)
58
+ return e.toHex();
59
+ let t = "";
60
+ for (let r = 0; r < e.length; r++)
61
+ t += Ke[e[r]];
62
+ return t;
63
+ }
64
+ const J = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
65
+ function Wt(e) {
66
+ if (e >= J._0 && e <= J._9)
67
+ return e - J._0;
68
+ if (e >= J.A && e <= J.F)
69
+ return e - (J.A - 10);
70
+ if (e >= J.a && e <= J.f)
71
+ return e - (J.a - 10);
72
+ }
73
+ function Rt(e) {
74
+ if (typeof e != "string")
75
+ throw new Error("hex string expected, got " + typeof e);
76
+ if (ue)
77
+ return Uint8Array.fromHex(e);
78
+ const t = e.length, r = t / 2;
79
+ if (t % 2)
80
+ throw new Error("hex string expected, got unpadded hex of length " + t);
81
+ const n = new Uint8Array(r);
82
+ for (let s = 0, i = 0; s < r; s++, i += 2) {
83
+ const o = Wt(e.charCodeAt(i)), c = Wt(e.charCodeAt(i + 1));
84
+ if (o === void 0 || c === void 0) {
85
+ const f = e[i] + e[i + 1];
86
+ throw new Error('hex string expected, got non-hex character "' + f + '" at index ' + i);
87
+ }
88
+ n[s] = o * 16 + c;
89
+ }
90
+ return n;
91
+ }
92
+ function it(...e) {
93
+ let t = 0;
94
+ for (let n = 0; n < e.length; n++) {
95
+ const s = e[n];
96
+ N(s), t += s.length;
97
+ }
98
+ const r = new Uint8Array(t);
99
+ for (let n = 0, s = 0; n < e.length; n++) {
100
+ const i = e[n];
101
+ r.set(i, s), s += i.length;
102
+ }
103
+ return r;
104
+ }
105
+ function Ge(e, t = {}) {
106
+ const r = (s, i) => e(i).update(s).digest(), n = e(void 0);
107
+ return r.outputLen = n.outputLen, r.blockLen = n.blockLen, r.create = (s) => e(s), Object.assign(r, t), Object.freeze(r);
108
+ }
109
+ function le(e = 32) {
110
+ const t = typeof globalThis == "object" ? globalThis.crypto : null;
111
+ if (typeof (t == null ? void 0 : t.getRandomValues) != "function")
112
+ throw new Error("crypto.getRandomValues must be defined");
113
+ return t.getRandomValues(new Uint8Array(e));
114
+ }
115
+ const Xe = (e) => ({
116
+ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, e])
117
+ });
118
+ function ze(e, t, r) {
119
+ return e & t ^ ~e & r;
120
+ }
121
+ function Fe(e, t, r) {
122
+ return e & t ^ e & r ^ t & r;
123
+ }
124
+ let We = class {
125
+ constructor(t, r, n, s) {
126
+ m(this, "blockLen");
127
+ m(this, "outputLen");
128
+ m(this, "padOffset");
129
+ m(this, "isLE");
130
+ // For partial updates less than block size
131
+ m(this, "buffer");
132
+ m(this, "view");
133
+ m(this, "finished", !1);
134
+ m(this, "length", 0);
135
+ m(this, "pos", 0);
136
+ m(this, "destroyed", !1);
137
+ this.blockLen = t, this.outputLen = r, this.padOffset = n, this.isLE = s, this.buffer = new Uint8Array(t), this.view = Ot(this.buffer);
138
+ }
139
+ update(t) {
140
+ Bt(this), N(t);
141
+ const { view: r, buffer: n, blockLen: s } = this, i = t.length;
142
+ for (let o = 0; o < i; ) {
143
+ const c = Math.min(s - this.pos, i - o);
144
+ if (c === s) {
145
+ const f = Ot(t);
146
+ for (; s <= i - o; o += s)
147
+ this.process(f, o);
148
+ continue;
149
+ }
150
+ n.set(t.subarray(o, o + c), this.pos), this.pos += c, o += c, this.pos === s && (this.process(r, 0), this.pos = 0);
151
+ }
152
+ return this.length += t.length, this.roundClean(), this;
153
+ }
154
+ digestInto(t) {
155
+ Bt(this), je(t, this), this.finished = !0;
156
+ const { buffer: r, view: n, blockLen: s, isLE: i } = this;
157
+ let { pos: o } = this;
158
+ r[o++] = 128, vt(this.buffer.subarray(o)), this.padOffset > s - o && (this.process(n, 0), o = 0);
159
+ for (let d = o; d < s; d++)
160
+ r[d] = 0;
161
+ n.setBigUint64(s - 8, BigInt(this.length * 8), i), this.process(n, 0);
162
+ const c = Ot(t), f = this.outputLen;
163
+ if (f % 4)
164
+ throw new Error("_sha2: outputLen must be aligned to 32bit");
165
+ const a = f / 4, h = this.get();
166
+ if (a > h.length)
167
+ throw new Error("_sha2: outputLen bigger than state");
168
+ for (let d = 0; d < a; d++)
169
+ c.setUint32(4 * d, h[d], i);
170
+ }
171
+ digest() {
172
+ const { buffer: t, outputLen: r } = this;
173
+ this.digestInto(t);
174
+ const n = t.slice(0, r);
175
+ return this.destroy(), n;
176
+ }
177
+ _cloneInto(t) {
178
+ t || (t = new this.constructor()), t.set(...this.get());
179
+ const { blockLen: r, buffer: n, length: s, finished: i, destroyed: o, pos: c } = this;
180
+ return t.destroyed = o, t.finished = i, t.length = s, t.pos = c, s % r && t.buffer.set(n), t;
181
+ }
182
+ clone() {
183
+ return this._cloneInto();
184
+ }
185
+ };
186
+ const et = /* @__PURE__ */ Uint32Array.from([
187
+ 1779033703,
188
+ 3144134277,
189
+ 1013904242,
190
+ 2773480762,
191
+ 1359893119,
192
+ 2600822924,
193
+ 528734635,
194
+ 1541459225
195
+ ]), Pe = /* @__PURE__ */ Uint32Array.from([
196
+ 1116352408,
197
+ 1899447441,
198
+ 3049323471,
199
+ 3921009573,
200
+ 961987163,
201
+ 1508970993,
202
+ 2453635748,
203
+ 2870763221,
204
+ 3624381080,
205
+ 310598401,
206
+ 607225278,
207
+ 1426881987,
208
+ 1925078388,
209
+ 2162078206,
210
+ 2614888103,
211
+ 3248222580,
212
+ 3835390401,
213
+ 4022224774,
214
+ 264347078,
215
+ 604807628,
216
+ 770255983,
217
+ 1249150122,
218
+ 1555081692,
219
+ 1996064986,
220
+ 2554220882,
221
+ 2821834349,
222
+ 2952996808,
223
+ 3210313671,
224
+ 3336571891,
225
+ 3584528711,
226
+ 113926993,
227
+ 338241895,
228
+ 666307205,
229
+ 773529912,
230
+ 1294757372,
231
+ 1396182291,
232
+ 1695183700,
233
+ 1986661051,
234
+ 2177026350,
235
+ 2456956037,
236
+ 2730485921,
237
+ 2820302411,
238
+ 3259730800,
239
+ 3345764771,
240
+ 3516065817,
241
+ 3600352804,
242
+ 4094571909,
243
+ 275423344,
244
+ 430227734,
245
+ 506948616,
246
+ 659060556,
247
+ 883997877,
248
+ 958139571,
249
+ 1322822218,
250
+ 1537002063,
251
+ 1747873779,
252
+ 1955562222,
253
+ 2024104815,
254
+ 2227730452,
255
+ 2361852424,
256
+ 2428436474,
257
+ 2756734187,
258
+ 3204031479,
259
+ 3329325298
260
+ ]), nt = /* @__PURE__ */ new Uint32Array(64);
261
+ class Qe extends We {
262
+ constructor(t) {
263
+ super(64, t, 8, !1);
264
+ }
265
+ get() {
266
+ const { A: t, B: r, C: n, D: s, E: i, F: o, G: c, H: f } = this;
267
+ return [t, r, n, s, i, o, c, f];
268
+ }
269
+ // prettier-ignore
270
+ set(t, r, n, s, i, o, c, f) {
271
+ this.A = t | 0, this.B = r | 0, this.C = n | 0, this.D = s | 0, this.E = i | 0, this.F = o | 0, this.G = c | 0, this.H = f | 0;
272
+ }
273
+ process(t, r) {
274
+ for (let d = 0; d < 16; d++, r += 4)
275
+ nt[d] = t.getUint32(r, !1);
276
+ for (let d = 16; d < 64; d++) {
277
+ const y = nt[d - 15], g = nt[d - 2], x = P(y, 7) ^ P(y, 18) ^ y >>> 3, U = P(g, 17) ^ P(g, 19) ^ g >>> 10;
278
+ nt[d] = U + nt[d - 7] + x + nt[d - 16] | 0;
279
+ }
280
+ let { A: n, B: s, C: i, D: o, E: c, F: f, G: a, H: h } = this;
281
+ for (let d = 0; d < 64; d++) {
282
+ const y = P(c, 6) ^ P(c, 11) ^ P(c, 25), g = h + y + ze(c, f, a) + Pe[d] + nt[d] | 0, U = (P(n, 2) ^ P(n, 13) ^ P(n, 22)) + Fe(n, s, i) | 0;
283
+ h = a, a = f, f = c, c = o + g | 0, o = i, i = s, s = n, n = g + U | 0;
284
+ }
285
+ n = n + this.A | 0, s = s + this.B | 0, i = i + this.C | 0, o = o + this.D | 0, c = c + this.E | 0, f = f + this.F | 0, a = a + this.G | 0, h = h + this.H | 0, this.set(n, s, i, o, c, f, a, h);
286
+ }
287
+ roundClean() {
288
+ vt(nt);
289
+ }
290
+ destroy() {
291
+ this.set(0, 0, 0, 0, 0, 0, 0, 0), vt(this.buffer);
292
+ }
293
+ }
294
+ class Je extends Qe {
295
+ constructor() {
296
+ super(32);
297
+ // We cannot use array here since array allows indexing by variable
298
+ // which means optimizer/compiler cannot use registers.
299
+ m(this, "A", et[0] | 0);
300
+ m(this, "B", et[1] | 0);
301
+ m(this, "C", et[2] | 0);
302
+ m(this, "D", et[3] | 0);
303
+ m(this, "E", et[4] | 0);
304
+ m(this, "F", et[5] | 0);
305
+ m(this, "G", et[6] | 0);
306
+ m(this, "H", et[7] | 0);
307
+ }
308
+ }
309
+ const tn = /* @__PURE__ */ Ge(
310
+ () => new Je(),
311
+ /* @__PURE__ */ Xe(1)
312
+ );
313
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
314
+ const Tt = /* @__PURE__ */ BigInt(0), $t = /* @__PURE__ */ BigInt(1);
315
+ function At(e, t = "") {
316
+ if (typeof e != "boolean") {
317
+ const r = t && `"${t}" `;
318
+ throw new Error(r + "expected boolean, got type=" + typeof e);
319
+ }
320
+ return e;
321
+ }
322
+ function de(e) {
323
+ if (typeof e == "bigint") {
324
+ if (!Et(e))
325
+ throw new Error("positive bigint expected, got " + e);
326
+ } else
327
+ ut(e);
328
+ return e;
329
+ }
330
+ function pt(e) {
331
+ const t = de(e).toString(16);
332
+ return t.length & 1 ? "0" + t : t;
333
+ }
334
+ function he(e) {
335
+ if (typeof e != "string")
336
+ throw new Error("hex string expected, got " + typeof e);
337
+ return e === "" ? Tt : BigInt("0x" + e);
338
+ }
339
+ function Ut(e) {
340
+ return he(Lt(e));
341
+ }
342
+ function ge(e) {
343
+ return he(Lt(en(N(e)).reverse()));
344
+ }
345
+ function Mt(e, t) {
346
+ ut(t), e = de(e);
347
+ const r = Rt(e.toString(16).padStart(t * 2, "0"));
348
+ if (r.length !== t)
349
+ throw new Error("number too large");
350
+ return r;
351
+ }
352
+ function we(e, t) {
353
+ return Mt(e, t).reverse();
354
+ }
355
+ function en(e) {
356
+ return Uint8Array.from(e);
357
+ }
358
+ const Et = (e) => typeof e == "bigint" && Tt <= e;
359
+ function nn(e, t, r) {
360
+ return Et(e) && Et(t) && Et(r) && t <= e && e < r;
361
+ }
362
+ function rn(e, t, r, n) {
363
+ if (!nn(t, r, n))
364
+ throw new Error("expected valid " + e + ": " + r + " <= n < " + n + ", got " + t);
365
+ }
366
+ function sn(e) {
367
+ let t;
368
+ for (t = 0; e > Tt; e >>= $t, t += 1)
369
+ ;
370
+ return t;
371
+ }
372
+ const Yt = (e) => ($t << BigInt(e)) - $t;
373
+ function on(e, t, r) {
374
+ if (ut(e, "hashLen"), ut(t, "qByteLen"), typeof r != "function")
375
+ throw new Error("hmacFn must be a function");
376
+ const n = (I) => new Uint8Array(I), s = Uint8Array.of(), i = Uint8Array.of(0), o = Uint8Array.of(1), c = 1e3;
377
+ let f = n(e), a = n(e), h = 0;
378
+ const d = () => {
379
+ f.fill(1), a.fill(0), h = 0;
380
+ }, y = (...I) => r(a, it(f, ...I)), g = (I = s) => {
381
+ a = y(i, I), f = y(), I.length !== 0 && (a = y(o, I), f = y());
382
+ }, x = () => {
383
+ if (h++ >= c)
384
+ throw new Error("drbg: tried max amount of iterations");
385
+ let I = 0;
386
+ const O = [];
387
+ for (; I < t; ) {
388
+ f = y();
389
+ const j = f.slice();
390
+ O.push(j), I += f.length;
391
+ }
392
+ return it(...O);
393
+ };
394
+ return (I, O) => {
395
+ d(), g(I);
396
+ let j;
397
+ for (; !(j = O(x())); )
398
+ g();
399
+ return d(), j;
400
+ };
401
+ }
402
+ function jt(e, t = {}, r = {}) {
403
+ if (!e || typeof e != "object")
404
+ throw new Error("expected valid options object");
405
+ function n(i, o, c) {
406
+ const f = e[i];
407
+ if (c && f === void 0)
408
+ return;
409
+ const a = typeof f;
410
+ if (a !== o || f === null)
411
+ throw new Error(`param "${i}" is invalid: expected ${o}, got ${a}`);
412
+ }
413
+ const s = (i, o) => Object.entries(i).forEach(([c, f]) => n(c, f, o));
414
+ s(t, !1), s(r, !0);
415
+ }
416
+ function Pt(e) {
417
+ const t = /* @__PURE__ */ new WeakMap();
418
+ return (r, ...n) => {
419
+ const s = t.get(r);
420
+ if (s !== void 0)
421
+ return s;
422
+ const i = e(r, ...n);
423
+ return t.set(r, i), i;
424
+ };
425
+ }
426
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
427
+ const Y = /* @__PURE__ */ BigInt(0), M = /* @__PURE__ */ BigInt(1), ft = /* @__PURE__ */ BigInt(2), be = /* @__PURE__ */ BigInt(3), pe = /* @__PURE__ */ BigInt(4), ye = /* @__PURE__ */ BigInt(5), cn = /* @__PURE__ */ BigInt(7), xe = /* @__PURE__ */ BigInt(8), fn = /* @__PURE__ */ BigInt(9), me = /* @__PURE__ */ BigInt(16);
428
+ function F(e, t) {
429
+ const r = e % t;
430
+ return r >= Y ? r : t + r;
431
+ }
432
+ function X(e, t, r) {
433
+ let n = e;
434
+ for (; t-- > Y; )
435
+ n *= n, n %= r;
436
+ return n;
437
+ }
438
+ function Qt(e, t) {
439
+ if (e === Y)
440
+ throw new Error("invert: expected non-zero number");
441
+ if (t <= Y)
442
+ throw new Error("invert: expected positive modulus, got " + t);
443
+ let r = F(e, t), n = t, s = Y, i = M;
444
+ for (; r !== Y; ) {
445
+ const c = n / r, f = n % r, a = s - i * c;
446
+ n = r, r = f, s = i, i = a;
447
+ }
448
+ if (n !== M)
449
+ throw new Error("invert: does not exist");
450
+ return F(s, t);
451
+ }
452
+ function Kt(e, t, r) {
453
+ if (!e.eql(e.sqr(t), r))
454
+ throw new Error("Cannot find square root");
455
+ }
456
+ function Ee(e, t) {
457
+ const r = (e.ORDER + M) / pe, n = e.pow(t, r);
458
+ return Kt(e, n, t), n;
459
+ }
460
+ function an(e, t) {
461
+ const r = (e.ORDER - ye) / xe, n = e.mul(t, ft), s = e.pow(n, r), i = e.mul(t, s), o = e.mul(e.mul(i, ft), s), c = e.mul(i, e.sub(o, e.ONE));
462
+ return Kt(e, c, t), c;
463
+ }
464
+ function un(e) {
465
+ const t = St(e), r = Be(e), n = r(t, t.neg(t.ONE)), s = r(t, n), i = r(t, t.neg(n)), o = (e + cn) / me;
466
+ return (c, f) => {
467
+ let a = c.pow(f, o), h = c.mul(a, n);
468
+ const d = c.mul(a, s), y = c.mul(a, i), g = c.eql(c.sqr(h), f), x = c.eql(c.sqr(d), f);
469
+ a = c.cmov(a, h, g), h = c.cmov(y, d, x);
470
+ const U = c.eql(c.sqr(h), f), I = c.cmov(a, h, U);
471
+ return Kt(c, I, f), I;
472
+ };
473
+ }
474
+ function Be(e) {
475
+ if (e < be)
476
+ throw new Error("sqrt is not defined for small field");
477
+ let t = e - M, r = 0;
478
+ for (; t % ft === Y; )
479
+ t /= ft, r++;
480
+ let n = ft;
481
+ const s = St(e);
482
+ for (; Jt(s, n) === 1; )
483
+ if (n++ > 1e3)
484
+ throw new Error("Cannot find square root: probably non-prime P");
485
+ if (r === 1)
486
+ return Ee;
487
+ let i = s.pow(n, t);
488
+ const o = (t + M) / ft;
489
+ return function(f, a) {
490
+ if (f.is0(a))
491
+ return a;
492
+ if (Jt(f, a) !== 1)
493
+ throw new Error("Cannot find square root");
494
+ let h = r, d = f.mul(f.ONE, i), y = f.pow(a, t), g = f.pow(a, o);
495
+ for (; !f.eql(y, f.ONE); ) {
496
+ if (f.is0(y))
497
+ return f.ZERO;
498
+ let x = 1, U = f.sqr(y);
499
+ for (; !f.eql(U, f.ONE); )
500
+ if (x++, U = f.sqr(U), x === h)
501
+ throw new Error("Cannot find square root");
502
+ const I = M << BigInt(h - x - 1), O = f.pow(d, I);
503
+ h = x, d = f.sqr(O), y = f.mul(y, d), g = f.mul(g, O);
504
+ }
505
+ return g;
506
+ };
507
+ }
508
+ function ln(e) {
509
+ return e % pe === be ? Ee : e % xe === ye ? an : e % me === fn ? un(e) : Be(e);
510
+ }
511
+ const dn = [
512
+ "create",
513
+ "isValid",
514
+ "is0",
515
+ "neg",
516
+ "inv",
517
+ "sqrt",
518
+ "sqr",
519
+ "eql",
520
+ "add",
521
+ "sub",
522
+ "mul",
523
+ "pow",
524
+ "div",
525
+ "addN",
526
+ "subN",
527
+ "mulN",
528
+ "sqrN"
529
+ ];
530
+ function hn(e) {
531
+ const t = {
532
+ ORDER: "bigint",
533
+ BYTES: "number",
534
+ BITS: "number"
535
+ }, r = dn.reduce((n, s) => (n[s] = "function", n), t);
536
+ return jt(e, r), e;
537
+ }
538
+ function gn(e, t, r) {
539
+ if (r < Y)
540
+ throw new Error("invalid exponent, negatives unsupported");
541
+ if (r === Y)
542
+ return e.ONE;
543
+ if (r === M)
544
+ return t;
545
+ let n = e.ONE, s = t;
546
+ for (; r > Y; )
547
+ r & M && (n = e.mul(n, s)), s = e.sqr(s), r >>= M;
548
+ return n;
549
+ }
550
+ function ve(e, t, r = !1) {
551
+ const n = new Array(t.length).fill(r ? e.ZERO : void 0), s = t.reduce((o, c, f) => e.is0(c) ? o : (n[f] = o, e.mul(o, c)), e.ONE), i = e.inv(s);
552
+ return t.reduceRight((o, c, f) => e.is0(c) ? o : (n[f] = e.mul(o, n[f]), e.mul(o, c)), i), n;
553
+ }
554
+ function Jt(e, t) {
555
+ const r = (e.ORDER - M) / ft, n = e.pow(t, r), s = e.eql(n, e.ONE), i = e.eql(n, e.ZERO), o = e.eql(n, e.neg(e.ONE));
556
+ if (!s && !i && !o)
557
+ throw new Error("invalid Legendre symbol result");
558
+ return s ? 1 : i ? 0 : -1;
559
+ }
560
+ function wn(e, t) {
561
+ t !== void 0 && ut(t);
562
+ const r = t !== void 0 ? t : e.toString(2).length, n = Math.ceil(r / 8);
563
+ return { nBitLength: r, nByteLength: n };
564
+ }
565
+ class bn {
566
+ constructor(t, r = {}) {
567
+ m(this, "ORDER");
568
+ m(this, "BITS");
569
+ m(this, "BYTES");
570
+ m(this, "isLE");
571
+ m(this, "ZERO", Y);
572
+ m(this, "ONE", M);
573
+ m(this, "_lengths");
574
+ m(this, "_sqrt");
575
+ // cached sqrt
576
+ m(this, "_mod");
577
+ var o;
578
+ if (t <= Y)
579
+ throw new Error("invalid field: expected ORDER > 0, got " + t);
580
+ let n;
581
+ this.isLE = !1, r != null && typeof r == "object" && (typeof r.BITS == "number" && (n = r.BITS), typeof r.sqrt == "function" && (this.sqrt = r.sqrt), typeof r.isLE == "boolean" && (this.isLE = r.isLE), r.allowedLengths && (this._lengths = (o = r.allowedLengths) == null ? void 0 : o.slice()), typeof r.modFromBytes == "boolean" && (this._mod = r.modFromBytes));
582
+ const { nBitLength: s, nByteLength: i } = wn(t, n);
583
+ if (i > 2048)
584
+ throw new Error("invalid field: expected ORDER of <= 2048 bytes");
585
+ this.ORDER = t, this.BITS = s, this.BYTES = i, this._sqrt = void 0, Object.preventExtensions(this);
586
+ }
587
+ create(t) {
588
+ return F(t, this.ORDER);
589
+ }
590
+ isValid(t) {
591
+ if (typeof t != "bigint")
592
+ throw new Error("invalid field element: expected bigint, got " + typeof t);
593
+ return Y <= t && t < this.ORDER;
594
+ }
595
+ is0(t) {
596
+ return t === Y;
597
+ }
598
+ // is valid and invertible
599
+ isValidNot0(t) {
600
+ return !this.is0(t) && this.isValid(t);
601
+ }
602
+ isOdd(t) {
603
+ return (t & M) === M;
604
+ }
605
+ neg(t) {
606
+ return F(-t, this.ORDER);
607
+ }
608
+ eql(t, r) {
609
+ return t === r;
610
+ }
611
+ sqr(t) {
612
+ return F(t * t, this.ORDER);
613
+ }
614
+ add(t, r) {
615
+ return F(t + r, this.ORDER);
616
+ }
617
+ sub(t, r) {
618
+ return F(t - r, this.ORDER);
619
+ }
620
+ mul(t, r) {
621
+ return F(t * r, this.ORDER);
622
+ }
623
+ pow(t, r) {
624
+ return gn(this, t, r);
625
+ }
626
+ div(t, r) {
627
+ return F(t * Qt(r, this.ORDER), this.ORDER);
628
+ }
629
+ // Same as above, but doesn't normalize
630
+ sqrN(t) {
631
+ return t * t;
632
+ }
633
+ addN(t, r) {
634
+ return t + r;
635
+ }
636
+ subN(t, r) {
637
+ return t - r;
638
+ }
639
+ mulN(t, r) {
640
+ return t * r;
641
+ }
642
+ inv(t) {
643
+ return Qt(t, this.ORDER);
644
+ }
645
+ sqrt(t) {
646
+ return this._sqrt || (this._sqrt = ln(this.ORDER)), this._sqrt(this, t);
647
+ }
648
+ toBytes(t) {
649
+ return this.isLE ? we(t, this.BYTES) : Mt(t, this.BYTES);
650
+ }
651
+ fromBytes(t, r = !1) {
652
+ N(t);
653
+ const { _lengths: n, BYTES: s, isLE: i, ORDER: o, _mod: c } = this;
654
+ if (n) {
655
+ if (!n.includes(t.length) || t.length > s)
656
+ throw new Error("Field.fromBytes: expected " + n + " bytes, got " + t.length);
657
+ const a = new Uint8Array(s);
658
+ a.set(t, i ? 0 : a.length - t.length), t = a;
659
+ }
660
+ if (t.length !== s)
661
+ throw new Error("Field.fromBytes: expected " + s + " bytes, got " + t.length);
662
+ let f = i ? ge(t) : Ut(t);
663
+ if (c && (f = F(f, o)), !r && !this.isValid(f))
664
+ throw new Error("invalid field element: outside of range 0..ORDER");
665
+ return f;
666
+ }
667
+ // TODO: we don't need it here, move out to separate fn
668
+ invertBatch(t) {
669
+ return ve(this, t);
670
+ }
671
+ // We can't move this out because Fp6, Fp12 implement it
672
+ // and it's unclear what to return in there.
673
+ cmov(t, r, n) {
674
+ return n ? r : t;
675
+ }
676
+ }
677
+ function St(e, t = {}) {
678
+ return new bn(e, t);
679
+ }
680
+ function Re(e) {
681
+ if (typeof e != "bigint")
682
+ throw new Error("field order must be bigint");
683
+ const t = e.toString(2).length;
684
+ return Math.ceil(t / 8);
685
+ }
686
+ function Ae(e) {
687
+ const t = Re(e);
688
+ return t + Math.ceil(t / 2);
689
+ }
690
+ function pn(e, t, r = !1) {
691
+ N(e);
692
+ const n = e.length, s = Re(t), i = Ae(t);
693
+ if (n < 16 || n < i || n > 1024)
694
+ throw new Error("expected " + i + "-1024 bytes of input, got " + n);
695
+ const o = r ? ge(e) : Ut(e), c = F(o, t - M) + M;
696
+ return r ? we(c, s) : Mt(c, s);
697
+ }
698
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
699
+ const dt = /* @__PURE__ */ BigInt(0), at = /* @__PURE__ */ BigInt(1);
700
+ function It(e, t) {
701
+ const r = t.negate();
702
+ return e ? r : t;
703
+ }
704
+ function te(e, t) {
705
+ const r = ve(e.Fp, t.map((n) => n.Z));
706
+ return t.map((n, s) => e.fromAffine(n.toAffine(r[s])));
707
+ }
708
+ function Ie(e, t) {
709
+ if (!Number.isSafeInteger(e) || e <= 0 || e > t)
710
+ throw new Error("invalid window size, expected [1.." + t + "], got W=" + e);
711
+ }
712
+ function Ht(e, t) {
713
+ Ie(e, t);
714
+ const r = Math.ceil(t / e) + 1, n = 2 ** (e - 1), s = 2 ** e, i = Yt(e), o = BigInt(e);
715
+ return { windows: r, windowSize: n, mask: i, maxNumber: s, shiftBy: o };
716
+ }
717
+ function ee(e, t, r) {
718
+ const { windowSize: n, mask: s, maxNumber: i, shiftBy: o } = r;
719
+ let c = Number(e & s), f = e >> o;
720
+ c > n && (c -= i, f += at);
721
+ const a = t * n, h = a + Math.abs(c) - 1, d = c === 0, y = c < 0, g = t % 2 !== 0;
722
+ return { nextN: f, offset: h, isZero: d, isNeg: y, isNegF: g, offsetF: a };
723
+ }
724
+ const qt = /* @__PURE__ */ new WeakMap(), Le = /* @__PURE__ */ new WeakMap();
725
+ function Nt(e) {
726
+ return Le.get(e) || 1;
727
+ }
728
+ function ne(e) {
729
+ if (e !== dt)
730
+ throw new Error("invalid wNAF");
731
+ }
732
+ class yn {
733
+ // Parametrized with a given Point class (not individual point)
734
+ constructor(t, r) {
735
+ m(this, "BASE");
736
+ m(this, "ZERO");
737
+ m(this, "Fn");
738
+ m(this, "bits");
739
+ this.BASE = t.BASE, this.ZERO = t.ZERO, this.Fn = t.Fn, this.bits = r;
740
+ }
741
+ // non-const time multiplication ladder
742
+ _unsafeLadder(t, r, n = this.ZERO) {
743
+ let s = t;
744
+ for (; r > dt; )
745
+ r & at && (n = n.add(s)), s = s.double(), r >>= at;
746
+ return n;
747
+ }
748
+ /**
749
+ * Creates a wNAF precomputation window. Used for caching.
750
+ * Default window size is set by `utils.precompute()` and is equal to 8.
751
+ * Number of precomputed points depends on the curve size:
752
+ * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
753
+ * - 𝑊 is the window size
754
+ * - 𝑛 is the bitlength of the curve order.
755
+ * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
756
+ * @param point Point instance
757
+ * @param W window size
758
+ * @returns precomputed point tables flattened to a single array
759
+ */
760
+ precomputeWindow(t, r) {
761
+ const { windows: n, windowSize: s } = Ht(r, this.bits), i = [];
762
+ let o = t, c = o;
763
+ for (let f = 0; f < n; f++) {
764
+ c = o, i.push(c);
765
+ for (let a = 1; a < s; a++)
766
+ c = c.add(o), i.push(c);
767
+ o = c.double();
768
+ }
769
+ return i;
770
+ }
771
+ /**
772
+ * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
773
+ * More compact implementation:
774
+ * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
775
+ * @returns real and fake (for const-time) points
776
+ */
777
+ wNAF(t, r, n) {
778
+ if (!this.Fn.isValid(n))
779
+ throw new Error("invalid scalar");
780
+ let s = this.ZERO, i = this.BASE;
781
+ const o = Ht(t, this.bits);
782
+ for (let c = 0; c < o.windows; c++) {
783
+ const { nextN: f, offset: a, isZero: h, isNeg: d, isNegF: y, offsetF: g } = ee(n, c, o);
784
+ n = f, h ? i = i.add(It(y, r[g])) : s = s.add(It(d, r[a]));
785
+ }
786
+ return ne(n), { p: s, f: i };
787
+ }
788
+ /**
789
+ * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
790
+ * @param acc accumulator point to add result of multiplication
791
+ * @returns point
792
+ */
793
+ wNAFUnsafe(t, r, n, s = this.ZERO) {
794
+ const i = Ht(t, this.bits);
795
+ for (let o = 0; o < i.windows && n !== dt; o++) {
796
+ const { nextN: c, offset: f, isZero: a, isNeg: h } = ee(n, o, i);
797
+ if (n = c, !a) {
798
+ const d = r[f];
799
+ s = s.add(h ? d.negate() : d);
800
+ }
801
+ }
802
+ return ne(n), s;
803
+ }
804
+ getPrecomputes(t, r, n) {
805
+ let s = qt.get(r);
806
+ return s || (s = this.precomputeWindow(r, t), t !== 1 && (typeof n == "function" && (s = n(s)), qt.set(r, s))), s;
807
+ }
808
+ cached(t, r, n) {
809
+ const s = Nt(t);
810
+ return this.wNAF(s, this.getPrecomputes(s, t, n), r);
811
+ }
812
+ unsafe(t, r, n, s) {
813
+ const i = Nt(t);
814
+ return i === 1 ? this._unsafeLadder(t, r, s) : this.wNAFUnsafe(i, this.getPrecomputes(i, t, n), r, s);
815
+ }
816
+ // We calculate precomputes for elliptic curve point multiplication
817
+ // using windowed method. This specifies window size and
818
+ // stores precomputed values. Usually only base point would be precomputed.
819
+ createCache(t, r) {
820
+ Ie(r, this.bits), Le.set(t, r), qt.delete(t);
821
+ }
822
+ hasCache(t) {
823
+ return Nt(t) !== 1;
824
+ }
825
+ }
826
+ function xn(e, t, r, n) {
827
+ let s = t, i = e.ZERO, o = e.ZERO;
828
+ for (; r > dt || n > dt; )
829
+ r & at && (i = i.add(s)), n & at && (o = o.add(s)), s = s.double(), r >>= at, n >>= at;
830
+ return { p1: i, p2: o };
831
+ }
832
+ function re(e, t, r) {
833
+ if (t) {
834
+ if (t.ORDER !== e)
835
+ throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
836
+ return hn(t), t;
837
+ } else
838
+ return St(e, { isLE: r });
839
+ }
840
+ function mn(e, t, r = {}, n) {
841
+ if (n === void 0 && (n = e === "edwards"), !t || typeof t != "object")
842
+ throw new Error(`expected valid ${e} CURVE object`);
843
+ for (const f of ["p", "n", "h"]) {
844
+ const a = t[f];
845
+ if (!(typeof a == "bigint" && a > dt))
846
+ throw new Error(`CURVE.${f} must be positive bigint`);
847
+ }
848
+ const s = re(t.p, r.Fp, n), i = re(t.n, r.Fn, n), c = ["Gx", "Gy", "a", "b"];
849
+ for (const f of c)
850
+ if (!s.isValid(t[f]))
851
+ throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);
852
+ return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: s, Fn: i };
853
+ }
854
+ function En(e, t) {
855
+ return function(n) {
856
+ const s = e(n);
857
+ return { secretKey: s, publicKey: t(s) };
858
+ };
859
+ }
860
+ class Ue {
861
+ constructor(t, r) {
862
+ m(this, "oHash");
863
+ m(this, "iHash");
864
+ m(this, "blockLen");
865
+ m(this, "outputLen");
866
+ m(this, "finished", !1);
867
+ m(this, "destroyed", !1);
868
+ if (ae(t), N(r, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function")
869
+ throw new Error("Expected instance of class which extends utils.Hash");
870
+ this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen;
871
+ const n = this.blockLen, s = new Uint8Array(n);
872
+ s.set(r.length > n ? t.create().update(r).digest() : r);
873
+ for (let i = 0; i < s.length; i++)
874
+ s[i] ^= 54;
875
+ this.iHash.update(s), this.oHash = t.create();
876
+ for (let i = 0; i < s.length; i++)
877
+ s[i] ^= 106;
878
+ this.oHash.update(s), vt(s);
879
+ }
880
+ update(t) {
881
+ return Bt(this), this.iHash.update(t), this;
882
+ }
883
+ digestInto(t) {
884
+ Bt(this), N(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy();
885
+ }
886
+ digest() {
887
+ const t = new Uint8Array(this.oHash.outputLen);
888
+ return this.digestInto(t), t;
889
+ }
890
+ _cloneInto(t) {
891
+ t || (t = Object.create(Object.getPrototypeOf(this), {}));
892
+ const { oHash: r, iHash: n, finished: s, destroyed: i, blockLen: o, outputLen: c } = this;
893
+ return t = t, t.finished = s, t.destroyed = i, t.blockLen = o, t.outputLen = c, t.oHash = r._cloneInto(t.oHash), t.iHash = n._cloneInto(t.iHash), t;
894
+ }
895
+ clone() {
896
+ return this._cloneInto();
897
+ }
898
+ destroy() {
899
+ this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy();
900
+ }
901
+ }
902
+ const Se = (e, t, r) => new Ue(e, t).update(r).digest();
903
+ Se.create = (e, t) => new Ue(e, t);
904
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
905
+ const se = (e, t) => (e + (e >= 0 ? t : -t) / _e) / t;
906
+ function Bn(e, t, r) {
907
+ const [[n, s], [i, o]] = t, c = se(o * e, r), f = se(-s * e, r);
908
+ let a = e - c * n - f * i, h = -c * s - f * o;
909
+ const d = a < tt, y = h < tt;
910
+ d && (a = -a), y && (h = -h);
911
+ const g = Yt(Math.ceil(sn(r) / 2)) + lt;
912
+ if (a < tt || a >= g || h < tt || h >= g)
913
+ throw new Error("splitScalar (endomorphism): failed, k=" + e);
914
+ return { k1neg: d, k1: a, k2neg: y, k2: h };
915
+ }
916
+ function Zt(e) {
917
+ if (!["compact", "recovered", "der"].includes(e))
918
+ throw new Error('Signature format must be "compact", "recovered", or "der"');
919
+ return e;
920
+ }
921
+ function Dt(e, t) {
922
+ const r = {};
923
+ for (let n of Object.keys(t))
924
+ r[n] = e[n] === void 0 ? t[n] : e[n];
925
+ return At(r.lowS, "lowS"), At(r.prehash, "prehash"), r.format !== void 0 && Zt(r.format), r;
926
+ }
927
+ class vn extends Error {
928
+ constructor(t = "") {
929
+ super(t);
930
+ }
931
+ }
932
+ const ot = {
933
+ // asn.1 DER encoding utils
934
+ Err: vn,
935
+ // Basic building block is TLV (Tag-Length-Value)
936
+ _tlv: {
937
+ encode: (e, t) => {
938
+ const { Err: r } = ot;
939
+ if (e < 0 || e > 256)
940
+ throw new r("tlv.encode: wrong tag");
941
+ if (t.length & 1)
942
+ throw new r("tlv.encode: unpadded data");
943
+ const n = t.length / 2, s = pt(n);
944
+ if (s.length / 2 & 128)
945
+ throw new r("tlv.encode: long form length too big");
946
+ const i = n > 127 ? pt(s.length / 2 | 128) : "";
947
+ return pt(e) + i + s + t;
948
+ },
949
+ // v - value, l - left bytes (unparsed)
950
+ decode(e, t) {
951
+ const { Err: r } = ot;
952
+ let n = 0;
953
+ if (e < 0 || e > 256)
954
+ throw new r("tlv.encode: wrong tag");
955
+ if (t.length < 2 || t[n++] !== e)
956
+ throw new r("tlv.decode: wrong tlv");
957
+ const s = t[n++], i = !!(s & 128);
958
+ let o = 0;
959
+ if (!i)
960
+ o = s;
961
+ else {
962
+ const f = s & 127;
963
+ if (!f)
964
+ throw new r("tlv.decode(long): indefinite length not supported");
965
+ if (f > 4)
966
+ throw new r("tlv.decode(long): byte length is too big");
967
+ const a = t.subarray(n, n + f);
968
+ if (a.length !== f)
969
+ throw new r("tlv.decode: length bytes not complete");
970
+ if (a[0] === 0)
971
+ throw new r("tlv.decode(long): zero leftmost byte");
972
+ for (const h of a)
973
+ o = o << 8 | h;
974
+ if (n += f, o < 128)
975
+ throw new r("tlv.decode(long): not minimal encoding");
976
+ }
977
+ const c = t.subarray(n, n + o);
978
+ if (c.length !== o)
979
+ throw new r("tlv.decode: wrong value length");
980
+ return { v: c, l: t.subarray(n + o) };
981
+ }
982
+ },
983
+ // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
984
+ // since we always use positive integers here. It must always be empty:
985
+ // - add zero byte if exists
986
+ // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
987
+ _int: {
988
+ encode(e) {
989
+ const { Err: t } = ot;
990
+ if (e < tt)
991
+ throw new t("integer: negative integers are not allowed");
992
+ let r = pt(e);
993
+ if (Number.parseInt(r[0], 16) & 8 && (r = "00" + r), r.length & 1)
994
+ throw new t("unexpected DER parsing assertion: unpadded hex");
995
+ return r;
996
+ },
997
+ decode(e) {
998
+ const { Err: t } = ot;
999
+ if (e[0] & 128)
1000
+ throw new t("invalid signature integer: negative");
1001
+ if (e[0] === 0 && !(e[1] & 128))
1002
+ throw new t("invalid signature integer: unnecessary leading zero");
1003
+ return Ut(e);
1004
+ }
1005
+ },
1006
+ toSig(e) {
1007
+ const { Err: t, _int: r, _tlv: n } = ot, s = N(e, void 0, "signature"), { v: i, l: o } = n.decode(48, s);
1008
+ if (o.length)
1009
+ throw new t("invalid signature: left bytes after parsing");
1010
+ const { v: c, l: f } = n.decode(2, i), { v: a, l: h } = n.decode(2, f);
1011
+ if (h.length)
1012
+ throw new t("invalid signature: left bytes after parsing");
1013
+ return { r: r.decode(c), s: r.decode(a) };
1014
+ },
1015
+ hexFromSig(e) {
1016
+ const { _tlv: t, _int: r } = ot, n = t.encode(2, r.encode(e.r)), s = t.encode(2, r.encode(e.s)), i = n + s;
1017
+ return t.encode(48, i);
1018
+ }
1019
+ }, tt = BigInt(0), lt = BigInt(1), _e = BigInt(2), yt = BigInt(3), Rn = BigInt(4);
1020
+ function An(e, t = {}) {
1021
+ const r = mn("weierstrass", e, t), { Fp: n, Fn: s } = r;
1022
+ let i = r.CURVE;
1023
+ const { h: o, n: c } = i;
1024
+ jt(t, {}, {
1025
+ allowInfinityPoint: "boolean",
1026
+ clearCofactor: "function",
1027
+ isTorsionFree: "function",
1028
+ fromBytes: "function",
1029
+ toBytes: "function",
1030
+ endo: "object"
1031
+ });
1032
+ const { endo: f } = t;
1033
+ if (f && (!n.is0(i.a) || typeof f.beta != "bigint" || !Array.isArray(f.basises)))
1034
+ throw new Error('invalid endo: expected "beta": bigint and "basises": array');
1035
+ const a = He(n, s);
1036
+ function h() {
1037
+ if (!n.isOdd)
1038
+ throw new Error("compression is not supported: Field does not have .isOdd()");
1039
+ }
1040
+ function d(S, l, u) {
1041
+ const { x: w, y: p } = l.toAffine(), B = n.toBytes(w);
1042
+ if (At(u, "isCompressed"), u) {
1043
+ h();
1044
+ const E = !n.isOdd(p);
1045
+ return it(Oe(E), B);
1046
+ } else
1047
+ return it(Uint8Array.of(4), B, n.toBytes(p));
1048
+ }
1049
+ function y(S) {
1050
+ N(S, void 0, "Point");
1051
+ const { publicKey: l, publicKeyUncompressed: u } = a, w = S.length, p = S[0], B = S.subarray(1);
1052
+ if (w === l && (p === 2 || p === 3)) {
1053
+ const E = n.fromBytes(B);
1054
+ if (!n.isValid(E))
1055
+ throw new Error("bad point: is not on curve, wrong x");
1056
+ const v = U(E);
1057
+ let b;
1058
+ try {
1059
+ b = n.sqrt(v);
1060
+ } catch (D) {
1061
+ const H = D instanceof Error ? ": " + D.message : "";
1062
+ throw new Error("bad point: is not on curve, sqrt error" + H);
1063
+ }
1064
+ h();
1065
+ const R = n.isOdd(b);
1066
+ return (p & 1) === 1 !== R && (b = n.neg(b)), { x: E, y: b };
1067
+ } else if (w === u && p === 4) {
1068
+ const E = n.BYTES, v = n.fromBytes(B.subarray(0, E)), b = n.fromBytes(B.subarray(E, E * 2));
1069
+ if (!I(v, b))
1070
+ throw new Error("bad point: is not on curve");
1071
+ return { x: v, y: b };
1072
+ } else
1073
+ throw new Error(`bad point: got length ${w}, expected compressed=${l} or uncompressed=${u}`);
1074
+ }
1075
+ const g = t.toBytes || d, x = t.fromBytes || y;
1076
+ function U(S) {
1077
+ const l = n.sqr(S), u = n.mul(l, S);
1078
+ return n.add(n.add(u, n.mul(S, i.a)), i.b);
1079
+ }
1080
+ function I(S, l) {
1081
+ const u = n.sqr(l), w = U(S);
1082
+ return n.eql(u, w);
1083
+ }
1084
+ if (!I(i.Gx, i.Gy))
1085
+ throw new Error("bad curve params: generator point");
1086
+ const O = n.mul(n.pow(i.a, yt), Rn), j = n.mul(n.sqr(i.b), BigInt(27));
1087
+ if (n.is0(n.add(O, j)))
1088
+ throw new Error("bad curve params: a or b");
1089
+ function W(S, l, u = !1) {
1090
+ if (!n.isValid(l) || u && n.is0(l))
1091
+ throw new Error(`bad point coordinate ${S}`);
1092
+ return l;
1093
+ }
1094
+ function Z(S) {
1095
+ if (!(S instanceof z))
1096
+ throw new Error("Weierstrass Point expected");
1097
+ }
1098
+ function k(S) {
1099
+ if (!f || !f.basises)
1100
+ throw new Error("no endo");
1101
+ return Bn(S, f.basises, s.ORDER);
1102
+ }
1103
+ const G = Pt((S, l) => {
1104
+ const { X: u, Y: w, Z: p } = S;
1105
+ if (n.eql(p, n.ONE))
1106
+ return { x: u, y: w };
1107
+ const B = S.is0();
1108
+ l == null && (l = B ? n.ONE : n.inv(p));
1109
+ const E = n.mul(u, l), v = n.mul(w, l), b = n.mul(p, l);
1110
+ if (B)
1111
+ return { x: n.ZERO, y: n.ZERO };
1112
+ if (!n.eql(b, n.ONE))
1113
+ throw new Error("invZ was invalid");
1114
+ return { x: E, y: v };
1115
+ }), _t = Pt((S) => {
1116
+ if (S.is0()) {
1117
+ if (t.allowInfinityPoint && !n.is0(S.Y))
1118
+ return;
1119
+ throw new Error("bad point: ZERO");
1120
+ }
1121
+ const { x: l, y: u } = S.toAffine();
1122
+ if (!n.isValid(l) || !n.isValid(u))
1123
+ throw new Error("bad point: x or y not field elements");
1124
+ if (!I(l, u))
1125
+ throw new Error("bad point: equation left != right");
1126
+ if (!S.isTorsionFree())
1127
+ throw new Error("bad point: not in prime-order subgroup");
1128
+ return !0;
1129
+ });
1130
+ function ht(S, l, u, w, p) {
1131
+ return u = new z(n.mul(u.X, S), u.Y, u.Z), l = It(w, l), u = It(p, u), l.add(u);
1132
+ }
1133
+ const _ = class _ {
1134
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
1135
+ constructor(l, u, w) {
1136
+ m(this, "X");
1137
+ m(this, "Y");
1138
+ m(this, "Z");
1139
+ this.X = W("x", l), this.Y = W("y", u, !0), this.Z = W("z", w), Object.freeze(this);
1140
+ }
1141
+ static CURVE() {
1142
+ return i;
1143
+ }
1144
+ /** Does NOT validate if the point is valid. Use `.assertValidity()`. */
1145
+ static fromAffine(l) {
1146
+ const { x: u, y: w } = l || {};
1147
+ if (!l || !n.isValid(u) || !n.isValid(w))
1148
+ throw new Error("invalid affine point");
1149
+ if (l instanceof _)
1150
+ throw new Error("projective point not allowed");
1151
+ return n.is0(u) && n.is0(w) ? _.ZERO : new _(u, w, n.ONE);
1152
+ }
1153
+ static fromBytes(l) {
1154
+ const u = _.fromAffine(x(N(l, void 0, "point")));
1155
+ return u.assertValidity(), u;
1156
+ }
1157
+ static fromHex(l) {
1158
+ return _.fromBytes(Rt(l));
1159
+ }
1160
+ get x() {
1161
+ return this.toAffine().x;
1162
+ }
1163
+ get y() {
1164
+ return this.toAffine().y;
1165
+ }
1166
+ /**
1167
+ *
1168
+ * @param windowSize
1169
+ * @param isLazy true will defer table computation until the first multiplication
1170
+ * @returns
1171
+ */
1172
+ precompute(l = 8, u = !0) {
1173
+ return ct.createCache(this, l), u || this.multiply(yt), this;
1174
+ }
1175
+ // TODO: return `this`
1176
+ /** A point on curve is valid if it conforms to equation. */
1177
+ assertValidity() {
1178
+ _t(this);
1179
+ }
1180
+ hasEvenY() {
1181
+ const { y: l } = this.toAffine();
1182
+ if (!n.isOdd)
1183
+ throw new Error("Field doesn't support isOdd");
1184
+ return !n.isOdd(l);
1185
+ }
1186
+ /** Compare one point to another. */
1187
+ equals(l) {
1188
+ Z(l);
1189
+ const { X: u, Y: w, Z: p } = this, { X: B, Y: E, Z: v } = l, b = n.eql(n.mul(u, v), n.mul(B, p)), R = n.eql(n.mul(w, v), n.mul(E, p));
1190
+ return b && R;
1191
+ }
1192
+ /** Flips point to one corresponding to (x, -y) in Affine coordinates. */
1193
+ negate() {
1194
+ return new _(this.X, n.neg(this.Y), this.Z);
1195
+ }
1196
+ // Renes-Costello-Batina exception-free doubling formula.
1197
+ // There is 30% faster Jacobian formula, but it is not complete.
1198
+ // https://eprint.iacr.org/2015/1060, algorithm 3
1199
+ // Cost: 8M + 3S + 3*a + 2*b3 + 15add.
1200
+ double() {
1201
+ const { a: l, b: u } = i, w = n.mul(u, yt), { X: p, Y: B, Z: E } = this;
1202
+ let v = n.ZERO, b = n.ZERO, R = n.ZERO, L = n.mul(p, p), D = n.mul(B, B), H = n.mul(E, E), A = n.mul(p, B);
1203
+ return A = n.add(A, A), R = n.mul(p, E), R = n.add(R, R), v = n.mul(l, R), b = n.mul(w, H), b = n.add(v, b), v = n.sub(D, b), b = n.add(D, b), b = n.mul(v, b), v = n.mul(A, v), R = n.mul(w, R), H = n.mul(l, H), A = n.sub(L, H), A = n.mul(l, A), A = n.add(A, R), R = n.add(L, L), L = n.add(R, L), L = n.add(L, H), L = n.mul(L, A), b = n.add(b, L), H = n.mul(B, E), H = n.add(H, H), L = n.mul(H, A), v = n.sub(v, L), R = n.mul(H, D), R = n.add(R, R), R = n.add(R, R), new _(v, b, R);
1204
+ }
1205
+ // Renes-Costello-Batina exception-free addition formula.
1206
+ // There is 30% faster Jacobian formula, but it is not complete.
1207
+ // https://eprint.iacr.org/2015/1060, algorithm 1
1208
+ // Cost: 12M + 0S + 3*a + 3*b3 + 23add.
1209
+ add(l) {
1210
+ Z(l);
1211
+ const { X: u, Y: w, Z: p } = this, { X: B, Y: E, Z: v } = l;
1212
+ let b = n.ZERO, R = n.ZERO, L = n.ZERO;
1213
+ const D = i.a, H = n.mul(i.b, yt);
1214
+ let A = n.mul(u, B), $ = n.mul(w, E), V = n.mul(p, v), K = n.add(u, w), q = n.add(B, E);
1215
+ K = n.mul(K, q), q = n.add(A, $), K = n.sub(K, q), q = n.add(u, p);
1216
+ let T = n.add(B, v);
1217
+ return q = n.mul(q, T), T = n.add(A, V), q = n.sub(q, T), T = n.add(w, p), b = n.add(E, v), T = n.mul(T, b), b = n.add($, V), T = n.sub(T, b), L = n.mul(D, q), b = n.mul(H, V), L = n.add(b, L), b = n.sub($, L), L = n.add($, L), R = n.mul(b, L), $ = n.add(A, A), $ = n.add($, A), V = n.mul(D, V), q = n.mul(H, q), $ = n.add($, V), V = n.sub(A, V), V = n.mul(D, V), q = n.add(q, V), A = n.mul($, q), R = n.add(R, A), A = n.mul(T, q), b = n.mul(K, b), b = n.sub(b, A), A = n.mul(K, $), L = n.mul(T, L), L = n.add(L, A), new _(b, R, L);
1218
+ }
1219
+ subtract(l) {
1220
+ return this.add(l.negate());
1221
+ }
1222
+ is0() {
1223
+ return this.equals(_.ZERO);
1224
+ }
1225
+ /**
1226
+ * Constant time multiplication.
1227
+ * Uses wNAF method. Windowed method may be 10% faster,
1228
+ * but takes 2x longer to generate and consumes 2x memory.
1229
+ * Uses precomputes when available.
1230
+ * Uses endomorphism for Koblitz curves.
1231
+ * @param scalar by which the point would be multiplied
1232
+ * @returns New point
1233
+ */
1234
+ multiply(l) {
1235
+ const { endo: u } = t;
1236
+ if (!s.isValidNot0(l))
1237
+ throw new Error("invalid scalar: out of range");
1238
+ let w, p;
1239
+ const B = (E) => ct.cached(this, E, (v) => te(_, v));
1240
+ if (u) {
1241
+ const { k1neg: E, k1: v, k2neg: b, k2: R } = k(l), { p: L, f: D } = B(v), { p: H, f: A } = B(R);
1242
+ p = D.add(A), w = ht(u.beta, L, H, E, b);
1243
+ } else {
1244
+ const { p: E, f: v } = B(l);
1245
+ w = E, p = v;
1246
+ }
1247
+ return te(_, [w, p])[0];
1248
+ }
1249
+ /**
1250
+ * Non-constant-time multiplication. Uses double-and-add algorithm.
1251
+ * It's faster, but should only be used when you don't care about
1252
+ * an exposed secret key e.g. sig verification, which works over *public* keys.
1253
+ */
1254
+ multiplyUnsafe(l) {
1255
+ const { endo: u } = t, w = this;
1256
+ if (!s.isValid(l))
1257
+ throw new Error("invalid scalar: out of range");
1258
+ if (l === tt || w.is0())
1259
+ return _.ZERO;
1260
+ if (l === lt)
1261
+ return w;
1262
+ if (ct.hasCache(this))
1263
+ return this.multiply(l);
1264
+ if (u) {
1265
+ const { k1neg: p, k1: B, k2neg: E, k2: v } = k(l), { p1: b, p2: R } = xn(_, w, B, v);
1266
+ return ht(u.beta, b, R, p, E);
1267
+ } else
1268
+ return ct.unsafe(w, l);
1269
+ }
1270
+ /**
1271
+ * Converts Projective point to affine (x, y) coordinates.
1272
+ * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
1273
+ */
1274
+ toAffine(l) {
1275
+ return G(this, l);
1276
+ }
1277
+ /**
1278
+ * Checks whether Point is free of torsion elements (is in prime subgroup).
1279
+ * Always torsion-free for cofactor=1 curves.
1280
+ */
1281
+ isTorsionFree() {
1282
+ const { isTorsionFree: l } = t;
1283
+ return o === lt ? !0 : l ? l(_, this) : ct.unsafe(this, c).is0();
1284
+ }
1285
+ clearCofactor() {
1286
+ const { clearCofactor: l } = t;
1287
+ return o === lt ? this : l ? l(_, this) : this.multiplyUnsafe(o);
1288
+ }
1289
+ isSmallOrder() {
1290
+ return this.multiplyUnsafe(o).is0();
1291
+ }
1292
+ toBytes(l = !0) {
1293
+ return At(l, "isCompressed"), this.assertValidity(), g(_, this, l);
1294
+ }
1295
+ toHex(l = !0) {
1296
+ return Lt(this.toBytes(l));
1297
+ }
1298
+ toString() {
1299
+ return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
1300
+ }
1301
+ };
1302
+ // base / generator point
1303
+ m(_, "BASE", new _(i.Gx, i.Gy, n.ONE)), // zero / infinity / identity point
1304
+ m(_, "ZERO", new _(n.ZERO, n.ONE, n.ZERO)), // 0, 1, 0
1305
+ // math field
1306
+ m(_, "Fp", n), // scalar field
1307
+ m(_, "Fn", s);
1308
+ let z = _;
1309
+ const bt = s.BITS, ct = new yn(z, t.endo ? Math.ceil(bt / 2) : bt);
1310
+ return z.BASE.precompute(8), z;
1311
+ }
1312
+ function Oe(e) {
1313
+ return Uint8Array.of(e ? 2 : 3);
1314
+ }
1315
+ function He(e, t) {
1316
+ return {
1317
+ secretKey: t.BYTES,
1318
+ publicKey: 1 + e.BYTES,
1319
+ publicKeyUncompressed: 1 + 2 * e.BYTES,
1320
+ publicKeyHasPrefix: !0,
1321
+ signature: 2 * t.BYTES
1322
+ };
1323
+ }
1324
+ function In(e, t = {}) {
1325
+ const { Fn: r } = e, n = t.randomBytes || le, s = Object.assign(He(e.Fp, r), { seed: Ae(r.ORDER) });
1326
+ function i(g) {
1327
+ try {
1328
+ const x = r.fromBytes(g);
1329
+ return r.isValidNot0(x);
1330
+ } catch {
1331
+ return !1;
1332
+ }
1333
+ }
1334
+ function o(g, x) {
1335
+ const { publicKey: U, publicKeyUncompressed: I } = s;
1336
+ try {
1337
+ const O = g.length;
1338
+ return x === !0 && O !== U || x === !1 && O !== I ? !1 : !!e.fromBytes(g);
1339
+ } catch {
1340
+ return !1;
1341
+ }
1342
+ }
1343
+ function c(g = n(s.seed)) {
1344
+ return pn(N(g, s.seed, "seed"), r.ORDER);
1345
+ }
1346
+ function f(g, x = !0) {
1347
+ return e.BASE.multiply(r.fromBytes(g)).toBytes(x);
1348
+ }
1349
+ function a(g) {
1350
+ const { secretKey: x, publicKey: U, publicKeyUncompressed: I } = s;
1351
+ if (!Vt(g) || "_lengths" in r && r._lengths || x === U)
1352
+ return;
1353
+ const O = N(g, void 0, "key").length;
1354
+ return O === U || O === I;
1355
+ }
1356
+ function h(g, x, U = !0) {
1357
+ if (a(g) === !0)
1358
+ throw new Error("first arg must be private key");
1359
+ if (a(x) === !1)
1360
+ throw new Error("second arg must be public key");
1361
+ const I = r.fromBytes(g);
1362
+ return e.fromBytes(x).multiply(I).toBytes(U);
1363
+ }
1364
+ const d = {
1365
+ isValidSecretKey: i,
1366
+ isValidPublicKey: o,
1367
+ randomSecretKey: c
1368
+ }, y = En(c, f);
1369
+ return Object.freeze({ getPublicKey: f, getSharedSecret: h, keygen: y, Point: e, utils: d, lengths: s });
1370
+ }
1371
+ function Ln(e, t, r = {}) {
1372
+ ae(t), jt(r, {}, {
1373
+ hmac: "function",
1374
+ lowS: "boolean",
1375
+ randomBytes: "function",
1376
+ bits2int: "function",
1377
+ bits2int_modN: "function"
1378
+ }), r = Object.assign({}, r);
1379
+ const n = r.randomBytes || le, s = r.hmac || ((l, u) => Se(t, l, u)), { Fp: i, Fn: o } = e, { ORDER: c, BITS: f } = o, { keygen: a, getPublicKey: h, getSharedSecret: d, utils: y, lengths: g } = In(e, r), x = {
1380
+ prehash: !0,
1381
+ lowS: typeof r.lowS == "boolean" ? r.lowS : !0,
1382
+ format: "compact",
1383
+ extraEntropy: !1
1384
+ }, U = c * _e < i.ORDER;
1385
+ function I(l) {
1386
+ const u = c >> lt;
1387
+ return l > u;
1388
+ }
1389
+ function O(l, u) {
1390
+ if (!o.isValidNot0(u))
1391
+ throw new Error(`invalid signature ${l}: out of range 1..Point.Fn.ORDER`);
1392
+ return u;
1393
+ }
1394
+ function j() {
1395
+ if (U)
1396
+ throw new Error('"recovered" sig type is not supported for cofactor >2 curves');
1397
+ }
1398
+ function W(l, u) {
1399
+ Zt(u);
1400
+ const w = g.signature, p = u === "compact" ? w : u === "recovered" ? w + 1 : void 0;
1401
+ return N(l, p);
1402
+ }
1403
+ class Z {
1404
+ constructor(u, w, p) {
1405
+ m(this, "r");
1406
+ m(this, "s");
1407
+ m(this, "recovery");
1408
+ if (this.r = O("r", u), this.s = O("s", w), p != null) {
1409
+ if (j(), ![0, 1, 2, 3].includes(p))
1410
+ throw new Error("invalid recovery id");
1411
+ this.recovery = p;
1412
+ }
1413
+ Object.freeze(this);
1414
+ }
1415
+ static fromBytes(u, w = x.format) {
1416
+ W(u, w);
1417
+ let p;
1418
+ if (w === "der") {
1419
+ const { r: b, s: R } = ot.toSig(N(u));
1420
+ return new Z(b, R);
1421
+ }
1422
+ w === "recovered" && (p = u[0], w = "compact", u = u.subarray(1));
1423
+ const B = g.signature / 2, E = u.subarray(0, B), v = u.subarray(B, B * 2);
1424
+ return new Z(o.fromBytes(E), o.fromBytes(v), p);
1425
+ }
1426
+ static fromHex(u, w) {
1427
+ return this.fromBytes(Rt(u), w);
1428
+ }
1429
+ assertRecovery() {
1430
+ const { recovery: u } = this;
1431
+ if (u == null)
1432
+ throw new Error("invalid recovery id: must be present");
1433
+ return u;
1434
+ }
1435
+ addRecoveryBit(u) {
1436
+ return new Z(this.r, this.s, u);
1437
+ }
1438
+ recoverPublicKey(u) {
1439
+ const { r: w, s: p } = this, B = this.assertRecovery(), E = B === 2 || B === 3 ? w + c : w;
1440
+ if (!i.isValid(E))
1441
+ throw new Error("invalid recovery id: sig.r+curve.n != R.x");
1442
+ const v = i.toBytes(E), b = e.fromBytes(it(Oe((B & 1) === 0), v)), R = o.inv(E), L = G(N(u, void 0, "msgHash")), D = o.create(-L * R), H = o.create(p * R), A = e.BASE.multiplyUnsafe(D).add(b.multiplyUnsafe(H));
1443
+ if (A.is0())
1444
+ throw new Error("invalid recovery: point at infinify");
1445
+ return A.assertValidity(), A;
1446
+ }
1447
+ // Signatures should be low-s, to prevent malleability.
1448
+ hasHighS() {
1449
+ return I(this.s);
1450
+ }
1451
+ toBytes(u = x.format) {
1452
+ if (Zt(u), u === "der")
1453
+ return Rt(ot.hexFromSig(this));
1454
+ const { r: w, s: p } = this, B = o.toBytes(w), E = o.toBytes(p);
1455
+ return u === "recovered" ? (j(), it(Uint8Array.of(this.assertRecovery()), B, E)) : it(B, E);
1456
+ }
1457
+ toHex(u) {
1458
+ return Lt(this.toBytes(u));
1459
+ }
1460
+ }
1461
+ const k = r.bits2int || function(u) {
1462
+ if (u.length > 8192)
1463
+ throw new Error("input is too large");
1464
+ const w = Ut(u), p = u.length * 8 - f;
1465
+ return p > 0 ? w >> BigInt(p) : w;
1466
+ }, G = r.bits2int_modN || function(u) {
1467
+ return o.create(k(u));
1468
+ }, _t = Yt(f);
1469
+ function ht(l) {
1470
+ return rn("num < 2^" + f, l, tt, _t), o.toBytes(l);
1471
+ }
1472
+ function z(l, u) {
1473
+ return N(l, void 0, "message"), u ? N(t(l), void 0, "prehashed message") : l;
1474
+ }
1475
+ function bt(l, u, w) {
1476
+ const { lowS: p, prehash: B, extraEntropy: E } = Dt(w, x);
1477
+ l = z(l, B);
1478
+ const v = G(l), b = o.fromBytes(u);
1479
+ if (!o.isValidNot0(b))
1480
+ throw new Error("invalid private key");
1481
+ const R = [ht(b), ht(v)];
1482
+ if (E != null && E !== !1) {
1483
+ const A = E === !0 ? n(g.secretKey) : E;
1484
+ R.push(N(A, void 0, "extraEntropy"));
1485
+ }
1486
+ const L = it(...R), D = v;
1487
+ function H(A) {
1488
+ const $ = k(A);
1489
+ if (!o.isValidNot0($))
1490
+ return;
1491
+ const V = o.inv($), K = e.BASE.multiply($).toAffine(), q = o.create(K.x);
1492
+ if (q === tt)
1493
+ return;
1494
+ const T = o.create(V * o.create(D + q * b));
1495
+ if (T === tt)
1496
+ return;
1497
+ let zt = (K.x === q ? 0 : 2) | Number(K.y & lt), Ft = T;
1498
+ return p && I(T) && (Ft = o.neg(T), zt ^= 1), new Z(q, Ft, U ? void 0 : zt);
1499
+ }
1500
+ return { seed: L, k2sig: H };
1501
+ }
1502
+ function ct(l, u, w = {}) {
1503
+ const { seed: p, k2sig: B } = bt(l, u, w);
1504
+ return on(t.outputLen, o.BYTES, s)(p, B).toBytes(w.format);
1505
+ }
1506
+ function _(l, u, w, p = {}) {
1507
+ const { lowS: B, prehash: E, format: v } = Dt(p, x);
1508
+ if (w = N(w, void 0, "publicKey"), u = z(u, E), !Vt(l)) {
1509
+ const b = l instanceof Z ? ", use sig.toBytes()" : "";
1510
+ throw new Error("verify expects Uint8Array signature" + b);
1511
+ }
1512
+ W(l, v);
1513
+ try {
1514
+ const b = Z.fromBytes(l, v), R = e.fromBytes(w);
1515
+ if (B && b.hasHighS())
1516
+ return !1;
1517
+ const { r: L, s: D } = b, H = G(u), A = o.inv(D), $ = o.create(H * A), V = o.create(L * A), K = e.BASE.multiplyUnsafe($).add(R.multiplyUnsafe(V));
1518
+ return K.is0() ? !1 : o.create(K.x) === L;
1519
+ } catch {
1520
+ return !1;
1521
+ }
1522
+ }
1523
+ function S(l, u, w = {}) {
1524
+ const { prehash: p } = Dt(w, x);
1525
+ return u = z(u, p), Z.fromBytes(l, "recovered").recoverPublicKey(u).toBytes();
1526
+ }
1527
+ return Object.freeze({
1528
+ keygen: a,
1529
+ getPublicKey: h,
1530
+ getSharedSecret: d,
1531
+ utils: y,
1532
+ lengths: g,
1533
+ Point: e,
1534
+ sign: ct,
1535
+ verify: _,
1536
+ recoverPublicKey: S,
1537
+ Signature: Z,
1538
+ hash: t
1539
+ });
1540
+ }
1541
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1542
+ const Gt = {
1543
+ p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
1544
+ n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
1545
+ h: BigInt(1),
1546
+ a: BigInt(0),
1547
+ b: BigInt(7),
1548
+ Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
1549
+ Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
1550
+ }, Un = {
1551
+ beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
1552
+ basises: [
1553
+ [BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
1554
+ [BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
1555
+ ]
1556
+ }, oe = /* @__PURE__ */ BigInt(2);
1557
+ function Sn(e) {
1558
+ const t = Gt.p, r = BigInt(3), n = BigInt(6), s = BigInt(11), i = BigInt(22), o = BigInt(23), c = BigInt(44), f = BigInt(88), a = e * e * e % t, h = a * a * e % t, d = X(h, r, t) * h % t, y = X(d, r, t) * h % t, g = X(y, oe, t) * a % t, x = X(g, s, t) * g % t, U = X(x, i, t) * x % t, I = X(U, c, t) * U % t, O = X(I, f, t) * I % t, j = X(O, c, t) * U % t, W = X(j, r, t) * h % t, Z = X(W, o, t) * x % t, k = X(Z, n, t) * a % t, G = X(k, oe, t);
1559
+ if (!Ct.eql(Ct.sqr(G), e))
1560
+ throw new Error("Cannot find square root");
1561
+ return G;
1562
+ }
1563
+ const Ct = St(Gt.p, { sqrt: Sn }), _n = /* @__PURE__ */ An(Gt, {
1564
+ Fp: Ct,
1565
+ endo: Un
1566
+ }), On = /* @__PURE__ */ Ln(_n, tn);
1567
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
1568
+ function Hn(e) {
1569
+ return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array";
1570
+ }
1571
+ function Xt(e, ...t) {
1572
+ if (!Hn(e))
1573
+ throw new Error("Uint8Array expected");
1574
+ if (t.length > 0 && !t.includes(e.length))
1575
+ throw new Error("Uint8Array expected of length " + t + ", got length=" + e.length);
1576
+ }
1577
+ function ie(e, t = !0) {
1578
+ if (e.destroyed)
1579
+ throw new Error("Hash instance has been destroyed");
1580
+ if (t && e.finished)
1581
+ throw new Error("Hash#digest() has already been called");
1582
+ }
1583
+ function qn(e, t) {
1584
+ Xt(e);
1585
+ const r = t.outputLen;
1586
+ if (e.length < r)
1587
+ throw new Error("digestInto() expects output buffer of length at least " + r);
1588
+ }
1589
+ function gt(...e) {
1590
+ for (let t = 0; t < e.length; t++)
1591
+ e[t].fill(0);
1592
+ }
1593
+ function kt(e) {
1594
+ return new DataView(e.buffer, e.byteOffset, e.byteLength);
1595
+ }
1596
+ function Q(e, t) {
1597
+ return e << 32 - t | e >>> t;
1598
+ }
1599
+ function xt(e, t) {
1600
+ return e << t | e >>> 32 - t >>> 0;
1601
+ }
1602
+ function Nn(e) {
1603
+ if (typeof e != "string")
1604
+ throw new Error("string expected");
1605
+ return new Uint8Array(new TextEncoder().encode(e));
1606
+ }
1607
+ function qe(e) {
1608
+ return typeof e == "string" && (e = Nn(e)), Xt(e), e;
1609
+ }
1610
+ class Dn {
1611
+ }
1612
+ function Ne(e) {
1613
+ const t = (n) => e().update(qe(n)).digest(), r = e();
1614
+ return t.outputLen = r.outputLen, t.blockLen = r.blockLen, t.create = () => e(), t;
1615
+ }
1616
+ function kn(e, t, r, n) {
1617
+ if (typeof e.setBigUint64 == "function")
1618
+ return e.setBigUint64(t, r, n);
1619
+ const s = BigInt(32), i = BigInt(4294967295), o = Number(r >> s & i), c = Number(r & i), f = n ? 4 : 0, a = n ? 0 : 4;
1620
+ e.setUint32(t + f, o, n), e.setUint32(t + a, c, n);
1621
+ }
1622
+ function $n(e, t, r) {
1623
+ return e & t ^ ~e & r;
1624
+ }
1625
+ function Zn(e, t, r) {
1626
+ return e & t ^ e & r ^ t & r;
1627
+ }
1628
+ class De extends Dn {
1629
+ constructor(t, r, n, s) {
1630
+ super(), this.finished = !1, this.length = 0, this.pos = 0, this.destroyed = !1, this.blockLen = t, this.outputLen = r, this.padOffset = n, this.isLE = s, this.buffer = new Uint8Array(t), this.view = kt(this.buffer);
1631
+ }
1632
+ update(t) {
1633
+ ie(this), t = qe(t), Xt(t);
1634
+ const { view: r, buffer: n, blockLen: s } = this, i = t.length;
1635
+ for (let o = 0; o < i; ) {
1636
+ const c = Math.min(s - this.pos, i - o);
1637
+ if (c === s) {
1638
+ const f = kt(t);
1639
+ for (; s <= i - o; o += s)
1640
+ this.process(f, o);
1641
+ continue;
1642
+ }
1643
+ n.set(t.subarray(o, o + c), this.pos), this.pos += c, o += c, this.pos === s && (this.process(r, 0), this.pos = 0);
1644
+ }
1645
+ return this.length += t.length, this.roundClean(), this;
1646
+ }
1647
+ digestInto(t) {
1648
+ ie(this), qn(t, this), this.finished = !0;
1649
+ const { buffer: r, view: n, blockLen: s, isLE: i } = this;
1650
+ let { pos: o } = this;
1651
+ r[o++] = 128, gt(this.buffer.subarray(o)), this.padOffset > s - o && (this.process(n, 0), o = 0);
1652
+ for (let d = o; d < s; d++)
1653
+ r[d] = 0;
1654
+ kn(n, s - 8, BigInt(this.length * 8), i), this.process(n, 0);
1655
+ const c = kt(t), f = this.outputLen;
1656
+ if (f % 4)
1657
+ throw new Error("_sha2: outputLen should be aligned to 32bit");
1658
+ const a = f / 4, h = this.get();
1659
+ if (a > h.length)
1660
+ throw new Error("_sha2: outputLen bigger than state");
1661
+ for (let d = 0; d < a; d++)
1662
+ c.setUint32(4 * d, h[d], i);
1663
+ }
1664
+ digest() {
1665
+ const { buffer: t, outputLen: r } = this;
1666
+ this.digestInto(t);
1667
+ const n = t.slice(0, r);
1668
+ return this.destroy(), n;
1669
+ }
1670
+ _cloneInto(t) {
1671
+ t || (t = new this.constructor()), t.set(...this.get());
1672
+ const { blockLen: r, buffer: n, length: s, finished: i, destroyed: o, pos: c } = this;
1673
+ return t.destroyed = o, t.finished = i, t.length = s, t.pos = c, s % r && t.buffer.set(n), t;
1674
+ }
1675
+ clone() {
1676
+ return this._cloneInto();
1677
+ }
1678
+ }
1679
+ const rt = /* @__PURE__ */ Uint32Array.from([
1680
+ 1779033703,
1681
+ 3144134277,
1682
+ 1013904242,
1683
+ 2773480762,
1684
+ 1359893119,
1685
+ 2600822924,
1686
+ 528734635,
1687
+ 1541459225
1688
+ ]), Cn = /* @__PURE__ */ Uint8Array.from([
1689
+ 7,
1690
+ 4,
1691
+ 13,
1692
+ 1,
1693
+ 10,
1694
+ 6,
1695
+ 15,
1696
+ 3,
1697
+ 12,
1698
+ 0,
1699
+ 9,
1700
+ 5,
1701
+ 2,
1702
+ 14,
1703
+ 11,
1704
+ 8
1705
+ ]), ke = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), Vn = ke.map((e) => (9 * e + 5) % 16), $e = /* @__PURE__ */ (() => {
1706
+ const r = [[ke], [Vn]];
1707
+ for (let n = 0; n < 4; n++)
1708
+ for (let s of r)
1709
+ s.push(s[n].map((i) => Cn[i]));
1710
+ return r;
1711
+ })(), Ze = $e[0], Ce = $e[1], Ve = /* @__PURE__ */ [
1712
+ [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
1713
+ [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
1714
+ [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
1715
+ [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
1716
+ [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5]
1717
+ ].map((e) => Uint8Array.from(e)), Tn = /* @__PURE__ */ Ze.map((e, t) => e.map((r) => Ve[t][r])), Mn = /* @__PURE__ */ Ce.map((e, t) => e.map((r) => Ve[t][r])), Yn = /* @__PURE__ */ Uint32Array.from([
1718
+ 0,
1719
+ 1518500249,
1720
+ 1859775393,
1721
+ 2400959708,
1722
+ 2840853838
1723
+ ]), jn = /* @__PURE__ */ Uint32Array.from([
1724
+ 1352829926,
1725
+ 1548603684,
1726
+ 1836072691,
1727
+ 2053994217,
1728
+ 0
1729
+ ]);
1730
+ function ce(e, t, r, n) {
1731
+ return e === 0 ? t ^ r ^ n : e === 1 ? t & r | ~t & n : e === 2 ? (t | ~r) ^ n : e === 3 ? t & n | r & ~n : t ^ (r | ~n);
1732
+ }
1733
+ const mt = /* @__PURE__ */ new Uint32Array(16);
1734
+ class Kn extends De {
1735
+ constructor() {
1736
+ super(64, 20, 8, !0), this.h0 = 1732584193, this.h1 = -271733879, this.h2 = -1732584194, this.h3 = 271733878, this.h4 = -1009589776;
1737
+ }
1738
+ get() {
1739
+ const { h0: t, h1: r, h2: n, h3: s, h4: i } = this;
1740
+ return [t, r, n, s, i];
1741
+ }
1742
+ set(t, r, n, s, i) {
1743
+ this.h0 = t | 0, this.h1 = r | 0, this.h2 = n | 0, this.h3 = s | 0, this.h4 = i | 0;
1744
+ }
1745
+ process(t, r) {
1746
+ for (let g = 0; g < 16; g++, r += 4)
1747
+ mt[g] = t.getUint32(r, !0);
1748
+ let n = this.h0 | 0, s = n, i = this.h1 | 0, o = i, c = this.h2 | 0, f = c, a = this.h3 | 0, h = a, d = this.h4 | 0, y = d;
1749
+ for (let g = 0; g < 5; g++) {
1750
+ const x = 4 - g, U = Yn[g], I = jn[g], O = Ze[g], j = Ce[g], W = Tn[g], Z = Mn[g];
1751
+ for (let k = 0; k < 16; k++) {
1752
+ const G = xt(n + ce(g, i, c, a) + mt[O[k]] + U, W[k]) + d | 0;
1753
+ n = d, d = a, a = xt(c, 10) | 0, c = i, i = G;
1754
+ }
1755
+ for (let k = 0; k < 16; k++) {
1756
+ const G = xt(s + ce(x, o, f, h) + mt[j[k]] + I, Z[k]) + y | 0;
1757
+ s = y, y = h, h = xt(f, 10) | 0, f = o, o = G;
1758
+ }
1759
+ }
1760
+ this.set(this.h1 + c + h | 0, this.h2 + a + y | 0, this.h3 + d + s | 0, this.h4 + n + o | 0, this.h0 + i + f | 0);
1761
+ }
1762
+ roundClean() {
1763
+ gt(mt);
1764
+ }
1765
+ destroy() {
1766
+ this.destroyed = !0, gt(this.buffer), this.set(0, 0, 0, 0, 0);
1767
+ }
1768
+ }
1769
+ const Gn = /* @__PURE__ */ Ne(() => new Kn()), Xn = Gn, zn = /* @__PURE__ */ Uint32Array.from([
1770
+ 1116352408,
1771
+ 1899447441,
1772
+ 3049323471,
1773
+ 3921009573,
1774
+ 961987163,
1775
+ 1508970993,
1776
+ 2453635748,
1777
+ 2870763221,
1778
+ 3624381080,
1779
+ 310598401,
1780
+ 607225278,
1781
+ 1426881987,
1782
+ 1925078388,
1783
+ 2162078206,
1784
+ 2614888103,
1785
+ 3248222580,
1786
+ 3835390401,
1787
+ 4022224774,
1788
+ 264347078,
1789
+ 604807628,
1790
+ 770255983,
1791
+ 1249150122,
1792
+ 1555081692,
1793
+ 1996064986,
1794
+ 2554220882,
1795
+ 2821834349,
1796
+ 2952996808,
1797
+ 3210313671,
1798
+ 3336571891,
1799
+ 3584528711,
1800
+ 113926993,
1801
+ 338241895,
1802
+ 666307205,
1803
+ 773529912,
1804
+ 1294757372,
1805
+ 1396182291,
1806
+ 1695183700,
1807
+ 1986661051,
1808
+ 2177026350,
1809
+ 2456956037,
1810
+ 2730485921,
1811
+ 2820302411,
1812
+ 3259730800,
1813
+ 3345764771,
1814
+ 3516065817,
1815
+ 3600352804,
1816
+ 4094571909,
1817
+ 275423344,
1818
+ 430227734,
1819
+ 506948616,
1820
+ 659060556,
1821
+ 883997877,
1822
+ 958139571,
1823
+ 1322822218,
1824
+ 1537002063,
1825
+ 1747873779,
1826
+ 1955562222,
1827
+ 2024104815,
1828
+ 2227730452,
1829
+ 2361852424,
1830
+ 2428436474,
1831
+ 2756734187,
1832
+ 3204031479,
1833
+ 3329325298
1834
+ ]), st = /* @__PURE__ */ new Uint32Array(64);
1835
+ class Fn extends De {
1836
+ constructor(t = 32) {
1837
+ super(64, t, 8, !1), this.A = rt[0] | 0, this.B = rt[1] | 0, this.C = rt[2] | 0, this.D = rt[3] | 0, this.E = rt[4] | 0, this.F = rt[5] | 0, this.G = rt[6] | 0, this.H = rt[7] | 0;
1838
+ }
1839
+ get() {
1840
+ const { A: t, B: r, C: n, D: s, E: i, F: o, G: c, H: f } = this;
1841
+ return [t, r, n, s, i, o, c, f];
1842
+ }
1843
+ // prettier-ignore
1844
+ set(t, r, n, s, i, o, c, f) {
1845
+ this.A = t | 0, this.B = r | 0, this.C = n | 0, this.D = s | 0, this.E = i | 0, this.F = o | 0, this.G = c | 0, this.H = f | 0;
1846
+ }
1847
+ process(t, r) {
1848
+ for (let d = 0; d < 16; d++, r += 4)
1849
+ st[d] = t.getUint32(r, !1);
1850
+ for (let d = 16; d < 64; d++) {
1851
+ const y = st[d - 15], g = st[d - 2], x = Q(y, 7) ^ Q(y, 18) ^ y >>> 3, U = Q(g, 17) ^ Q(g, 19) ^ g >>> 10;
1852
+ st[d] = U + st[d - 7] + x + st[d - 16] | 0;
1853
+ }
1854
+ let { A: n, B: s, C: i, D: o, E: c, F: f, G: a, H: h } = this;
1855
+ for (let d = 0; d < 64; d++) {
1856
+ const y = Q(c, 6) ^ Q(c, 11) ^ Q(c, 25), g = h + y + $n(c, f, a) + zn[d] + st[d] | 0, U = (Q(n, 2) ^ Q(n, 13) ^ Q(n, 22)) + Zn(n, s, i) | 0;
1857
+ h = a, a = f, f = c, c = o + g | 0, o = i, i = s, s = n, n = g + U | 0;
1858
+ }
1859
+ n = n + this.A | 0, s = s + this.B | 0, i = i + this.C | 0, o = o + this.D | 0, c = c + this.E | 0, f = f + this.F | 0, a = a + this.G | 0, h = h + this.H | 0, this.set(n, s, i, o, c, f, a, h);
1860
+ }
1861
+ roundClean() {
1862
+ gt(st);
1863
+ }
1864
+ destroy() {
1865
+ this.set(0, 0, 0, 0, 0, 0, 0, 0), gt(this.buffer);
1866
+ }
1867
+ }
1868
+ const Wn = /* @__PURE__ */ Ne(() => new Fn()), wt = Wn, Pn = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1869
+ function Qn(e) {
1870
+ const t = [0];
1871
+ for (let r = 0; r < e.length; r++) {
1872
+ const n = e[r];
1873
+ if (!n) continue;
1874
+ const s = Pn.indexOf(n);
1875
+ if (s === -1) throw new Error("Invalid base58 character");
1876
+ for (let o = 0; o < t.length; o++)
1877
+ t[o] *= 58;
1878
+ t[0] += s;
1879
+ let i = 0;
1880
+ for (let o = 0; o < t.length; o++) {
1881
+ const c = t[o];
1882
+ t[o] = c + i, i = t[o] >> 8, t[o] &= 255;
1883
+ }
1884
+ for (; i > 0; )
1885
+ t.push(i & 255), i >>= 8;
1886
+ }
1887
+ for (let r = 0; r < e.length && e[r] === "1"; r++)
1888
+ t.push(0);
1889
+ return new Uint8Array(t.reverse());
1890
+ }
1891
+ function Jn(e) {
1892
+ const t = "qpzry9x8gf2tvdw0s3jn54khce6mua7l", n = e.toLowerCase().split("1");
1893
+ if (n.length !== 2) return null;
1894
+ const s = n[0], i = n[1];
1895
+ if (!s || !i || s !== "dgb") return null;
1896
+ const o = [];
1897
+ for (const h of i) {
1898
+ const d = t.indexOf(h);
1899
+ if (d === -1) return null;
1900
+ o.push(d);
1901
+ }
1902
+ const c = o.slice(0, -6);
1903
+ if (c.length < 1) return null;
1904
+ const f = c[0];
1905
+ if (f === void 0) return null;
1906
+ const a = tr(c.slice(1), 5, 8);
1907
+ return a ? { version: f, program: new Uint8Array(a) } : null;
1908
+ }
1909
+ function tr(e, t, r, n) {
1910
+ let s = 0, i = 0;
1911
+ const o = [], c = (1 << r) - 1;
1912
+ for (const f of e) {
1913
+ if (f < 0 || f >> t !== 0) return null;
1914
+ for (s = s << t | f, i += t; i >= r; )
1915
+ i -= r, o.push(s >> i & c);
1916
+ }
1917
+ return i >= t || s << r - i & c ? null : o;
1918
+ }
1919
+ function er(e, t) {
1920
+ const r = new TextEncoder().encode(t), n = new Uint8Array([r.length]), s = new TextEncoder().encode(e), i = [];
1921
+ let o = s.length;
1922
+ if (o < 253)
1923
+ i.push(o);
1924
+ else if (o <= 65535)
1925
+ i.push(253, o & 255, o >> 8 & 255);
1926
+ else if (o <= 4294967295)
1927
+ i.push(
1928
+ 254,
1929
+ o & 255,
1930
+ o >> 8 & 255,
1931
+ o >> 16 & 255,
1932
+ o >> 24 & 255
1933
+ );
1934
+ else
1935
+ throw new Error("Message too long");
1936
+ const c = new Uint8Array(i), f = n.length + r.length + c.length + s.length, a = new Uint8Array(f);
1937
+ let h = 0;
1938
+ return a.set(n, h), h += n.length, a.set(r, h), h += r.length, a.set(c, h), h += c.length, a.set(s, h), wt(wt(a));
1939
+ }
1940
+ function nr(e, t) {
1941
+ if (t.length !== 65)
1942
+ throw new Error("Invalid signature length");
1943
+ const r = t[0];
1944
+ if (r === void 0) throw new Error("Invalid signature");
1945
+ const n = r - 27, s = n >= 4;
1946
+ if (n % 4 > 3)
1947
+ throw new Error("Invalid recovery ID");
1948
+ const o = t.slice(1, 33), c = t.slice(33, 65), f = new On.Signature(
1949
+ BigInt("0x" + Array.from(o).map((a) => a.toString(16).padStart(2, "0")).join("")),
1950
+ BigInt("0x" + Array.from(c).map((a) => a.toString(16).padStart(2, "0")).join(""))
1951
+ );
1952
+ try {
1953
+ return [f.recoverPublicKey(e).toHex(s)].map((h) => {
1954
+ const d = new Uint8Array(h.length / 2);
1955
+ for (let y = 0; y < h.length; y += 2)
1956
+ d[y / 2] = parseInt(h.slice(y, y + 2), 16);
1957
+ return d;
1958
+ });
1959
+ } catch {
1960
+ throw new Error("Failed to recover public key");
1961
+ }
1962
+ }
1963
+ function fe(e) {
1964
+ return Xn(wt(e));
1965
+ }
1966
+ function rr(e, t) {
1967
+ if (e.startsWith("D") || e.startsWith("S"))
1968
+ try {
1969
+ const r = Qn(e);
1970
+ if (r.length < 25) return !1;
1971
+ const n = r.slice(0, -4), s = r.slice(-4), o = wt(wt(n)).slice(0, 4);
1972
+ if (!s.every((a, h) => a === o[h]))
1973
+ return !1;
1974
+ const c = n.slice(1), f = fe(t);
1975
+ return c.every((a, h) => a === f[h]);
1976
+ } catch {
1977
+ return !1;
1978
+ }
1979
+ if (e.toLowerCase().startsWith("dgb1"))
1980
+ try {
1981
+ const r = Jn(e);
1982
+ if (!r) return !1;
1983
+ const { version: n, program: s } = r;
1984
+ if (n === 0) {
1985
+ const i = fe(t);
1986
+ return s.every((o, c) => o === i[c]);
1987
+ }
1988
+ return !1;
1989
+ } catch {
1990
+ return !1;
1991
+ }
1992
+ return !1;
1993
+ }
1994
+ async function sr(e, t, r) {
1995
+ const n = `DigiByte Signed Message:
10
1996
  `;
11
1997
  try {
12
- return !!m.verify(
13
- t,
14
- // The message that was signed (the DigiID URI)
15
- r,
16
- // The DigiByte address (D..., S..., or dgb1...)
17
- i,
18
- // The signature string (Base64 encoded)
19
- c,
20
- // The DigiByte specific message prefix
21
- !0
22
- // Set checkSegwitAlways to true to handle all address types correctly
23
- );
24
- } catch (a) {
25
- const s = a instanceof Error ? a.message : String(a);
26
- throw new e(`Signature verification failed: ${s}`);
1998
+ const s = Uint8Array.from(atob(r), (c) => c.charCodeAt(0));
1999
+ if (s.length !== 65)
2000
+ throw new Error("Invalid signature length");
2001
+ const i = er(e, n), o = nr(i, s);
2002
+ for (const c of o)
2003
+ if (rr(t, c))
2004
+ return !0;
2005
+ return !1;
2006
+ } catch (s) {
2007
+ const i = s instanceof Error ? s.message : String(s);
2008
+ throw new C(`Signature verification failed: ${i}`);
27
2009
  }
28
2010
  }
29
- function b(t = 16) {
30
- return f(t).toString("hex");
2011
+ function or(e = 16) {
2012
+ return Ye(e).toString("hex");
31
2013
  }
32
- function k(t) {
33
- if (!t.callbackUrl)
34
- throw new e("Callback URL is required.");
35
- let r;
2014
+ function ar(e) {
2015
+ if (!e.callbackUrl)
2016
+ throw new C("Callback URL is required.");
2017
+ let t;
36
2018
  try {
37
- r = new URL(t.callbackUrl);
2019
+ t = new URL(e.callbackUrl);
38
2020
  } catch (o) {
39
- throw new e(`Invalid callback URL: ${o.message}`);
40
- }
41
- const i = r.host + r.pathname, c = t.nonce || b(), a = t.unsecure ? "1" : "0";
42
- if (t.unsecure && r.protocol !== "http:")
43
- throw new e("Unsecure flag is true, but callback URL does not use http protocol.");
44
- if (!t.unsecure && r.protocol !== "https:")
45
- throw new e("Callback URL must use https protocol unless unsecure flag is set to true.");
46
- return `digiid://${i}?x=${c}&u=${a}`;
47
- }
48
- async function R(t, r) {
49
- const { address: i, uri: c, signature: a } = t, { expectedCallbackUrl: s, expectedNonce: o } = r;
50
- if (!i || !c || !a)
51
- throw new e("Missing required callback data: address, uri, or signature.");
52
- let l;
2021
+ throw new C(`Invalid callback URL: ${o.message}`);
2022
+ }
2023
+ const r = t.host + t.pathname, n = e.nonce || or(), s = e.unsecure ? "1" : "0";
2024
+ if (e.unsecure && t.protocol !== "http:")
2025
+ throw new C("Unsecure flag is true, but callback URL does not use http protocol.");
2026
+ if (!e.unsecure && t.protocol !== "https:")
2027
+ throw new C("Callback URL must use https protocol unless unsecure flag is set to true.");
2028
+ return `digiid://${r}?x=${n}&u=${s}`;
2029
+ }
2030
+ async function ur(e, t) {
2031
+ const { address: r, uri: n, signature: s } = e, { expectedCallbackUrl: i, expectedNonce: o } = t;
2032
+ if (!r || !n || !s)
2033
+ throw new C("Missing required callback data: address, uri, or signature.");
2034
+ let c;
53
2035
  try {
54
- const n = c.replace(/^digiid:/, "http:");
55
- l = new URL(n);
56
- } catch (n) {
57
- throw new e(`Invalid URI received in callback: ${n.message}`);
58
- }
59
- const u = l.searchParams.get("x"), h = l.searchParams.get("u"), g = l.host + l.pathname;
60
- if (u === null || h === null)
61
- throw new e("URI missing nonce (x) or unsecure (u) parameter.");
2036
+ const x = n.replace(/^digiid:/, "http:");
2037
+ c = new URL(x);
2038
+ } catch (x) {
2039
+ throw new C(`Invalid URI received in callback: ${x.message}`);
2040
+ }
2041
+ const f = c.searchParams.get("x"), a = c.searchParams.get("u"), h = c.host + c.pathname;
2042
+ if (f === null || a === null)
2043
+ throw new C("URI missing nonce (x) or unsecure (u) parameter.");
62
2044
  let d;
63
2045
  try {
64
- d = typeof s == "string" ? new URL(s) : s;
65
- } catch (n) {
66
- throw new e(`Invalid expectedCallbackUrl provided: ${n.message}`);
67
- }
68
- const p = d.host + d.pathname;
69
- if (g !== p)
70
- throw new e(`Callback URL mismatch: URI contained "${g}", expected "${p}"`);
71
- const w = d.protocol;
72
- if (h === "1" && w !== "http:")
73
- throw new e("URI indicates unsecure (u=1), but expectedCallbackUrl is not http.");
74
- if (h === "0" && w !== "https:")
75
- throw new e("URI indicates secure (u=0), but expectedCallbackUrl is not https.");
76
- if (o && u !== o)
77
- throw new e(`Nonce mismatch: URI contained "${u}", expected "${o}". Possible replay attack.`);
2046
+ d = typeof i == "string" ? new URL(i) : i;
2047
+ } catch (x) {
2048
+ throw new C(`Invalid expectedCallbackUrl provided: ${x.message}`);
2049
+ }
2050
+ const y = d.host + d.pathname;
2051
+ if (h !== y)
2052
+ throw new C(`Callback URL mismatch: URI contained "${h}", expected "${y}"`);
2053
+ const g = d.protocol;
2054
+ if (a === "1" && g !== "http:")
2055
+ throw new C("URI indicates unsecure (u=1), but expectedCallbackUrl is not http.");
2056
+ if (a === "0" && g !== "https:")
2057
+ throw new C("URI indicates secure (u=0), but expectedCallbackUrl is not https.");
2058
+ if (o && f !== o)
2059
+ throw new C(`Nonce mismatch: URI contained "${f}", expected "${o}". Possible replay attack.`);
78
2060
  try {
79
- if (!await U(c, i, a))
80
- throw new e("Invalid signature.");
81
- } catch (n) {
82
- throw n instanceof e ? n : new e(`Unexpected error during signature verification: ${n.message}`);
2061
+ if (!await sr(n, r, s))
2062
+ throw new C("Invalid signature.");
2063
+ } catch (x) {
2064
+ throw x instanceof C ? x : new C(`Unexpected error during signature verification: ${x.message}`);
83
2065
  }
84
2066
  return {
85
2067
  isValid: !0,
86
- address: i,
87
- nonce: u
2068
+ address: r,
2069
+ nonce: f
88
2070
  // Return the nonce from the URI
89
2071
  };
90
2072
  }
91
2073
  export {
92
- e as DigiIDError,
93
- U as _internalVerifySignature,
94
- k as generateDigiIDUri,
95
- R as verifyDigiIDCallback
2074
+ C as DigiIDError,
2075
+ sr as _internalVerifySignature,
2076
+ ar as generateDigiIDUri,
2077
+ ur as verifyDigiIDCallback
96
2078
  };
97
2079
  //# sourceMappingURL=digiid-ts.es.js.map