From ab004dfddbafb7f8e340f9ea9662700d32ac88ad Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Tue, 21 Oct 2025 15:12:15 +0200 Subject: [PATCH 1/8] Add URLs to ENV vars with network type --- src/vite-env.d.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 1d144581d..a549771f3 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -4,8 +4,26 @@ interface ImportMetaEnv { readonly BASE_URL: string readonly VITE_SENTRY_DSN?: string - readonly VITE_ARK_SERVER?: string - readonly VITE_BOLTZ_URL?: string + readonly VITE_ARK_SERVER_URL_BITCOIN?: string + readonly VITE_ARK_SERVER_URL_TESTNET?: string + readonly VITE_ARK_SERVER_URL_MUTINYNET?: string + readonly VITE_ARK_SERVER_URL_REGTEST?: string + readonly VITE_ARK_SERVER_URL_SIGNET?: string + readonly VITE_BOLTZ_URL_BITCOIN?: string + readonly VITE_BOLTZ_URL_TESTNET?: string + readonly VITE_BOLTZ_URL_MUTINYNET?: string + readonly VITE_BOLTZ_URL_REGTEST?: string + readonly VITE_BOLTZ_URL_SIGNET?: string + readonly VITE_ESPLORA_URL_BITCOIN?: string + readonly VITE_ESPLORA_URL_TESTNET?: string + readonly VITE_ESPLORA_URL_MUTINYNET?: string + readonly VITE_ESPLORA_URL_REGTEST?: string + readonly VITE_ESPLORA_URL_SIGNET?: string + readonly VITE_INDEXER_URL_BITCOIN?: string + readonly VITE_INDEXER_URL_TESTNET?: string + readonly VITE_INDEXER_URL_MUTINYNET?: string + readonly VITE_INDEXER_URL_REGTEST?: string + readonly VITE_INDEXER_URL_SIGNET?: string // Add other env variables as needed } From 56924cd279173ac508ca788ea3333a2c30b5af23 Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Tue, 21 Oct 2025 21:01:39 +0200 Subject: [PATCH 2/8] SSOT for service urls --- src/lib/urls.ts | 134 ++++++++++++++++++++++++++++++++++++ src/providers/lightning.tsx | 10 +-- src/vite-env.d.ts | 4 -- 3 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 src/lib/urls.ts diff --git a/src/lib/urls.ts b/src/lib/urls.ts new file mode 100644 index 000000000..b9dac8c13 --- /dev/null +++ b/src/lib/urls.ts @@ -0,0 +1,134 @@ +import { Network } from '@arkade-os/boltz-swap' + +const DEFAULT_ARK_SERVER_URLS: Partial> = {} + +const DEFAULT_BOLTZ_SERVER_URLS: Partial> = { + mutinynet: 'https://api.boltz.mutinynet.arkade.sh', + signet: 'https://boltz.signet.arkade.sh', + regtest: 'http://localhost:9069', +} + +const DEFAULT_ESPLORA_SERVER_URLS: Partial> = {} + +const DEFAULT_INDEXER_SERVER_URLS: Partial> = {} + +export function getArkUrl(network: Network): string { + const defaultUrl = DEFAULT_ARK_SERVER_URLS[network] + if (network === 'bitcoin') { + return importArkBitcoinUrl() ?? defaultUrl + } + if (network === 'mutinynet') { + return importArkMutinyUrl() ?? defaultUrl + } + if (network === 'regtest') { + return importArkRegTestUrl() ?? defaultUrl + } + return importArkSigNetUrl() ?? defaultUrl +} + +export function getBoltzUrl(network: Network): string { + const defaultUrl = DEFAULT_BOLTZ_SERVER_URLS[network] + + if (network === 'bitcoin') { + return importBoltzBitcoinUrl() ?? defaultUrl + } + if (network === 'mutinynet') { + return importBoltzMutinyUrl() ?? defaultUrl + } + if (network === 'regtest') { + return importBoltzRegTestUrl() ?? defaultUrl + } + return importBoltzSigNetUrl() ?? defaultUrl +} + +export function getEsploraUrl(network: Network): string { + const defaultUrl = DEFAULT_ESPLORA_SERVER_URLS[network] + if (network === 'bitcoin') { + return importEsploraBitcoinUrl() ?? defaultUrl + } + if (network === 'mutinynet') { + return importEsploraMutinyUrl() ?? defaultUrl + } + if (network === 'regtest') { + return importEsploraRegTestUrl() ?? defaultUrl + } + return importEsploraSigNetUrl() ?? defaultUrl +} + +export function getIndexerUrl(network: Network): string { + const defaultUrl = DEFAULT_INDEXER_SERVER_URLS[network] + if (network === 'bitcoin') { + return importIndexerBitcoinUrl() ?? defaultUrl + } + if (network === 'mutinynet') { + return importIndexerMutinyUrl() ?? defaultUrl + } + if (network === 'regtest') { + return importIndexerRegTestUrl() ?? defaultUrl + } + return importIndexerSigNetUrl() ?? defaultUrl +} + +function importArkBitcoinUrl(): string { + return import.meta.env.VITE_ARK_SERVER_URL_BITCOIN ?? '' +} + +function importArkMutinyUrl(): string { + return import.meta.env.VITE_ARK_SERVER_URL_MUTINYNET ?? '' +} + +function importArkSigNetUrl(): string { + return import.meta.env.VITE_ARK_SERVER_URL_SIGNET ?? '' +} + +function importArkRegTestUrl(): string { + return import.meta.env.VITE_ARK_SERVER_URL_REGTEST ?? '' +} + +function importBoltzBitcoinUrl(): string { + return import.meta.env.VITE_BOLTZ_URL_BITCOIN ?? '' +} + +function importBoltzMutinyUrl(): string { + return import.meta.env.VITE_BOLTZ_URL_MUTINYNET ?? '' +} + +function importBoltzSigNetUrl(): string { + return import.meta.env.VITE_BOLTZ_URL_SIGNET ?? '' +} + +function importBoltzRegTestUrl(): string { + return import.meta.env.VITE_BOLTZ_URL_REGTEST ?? '' +} + +function importEsploraBitcoinUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_BITCOIN ?? '' +} + +function importEsploraMutinyUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_MUTINYNET ?? '' +} + +function importEsploraSigNetUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_SIGNET ?? '' +} + +function importEsploraRegTestUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_REGTEST ?? '' +} + +function importIndexerBitcoinUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_BITCOIN ?? '' +} + +function importIndexerMutinyUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_MUTINYNET ?? '' +} + +function importIndexerSigNetUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_SIGNET ?? '' +} + +function importIndexerRegTestUrl(): string { + return import.meta.env.VITE_ESPLORA_URL_REGTEST ?? '' +} diff --git a/src/providers/lightning.tsx b/src/providers/lightning.tsx index 10ff44acb..9a780c78f 100644 --- a/src/providers/lightning.tsx +++ b/src/providers/lightning.tsx @@ -5,13 +5,7 @@ import { WalletContext } from './wallet' import { FeesResponse, Network } from '@arkade-os/boltz-swap' import { ConfigContext } from './config' import { consoleError } from '../lib/logs' - -const BASE_URLS: Record = { - bitcoin: import.meta.env.VITE_BOLTZ_URL ?? 'https://boltz-v8.arkade.sh', - mutinynet: 'https://api.boltz.mutinynet.arkade.sh', - signet: 'https://boltz.signet.arkade.sh', - regtest: 'http://localhost:9069', -} +import { getBoltzUrl } from '../lib/urls' interface LightningContextProps { connected: boolean @@ -42,7 +36,7 @@ export const LightningProvider = ({ children }: { children: ReactNode }) => { // create swap provider on first run with svcWallet useEffect(() => { if (!aspInfo.network || !svcWallet) return - const baseUrl = BASE_URLS[aspInfo.network as Network] + const baseUrl = getBoltzUrl(aspInfo.network as Network) if (!baseUrl) return // No boltz server for this network setSwapProvider(new LightningSwapProvider(baseUrl, aspInfo, svcWallet)) setConnected(config.apps.boltz.connected) diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index a549771f3..02b56df1f 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -5,22 +5,18 @@ interface ImportMetaEnv { readonly BASE_URL: string readonly VITE_SENTRY_DSN?: string readonly VITE_ARK_SERVER_URL_BITCOIN?: string - readonly VITE_ARK_SERVER_URL_TESTNET?: string readonly VITE_ARK_SERVER_URL_MUTINYNET?: string readonly VITE_ARK_SERVER_URL_REGTEST?: string readonly VITE_ARK_SERVER_URL_SIGNET?: string readonly VITE_BOLTZ_URL_BITCOIN?: string - readonly VITE_BOLTZ_URL_TESTNET?: string readonly VITE_BOLTZ_URL_MUTINYNET?: string readonly VITE_BOLTZ_URL_REGTEST?: string readonly VITE_BOLTZ_URL_SIGNET?: string readonly VITE_ESPLORA_URL_BITCOIN?: string - readonly VITE_ESPLORA_URL_TESTNET?: string readonly VITE_ESPLORA_URL_MUTINYNET?: string readonly VITE_ESPLORA_URL_REGTEST?: string readonly VITE_ESPLORA_URL_SIGNET?: string readonly VITE_INDEXER_URL_BITCOIN?: string - readonly VITE_INDEXER_URL_TESTNET?: string readonly VITE_INDEXER_URL_MUTINYNET?: string readonly VITE_INDEXER_URL_REGTEST?: string readonly VITE_INDEXER_URL_SIGNET?: string From 69e29d28179506ba1e5e3600e6ce3be30a7b06b2 Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Tue, 21 Oct 2025 22:22:41 +0200 Subject: [PATCH 3/8] Fixes and improvements --- public/wallet-service-worker.mjs | 6857 ++++++++++++++++++------------ src/lib/urls.ts | 132 +- 2 files changed, 4327 insertions(+), 2662 deletions(-) diff --git a/public/wallet-service-worker.mjs b/public/wallet-service-worker.mjs index c9a4a5cbd..8436e7169 100644 --- a/public/wallet-service-worker.mjs +++ b/public/wallet-service-worker.mjs @@ -1,80 +1,80 @@ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -function ko(e) { +function ys(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function fe(e, t = "") { +function Te(e, t = "") { if (!Number.isSafeInteger(e) || e < 0) { const n = t && `"${t}" `; - throw new Error(`${n}expected integer >0, got ${e}`); + throw new Error(`${n}expected integer >= 0, got ${e}`); } } -function V(e, t, n = "") { - const r = ko(e), o = e?.length, s = t !== void 0; +function G(e, t, n = "") { + const r = ys(e), o = e?.length, s = t !== void 0; if (!r || s && o !== t) { const i = n && `"${n}" `, c = s ? ` of length ${t}` : "", a = r ? `length=${o}` : `type=${typeof e}`; throw new Error(i + "expected Uint8Array" + c + ", got " + a); } return e; } -function di(e) { +function vc(e) { if (typeof e != "function" || typeof e.create != "function") throw new Error("Hash must wrapped by utils.createHasher"); - fe(e.outputLen), fe(e.blockLen); + Te(e.outputLen), Te(e.blockLen); } -function zn(e, t = !0) { +function yr(e, t = !0) { if (e.destroyed) throw new Error("Hash instance has been destroyed"); if (t && e.finished) throw new Error("Hash#digest() has already been called"); } -function ca(e, t) { - V(e, void 0, "digestInto() output"); +function Gu(e, t) { + G(e, void 0, "digestInto() output"); const n = t.outputLen; if (e.length < n) throw new Error('"digestInto() output" expected to be of length >=' + n); } -function De(...e) { +function rn(...e) { for (let t = 0; t < e.length; t++) e[t].fill(0); } -function Ur(e) { +function ho(e) { return new DataView(e.buffer, e.byteOffset, e.byteLength); } -function Mt(e, t) { +function qt(e, t) { return e << 32 - t | e >>> t; } -function Bn(e, t) { +function Zn(e, t) { return e << t | e >>> 32 - t >>> 0; } -const hi = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", aa = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); -function yr(e) { - if (V(e), hi) +const Tc = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", qu = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); +function Yr(e) { + if (G(e), Tc) return e.toHex(); let t = ""; for (let n = 0; n < e.length; n++) - t += aa[e[n]]; + t += qu[e[n]]; return t; } -const Zt = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; -function as(e) { - if (e >= Zt._0 && e <= Zt._9) - return e - Zt._0; - if (e >= Zt.A && e <= Zt.F) - return e - (Zt.A - 10); - if (e >= Zt.a && e <= Zt.f) - return e - (Zt.a - 10); -} -function jn(e) { +const ne = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; +function ci(e) { + if (e >= ne._0 && e <= ne._9) + return e - ne._0; + if (e >= ne.A && e <= ne.F) + return e - (ne.A - 10); + if (e >= ne.a && e <= ne.f) + return e - (ne.a - 10); +} +function mr(e) { if (typeof e != "string") throw new Error("hex string expected, got " + typeof e); - if (hi) + if (Tc) return Uint8Array.fromHex(e); const t = e.length, n = t / 2; if (t % 2) throw new Error("hex string expected, got unpadded hex of length " + t); const r = new Uint8Array(n); for (let o = 0, s = 0; o < n; o++, s += 2) { - const i = as(e.charCodeAt(s)), c = as(e.charCodeAt(s + 1)); + const i = ci(e.charCodeAt(s)), c = ci(e.charCodeAt(s + 1)); if (i === void 0 || c === void 0) { const a = e[s] + e[s + 1]; throw new Error('hex string expected, got non-hex character "' + a + '" at index ' + s); @@ -83,11 +83,11 @@ function jn(e) { } return r; } -function Lt(...e) { +function Kt(...e) { let t = 0; for (let r = 0; r < e.length; r++) { const o = e[r]; - V(o), t += o.length; + G(o), t += o.length; } const n = new Uint8Array(t); for (let r = 0, o = 0; r < e.length; r++) { @@ -96,26 +96,26 @@ function Lt(...e) { } return n; } -function pi(e, t = {}) { +function Ac(e, t = {}) { const n = (o, s) => e(s).update(o).digest(), r = e(void 0); return n.outputLen = r.outputLen, n.blockLen = r.blockLen, n.create = (o) => e(o), Object.assign(n, t), Object.freeze(n); } -function En(e = 32) { +function Wn(e = 32) { const t = typeof globalThis == "object" ? globalThis.crypto : null; if (typeof t?.getRandomValues != "function") throw new Error("crypto.getRandomValues must be defined"); return t.getRandomValues(new Uint8Array(e)); } -const ua = (e) => ({ +const ju = (e) => ({ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, e]) }); -function fa(e, t, n) { +function Yu(e, t, n) { return e & t ^ ~e & n; } -function la(e, t, n) { +function Zu(e, t, n) { return e & t ^ e & n ^ t & n; } -class gi { +let Ic = class { blockLen; outputLen; padOffset; @@ -128,15 +128,15 @@ class gi { pos = 0; destroyed = !1; constructor(t, n, r, o) { - this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = Ur(this.buffer); + this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = ho(this.buffer); } update(t) { - zn(this), V(t); + yr(this), G(t); const { view: n, buffer: r, blockLen: o } = this, s = t.length; for (let i = 0; i < s; ) { const c = Math.min(o - this.pos, s - i); if (c === o) { - const a = Ur(t); + const a = ho(t); for (; o <= s - i; i += o) this.process(a, i); continue; @@ -146,14 +146,14 @@ class gi { return this.length += t.length, this.roundClean(), this; } digestInto(t) { - zn(this), ca(t, this), this.finished = !0; + yr(this), Gu(t, this), this.finished = !0; const { buffer: n, view: r, blockLen: o, isLE: s } = this; let { pos: i } = this; - n[i++] = 128, De(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(r, 0), i = 0); + n[i++] = 128, rn(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(r, 0), i = 0); for (let l = i; l < o; l++) n[l] = 0; r.setBigUint64(o - 8, BigInt(this.length * 8), s), this.process(r, 0); - const c = Ur(t), a = this.outputLen; + const c = ho(t), a = this.outputLen; if (a % 4) throw new Error("_sha2: outputLen must be aligned to 32bit"); const u = a / 4, f = this.get(); @@ -176,8 +176,8 @@ class gi { clone() { return this._cloneInto(); } -} -const re = /* @__PURE__ */ Uint32Array.from([ +}; +const he = /* @__PURE__ */ Uint32Array.from([ 1779033703, 3144134277, 1013904242, @@ -186,7 +186,7 @@ const re = /* @__PURE__ */ Uint32Array.from([ 2600822924, 528734635, 1541459225 -]), da = /* @__PURE__ */ Uint32Array.from([ +]), Xu = /* @__PURE__ */ Uint32Array.from([ 1116352408, 1899447441, 3049323471, @@ -251,8 +251,8 @@ const re = /* @__PURE__ */ Uint32Array.from([ 2756734187, 3204031479, 3329325298 -]), oe = /* @__PURE__ */ new Uint32Array(64); -class ha extends gi { +]), pe = /* @__PURE__ */ new Uint32Array(64); +let Qu = class extends Ic { constructor(t) { super(64, t, 8, !1); } @@ -266,87 +266,86 @@ class ha extends gi { } process(t, n) { for (let l = 0; l < 16; l++, n += 4) - oe[l] = t.getUint32(n, !1); + pe[l] = t.getUint32(n, !1); for (let l = 16; l < 64; l++) { - const d = oe[l - 15], h = oe[l - 2], w = Mt(d, 7) ^ Mt(d, 18) ^ d >>> 3, p = Mt(h, 17) ^ Mt(h, 19) ^ h >>> 10; - oe[l] = p + oe[l - 7] + w + oe[l - 16] | 0; + const d = pe[l - 15], h = pe[l - 2], w = qt(d, 7) ^ qt(d, 18) ^ d >>> 3, g = qt(h, 17) ^ qt(h, 19) ^ h >>> 10; + pe[l] = g + pe[l - 7] + w + pe[l - 16] | 0; } let { A: r, B: o, C: s, D: i, E: c, F: a, G: u, H: f } = this; for (let l = 0; l < 64; l++) { - const d = Mt(c, 6) ^ Mt(c, 11) ^ Mt(c, 25), h = f + d + fa(c, a, u) + da[l] + oe[l] | 0, p = (Mt(r, 2) ^ Mt(r, 13) ^ Mt(r, 22)) + la(r, o, s) | 0; - f = u, u = a, a = c, c = i + h | 0, i = s, s = o, o = r, r = h + p | 0; + const d = qt(c, 6) ^ qt(c, 11) ^ qt(c, 25), h = f + d + Yu(c, a, u) + Xu[l] + pe[l] | 0, g = (qt(r, 2) ^ qt(r, 13) ^ qt(r, 22)) + Zu(r, o, s) | 0; + f = u, u = a, a = c, c = i + h | 0, i = s, s = o, o = r, r = h + g | 0; } r = r + this.A | 0, o = o + this.B | 0, s = s + this.C | 0, i = i + this.D | 0, c = c + this.E | 0, a = a + this.F | 0, u = u + this.G | 0, f = f + this.H | 0, this.set(r, o, s, i, c, a, u, f); } roundClean() { - De(oe); + rn(pe); } destroy() { - this.set(0, 0, 0, 0, 0, 0, 0, 0), De(this.buffer); + this.set(0, 0, 0, 0, 0, 0, 0, 0), rn(this.buffer); } -} -class pa extends ha { +}, Ju = class extends Qu { // We cannot use array here since array allows indexing by variable // which means optimizer/compiler cannot use registers. - A = re[0] | 0; - B = re[1] | 0; - C = re[2] | 0; - D = re[3] | 0; - E = re[4] | 0; - F = re[5] | 0; - G = re[6] | 0; - H = re[7] | 0; + A = he[0] | 0; + B = he[1] | 0; + C = he[2] | 0; + D = he[3] | 0; + E = he[4] | 0; + F = he[5] | 0; + G = he[6] | 0; + H = he[7] | 0; constructor() { super(32); } -} -const ht = /* @__PURE__ */ pi( - () => new pa(), - /* @__PURE__ */ ua(1) +}; +const wt = /* @__PURE__ */ Ac( + () => new Ju(), + /* @__PURE__ */ ju(1) ); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const Ao = /* @__PURE__ */ BigInt(0), Wr = /* @__PURE__ */ BigInt(1); -function qn(e, t = "") { +const ms = /* @__PURE__ */ BigInt(0), Ro = /* @__PURE__ */ BigInt(1); +function br(e, t = "") { if (typeof e != "boolean") { const n = t && `"${t}" `; throw new Error(n + "expected boolean, got type=" + typeof e); } return e; } -function wi(e) { +function kc(e) { if (typeof e == "bigint") { - if (!Hn(e)) + if (!fr(e)) throw new Error("positive bigint expected, got " + e); } else - fe(e); + Te(e); return e; } -function On(e) { - const t = wi(e).toString(16); +function Xn(e) { + const t = kc(e).toString(16); return t.length & 1 ? "0" + t : t; } -function yi(e) { +function Bc(e) { if (typeof e != "string") throw new Error("hex string expected, got " + typeof e); - return e === "" ? Ao : BigInt("0x" + e); + return e === "" ? ms : BigInt("0x" + e); } -function qt(e) { - return yi(yr(e)); +function le(e) { + return Bc(Yr(e)); } -function mi(e) { - return yi(yr(ga(V(e)).reverse())); +function Oc(e) { + return Bc(Yr(tf(G(e)).reverse())); } -function Sn(e, t) { - fe(t), e = wi(e); - const n = jn(e.toString(16).padStart(t * 2, "0")); +function zn(e, t) { + Te(t), e = kc(e); + const n = mr(e.toString(16).padStart(t * 2, "0")); if (n.length !== t) throw new Error("number too large"); return n; } -function xi(e, t) { - return Sn(e, t).reverse(); +function $c(e, t) { + return zn(e, t).reverse(); } -function ln(e, t) { +function Ln(e, t) { if (e.length !== t.length) return !1; let n = 0; @@ -354,10 +353,10 @@ function ln(e, t) { n |= e[r] ^ t[r]; return n === 0; } -function ga(e) { +function tf(e) { return Uint8Array.from(e); } -function wa(e) { +function ef(e) { return Uint8Array.from(e, (t, n) => { const r = t.charCodeAt(0); if (t.length !== 1 || r > 127) @@ -365,51 +364,51 @@ function wa(e) { return r; }); } -const Hn = (e) => typeof e == "bigint" && Ao <= e; -function ya(e, t, n) { - return Hn(e) && Hn(t) && Hn(n) && t <= e && e < n; +const fr = (e) => typeof e == "bigint" && ms <= e; +function nf(e, t, n) { + return fr(e) && fr(t) && fr(n) && t <= e && e < n; } -function bi(e, t, n, r) { - if (!ya(t, n, r)) +function Uc(e, t, n, r) { + if (!nf(t, n, r)) throw new Error("expected valid " + e + ": " + n + " <= n < " + r + ", got " + t); } -function ma(e) { +function rf(e) { let t; - for (t = 0; e > Ao; e >>= Wr, t += 1) + for (t = 0; e > ms; e >>= Ro, t += 1) ; return t; } -const Io = (e) => (Wr << BigInt(e)) - Wr; -function xa(e, t, n) { - if (fe(e, "hashLen"), fe(t, "qByteLen"), typeof n != "function") +const bs = (e) => (Ro << BigInt(e)) - Ro; +function of(e, t, n) { + if (Te(e, "hashLen"), Te(t, "qByteLen"), typeof n != "function") throw new Error("hmacFn must be a function"); - const r = (m) => new Uint8Array(m), o = Uint8Array.of(), s = Uint8Array.of(0), i = Uint8Array.of(1), c = 1e3; + const r = (y) => new Uint8Array(y), o = Uint8Array.of(), s = Uint8Array.of(0), i = Uint8Array.of(1), c = 1e3; let a = r(e), u = r(e), f = 0; const l = () => { a.fill(1), u.fill(0), f = 0; - }, d = (...m) => n(u, Lt(a, ...m)), h = (m = o) => { - u = d(s, m), a = d(), m.length !== 0 && (u = d(i, m), a = d()); + }, d = (...y) => n(u, Kt(a, ...y)), h = (y = o) => { + u = d(s, y), a = d(), y.length !== 0 && (u = d(i, y), a = d()); }, w = () => { if (f++ >= c) throw new Error("drbg: tried max amount of iterations"); - let m = 0; - const x = []; - for (; m < t; ) { + let y = 0; + const S = []; + for (; y < t; ) { a = d(); - const b = a.slice(); - x.push(b), m += a.length; + const v = a.slice(); + S.push(v), y += a.length; } - return Lt(...x); + return Kt(...S); }; - return (m, x) => { - l(), h(m); - let b; - for (; !(b = x(w())); ) + return (y, S) => { + l(), h(y); + let v; + for (; !(v = S(w())); ) h(); - return l(), b; + return l(), v; }; } -function Bo(e, t = {}, n = {}) { +function Es(e, t = {}, n = {}) { if (!e || typeof e != "object") throw new Error("expected valid options object"); function r(s, i, c) { @@ -423,7 +422,7 @@ function Bo(e, t = {}, n = {}) { const o = (s, i) => Object.entries(s).forEach(([c, a]) => r(c, a, i)); o(t, !1), o(n, !0); } -function us(e) { +function ai(e) { const t = /* @__PURE__ */ new WeakMap(); return (n, ...r) => { const o = t.get(n); @@ -434,91 +433,91 @@ function us(e) { }; } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const yt = /* @__PURE__ */ BigInt(0), gt = /* @__PURE__ */ BigInt(1), Ee = /* @__PURE__ */ BigInt(2), Ei = /* @__PURE__ */ BigInt(3), Si = /* @__PURE__ */ BigInt(4), Ti = /* @__PURE__ */ BigInt(5), ba = /* @__PURE__ */ BigInt(7), vi = /* @__PURE__ */ BigInt(8), Ea = /* @__PURE__ */ BigInt(9), ki = /* @__PURE__ */ BigInt(16); -function Ct(e, t) { +const xt = /* @__PURE__ */ BigInt(0), mt = /* @__PURE__ */ BigInt(1), Ce = /* @__PURE__ */ BigInt(2), Rc = /* @__PURE__ */ BigInt(3), Nc = /* @__PURE__ */ BigInt(4), Lc = /* @__PURE__ */ BigInt(5), sf = /* @__PURE__ */ BigInt(7), Cc = /* @__PURE__ */ BigInt(8), cf = /* @__PURE__ */ BigInt(9), _c = /* @__PURE__ */ BigInt(16); +function Vt(e, t) { const n = e % t; - return n >= yt ? n : t + n; + return n >= xt ? n : t + n; } -function It(e, t, n) { +function Rt(e, t, n) { let r = e; - for (; t-- > yt; ) + for (; t-- > xt; ) r *= r, r %= n; return r; } -function fs(e, t) { - if (e === yt) +function ui(e, t) { + if (e === xt) throw new Error("invert: expected non-zero number"); - if (t <= yt) + if (t <= xt) throw new Error("invert: expected positive modulus, got " + t); - let n = Ct(e, t), r = t, o = yt, s = gt; - for (; n !== yt; ) { + let n = Vt(e, t), r = t, o = xt, s = mt; + for (; n !== xt; ) { const c = r / n, a = r % n, u = o - s * c; r = n, n = a, o = s, s = u; } - if (r !== gt) + if (r !== mt) throw new Error("invert: does not exist"); - return Ct(o, t); + return Vt(o, t); } -function Oo(e, t, n) { +function xs(e, t, n) { if (!e.eql(e.sqr(t), n)) throw new Error("Cannot find square root"); } -function Ai(e, t) { - const n = (e.ORDER + gt) / Si, r = e.pow(t, n); - return Oo(e, r, t), r; +function Pc(e, t) { + const n = (e.ORDER + mt) / Nc, r = e.pow(t, n); + return xs(e, r, t), r; } -function Sa(e, t) { - const n = (e.ORDER - Ti) / vi, r = e.mul(t, Ee), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, Ee), o), c = e.mul(s, e.sub(i, e.ONE)); - return Oo(e, c, t), c; +function af(e, t) { + const n = (e.ORDER - Lc) / Cc, r = e.mul(t, Ce), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, Ce), o), c = e.mul(s, e.sub(i, e.ONE)); + return xs(e, c, t), c; } -function Ta(e) { - const t = mr(e), n = Ii(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + ba) / ki; +function uf(e) { + const t = Zr(e), n = Hc(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + sf) / _c; return (c, a) => { let u = c.pow(a, i), f = c.mul(u, r); const l = c.mul(u, o), d = c.mul(u, s), h = c.eql(c.sqr(f), a), w = c.eql(c.sqr(l), a); u = c.cmov(u, f, h), f = c.cmov(d, l, w); - const p = c.eql(c.sqr(f), a), m = c.cmov(u, f, p); - return Oo(c, m, a), m; + const g = c.eql(c.sqr(f), a), y = c.cmov(u, f, g); + return xs(c, y, a), y; }; } -function Ii(e) { - if (e < Ei) +function Hc(e) { + if (e < Rc) throw new Error("sqrt is not defined for small field"); - let t = e - gt, n = 0; - for (; t % Ee === yt; ) - t /= Ee, n++; - let r = Ee; - const o = mr(e); - for (; ls(o, r) === 1; ) + let t = e - mt, n = 0; + for (; t % Ce === xt; ) + t /= Ce, n++; + let r = Ce; + const o = Zr(e); + for (; fi(o, r) === 1; ) if (r++ > 1e3) throw new Error("Cannot find square root: probably non-prime P"); if (n === 1) - return Ai; + return Pc; let s = o.pow(r, t); - const i = (t + gt) / Ee; + const i = (t + mt) / Ce; return function(a, u) { if (a.is0(u)) return u; - if (ls(a, u) !== 1) + if (fi(a, u) !== 1) throw new Error("Cannot find square root"); let f = n, l = a.mul(a.ONE, s), d = a.pow(u, t), h = a.pow(u, i); for (; !a.eql(d, a.ONE); ) { if (a.is0(d)) return a.ZERO; - let w = 1, p = a.sqr(d); - for (; !a.eql(p, a.ONE); ) - if (w++, p = a.sqr(p), w === f) + let w = 1, g = a.sqr(d); + for (; !a.eql(g, a.ONE); ) + if (w++, g = a.sqr(g), w === f) throw new Error("Cannot find square root"); - const m = gt << BigInt(f - w - 1), x = a.pow(l, m); - f = w, l = a.sqr(x), d = a.mul(d, l), h = a.mul(h, x); + const y = mt << BigInt(f - w - 1), S = a.pow(l, y); + f = w, l = a.sqr(S), d = a.mul(d, l), h = a.mul(h, S); } return h; }; } -function va(e) { - return e % Si === Ei ? Ai : e % vi === Ti ? Sa : e % ki === Ea ? Ta(e) : Ii(e); +function ff(e) { + return e % Nc === Rc ? Pc : e % Cc === Lc ? af : e % _c === cf ? uf(e) : Hc(e); } -const ka = [ +const lf = [ "create", "isValid", "is0", @@ -537,103 +536,103 @@ const ka = [ "mulN", "sqrN" ]; -function Aa(e) { +function df(e) { const t = { ORDER: "bigint", BYTES: "number", BITS: "number" - }, n = ka.reduce((r, o) => (r[o] = "function", r), t); - return Bo(e, n), e; + }, n = lf.reduce((r, o) => (r[o] = "function", r), t); + return Es(e, n), e; } -function Ia(e, t, n) { - if (n < yt) +function hf(e, t, n) { + if (n < xt) throw new Error("invalid exponent, negatives unsupported"); - if (n === yt) + if (n === xt) return e.ONE; - if (n === gt) + if (n === mt) return t; let r = e.ONE, o = t; - for (; n > yt; ) - n & gt && (r = e.mul(r, o)), o = e.sqr(o), n >>= gt; + for (; n > xt; ) + n & mt && (r = e.mul(r, o)), o = e.sqr(o), n >>= mt; return r; } -function Bi(e, t, n = !1) { +function Vc(e, t, n = !1) { const r = new Array(t.length).fill(n ? e.ZERO : void 0), o = t.reduce((i, c, a) => e.is0(c) ? i : (r[a] = i, e.mul(i, c)), e.ONE), s = e.inv(o); return t.reduceRight((i, c, a) => e.is0(c) ? i : (r[a] = e.mul(i, r[a]), e.mul(i, c)), s), r; } -function ls(e, t) { - const n = (e.ORDER - gt) / Ee, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); +function fi(e, t) { + const n = (e.ORDER - mt) / Ce, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); if (!o && !s && !i) throw new Error("invalid Legendre symbol result"); return o ? 1 : s ? 0 : -1; } -function Ba(e, t) { - t !== void 0 && fe(t); +function pf(e, t) { + t !== void 0 && Te(t); const n = t !== void 0 ? t : e.toString(2).length, r = Math.ceil(n / 8); return { nBitLength: n, nByteLength: r }; } -class Oa { +let gf = class { ORDER; BITS; BYTES; isLE; - ZERO = yt; - ONE = gt; + ZERO = xt; + ONE = mt; _lengths; _sqrt; // cached sqrt _mod; constructor(t, n = {}) { - if (t <= yt) + if (t <= xt) throw new Error("invalid field: expected ORDER > 0, got " + t); let r; this.isLE = !1, n != null && typeof n == "object" && (typeof n.BITS == "number" && (r = n.BITS), typeof n.sqrt == "function" && (this.sqrt = n.sqrt), typeof n.isLE == "boolean" && (this.isLE = n.isLE), n.allowedLengths && (this._lengths = n.allowedLengths?.slice()), typeof n.modFromBytes == "boolean" && (this._mod = n.modFromBytes)); - const { nBitLength: o, nByteLength: s } = Ba(t, r); + const { nBitLength: o, nByteLength: s } = pf(t, r); if (s > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes"); this.ORDER = t, this.BITS = o, this.BYTES = s, this._sqrt = void 0, Object.preventExtensions(this); } create(t) { - return Ct(t, this.ORDER); + return Vt(t, this.ORDER); } isValid(t) { if (typeof t != "bigint") throw new Error("invalid field element: expected bigint, got " + typeof t); - return yt <= t && t < this.ORDER; + return xt <= t && t < this.ORDER; } is0(t) { - return t === yt; + return t === xt; } // is valid and invertible isValidNot0(t) { return !this.is0(t) && this.isValid(t); } isOdd(t) { - return (t & gt) === gt; + return (t & mt) === mt; } neg(t) { - return Ct(-t, this.ORDER); + return Vt(-t, this.ORDER); } eql(t, n) { return t === n; } sqr(t) { - return Ct(t * t, this.ORDER); + return Vt(t * t, this.ORDER); } add(t, n) { - return Ct(t + n, this.ORDER); + return Vt(t + n, this.ORDER); } sub(t, n) { - return Ct(t - n, this.ORDER); + return Vt(t - n, this.ORDER); } mul(t, n) { - return Ct(t * n, this.ORDER); + return Vt(t * n, this.ORDER); } pow(t, n) { - return Ia(this, t, n); + return hf(this, t, n); } div(t, n) { - return Ct(t * fs(n, this.ORDER), this.ORDER); + return Vt(t * ui(n, this.ORDER), this.ORDER); } // Same as above, but doesn't normalize sqrN(t) { @@ -649,16 +648,16 @@ class Oa { return t * n; } inv(t) { - return fs(t, this.ORDER); + return ui(t, this.ORDER); } sqrt(t) { - return this._sqrt || (this._sqrt = va(this.ORDER)), this._sqrt(this, t); + return this._sqrt || (this._sqrt = ff(this.ORDER)), this._sqrt(this, t); } toBytes(t) { - return this.isLE ? xi(t, this.BYTES) : Sn(t, this.BYTES); + return this.isLE ? $c(t, this.BYTES) : zn(t, this.BYTES); } fromBytes(t, n = !1) { - V(t); + G(t); const { _lengths: r, BYTES: o, isLE: s, ORDER: i, _mod: c } = this; if (r) { if (!r.includes(t.length) || t.length > o) @@ -668,77 +667,77 @@ class Oa { } if (t.length !== o) throw new Error("Field.fromBytes: expected " + o + " bytes, got " + t.length); - let a = s ? mi(t) : qt(t); - if (c && (a = Ct(a, i)), !n && !this.isValid(a)) + let a = s ? Oc(t) : le(t); + if (c && (a = Vt(a, i)), !n && !this.isValid(a)) throw new Error("invalid field element: outside of range 0..ORDER"); return a; } // TODO: we don't need it here, move out to separate fn invertBatch(t) { - return Bi(this, t); + return Vc(this, t); } // We can't move this out because Fp6, Fp12 implement it // and it's unclear what to return in there. cmov(t, n, r) { return r ? n : t; } +}; +function Zr(e, t = {}) { + return new gf(e, t); } -function mr(e, t = {}) { - return new Oa(e, t); -} -function Oi(e) { +function Dc(e) { if (typeof e != "bigint") throw new Error("field order must be bigint"); const t = e.toString(2).length; return Math.ceil(t / 8); } -function Ui(e) { - const t = Oi(e); +function Mc(e) { + const t = Dc(e); return t + Math.ceil(t / 2); } -function Ni(e, t, n = !1) { - V(e); - const r = e.length, o = Oi(t), s = Ui(t); +function Kc(e, t, n = !1) { + G(e); + const r = e.length, o = Dc(t), s = Mc(t); if (r < 16 || r < s || r > 1024) throw new Error("expected " + s + "-1024 bytes of input, got " + r); - const i = n ? mi(e) : qt(e), c = Ct(i, t - gt) + gt; - return n ? xi(c, o) : Sn(c, o); + const i = n ? Oc(e) : le(e), c = Vt(i, t - mt) + mt; + return n ? $c(c, o) : zn(c, o); } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const He = /* @__PURE__ */ BigInt(0), Se = /* @__PURE__ */ BigInt(1); -function Yn(e, t) { +const on = /* @__PURE__ */ BigInt(0), _e = /* @__PURE__ */ BigInt(1); +function Er(e, t) { const n = t.negate(); return e ? n : t; } -function ds(e, t) { - const n = Bi(e.Fp, t.map((r) => r.Z)); +function li(e, t) { + const n = Vc(e.Fp, t.map((r) => r.Z)); return t.map((r, o) => e.fromAffine(r.toAffine(n[o]))); } -function Ri(e, t) { +function Fc(e, t) { if (!Number.isSafeInteger(e) || e <= 0 || e > t) throw new Error("invalid window size, expected [1.." + t + "], got W=" + e); } -function Nr(e, t) { - Ri(e, t); - const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = Io(e), i = BigInt(e); +function po(e, t) { + Fc(e, t); + const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = bs(e), i = BigInt(e); return { windows: n, windowSize: r, mask: s, maxNumber: o, shiftBy: i }; } -function hs(e, t, n) { +function di(e, t, n) { const { windowSize: r, mask: o, maxNumber: s, shiftBy: i } = n; let c = Number(e & o), a = e >> i; - c > r && (c -= s, a += Se); + c > r && (c -= s, a += _e); const u = t * r, f = u + Math.abs(c) - 1, l = c === 0, d = c < 0, h = t % 2 !== 0; return { nextN: a, offset: f, isZero: l, isNeg: d, isNegF: h, offsetF: u }; } -const Rr = /* @__PURE__ */ new WeakMap(), Ci = /* @__PURE__ */ new WeakMap(); -function Cr(e) { - return Ci.get(e) || 1; +const go = /* @__PURE__ */ new WeakMap(), Wc = /* @__PURE__ */ new WeakMap(); +function wo(e) { + return Wc.get(e) || 1; } -function ps(e) { - if (e !== He) +function hi(e) { + if (e !== on) throw new Error("invalid wNAF"); } -let Ua = class { +let wf = class { BASE; ZERO; Fn; @@ -750,8 +749,8 @@ let Ua = class { // non-const time multiplication ladder _unsafeLadder(t, n, r = this.ZERO) { let o = t; - for (; n > He; ) - n & Se && (r = r.add(o)), o = o.double(), n >>= Se; + for (; n > on; ) + n & _e && (r = r.add(o)), o = o.double(), n >>= _e; return r; } /** @@ -767,7 +766,7 @@ let Ua = class { * @returns precomputed point tables flattened to a single array */ precomputeWindow(t, n) { - const { windows: r, windowSize: o } = Nr(n, this.bits), s = []; + const { windows: r, windowSize: o } = po(n, this.bits), s = []; let i = t, c = i; for (let a = 0; a < r; a++) { c = i, s.push(c); @@ -787,12 +786,12 @@ let Ua = class { if (!this.Fn.isValid(r)) throw new Error("invalid scalar"); let o = this.ZERO, s = this.BASE; - const i = Nr(t, this.bits); + const i = po(t, this.bits); for (let c = 0; c < i.windows; c++) { - const { nextN: a, offset: u, isZero: f, isNeg: l, isNegF: d, offsetF: h } = hs(r, c, i); - r = a, f ? s = s.add(Yn(d, n[h])) : o = o.add(Yn(l, n[u])); + const { nextN: a, offset: u, isZero: f, isNeg: l, isNegF: d, offsetF: h } = di(r, c, i); + r = a, f ? s = s.add(Er(d, n[h])) : o = o.add(Er(l, n[u])); } - return ps(r), { p: o, f: s }; + return hi(r), { p: o, f: s }; } /** * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form. @@ -800,73 +799,73 @@ let Ua = class { * @returns point */ wNAFUnsafe(t, n, r, o = this.ZERO) { - const s = Nr(t, this.bits); - for (let i = 0; i < s.windows && r !== He; i++) { - const { nextN: c, offset: a, isZero: u, isNeg: f } = hs(r, i, s); + const s = po(t, this.bits); + for (let i = 0; i < s.windows && r !== on; i++) { + const { nextN: c, offset: a, isZero: u, isNeg: f } = di(r, i, s); if (r = c, !u) { const l = n[a]; o = o.add(f ? l.negate() : l); } } - return ps(r), o; + return hi(r), o; } getPrecomputes(t, n, r) { - let o = Rr.get(n); - return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), Rr.set(n, o))), o; + let o = go.get(n); + return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), go.set(n, o))), o; } cached(t, n, r) { - const o = Cr(t); + const o = wo(t); return this.wNAF(o, this.getPrecomputes(o, t, r), n); } unsafe(t, n, r, o) { - const s = Cr(t); + const s = wo(t); return s === 1 ? this._unsafeLadder(t, n, o) : this.wNAFUnsafe(s, this.getPrecomputes(s, t, r), n, o); } // We calculate precomputes for elliptic curve point multiplication // using windowed method. This specifies window size and // stores precomputed values. Usually only base point would be precomputed. createCache(t, n) { - Ri(n, this.bits), Ci.set(t, n), Rr.delete(t); + Fc(n, this.bits), Wc.set(t, n), go.delete(t); } hasCache(t) { - return Cr(t) !== 1; + return wo(t) !== 1; } }; -function Na(e, t, n, r) { +function yf(e, t, n, r) { let o = t, s = e.ZERO, i = e.ZERO; - for (; n > He || r > He; ) - n & Se && (s = s.add(o)), r & Se && (i = i.add(o)), o = o.double(), n >>= Se, r >>= Se; + for (; n > on || r > on; ) + n & _e && (s = s.add(o)), r & _e && (i = i.add(o)), o = o.double(), n >>= _e, r >>= _e; return { p1: s, p2: i }; } -function gs(e, t, n) { +function pi(e, t, n) { if (t) { if (t.ORDER !== e) throw new Error("Field.ORDER must match order: Fp == p, Fn == n"); - return Aa(t), t; + return df(t), t; } else - return mr(e, { isLE: n }); + return Zr(e, { isLE: n }); } -function Ra(e, t, n = {}, r) { +function mf(e, t, n = {}, r) { if (r === void 0 && (r = e === "edwards"), !t || typeof t != "object") throw new Error(`expected valid ${e} CURVE object`); for (const a of ["p", "n", "h"]) { const u = t[a]; - if (!(typeof u == "bigint" && u > He)) + if (!(typeof u == "bigint" && u > on)) throw new Error(`CURVE.${a} must be positive bigint`); } - const o = gs(t.p, n.Fp, r), s = gs(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; + const o = pi(t.p, n.Fp, r), s = pi(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; for (const a of c) if (!o.isValid(t[a])) throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`); return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: o, Fn: s }; } -function $i(e, t) { +function zc(e, t) { return function(r) { const o = e(r); return { secretKey: o, publicKey: t(o) }; }; } -class Li { +let Gc = class { oHash; iHash; blockLen; @@ -874,7 +873,7 @@ class Li { finished = !1; destroyed = !1; constructor(t, n) { - if (di(t), V(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") + if (vc(t), G(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") throw new Error("Expected instance of class which extends utils.Hash"); this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen; const r = this.blockLen, o = new Uint8Array(r); @@ -884,13 +883,13 @@ class Li { this.iHash.update(o), this.oHash = t.create(); for (let s = 0; s < o.length; s++) o[s] ^= 106; - this.oHash.update(o), De(o); + this.oHash.update(o), rn(o); } update(t) { - return zn(this), this.iHash.update(t), this; + return yr(this), this.iHash.update(t), this; } digestInto(t) { - zn(this), V(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); + yr(this), G(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); } digest() { const t = new Uint8Array(this.oHash.outputLen); @@ -907,57 +906,57 @@ class Li { destroy() { this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy(); } -} -const Pi = (e, t, n) => new Li(e, t).update(n).digest(); -Pi.create = (e, t) => new Li(e, t); +}; +const qc = (e, t, n) => new Gc(e, t).update(n).digest(); +qc.create = (e, t) => new Gc(e, t); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const ws = (e, t) => (e + (e >= 0 ? t : -t) / _i) / t; -function Ca(e, t, n) { - const [[r, o], [s, i]] = t, c = ws(i * e, n), a = ws(-o * e, n); +const gi = (e, t) => (e + (e >= 0 ? t : -t) / jc) / t; +function bf(e, t, n) { + const [[r, o], [s, i]] = t, c = gi(i * e, n), a = gi(-o * e, n); let u = e - c * r - a * s, f = -c * o - a * i; - const l = u < Qt, d = f < Qt; + const l = u < se, d = f < se; l && (u = -u), d && (f = -f); - const h = Io(Math.ceil(ma(n) / 2)) + $e; - if (u < Qt || u >= h || f < Qt || f >= h) + const h = bs(Math.ceil(rf(n) / 2)) + Xe; + if (u < se || u >= h || f < se || f >= h) throw new Error("splitScalar (endomorphism): failed, k=" + e); return { k1neg: l, k1: u, k2neg: d, k2: f }; } -function Gr(e) { +function No(e) { if (!["compact", "recovered", "der"].includes(e)) throw new Error('Signature format must be "compact", "recovered", or "der"'); return e; } -function $r(e, t) { +function yo(e, t) { const n = {}; for (let r of Object.keys(t)) n[r] = e[r] === void 0 ? t[r] : e[r]; - return qn(n.lowS, "lowS"), qn(n.prehash, "prehash"), n.format !== void 0 && Gr(n.format), n; + return br(n.lowS, "lowS"), br(n.prehash, "prehash"), n.format !== void 0 && No(n.format), n; } -class $a extends Error { +let Ef = class extends Error { constructor(t = "") { super(t); } -} -const ie = { +}; +const me = { // asn.1 DER encoding utils - Err: $a, + Err: Ef, // Basic building block is TLV (Tag-Length-Value) _tlv: { encode: (e, t) => { - const { Err: n } = ie; + const { Err: n } = me; if (e < 0 || e > 256) throw new n("tlv.encode: wrong tag"); if (t.length & 1) throw new n("tlv.encode: unpadded data"); - const r = t.length / 2, o = On(r); + const r = t.length / 2, o = Xn(r); if (o.length / 2 & 128) throw new n("tlv.encode: long form length too big"); - const s = r > 127 ? On(o.length / 2 | 128) : ""; - return On(e) + s + o + t; + const s = r > 127 ? Xn(o.length / 2 | 128) : ""; + return Xn(e) + s + o + t; }, // v - value, l - left bytes (unparsed) decode(e, t) { - const { Err: n } = ie; + const { Err: n } = me; let r = 0; if (e < 0 || e > 256) throw new n("tlv.encode: wrong tag"); @@ -995,25 +994,25 @@ const ie = { // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding) _int: { encode(e) { - const { Err: t } = ie; - if (e < Qt) + const { Err: t } = me; + if (e < se) throw new t("integer: negative integers are not allowed"); - let n = On(e); + let n = Xn(e); if (Number.parseInt(n[0], 16) & 8 && (n = "00" + n), n.length & 1) throw new t("unexpected DER parsing assertion: unpadded hex"); return n; }, decode(e) { - const { Err: t } = ie; + const { Err: t } = me; if (e[0] & 128) throw new t("invalid signature integer: negative"); if (e[0] === 0 && !(e[1] & 128)) throw new t("invalid signature integer: unnecessary leading zero"); - return qt(e); + return le(e); } }, toSig(e) { - const { Err: t, _int: n, _tlv: r } = ie, o = V(e, void 0, "signature"), { v: s, l: i } = r.decode(48, o); + const { Err: t, _int: n, _tlv: r } = me, o = G(e, void 0, "signature"), { v: s, l: i } = r.decode(48, o); if (i.length) throw new t("invalid signature: left bytes after parsing"); const { v: c, l: a } = r.decode(2, s), { v: u, l: f } = r.decode(2, a); @@ -1022,15 +1021,15 @@ const ie = { return { r: n.decode(c), s: n.decode(u) }; }, hexFromSig(e) { - const { _tlv: t, _int: n } = ie, r = t.encode(2, n.encode(e.r)), o = t.encode(2, n.encode(e.s)), s = r + o; + const { _tlv: t, _int: n } = me, r = t.encode(2, n.encode(e.r)), o = t.encode(2, n.encode(e.s)), s = r + o; return t.encode(48, s); } -}, Qt = BigInt(0), $e = BigInt(1), _i = BigInt(2), Un = BigInt(3), La = BigInt(4); -function Pa(e, t = {}) { - const n = Ra("weierstrass", e, t), { Fp: r, Fn: o } = n; +}, se = BigInt(0), Xe = BigInt(1), jc = BigInt(2), Qn = BigInt(3), xf = BigInt(4); +function Sf(e, t = {}) { + const n = mf("weierstrass", e, t), { Fp: r, Fn: o } = n; let s = n.CURVE; const { h: i, n: c } = s; - Bo(t, {}, { + Es(t, {}, { allowInfinityPoint: "boolean", clearCofactor: "function", isTorsionFree: "function", @@ -1041,109 +1040,109 @@ function Pa(e, t = {}) { const { endo: a } = t; if (a && (!r.is0(s.a) || typeof a.beta != "bigint" || !Array.isArray(a.basises))) throw new Error('invalid endo: expected "beta": bigint and "basises": array'); - const u = Di(r, o); + const u = Zc(r, o); function f() { if (!r.isOdd) throw new Error("compression is not supported: Field does not have .isOdd()"); } - function l(K, k, T) { - const { x: g, y: E } = k.toAffine(), U = r.toBytes(g); - if (qn(T, "isCompressed"), T) { + function l(_, b, m) { + const { x: p, y: E } = b.toAffine(), A = r.toBytes(p); + if (br(m, "isCompressed"), m) { f(); - const C = !r.isOdd(E); - return Lt(Vi(C), U); + const k = !r.isOdd(E); + return Kt(Yc(k), A); } else - return Lt(Uint8Array.of(4), U, r.toBytes(E)); - } - function d(K) { - V(K, void 0, "Point"); - const { publicKey: k, publicKeyUncompressed: T } = u, g = K.length, E = K[0], U = K.subarray(1); - if (g === k && (E === 2 || E === 3)) { - const C = r.fromBytes(U); - if (!r.isValid(C)) + return Kt(Uint8Array.of(4), A, r.toBytes(E)); + } + function d(_) { + G(_, void 0, "Point"); + const { publicKey: b, publicKeyUncompressed: m } = u, p = _.length, E = _[0], A = _.subarray(1); + if (p === b && (E === 2 || E === 3)) { + const k = r.fromBytes(A); + if (!r.isValid(k)) throw new Error("bad point: is not on curve, wrong x"); - const R = p(C); - let B; + const I = g(k); + let T; try { - B = r.sqrt(R); - } catch (et) { - const Y = et instanceof Error ? ": " + et.message : ""; - throw new Error("bad point: is not on curve, sqrt error" + Y); + T = r.sqrt(I); + } catch (F) { + const H = F instanceof Error ? ": " + F.message : ""; + throw new Error("bad point: is not on curve, sqrt error" + H); } f(); - const $ = r.isOdd(B); - return (E & 1) === 1 !== $ && (B = r.neg(B)), { x: C, y: B }; - } else if (g === T && E === 4) { - const C = r.BYTES, R = r.fromBytes(U.subarray(0, C)), B = r.fromBytes(U.subarray(C, C * 2)); - if (!m(R, B)) + const B = r.isOdd(T); + return (E & 1) === 1 !== B && (T = r.neg(T)), { x: k, y: T }; + } else if (p === m && E === 4) { + const k = r.BYTES, I = r.fromBytes(A.subarray(0, k)), T = r.fromBytes(A.subarray(k, k * 2)); + if (!y(I, T)) throw new Error("bad point: is not on curve"); - return { x: R, y: B }; + return { x: I, y: T }; } else - throw new Error(`bad point: got length ${g}, expected compressed=${k} or uncompressed=${T}`); + throw new Error(`bad point: got length ${p}, expected compressed=${b} or uncompressed=${m}`); } const h = t.toBytes || l, w = t.fromBytes || d; - function p(K) { - const k = r.sqr(K), T = r.mul(k, K); - return r.add(r.add(T, r.mul(K, s.a)), s.b); + function g(_) { + const b = r.sqr(_), m = r.mul(b, _); + return r.add(r.add(m, r.mul(_, s.a)), s.b); } - function m(K, k) { - const T = r.sqr(k), g = p(K); - return r.eql(T, g); + function y(_, b) { + const m = r.sqr(b), p = g(_); + return r.eql(m, p); } - if (!m(s.Gx, s.Gy)) + if (!y(s.Gx, s.Gy)) throw new Error("bad curve params: generator point"); - const x = r.mul(r.pow(s.a, Un), La), b = r.mul(r.sqr(s.b), BigInt(27)); - if (r.is0(r.add(x, b))) + const S = r.mul(r.pow(s.a, Qn), xf), v = r.mul(r.sqr(s.b), BigInt(27)); + if (r.is0(r.add(S, v))) throw new Error("bad curve params: a or b"); - function v(K, k, T = !1) { - if (!r.isValid(k) || T && r.is0(k)) - throw new Error(`bad point coordinate ${K}`); - return k; + function O(_, b, m = !1) { + if (!r.isValid(b) || m && r.is0(b)) + throw new Error(`bad point coordinate ${_}`); + return b; } - function O(K) { - if (!(K instanceof H)) + function N(_) { + if (!(_ instanceof C)) throw new Error("Weierstrass Point expected"); } - function A(K) { + function U(_) { if (!a || !a.basises) throw new Error("no endo"); - return Ca(K, a.basises, o.ORDER); + return bf(_, a.basises, o.ORDER); } - const F = us((K, k) => { - const { X: T, Y: g, Z: E } = K; + const W = ai((_, b) => { + const { X: m, Y: p, Z: E } = _; if (r.eql(E, r.ONE)) - return { x: T, y: g }; - const U = K.is0(); - k == null && (k = U ? r.ONE : r.inv(E)); - const C = r.mul(T, k), R = r.mul(g, k), B = r.mul(E, k); - if (U) + return { x: m, y: p }; + const A = _.is0(); + b == null && (b = A ? r.ONE : r.inv(E)); + const k = r.mul(m, b), I = r.mul(p, b), T = r.mul(E, b); + if (A) return { x: r.ZERO, y: r.ZERO }; - if (!r.eql(B, r.ONE)) + if (!r.eql(T, r.ONE)) throw new Error("invZ was invalid"); - return { x: C, y: R }; - }), y = us((K) => { - if (K.is0()) { - if (t.allowInfinityPoint && !r.is0(K.Y)) + return { x: k, y: I }; + }), x = ai((_) => { + if (_.is0()) { + if (t.allowInfinityPoint && !r.is0(_.Y)) return; throw new Error("bad point: ZERO"); } - const { x: k, y: T } = K.toAffine(); - if (!r.isValid(k) || !r.isValid(T)) + const { x: b, y: m } = _.toAffine(); + if (!r.isValid(b) || !r.isValid(m)) throw new Error("bad point: x or y not field elements"); - if (!m(k, T)) + if (!y(b, m)) throw new Error("bad point: equation left != right"); - if (!K.isTorsionFree()) + if (!_.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup"); return !0; }); - function tt(K, k, T, g, E) { - return T = new H(r.mul(T.X, K), T.Y, T.Z), k = Yn(g, k), T = Yn(E, T), k.add(T); + function Q(_, b, m, p, E) { + return m = new C(r.mul(m.X, _), m.Y, m.Z), b = Er(p, b), m = Er(E, m), b.add(m); } - class H { + class C { // base / generator point - static BASE = new H(s.Gx, s.Gy, r.ONE); + static BASE = new C(s.Gx, s.Gy, r.ONE); // zero / infinity / identity point - static ZERO = new H(r.ZERO, r.ONE, r.ZERO); + static ZERO = new C(r.ZERO, r.ONE, r.ZERO); // 0, 1, 0 // math field static Fp = r; @@ -1153,27 +1152,27 @@ function Pa(e, t = {}) { Y; Z; /** Does NOT validate if the point is valid. Use `.assertValidity()`. */ - constructor(k, T, g) { - this.X = v("x", k), this.Y = v("y", T, !0), this.Z = v("z", g), Object.freeze(this); + constructor(b, m, p) { + this.X = O("x", b), this.Y = O("y", m, !0), this.Z = O("z", p), Object.freeze(this); } static CURVE() { return s; } /** Does NOT validate if the point is valid. Use `.assertValidity()`. */ - static fromAffine(k) { - const { x: T, y: g } = k || {}; - if (!k || !r.isValid(T) || !r.isValid(g)) + static fromAffine(b) { + const { x: m, y: p } = b || {}; + if (!b || !r.isValid(m) || !r.isValid(p)) throw new Error("invalid affine point"); - if (k instanceof H) + if (b instanceof C) throw new Error("projective point not allowed"); - return r.is0(T) && r.is0(g) ? H.ZERO : new H(T, g, r.ONE); + return r.is0(m) && r.is0(p) ? C.ZERO : new C(m, p, r.ONE); } - static fromBytes(k) { - const T = H.fromAffine(w(V(k, void 0, "point"))); - return T.assertValidity(), T; + static fromBytes(b) { + const m = C.fromAffine(w(G(b, void 0, "point"))); + return m.assertValidity(), m; } - static fromHex(k) { - return H.fromBytes(jn(k)); + static fromHex(b) { + return C.fromBytes(mr(b)); } get x() { return this.toAffine().x; @@ -1187,58 +1186,58 @@ function Pa(e, t = {}) { * @param isLazy true will defer table computation until the first multiplication * @returns */ - precompute(k = 8, T = !0) { - return Yt.createCache(this, k), T || this.multiply(Un), this; + precompute(b = 8, m = !0) { + return gt.createCache(this, b), m || this.multiply(Qn), this; } // TODO: return `this` /** A point on curve is valid if it conforms to equation. */ assertValidity() { - y(this); + x(this); } hasEvenY() { - const { y: k } = this.toAffine(); + const { y: b } = this.toAffine(); if (!r.isOdd) throw new Error("Field doesn't support isOdd"); - return !r.isOdd(k); + return !r.isOdd(b); } /** Compare one point to another. */ - equals(k) { - O(k); - const { X: T, Y: g, Z: E } = this, { X: U, Y: C, Z: R } = k, B = r.eql(r.mul(T, R), r.mul(U, E)), $ = r.eql(r.mul(g, R), r.mul(C, E)); - return B && $; + equals(b) { + N(b); + const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b, T = r.eql(r.mul(m, I), r.mul(A, E)), B = r.eql(r.mul(p, I), r.mul(k, E)); + return T && B; } /** Flips point to one corresponding to (x, -y) in Affine coordinates. */ negate() { - return new H(this.X, r.neg(this.Y), this.Z); + return new C(this.X, r.neg(this.Y), this.Z); } // Renes-Costello-Batina exception-free doubling formula. // There is 30% faster Jacobian formula, but it is not complete. // https://eprint.iacr.org/2015/1060, algorithm 3 // Cost: 8M + 3S + 3*a + 2*b3 + 15add. double() { - const { a: k, b: T } = s, g = r.mul(T, Un), { X: E, Y: U, Z: C } = this; - let R = r.ZERO, B = r.ZERO, $ = r.ZERO, P = r.mul(E, E), et = r.mul(U, U), Y = r.mul(C, C), M = r.mul(E, U); - return M = r.add(M, M), $ = r.mul(E, C), $ = r.add($, $), R = r.mul(k, $), B = r.mul(g, Y), B = r.add(R, B), R = r.sub(et, B), B = r.add(et, B), B = r.mul(R, B), R = r.mul(M, R), $ = r.mul(g, $), Y = r.mul(k, Y), M = r.sub(P, Y), M = r.mul(k, M), M = r.add(M, $), $ = r.add(P, P), P = r.add($, P), P = r.add(P, Y), P = r.mul(P, M), B = r.add(B, P), Y = r.mul(U, C), Y = r.add(Y, Y), P = r.mul(Y, M), R = r.sub(R, P), $ = r.mul(Y, et), $ = r.add($, $), $ = r.add($, $), new H(R, B, $); + const { a: b, b: m } = s, p = r.mul(m, Qn), { X: E, Y: A, Z: k } = this; + let I = r.ZERO, T = r.ZERO, B = r.ZERO, R = r.mul(E, E), F = r.mul(A, A), H = r.mul(k, k), L = r.mul(E, A); + return L = r.add(L, L), B = r.mul(E, k), B = r.add(B, B), I = r.mul(b, B), T = r.mul(p, H), T = r.add(I, T), I = r.sub(F, T), T = r.add(F, T), T = r.mul(I, T), I = r.mul(L, I), B = r.mul(p, B), H = r.mul(b, H), L = r.sub(R, H), L = r.mul(b, L), L = r.add(L, B), B = r.add(R, R), R = r.add(B, R), R = r.add(R, H), R = r.mul(R, L), T = r.add(T, R), H = r.mul(A, k), H = r.add(H, H), R = r.mul(H, L), I = r.sub(I, R), B = r.mul(H, F), B = r.add(B, B), B = r.add(B, B), new C(I, T, B); } // Renes-Costello-Batina exception-free addition formula. // There is 30% faster Jacobian formula, but it is not complete. // https://eprint.iacr.org/2015/1060, algorithm 1 // Cost: 12M + 0S + 3*a + 3*b3 + 23add. - add(k) { - O(k); - const { X: T, Y: g, Z: E } = this, { X: U, Y: C, Z: R } = k; - let B = r.ZERO, $ = r.ZERO, P = r.ZERO; - const et = s.a, Y = r.mul(s.b, Un); - let M = r.mul(T, U), Q = r.mul(g, C), lt = r.mul(E, R), Ht = r.add(T, g), J = r.add(U, C); - Ht = r.mul(Ht, J), J = r.add(M, Q), Ht = r.sub(Ht, J), J = r.add(T, E); - let dt = r.add(U, R); - return J = r.mul(J, dt), dt = r.add(M, lt), J = r.sub(J, dt), dt = r.add(g, E), B = r.add(C, R), dt = r.mul(dt, B), B = r.add(Q, lt), dt = r.sub(dt, B), P = r.mul(et, J), B = r.mul(Y, lt), P = r.add(B, P), B = r.sub(Q, P), P = r.add(Q, P), $ = r.mul(B, P), Q = r.add(M, M), Q = r.add(Q, M), lt = r.mul(et, lt), J = r.mul(Y, J), Q = r.add(Q, lt), lt = r.sub(M, lt), lt = r.mul(et, lt), J = r.add(J, lt), M = r.mul(Q, J), $ = r.add($, M), M = r.mul(dt, J), B = r.mul(Ht, B), B = r.sub(B, M), M = r.mul(Ht, Q), P = r.mul(dt, P), P = r.add(P, M), new H(B, $, P); - } - subtract(k) { - return this.add(k.negate()); + add(b) { + N(b); + const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b; + let T = r.ZERO, B = r.ZERO, R = r.ZERO; + const F = s.a, H = r.mul(s.b, Qn); + let L = r.mul(m, A), D = r.mul(p, k), q = r.mul(E, I), st = r.add(m, p), M = r.add(A, k); + st = r.mul(st, M), M = r.add(L, D), st = r.sub(st, M), M = r.add(m, E); + let Y = r.add(A, I); + return M = r.mul(M, Y), Y = r.add(L, q), M = r.sub(M, Y), Y = r.add(p, E), T = r.add(k, I), Y = r.mul(Y, T), T = r.add(D, q), Y = r.sub(Y, T), R = r.mul(F, M), T = r.mul(H, q), R = r.add(T, R), T = r.sub(D, R), R = r.add(D, R), B = r.mul(T, R), D = r.add(L, L), D = r.add(D, L), q = r.mul(F, q), M = r.mul(H, M), D = r.add(D, q), q = r.sub(L, q), q = r.mul(F, q), M = r.add(M, q), L = r.mul(D, M), B = r.add(B, L), L = r.mul(Y, M), T = r.mul(st, T), T = r.sub(T, L), L = r.mul(st, D), R = r.mul(Y, R), R = r.add(R, L), new C(T, B, R); + } + subtract(b) { + return this.add(b.negate()); } is0() { - return this.equals(H.ZERO); + return this.equals(C.ZERO); } /** * Constant time multiplication. @@ -1249,81 +1248,81 @@ function Pa(e, t = {}) { * @param scalar by which the point would be multiplied * @returns New point */ - multiply(k) { - const { endo: T } = t; - if (!o.isValidNot0(k)) + multiply(b) { + const { endo: m } = t; + if (!o.isValidNot0(b)) throw new Error("invalid scalar: out of range"); - let g, E; - const U = (C) => Yt.cached(this, C, (R) => ds(H, R)); - if (T) { - const { k1neg: C, k1: R, k2neg: B, k2: $ } = A(k), { p: P, f: et } = U(R), { p: Y, f: M } = U($); - E = et.add(M), g = tt(T.beta, P, Y, C, B); + let p, E; + const A = (k) => gt.cached(this, k, (I) => li(C, I)); + if (m) { + const { k1neg: k, k1: I, k2neg: T, k2: B } = U(b), { p: R, f: F } = A(I), { p: H, f: L } = A(B); + E = F.add(L), p = Q(m.beta, R, H, k, T); } else { - const { p: C, f: R } = U(k); - g = C, E = R; + const { p: k, f: I } = A(b); + p = k, E = I; } - return ds(H, [g, E])[0]; + return li(C, [p, E])[0]; } /** * Non-constant-time multiplication. Uses double-and-add algorithm. * It's faster, but should only be used when you don't care about * an exposed secret key e.g. sig verification, which works over *public* keys. */ - multiplyUnsafe(k) { - const { endo: T } = t, g = this; - if (!o.isValid(k)) + multiplyUnsafe(b) { + const { endo: m } = t, p = this; + if (!o.isValid(b)) throw new Error("invalid scalar: out of range"); - if (k === Qt || g.is0()) - return H.ZERO; - if (k === $e) - return g; - if (Yt.hasCache(this)) - return this.multiply(k); - if (T) { - const { k1neg: E, k1: U, k2neg: C, k2: R } = A(k), { p1: B, p2: $ } = Na(H, g, U, R); - return tt(T.beta, B, $, E, C); + if (b === se || p.is0()) + return C.ZERO; + if (b === Xe) + return p; + if (gt.hasCache(this)) + return this.multiply(b); + if (m) { + const { k1neg: E, k1: A, k2neg: k, k2: I } = U(b), { p1: T, p2: B } = yf(C, p, A, I); + return Q(m.beta, T, B, E, k); } else - return Yt.unsafe(g, k); + return gt.unsafe(p, b); } /** * Converts Projective point to affine (x, y) coordinates. * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch */ - toAffine(k) { - return F(this, k); + toAffine(b) { + return W(this, b); } /** * Checks whether Point is free of torsion elements (is in prime subgroup). * Always torsion-free for cofactor=1 curves. */ isTorsionFree() { - const { isTorsionFree: k } = t; - return i === $e ? !0 : k ? k(H, this) : Yt.unsafe(this, c).is0(); + const { isTorsionFree: b } = t; + return i === Xe ? !0 : b ? b(C, this) : gt.unsafe(this, c).is0(); } clearCofactor() { - const { clearCofactor: k } = t; - return i === $e ? this : k ? k(H, this) : this.multiplyUnsafe(i); + const { clearCofactor: b } = t; + return i === Xe ? this : b ? b(C, this) : this.multiplyUnsafe(i); } isSmallOrder() { return this.multiplyUnsafe(i).is0(); } - toBytes(k = !0) { - return qn(k, "isCompressed"), this.assertValidity(), h(H, this, k); + toBytes(b = !0) { + return br(b, "isCompressed"), this.assertValidity(), h(C, this, b); } - toHex(k = !0) { - return yr(this.toBytes(k)); + toHex(b = !0) { + return Yr(this.toBytes(b)); } toString() { return ``; } } - const Ue = o.BITS, Yt = new Ua(H, t.endo ? Math.ceil(Ue / 2) : Ue); - return H.BASE.precompute(8), H; + const Pt = o.BITS, gt = new wf(C, t.endo ? Math.ceil(Pt / 2) : Pt); + return C.BASE.precompute(8), C; } -function Vi(e) { +function Yc(e) { return Uint8Array.of(e ? 2 : 3); } -function Di(e, t) { +function Zc(e, t) { return { secretKey: t.BYTES, publicKey: 1 + e.BYTES, @@ -1332,8 +1331,8 @@ function Di(e, t) { signature: 2 * t.BYTES }; } -function _a(e, t = {}) { - const { Fn: n } = e, r = t.randomBytes || En, o = Object.assign(Di(e.Fp, n), { seed: Ui(n.ORDER) }); +function vf(e, t = {}) { + const { Fn: n } = e, r = t.randomBytes || Wn, o = Object.assign(Zc(e.Fp, n), { seed: Mc(n.ORDER) }); function s(h) { try { const w = n.fromBytes(h); @@ -1343,197 +1342,197 @@ function _a(e, t = {}) { } } function i(h, w) { - const { publicKey: p, publicKeyUncompressed: m } = o; + const { publicKey: g, publicKeyUncompressed: y } = o; try { - const x = h.length; - return w === !0 && x !== p || w === !1 && x !== m ? !1 : !!e.fromBytes(h); + const S = h.length; + return w === !0 && S !== g || w === !1 && S !== y ? !1 : !!e.fromBytes(h); } catch { return !1; } } function c(h = r(o.seed)) { - return Ni(V(h, o.seed, "seed"), n.ORDER); + return Kc(G(h, o.seed, "seed"), n.ORDER); } function a(h, w = !0) { return e.BASE.multiply(n.fromBytes(h)).toBytes(w); } function u(h) { - const { secretKey: w, publicKey: p, publicKeyUncompressed: m } = o; - if (!ko(h) || "_lengths" in n && n._lengths || w === p) + const { secretKey: w, publicKey: g, publicKeyUncompressed: y } = o; + if (!ys(h) || "_lengths" in n && n._lengths || w === g) return; - const x = V(h, void 0, "key").length; - return x === p || x === m; + const S = G(h, void 0, "key").length; + return S === g || S === y; } - function f(h, w, p = !0) { + function f(h, w, g = !0) { if (u(h) === !0) throw new Error("first arg must be private key"); if (u(w) === !1) throw new Error("second arg must be public key"); - const m = n.fromBytes(h); - return e.fromBytes(w).multiply(m).toBytes(p); + const y = n.fromBytes(h); + return e.fromBytes(w).multiply(y).toBytes(g); } const l = { isValidSecretKey: s, isValidPublicKey: i, randomSecretKey: c - }, d = $i(c, a); + }, d = zc(c, a); return Object.freeze({ getPublicKey: a, getSharedSecret: f, keygen: d, Point: e, utils: l, lengths: o }); } -function Va(e, t, n = {}) { - di(t), Bo(n, {}, { +function Tf(e, t, n = {}) { + vc(t), Es(n, {}, { hmac: "function", lowS: "boolean", randomBytes: "function", bits2int: "function", bits2int_modN: "function" }), n = Object.assign({}, n); - const r = n.randomBytes || En, o = n.hmac || ((T, g) => Pi(t, T, g)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: l, utils: d, lengths: h } = _a(e, n), w = { + const r = n.randomBytes || Wn, o = n.hmac || ((m, p) => qc(t, m, p)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: l, utils: d, lengths: h } = vf(e, n), w = { prehash: !0, lowS: typeof n.lowS == "boolean" ? n.lowS : !0, format: "compact", extraEntropy: !1 - }, p = c * _i < s.ORDER; - function m(T) { - const g = c >> $e; - return T > g; - } - function x(T, g) { - if (!i.isValidNot0(g)) - throw new Error(`invalid signature ${T}: out of range 1..Point.Fn.ORDER`); - return g; - } - function b() { - if (p) + }, g = c * jc < s.ORDER; + function y(m) { + const p = c >> Xe; + return m > p; + } + function S(m, p) { + if (!i.isValidNot0(p)) + throw new Error(`invalid signature ${m}: out of range 1..Point.Fn.ORDER`); + return p; + } + function v() { + if (g) throw new Error('"recovered" sig type is not supported for cofactor >2 curves'); } - function v(T, g) { - Gr(g); - const E = h.signature, U = g === "compact" ? E : g === "recovered" ? E + 1 : void 0; - return V(T, U); + function O(m, p) { + No(p); + const E = h.signature, A = p === "compact" ? E : p === "recovered" ? E + 1 : void 0; + return G(m, A); } - class O { + class N { r; s; recovery; - constructor(g, E, U) { - if (this.r = x("r", g), this.s = x("s", E), U != null) { - if (b(), ![0, 1, 2, 3].includes(U)) + constructor(p, E, A) { + if (this.r = S("r", p), this.s = S("s", E), A != null) { + if (v(), ![0, 1, 2, 3].includes(A)) throw new Error("invalid recovery id"); - this.recovery = U; + this.recovery = A; } Object.freeze(this); } - static fromBytes(g, E = w.format) { - v(g, E); - let U; + static fromBytes(p, E = w.format) { + O(p, E); + let A; if (E === "der") { - const { r: $, s: P } = ie.toSig(V(g)); - return new O($, P); + const { r: B, s: R } = me.toSig(G(p)); + return new N(B, R); } - E === "recovered" && (U = g[0], E = "compact", g = g.subarray(1)); - const C = h.signature / 2, R = g.subarray(0, C), B = g.subarray(C, C * 2); - return new O(i.fromBytes(R), i.fromBytes(B), U); + E === "recovered" && (A = p[0], E = "compact", p = p.subarray(1)); + const k = h.signature / 2, I = p.subarray(0, k), T = p.subarray(k, k * 2); + return new N(i.fromBytes(I), i.fromBytes(T), A); } - static fromHex(g, E) { - return this.fromBytes(jn(g), E); + static fromHex(p, E) { + return this.fromBytes(mr(p), E); } assertRecovery() { - const { recovery: g } = this; - if (g == null) + const { recovery: p } = this; + if (p == null) throw new Error("invalid recovery id: must be present"); - return g; + return p; } - addRecoveryBit(g) { - return new O(this.r, this.s, g); + addRecoveryBit(p) { + return new N(this.r, this.s, p); } - recoverPublicKey(g) { - const { r: E, s: U } = this, C = this.assertRecovery(), R = C === 2 || C === 3 ? E + c : E; - if (!s.isValid(R)) + recoverPublicKey(p) { + const { r: E, s: A } = this, k = this.assertRecovery(), I = k === 2 || k === 3 ? E + c : E; + if (!s.isValid(I)) throw new Error("invalid recovery id: sig.r+curve.n != R.x"); - const B = s.toBytes(R), $ = e.fromBytes(Lt(Vi((C & 1) === 0), B)), P = i.inv(R), et = F(V(g, void 0, "msgHash")), Y = i.create(-et * P), M = i.create(U * P), Q = e.BASE.multiplyUnsafe(Y).add($.multiplyUnsafe(M)); - if (Q.is0()) + const T = s.toBytes(I), B = e.fromBytes(Kt(Yc((k & 1) === 0), T)), R = i.inv(I), F = W(G(p, void 0, "msgHash")), H = i.create(-F * R), L = i.create(A * R), D = e.BASE.multiplyUnsafe(H).add(B.multiplyUnsafe(L)); + if (D.is0()) throw new Error("invalid recovery: point at infinify"); - return Q.assertValidity(), Q; + return D.assertValidity(), D; } // Signatures should be low-s, to prevent malleability. hasHighS() { - return m(this.s); + return y(this.s); } - toBytes(g = w.format) { - if (Gr(g), g === "der") - return jn(ie.hexFromSig(this)); - const { r: E, s: U } = this, C = i.toBytes(E), R = i.toBytes(U); - return g === "recovered" ? (b(), Lt(Uint8Array.of(this.assertRecovery()), C, R)) : Lt(C, R); + toBytes(p = w.format) { + if (No(p), p === "der") + return mr(me.hexFromSig(this)); + const { r: E, s: A } = this, k = i.toBytes(E), I = i.toBytes(A); + return p === "recovered" ? (v(), Kt(Uint8Array.of(this.assertRecovery()), k, I)) : Kt(k, I); } - toHex(g) { - return yr(this.toBytes(g)); + toHex(p) { + return Yr(this.toBytes(p)); } } - const A = n.bits2int || function(g) { - if (g.length > 8192) + const U = n.bits2int || function(p) { + if (p.length > 8192) throw new Error("input is too large"); - const E = qt(g), U = g.length * 8 - a; - return U > 0 ? E >> BigInt(U) : E; - }, F = n.bits2int_modN || function(g) { - return i.create(A(g)); - }, y = Io(a); - function tt(T) { - return bi("num < 2^" + a, T, Qt, y), i.toBytes(T); - } - function H(T, g) { - return V(T, void 0, "message"), g ? V(t(T), void 0, "prehashed message") : T; - } - function Ue(T, g, E) { - const { lowS: U, prehash: C, extraEntropy: R } = $r(E, w); - T = H(T, C); - const B = F(T), $ = i.fromBytes(g); - if (!i.isValidNot0($)) + const E = le(p), A = p.length * 8 - a; + return A > 0 ? E >> BigInt(A) : E; + }, W = n.bits2int_modN || function(p) { + return i.create(U(p)); + }, x = bs(a); + function Q(m) { + return Uc("num < 2^" + a, m, se, x), i.toBytes(m); + } + function C(m, p) { + return G(m, void 0, "message"), p ? G(t(m), void 0, "prehashed message") : m; + } + function Pt(m, p, E) { + const { lowS: A, prehash: k, extraEntropy: I } = yo(E, w); + m = C(m, k); + const T = W(m), B = i.fromBytes(p); + if (!i.isValidNot0(B)) throw new Error("invalid private key"); - const P = [tt($), tt(B)]; - if (R != null && R !== !1) { - const Q = R === !0 ? r(h.secretKey) : R; - P.push(V(Q, void 0, "extraEntropy")); - } - const et = Lt(...P), Y = B; - function M(Q) { - const lt = A(Q); - if (!i.isValidNot0(lt)) + const R = [Q(B), Q(T)]; + if (I != null && I !== !1) { + const D = I === !0 ? r(h.secretKey) : I; + R.push(G(D, void 0, "extraEntropy")); + } + const F = Kt(...R), H = T; + function L(D) { + const q = U(D); + if (!i.isValidNot0(q)) return; - const Ht = i.inv(lt), J = e.BASE.multiply(lt).toAffine(), dt = i.create(J.x); - if (dt === Qt) + const st = i.inv(q), M = e.BASE.multiply(q).toAffine(), Y = i.create(M.x); + if (Y === se) return; - const In = i.create(Ht * i.create(Y + dt * $)); - if (In === Qt) + const ee = i.create(st * i.create(H + Y * B)); + if (ee === se) return; - let is = (J.x === dt ? 0 : 2) | Number(J.y & $e), cs = In; - return U && m(In) && (cs = i.neg(In), is ^= 1), new O(dt, cs, p ? void 0 : is); + let En = (M.x === Y ? 0 : 2) | Number(M.y & Xe), xn = ee; + return A && y(ee) && (xn = i.neg(ee), En ^= 1), new N(Y, xn, g ? void 0 : En); } - return { seed: et, k2sig: M }; + return { seed: F, k2sig: L }; } - function Yt(T, g, E = {}) { - const { seed: U, k2sig: C } = Ue(T, g, E); - return xa(t.outputLen, i.BYTES, o)(U, C).toBytes(E.format); + function gt(m, p, E = {}) { + const { seed: A, k2sig: k } = Pt(m, p, E); + return of(t.outputLen, i.BYTES, o)(A, k).toBytes(E.format); } - function K(T, g, E, U = {}) { - const { lowS: C, prehash: R, format: B } = $r(U, w); - if (E = V(E, void 0, "publicKey"), g = H(g, R), !ko(T)) { - const $ = T instanceof O ? ", use sig.toBytes()" : ""; - throw new Error("verify expects Uint8Array signature" + $); + function _(m, p, E, A = {}) { + const { lowS: k, prehash: I, format: T } = yo(A, w); + if (E = G(E, void 0, "publicKey"), p = C(p, I), !ys(m)) { + const B = m instanceof N ? ", use sig.toBytes()" : ""; + throw new Error("verify expects Uint8Array signature" + B); } - v(T, B); + O(m, T); try { - const $ = O.fromBytes(T, B), P = e.fromBytes(E); - if (C && $.hasHighS()) + const B = N.fromBytes(m, T), R = e.fromBytes(E); + if (k && B.hasHighS()) return !1; - const { r: et, s: Y } = $, M = F(g), Q = i.inv(Y), lt = i.create(M * Q), Ht = i.create(et * Q), J = e.BASE.multiplyUnsafe(lt).add(P.multiplyUnsafe(Ht)); - return J.is0() ? !1 : i.create(J.x) === et; + const { r: F, s: H } = B, L = W(p), D = i.inv(H), q = i.create(L * D), st = i.create(F * D), M = e.BASE.multiplyUnsafe(q).add(R.multiplyUnsafe(st)); + return M.is0() ? !1 : i.create(M.x) === F; } catch { return !1; } } - function k(T, g, E = {}) { - const { prehash: U } = $r(E, w); - return g = H(g, U), O.fromBytes(T, "recovered").recoverPublicKey(g).toBytes(); + function b(m, p, E = {}) { + const { prehash: A } = yo(E, w); + return p = C(p, A), N.fromBytes(m, "recovered").recoverPublicKey(p).toBytes(); } return Object.freeze({ keygen: u, @@ -1542,15 +1541,15 @@ function Va(e, t, n = {}) { utils: d, lengths: h, Point: e, - sign: Yt, - verify: K, - recoverPublicKey: k, - Signature: O, + sign: gt, + verify: _, + recoverPublicKey: b, + Signature: N, hash: t }); } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const xr = { +const Xr = { p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"), n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"), h: BigInt(1), @@ -1558,87 +1557,87 @@ const xr = { b: BigInt(7), Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"), Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8") -}, Da = { +}, Af = { beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"), basises: [ [BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")], [BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")] ] -}, Ha = /* @__PURE__ */ BigInt(0), zr = /* @__PURE__ */ BigInt(2); -function Ma(e) { - const t = xr.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, l = It(f, n, t) * f % t, d = It(l, n, t) * f % t, h = It(d, zr, t) * u % t, w = It(h, o, t) * h % t, p = It(w, s, t) * w % t, m = It(p, c, t) * p % t, x = It(m, a, t) * m % t, b = It(x, c, t) * p % t, v = It(b, n, t) * f % t, O = It(v, i, t) * w % t, A = It(O, r, t) * u % t, F = It(A, zr, t); - if (!Zn.eql(Zn.sqr(F), e)) +}, If = /* @__PURE__ */ BigInt(0), Lo = /* @__PURE__ */ BigInt(2); +function kf(e) { + const t = Xr.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, l = Rt(f, n, t) * f % t, d = Rt(l, n, t) * f % t, h = Rt(d, Lo, t) * u % t, w = Rt(h, o, t) * h % t, g = Rt(w, s, t) * w % t, y = Rt(g, c, t) * g % t, S = Rt(y, a, t) * y % t, v = Rt(S, c, t) * g % t, O = Rt(v, n, t) * f % t, N = Rt(O, i, t) * w % t, U = Rt(N, r, t) * u % t, W = Rt(U, Lo, t); + if (!xr.eql(xr.sqr(W), e)) throw new Error("Cannot find square root"); - return F; -} -const Zn = mr(xr.p, { sqrt: Ma }), Oe = /* @__PURE__ */ Pa(xr, { - Fp: Zn, - endo: Da -}), Ft = /* @__PURE__ */ Va(Oe, ht), ys = {}; -function Xn(e, ...t) { - let n = ys[e]; + return W; +} +const xr = Zr(Xr.p, { sqrt: kf }), Ge = /* @__PURE__ */ Sf(Xr, { + Fp: xr, + endo: Af +}), xe = /* @__PURE__ */ Tf(Ge, wt), wi = {}; +function Sr(e, ...t) { + let n = wi[e]; if (n === void 0) { - const r = ht(wa(e)); - n = Lt(r, r), ys[e] = n; + const r = wt(ef(e)); + n = Kt(r, r), wi[e] = n; } - return ht(Lt(n, ...t)); + return wt(Kt(n, ...t)); } -const Uo = (e) => e.toBytes(!0).slice(1), No = (e) => e % zr === Ha; -function jr(e) { - const { Fn: t, BASE: n } = Oe, r = t.fromBytes(e), o = n.multiply(r); - return { scalar: No(o.y) ? r : t.neg(r), bytes: Uo(o) }; +const Ss = (e) => e.toBytes(!0).slice(1), vs = (e) => e % Lo === If; +function Co(e) { + const { Fn: t, BASE: n } = Ge, r = t.fromBytes(e), o = n.multiply(r); + return { scalar: vs(o.y) ? r : t.neg(r), bytes: Ss(o) }; } -function Hi(e) { - const t = Zn; +function Xc(e) { + const t = xr; if (!t.isValidNot0(e)) throw new Error("invalid x: Fail if x ≥ p"); const n = t.create(e * e), r = t.create(n * e + BigInt(7)); let o = t.sqrt(r); - No(o) || (o = t.neg(o)); - const s = Oe.fromAffine({ x: e, y: o }); + vs(o) || (o = t.neg(o)); + const s = Ge.fromAffine({ x: e, y: o }); return s.assertValidity(), s; } -const sn = qt; -function Mi(...e) { - return Oe.Fn.create(sn(Xn("BIP0340/challenge", ...e))); +const Bn = le; +function Qc(...e) { + return Ge.Fn.create(Bn(Sr("BIP0340/challenge", ...e))); } -function ms(e) { - return jr(e).bytes; +function yi(e) { + return Co(e).bytes; } -function Ka(e, t, n = En(32)) { - const { Fn: r } = Oe, o = V(e, void 0, "message"), { bytes: s, scalar: i } = jr(t), c = V(n, 32, "auxRand"), a = r.toBytes(i ^ sn(Xn("BIP0340/aux", c))), u = Xn("BIP0340/nonce", a, s, o), { bytes: f, scalar: l } = jr(u), d = Mi(f, s, o), h = new Uint8Array(64); - if (h.set(f, 0), h.set(r.toBytes(r.create(l + d * i)), 32), !Ki(h, o, s)) +function Bf(e, t, n = Wn(32)) { + const { Fn: r } = Ge, o = G(e, void 0, "message"), { bytes: s, scalar: i } = Co(t), c = G(n, 32, "auxRand"), a = r.toBytes(i ^ Bn(Sr("BIP0340/aux", c))), u = Sr("BIP0340/nonce", a, s, o), { bytes: f, scalar: l } = Co(u), d = Qc(f, s, o), h = new Uint8Array(64); + if (h.set(f, 0), h.set(r.toBytes(r.create(l + d * i)), 32), !Jc(h, o, s)) throw new Error("sign: Invalid signature produced"); return h; } -function Ki(e, t, n) { - const { Fp: r, Fn: o, BASE: s } = Oe, i = V(e, 64, "signature"), c = V(t, void 0, "message"), a = V(n, 32, "publicKey"); +function Jc(e, t, n) { + const { Fp: r, Fn: o, BASE: s } = Ge, i = G(e, 64, "signature"), c = G(t, void 0, "message"), a = G(n, 32, "publicKey"); try { - const u = Hi(sn(a)), f = sn(i.subarray(0, 32)); + const u = Xc(Bn(a)), f = Bn(i.subarray(0, 32)); if (!r.isValidNot0(f)) return !1; - const l = sn(i.subarray(32, 64)); + const l = Bn(i.subarray(32, 64)); if (!o.isValidNot0(l)) return !1; - const d = Mi(o.toBytes(f), Uo(u), c), h = s.multiplyUnsafe(l).add(u.multiplyUnsafe(o.neg(d))), { x: w, y: p } = h.toAffine(); - return !(h.is0() || !No(p) || w !== f); + const d = Qc(o.toBytes(f), Ss(u), c), h = s.multiplyUnsafe(l).add(u.multiplyUnsafe(o.neg(d))), { x: w, y: g } = h.toAffine(); + return !(h.is0() || !vs(g) || w !== f); } catch { return !1; } } -const Nt = /* @__PURE__ */ (() => { - const n = (r = En(48)) => Ni(r, xr.n); +const de = /* @__PURE__ */ (() => { + const n = (r = Wn(48)) => Kc(r, Xr.n); return { - keygen: $i(n, ms), - getPublicKey: ms, - sign: Ka, - verify: Ki, - Point: Oe, + keygen: zc(n, yi), + getPublicKey: yi, + sign: Bf, + verify: Jc, + Point: Ge, utils: { randomSecretKey: n, - taggedHash: Xn, - lift_x: Hi, - pointToBytes: Uo + taggedHash: Sr, + lift_x: Xc, + pointToBytes: Ss }, lengths: { secretKey: 32, @@ -1648,7 +1647,7 @@ const Nt = /* @__PURE__ */ (() => { seed: 48 } }; -})(), Fa = /* @__PURE__ */ Uint8Array.from([ +})(), Of = /* @__PURE__ */ Uint8Array.from([ 7, 4, 13, @@ -1665,36 +1664,36 @@ const Nt = /* @__PURE__ */ (() => { 14, 11, 8 -]), Fi = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), Wa = Fi.map((e) => (9 * e + 5) % 16), Wi = /* @__PURE__ */ (() => { - const n = [[Fi], [Wa]]; +]), ta = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), $f = ta.map((e) => (9 * e + 5) % 16), ea = /* @__PURE__ */ (() => { + const n = [[ta], [$f]]; for (let r = 0; r < 4; r++) for (let o of n) - o.push(o[r].map((s) => Fa[s])); + o.push(o[r].map((s) => Of[s])); return n; -})(), Gi = Wi[0], zi = Wi[1], ji = /* @__PURE__ */ [ +})(), na = ea[0], ra = ea[1], oa = /* @__PURE__ */ [ [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8], [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7], [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9], [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6], [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5] -].map((e) => Uint8Array.from(e)), Ga = /* @__PURE__ */ Gi.map((e, t) => e.map((n) => ji[t][n])), za = /* @__PURE__ */ zi.map((e, t) => e.map((n) => ji[t][n])), ja = /* @__PURE__ */ Uint32Array.from([ +].map((e) => Uint8Array.from(e)), Uf = /* @__PURE__ */ na.map((e, t) => e.map((n) => oa[t][n])), Rf = /* @__PURE__ */ ra.map((e, t) => e.map((n) => oa[t][n])), Nf = /* @__PURE__ */ Uint32Array.from([ 0, 1518500249, 1859775393, 2400959708, 2840853838 -]), qa = /* @__PURE__ */ Uint32Array.from([ +]), Lf = /* @__PURE__ */ Uint32Array.from([ 1352829926, 1548603684, 1836072691, 2053994217, 0 ]); -function xs(e, t, n, r) { +function mi(e, t, n, r) { return e === 0 ? t ^ n ^ r : e === 1 ? t & n | ~t & r : e === 2 ? (t | ~n) ^ r : e === 3 ? t & r | n & ~r : t ^ (n | ~r); } -const Nn = /* @__PURE__ */ new Uint32Array(16); -class Ya extends gi { +const Jn = /* @__PURE__ */ new Uint32Array(16); +class Cf extends Ic { h0 = 1732584193; h1 = -271733879; h2 = -1732584194; @@ -1712,84 +1711,84 @@ class Ya extends gi { } process(t, n) { for (let h = 0; h < 16; h++, n += 4) - Nn[h] = t.getUint32(n, !0); + Jn[h] = t.getUint32(n, !0); let r = this.h0 | 0, o = r, s = this.h1 | 0, i = s, c = this.h2 | 0, a = c, u = this.h3 | 0, f = u, l = this.h4 | 0, d = l; for (let h = 0; h < 5; h++) { - const w = 4 - h, p = ja[h], m = qa[h], x = Gi[h], b = zi[h], v = Ga[h], O = za[h]; - for (let A = 0; A < 16; A++) { - const F = Bn(r + xs(h, s, c, u) + Nn[x[A]] + p, v[A]) + l | 0; - r = l, l = u, u = Bn(c, 10) | 0, c = s, s = F; + const w = 4 - h, g = Nf[h], y = Lf[h], S = na[h], v = ra[h], O = Uf[h], N = Rf[h]; + for (let U = 0; U < 16; U++) { + const W = Zn(r + mi(h, s, c, u) + Jn[S[U]] + g, O[U]) + l | 0; + r = l, l = u, u = Zn(c, 10) | 0, c = s, s = W; } - for (let A = 0; A < 16; A++) { - const F = Bn(o + xs(w, i, a, f) + Nn[b[A]] + m, O[A]) + d | 0; - o = d, d = f, f = Bn(a, 10) | 0, a = i, i = F; + for (let U = 0; U < 16; U++) { + const W = Zn(o + mi(w, i, a, f) + Jn[v[U]] + y, N[U]) + d | 0; + o = d, d = f, f = Zn(a, 10) | 0, a = i, i = W; } } this.set(this.h1 + c + f | 0, this.h2 + u + d | 0, this.h3 + l + o | 0, this.h4 + r + i | 0, this.h0 + s + a | 0); } roundClean() { - De(Nn); + rn(Jn); } destroy() { - this.destroyed = !0, De(this.buffer), this.set(0, 0, 0, 0, 0); + this.destroyed = !0, rn(this.buffer), this.set(0, 0, 0, 0, 0); } } -const Za = /* @__PURE__ */ pi(() => new Ya()); +const _f = /* @__PURE__ */ Ac(() => new Cf()); /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -function Me(e) { +function sn(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function qi(e) { - if (!Me(e)) +function sa(e) { + if (!sn(e)) throw new Error("Uint8Array expected"); } -function Yi(e, t) { +function ia(e, t) { return Array.isArray(t) ? t.length === 0 ? !0 : e ? t.every((n) => typeof n == "string") : t.every((n) => Number.isSafeInteger(n)) : !1; } -function Ro(e) { +function Ts(e) { if (typeof e != "function") throw new Error("function expected"); return !0; } -function le(e, t) { +function Ae(e, t) { if (typeof t != "string") throw new Error(`${e}: string expected`); return !0; } -function Xe(e) { +function yn(e) { if (!Number.isSafeInteger(e)) throw new Error(`invalid integer: ${e}`); } -function Qn(e) { +function vr(e) { if (!Array.isArray(e)) throw new Error("array expected"); } -function Jn(e, t) { - if (!Yi(!0, t)) +function Tr(e, t) { + if (!ia(!0, t)) throw new Error(`${e}: array of strings expected`); } -function Co(e, t) { - if (!Yi(!1, t)) +function As(e, t) { + if (!ia(!1, t)) throw new Error(`${e}: array of numbers expected`); } // @__NO_SIDE_EFFECTS__ -function Tn(...e) { +function Gn(...e) { const t = (s) => s, n = (s, i) => (c) => s(i(c)), r = e.map((s) => s.encode).reduceRight(n, t), o = e.map((s) => s.decode).reduce(n, t); return { encode: r, decode: o }; } // @__NO_SIDE_EFFECTS__ -function br(e) { +function Qr(e) { const t = typeof e == "string" ? e.split("") : e, n = t.length; - Jn("alphabet", t); + Tr("alphabet", t); const r = new Map(t.map((o, s) => [o, s])); return { - encode: (o) => (Qn(o), o.map((s) => { + encode: (o) => (vr(o), o.map((s) => { if (!Number.isSafeInteger(s) || s < 0 || s >= n) throw new Error(`alphabet.encode: digit index outside alphabet "${s}". Allowed: ${e}`); return t[s]; })), - decode: (o) => (Qn(o), o.map((s) => { - le("alphabet.decode", s); + decode: (o) => (vr(o), o.map((s) => { + Ae("alphabet.decode", s); const i = r.get(s); if (i === void 0) throw new Error(`Unknown letter: "${s}". Allowed: ${e}`); @@ -1798,22 +1797,22 @@ function br(e) { }; } // @__NO_SIDE_EFFECTS__ -function Er(e = "") { - return le("join", e), { - encode: (t) => (Jn("join.decode", t), t.join(e)), - decode: (t) => (le("join.decode", t), t.split(e)) +function Jr(e = "") { + return Ae("join", e), { + encode: (t) => (Tr("join.decode", t), t.join(e)), + decode: (t) => (Ae("join.decode", t), t.split(e)) }; } // @__NO_SIDE_EFFECTS__ -function Xa(e, t = "=") { - return Xe(e), le("padding", t), { +function Pf(e, t = "=") { + return yn(e), Ae("padding", t), { encode(n) { - for (Jn("padding.encode", n); n.length * e % 8; ) + for (Tr("padding.encode", n); n.length * e % 8; ) n.push(t); return n; }, decode(n) { - Jn("padding.decode", n); + Tr("padding.decode", n); let r = n.length; if (r * e % 8) throw new Error("padding: invalid, string should have whole number of bytes"); @@ -1825,19 +1824,19 @@ function Xa(e, t = "=") { }; } // @__NO_SIDE_EFFECTS__ -function Qa(e) { - return Ro(e), { encode: (t) => t, decode: (t) => e(t) }; +function Hf(e) { + return Ts(e), { encode: (t) => t, decode: (t) => e(t) }; } -function bs(e, t, n) { +function bi(e, t, n) { if (t < 2) throw new Error(`convertRadix: invalid from=${t}, base cannot be less than 2`); if (n < 2) throw new Error(`convertRadix: invalid to=${n}, base cannot be less than 2`); - if (Qn(e), !e.length) + if (vr(e), !e.length) return []; let r = 0; const o = [], s = Array.from(e, (c) => { - if (Xe(c), c < 0 || c >= t) + if (yn(c), c < 0 || c >= t) throw new Error(`invalid integer: ${c}`); return c; }), i = s.length; @@ -1863,29 +1862,29 @@ function bs(e, t, n) { o.push(0); return o.reverse(); } -const Zi = (e, t) => t === 0 ? e : Zi(t, e % t), tr = /* @__NO_SIDE_EFFECTS__ */ (e, t) => e + (t - Zi(e, t)), Mn = /* @__PURE__ */ (() => { +const ca = (e, t) => t === 0 ? e : ca(t, e % t), Ar = /* @__NO_SIDE_EFFECTS__ */ (e, t) => e + (t - ca(e, t)), lr = /* @__PURE__ */ (() => { let e = []; for (let t = 0; t < 40; t++) e.push(2 ** t); return e; })(); -function qr(e, t, n, r) { - if (Qn(e), t <= 0 || t > 32) +function _o(e, t, n, r) { + if (vr(e), t <= 0 || t > 32) throw new Error(`convertRadix2: wrong from=${t}`); if (n <= 0 || n > 32) throw new Error(`convertRadix2: wrong to=${n}`); - if (/* @__PURE__ */ tr(t, n) > 32) - throw new Error(`convertRadix2: carry overflow from=${t} to=${n} carryBits=${/* @__PURE__ */ tr(t, n)}`); + if (/* @__PURE__ */ Ar(t, n) > 32) + throw new Error(`convertRadix2: carry overflow from=${t} to=${n} carryBits=${/* @__PURE__ */ Ar(t, n)}`); let o = 0, s = 0; - const i = Mn[t], c = Mn[n] - 1, a = []; + const i = lr[t], c = lr[n] - 1, a = []; for (const u of e) { - if (Xe(u), u >= i) + if (yn(u), u >= i) throw new Error(`convertRadix2: invalid data word=${u} from=${t}`); if (o = o << t | u, s + t > 32) throw new Error(`convertRadix2: carry overflow pos=${s} from=${t}`); for (s += t; s >= n; s -= n) a.push((o >> s - n & c) >>> 0); - const f = Mn[s]; + const f = lr[s]; if (f === void 0) throw new Error("invalid carry"); o &= f - 1; @@ -1897,51 +1896,51 @@ function qr(e, t, n, r) { return r && s > 0 && a.push(o >>> 0), a; } // @__NO_SIDE_EFFECTS__ -function Ja(e) { - Xe(e); +function Vf(e) { + yn(e); const t = 2 ** 8; return { encode: (n) => { - if (!Me(n)) + if (!sn(n)) throw new Error("radix.encode input should be Uint8Array"); - return bs(Array.from(n), t, e); + return bi(Array.from(n), t, e); }, - decode: (n) => (Co("radix.decode", n), Uint8Array.from(bs(n, e, t))) + decode: (n) => (As("radix.decode", n), Uint8Array.from(bi(n, e, t))) }; } // @__NO_SIDE_EFFECTS__ -function $o(e, t = !1) { - if (Xe(e), e <= 0 || e > 32) +function Is(e, t = !1) { + if (yn(e), e <= 0 || e > 32) throw new Error("radix2: bits should be in (0..32]"); - if (/* @__PURE__ */ tr(8, e) > 32 || /* @__PURE__ */ tr(e, 8) > 32) + if (/* @__PURE__ */ Ar(8, e) > 32 || /* @__PURE__ */ Ar(e, 8) > 32) throw new Error("radix2: carry overflow"); return { encode: (n) => { - if (!Me(n)) + if (!sn(n)) throw new Error("radix2.encode input should be Uint8Array"); - return qr(Array.from(n), 8, e, !t); + return _o(Array.from(n), 8, e, !t); }, - decode: (n) => (Co("radix2.decode", n), Uint8Array.from(qr(n, e, 8, t))) + decode: (n) => (As("radix2.decode", n), Uint8Array.from(_o(n, e, 8, t))) }; } -function Es(e) { - return Ro(e), function(...t) { +function Ei(e) { + return Ts(e), function(...t) { try { return e.apply(null, t); } catch { } }; } -function tu(e, t) { - return Xe(e), Ro(t), { +function Df(e, t) { + return yn(e), Ts(t), { encode(n) { - if (!Me(n)) + if (!sn(n)) throw new Error("checksum.encode: input should be Uint8Array"); const r = t(n).slice(0, e), o = new Uint8Array(n.length + e); return o.set(n), o.set(r, n.length), o; }, decode(n) { - if (!Me(n)) + if (!sn(n)) throw new Error("checksum.decode: input should be Uint8Array"); const r = n.slice(0, -e), o = n.slice(-e), s = t(r).slice(0, e); for (let i = 0; i < e; i++) @@ -1951,79 +1950,79 @@ function tu(e, t) { } }; } -const eu = typeof Uint8Array.from([]).toBase64 == "function" && typeof Uint8Array.fromBase64 == "function", nu = (e, t) => { - le("base64", e); +const Mf = typeof Uint8Array.from([]).toBase64 == "function" && typeof Uint8Array.fromBase64 == "function", Kf = (e, t) => { + Ae("base64", e); const n = /^[A-Za-z0-9=+/]+$/, r = "base64"; if (e.length > 0 && !n.test(e)) throw new Error("invalid base64"); return Uint8Array.fromBase64(e, { alphabet: r, lastChunkHandling: "strict" }); -}, Et = eu ? { +}, It = Mf ? { encode(e) { - return qi(e), e.toBase64(); + return sa(e), e.toBase64(); }, decode(e) { - return nu(e); + return Kf(e); } -} : /* @__PURE__ */ Tn(/* @__PURE__ */ $o(6), /* @__PURE__ */ br("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */ Xa(6), /* @__PURE__ */ Er("")), ru = /* @__NO_SIDE_EFFECTS__ */ (e) => /* @__PURE__ */ Tn(/* @__PURE__ */ Ja(58), /* @__PURE__ */ br(e), /* @__PURE__ */ Er("")), Yr = /* @__PURE__ */ ru("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"), ou = (e) => /* @__PURE__ */ Tn(tu(4, (t) => e(e(t))), Yr), Zr = /* @__PURE__ */ Tn(/* @__PURE__ */ br("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), /* @__PURE__ */ Er("")), Ss = [996825010, 642813549, 513874426, 1027748829, 705979059]; -function Je(e) { +} : /* @__PURE__ */ Gn(/* @__PURE__ */ Is(6), /* @__PURE__ */ Qr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */ Pf(6), /* @__PURE__ */ Jr("")), Ff = /* @__NO_SIDE_EFFECTS__ */ (e) => /* @__PURE__ */ Gn(/* @__PURE__ */ Vf(58), /* @__PURE__ */ Qr(e), /* @__PURE__ */ Jr("")), Po = /* @__PURE__ */ Ff("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"), Wf = (e) => /* @__PURE__ */ Gn(Df(4, (t) => e(e(t))), Po), Ho = /* @__PURE__ */ Gn(/* @__PURE__ */ Qr("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), /* @__PURE__ */ Jr("")), xi = [996825010, 642813549, 513874426, 1027748829, 705979059]; +function Sn(e) { const t = e >> 25; let n = (e & 33554431) << 5; - for (let r = 0; r < Ss.length; r++) - (t >> r & 1) === 1 && (n ^= Ss[r]); + for (let r = 0; r < xi.length; r++) + (t >> r & 1) === 1 && (n ^= xi[r]); return n; } -function Ts(e, t, n = 1) { +function Si(e, t, n = 1) { const r = e.length; let o = 1; for (let s = 0; s < r; s++) { const i = e.charCodeAt(s); if (i < 33 || i > 126) throw new Error(`Invalid prefix (${e})`); - o = Je(o) ^ i >> 5; + o = Sn(o) ^ i >> 5; } - o = Je(o); + o = Sn(o); for (let s = 0; s < r; s++) - o = Je(o) ^ e.charCodeAt(s) & 31; + o = Sn(o) ^ e.charCodeAt(s) & 31; for (let s of t) - o = Je(o) ^ s; + o = Sn(o) ^ s; for (let s = 0; s < 6; s++) - o = Je(o); - return o ^= n, Zr.encode(qr([o % Mn[30]], 30, 5, !1)); + o = Sn(o); + return o ^= n, Ho.encode(_o([o % lr[30]], 30, 5, !1)); } // @__NO_SIDE_EFFECTS__ -function Xi(e) { - const t = e === "bech32" ? 1 : 734539939, n = /* @__PURE__ */ $o(5), r = n.decode, o = n.encode, s = Es(r); +function aa(e) { + const t = e === "bech32" ? 1 : 734539939, n = /* @__PURE__ */ Is(5), r = n.decode, o = n.encode, s = Ei(r); function i(l, d, h = 90) { - le("bech32.encode prefix", l), Me(d) && (d = Array.from(d)), Co("bech32.encode", d); + Ae("bech32.encode prefix", l), sn(d) && (d = Array.from(d)), As("bech32.encode", d); const w = l.length; if (w === 0) throw new TypeError(`Invalid prefix length ${w}`); - const p = w + 7 + d.length; - if (h !== !1 && p > h) - throw new TypeError(`Length ${p} exceeds limit ${h}`); - const m = l.toLowerCase(), x = Ts(m, d, t); - return `${m}1${Zr.encode(d)}${x}`; + const g = w + 7 + d.length; + if (h !== !1 && g > h) + throw new TypeError(`Length ${g} exceeds limit ${h}`); + const y = l.toLowerCase(), S = Si(y, d, t); + return `${y}1${Ho.encode(d)}${S}`; } function c(l, d = 90) { - le("bech32.decode input", l); + Ae("bech32.decode input", l); const h = l.length; if (h < 8 || d !== !1 && h > d) throw new TypeError(`invalid string length: ${h} (${l}). Expected (8..${d})`); const w = l.toLowerCase(); if (l !== w && l !== l.toUpperCase()) throw new Error("String must be lowercase or uppercase"); - const p = w.lastIndexOf("1"); - if (p === 0 || p === -1) + const g = w.lastIndexOf("1"); + if (g === 0 || g === -1) throw new Error('Letter "1" must be present between prefix and data only'); - const m = w.slice(0, p), x = w.slice(p + 1); - if (x.length < 6) + const y = w.slice(0, g), S = w.slice(g + 1); + if (S.length < 6) throw new Error("Data must be at least 6 characters long"); - const b = Zr.decode(x).slice(0, -6), v = Ts(m, b, t); - if (!x.endsWith(v)) - throw new Error(`Invalid checksum in ${l}: expected "${v}"`); - return { prefix: m, words: b }; + const v = Ho.decode(S).slice(0, -6), O = Si(y, v, t); + if (!S.endsWith(O)) + throw new Error(`Invalid checksum in ${l}: expected "${O}"`); + return { prefix: y, words: v }; } - const a = Es(c); + const a = Ei(c); function u(l) { const { prefix: d, words: h } = c(l, !1); return { prefix: d, words: h, bytes: r(h) }; @@ -2042,22 +2041,22 @@ function Xi(e) { toWords: o }; } -const Xr = /* @__PURE__ */ Xi("bech32"), Ne = /* @__PURE__ */ Xi("bech32m"), su = { +const Vo = /* @__PURE__ */ aa("bech32"), je = /* @__PURE__ */ aa("bech32m"), zf = { encode: (e) => new TextDecoder().decode(e), decode: (e) => new TextEncoder().encode(e) -}, iu = typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", cu = { +}, Gf = typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", qf = { encode(e) { - return qi(e), e.toHex(); + return sa(e), e.toHex(); }, decode(e) { - return le("hex", e), Uint8Array.fromHex(e); + return Ae("hex", e), Uint8Array.fromHex(e); } -}, S = iu ? cu : /* @__PURE__ */ Tn(/* @__PURE__ */ $o(4), /* @__PURE__ */ br("0123456789abcdef"), /* @__PURE__ */ Er(""), /* @__PURE__ */ Qa((e) => { +}, $ = Gf ? qf : /* @__PURE__ */ Gn(/* @__PURE__ */ Is(4), /* @__PURE__ */ Qr("0123456789abcdef"), /* @__PURE__ */ Jr(""), /* @__PURE__ */ Hf((e) => { if (typeof e != "string" || e.length % 2 !== 0) throw new TypeError(`hex.decode: expected string, got ${typeof e} with length ${e.length}`); return e.toLowerCase(); -})), X = /* @__PURE__ */ Uint8Array.of(), Qi = /* @__PURE__ */ Uint8Array.of(0); -function Ke(e, t) { +})), ot = /* @__PURE__ */ Uint8Array.of(), ua = /* @__PURE__ */ Uint8Array.of(0); +function cn(e, t) { if (e.length !== t.length) return !1; for (let n = 0; n < e.length; n++) @@ -2065,14 +2064,14 @@ function Ke(e, t) { return !1; return !0; } -function Ot(e) { +function Ct(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function au(...e) { +function jf(...e) { let t = 0; for (let r = 0; r < e.length; r++) { const o = e[r]; - if (!Ot(o)) + if (!Ct(o)) throw new Error("Uint8Array expected"); t += o.length; } @@ -2083,48 +2082,48 @@ function au(...e) { } return n; } -const Ji = (e) => new DataView(e.buffer, e.byteOffset, e.byteLength); -function vn(e) { +const fa = (e) => new DataView(e.buffer, e.byteOffset, e.byteLength); +function qn(e) { return Object.prototype.toString.call(e) === "[object Object]"; } -function Gt(e) { +function Qt(e) { return Number.isSafeInteger(e); } -const Lo = { - equalBytes: Ke, - isBytes: Ot, - concatBytes: au -}, tc = (e) => { - if (e !== null && typeof e != "string" && !Pt(e) && !Ot(e) && !Gt(e)) +const ks = { + equalBytes: cn, + isBytes: Ct, + concatBytes: jf +}, la = (e) => { + if (e !== null && typeof e != "string" && !Ft(e) && !Ct(e) && !Qt(e)) throw new Error(`lengthCoder: expected null | number | Uint8Array | CoderType, got ${e} (${typeof e})`); return { encodeStream(t, n) { if (e === null) return; - if (Pt(e)) + if (Ft(e)) return e.encodeStream(t, n); let r; - if (typeof e == "number" ? r = e : typeof e == "string" && (r = ne.resolve(t.stack, e)), typeof r == "bigint" && (r = Number(r)), r === void 0 || r !== n) + if (typeof e == "number" ? r = e : typeof e == "string" && (r = fe.resolve(t.stack, e)), typeof r == "bigint" && (r = Number(r)), r === void 0 || r !== n) throw t.err(`Wrong length: ${r} len=${e} exp=${n} (${typeof n})`); }, decodeStream(t) { let n; - if (Pt(e) ? n = Number(e.decodeStream(t)) : typeof e == "number" ? n = e : typeof e == "string" && (n = ne.resolve(t.stack, e)), typeof n == "bigint" && (n = Number(n)), typeof n != "number") + if (Ft(e) ? n = Number(e.decodeStream(t)) : typeof e == "number" ? n = e : typeof e == "string" && (n = fe.resolve(t.stack, e)), typeof n == "bigint" && (n = Number(n)), typeof n != "number") throw t.err(`Wrong length: ${n}`); return n; } }; -}, at = { +}, dt = { BITS: 32, FULL_MASK: -1 >>> 0, // 1<<32 will overflow len: (e) => Math.ceil(e / 32), - create: (e) => new Uint32Array(at.len(e)), + create: (e) => new Uint32Array(dt.len(e)), clean: (e) => e.fill(0), debug: (e) => Array.from(e).map((t) => (t >>> 0).toString(2).padStart(32, "0")), checkLen: (e, t) => { - if (at.len(t) !== e.length) - throw new Error(`wrong length=${e.length}. Expected: ${at.len(t)}`); + if (dt.len(t) !== e.length) + throw new Error(`wrong length=${e.length}. Expected: ${dt.len(t)}`); }, chunkLen: (e, t, n) => { if (t < 0) @@ -2138,8 +2137,8 @@ const Lo = { mask: 1 << 32 - (e + t) % 32 - 1 }), indices: (e, t, n = !1) => { - at.checkLen(e, t); - const { FULL_MASK: r, BITS: o } = at, s = o - t % o, i = s ? r >>> s << s : r, c = []; + dt.checkLen(e, t); + const { FULL_MASK: r, BITS: o } = dt, s = o - t % o, i = s ? r >>> s << s : r, c = []; for (let a = 0; a < e.length; a++) { let u = e[a]; if (n && (u = ~u), a === e.length - 1 && (u &= i), u !== 0) @@ -2157,21 +2156,21 @@ const Lo = { n === void 0 || r !== n.pos + n.length ? t.push(n = { pos: r, length: 1 }) : n.length += 1; return t; }, - rangeDebug: (e, t, n = !1) => `[${at.range(at.indices(e, t, n)).map((r) => `(${r.pos}/${r.length})`).join(", ")}]`, + rangeDebug: (e, t, n = !1) => `[${dt.range(dt.indices(e, t, n)).map((r) => `(${r.pos}/${r.length})`).join(", ")}]`, setRange: (e, t, n, r, o = !0) => { - at.chunkLen(t, n, r); - const { FULL_MASK: s, BITS: i } = at, c = n % i ? Math.floor(n / i) : void 0, a = n + r, u = a % i ? Math.floor(a / i) : void 0; + dt.chunkLen(t, n, r); + const { FULL_MASK: s, BITS: i } = dt, c = n % i ? Math.floor(n / i) : void 0, a = n + r, u = a % i ? Math.floor(a / i) : void 0; if (c !== void 0 && c === u) - return at.set(e, c, s >>> i - r << i - r - n, o); - if (c !== void 0 && !at.set(e, c, s >>> n % i, o)) + return dt.set(e, c, s >>> i - r << i - r - n, o); + if (c !== void 0 && !dt.set(e, c, s >>> n % i, o)) return !1; const f = c !== void 0 ? c + 1 : n / i, l = u !== void 0 ? u : a / i; for (let d = f; d < l; d++) - if (!at.set(e, d, s, o)) + if (!dt.set(e, d, s, o)) return !1; - return !(u !== void 0 && c !== u && !at.set(e, u, s << i - a % i, o)); + return !(u !== void 0 && c !== u && !dt.set(e, u, s << i - a % i, o)); } -}, ne = { +}, fe = { /** * Internal method for handling stack of paths (debug, errors, dynamic fields via path) * This is looks ugly (callback), but allows us to force stack cleaning by construction (.pop always after function). @@ -2193,7 +2192,7 @@ const Lo = { return t.join("/"); }, err: (e, t, n) => { - const r = new Error(`${e}(${ne.path(t)}): ${typeof n == "string" ? n : n.message}`); + const r = new Error(`${e}(${fe.path(t)}): ${typeof n == "string" ? n : n.message}`); return n instanceof Error && n.stack && (r.stack = n.stack), r; }, resolve: (e, t) => { @@ -2210,7 +2209,7 @@ const Lo = { return s; } }; -class Po { +class Bs { pos = 0; data; opts; @@ -2223,16 +2222,16 @@ class Po { // bitset view; constructor(t, n = {}, r = [], o = void 0, s = 0) { - this.data = t, this.opts = n, this.stack = r, this.parent = o, this.parentOffset = s, this.view = Ji(t); + this.data = t, this.opts = n, this.stack = r, this.parent = o, this.parentOffset = s, this.view = fa(t); } /** Internal method for pointers. */ _enablePointers() { if (this.parent) return this.parent._enablePointers(); - this.bs || (this.bs = at.create(this.data.length), at.setRange(this.bs, this.data.length, 0, this.pos, this.opts.allowMultipleReads)); + this.bs || (this.bs = dt.create(this.data.length), dt.setRange(this.bs, this.data.length, 0, this.pos, this.opts.allowMultipleReads)); } markBytesBS(t, n) { - return this.parent ? this.parent.markBytesBS(this.parentOffset + t, n) : !n || !this.bs ? !0 : at.setRange(this.bs, this.data.length, t, n, !1); + return this.parent ? this.parent.markBytesBS(this.parentOffset + t, n) : !n || !this.bs ? !0 : dt.setRange(this.bs, this.data.length, t, n, !1); } markBytes(t) { const n = this.pos; @@ -2243,7 +2242,7 @@ class Po { return r; } pushObj(t, n) { - return ne.pushObj(this.stack, t, n); + return fe.pushObj(this.stack, t, n); } readView(t, n) { if (!Number.isFinite(t)) @@ -2262,27 +2261,27 @@ class Po { finish() { if (!this.opts.allowUnreadBytes) { if (this.bitPos) - throw this.err(`${this.bitPos} bits left after unpack: ${S.encode(this.data.slice(this.pos))}`); + throw this.err(`${this.bitPos} bits left after unpack: ${$.encode(this.data.slice(this.pos))}`); if (this.bs && !this.parent) { - const t = at.indices(this.bs, this.data.length, !0); + const t = dt.indices(this.bs, this.data.length, !0); if (t.length) { - const n = at.range(t).map(({ pos: r, length: o }) => `(${r}/${o})[${S.encode(this.data.subarray(r, r + o))}]`).join(", "); + const n = dt.range(t).map(({ pos: r, length: o }) => `(${r}/${o})[${$.encode(this.data.subarray(r, r + o))}]`).join(", "); throw this.err(`unread byte ranges: ${n} (total=${this.data.length})`); } else return; } if (!this.isEnd()) - throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${S.encode(this.data.slice(this.pos))}`); + throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${$.encode(this.data.slice(this.pos))}`); } } // User methods err(t) { - return ne.err("Reader", this.stack, t); + return fe.err("Reader", this.stack, t); } offsetReader(t) { if (t > this.data.length) throw this.err("offsetReader: Unexpected end of buffer"); - return new Po(this.absBytes(t), this.opts, this.stack, this, t); + return new Bs(this.absBytes(t), this.opts, this.stack, this, t); } bytes(t, n = !1) { if (this.bitPos) @@ -2324,7 +2323,7 @@ class Po { return n >>> 0; } find(t, n = this.pos) { - if (!Ot(t)) + if (!Ct(t)) throw this.err(`find: needle is not bytes! ${t}`); if (this.bitPos) throw this.err("findByte: bitPos not empty"); @@ -2333,12 +2332,12 @@ class Po { for (let r = n; (r = this.data.indexOf(t[0], r)) !== -1; r++) { if (r === -1 || this.data.length - r < t.length) return; - if (Ke(t, this.data.subarray(r, r + t.length))) + if (cn(t, this.data.subarray(r, r + t.length))) return r; } } } -class uu { +class Yf { pos = 0; stack; // We could have a single buffer here and re-alloc it with @@ -2352,15 +2351,15 @@ class uu { view; finished = !1; constructor(t = []) { - this.stack = t, this.view = Ji(this.viewBuf); + this.stack = t, this.view = fa(this.viewBuf); } pushObj(t, n) { - return ne.pushObj(this.stack, t, n); + return fe.pushObj(this.stack, t, n); } writeView(t, n) { if (this.finished) throw this.err("buffer: finished"); - if (!Gt(t) || t > 8) + if (!Qt(t) || t > 8) throw new Error(`wrong writeView length=${t}`); n(this.view), this.bytes(this.viewBuf.slice(0, t)), this.viewBuf.fill(0); } @@ -2368,7 +2367,7 @@ class uu { err(t) { if (this.finished) throw this.err("buffer: finished"); - return ne.err("Reader", this.stack, t); + return fe.err("Reader", this.stack, t); } bytes(t) { if (this.finished) @@ -2417,8 +2416,8 @@ class uu { } } } -const Qr = (e) => Uint8Array.from(e).reverse(); -function fu(e, t, n) { +const Do = (e) => Uint8Array.from(e).reverse(); +function Zf(e, t, n) { if (n) { const r = 2n ** (t - 1n); if (e < -r || e >= r) @@ -2426,28 +2425,28 @@ function fu(e, t, n) { } else if (0n > e || e >= 2n ** t) throw new Error(`value out of unsigned bounds. Expected 0 <= ${e} < ${2n ** t}`); } -function ec(e) { +function da(e) { return { // NOTE: we cannot export validate here, since it is likely mistake. encodeStream: e.encodeStream, decodeStream: e.decodeStream, size: e.size, encode: (t) => { - const n = new uu(); + const n = new Yf(); return e.encodeStream(n, t), n.finish(); }, decode: (t, n = {}) => { - const r = new Po(t, n), o = e.decodeStream(r); + const r = new Bs(t, n), o = e.decodeStream(r); return r.finish(), o; } }; } -function xt(e, t) { - if (!Pt(e)) +function Tt(e, t) { + if (!Ft(e)) throw new Error(`validate: invalid inner value ${e}`); if (typeof t != "function") throw new Error("validate: fn should be function"); - return ec({ + return da({ size: e.size, encodeStream: (n, r) => { let o; @@ -2468,14 +2467,14 @@ function xt(e, t) { } }); } -const bt = (e) => { - const t = ec(e); - return e.validate ? xt(t, e.validate) : t; -}, Sr = (e) => vn(e) && typeof e.decode == "function" && typeof e.encode == "function"; -function Pt(e) { - return vn(e) && Sr(e) && typeof e.encodeStream == "function" && typeof e.decodeStream == "function" && (e.size === void 0 || Gt(e.size)); +const At = (e) => { + const t = da(e); + return e.validate ? Tt(t, e.validate) : t; +}, to = (e) => qn(e) && typeof e.decode == "function" && typeof e.encode == "function"; +function Ft(e) { + return qn(e) && to(e) && typeof e.encodeStream == "function" && typeof e.decodeStream == "function" && (e.size === void 0 || Qt(e.size)); } -function lu() { +function Xf() { return { encode: (e) => { if (!Array.isArray(e)) @@ -2492,13 +2491,13 @@ function lu() { return t; }, decode: (e) => { - if (!vn(e)) + if (!qn(e)) throw new Error(`expected plain object, got ${e}`); return Object.entries(e); } }; } -const du = { +const Qf = { encode: (e) => { if (typeof e != "bigint") throw new Error(`expected bigint, got ${typeof e}`); @@ -2507,17 +2506,17 @@ const du = { return Number(e); }, decode: (e) => { - if (!Gt(e)) + if (!Qt(e)) throw new Error("element is not a safe integer"); return BigInt(e); } }; -function hu(e) { - if (!vn(e)) +function Jf(e) { + if (!qn(e)) throw new Error("plain object expected"); return { encode: (t) => { - if (!Gt(t) || !(t in e)) + if (!Qt(t) || !(t in e)) throw new Error(`wrong value ${t}`); return e[t]; }, @@ -2528,8 +2527,8 @@ function hu(e) { } }; } -function pu(e, t = !1) { - if (!Gt(e)) +function tl(e, t = !1) { + if (!Qt(e)) throw new Error(`decimal/precision: wrong value ${e}`); if (typeof t != "boolean") throw new Error(`decimal/round: expected boolean, got ${typeof t}`); @@ -2564,11 +2563,11 @@ function pu(e, t = !1) { } }; } -function gu(e) { +function el(e) { if (!Array.isArray(e)) throw new Error(`expected array, got ${typeof e}`); for (const t of e) - if (!Sr(t)) + if (!to(t)) throw new Error(`wrong base coder ${t}`); return { encode: (t) => { @@ -2589,12 +2588,12 @@ function gu(e) { } }; } -const nc = (e) => { - if (!Sr(e)) +const ha = (e) => { + if (!to(e)) throw new Error("BaseCoder expected"); return { encode: e.decode, decode: e.encode }; -}, Tr = { dict: lu, numberBigint: du, tsEnum: hu, decimal: pu, match: gu, reverse: nc }, _o = (e, t = !1, n = !1, r = !0) => { - if (!Gt(e)) +}, eo = { dict: Xf, numberBigint: Qf, tsEnum: Jf, decimal: tl, match: el, reverse: ha }, Os = (e, t = !1, n = !1, r = !0) => { + if (!Qt(e)) throw new Error(`bigint/size: wrong value ${e}`); if (typeof t != "boolean") throw new Error(`bigint/le: expected boolean, got ${typeof t}`); @@ -2603,7 +2602,7 @@ const nc = (e) => { if (typeof r != "boolean") throw new Error(`bigint/sized: expected boolean, got ${typeof r}`); const o = BigInt(e), s = 2n ** (8n * o - 1n); - return bt({ + return At({ size: r ? e : void 0, encodeStream: (i, c) => { n && c < 0 && (c = c | s); @@ -2620,7 +2619,7 @@ const nc = (e) => { i.bytes(t ? u.reverse() : u); }, decodeStream: (i) => { - const c = i.bytes(r ? e : Math.min(e, i.leftBytes)), a = t ? c : Qr(c); + const c = i.bytes(r ? e : Math.min(e, i.leftBytes)), a = t ? c : Do(c); let u = 0n; for (let f = 0; f < a.length; f++) u |= BigInt(a[f]) << 8n * BigInt(f); @@ -2629,10 +2628,10 @@ const nc = (e) => { validate: (i) => { if (typeof i != "bigint") throw new Error(`bigint: invalid value: ${i}`); - return fu(i, 8n * o, !!n), i; + return Zf(i, 8n * o, !!n), i; } }); -}, rc = /* @__PURE__ */ _o(32, !1), Kn = /* @__PURE__ */ _o(8, !0), wu = /* @__PURE__ */ _o(8, !0, !0), yu = (e, t) => bt({ +}, pa = /* @__PURE__ */ Os(32, !1), dr = /* @__PURE__ */ Os(8, !0), nl = /* @__PURE__ */ Os(8, !0, !0), rl = (e, t) => At({ size: e, encodeStream: (n, r) => n.writeView(e, (o) => t.write(o, r)), decodeStream: (n) => n.readView(e, t.read), @@ -2641,46 +2640,46 @@ const nc = (e) => { throw new Error(`viewCoder: expected number, got ${typeof n}`); return t.validate && t.validate(n), n; } -}), kn = (e, t, n) => { +}), jn = (e, t, n) => { const r = e * 8, o = 2 ** (r - 1), s = (a) => { - if (!Gt(a)) + if (!Qt(a)) throw new Error(`sintView: value is not safe integer: ${a}`); if (a < -o || a >= o) throw new Error(`sintView: value out of bounds. Expected ${-o} <= ${a} < ${o}`); }, i = 2 ** r, c = (a) => { - if (!Gt(a)) + if (!Qt(a)) throw new Error(`uintView: value is not safe integer: ${a}`); if (0 > a || a >= i) throw new Error(`uintView: value out of bounds. Expected 0 <= ${a} < ${i}`); }; - return yu(e, { + return rl(e, { write: n.write, read: n.read, validate: t ? s : c }); -}, W = /* @__PURE__ */ kn(4, !1, { +}, Z = /* @__PURE__ */ jn(4, !1, { read: (e, t) => e.getUint32(t, !0), write: (e, t) => e.setUint32(0, t, !0) -}), mu = /* @__PURE__ */ kn(4, !1, { +}), ol = /* @__PURE__ */ jn(4, !1, { read: (e, t) => e.getUint32(t, !1), write: (e, t) => e.setUint32(0, t, !1) -}), Re = /* @__PURE__ */ kn(4, !0, { +}), Ye = /* @__PURE__ */ jn(4, !0, { read: (e, t) => e.getInt32(t, !0), write: (e, t) => e.setInt32(0, t, !0) -}), vs = /* @__PURE__ */ kn(2, !1, { +}), vi = /* @__PURE__ */ jn(2, !1, { read: (e, t) => e.getUint16(t, !0), write: (e, t) => e.setUint16(0, t, !0) -}), ae = /* @__PURE__ */ kn(1, !1, { +}), Se = /* @__PURE__ */ jn(1, !1, { read: (e, t) => e.getUint8(t), write: (e, t) => e.setUint8(0, t) -}), Z = (e, t = !1) => { +}), rt = (e, t = !1) => { if (typeof t != "boolean") throw new Error(`bytes/le: expected boolean, got ${typeof t}`); - const n = tc(e), r = Ot(e); - return bt({ + const n = la(e), r = Ct(e); + return At({ size: typeof e == "number" ? e : void 0, encodeStream: (o, s) => { - r || n.encodeStream(o, s.length), o.bytes(t ? Qr(s) : s), r && o.bytes(e); + r || n.encodeStream(o, s.length), o.bytes(t ? Do(s) : s), r && o.bytes(e); }, decodeStream: (o) => { let s; @@ -2691,30 +2690,30 @@ const nc = (e) => { s = o.bytes(i - o.pos), o.bytes(e.length); } else s = o.bytes(e === null ? o.leftBytes : n.decodeStream(o)); - return t ? Qr(s) : s; + return t ? Do(s) : s; }, validate: (o) => { - if (!Ot(o)) + if (!Ct(o)) throw new Error(`bytes: invalid value ${o}`); return o; } }); }; -function xu(e, t) { - if (!Pt(t)) +function sl(e, t) { + if (!Ft(t)) throw new Error(`prefix: invalid inner value ${t}`); - return de(Z(e), nc(t)); + return Ie(rt(e), ha(t)); } -const Vo = (e, t = !1) => xt(de(Z(e, t), su), (n) => { +const $s = (e, t = !1) => Tt(Ie(rt(e, t), zf), (n) => { if (typeof n != "string") throw new Error(`expected string, got ${typeof n}`); return n; -}), bu = (e, t = { isLE: !1, with0x: !1 }) => { - let n = de(Z(e, t.isLE), S); +}), il = (e, t = { isLE: !1, with0x: !1 }) => { + let n = Ie(rt(e, t.isLE), $); const r = t.with0x; if (typeof r != "boolean") throw new Error(`hex/with0x: expected boolean, got ${typeof r}`); - return r && (n = de(n, { + return r && (n = Ie(n, { encode: (o) => `0x${o}`, decode: (o) => { if (!o.startsWith("0x")) @@ -2723,12 +2722,12 @@ const Vo = (e, t = !1) => xt(de(Z(e, t), su), (n) => { } })), n; }; -function de(e, t) { - if (!Pt(e)) +function Ie(e, t) { + if (!Ft(e)) throw new Error(`apply: invalid inner value ${e}`); - if (!Sr(t)) + if (!to(t)) throw new Error(`apply: invalid base value ${e}`); - return bt({ + return At({ size: e.size, encodeStream: (n, r) => { let o; @@ -2749,19 +2748,19 @@ function de(e, t) { } }); } -const Eu = (e, t = !1) => { - if (!Ot(e)) +const cl = (e, t = !1) => { + if (!Ct(e)) throw new Error(`flag/flagValue: expected Uint8Array, got ${typeof e}`); if (typeof t != "boolean") throw new Error(`flag/xor: expected boolean, got ${typeof t}`); - return bt({ + return At({ size: e.length, encodeStream: (n, r) => { !!r !== t && n.bytes(e); }, decodeStream: (n) => { let r = n.leftBytes >= e.length; - return r && (r = Ke(n.bytes(e.length, !0), e), r && n.bytes(e.length)), r !== t; + return r && (r = cn(n.bytes(e.length, !0), e), r && n.bytes(e.length)), r !== t; }, validate: (n) => { if (n !== void 0 && typeof n != "boolean") @@ -2770,31 +2769,31 @@ const Eu = (e, t = !1) => { } }); }; -function Su(e, t, n) { - if (!Pt(t)) +function al(e, t, n) { + if (!Ft(t)) throw new Error(`flagged: invalid inner value ${t}`); - return bt({ + return At({ encodeStream: (r, o) => { - ne.resolve(r.stack, e) && t.encodeStream(r, o); + fe.resolve(r.stack, e) && t.encodeStream(r, o); }, decodeStream: (r) => { let o = !1; - if (o = !!ne.resolve(r.stack, e), o) + if (o = !!fe.resolve(r.stack, e), o) return t.decodeStream(r); } }); } -function Do(e, t, n = !0) { - if (!Pt(e)) +function Us(e, t, n = !0) { + if (!Ft(e)) throw new Error(`magic: invalid inner value ${e}`); if (typeof n != "boolean") throw new Error(`magic: expected boolean, got ${typeof n}`); - return bt({ + return At({ size: e.size, encodeStream: (r, o) => e.encodeStream(r, t), decodeStream: (r) => { const o = e.decodeStream(r); - if (n && typeof o != "object" && o !== t || Ot(t) && !Ke(t, o)) + if (n && typeof o != "object" && o !== t || Ct(t) && !cn(t, o)) throw r.err(`magic: invalid value: ${o} !== ${t}`); }, validate: (r) => { @@ -2804,25 +2803,25 @@ function Do(e, t, n = !0) { } }); } -function oc(e) { +function ga(e) { let t = 0; for (const n of e) { if (n.size === void 0) return; - if (!Gt(n.size)) + if (!Qt(n.size)) throw new Error(`sizeof: wrong element size=${t}`); t += n.size; } return t; } -function ft(e) { - if (!vn(e)) +function pt(e) { + if (!qn(e)) throw new Error(`struct: expected plain object, got ${e}`); for (const t in e) - if (!Pt(e[t])) + if (!Ft(e[t])) throw new Error(`struct: field ${t} is not CoderType`); - return bt({ - size: oc(Object.values(e)), + return At({ + size: ga(Object.values(e)), encodeStream: (t, n) => { t.pushObj(n, (r) => { for (const o in e) @@ -2843,14 +2842,14 @@ function ft(e) { } }); } -function Tu(e) { +function ul(e) { if (!Array.isArray(e)) throw new Error(`Packed.Tuple: got ${typeof e} instead of array`); for (let t = 0; t < e.length; t++) - if (!Pt(e[t])) + if (!Ft(e[t])) throw new Error(`tuple: field ${t} is not CoderType`); - return bt({ - size: oc(e), + return At({ + size: ga(e), encodeStream: (t, n) => { if (!Array.isArray(n)) throw t.err(`tuple: invalid value ${n}`); @@ -2875,28 +2874,28 @@ function Tu(e) { } }); } -function mt(e, t) { - if (!Pt(t)) +function vt(e, t) { + if (!Ft(t)) throw new Error(`array: invalid inner value ${t}`); - const n = tc(typeof e == "string" ? `../${e}` : e); - return bt({ + const n = la(typeof e == "string" ? `../${e}` : e); + return At({ size: typeof e == "number" && t.size ? e * t.size : void 0, encodeStream: (r, o) => { const s = r; s.pushObj(o, (i) => { - Ot(e) || n.encodeStream(r, o.length); + Ct(e) || n.encodeStream(r, o.length); for (let c = 0; c < o.length; c++) i(`${c}`, () => { const a = o[c], u = r.pos; - if (t.encodeStream(r, a), Ot(e)) { + if (t.encodeStream(r, a), Ct(e)) { if (e.length > s.pos - u) return; const f = s.finish(!1).subarray(u, s.pos); - if (Ke(f.subarray(0, e.length), e)) + if (cn(f.subarray(0, e.length), e)) throw s.err(`array: inner element encoding same as separator. elm=${a} data=${f}`); } }); - }), Ot(e) && r.bytes(e); + }), Ct(e) && r.bytes(e); }, decodeStream: (r) => { const o = []; @@ -2904,9 +2903,9 @@ function mt(e, t) { if (e === null) for (let i = 0; !r.isEnd() && (s(`${i}`, () => o.push(t.decodeStream(r))), !(t.size && r.leftBytes < t.size)); i++) ; - else if (Ot(e)) + else if (Ct(e)) for (let i = 0; ; i++) { - if (Ke(r.bytes(e.length, !0), e)) { + if (cn(r.bytes(e.length, !0), e)) { r.bytes(e.length); break; } @@ -2927,62 +2926,62 @@ function mt(e, t) { } }); } -const Qe = Ft.Point, ks = Qe.Fn, sc = Qe.Fn.ORDER, An = (e) => e % 2n === 0n, q = Lo.isBytes, ce = Lo.concatBytes, rt = Lo.equalBytes, ic = (e) => Za(ht(e)), se = (...e) => ht(ht(ce(...e))), Jr = Nt.utils.randomSecretKey, Ho = Nt.getPublicKey, cc = Ft.getPublicKey, As = (e) => e.r < sc / 2n; -function vu(e, t, n = !1) { - let r = Ft.Signature.fromBytes(Ft.sign(e, t, { prehash: !1 })); - if (n && !As(r)) { +const mn = xe.Point, Ti = mn.Fn, wa = mn.Fn.ORDER, Yn = (e) => e % 2n === 0n, nt = ks.isBytes, Ee = ks.concatBytes, ct = ks.equalBytes, ya = (e) => _f(wt(e)), ge = (...e) => wt(wt(Ee(...e))), Mo = de.utils.randomSecretKey, Rs = de.getPublicKey, ma = xe.getPublicKey, Ai = (e) => e.r < wa / 2n; +function fl(e, t, n = !1) { + let r = xe.Signature.fromBytes(xe.sign(e, t, { prehash: !1 })); + if (n && !Ai(r)) { const o = new Uint8Array(32); let s = 0; - for (; !As(r); ) - if (o.set(W.encode(s++)), r = Ft.Signature.fromBytes(Ft.sign(e, t, { prehash: !1, extraEntropy: o })), s > 4294967295) + for (; !Ai(r); ) + if (o.set(Z.encode(s++)), r = xe.Signature.fromBytes(xe.sign(e, t, { prehash: !1, extraEntropy: o })), s > 4294967295) throw new Error("lowR counter overflow: report the error"); } return r.toBytes("der"); } -const Is = Nt.sign, Mo = Nt.utils.taggedHash, St = { +const Ii = de.sign, Ns = de.utils.taggedHash, kt = { ecdsa: 0, schnorr: 1 }; -function Fe(e, t) { +function an(e, t) { const n = e.length; - if (t === St.ecdsa) { + if (t === kt.ecdsa) { if (n === 32) throw new Error("Expected non-Schnorr key"); - return Qe.fromBytes(e), e; - } else if (t === St.schnorr) { + return mn.fromBytes(e), e; + } else if (t === kt.schnorr) { if (n !== 32) throw new Error("Expected 32-byte Schnorr key"); - return Nt.utils.lift_x(qt(e)), e; + return de.utils.lift_x(le(e)), e; } else throw new Error("Unknown key type"); } -function ac(e, t) { - const r = Nt.utils.taggedHash("TapTweak", e, t), o = qt(r); - if (o >= sc) +function ba(e, t) { + const r = de.utils.taggedHash("TapTweak", e, t), o = le(r); + if (o >= wa) throw new Error("tweak higher than curve order"); return o; } -function ku(e, t = Uint8Array.of()) { - const n = Nt.utils, r = qt(e), o = Qe.BASE.multiply(r), s = An(o.y) ? r : ks.neg(r), i = n.pointToBytes(o), c = ac(i, t); - return Sn(ks.add(s, c), 32); +function ll(e, t = Uint8Array.of()) { + const n = de.utils, r = le(e), o = mn.BASE.multiply(r), s = Yn(o.y) ? r : Ti.neg(r), i = n.pointToBytes(o), c = ba(i, t); + return zn(Ti.add(s, c), 32); } -function to(e, t) { - const n = Nt.utils, r = ac(e, t), s = n.lift_x(qt(e)).add(Qe.BASE.multiply(r)), i = An(s.y) ? 0 : 1; +function Ko(e, t) { + const n = de.utils, r = ba(e, t), s = n.lift_x(le(e)).add(mn.BASE.multiply(r)), i = Yn(s.y) ? 0 : 1; return [n.pointToBytes(s), i]; } -const Ko = ht(Qe.BASE.toBytes(!1)), We = { +const Ls = wt(mn.BASE.toBytes(!1)), un = { bech32: "bc", pubKeyHash: 0, scriptHash: 5, wif: 128 -}, Rn = { +}, tr = { bech32: "tb", pubKeyHash: 111, scriptHash: 196, wif: 239 }; -function er(e, t) { - if (!q(e) || !q(t)) +function Ir(e, t) { + if (!nt(e) || !nt(t)) throw new Error(`cmp: wrong type a=${typeof e} b=${typeof t}`); const n = Math.min(e.length, t.length); for (let r = 0; r < n; r++) @@ -2990,7 +2989,7 @@ function er(e, t) { return Math.sign(e[r] - t[r]); return Math.sign(e.length - t.length); } -function uc(e) { +function Ea(e) { const t = {}; for (const n in e) { if (t[e[n]] !== void 0) @@ -2999,7 +2998,7 @@ function uc(e) { } return t; } -const ct = { +const lt = { OP_0: 0, PUSHDATA1: 76, PUSHDATA2: 77, @@ -3122,9 +3121,9 @@ const ct = { CHECKSIGADD: 186, // Invalid INVALID: 255 -}, Au = uc(ct); -function Fo(e = 6, t = !1) { - return bt({ +}, dl = Ea(lt); +function Cs(e = 6, t = !1) { + return At({ encodeStream: (n, r) => { if (r === 0n) return; @@ -3151,63 +3150,63 @@ function Fo(e = 6, t = !1) { } }); } -function Iu(e, t = 4, n = !0) { +function hl(e, t = 4, n = !0) { if (typeof e == "number") return e; - if (q(e)) + if (nt(e)) try { - const r = Fo(t, n).decode(e); + const r = Cs(t, n).decode(e); return r > Number.MAX_SAFE_INTEGER ? void 0 : Number(r); } catch { return; } } -const L = bt({ +const K = At({ encodeStream: (e, t) => { for (let n of t) { if (typeof n == "string") { - if (ct[n] === void 0) + if (lt[n] === void 0) throw new Error(`Unknown opcode=${n}`); - e.byte(ct[n]); + e.byte(lt[n]); continue; } else if (typeof n == "number") { if (n === 0) { e.byte(0); continue; } else if (1 <= n && n <= 16) { - e.byte(ct.OP_1 - 1 + n); + e.byte(lt.OP_1 - 1 + n); continue; } } - if (typeof n == "number" && (n = Fo().encode(BigInt(n))), !q(n)) + if (typeof n == "number" && (n = Cs().encode(BigInt(n))), !nt(n)) throw new Error(`Wrong Script OP=${n} (${typeof n})`); const r = n.length; - r < ct.PUSHDATA1 ? e.byte(r) : r <= 255 ? (e.byte(ct.PUSHDATA1), e.byte(r)) : r <= 65535 ? (e.byte(ct.PUSHDATA2), e.bytes(vs.encode(r))) : (e.byte(ct.PUSHDATA4), e.bytes(W.encode(r))), e.bytes(n); + r < lt.PUSHDATA1 ? e.byte(r) : r <= 255 ? (e.byte(lt.PUSHDATA1), e.byte(r)) : r <= 65535 ? (e.byte(lt.PUSHDATA2), e.bytes(vi.encode(r))) : (e.byte(lt.PUSHDATA4), e.bytes(Z.encode(r))), e.bytes(n); } }, decodeStream: (e) => { const t = []; for (; !e.isEnd(); ) { const n = e.byte(); - if (ct.OP_0 < n && n <= ct.PUSHDATA4) { + if (lt.OP_0 < n && n <= lt.PUSHDATA4) { let r; - if (n < ct.PUSHDATA1) + if (n < lt.PUSHDATA1) r = n; - else if (n === ct.PUSHDATA1) - r = ae.decodeStream(e); - else if (n === ct.PUSHDATA2) - r = vs.decodeStream(e); - else if (n === ct.PUSHDATA4) - r = W.decodeStream(e); + else if (n === lt.PUSHDATA1) + r = Se.decodeStream(e); + else if (n === lt.PUSHDATA2) + r = vi.decodeStream(e); + else if (n === lt.PUSHDATA4) + r = Z.decodeStream(e); else throw new Error("Should be not possible"); t.push(e.bytes(r)); } else if (n === 0) t.push(0); - else if (ct.OP_1 <= n && n <= ct.OP_16) - t.push(n - (ct.OP_1 - 1)); + else if (lt.OP_1 <= n && n <= lt.OP_16) + t.push(n - (lt.OP_1 - 1)); else { - const r = Au[n]; + const r = dl[n]; if (r === void 0) throw new Error(`Unknown opcode=${n.toString(16)}`); t.push(r); @@ -3215,15 +3214,15 @@ const L = bt({ } return t; } -}), Bs = { +}), ki = { 253: [253, 2, 253n, 65535n], 254: [254, 4, 65536n, 4294967295n], 255: [255, 8, 4294967296n, 18446744073709551615n] -}, vr = bt({ +}, no = At({ encodeStream: (e, t) => { if (typeof t == "number" && (t = BigInt(t)), 0n <= t && t <= 252n) return e.byte(Number(t)); - for (const [n, r, o, s] of Object.values(Bs)) + for (const [n, r, o, s] of Object.values(ki)) if (!(o > t || t > s)) { e.byte(n); for (let i = 0; i < r; i++) @@ -3236,7 +3235,7 @@ const L = bt({ const t = e.byte(); if (t <= 252) return BigInt(t); - const [n, r, o] = Bs[t]; + const [n, r, o] = ki[t]; let s = 0n; for (let i = 0; i < r; i++) s |= BigInt(e.byte()) << 8n * BigInt(i); @@ -3244,98 +3243,98 @@ const L = bt({ throw e.err(`Wrong CompactSize(${8 * r})`); return s; } -}), _t = de(vr, Tr.numberBigint), $t = Z(vr), dn = mt(_t, $t), nr = (e) => mt(vr, e), fc = ft({ - txid: Z(32, !0), +}), Wt = Ie(no, eo.numberBigint), Mt = rt(no), Cn = vt(Wt, Mt), kr = (e) => vt(no, e), xa = pt({ + txid: rt(32, !0), // hash(prev_tx), - index: W, + index: Z, // output number of previous tx - finalScriptSig: $t, + finalScriptSig: Mt, // btc merges input and output script, executes it. If ok = tx passes - sequence: W + sequence: Z // ? -}), Te = ft({ amount: Kn, script: $t }), Bu = ft({ - version: Re, - segwitFlag: Eu(new Uint8Array([0, 1])), - inputs: nr(fc), - outputs: nr(Te), - witnesses: Su("segwitFlag", mt("inputs/length", dn)), +}), Pe = pt({ amount: dr, script: Mt }), pl = pt({ + version: Ye, + segwitFlag: cl(new Uint8Array([0, 1])), + inputs: kr(xa), + outputs: kr(Pe), + witnesses: al("segwitFlag", vt("inputs/length", Cn)), // < 500000000 Block number at which this transaction is unlocked // >= 500000000 UNIX timestamp at which this transaction is unlocked // Handled as part of PSBTv2 - lockTime: W + lockTime: Z }); -function Ou(e) { +function gl(e) { if (e.segwitFlag && e.witnesses && !e.witnesses.length) throw new Error("Segwit flag with empty witnesses array"); return e; } -const Le = xt(Bu, Ou), on = ft({ - version: Re, - inputs: nr(fc), - outputs: nr(Te), - lockTime: W -}), eo = xt(Z(null), (e) => Fe(e, St.ecdsa)), rr = xt(Z(32), (e) => Fe(e, St.schnorr)), Os = xt(Z(null), (e) => { +const Qe = Tt(pl, gl), kn = pt({ + version: Ye, + inputs: kr(xa), + outputs: kr(Pe), + lockTime: Z +}), Fo = Tt(rt(null), (e) => an(e, kt.ecdsa)), Br = Tt(rt(32), (e) => an(e, kt.schnorr)), Bi = Tt(rt(null), (e) => { if (e.length !== 64 && e.length !== 65) throw new Error("Schnorr signature should be 64 or 65 bytes long"); return e; -}), kr = ft({ - fingerprint: mu, - path: mt(null, W) -}), lc = ft({ - hashes: mt(_t, Z(32)), - der: kr -}), Uu = Z(78), Nu = ft({ pubKey: rr, leafHash: Z(32) }), Ru = ft({ - version: ae, +}), ro = pt({ + fingerprint: ol, + path: vt(null, Z) +}), Sa = pt({ + hashes: vt(Wt, rt(32)), + der: ro +}), wl = rt(78), yl = pt({ pubKey: Br, leafHash: rt(32) }), ml = pt({ + version: Se, // With parity :( - internalKey: Z(32), - merklePath: mt(null, Z(32)) -}), Wt = xt(Ru, (e) => { + internalKey: rt(32), + merklePath: vt(null, rt(32)) +}), Zt = Tt(ml, (e) => { if (e.merklePath.length > 128) throw new Error("TaprootControlBlock: merklePath should be of length 0..128 (inclusive)"); return e; -}), Cu = mt(null, ft({ - depth: ae, - version: ae, - script: $t -})), nt = Z(null), Us = Z(20), tn = Z(32), Wo = { - unsignedTx: [0, !1, on, [0], [0], !1], - xpub: [1, Uu, kr, [], [0, 2], !1], - txVersion: [2, !1, W, [2], [2], !1], - fallbackLocktime: [3, !1, W, [], [2], !1], - inputCount: [4, !1, _t, [2], [2], !1], - outputCount: [5, !1, _t, [2], [2], !1], - txModifiable: [6, !1, ae, [], [2], !1], +}), bl = vt(null, pt({ + depth: Se, + version: Se, + script: Mt +})), it = rt(null), Oi = rt(20), vn = rt(32), _s = { + unsignedTx: [0, !1, kn, [0], [0], !1], + xpub: [1, wl, ro, [], [0, 2], !1], + txVersion: [2, !1, Z, [2], [2], !1], + fallbackLocktime: [3, !1, Z, [], [2], !1], + inputCount: [4, !1, Wt, [2], [2], !1], + outputCount: [5, !1, Wt, [2], [2], !1], + txModifiable: [6, !1, Se, [], [2], !1], // TODO: bitfield - version: [251, !1, W, [], [0, 2], !1], - proprietary: [252, nt, nt, [], [0, 2], !1] -}, Ar = { - nonWitnessUtxo: [0, !1, Le, [], [0, 2], !1], - witnessUtxo: [1, !1, Te, [], [0, 2], !1], - partialSig: [2, eo, nt, [], [0, 2], !1], - sighashType: [3, !1, W, [], [0, 2], !1], - redeemScript: [4, !1, nt, [], [0, 2], !1], - witnessScript: [5, !1, nt, [], [0, 2], !1], - bip32Derivation: [6, eo, kr, [], [0, 2], !1], - finalScriptSig: [7, !1, nt, [], [0, 2], !1], - finalScriptWitness: [8, !1, dn, [], [0, 2], !1], - porCommitment: [9, !1, nt, [], [0, 2], !1], - ripemd160: [10, Us, nt, [], [0, 2], !1], - sha256: [11, tn, nt, [], [0, 2], !1], - hash160: [12, Us, nt, [], [0, 2], !1], - hash256: [13, tn, nt, [], [0, 2], !1], - txid: [14, !1, tn, [2], [2], !0], - index: [15, !1, W, [2], [2], !0], - sequence: [16, !1, W, [], [2], !0], - requiredTimeLocktime: [17, !1, W, [], [2], !1], - requiredHeightLocktime: [18, !1, W, [], [2], !1], - tapKeySig: [19, !1, Os, [], [0, 2], !1], - tapScriptSig: [20, Nu, Os, [], [0, 2], !1], - tapLeafScript: [21, Wt, nt, [], [0, 2], !1], - tapBip32Derivation: [22, tn, lc, [], [0, 2], !1], - tapInternalKey: [23, !1, rr, [], [0, 2], !1], - tapMerkleRoot: [24, !1, tn, [], [0, 2], !1], - proprietary: [252, nt, nt, [], [0, 2], !1] -}, $u = [ + version: [251, !1, Z, [], [0, 2], !1], + proprietary: [252, it, it, [], [0, 2], !1] +}, oo = { + nonWitnessUtxo: [0, !1, Qe, [], [0, 2], !1], + witnessUtxo: [1, !1, Pe, [], [0, 2], !1], + partialSig: [2, Fo, it, [], [0, 2], !1], + sighashType: [3, !1, Z, [], [0, 2], !1], + redeemScript: [4, !1, it, [], [0, 2], !1], + witnessScript: [5, !1, it, [], [0, 2], !1], + bip32Derivation: [6, Fo, ro, [], [0, 2], !1], + finalScriptSig: [7, !1, it, [], [0, 2], !1], + finalScriptWitness: [8, !1, Cn, [], [0, 2], !1], + porCommitment: [9, !1, it, [], [0, 2], !1], + ripemd160: [10, Oi, it, [], [0, 2], !1], + sha256: [11, vn, it, [], [0, 2], !1], + hash160: [12, Oi, it, [], [0, 2], !1], + hash256: [13, vn, it, [], [0, 2], !1], + txid: [14, !1, vn, [2], [2], !0], + index: [15, !1, Z, [2], [2], !0], + sequence: [16, !1, Z, [], [2], !0], + requiredTimeLocktime: [17, !1, Z, [], [2], !1], + requiredHeightLocktime: [18, !1, Z, [], [2], !1], + tapKeySig: [19, !1, Bi, [], [0, 2], !1], + tapScriptSig: [20, yl, Bi, [], [0, 2], !1], + tapLeafScript: [21, Zt, it, [], [0, 2], !1], + tapBip32Derivation: [22, vn, Sa, [], [0, 2], !1], + tapInternalKey: [23, !1, Br, [], [0, 2], !1], + tapMerkleRoot: [24, !1, vn, [], [0, 2], !1], + proprietary: [252, it, it, [], [0, 2], !1] +}, El = [ "txid", "sequence", "index", @@ -3344,40 +3343,40 @@ const Le = xt(Bu, Ou), on = ft({ "finalScriptSig", "finalScriptWitness", "unknown" -], Lu = [ +], xl = [ "partialSig", "finalScriptSig", "finalScriptWitness", "tapKeySig", "tapScriptSig" -], hn = { - redeemScript: [0, !1, nt, [], [0, 2], !1], - witnessScript: [1, !1, nt, [], [0, 2], !1], - bip32Derivation: [2, eo, kr, [], [0, 2], !1], - amount: [3, !1, wu, [2], [2], !0], - script: [4, !1, nt, [2], [2], !0], - tapInternalKey: [5, !1, rr, [], [0, 2], !1], - tapTree: [6, !1, Cu, [], [0, 2], !1], - tapBip32Derivation: [7, rr, lc, [], [0, 2], !1], - proprietary: [252, nt, nt, [], [0, 2], !1] -}, Pu = [], Ns = mt(Qi, ft({ +], _n = { + redeemScript: [0, !1, it, [], [0, 2], !1], + witnessScript: [1, !1, it, [], [0, 2], !1], + bip32Derivation: [2, Fo, ro, [], [0, 2], !1], + amount: [3, !1, nl, [2], [2], !0], + script: [4, !1, it, [2], [2], !0], + tapInternalKey: [5, !1, Br, [], [0, 2], !1], + tapTree: [6, !1, bl, [], [0, 2], !1], + tapBip32Derivation: [7, Br, Sa, [], [0, 2], !1], + proprietary: [252, it, it, [], [0, 2], !1] +}, Sl = [], $i = vt(ua, pt({ // := WHERE keylen = len(keytype)+len(keydata) - key: xu(_t, ft({ type: _t, key: Z(null) })), + key: sl(Wt, pt({ type: Wt, key: rt(null) })), // := - value: Z(_t) + value: rt(Wt) })); -function no(e) { +function Wo(e) { const [t, n, r, o, s, i] = e; return { type: t, kc: n, vc: r, reqInc: o, allowInc: s, silentIgnore: i }; } -ft({ type: _t, key: Z(null) }); -function Go(e) { +pt({ type: Wt, key: rt(null) }); +function Ps(e) { const t = {}; for (const n in e) { const [r, o, s] = e[n]; t[r] = [n, o, s]; } - return bt({ + return At({ encodeStream: (n, r) => { let o = []; for (const s in e) { @@ -3386,32 +3385,32 @@ function Go(e) { continue; const [c, a, u] = e[s]; if (!a) - o.push({ key: { type: c, key: X }, value: u.encode(i) }); + o.push({ key: { type: c, key: ot }, value: u.encode(i) }); else { const f = i.map(([l, d]) => [ a.encode(l), u.encode(d) ]); - f.sort((l, d) => er(l[0], d[0])); + f.sort((l, d) => Ir(l[0], d[0])); for (const [l, d] of f) o.push({ key: { key: l, type: c }, value: d }); } } if (r.unknown) { - r.unknown.sort((s, i) => er(s[0].key, i[0].key)); + r.unknown.sort((s, i) => Ir(s[0].key, i[0].key)); for (const [s, i] of r.unknown) o.push({ key: s, value: i }); } - Ns.encodeStream(n, o); + $i.encodeStream(n, o); }, decodeStream: (n) => { - const r = Ns.decodeStream(n), o = {}, s = {}; + const r = $i.decodeStream(n), o = {}, s = {}; for (const i of r) { let c = "unknown", a = i.key.key, u = i.value; if (t[i.key.type]) { const [f, l, d] = t[i.key.type]; if (c = f, !l && a.length) - throw new Error(`PSBT: Non-empty key for ${c} (key=${S.encode(a)} value=${S.encode(u)}`); + throw new Error(`PSBT: Non-empty key for ${c} (key=${$.encode(a)} value=${$.encode(u)}`); if (a = l ? l.decode(a) : void 0, u = d.decode(u), !l) { if (o[c]) throw new Error(`PSBT: Same keys: ${c} (key=${a} value=${u})`); @@ -3428,17 +3427,17 @@ function Go(e) { } }); } -const zo = xt(Go(Ar), (e) => { +const Hs = Tt(Ps(oo), (e) => { if (e.finalScriptWitness && !e.finalScriptWitness.length) throw new Error("validateInput: empty finalScriptWitness"); if (e.partialSig && !e.partialSig.length) throw new Error("Empty partialSig"); if (e.partialSig) for (const [t] of e.partialSig) - Fe(t, St.ecdsa); + an(t, kt.ecdsa); if (e.bip32Derivation) for (const [t] of e.bip32Derivation) - Fe(t, St.ecdsa); + an(t, kt.ecdsa); if (e.requiredTimeLocktime !== void 0 && e.requiredTimeLocktime < 5e8) throw new Error(`validateInput: wrong timeLocktime=${e.requiredTimeLocktime}`); if (e.requiredHeightLocktime !== void 0 && (e.requiredHeightLocktime <= 0 || e.requiredHeightLocktime >= 5e8)) @@ -3451,12 +3450,12 @@ const zo = xt(Go(Ar), (e) => { throw new Error("validateInput: tapLeafScript version has parity bit!"); } return e; -}), jo = xt(Go(hn), (e) => { +}), Vs = Tt(Ps(_n), (e) => { if (e.bip32Derivation) for (const [t] of e.bip32Derivation) - Fe(t, St.ecdsa); + an(t, kt.ecdsa); return e; -}), dc = xt(Go(Wo), (e) => { +}), va = Tt(Ps(_s), (e) => { if ((e.version || 0) === 0) { if (!e.unsignedTx) throw new Error("PSBTv0: missing unsignedTx"); @@ -3465,43 +3464,43 @@ const zo = xt(Go(Ar), (e) => { throw new Error("PSBTv0: input scriptSig found in unsignedTx"); } return e; -}), _u = ft({ - magic: Do(Vo(new Uint8Array([255])), "psbt"), - global: dc, - inputs: mt("global/unsignedTx/inputs/length", zo), - outputs: mt(null, jo) -}), Vu = ft({ - magic: Do(Vo(new Uint8Array([255])), "psbt"), - global: dc, - inputs: mt("global/inputCount", zo), - outputs: mt("global/outputCount", jo) +}), vl = pt({ + magic: Us($s(new Uint8Array([255])), "psbt"), + global: va, + inputs: vt("global/unsignedTx/inputs/length", Hs), + outputs: vt(null, Vs) +}), Tl = pt({ + magic: Us($s(new Uint8Array([255])), "psbt"), + global: va, + inputs: vt("global/inputCount", Hs), + outputs: vt("global/outputCount", Vs) }); -ft({ - magic: Do(Vo(new Uint8Array([255])), "psbt"), - items: mt(null, de(mt(Qi, Tu([bu(_t), Z(vr)])), Tr.dict())) +pt({ + magic: Us($s(new Uint8Array([255])), "psbt"), + items: vt(null, Ie(vt(ua, ul([il(Wt), rt(no)])), eo.dict())) }); -function Lr(e, t, n) { +function mo(e, t, n) { for (const r in n) { if (r === "unknown" || !t[r]) continue; - const { allowInc: o } = no(t[r]); + const { allowInc: o } = Wo(t[r]); if (!o.includes(e)) throw new Error(`PSBTv${e}: field ${r} is not allowed`); } for (const r in t) { - const { reqInc: o } = no(t[r]); + const { reqInc: o } = Wo(t[r]); if (o.includes(e) && n[r] === void 0) throw new Error(`PSBTv${e}: missing required field ${r}`); } } -function Rs(e, t, n) { +function Ui(e, t, n) { const r = {}; for (const o in n) { const s = o; if (s !== "unknown") { if (!t[s]) continue; - const { allowInc: i, silentIgnore: c } = no(t[s]); + const { allowInc: i, silentIgnore: c } = Wo(t[s]); if (!i.includes(e)) { if (c) continue; @@ -3512,13 +3511,13 @@ function Rs(e, t, n) { } return r; } -function hc(e) { +function Ta(e) { const t = e && e.global && e.global.version || 0; - Lr(t, Wo, e.global); + mo(t, _s, e.global); for (const i of e.inputs) - Lr(t, Ar, i); + mo(t, oo, i); for (const i of e.outputs) - Lr(t, hn, i); + mo(t, _n, i); const n = t ? e.global.inputCount : e.global.unsignedTx.inputs.length; if (e.inputs.length < n) throw new Error("Not enough inputs"); @@ -3533,7 +3532,7 @@ function hc(e) { throw new Error(`Unexpected outputs left in tx=${s}`); return e; } -function ro(e, t, n, r, o) { +function zo(e, t, n, r, o) { const s = { ...n, ...t }; for (const i in e) { const c = i, [a, u, f] = e[c], l = r && !r.includes(i); @@ -3547,41 +3546,41 @@ function ro(e, t, n, r, o) { if (h) { if (!Array.isArray(h)) throw new Error(`keyMap(${i}): KV pairs should be [k, v][]`); - h = h.map((m) => { - if (m.length !== 2) + h = h.map((y) => { + if (y.length !== 2) throw new Error(`keyMap(${i}): KV pairs should be [k, v][]`); return [ - typeof m[0] == "string" ? u.decode(S.decode(m[0])) : m[0], - typeof m[1] == "string" ? f.decode(S.decode(m[1])) : m[1] + typeof y[0] == "string" ? u.decode($.decode(y[0])) : y[0], + typeof y[1] == "string" ? f.decode($.decode(y[1])) : y[1] ]; }); - const w = {}, p = (m, x, b) => { - if (w[m] === void 0) { - w[m] = [x, b]; + const w = {}, g = (y, S, v) => { + if (w[y] === void 0) { + w[y] = [S, v]; return; } - const v = S.encode(f.encode(w[m][1])), O = S.encode(f.encode(b)); - if (v !== O) - throw new Error(`keyMap(${c}): same key=${m} oldVal=${v} newVal=${O}`); + const O = $.encode(f.encode(w[y][1])), N = $.encode(f.encode(v)); + if (O !== N) + throw new Error(`keyMap(${c}): same key=${y} oldVal=${O} newVal=${N}`); }; - for (const [m, x] of d) { - const b = S.encode(u.encode(m)); - p(b, m, x); + for (const [y, S] of d) { + const v = $.encode(u.encode(y)); + g(v, y, S); } - for (const [m, x] of h) { - const b = S.encode(u.encode(m)); - if (x === void 0) { + for (const [y, S] of h) { + const v = $.encode(u.encode(y)); + if (S === void 0) { if (l) - throw new Error(`Cannot remove signed field=${c}/${m}`); - delete w[b]; + throw new Error(`Cannot remove signed field=${c}/${y}`); + delete w[v]; } else - p(b, m, x); + g(v, y, S); } s[c] = Object.values(w); } } else if (typeof s[i] == "string") - s[i] = f.decode(S.decode(s[i])); - else if (l && i in t && n && n[i] !== void 0 && !rt(f.encode(t[i]), f.encode(n[i]))) + s[i] = f.decode($.decode(s[i])); + else if (l && i in t && n && n[i] !== void 0 && !ct(f.encode(t[i]), f.encode(n[i]))) throw new Error(`Cannot change signed field=${i}`); } for (const i in s) @@ -3592,54 +3591,54 @@ function ro(e, t, n, r, o) { } return s; } -const Cs = xt(_u, hc), $s = xt(Vu, hc), Du = { +const Ri = Tt(vl, Ta), Ni = Tt(Tl, Ta), Al = { encode(e) { - if (!(e.length !== 2 || e[0] !== 1 || !q(e[1]) || S.encode(e[1]) !== "4e73")) - return { type: "p2a", script: L.encode(e) }; + if (!(e.length !== 2 || e[0] !== 1 || !nt(e[1]) || $.encode(e[1]) !== "4e73")) + return { type: "p2a", script: K.encode(e) }; }, decode: (e) => { if (e.type === "p2a") - return [1, S.decode("4e73")]; + return [1, $.decode("4e73")]; } }; -function Ce(e, t) { +function Ze(e, t) { try { - return Fe(e, t), !0; + return an(e, t), !0; } catch { return !1; } } -const Hu = { +const Il = { encode(e) { - if (!(e.length !== 2 || !q(e[0]) || !Ce(e[0], St.ecdsa) || e[1] !== "CHECKSIG")) + if (!(e.length !== 2 || !nt(e[0]) || !Ze(e[0], kt.ecdsa) || e[1] !== "CHECKSIG")) return { type: "pk", pubkey: e[0] }; }, decode: (e) => e.type === "pk" ? [e.pubkey, "CHECKSIG"] : void 0 -}, Mu = { +}, kl = { encode(e) { - if (!(e.length !== 5 || e[0] !== "DUP" || e[1] !== "HASH160" || !q(e[2])) && !(e[3] !== "EQUALVERIFY" || e[4] !== "CHECKSIG")) + if (!(e.length !== 5 || e[0] !== "DUP" || e[1] !== "HASH160" || !nt(e[2])) && !(e[3] !== "EQUALVERIFY" || e[4] !== "CHECKSIG")) return { type: "pkh", hash: e[2] }; }, decode: (e) => e.type === "pkh" ? ["DUP", "HASH160", e.hash, "EQUALVERIFY", "CHECKSIG"] : void 0 -}, Ku = { +}, Bl = { encode(e) { - if (!(e.length !== 3 || e[0] !== "HASH160" || !q(e[1]) || e[2] !== "EQUAL")) + if (!(e.length !== 3 || e[0] !== "HASH160" || !nt(e[1]) || e[2] !== "EQUAL")) return { type: "sh", hash: e[1] }; }, decode: (e) => e.type === "sh" ? ["HASH160", e.hash, "EQUAL"] : void 0 -}, Fu = { +}, Ol = { encode(e) { - if (!(e.length !== 2 || e[0] !== 0 || !q(e[1])) && e[1].length === 32) + if (!(e.length !== 2 || e[0] !== 0 || !nt(e[1])) && e[1].length === 32) return { type: "wsh", hash: e[1] }; }, decode: (e) => e.type === "wsh" ? [0, e.hash] : void 0 -}, Wu = { +}, $l = { encode(e) { - if (!(e.length !== 2 || e[0] !== 0 || !q(e[1])) && e[1].length === 20) + if (!(e.length !== 2 || e[0] !== 0 || !nt(e[1])) && e[1].length === 20) return { type: "wpkh", hash: e[1] }; }, decode: (e) => e.type === "wpkh" ? [0, e.hash] : void 0 -}, Gu = { +}, Ul = { encode(e) { const t = e.length - 1; if (e[t] !== "CHECKMULTISIG") @@ -3650,20 +3649,20 @@ const Hu = { const o = e.slice(1, -2); if (r === o.length) { for (const s of o) - if (!q(s)) + if (!nt(s)) return; return { type: "ms", m: n, pubkeys: o }; } }, // checkmultisig(n, ..pubkeys, m) decode: (e) => e.type === "ms" ? [e.m, ...e.pubkeys, e.pubkeys.length, "CHECKMULTISIG"] : void 0 -}, zu = { +}, Rl = { encode(e) { - if (!(e.length !== 2 || e[0] !== 1 || !q(e[1]))) + if (!(e.length !== 2 || e[0] !== 1 || !nt(e[1]))) return { type: "tr", pubkey: e[1] }; }, decode: (e) => e.type === "tr" ? [1, e.pubkey] : void 0 -}, ju = { +}, Nl = { encode(e) { const t = e.length - 1; if (e[t] !== "CHECKSIG") @@ -3676,7 +3675,7 @@ const Hu = { return; continue; } - if (!q(o)) + if (!nt(o)) return; n.push(o); } @@ -3690,12 +3689,12 @@ const Hu = { t.push(e.pubkeys[n], "CHECKSIGVERIFY"); return t.push(e.pubkeys[e.pubkeys.length - 1], "CHECKSIG"), t; } -}, qu = { +}, Ll = { encode(e) { const t = e.length - 1; if (e[t] !== "NUMEQUAL" || e[1] !== "CHECKSIG") return; - const n = [], r = Iu(e[t - 1]); + const n = [], r = hl(e[t - 1]); if (typeof r == "number") { for (let o = 0; o < t - 1; o++) { const s = e[o]; @@ -3704,7 +3703,7 @@ const Hu = { throw new Error("OutScript.encode/tr_ms: wrong element"); continue; } - if (!q(s)) + if (!nt(s)) throw new Error("OutScript.encode/tr_ms: wrong key element"); n.push(s); } @@ -3719,45 +3718,45 @@ const Hu = { t.push(e.pubkeys[n], "CHECKSIGADD"); return t.push(e.m, "NUMEQUAL"), t; } -}, Yu = { +}, Cl = { encode(e) { - return { type: "unknown", script: L.encode(e) }; + return { type: "unknown", script: K.encode(e) }; }, - decode: (e) => e.type === "unknown" ? L.decode(e.script) : void 0 -}, Zu = [ - Du, - Hu, - Mu, - Ku, - Fu, - Wu, - Gu, - zu, - ju, - qu, - Yu -], Xu = de(L, Tr.match(Zu)), st = xt(Xu, (e) => { - if (e.type === "pk" && !Ce(e.pubkey, St.ecdsa)) + decode: (e) => e.type === "unknown" ? K.decode(e.script) : void 0 +}, _l = [ + Al, + Il, + kl, + Bl, + Ol, + $l, + Ul, + Rl, + Nl, + Ll, + Cl +], Pl = Ie(K, eo.match(_l)), ut = Tt(Pl, (e) => { + if (e.type === "pk" && !Ze(e.pubkey, kt.ecdsa)) throw new Error("OutScript/pk: wrong key"); - if ((e.type === "pkh" || e.type === "sh" || e.type === "wpkh") && (!q(e.hash) || e.hash.length !== 20)) + if ((e.type === "pkh" || e.type === "sh" || e.type === "wpkh") && (!nt(e.hash) || e.hash.length !== 20)) throw new Error(`OutScript/${e.type}: wrong hash`); - if (e.type === "wsh" && (!q(e.hash) || e.hash.length !== 32)) + if (e.type === "wsh" && (!nt(e.hash) || e.hash.length !== 32)) throw new Error("OutScript/wsh: wrong hash"); - if (e.type === "tr" && (!q(e.pubkey) || !Ce(e.pubkey, St.schnorr))) + if (e.type === "tr" && (!nt(e.pubkey) || !Ze(e.pubkey, kt.schnorr))) throw new Error("OutScript/tr: wrong taproot public key"); if ((e.type === "ms" || e.type === "tr_ns" || e.type === "tr_ms") && !Array.isArray(e.pubkeys)) throw new Error("OutScript/multisig: wrong pubkeys array"); if (e.type === "ms") { const t = e.pubkeys.length; for (const n of e.pubkeys) - if (!Ce(n, St.ecdsa)) + if (!Ze(n, kt.ecdsa)) throw new Error("OutScript/multisig: wrong pubkey"); if (e.m <= 0 || t > 16 || e.m > t) throw new Error("OutScript/multisig: invalid params"); } if (e.type === "tr_ns" || e.type === "tr_ms") { for (const t of e.pubkeys) - if (!Ce(t, St.schnorr)) + if (!Ze(t, kt.schnorr)) throw new Error(`OutScript/${e.type}: wrong pubkey`); } if (e.type === "tr_ms") { @@ -3767,47 +3766,47 @@ const Hu = { } return e; }); -function Ls(e, t) { - if (!rt(e.hash, ht(t))) +function Li(e, t) { + if (!ct(e.hash, wt(t))) throw new Error("checkScript: wsh wrong witnessScript hash"); - const n = st.decode(t); + const n = ut.decode(t); if (n.type === "tr" || n.type === "tr_ns" || n.type === "tr_ms") throw new Error(`checkScript: P2${n.type} cannot be wrapped in P2SH`); if (n.type === "wpkh" || n.type === "sh") throw new Error(`checkScript: P2${n.type} cannot be wrapped in P2WSH`); } -function pc(e, t, n) { +function Aa(e, t, n) { if (e) { - const r = st.decode(e); + const r = ut.decode(e); if (r.type === "tr_ns" || r.type === "tr_ms" || r.type === "ms" || r.type == "pk") throw new Error(`checkScript: non-wrapped ${r.type}`); if (r.type === "sh" && t) { - if (!rt(r.hash, ic(t))) + if (!ct(r.hash, ya(t))) throw new Error("checkScript: sh wrong redeemScript hash"); - const o = st.decode(t); + const o = ut.decode(t); if (o.type === "tr" || o.type === "tr_ns" || o.type === "tr_ms") throw new Error(`checkScript: P2${o.type} cannot be wrapped in P2SH`); if (o.type === "sh") throw new Error("checkScript: P2SH cannot be wrapped in P2SH"); } - r.type === "wsh" && n && Ls(r, n); + r.type === "wsh" && n && Li(r, n); } if (t) { - const r = st.decode(t); - r.type === "wsh" && n && Ls(r, n); + const r = ut.decode(t); + r.type === "wsh" && n && Li(r, n); } } -function Qu(e) { +function Hl(e) { const t = {}; for (const n of e) { - const r = S.encode(n); + const r = $.encode(n); if (t[r]) - throw new Error(`Multisig: non-uniq pubkey: ${e.map(S.encode)}`); + throw new Error(`Multisig: non-uniq pubkey: ${e.map($.encode)}`); t[r] = !0; } } -function Ju(e, t, n = !1, r) { - const o = st.decode(e); +function Vl(e, t, n = !1, r) { + const o = ut.decode(e); if (o.type === "unknown" && n) return; if (!["tr_ns", "tr_ms"].includes(o.type)) @@ -3815,13 +3814,13 @@ function Ju(e, t, n = !1, r) { const s = o; if (!n && s.pubkeys) for (const i of s.pubkeys) { - if (rt(i, Ko)) + if (ct(i, Ls)) throw new Error("Unspendable taproot key in leaf script"); - if (rt(i, t)) + if (ct(i, t)) throw new Error("Using P2TR with leaf script with same key as internal key is not supported"); } } -function gc(e) { +function Ia(e) { const t = Array.from(e); for (; t.length >= 2; ) { t.sort((i, c) => (c.weight || 1) - (i.weight || 1)); @@ -3836,7 +3835,7 @@ function gc(e) { const n = t[0]; return n?.childs || n; } -function oo(e, t = []) { +function Go(e, t = []) { if (!e) throw new Error("taprootAddPath: empty tree"); if (e.type === "leaf") @@ -3847,80 +3846,80 @@ function oo(e, t = []) { ...e, path: t, // Left element has right hash in path and otherwise - left: oo(e.left, [e.right.hash, ...t]), - right: oo(e.right, [e.left.hash, ...t]) + left: Go(e.left, [e.right.hash, ...t]), + right: Go(e.right, [e.left.hash, ...t]) }; } -function so(e) { +function qo(e) { if (!e) throw new Error("taprootAddPath: empty tree"); if (e.type === "leaf") return [e]; if (e.type !== "branch") throw new Error(`taprootWalkTree: wrong type=${e}`); - return [...so(e.left), ...so(e.right)]; + return [...qo(e.left), ...qo(e.right)]; } -function io(e, t, n = !1, r) { +function jo(e, t, n = !1, r) { if (!e) throw new Error("taprootHashTree: empty tree"); if (Array.isArray(e) && e.length === 1 && (e = e[0]), !Array.isArray(e)) { const { leafVersion: a, script: u } = e; - if (e.tapLeafScript || e.tapMerkleRoot && !rt(e.tapMerkleRoot, X)) + if (e.tapLeafScript || e.tapMerkleRoot && !ct(e.tapMerkleRoot, ot)) throw new Error("P2TR: tapRoot leafScript cannot have tree"); - const f = typeof u == "string" ? S.decode(u) : u; - if (!q(f)) + const f = typeof u == "string" ? $.decode(u) : u; + if (!nt(f)) throw new Error(`checkScript: wrong script type=${f}`); - return Ju(f, t, n), { + return Vl(f, t, n), { type: "leaf", version: a, script: f, - hash: cn(f, a) + hash: On(f, a) }; } - if (e.length !== 2 && (e = gc(e)), e.length !== 2) + if (e.length !== 2 && (e = Ia(e)), e.length !== 2) throw new Error("hashTree: non binary tree!"); - const o = io(e[0], t, n), s = io(e[1], t, n); + const o = jo(e[0], t, n), s = jo(e[1], t, n); let [i, c] = [o.hash, s.hash]; - return er(c, i) === -1 && ([i, c] = [c, i]), { type: "branch", left: o, right: s, hash: Mo("TapBranch", i, c) }; + return Ir(c, i) === -1 && ([i, c] = [c, i]), { type: "branch", left: o, right: s, hash: Ns("TapBranch", i, c) }; } -const pn = 192, cn = (e, t = pn) => Mo("TapLeaf", new Uint8Array([t]), $t.encode(e)); -function tf(e, t, n = We, r = !1, o) { +const Pn = 192, On = (e, t = Pn) => Ns("TapLeaf", new Uint8Array([t]), Mt.encode(e)); +function Dl(e, t, n = un, r = !1, o) { if (!e && !t) throw new Error("p2tr: should have pubKey or scriptTree (or both)"); - const s = typeof e == "string" ? S.decode(e) : e || Ko; - if (!Ce(s, St.schnorr)) + const s = typeof e == "string" ? $.decode(e) : e || Ls; + if (!Ze(s, kt.schnorr)) throw new Error("p2tr: non-schnorr pubkey"); if (t) { - let i = oo(io(t, s, r)); - const c = i.hash, [a, u] = to(s, c), f = so(i).map((l) => ({ + let i = Go(jo(t, s, r)); + const c = i.hash, [a, u] = Ko(s, c), f = qo(i).map((l) => ({ ...l, - controlBlock: Wt.encode({ - version: (l.version || pn) + u, + controlBlock: Zt.encode({ + version: (l.version || Pn) + u, internalKey: s, merklePath: l.path }) })); return { type: "tr", - script: st.encode({ type: "tr", pubkey: a }), - address: ke(n).encode({ type: "tr", pubkey: a }), + script: ut.encode({ type: "tr", pubkey: a }), + address: Me(n).encode({ type: "tr", pubkey: a }), // For tests tweakedPubkey: a, // PSBT stuff tapInternalKey: s, leaves: f, tapLeafScript: f.map((l) => [ - Wt.decode(l.controlBlock), - ce(l.script, new Uint8Array([l.version || pn])) + Zt.decode(l.controlBlock), + Ee(l.script, new Uint8Array([l.version || Pn])) ]), tapMerkleRoot: c }; } else { - const i = to(s, X)[0]; + const i = Ko(s, ot)[0]; return { type: "tr", - script: st.encode({ type: "tr", pubkey: i }), - address: ke(n).encode({ type: "tr", pubkey: i }), + script: ut.encode({ type: "tr", pubkey: i }), + address: Me(n).encode({ type: "tr", pubkey: i }), // For tests tweakedPubkey: i, // PSBT stuff @@ -3928,14 +3927,14 @@ function tf(e, t, n = We, r = !1, o) { }; } } -function ef(e, t, n = !1) { - return n || Qu(t), { +function Ml(e, t, n = !1) { + return n || Hl(t), { type: "tr_ms", - script: st.encode({ type: "tr_ms", pubkeys: t, m: e }) + script: ut.encode({ type: "tr_ms", pubkeys: t, m: e }) }; } -const wc = ou(ht); -function yc(e, t) { +const ka = Wf(wt); +function Ba(e, t) { if (t.length < 2 || t.length > 40) throw new Error("Witness: invalid length"); if (e > 16) @@ -3943,28 +3942,28 @@ function yc(e, t) { if (e === 0 && !(t.length === 20 || t.length === 32)) throw new Error("Witness: invalid length for version"); } -function Pr(e, t, n = We) { - yc(e, t); - const r = e === 0 ? Xr : Ne; +function bo(e, t, n = un) { + Ba(e, t); + const r = e === 0 ? Vo : je; return r.encode(n.bech32, [e].concat(r.toWords(t))); } -function Ps(e, t) { - return wc.encode(ce(Uint8Array.from(t), e)); +function Ci(e, t) { + return ka.encode(Ee(Uint8Array.from(t), e)); } -function ke(e = We) { +function Me(e = un) { return { encode(t) { const { type: n } = t; if (n === "wpkh") - return Pr(0, t.hash, e); + return bo(0, t.hash, e); if (n === "wsh") - return Pr(0, t.hash, e); + return bo(0, t.hash, e); if (n === "tr") - return Pr(1, t.pubkey, e); + return bo(1, t.pubkey, e); if (n === "pkh") - return Ps(t.hash, [e.pubKeyHash]); + return Ci(t.hash, [e.pubKeyHash]); if (n === "sh") - return Ps(t.hash, [e.scriptHash]); + return Ci(t.hash, [e.scriptHash]); throw new Error(`Unknown address type=${n}`); }, decode(t) { @@ -3973,16 +3972,16 @@ function ke(e = We) { if (e.bech32 && t.toLowerCase().startsWith(`${e.bech32}1`)) { let r; try { - if (r = Xr.decode(t), r.words[0] !== 0) + if (r = Vo.decode(t), r.words[0] !== 0) throw new Error(`bech32: wrong version=${r.words[0]}`); } catch { - if (r = Ne.decode(t), r.words[0] === 0) + if (r = je.decode(t), r.words[0] === 0) throw new Error(`bech32m: wrong version=${r.words[0]}`); } if (r.prefix !== e.bech32) throw new Error(`wrong bech32 prefix=${r.prefix}`); - const [o, ...s] = r.words, i = Xr.fromWords(s); - if (yc(o, i), o === 0 && i.length === 32) + const [o, ...s] = r.words, i = Vo.fromWords(s); + if (Ba(o, i), o === 0 && i.length === 32) return { type: "wsh", hash: i }; if (o === 0 && i.length === 20) return { type: "wpkh", hash: i }; @@ -3990,7 +3989,7 @@ function ke(e = We) { return { type: "tr", pubkey: i }; throw new Error("Unknown witness program"); } - const n = wc.decode(t); + const n = ka.decode(t); if (n.length !== 21) throw new Error("Invalid base58 address"); if (n[0] === e.pubKeyHash) @@ -4004,92 +4003,92 @@ function ke(e = We) { } }; } -const Cn = new Uint8Array(32), nf = { +const er = new Uint8Array(32), Kl = { amount: 0xffffffffffffffffn, - script: X -}, rf = (e) => Math.ceil(e / 4), of = 8, sf = 2, me = 0, qo = 4294967295; -Tr.decimal(of); -const an = (e, t) => e === void 0 ? t : e; -function or(e) { + script: ot +}, Fl = (e) => Math.ceil(e / 4), Wl = 8, zl = 2, Re = 0, Ds = 4294967295; +eo.decimal(Wl); +const $n = (e, t) => e === void 0 ? t : e; +function Or(e) { if (Array.isArray(e)) - return e.map((t) => or(t)); - if (q(e)) + return e.map((t) => Or(t)); + if (nt(e)) return Uint8Array.from(e); if (["number", "bigint", "boolean", "string", "undefined"].includes(typeof e)) return e; if (e === null) return e; if (typeof e == "object") - return Object.fromEntries(Object.entries(e).map(([t, n]) => [t, or(n)])); + return Object.fromEntries(Object.entries(e).map(([t, n]) => [t, Or(n)])); throw new Error(`cloneDeep: unknown type=${e} (${typeof e})`); } -const D = { +const j = { DEFAULT: 0, ALL: 1, NONE: 2, SINGLE: 3, ANYONECANPAY: 128 -}, Ae = { - DEFAULT: D.DEFAULT, - ALL: D.ALL, - NONE: D.NONE, - SINGLE: D.SINGLE, - DEFAULT_ANYONECANPAY: D.DEFAULT | D.ANYONECANPAY, - ALL_ANYONECANPAY: D.ALL | D.ANYONECANPAY, - NONE_ANYONECANPAY: D.NONE | D.ANYONECANPAY, - SINGLE_ANYONECANPAY: D.SINGLE | D.ANYONECANPAY -}, cf = uc(Ae); -function af(e, t, n, r = X) { - return rt(n, t) && (e = ku(e, r), t = Ho(e)), { privKey: e, pubKey: t }; -} -function xe(e) { +}, Ke = { + DEFAULT: j.DEFAULT, + ALL: j.ALL, + NONE: j.NONE, + SINGLE: j.SINGLE, + DEFAULT_ANYONECANPAY: j.DEFAULT | j.ANYONECANPAY, + ALL_ANYONECANPAY: j.ALL | j.ANYONECANPAY, + NONE_ANYONECANPAY: j.NONE | j.ANYONECANPAY, + SINGLE_ANYONECANPAY: j.SINGLE | j.ANYONECANPAY +}, Gl = Ea(Ke); +function ql(e, t, n, r = ot) { + return ct(n, t) && (e = ll(e, r), t = Rs(e)), { privKey: e, pubKey: t }; +} +function Ne(e) { if (e.script === void 0 || e.amount === void 0) throw new Error("Transaction/output: script and amount required"); return { script: e.script, amount: e.amount }; } -function en(e) { +function Tn(e) { if (e.txid === void 0 || e.index === void 0) throw new Error("Transaction/input: txid and index required"); return { txid: e.txid, index: e.index, - sequence: an(e.sequence, qo), - finalScriptSig: an(e.finalScriptSig, X) + sequence: $n(e.sequence, Ds), + finalScriptSig: $n(e.finalScriptSig, ot) }; } -function _r(e) { +function Eo(e) { for (const t in e) { const n = t; - $u.includes(n) || delete e[n]; + El.includes(n) || delete e[n]; } } -const Vr = ft({ txid: Z(32, !0), index: W }); -function uf(e) { - if (typeof e != "number" || typeof cf[e] != "string") +const xo = pt({ txid: rt(32, !0), index: Z }); +function jl(e) { + if (typeof e != "number" || typeof Gl[e] != "string") throw new Error(`Invalid SigHash=${e}`); return e; } -function _s(e) { +function _i(e) { const t = e & 31; return { - isAny: !!(e & D.ANYONECANPAY), - isNone: t === D.NONE, - isSingle: t === D.SINGLE + isAny: !!(e & j.ANYONECANPAY), + isNone: t === j.NONE, + isSingle: t === j.SINGLE }; } -function ff(e) { +function Yl(e) { if (e !== void 0 && {}.toString.call(e) !== "[object Object]") throw new Error(`Wrong object type for transaction options: ${e}`); const t = { ...e, // Defaults - version: an(e.version, sf), - lockTime: an(e.lockTime, 0), - PSBTVersion: an(e.PSBTVersion, 0) + version: $n(e.version, zl), + lockTime: $n(e.lockTime, 0), + PSBTVersion: $n(e.PSBTVersion, 0) }; if (typeof t.allowUnknowInput < "u" && (e.allowUnknownInputs = t.allowUnknowInput), typeof t.allowUnknowOutput < "u" && (e.allowUnknownOutputs = t.allowUnknowOutput), typeof t.lockTime != "number") throw new Error("Transaction lock time should be number"); - if (W.encode(t.lockTime), t.PSBTVersion !== 0 && t.PSBTVersion !== 2) + if (Z.encode(t.lockTime), t.PSBTVersion !== 0 && t.PSBTVersion !== 2) throw new Error(`Unknown PSBT version ${t.PSBTVersion}`); for (const n of [ "allowUnknownVersion", @@ -4119,29 +4118,29 @@ function ff(e) { } return Object.freeze(t); } -function Vs(e) { +function Pi(e) { if (e.nonWitnessUtxo && e.index !== void 0) { const t = e.nonWitnessUtxo.outputs.length - 1; if (e.index > t) throw new Error(`validateInput: index(${e.index}) not in nonWitnessUtxo`); const n = e.nonWitnessUtxo.outputs[e.index]; - if (e.witnessUtxo && (!rt(e.witnessUtxo.script, n.script) || e.witnessUtxo.amount !== n.amount)) + if (e.witnessUtxo && (!ct(e.witnessUtxo.script, n.script) || e.witnessUtxo.amount !== n.amount)) throw new Error("validateInput: witnessUtxo different from nonWitnessUtxo"); if (e.txid) { if (e.nonWitnessUtxo.outputs.length - 1 < e.index) throw new Error("nonWitnessUtxo: incorect output index"); - const o = ee.fromRaw(Le.encode(e.nonWitnessUtxo), { + const o = ue.fromRaw(Qe.encode(e.nonWitnessUtxo), { allowUnknownOutputs: !0, disableScriptCheck: !0, allowUnknownInputs: !0 - }), s = S.encode(e.txid); + }), s = $.encode(e.txid); if (o.isFinal && o.id !== s) throw new Error(`nonWitnessUtxo: wrong txid, exp=${s} got=${o.id}`); } } return e; } -function Fn(e) { +function hr(e) { if (e.nonWitnessUtxo) { if (e.index === void 0) throw new Error("Unknown input index"); @@ -4152,21 +4151,21 @@ function Fn(e) { throw new Error("Cannot find previous output info"); } } -function Ds(e, t, n, r = !1, o = !1) { +function Hi(e, t, n, r = !1, o = !1) { let { nonWitnessUtxo: s, txid: i } = e; - typeof s == "string" && (s = S.decode(s)), q(s) && (s = Le.decode(s)), !("nonWitnessUtxo" in e) && s === void 0 && (s = t?.nonWitnessUtxo), typeof i == "string" && (i = S.decode(i)), i === void 0 && (i = t?.txid); + typeof s == "string" && (s = $.decode(s)), nt(s) && (s = Qe.decode(s)), !("nonWitnessUtxo" in e) && s === void 0 && (s = t?.nonWitnessUtxo), typeof i == "string" && (i = $.decode(i)), i === void 0 && (i = t?.txid); let c = { ...t, ...e, nonWitnessUtxo: s, txid: i }; - !("nonWitnessUtxo" in e) && c.nonWitnessUtxo === void 0 && delete c.nonWitnessUtxo, c.sequence === void 0 && (c.sequence = qo), c.tapMerkleRoot === null && delete c.tapMerkleRoot, c = ro(Ar, c, t, n, o), zo.encode(c); + !("nonWitnessUtxo" in e) && c.nonWitnessUtxo === void 0 && delete c.nonWitnessUtxo, c.sequence === void 0 && (c.sequence = Ds), c.tapMerkleRoot === null && delete c.tapMerkleRoot, c = zo(oo, c, t, n, o), Hs.encode(c); let a; - return c.nonWitnessUtxo && c.index !== void 0 ? a = c.nonWitnessUtxo.outputs[c.index] : c.witnessUtxo && (a = c.witnessUtxo), a && !r && pc(a && a.script, c.redeemScript, c.witnessScript), c; + return c.nonWitnessUtxo && c.index !== void 0 ? a = c.nonWitnessUtxo.outputs[c.index] : c.witnessUtxo && (a = c.witnessUtxo), a && !r && Aa(a && a.script, c.redeemScript, c.witnessScript), c; } -function Hs(e, t = !1) { - let n = "legacy", r = D.ALL; - const o = Fn(e), s = st.decode(o.script); +function Vi(e, t = !1) { + let n = "legacy", r = j.ALL; + const o = hr(e), s = ut.decode(o.script); let i = s.type, c = s; const a = [s]; if (s.type === "tr") - return r = D.DEFAULT, { + return r = j.DEFAULT, { txType: "taproot", type: "tr", last: s, @@ -4178,19 +4177,19 @@ function Hs(e, t = !1) { if ((s.type === "wpkh" || s.type === "wsh") && (n = "segwit"), s.type === "sh") { if (!e.redeemScript) throw new Error("inputType: sh without redeemScript"); - let d = st.decode(e.redeemScript); + let d = ut.decode(e.redeemScript); (d.type === "wpkh" || d.type === "wsh") && (n = "segwit"), a.push(d), c = d, i += `-${d.type}`; } if (c.type === "wsh") { if (!e.witnessScript) throw new Error("inputType: wsh without witnessScript"); - let d = st.decode(e.witnessScript); + let d = ut.decode(e.witnessScript); d.type === "wsh" && (n = "segwit"), a.push(d), c = d, i += `-${d.type}`; } const u = a[a.length - 1]; if (u.type === "sh" || u.type === "wsh") throw new Error("inputType: sh/wsh cannot be terminal type"); - const f = st.encode(u), l = { + const f = ut.encode(u), l = { type: i, txType: n, last: u, @@ -4203,7 +4202,7 @@ function Hs(e, t = !1) { return l; } } -let ee = class Wn { +let ue = class pr { global = {}; inputs = []; // use getInput() @@ -4211,12 +4210,12 @@ let ee = class Wn { // use getOutput() opts; constructor(t = {}) { - const n = this.opts = ff(t); - n.lockTime !== me && (this.global.fallbackLocktime = n.lockTime), this.global.txVersion = n.version; + const n = this.opts = Yl(t); + n.lockTime !== Re && (this.global.fallbackLocktime = n.lockTime), this.global.txVersion = n.version; } // Import static fromRaw(t, n = {}) { - const r = Le.decode(t), o = new Wn({ ...n, version: r.version, lockTime: r.lockTime }); + const r = Qe.decode(t), o = new pr({ ...n, version: r.version, lockTime: r.lockTime }); for (const s of r.outputs) o.addOutput(s); if (o.outputs = r.outputs, o.inputs = r.inputs, r.witnesses) @@ -4228,10 +4227,10 @@ let ee = class Wn { static fromPSBT(t, n = {}) { let r; try { - r = Cs.decode(t); + r = Ri.decode(t); } catch (l) { try { - r = $s.decode(t); + r = Ni.decode(t); } catch { throw l; } @@ -4239,9 +4238,9 @@ let ee = class Wn { const o = r.global.version || 0; if (o !== 0 && o !== 2) throw new Error(`Wrong PSBT version=${o}`); - const s = r.global.unsignedTx, i = o === 0 ? s?.version : r.global.txVersion, c = o === 0 ? s?.lockTime : r.global.fallbackLocktime, a = new Wn({ ...n, version: i, lockTime: c, PSBTVersion: o }), u = o === 0 ? s?.inputs.length : r.global.inputCount; - a.inputs = r.inputs.slice(0, u).map((l, d) => Vs({ - finalScriptSig: X, + const s = r.global.unsignedTx, i = o === 0 ? s?.version : r.global.txVersion, c = o === 0 ? s?.lockTime : r.global.fallbackLocktime, a = new pr({ ...n, version: i, lockTime: c, PSBTVersion: o }), u = o === 0 ? s?.inputs.length : r.global.inputCount; + a.inputs = r.inputs.slice(0, u).map((l, d) => Pi({ + finalScriptSig: ot, ...r.global.unsignedTx?.inputs[d], ...l })); @@ -4249,24 +4248,24 @@ let ee = class Wn { return a.outputs = r.outputs.slice(0, f).map((l, d) => ({ ...l, ...r.global.unsignedTx?.outputs[d] - })), a.global = { ...r.global, txVersion: i }, c !== me && (a.global.fallbackLocktime = c), a; + })), a.global = { ...r.global, txVersion: i }, c !== Re && (a.global.fallbackLocktime = c), a; } toPSBT(t = this.opts.PSBTVersion) { if (t !== 0 && t !== 2) throw new Error(`Wrong PSBT version=${t}`); - const n = this.inputs.map((s) => Vs(Rs(t, Ar, s))); + const n = this.inputs.map((s) => Pi(Ui(t, oo, s))); for (const s of n) s.partialSig && !s.partialSig.length && delete s.partialSig, s.finalScriptSig && !s.finalScriptSig.length && delete s.finalScriptSig, s.finalScriptWitness && !s.finalScriptWitness.length && delete s.finalScriptWitness; - const r = this.outputs.map((s) => Rs(t, hn, s)), o = { ...this.global }; - return t === 0 ? (o.unsignedTx = on.decode(on.encode({ + const r = this.outputs.map((s) => Ui(t, _n, s)), o = { ...this.global }; + return t === 0 ? (o.unsignedTx = kn.decode(kn.encode({ version: this.version, lockTime: this.lockTime, - inputs: this.inputs.map(en).map((s) => ({ + inputs: this.inputs.map(Tn).map((s) => ({ ...s, - finalScriptSig: X + finalScriptSig: ot })), - outputs: this.outputs.map(xe) - })), delete o.fallbackLocktime, delete o.txVersion) : (o.version = t, o.txVersion = this.version, o.inputCount = this.inputs.length, o.outputCount = this.outputs.length, o.fallbackLocktime && o.fallbackLocktime === me && delete o.fallbackLocktime), this.opts.bip174jsCompat && (n.length || n.push({}), r.length || r.push({})), (t === 0 ? Cs : $s).encode({ + outputs: this.outputs.map(Ne) + })), delete o.fallbackLocktime, delete o.txVersion) : (o.version = t, o.txVersion = this.version, o.inputCount = this.inputs.length, o.outputCount = this.outputs.length, o.fallbackLocktime && o.fallbackLocktime === Re && delete o.fallbackLocktime), this.opts.bip174jsCompat && (n.length || n.push({}), r.length || r.push({})), (t === 0 ? Ri : Ni).encode({ global: o, inputs: n, outputs: r @@ -4274,10 +4273,10 @@ let ee = class Wn { } // BIP370 lockTime (https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#determining-lock-time) get lockTime() { - let t = me, n = 0, r = me, o = 0; + let t = Re, n = 0, r = Re, o = 0; for (const s of this.inputs) s.requiredHeightLocktime && (t = Math.max(t, s.requiredHeightLocktime), n++), s.requiredTimeLocktime && (r = Math.max(r, s.requiredTimeLocktime), o++); - return n && n >= o ? t : r !== me ? r : this.global.fallbackLocktime || me; + return n && n >= o ? t : r !== Re ? r : this.global.fallbackLocktime || Re; } get version() { if (this.global.txVersion === void 0) @@ -4293,8 +4292,8 @@ let ee = class Wn { // We will lose some vectors -> smaller test coverage of preimages (very important!) inputSighash(t) { this.checkInputIdx(t); - const n = this.inputs[t].sighashType, r = n === void 0 ? D.DEFAULT : n, o = r === D.DEFAULT ? D.ALL : r & 3; - return { sigInputs: r & D.ANYONECANPAY, sigOutputs: o }; + const n = this.inputs[t].sighashType, r = n === void 0 ? j.DEFAULT : n, o = r === j.DEFAULT ? j.ALL : r & 3; + return { sigInputs: r & j.ANYONECANPAY, sigOutputs: o }; } // Very nice for debug purposes, but slow. If there is too much inputs/outputs to add, will be quadratic. // Some cache will be nice, but there chance to have bugs with cache invalidation @@ -4304,11 +4303,11 @@ let ee = class Wn { if (this.inputStatus(s) === "unsigned") continue; const { sigInputs: c, sigOutputs: a } = this.inputSighash(s); - if (c === D.ANYONECANPAY ? r.push(s) : t = !1, a === D.ALL) + if (c === j.ANYONECANPAY ? r.push(s) : t = !1, a === j.ALL) n = !1; - else if (a === D.SINGLE) + else if (a === j.SINGLE) o.push(s); - else if (a !== D.NONE) throw new Error(`Wrong signature hash output type: ${a}`); + else if (a !== j.NONE) throw new Error(`Wrong signature hash output type: ${a}`); } return { addInput: t, addOutput: n, inputs: r, outputs: o }; } @@ -4330,27 +4329,27 @@ let ee = class Wn { if (!this.isFinal) throw new Error("Transaction is not finalized"); let t = 32; - const n = this.outputs.map(xe); - t += 4 * _t.encode(this.outputs.length).length; + const n = this.outputs.map(Ne); + t += 4 * Wt.encode(this.outputs.length).length; for (const r of n) - t += 32 + 4 * $t.encode(r.script).length; - this.hasWitnesses && (t += 2), t += 4 * _t.encode(this.inputs.length).length; + t += 32 + 4 * Mt.encode(r.script).length; + this.hasWitnesses && (t += 2), t += 4 * Wt.encode(this.inputs.length).length; for (const r of this.inputs) - t += 160 + 4 * $t.encode(r.finalScriptSig || X).length, this.hasWitnesses && r.finalScriptWitness && (t += dn.encode(r.finalScriptWitness).length); + t += 160 + 4 * Mt.encode(r.finalScriptSig || ot).length, this.hasWitnesses && r.finalScriptWitness && (t += Cn.encode(r.finalScriptWitness).length); return t; } get vsize() { - return rf(this.weight); + return Fl(this.weight); } toBytes(t = !1, n = !1) { - return Le.encode({ + return Qe.encode({ version: this.version, lockTime: this.lockTime, - inputs: this.inputs.map(en).map((r) => ({ + inputs: this.inputs.map(Tn).map((r) => ({ ...r, - finalScriptSig: t && r.finalScriptSig || X + finalScriptSig: t && r.finalScriptSig || ot })), - outputs: this.outputs.map(xe), + outputs: this.outputs.map(Ne), witnesses: this.inputs.map((r) => r.finalScriptWitness || []), segwitFlag: n && this.hasWitnesses }); @@ -4359,13 +4358,13 @@ let ee = class Wn { return this.toBytes(!1, !1); } get hex() { - return S.encode(this.toBytes(!0, this.hasWitnesses)); + return $.encode(this.toBytes(!0, this.hasWitnesses)); } get hash() { - return S.encode(se(this.toBytes(!0))); + return $.encode(ge(this.toBytes(!0))); } get id() { - return S.encode(se(this.toBytes(!0)).reverse()); + return $.encode(ge(this.toBytes(!0)).reverse()); } // Input stuff checkInputIdx(t) { @@ -4373,7 +4372,7 @@ let ee = class Wn { throw new Error(`Wrong input index=${t}`); } getInput(t) { - return this.checkInputIdx(t), or(this.inputs[t]); + return this.checkInputIdx(t), Or(this.inputs[t]); } get inputsLength() { return this.inputs.length; @@ -4382,16 +4381,16 @@ let ee = class Wn { addInput(t, n = !1) { if (!n && !this.signStatus().addInput) throw new Error("Tx has signed inputs, cannot add new one"); - return this.inputs.push(Ds(t, void 0, void 0, this.opts.disableScriptCheck)), this.inputs.length - 1; + return this.inputs.push(Hi(t, void 0, void 0, this.opts.disableScriptCheck)), this.inputs.length - 1; } updateInput(t, n, r = !1) { this.checkInputIdx(t); let o; if (!r) { const s = this.signStatus(); - (!s.addInput || s.inputs.includes(t)) && (o = Lu); + (!s.addInput || s.inputs.includes(t)) && (o = xl); } - this.inputs[t] = Ds(n, this.inputs[t], o, this.opts.disableScriptCheck, this.opts.allowUnknown); + this.inputs[t] = Hi(n, this.inputs[t], o, this.opts.disableScriptCheck, this.opts.allowUnknown); } // Output stuff checkOutputIdx(t) { @@ -4399,12 +4398,12 @@ let ee = class Wn { throw new Error(`Wrong output index=${t}`); } getOutput(t) { - return this.checkOutputIdx(t), or(this.outputs[t]); + return this.checkOutputIdx(t), Or(this.outputs[t]); } - getOutputAddress(t, n = We) { + getOutputAddress(t, n = un) { const r = this.getOutput(t); if (r.script) - return ke(n).encode(st.decode(r.script)); + return Me(n).encode(ut.decode(r.script)); } get outputsLength() { return this.outputs.length; @@ -4413,11 +4412,11 @@ let ee = class Wn { let { amount: o, script: s } = t; if (o === void 0 && (o = n?.amount), typeof o != "bigint") throw new Error(`Wrong amount type, should be of type bigint in sats, but got ${o} of type ${typeof o}`); - typeof s == "string" && (s = S.decode(s)), s === void 0 && (s = n?.script); + typeof s == "string" && (s = $.decode(s)), s === void 0 && (s = n?.script); let i = { ...n, ...t, amount: o, script: s }; - if (i.amount === void 0 && delete i.amount, i = ro(hn, i, n, r, this.opts.allowUnknown), jo.encode(i), i.script && !this.opts.allowUnknownOutputs && st.decode(i.script).type === "unknown") + if (i.amount === void 0 && delete i.amount, i = zo(_n, i, n, r, this.opts.allowUnknown), Vs.encode(i), i.script && !this.opts.allowUnknownOutputs && ut.decode(i.script).type === "unknown") throw new Error("Transaction/output: unknown output script type, there is a chance that input is unspendable. Pass allowUnknownOutputs=true, if you sure"); - return this.opts.disableScriptCheck || pc(i.script, i.redeemScript, i.witnessScript), i; + return this.opts.disableScriptCheck || Aa(i.script, i.redeemScript, i.witnessScript), i; } addOutput(t, n = !1) { if (!n && !this.signStatus().addOutput) @@ -4429,23 +4428,23 @@ let ee = class Wn { let o; if (!r) { const s = this.signStatus(); - (!s.addOutput || s.outputs.includes(t)) && (o = Pu); + (!s.addOutput || s.outputs.includes(t)) && (o = Sl); } this.outputs[t] = this.normalizeOutput(n, this.outputs[t], o); } - addOutputAddress(t, n, r = We) { - return this.addOutput({ script: st.encode(ke(r).decode(t)), amount: n }); + addOutputAddress(t, n, r = un) { + return this.addOutput({ script: ut.encode(Me(r).decode(t)), amount: n }); } // Utils get fee() { let t = 0n; for (const r of this.inputs) { - const o = Fn(r); + const o = hr(r); if (!o) throw new Error("Empty input amount"); t += o.amount; } - const n = this.outputs.map(xe); + const n = this.outputs.map(Ne); for (const r of n) t -= r.amount; return t; @@ -4455,38 +4454,38 @@ let ee = class Wn { // There is optimization opportunity to re-use hashes for multiple inputs for witness v0/v1, // but we are trying to be less complicated for audit purpose for now. preimageLegacy(t, n, r) { - const { isAny: o, isNone: s, isSingle: i } = _s(r); + const { isAny: o, isNone: s, isSingle: i } = _i(r); if (t < 0 || !Number.isSafeInteger(t)) throw new Error(`Invalid input idx=${t}`); if (i && t >= this.outputs.length || t >= this.inputs.length) - return rc.encode(1n); - n = L.encode(L.decode(n).filter((f) => f !== "CODESEPARATOR")); - let c = this.inputs.map(en).map((f, l) => ({ + return pa.encode(1n); + n = K.encode(K.decode(n).filter((f) => f !== "CODESEPARATOR")); + let c = this.inputs.map(Tn).map((f, l) => ({ ...f, - finalScriptSig: l === t ? n : X + finalScriptSig: l === t ? n : ot })); o ? c = [c[t]] : (s || i) && (c = c.map((f, l) => ({ ...f, sequence: l === t ? f.sequence : 0 }))); - let a = this.outputs.map(xe); - s ? a = [] : i && (a = a.slice(0, t).fill(nf).concat([a[t]])); - const u = Le.encode({ + let a = this.outputs.map(Ne); + s ? a = [] : i && (a = a.slice(0, t).fill(Kl).concat([a[t]])); + const u = Qe.encode({ lockTime: this.lockTime, version: this.version, segwitFlag: !1, inputs: c, outputs: a }); - return se(u, Re.encode(r)); + return ge(u, Ye.encode(r)); } preimageWitnessV0(t, n, r, o) { - const { isAny: s, isNone: i, isSingle: c } = _s(r); - let a = Cn, u = Cn, f = Cn; - const l = this.inputs.map(en), d = this.outputs.map(xe); - s || (a = se(...l.map(Vr.encode))), !s && !c && !i && (u = se(...l.map((w) => W.encode(w.sequence)))), !c && !i ? f = se(...d.map(Te.encode)) : c && t < d.length && (f = se(Te.encode(d[t]))); + const { isAny: s, isNone: i, isSingle: c } = _i(r); + let a = er, u = er, f = er; + const l = this.inputs.map(Tn), d = this.outputs.map(Ne); + s || (a = ge(...l.map(xo.encode))), !s && !c && !i && (u = ge(...l.map((w) => Z.encode(w.sequence)))), !c && !i ? f = ge(...d.map(Pe.encode)) : c && t < d.length && (f = ge(Pe.encode(d[t]))); const h = l[t]; - return se(Re.encode(this.version), a, u, Z(32, !0).encode(h.txid), W.encode(h.index), $t.encode(n), Kn.encode(o), W.encode(h.sequence), f, W.encode(this.lockTime), W.encode(r)); + return ge(Ye.encode(this.version), a, u, rt(32, !0).encode(h.txid), Z.encode(h.index), Mt.encode(n), dr.encode(o), Z.encode(h.sequence), f, Z.encode(this.lockTime), Z.encode(r)); } preimageWitnessV1(t, n, r, o, s = -1, i, c = 192, a) { if (!Array.isArray(o) || this.inputs.length !== o.length) @@ -4494,38 +4493,38 @@ let ee = class Wn { if (!Array.isArray(n) || this.inputs.length !== n.length) throw new Error(`Invalid prevOutScript array=${n}`); const u = [ - ae.encode(0), - ae.encode(r), + Se.encode(0), + Se.encode(r), // U8 sigHash - Re.encode(this.version), - W.encode(this.lockTime) - ], f = r === D.DEFAULT ? D.ALL : r & 3, l = r & D.ANYONECANPAY, d = this.inputs.map(en), h = this.outputs.map(xe); - l !== D.ANYONECANPAY && u.push(...[ - d.map(Vr.encode), - o.map(Kn.encode), - n.map($t.encode), - d.map((p) => W.encode(p.sequence)) - ].map((p) => ht(ce(...p)))), f === D.ALL && u.push(ht(ce(...h.map(Te.encode)))); + Ye.encode(this.version), + Z.encode(this.lockTime) + ], f = r === j.DEFAULT ? j.ALL : r & 3, l = r & j.ANYONECANPAY, d = this.inputs.map(Tn), h = this.outputs.map(Ne); + l !== j.ANYONECANPAY && u.push(...[ + d.map(xo.encode), + o.map(dr.encode), + n.map(Mt.encode), + d.map((g) => Z.encode(g.sequence)) + ].map((g) => wt(Ee(...g)))), f === j.ALL && u.push(wt(Ee(...h.map(Pe.encode)))); const w = (a ? 1 : 0) | (i ? 2 : 0); - if (u.push(new Uint8Array([w])), l === D.ANYONECANPAY) { - const p = d[t]; - u.push(Vr.encode(p), Kn.encode(o[t]), $t.encode(n[t]), W.encode(p.sequence)); + if (u.push(new Uint8Array([w])), l === j.ANYONECANPAY) { + const g = d[t]; + u.push(xo.encode(g), dr.encode(o[t]), Mt.encode(n[t]), Z.encode(g.sequence)); } else - u.push(W.encode(t)); - return w & 1 && u.push(ht($t.encode(a || X))), f === D.SINGLE && u.push(t < h.length ? ht(Te.encode(h[t])) : Cn), i && u.push(cn(i, c), ae.encode(0), Re.encode(s)), Mo("TapSighash", ...u); + u.push(Z.encode(t)); + return w & 1 && u.push(wt(Mt.encode(a || ot))), f === j.SINGLE && u.push(t < h.length ? wt(Pe.encode(h[t])) : er), i && u.push(On(i, c), Se.encode(0), Ye.encode(s)), Ns("TapSighash", ...u); } // Signer can be privateKey OR instance of bip32 HD stuff signIdx(t, n, r, o) { this.checkInputIdx(n); - const s = this.inputs[n], i = Hs(s, this.opts.allowLegacyWitnessUtxo); - if (!q(t)) { + const s = this.inputs[n], i = Vi(s, this.opts.allowLegacyWitnessUtxo); + if (!nt(t)) { if (!s.bip32Derivation || !s.bip32Derivation.length) throw new Error("bip32Derivation: empty"); const f = s.bip32Derivation.filter((d) => d[1].fingerprint == t.fingerprint).map(([d, { path: h }]) => { let w = t; - for (const p of h) - w = w.deriveChild(p); - if (!rt(w.publicKey, d)) + for (const g of h) + w = w.deriveChild(g); + if (!ct(w.publicKey, d)) throw new Error("bip32Derivation: wrong pubKey"); if (!w.privateKey) throw new Error("bip32Derivation: no privateKey"); @@ -4538,56 +4537,56 @@ let ee = class Wn { this.signIdx(d.privateKey, n) && (l = !0); return l; } - r ? r.forEach(uf) : r = [i.defaultSighash]; + r ? r.forEach(jl) : r = [i.defaultSighash]; const c = i.sighash; if (!r.includes(c)) throw new Error(`Input with not allowed sigHash=${c}. Allowed: ${r.join(", ")}`); const { sigOutputs: a } = this.inputSighash(n); - if (a === D.SINGLE && n >= this.outputs.length) + if (a === j.SINGLE && n >= this.outputs.length) throw new Error(`Input with sighash SINGLE, but there is no output with corresponding index=${n}`); - const u = Fn(s); + const u = hr(s); if (i.txType === "taproot") { - const f = this.inputs.map(Fn), l = f.map((m) => m.script), d = f.map((m) => m.amount); - let h = !1, w = Ho(t), p = s.tapMerkleRoot || X; + const f = this.inputs.map(hr), l = f.map((y) => y.script), d = f.map((y) => y.amount); + let h = !1, w = Rs(t), g = s.tapMerkleRoot || ot; if (s.tapInternalKey) { - const { pubKey: m, privKey: x } = af(t, w, s.tapInternalKey, p), [b, v] = to(s.tapInternalKey, p); - if (rt(b, m)) { - const O = this.preimageWitnessV1(n, l, c, d), A = ce(Is(O, x, o), c !== D.DEFAULT ? new Uint8Array([c]) : X); - this.updateInput(n, { tapKeySig: A }, !0), h = !0; + const { pubKey: y, privKey: S } = ql(t, w, s.tapInternalKey, g), [v, O] = Ko(s.tapInternalKey, g); + if (ct(v, y)) { + const N = this.preimageWitnessV1(n, l, c, d), U = Ee(Ii(N, S, o), c !== j.DEFAULT ? new Uint8Array([c]) : ot); + this.updateInput(n, { tapKeySig: U }, !0), h = !0; } } if (s.tapLeafScript) { s.tapScriptSig = s.tapScriptSig || []; - for (const [m, x] of s.tapLeafScript) { - const b = x.subarray(0, -1), v = L.decode(b), O = x[x.length - 1], A = cn(b, O); - if (v.findIndex((H) => q(H) && rt(H, w)) === -1) + for (const [y, S] of s.tapLeafScript) { + const v = S.subarray(0, -1), O = K.decode(v), N = S[S.length - 1], U = On(v, N); + if (O.findIndex((C) => nt(C) && ct(C, w)) === -1) continue; - const y = this.preimageWitnessV1(n, l, c, d, void 0, b, O), tt = ce(Is(y, t, o), c !== D.DEFAULT ? new Uint8Array([c]) : X); - this.updateInput(n, { tapScriptSig: [[{ pubKey: w, leafHash: A }, tt]] }, !0), h = !0; + const x = this.preimageWitnessV1(n, l, c, d, void 0, v, N), Q = Ee(Ii(x, t, o), c !== j.DEFAULT ? new Uint8Array([c]) : ot); + this.updateInput(n, { tapScriptSig: [[{ pubKey: w, leafHash: U }, Q]] }, !0), h = !0; } } if (!h) throw new Error("No taproot scripts signed"); return !0; } else { - const f = cc(t); + const f = ma(t); let l = !1; - const d = ic(f); - for (const p of L.decode(i.lastScript)) - q(p) && (rt(p, f) || rt(p, d)) && (l = !0); + const d = ya(f); + for (const g of K.decode(i.lastScript)) + nt(g) && (ct(g, f) || ct(g, d)) && (l = !0); if (!l) throw new Error(`Input script doesn't have pubKey: ${i.lastScript}`); let h; if (i.txType === "legacy") h = this.preimageLegacy(n, i.lastScript, c); else if (i.txType === "segwit") { - let p = i.lastScript; - i.last.type === "wpkh" && (p = st.encode({ type: "pkh", hash: i.last.hash })), h = this.preimageWitnessV0(n, p, c, u.amount); + let g = i.lastScript; + i.last.type === "wpkh" && (g = ut.encode({ type: "pkh", hash: i.last.hash })), h = this.preimageWitnessV0(n, g, c, u.amount); } else throw new Error(`Transaction/sign: unknown tx type: ${i.txType}`); - const w = vu(h, t, this.opts.lowR); + const w = fl(h, t, this.opts.lowR); this.updateInput(n, { - partialSig: [[f, ce(w, new Uint8Array([c]))]] + partialSig: [[f, Ee(w, new Uint8Array([c]))]] }, !0); } return !0; @@ -4613,102 +4612,102 @@ let ee = class Wn { finalizeIdx(t) { if (this.checkInputIdx(t), this.fee < 0n) throw new Error("Outputs spends more than inputs amount"); - const n = this.inputs[t], r = Hs(n, this.opts.allowLegacyWitnessUtxo); + const n = this.inputs[t], r = Vi(n, this.opts.allowLegacyWitnessUtxo); if (r.txType === "taproot") { if (n.tapKeySig) n.finalScriptWitness = [n.tapKeySig]; else if (n.tapLeafScript && n.tapScriptSig) { - const a = n.tapLeafScript.sort((u, f) => Wt.encode(u[0]).length - Wt.encode(f[0]).length); + const a = n.tapLeafScript.sort((u, f) => Zt.encode(u[0]).length - Zt.encode(f[0]).length); for (const [u, f] of a) { - const l = f.slice(0, -1), d = f[f.length - 1], h = st.decode(l), w = cn(l, d), p = n.tapScriptSig.filter((x) => rt(x[0].leafHash, w)); - let m = []; + const l = f.slice(0, -1), d = f[f.length - 1], h = ut.decode(l), w = On(l, d), g = n.tapScriptSig.filter((S) => ct(S[0].leafHash, w)); + let y = []; if (h.type === "tr_ms") { - const x = h.m, b = h.pubkeys; - let v = 0; - for (const O of b) { - const A = p.findIndex((F) => rt(F[0].pubKey, O)); - if (v === x || A === -1) { - m.push(X); + const S = h.m, v = h.pubkeys; + let O = 0; + for (const N of v) { + const U = g.findIndex((W) => ct(W[0].pubKey, N)); + if (O === S || U === -1) { + y.push(ot); continue; } - m.push(p[A][1]), v++; + y.push(g[U][1]), O++; } - if (v !== x) + if (O !== S) continue; } else if (h.type === "tr_ns") { - for (const x of h.pubkeys) { - const b = p.findIndex((v) => rt(v[0].pubKey, x)); - b !== -1 && m.push(p[b][1]); + for (const S of h.pubkeys) { + const v = g.findIndex((O) => ct(O[0].pubKey, S)); + v !== -1 && y.push(g[v][1]); } - if (m.length !== h.pubkeys.length) + if (y.length !== h.pubkeys.length) continue; } else if (h.type === "unknown" && this.opts.allowUnknownInputs) { - const x = L.decode(l); - if (m = p.map(([{ pubKey: b }, v]) => { - const O = x.findIndex((A) => q(A) && rt(A, b)); - if (O === -1) + const S = K.decode(l); + if (y = g.map(([{ pubKey: v }, O]) => { + const N = S.findIndex((U) => nt(U) && ct(U, v)); + if (N === -1) throw new Error("finalize/taproot: cannot find position of pubkey in script"); - return { signature: v, pos: O }; - }).sort((b, v) => b.pos - v.pos).map((b) => b.signature), !m.length) + return { signature: O, pos: N }; + }).sort((v, O) => v.pos - O.pos).map((v) => v.signature), !y.length) continue; } else { - const x = this.opts.customScripts; - if (x) - for (const b of x) { - if (!b.finalizeTaproot) + const S = this.opts.customScripts; + if (S) + for (const v of S) { + if (!v.finalizeTaproot) continue; - const v = L.decode(l), O = b.encode(v); - if (O === void 0) + const O = K.decode(l), N = v.encode(O); + if (N === void 0) continue; - const A = b.finalizeTaproot(l, O, p); - if (A) { - n.finalScriptWitness = A.concat(Wt.encode(u)), n.finalScriptSig = X, _r(n); + const U = v.finalizeTaproot(l, N, g); + if (U) { + n.finalScriptWitness = U.concat(Zt.encode(u)), n.finalScriptSig = ot, Eo(n); return; } } throw new Error("Finalize: Unknown tapLeafScript"); } - n.finalScriptWitness = m.reverse().concat([l, Wt.encode(u)]); + n.finalScriptWitness = y.reverse().concat([l, Zt.encode(u)]); break; } if (!n.finalScriptWitness) throw new Error("finalize/taproot: empty witness"); } else throw new Error("finalize/taproot: unknown input"); - n.finalScriptSig = X, _r(n); + n.finalScriptSig = ot, Eo(n); return; } if (!n.partialSig || !n.partialSig.length) throw new Error("Not enough partial sign"); - let o = X, s = []; + let o = ot, s = []; if (r.last.type === "ms") { const a = r.last.m, u = r.last.pubkeys; let f = []; for (const l of u) { - const d = n.partialSig.find((h) => rt(l, h[0])); + const d = n.partialSig.find((h) => ct(l, h[0])); d && f.push(d[1]); } if (f = f.slice(0, a), f.length !== a) throw new Error(`Multisig: wrong signatures count, m=${a} n=${u.length} signatures=${f.length}`); - o = L.encode([0, ...f]); + o = K.encode([0, ...f]); } else if (r.last.type === "pk") - o = L.encode([n.partialSig[0][1]]); + o = K.encode([n.partialSig[0][1]]); else if (r.last.type === "pkh") - o = L.encode([n.partialSig[0][1], n.partialSig[0][0]]); + o = K.encode([n.partialSig[0][1], n.partialSig[0][0]]); else if (r.last.type === "wpkh") - o = X, s = [n.partialSig[0][1], n.partialSig[0][0]]; + o = ot, s = [n.partialSig[0][1], n.partialSig[0][0]]; else if (r.last.type === "unknown" && !this.opts.allowUnknownInputs) throw new Error("Unknown inputs not allowed"); let i, c; - if (r.type.includes("wsh-") && (o.length && r.lastScript.length && (s = L.decode(o).map((a) => { + if (r.type.includes("wsh-") && (o.length && r.lastScript.length && (s = K.decode(o).map((a) => { if (a === 0) - return X; - if (q(a)) + return ot; + if (nt(a)) return a; throw new Error(`Wrong witness op=${a}`); - })), s = s.concat(r.lastScript)), r.txType === "segwit" && (c = s), r.type.startsWith("sh-wsh-") ? i = L.encode([L.encode([0, ht(r.lastScript)])]) : r.type.startsWith("sh-") ? i = L.encode([...L.decode(o), r.lastScript]) : r.type.startsWith("wsh-") || r.txType !== "segwit" && (i = o), !i && !c) + })), s = s.concat(r.lastScript)), r.txType === "segwit" && (c = s), r.type.startsWith("sh-wsh-") ? i = K.encode([K.encode([0, wt(r.lastScript)])]) : r.type.startsWith("sh-") ? i = K.encode([...K.decode(o), r.lastScript]) : r.type.startsWith("wsh-") || r.txType !== "segwit" && (i = o), !i && !c) throw new Error("Unknown error finalizing input"); - i && (n.finalScriptSig = i), c && (n.finalScriptWitness = c), _r(n); + i && (n.finalScriptSig = i), c && (n.finalScriptWitness = c), Eo(n); } finalize() { for (let t = 0; t < this.inputs.length; t++) @@ -4730,10 +4729,10 @@ let ee = class Wn { for (const o of ["inputs", "outputs"]) if (this[o].length !== t[o].length) throw new Error(`Transaction/combine: different ${o} length this=${this[o].length} other=${t[o].length}`); - const n = this.global.unsignedTx ? on.encode(this.global.unsignedTx) : X, r = t.global.unsignedTx ? on.encode(t.global.unsignedTx) : X; - if (!rt(n, r)) + const n = this.global.unsignedTx ? kn.encode(this.global.unsignedTx) : ot, r = t.global.unsignedTx ? kn.encode(t.global.unsignedTx) : ot; + if (!ct(n, r)) throw new Error("Transaction/combine: different unsigned tx"); - this.global = ro(Wo, this.global, t.global, void 0, this.opts.allowUnknown); + this.global = zo(_s, this.global, t.global, void 0, this.opts.allowUnknown); for (let o = 0; o < this.inputs.length; o++) this.updateInput(o, t.inputs[o], !0); for (let o = 0; o < this.outputs.length; o++) @@ -4741,47 +4740,47 @@ let ee = class Wn { return this; } clone() { - return Wn.fromPSBT(this.toPSBT(this.opts.PSBTVersion), this.opts); + return pr.fromPSBT(this.toPSBT(this.opts.PSBTVersion), this.opts); } }; -class he extends ee { +class ke extends ue { constructor(t) { - super(Dr(t)); + super(So(t)); } static fromPSBT(t, n) { - return ee.fromPSBT(t, Dr(n)); + return ue.fromPSBT(t, So(n)); } static fromRaw(t, n) { - return ee.fromRaw(t, Dr(n)); + return ue.fromRaw(t, So(n)); } } -he.ARK_TX_OPTS = { +ke.ARK_TX_OPTS = { allowUnknown: !0, allowUnknownOutputs: !0, allowUnknownInputs: !0 }; -function Dr(e) { - return { ...he.ARK_TX_OPTS, ...e }; +function So(e) { + return { ...ke.ARK_TX_OPTS, ...e }; } -class Yo extends Error { +class Ms extends Error { idx; // Indice of participant constructor(t, n) { super(n), this.idx = t; } } -const { taggedHash: mc, pointToBytes: $n } = Nt.utils, Vt = Ft.Point, _ = Vt.Fn, zt = Ft.lengths.publicKey, co = new Uint8Array(zt), Ms = de(Z(33), { - decode: (e) => gn(e) ? co : e.toBytes(!0), - encode: (e) => ln(e, co) ? Vt.ZERO : Vt.fromBytes(e) -}), Ks = xt(rc, (e) => (bi("n", e, 1n, _.ORDER), e)), Pe = ft({ R1: Ms, R2: Ms }), xc = ft({ k1: Ks, k2: Ks, publicKey: Z(zt) }); -function Fs(e, ...t) { +const { taggedHash: Oa, pointToBytes: nr } = de.utils, zt = xe.Point, z = zt.Fn, Jt = xe.lengths.publicKey, Yo = new Uint8Array(Jt), Di = Ie(rt(33), { + decode: (e) => Hn(e) ? Yo : e.toBytes(!0), + encode: (e) => Ln(e, Yo) ? zt.ZERO : zt.fromBytes(e) +}), Mi = Tt(pa, (e) => (Uc("n", e, 1n, z.ORDER), e)), Je = pt({ R1: Di, R2: Di }), $a = pt({ k1: Mi, k2: Mi, publicKey: rt(Jt) }); +function Ki(e, ...t) { } -function Bt(e, ...t) { +function Lt(e, ...t) { if (!Array.isArray(e)) throw new Error("expected array"); - e.forEach((n) => V(n, ...t)); + e.forEach((n) => G(n, ...t)); } -function Ws(e) { +function Fi(e) { if (!Array.isArray(e)) throw new Error("expected array"); e.forEach((t, n) => { @@ -4789,80 +4788,80 @@ function Ws(e) { throw new Error("expected boolean in xOnly array, got" + t + "(" + n + ")"); }); } -const sr = (e, ...t) => _.create(_.fromBytes(mc(e, ...t), !0)), nn = (e, t) => An(e.y) ? t : _.neg(t); -function ve(e) { - return Vt.BASE.multiply(e); +const $r = (e, ...t) => z.create(z.fromBytes(Oa(e, ...t), !0)), An = (e, t) => Yn(e.y) ? t : z.neg(t); +function He(e) { + return zt.BASE.multiply(e); } -function gn(e) { - return e.equals(Vt.ZERO); +function Hn(e) { + return e.equals(zt.ZERO); } -function ao(e) { - return Bt(e, zt), e.sort(er); +function Zo(e) { + return Lt(e, Jt), e.sort(Ir); } -function bc(e) { - Bt(e, zt); +function Ua(e) { + Lt(e, Jt); for (let t = 1; t < e.length; t++) - if (!ln(e[t], e[0])) + if (!Ln(e[t], e[0])) return e[t]; - return co; + return Yo; } -function Ec(e) { - return Bt(e, zt), mc("KeyAgg list", ...e); +function Ra(e) { + return Lt(e, Jt), Oa("KeyAgg list", ...e); } -function Sc(e, t, n) { - return V(e, zt), V(t, zt), ln(e, t) ? 1n : sr("KeyAgg coefficient", n, e); +function Na(e, t, n) { + return G(e, Jt), G(t, Jt), Ln(e, t) ? 1n : $r("KeyAgg coefficient", n, e); } -function uo(e, t = [], n = []) { - if (Bt(e, zt), Bt(t, 32), t.length !== n.length) +function Xo(e, t = [], n = []) { + if (Lt(e, Jt), Lt(t, 32), t.length !== n.length) throw new Error("The tweaks and isXonly arrays must have the same length"); - const r = bc(e), o = Ec(e); - let s = Vt.ZERO; + const r = Ua(e), o = Ra(e); + let s = zt.ZERO; for (let a = 0; a < e.length; a++) { let u; try { - u = Vt.fromBytes(e[a]); + u = zt.fromBytes(e[a]); } catch { - throw new Yo(a, "pubkey"); + throw new Ms(a, "pubkey"); } - s = s.add(u.multiply(Sc(e[a], r, o))); + s = s.add(u.multiply(Na(e[a], r, o))); } - let i = _.ONE, c = _.ZERO; + let i = z.ONE, c = z.ZERO; for (let a = 0; a < t.length; a++) { - const u = n[a] && !An(s.y) ? _.neg(_.ONE) : _.ONE, f = _.fromBytes(t[a]); - if (s = s.multiply(u).add(ve(f)), gn(s)) + const u = n[a] && !Yn(s.y) ? z.neg(z.ONE) : z.ONE, f = z.fromBytes(t[a]); + if (s = s.multiply(u).add(He(f)), Hn(s)) throw new Error("The result of tweaking cannot be infinity"); - i = _.mul(u, i), c = _.add(f, _.mul(u, c)); + i = z.mul(u, i), c = z.add(f, z.mul(u, c)); } return { aggPublicKey: s, gAcc: i, tweakAcc: c }; } -const Gs = (e, t, n, r, o, s) => sr("MuSig/nonce", e, new Uint8Array([t.length]), t, new Uint8Array([n.length]), n, o, Sn(s.length, 4), s, new Uint8Array([r])); -function lf(e, t, n = new Uint8Array(0), r, o = new Uint8Array(0), s = En(32)) { - if (V(e, zt), Fs(t, 32), V(n), ![0, 32].includes(n.length)) +const Wi = (e, t, n, r, o, s) => $r("MuSig/nonce", e, new Uint8Array([t.length]), t, new Uint8Array([n.length]), n, o, zn(s.length, 4), s, new Uint8Array([r])); +function Zl(e, t, n = new Uint8Array(0), r, o = new Uint8Array(0), s = Wn(32)) { + if (G(e, Jt), Ki(t, 32), G(n), ![0, 32].includes(n.length)) throw new Error("wrong aggPublicKey"); - Fs(), V(o), V(s, 32); - const i = Uint8Array.of(0), c = Gs(s, e, n, 0, i, o), a = Gs(s, e, n, 1, i, o); + Ki(), G(o), G(s, 32); + const i = Uint8Array.of(0), c = Wi(s, e, n, 0, i, o), a = Wi(s, e, n, 1, i, o); return { - secret: xc.encode({ k1: c, k2: a, publicKey: e }), - public: Pe.encode({ R1: ve(c), R2: ve(a) }) + secret: $a.encode({ k1: c, k2: a, publicKey: e }), + public: Je.encode({ R1: He(c), R2: He(a) }) }; } -function df(e) { - Bt(e, 66); - let t = Vt.ZERO, n = Vt.ZERO; +function Xl(e) { + Lt(e, 66); + let t = zt.ZERO, n = zt.ZERO; for (let r = 0; r < e.length; r++) { const o = e[r]; try { - const { R1: s, R2: i } = Pe.decode(o); - if (gn(s) || gn(i)) + const { R1: s, R2: i } = Je.decode(o); + if (Hn(s) || Hn(i)) throw new Error("infinity point"); t = t.add(s), n = n.add(i); } catch { - throw new Yo(r, "pubnonce"); + throw new Ms(r, "pubnonce"); } } - return Pe.encode({ R1: t, R2: n }); + return Je.encode({ R1: t, R2: n }); } -class hf { +class Ql { publicKeys; Q; gAcc; @@ -4886,12 +4885,12 @@ class hf { * @throws {Error} If the input is invalid, such as wrong array sizes or lengths. */ constructor(t, n, r, o = [], s = []) { - if (Bt(n, 33), Bt(o, 32), Ws(s), V(r), o.length !== s.length) + if (Lt(n, 33), Lt(o, 32), Fi(s), G(r), o.length !== s.length) throw new Error("The tweaks and isXonly arrays must have the same length"); - const { aggPublicKey: i, gAcc: c, tweakAcc: a } = uo(n, o, s), { R1: u, R2: f } = Pe.decode(t); - this.publicKeys = n, this.Q = i, this.gAcc = c, this.tweakAcc = a, this.b = sr("MuSig/noncecoef", t, $n(i), r); + const { aggPublicKey: i, gAcc: c, tweakAcc: a } = Xo(n, o, s), { R1: u, R2: f } = Je.decode(t); + this.publicKeys = n, this.Q = i, this.gAcc = c, this.tweakAcc = a, this.b = $r("MuSig/noncecoef", t, nr(i), r); const l = u.add(f.multiply(this.b)); - this.R = gn(l) ? Vt.BASE : l, this.e = sr("BIP0340/challenge", $n(this.R), $n(i), r), this.tweaks = o, this.isXonly = s, this.L = Ec(n), this.secondKey = bc(n); + this.R = Hn(l) ? zt.BASE : l, this.e = $r("BIP0340/challenge", nr(this.R), nr(i), r), this.tweaks = o, this.isXonly = s, this.L = Ra(n), this.secondKey = Ua(n); } /** * Calculates the key aggregation coefficient for a given point. @@ -4902,16 +4901,16 @@ class hf { */ getSessionKeyAggCoeff(t) { const { publicKeys: n } = this, r = t.toBytes(!0); - if (!n.some((s) => ln(s, r))) + if (!n.some((s) => Ln(s, r))) throw new Error("The signer's pubkey must be included in the list of pubkeys"); - return Sc(r, this.secondKey, this.L); + return Na(r, this.secondKey, this.L); } partialSigVerifyInternal(t, n, r) { - const { Q: o, gAcc: s, b: i, R: c, e: a } = this, u = _.fromBytes(t, !0); - if (!_.isValid(u)) + const { Q: o, gAcc: s, b: i, R: c, e: a } = this, u = z.fromBytes(t, !0); + if (!z.isValid(u)) return !1; - const { R1: f, R2: l } = Pe.decode(n), d = f.add(l.multiply(i)), h = An(c.y) ? d : d.negate(), w = Vt.fromBytes(r), p = this.getSessionKeyAggCoeff(w), m = _.mul(nn(o, 1n), s), x = ve(u), b = h.add(w.multiply(_.mul(a, _.mul(p, m)))); - return x.equals(b); + const { R1: f, R2: l } = Je.decode(n), d = f.add(l.multiply(i)), h = Yn(c.y) ? d : d.negate(), w = zt.fromBytes(r), g = this.getSessionKeyAggCoeff(w), y = z.mul(An(o, 1n), s), S = He(u), v = h.add(w.multiply(z.mul(a, z.mul(g, y)))); + return S.equals(v); } /** * Generates a partial signature for a given message, secret nonce, secret key, and session context. @@ -4923,29 +4922,29 @@ class hf { * @throws {Error} If the input is invalid, such as wrong array sizes, invalid nonce or secret key. */ sign(t, n, r = !1) { - if (V(n, 32), typeof r != "boolean") + if (G(n, 32), typeof r != "boolean") throw new Error("expected boolean"); - const { Q: o, gAcc: s, b: i, R: c, e: a } = this, { k1: u, k2: f, publicKey: l } = xc.decode(t); - if (t.fill(0, 0, 64), !_.isValid(u)) + const { Q: o, gAcc: s, b: i, R: c, e: a } = this, { k1: u, k2: f, publicKey: l } = $a.decode(t); + if (t.fill(0, 0, 64), !z.isValid(u)) throw new Error("wrong k1"); - if (!_.isValid(f)) + if (!z.isValid(f)) throw new Error("wrong k1"); - const d = nn(c, u), h = nn(c, f), w = _.fromBytes(n); - if (_.is0(w)) + const d = An(c, u), h = An(c, f), w = z.fromBytes(n); + if (z.is0(w)) throw new Error("wrong d_"); - const p = ve(w), m = p.toBytes(!0); - if (!ln(m, l)) + const g = He(w), y = g.toBytes(!0); + if (!Ln(y, l)) throw new Error("Public key does not match nonceGen argument"); - const x = this.getSessionKeyAggCoeff(p), b = nn(o, 1n), v = _.mul(b, _.mul(s, w)), O = _.add(d, _.add(_.mul(i, h), _.mul(a, _.mul(x, v)))), A = _.toBytes(O); + const S = this.getSessionKeyAggCoeff(g), v = An(o, 1n), O = z.mul(v, z.mul(s, w)), N = z.add(d, z.add(z.mul(i, h), z.mul(a, z.mul(S, O)))), U = z.toBytes(N); if (!r) { - const F = Pe.encode({ - R1: ve(u), - R2: ve(f) + const W = Je.encode({ + R1: He(u), + R2: He(f) }); - if (!this.partialSigVerifyInternal(A, F, m)) + if (!this.partialSigVerifyInternal(U, W, y)) throw new Error("Partial signature verification failed"); } - return A; + return U; } /** * Verifies a partial signature against the aggregate public key and other session parameters. @@ -4961,7 +4960,7 @@ class hf { */ partialSigVerify(t, n, r) { const { publicKeys: o, tweaks: s, isXonly: i } = this; - if (V(t, 32), Bt(n, 66), Bt(o, zt), Bt(s, 32), Ws(i), fe(r), n.length !== o.length) + if (G(t, 32), Lt(n, 66), Lt(o, Jt), Lt(s, 32), Fi(i), Te(r), n.length !== o.length) throw new Error("The pubNonces and publicKeys arrays must have the same length"); if (s.length !== i.length) throw new Error("The tweaks and isXonly arrays must have the same length"); @@ -4977,28 +4976,257 @@ class hf { * @throws {Error} If the input is invalid, such as wrong array sizes, invalid signature. */ partialSigAgg(t) { - Bt(t, 32); + Lt(t, 32); const { Q: n, tweakAcc: r, R: o, e: s } = this; let i = 0n; for (let a = 0; a < t.length; a++) { - const u = _.fromBytes(t[a], !0); - if (!_.isValid(u)) - throw new Yo(a, "psig"); - i = _.add(i, u); + const u = z.fromBytes(t[a], !0); + if (!z.isValid(u)) + throw new Ms(a, "psig"); + i = z.add(i, u); } - const c = nn(n, 1n); - return i = _.add(i, _.mul(s, _.mul(c, r))), Lt($n(o), _.toBytes(i)); + const c = An(n, 1n); + return i = z.add(i, z.mul(s, z.mul(c, r))), Kt(nr(o), z.toBytes(i)); } } -function pf(e) { - const t = lf(e); +function Jl(e) { + const t = Zl(e); return { secNonce: t.secret, pubNonce: t.public }; } -function gf(e) { - return df(e); +function td(e) { + return Xl(e); +} +/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ +function Ks(e) { + return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; +} +function Fe(e, t = "") { + if (!Number.isSafeInteger(e) || e < 0) { + const n = t && `"${t}" `; + throw new Error(`${n}expected integer >0, got ${e}`); + } +} +function et(e, t, n = "") { + const r = Ks(e), o = e?.length, s = t !== void 0; + if (!r || s && o !== t) { + const i = n && `"${n}" `, c = s ? ` of length ${t}` : "", a = r ? `length=${o}` : `type=${typeof e}`; + throw new Error(i + "expected Uint8Array" + c + ", got " + a); + } + return e; +} +function La(e) { + if (typeof e != "function" || typeof e.create != "function") + throw new Error("Hash must wrapped by utils.createHasher"); + Fe(e.outputLen), Fe(e.blockLen); +} +function Ur(e, t = !0) { + if (e.destroyed) + throw new Error("Hash instance has been destroyed"); + if (t && e.finished) + throw new Error("Hash#digest() has already been called"); +} +function ed(e, t) { + et(e, void 0, "digestInto() output"); + const n = t.outputLen; + if (e.length < n) + throw new Error('"digestInto() output" expected to be of length >=' + n); +} +function Rr(...e) { + for (let t = 0; t < e.length; t++) + e[t].fill(0); +} +function vo(e) { + return new DataView(e.buffer, e.byteOffset, e.byteLength); +} +function jt(e, t) { + return e << 32 - t | e >>> t; +} +const Ca = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", nd = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); +function so(e) { + if (et(e), Ca) + return e.toHex(); + let t = ""; + for (let n = 0; n < e.length; n++) + t += nd[e[n]]; + return t; +} +const re = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; +function zi(e) { + if (e >= re._0 && e <= re._9) + return e - re._0; + if (e >= re.A && e <= re.F) + return e - (re.A - 10); + if (e >= re.a && e <= re.f) + return e - (re.a - 10); +} +function Nr(e) { + if (typeof e != "string") + throw new Error("hex string expected, got " + typeof e); + if (Ca) + return Uint8Array.fromHex(e); + const t = e.length, n = t / 2; + if (t % 2) + throw new Error("hex string expected, got unpadded hex of length " + t); + const r = new Uint8Array(n); + for (let o = 0, s = 0; o < n; o++, s += 2) { + const i = zi(e.charCodeAt(s)), c = zi(e.charCodeAt(s + 1)); + if (i === void 0 || c === void 0) { + const a = e[s] + e[s + 1]; + throw new Error('hex string expected, got non-hex character "' + a + '" at index ' + s); + } + r[o] = i * 16 + c; + } + return r; +} +function Xt(...e) { + let t = 0; + for (let r = 0; r < e.length; r++) { + const o = e[r]; + et(o), t += o.length; + } + const n = new Uint8Array(t); + for (let r = 0, o = 0; r < e.length; r++) { + const s = e[r]; + n.set(s, o), o += s.length; + } + return n; +} +function rd(e, t = {}) { + const n = (o, s) => e(s).update(o).digest(), r = e(void 0); + return n.outputLen = r.outputLen, n.blockLen = r.blockLen, n.create = (o) => e(o), Object.assign(n, t), Object.freeze(n); +} +function io(e = 32) { + const t = typeof globalThis == "object" ? globalThis.crypto : null; + if (typeof t?.getRandomValues != "function") + throw new Error("crypto.getRandomValues must be defined"); + return t.getRandomValues(new Uint8Array(e)); +} +const od = (e) => ({ + oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, e]) +}); +/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ +const Fs = /* @__PURE__ */ BigInt(0), Qo = /* @__PURE__ */ BigInt(1); +function Lr(e, t = "") { + if (typeof e != "boolean") { + const n = t && `"${t}" `; + throw new Error(n + "expected boolean, got type=" + typeof e); + } + return e; +} +function _a(e) { + if (typeof e == "bigint") { + if (!gr(e)) + throw new Error("positive bigint expected, got " + e); + } else + Fe(e); + return e; +} +function rr(e) { + const t = _a(e).toString(16); + return t.length & 1 ? "0" + t : t; +} +function Pa(e) { + if (typeof e != "string") + throw new Error("hex string expected, got " + typeof e); + return e === "" ? Fs : BigInt("0x" + e); +} +function bn(e) { + return Pa(so(e)); +} +function Ha(e) { + return Pa(so(sd(et(e)).reverse())); +} +function Ws(e, t) { + Fe(t), e = _a(e); + const n = Nr(e.toString(16).padStart(t * 2, "0")); + if (n.length !== t) + throw new Error("number too large"); + return n; +} +function Va(e, t) { + return Ws(e, t).reverse(); +} +function sd(e) { + return Uint8Array.from(e); +} +function id(e) { + return Uint8Array.from(e, (t, n) => { + const r = t.charCodeAt(0); + if (t.length !== 1 || r > 127) + throw new Error(`string contains non-ASCII character "${e[n]}" with code ${r} at position ${n}`); + return r; + }); +} +const gr = (e) => typeof e == "bigint" && Fs <= e; +function cd(e, t, n) { + return gr(e) && gr(t) && gr(n) && t <= e && e < n; +} +function ad(e, t, n, r) { + if (!cd(t, n, r)) + throw new Error("expected valid " + e + ": " + n + " <= n < " + r + ", got " + t); +} +function ud(e) { + let t; + for (t = 0; e > Fs; e >>= Qo, t += 1) + ; + return t; +} +const zs = (e) => (Qo << BigInt(e)) - Qo; +function fd(e, t, n) { + if (Fe(e, "hashLen"), Fe(t, "qByteLen"), typeof n != "function") + throw new Error("hmacFn must be a function"); + const r = (y) => new Uint8Array(y), o = Uint8Array.of(), s = Uint8Array.of(0), i = Uint8Array.of(1), c = 1e3; + let a = r(e), u = r(e), f = 0; + const l = () => { + a.fill(1), u.fill(0), f = 0; + }, d = (...y) => n(u, Xt(a, ...y)), h = (y = o) => { + u = d(s, y), a = d(), y.length !== 0 && (u = d(i, y), a = d()); + }, w = () => { + if (f++ >= c) + throw new Error("drbg: tried max amount of iterations"); + let y = 0; + const S = []; + for (; y < t; ) { + a = d(); + const v = a.slice(); + S.push(v), y += a.length; + } + return Xt(...S); + }; + return (y, S) => { + l(), h(y); + let v; + for (; !(v = S(w())); ) + h(); + return l(), v; + }; +} +function Gs(e, t = {}, n = {}) { + if (!e || typeof e != "object") + throw new Error("expected valid options object"); + function r(s, i, c) { + const a = e[s]; + if (c && a === void 0) + return; + const u = typeof a; + if (u !== i || a === null) + throw new Error(`param "${s}" is invalid: expected ${i}, got ${u}`); + } + const o = (s, i) => Object.entries(s).forEach(([c, a]) => r(c, a, i)); + o(t, !1), o(n, !0); +} +function Gi(e) { + const t = /* @__PURE__ */ new WeakMap(); + return (n, ...r) => { + const o = t.get(n); + if (o !== void 0) + return o; + const s = e(n, ...r); + return t.set(n, s), s; + }; } /*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) */ -const Tc = { +const Da = { p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn, n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n, h: 1n, @@ -5006,104 +5234,104 @@ const Tc = { b: 7n, Gx: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n, Gy: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n -}, { p: ue, n: pe, Gx: wf, Gy: yf, b: vc } = Tc, ut = 32, Ie = 64, ir = { - publicKey: ut + 1, - publicKeyUncompressed: Ie + 1, - signature: Ie, - seed: ut + ut / 2 -}, mf = (...e) => { +}, { p: ve, n: Be, Gx: ld, Gy: dd, b: Ma } = Da, ht = 32, We = 64, Cr = { + publicKey: ht + 1, + publicKeyUncompressed: We + 1, + signature: We, + seed: ht + ht / 2 +}, hd = (...e) => { "captureStackTrace" in Error && typeof Error.captureStackTrace == "function" && Error.captureStackTrace(...e); -}, G = (e = "") => { +}, X = (e = "") => { const t = new Error(e); - throw mf(t, G), t; -}, xf = (e) => typeof e == "bigint", bf = (e) => typeof e == "string", Ef = (e) => e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array", Tt = (e, t, n = "") => { - const r = Ef(e), o = e?.length, s = t !== void 0; + throw hd(t, X), t; +}, pd = (e) => typeof e == "bigint", gd = (e) => typeof e == "string", wd = (e) => e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array", Bt = (e, t, n = "") => { + const r = wd(e), o = e?.length, s = t !== void 0; if (!r || s && o !== t) { const i = n && `"${n}" `, c = s ? ` of length ${t}` : "", a = r ? `length=${o}` : `type=${typeof e}`; - G(i + "expected Uint8Array" + c + ", got " + a); + X(i + "expected Uint8Array" + c + ", got " + a); } return e; -}, ge = (e) => new Uint8Array(e), kc = (e, t) => e.toString(16).padStart(t, "0"), Ac = (e) => Array.from(Tt(e)).map((t) => kc(t, 2)).join(""), Xt = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, zs = (e) => { - if (e >= Xt._0 && e <= Xt._9) - return e - Xt._0; - if (e >= Xt.A && e <= Xt.F) - return e - (Xt.A - 10); - if (e >= Xt.a && e <= Xt.f) - return e - (Xt.a - 10); -}, Ic = (e) => { +}, Oe = (e) => new Uint8Array(e), Ka = (e, t) => e.toString(16).padStart(t, "0"), Fa = (e) => Array.from(Bt(e)).map((t) => Ka(t, 2)).join(""), oe = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, qi = (e) => { + if (e >= oe._0 && e <= oe._9) + return e - oe._0; + if (e >= oe.A && e <= oe.F) + return e - (oe.A - 10); + if (e >= oe.a && e <= oe.f) + return e - (oe.a - 10); +}, Wa = (e) => { const t = "hex invalid"; - if (!bf(e)) - return G(t); + if (!gd(e)) + return X(t); const n = e.length, r = n / 2; if (n % 2) - return G(t); - const o = ge(r); + return X(t); + const o = Oe(r); for (let s = 0, i = 0; s < r; s++, i += 2) { - const c = zs(e.charCodeAt(i)), a = zs(e.charCodeAt(i + 1)); + const c = qi(e.charCodeAt(i)), a = qi(e.charCodeAt(i + 1)); if (c === void 0 || a === void 0) - return G(t); + return X(t); o[s] = c * 16 + a; } return o; -}, Bc = () => globalThis?.crypto, js = () => Bc()?.subtle ?? G("crypto.subtle must be defined, consider polyfill"), jt = (...e) => { - const t = ge(e.reduce((r, o) => r + Tt(o).length, 0)); +}, za = () => globalThis?.crypto, ji = () => za()?.subtle ?? X("crypto.subtle must be defined, consider polyfill"), te = (...e) => { + const t = Oe(e.reduce((r, o) => r + Bt(o).length, 0)); let n = 0; return e.forEach((r) => { t.set(r, n), n += r.length; }), t; -}, Ir = (e = ut) => Bc().getRandomValues(ge(e)), wn = BigInt, Be = (e, t, n, r = "bad number: out of range") => xf(e) && t <= e && e < n ? e : G(r), I = (e, t = ue) => { +}, co = (e = ht) => za().getRandomValues(Oe(e)), Vn = BigInt, ze = (e, t, n, r = "bad number: out of range") => pd(e) && t <= e && e < n ? e : X(r), P = (e, t = ve) => { const n = e % t; return n >= 0n ? n : t + n; -}, Jt = (e) => I(e, pe), Oc = (e, t) => { - (e === 0n || t <= 0n) && G("no inverse n=" + e + " mod=" + t); - let n = I(e, t), r = t, o = 0n, s = 1n; +}, ie = (e) => P(e, Be), Ga = (e, t) => { + (e === 0n || t <= 0n) && X("no inverse n=" + e + " mod=" + t); + let n = P(e, t), r = t, o = 0n, s = 1n; for (; n !== 0n; ) { const i = r / n, c = r % n, a = o - s * i; r = n, n = c, o = s, s = a; } - return r === 1n ? I(o, t) : G("no inverse"); -}, Zo = (e) => { - const t = Xo[e]; - return typeof t != "function" && G("hashes." + e + " not set"), t; -}, Hr = (e) => e instanceof wt ? e : G("Point expected"), Uc = (e) => I(I(e * e) * e + vc), qs = (e) => Be(e, 0n, ue), Gn = (e) => Be(e, 1n, ue), fo = (e) => Be(e, 1n, pe), Ge = (e) => (e & 1n) === 0n, Br = (e) => Uint8Array.of(e), Sf = (e) => Br(Ge(e) ? 2 : 3), Nc = (e) => { - const t = Uc(Gn(e)); + return r === 1n ? P(o, t) : X("no inverse"); +}, qs = (e) => { + const t = js[e]; + return typeof t != "function" && X("hashes." + e + " not set"), t; +}, To = (e) => e instanceof Et ? e : X("Point expected"), qa = (e) => P(P(e * e) * e + Ma), Yi = (e) => ze(e, 0n, ve), wr = (e) => ze(e, 1n, ve), Jo = (e) => ze(e, 1n, Be), fn = (e) => (e & 1n) === 0n, ao = (e) => Uint8Array.of(e), yd = (e) => ao(fn(e) ? 2 : 3), ja = (e) => { + const t = qa(wr(e)); let n = 1n; - for (let r = t, o = (ue + 1n) / 4n; o > 0n; o >>= 1n) - o & 1n && (n = n * r % ue), r = r * r % ue; - return I(n * n) === t ? n : G("sqrt invalid"); + for (let r = t, o = (ve + 1n) / 4n; o > 0n; o >>= 1n) + o & 1n && (n = n * r % ve), r = r * r % ve; + return P(n * n) === t ? n : X("sqrt invalid"); }; -class wt { +class Et { static BASE; static ZERO; X; Y; Z; constructor(t, n, r) { - this.X = qs(t), this.Y = Gn(n), this.Z = qs(r), Object.freeze(this); + this.X = Yi(t), this.Y = wr(n), this.Z = Yi(r), Object.freeze(this); } static CURVE() { - return Tc; + return Da; } /** Create 3d xyz point from 2d xy. (0, 0) => (0, 1, 0), not (0, 0, 1) */ static fromAffine(t) { const { x: n, y: r } = t; - return n === 0n && r === 0n ? be : new wt(n, r, 1n); + return n === 0n && r === 0n ? Le : new Et(n, r, 1n); } /** Convert Uint8Array or hex string to Point. */ static fromBytes(t) { - Tt(t); - const { publicKey: n, publicKeyUncompressed: r } = ir; + Bt(t); + const { publicKey: n, publicKeyUncompressed: r } = Cr; let o; - const s = t.length, i = t[0], c = t.subarray(1), a = ze(c, 0, ut); + const s = t.length, i = t[0], c = t.subarray(1), a = ln(c, 0, ht); if (s === n && (i === 2 || i === 3)) { - let u = Nc(a); - const f = Ge(u); - Ge(wn(i)) !== f && (u = I(-u)), o = new wt(a, u, 1n); + let u = ja(a); + const f = fn(u); + fn(Vn(i)) !== f && (u = P(-u)), o = new Et(a, u, 1n); } - return s === r && i === 4 && (o = new wt(a, ze(c, ut, Ie), 1n)), o ? o.assertValidity() : G("bad point: not on curve"); + return s === r && i === 4 && (o = new Et(a, ln(c, ht, We), 1n)), o ? o.assertValidity() : X("bad point: not on curve"); } static fromHex(t) { - return wt.fromBytes(Ic(t)); + return Et.fromBytes(Wa(t)); } get x() { return this.toAffine().x; @@ -5113,15 +5341,15 @@ class wt { } /** Equality check: compare points P&Q. */ equals(t) { - const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = Hr(t), a = I(n * c), u = I(s * o), f = I(r * c), l = I(i * o); + const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = To(t), a = P(n * c), u = P(s * o), f = P(r * c), l = P(i * o); return a === u && f === l; } is0() { - return this.equals(be); + return this.equals(Le); } /** Flip point over y coordinate. */ negate() { - return new wt(this.X, I(-this.Y), this.Z); + return new Et(this.X, P(-this.Y), this.Z); } /** Point doubling: P+P, complete formula. */ double() { @@ -5134,16 +5362,16 @@ class wt { */ // prettier-ignore add(t) { - const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = Hr(t), a = 0n, u = vc; + const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = To(t), a = 0n, u = Ma; let f = 0n, l = 0n, d = 0n; - const h = I(u * 3n); - let w = I(n * s), p = I(r * i), m = I(o * c), x = I(n + r), b = I(s + i); - x = I(x * b), b = I(w + p), x = I(x - b), b = I(n + o); - let v = I(s + c); - return b = I(b * v), v = I(w + m), b = I(b - v), v = I(r + o), f = I(i + c), v = I(v * f), f = I(p + m), v = I(v - f), d = I(a * b), f = I(h * m), d = I(f + d), f = I(p - d), d = I(p + d), l = I(f * d), p = I(w + w), p = I(p + w), m = I(a * m), b = I(h * b), p = I(p + m), m = I(w - m), m = I(a * m), b = I(b + m), w = I(p * b), l = I(l + w), w = I(v * b), f = I(x * f), f = I(f - w), w = I(x * p), d = I(v * d), d = I(d + w), new wt(f, l, d); + const h = P(u * 3n); + let w = P(n * s), g = P(r * i), y = P(o * c), S = P(n + r), v = P(s + i); + S = P(S * v), v = P(w + g), S = P(S - v), v = P(n + o); + let O = P(s + c); + return v = P(v * O), O = P(w + y), v = P(v - O), O = P(r + o), f = P(i + c), O = P(O * f), f = P(g + y), O = P(O - f), d = P(a * v), f = P(h * y), d = P(f + d), f = P(g - d), d = P(g + d), l = P(f * d), g = P(w + w), g = P(g + w), y = P(a * y), v = P(h * v), g = P(g + y), y = P(w - y), y = P(a * y), v = P(v + y), w = P(g * v), l = P(l + w), w = P(O * v), f = P(S * f), f = P(f - w), w = P(S * g), d = P(O * d), d = P(d + w), new Et(f, l, d); } subtract(t) { - return this.add(Hr(t).negate()); + return this.add(To(t).negate()); } /** * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n. @@ -5154,12 +5382,12 @@ class wt { */ multiply(t, n = !0) { if (!n && t === 0n) - return be; - if (fo(t), t === 1n) + return Le; + if (Jo(t), t === 1n) return this; - if (this.equals(we)) - return qf(t).p; - let r = be, o = we; + if (this.equals($e)) + return Fd(t).p; + let r = Le, o = $e; for (let s = this; t > 0n; s = s.double(), t >>= 1n) t & 1n ? r = r.add(s) : n && (o = o.add(s)); return r; @@ -5170,262 +5398,1673 @@ class wt { /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */ toAffine() { const { X: t, Y: n, Z: r } = this; - if (this.equals(be)) + if (this.equals(Le)) return { x: 0n, y: 0n }; if (r === 1n) return { x: t, y: n }; - const o = Oc(r, ue); - return I(r * o) !== 1n && G("inverse invalid"), { x: I(t * o), y: I(n * o) }; + const o = Ga(r, ve); + return P(r * o) !== 1n && X("inverse invalid"), { x: P(t * o), y: P(n * o) }; } /** Checks if the point is valid and on-curve. */ assertValidity() { const { x: t, y: n } = this.toAffine(); - return Gn(t), Gn(n), I(n * n) === Uc(t) ? this : G("bad point: not on curve"); + return wr(t), wr(n), P(n * n) === qa(t) ? this : X("bad point: not on curve"); } /** Converts point to 33/65-byte Uint8Array. */ toBytes(t = !0) { - const { x: n, y: r } = this.assertValidity().toAffine(), o = At(n); - return t ? jt(Sf(r), o) : jt(Br(4), o, At(r)); + const { x: n, y: r } = this.assertValidity().toAffine(), o = Ut(n); + return t ? te(yd(r), o) : te(ao(4), o, Ut(r)); } toHex(t) { - return Ac(this.toBytes(t)); - } -} -const we = new wt(wf, yf, 1n), be = new wt(0n, 1n, 0n); -wt.BASE = we; -wt.ZERO = be; -const Tf = (e, t, n) => we.multiply(t, !1).add(e.multiply(n, !1)).assertValidity(), ye = (e) => wn("0x" + (Ac(e) || "0")), ze = (e, t, n) => ye(e.subarray(t, n)), vf = 2n ** 256n, At = (e) => Ic(kc(Be(e, 0n, vf), Ie)), Rc = (e) => { - const t = ye(Tt(e, ut, "secret key")); - return Be(t, 1n, pe, "invalid secret key: outside of range"); -}, Cc = (e) => e > pe >> 1n, kf = (e) => { - [0, 1, 2, 3].includes(e) || G("recovery id must be valid and present"); -}, Af = (e) => { - e != null && !Ys.includes(e) && G(`Signature format must be one of: ${Ys.join(", ")}`), e === Lc && G('Signature format "der" is not supported: switch to noble-curves'); -}, If = (e, t = je) => { - Af(t); - const n = ir.signature, r = n + 1; + return Fa(this.toBytes(t)); + } +} +const $e = new Et(ld, dd, 1n), Le = new Et(0n, 1n, 0n); +Et.BASE = $e; +Et.ZERO = Le; +const md = (e, t, n) => $e.multiply(t, !1).add(e.multiply(n, !1)).assertValidity(), Ue = (e) => Vn("0x" + (Fa(e) || "0")), ln = (e, t, n) => Ue(e.subarray(t, n)), bd = 2n ** 256n, Ut = (e) => Wa(Ka(ze(e, 0n, bd), We)), Ya = (e) => { + const t = Ue(Bt(e, ht, "secret key")); + return ze(t, 1n, Be, "invalid secret key: outside of range"); +}, Za = (e) => e > Be >> 1n, Ed = (e) => { + [0, 1, 2, 3].includes(e) || X("recovery id must be valid and present"); +}, xd = (e) => { + e != null && !Zi.includes(e) && X(`Signature format must be one of: ${Zi.join(", ")}`), e === Qa && X('Signature format "der" is not supported: switch to noble-curves'); +}, Sd = (e, t = dn) => { + xd(t); + const n = Cr.signature, r = n + 1; let o = `Signature format "${t}" expects Uint8Array with length `; - t === je && e.length !== n && G(o + n), t === ar && e.length !== r && G(o + r); + t === dn && e.length !== n && X(o + n), t === Pr && e.length !== r && X(o + r); }; -class cr { +class _r { r; s; recovery; constructor(t, n, r) { - this.r = fo(t), this.s = fo(n), r != null && (this.recovery = r), Object.freeze(this); + this.r = Jo(t), this.s = Jo(n), r != null && (this.recovery = r), Object.freeze(this); } - static fromBytes(t, n = je) { - If(t, n); + static fromBytes(t, n = dn) { + Sd(t, n); let r; - n === ar && (r = t[0], t = t.subarray(1)); - const o = ze(t, 0, ut), s = ze(t, ut, Ie); - return new cr(o, s, r); + n === Pr && (r = t[0], t = t.subarray(1)); + const o = ln(t, 0, ht), s = ln(t, ht, We); + return new _r(o, s, r); } addRecoveryBit(t) { - return new cr(this.r, this.s, t); + return new _r(this.r, this.s, t); } hasHighS() { - return Cc(this.s); + return Za(this.s); } - toBytes(t = je) { - const { r: n, s: r, recovery: o } = this, s = jt(At(n), At(r)); - return t === ar ? (kf(o), jt(Uint8Array.of(o), s)) : s; + toBytes(t = dn) { + const { r: n, s: r, recovery: o } = this, s = te(Ut(n), Ut(r)); + return t === Pr ? (Ed(o), te(Uint8Array.of(o), s)) : s; } } -const $c = (e) => { +const Xa = (e) => { const t = e.length * 8 - 256; - t > 1024 && G("msg invalid"); - const n = ye(e); - return t > 0 ? n >> wn(t) : n; -}, Bf = (e) => Jt($c(Tt(e))), je = "compact", ar = "recovered", Lc = "der", Ys = [je, ar, Lc], Zs = { + t > 1024 && X("msg invalid"); + const n = Ue(e); + return t > 0 ? n >> Vn(t) : n; +}, vd = (e) => ie(Xa(Bt(e))), dn = "compact", Pr = "recovered", Qa = "der", Zi = [dn, Pr, Qa], Xi = { lowS: !0, prehash: !0, - format: je, + format: dn, extraEntropy: !1 -}, Xs = "SHA-256", Xo = { +}, Qi = "SHA-256", js = { hmacSha256Async: async (e, t) => { - const n = js(), r = "HMAC", o = await n.importKey("raw", e, { name: r, hash: { name: Xs } }, !1, ["sign"]); - return ge(await n.sign(r, o, t)); + const n = ji(), r = "HMAC", o = await n.importKey("raw", e, { name: r, hash: { name: Qi } }, !1, ["sign"]); + return Oe(await n.sign(r, o, t)); }, hmacSha256: void 0, - sha256Async: async (e) => ge(await js().digest(Xs, e)), + sha256Async: async (e) => Oe(await ji().digest(Qi, e)), sha256: void 0 -}, Of = (e, t, n) => (Tt(e, void 0, "message"), t.prehash ? n ? Xo.sha256Async(e) : Zo("sha256")(e) : e), Uf = ge(0), Nf = Br(0), Rf = Br(1), Cf = 1e3, $f = "drbg: tried max amount of iterations", Lf = (e, t) => { - let n = ge(ut), r = ge(ut), o = 0; +}, Td = (e, t, n) => (Bt(e, void 0, "message"), t.prehash ? n ? js.sha256Async(e) : qs("sha256")(e) : e), Ad = Oe(0), Id = ao(0), kd = ao(1), Bd = 1e3, Od = "drbg: tried max amount of iterations", $d = (e, t) => { + let n = Oe(ht), r = Oe(ht), o = 0; const s = () => { n.fill(1), r.fill(0); - }, i = (...f) => Zo("hmacSha256")(r, jt(n, ...f)), c = (f = Uf) => { - r = i(Nf, f), n = i(), f.length !== 0 && (r = i(Rf, f), n = i()); - }, a = () => (o++ >= Cf && G($f), n = i(), n); + }, i = (...f) => qs("hmacSha256")(r, te(n, ...f)), c = (f = Ad) => { + r = i(Id, f), n = i(), f.length !== 0 && (r = i(kd, f), n = i()); + }, a = () => (o++ >= Bd && X(Od), n = i(), n); s(), c(e); let u; for (; !(u = t(a())); ) c(); return s(), u; -}, Pf = (e, t, n, r) => { +}, Ud = (e, t, n, r) => { let { lowS: o, extraEntropy: s } = n; - const i = At, c = Bf(e), a = i(c), u = Rc(t), f = [i(u), a]; + const i = Ut, c = vd(e), a = i(c), u = Ya(t), f = [i(u), a]; if (s != null && s !== !1) { - const w = s === !0 ? Ir(ut) : s; - f.push(Tt(w, void 0, "extraEntropy")); + const w = s === !0 ? co(ht) : s; + f.push(Bt(w, void 0, "extraEntropy")); } - const l = jt(...f), d = c; + const l = te(...f), d = c; return r(l, (w) => { - const p = $c(w); - if (!(1n <= p && p < pe)) + const g = Xa(w); + if (!(1n <= g && g < Be)) return; - const m = Oc(p, pe), x = we.multiply(p).toAffine(), b = Jt(x.x); - if (b === 0n) - return; - const v = Jt(m * Jt(d + b * u)); + const y = Ga(g, Be), S = $e.multiply(g).toAffine(), v = ie(S.x); if (v === 0n) return; - let O = (x.x === b ? 0 : 2) | Number(x.y & 1n), A = v; - return o && Cc(v) && (A = Jt(-v), O ^= 1), new cr(b, A, O).toBytes(n.format); + const O = ie(y * ie(d + v * u)); + if (O === 0n) + return; + let N = (S.x === v ? 0 : 2) | Number(S.y & 1n), U = O; + return o && Za(O) && (U = ie(-O), N ^= 1), new _r(v, U, N).toBytes(n.format); }); -}, _f = (e) => { +}, Rd = (e) => { const t = {}; - return Object.keys(Zs).forEach((n) => { - t[n] = e[n] ?? Zs[n]; + return Object.keys(Xi).forEach((n) => { + t[n] = e[n] ?? Xi[n]; }), t; -}, Vf = (e, t, n = {}) => (n = _f(n), e = Of(e, n, !1), Pf(e, t, n, Lf)), Df = (e = Ir(ir.seed)) => { - Tt(e), (e.length < ir.seed || e.length > 1024) && G("expected 40-1024b"); - const t = I(ye(e), pe - 1n); - return At(t + 1n); -}, Hf = (e) => (t) => { - const n = Df(t); +}, Nd = (e, t, n = {}) => (n = Rd(n), e = Td(e, n, !1), Ud(e, t, n, $d)), Ld = (e = co(Cr.seed)) => { + Bt(e), (e.length < Cr.seed || e.length > 1024) && X("expected 40-1024b"); + const t = P(Ue(e), Be - 1n); + return Ut(t + 1n); +}, Cd = (e) => (t) => { + const n = Ld(t); return { secretKey: n, publicKey: e(n) }; -}, Pc = (e) => Uint8Array.from("BIP0340/" + e, (t) => t.charCodeAt(0)), _c = "aux", Vc = "nonce", Dc = "challenge", lo = (e, ...t) => { - const n = Zo("sha256"), r = n(Pc(e)); - return n(jt(r, r, ...t)); -}, ho = async (e, ...t) => { - const n = Xo.sha256Async, r = await n(Pc(e)); - return await n(jt(r, r, ...t)); -}, Qo = (e) => { - const t = Rc(e), n = we.multiply(t), { x: r, y: o } = n.assertValidity().toAffine(), s = Ge(o) ? t : Jt(-t), i = At(r); +}, Ja = (e) => Uint8Array.from("BIP0340/" + e, (t) => t.charCodeAt(0)), tu = "aux", eu = "nonce", nu = "challenge", ts = (e, ...t) => { + const n = qs("sha256"), r = n(Ja(e)); + return n(te(r, r, ...t)); +}, es = async (e, ...t) => { + const n = js.sha256Async, r = await n(Ja(e)); + return await n(te(r, r, ...t)); +}, Ys = (e) => { + const t = Ya(e), n = $e.multiply(t), { x: r, y: o } = n.assertValidity().toAffine(), s = fn(o) ? t : ie(-t), i = Ut(r); return { d: s, px: i }; -}, Jo = (e) => Jt(ye(e)), Hc = (...e) => Jo(lo(Dc, ...e)), Mc = async (...e) => Jo(await ho(Dc, ...e)), Kc = (e) => Qo(e).px, Mf = Hf(Kc), Fc = (e, t, n) => { - const { px: r, d: o } = Qo(t); - return { m: Tt(e), px: r, d: o, a: Tt(n, ut) }; -}, Wc = (e) => { - const t = Jo(e); - t === 0n && G("sign failed: k is zero"); - const { px: n, d: r } = Qo(At(t)); +}, Zs = (e) => ie(Ue(e)), ru = (...e) => Zs(ts(nu, ...e)), ou = async (...e) => Zs(await es(nu, ...e)), su = (e) => Ys(e).px, _d = Cd(su), iu = (e, t, n) => { + const { px: r, d: o } = Ys(t); + return { m: Bt(e), px: r, d: o, a: Bt(n, ht) }; +}, cu = (e) => { + const t = Zs(e); + t === 0n && X("sign failed: k is zero"); + const { px: n, d: r } = Ys(Ut(t)); return { rx: n, k: r }; -}, Gc = (e, t, n, r) => jt(t, At(Jt(e + n * r))), zc = "invalid signature produced", Kf = (e, t, n = Ir(ut)) => { - const { m: r, px: o, d: s, a: i } = Fc(e, t, n), c = lo(_c, i), a = At(s ^ ye(c)), u = lo(Vc, a, o, r), { rx: f, k: l } = Wc(u), d = Hc(f, o, r), h = Gc(l, f, d, s); - return qc(h, r, o) || G(zc), h; -}, Ff = async (e, t, n = Ir(ut)) => { - const { m: r, px: o, d: s, a: i } = Fc(e, t, n), c = await ho(_c, i), a = At(s ^ ye(c)), u = await ho(Vc, a, o, r), { rx: f, k: l } = Wc(u), d = await Mc(f, o, r), h = Gc(l, f, d, s); - return await Yc(h, r, o) || G(zc), h; -}, Wf = (e, t) => e instanceof Promise ? e.then(t) : t(e), jc = (e, t, n, r) => { - const o = Tt(e, Ie, "signature"), s = Tt(t, void 0, "message"), i = Tt(n, ut, "publicKey"); +}, au = (e, t, n, r) => te(t, Ut(ie(e + n * r))), uu = "invalid signature produced", Pd = (e, t, n = co(ht)) => { + const { m: r, px: o, d: s, a: i } = iu(e, t, n), c = ts(tu, i), a = Ut(s ^ Ue(c)), u = ts(eu, a, o, r), { rx: f, k: l } = cu(u), d = ru(f, o, r), h = au(l, f, d, s); + return lu(h, r, o) || X(uu), h; +}, Hd = async (e, t, n = co(ht)) => { + const { m: r, px: o, d: s, a: i } = iu(e, t, n), c = await es(tu, i), a = Ut(s ^ Ue(c)), u = await es(eu, a, o, r), { rx: f, k: l } = cu(u), d = await ou(f, o, r), h = au(l, f, d, s); + return await du(h, r, o) || X(uu), h; +}, Vd = (e, t) => e instanceof Promise ? e.then(t) : t(e), fu = (e, t, n, r) => { + const o = Bt(e, We, "signature"), s = Bt(t, void 0, "message"), i = Bt(n, ht, "publicKey"); try { - const c = ye(i), a = Nc(c), u = Ge(a) ? a : I(-a), f = new wt(c, u, 1n).assertValidity(), l = At(f.toAffine().x), d = ze(o, 0, ut); - Be(d, 1n, ue); - const h = ze(o, ut, Ie); - Be(h, 1n, pe); - const w = jt(At(d), l, s); - return Wf(r(w), (p) => { - const { x: m, y: x } = Tf(f, h, Jt(-p)).toAffine(); - return !(!Ge(x) || m !== d); + const c = Ue(i), a = ja(c), u = fn(a) ? a : P(-a), f = new Et(c, u, 1n).assertValidity(), l = Ut(f.toAffine().x), d = ln(o, 0, ht); + ze(d, 1n, ve); + const h = ln(o, ht, We); + ze(h, 1n, Be); + const w = te(Ut(d), l, s); + return Vd(r(w), (g) => { + const { x: y, y: S } = md(f, h, ie(-g)).toAffine(); + return !(!fn(S) || y !== d); }); } catch { return !1; } -}, qc = (e, t, n) => jc(e, t, n, Hc), Yc = async (e, t, n) => jc(e, t, n, Mc), Gf = { - keygen: Mf, - getPublicKey: Kc, - sign: Kf, - verify: qc, - signAsync: Ff, - verifyAsync: Yc -}, ur = 8, zf = 256, Zc = Math.ceil(zf / ur) + 1, po = 2 ** (ur - 1), jf = () => { +}, lu = (e, t, n) => fu(e, t, n, ru), du = async (e, t, n) => fu(e, t, n, ou), Dd = { + keygen: _d, + getPublicKey: su, + sign: Pd, + verify: lu, + signAsync: Hd, + verifyAsync: du +}, Hr = 8, Md = 256, hu = Math.ceil(Md / Hr) + 1, ns = 2 ** (Hr - 1), Kd = () => { const e = []; - let t = we, n = t; - for (let r = 0; r < Zc; r++) { + let t = $e, n = t; + for (let r = 0; r < hu; r++) { n = t, e.push(n); - for (let o = 1; o < po; o++) + for (let o = 1; o < ns; o++) n = n.add(t), e.push(n); t = n.double(); } return e; }; -let Qs; -const Js = (e, t) => { +let Ji; +const tc = (e, t) => { const n = t.negate(); return e ? n : t; -}, qf = (e) => { - const t = Qs || (Qs = jf()); - let n = be, r = we; - const o = 2 ** ur, s = o, i = wn(o - 1), c = wn(ur); - for (let a = 0; a < Zc; a++) { +}, Fd = (e) => { + const t = Ji || (Ji = Kd()); + let n = Le, r = $e; + const o = 2 ** Hr, s = o, i = Vn(o - 1), c = Vn(Hr); + for (let a = 0; a < hu; a++) { let u = Number(e & i); - e >>= c, u > po && (u -= s, e += 1n); - const f = a * po, l = f, d = f + Math.abs(u) - 1, h = a % 2 !== 0, w = u < 0; - u === 0 ? r = r.add(Js(h, t[l])) : n = n.add(Js(w, t[d])); + e >>= c, u > ns && (u -= s, e += 1n); + const f = a * ns, l = f, d = f + Math.abs(u) - 1, h = a % 2 !== 0, w = u < 0; + u === 0 ? r = r.add(tc(h, t[l])) : n = n.add(tc(w, t[d])); } - return e !== 0n && G("invalid wnaf"), { p: n, f: r }; + return e !== 0n && X("invalid wnaf"), { p: n, f: r }; }; -function ts(e, t, n = {}) { - e = ao(e); - const { aggPublicKey: r } = uo(e); - if (!n.taprootTweak) - return { - preTweakedKey: r.toBytes(!0), - finalKey: r.toBytes(!0) - }; - const o = Nt.utils.taggedHash("TapTweak", r.toBytes(!0).subarray(1), n.taprootTweak ?? new Uint8Array(0)), { aggPublicKey: s } = uo(e, [o], [!0]); - return { - preTweakedKey: r.toBytes(!0), - finalKey: s.toBytes(!0) - }; +function Wd(e, t, n) { + return e & t ^ ~e & n; } -class Ln extends Error { - constructor(t) { - super(t), this.name = "PartialSignatureError"; - } +function zd(e, t, n) { + return e & t ^ e & n ^ t & n; } -class es { - constructor(t, n) { - if (this.s = t, this.R = n, t.length !== 32) - throw new Ln("Invalid s length"); - if (n.length !== 33) - throw new Ln("Invalid R length"); +class Gd { + blockLen; + outputLen; + padOffset; + isLE; + // For partial updates less than block size + buffer; + view; + finished = !1; + length = 0; + pos = 0; + destroyed = !1; + constructor(t, n, r, o) { + this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = vo(this.buffer); } - /** - * Encodes the partial signature into bytes - * Returns a 32-byte array containing just the s value - */ - encode() { - return new Uint8Array(this.s); + update(t) { + Ur(this), et(t); + const { view: n, buffer: r, blockLen: o } = this, s = t.length; + for (let i = 0; i < s; ) { + const c = Math.min(o - this.pos, s - i); + if (c === o) { + const a = vo(t); + for (; o <= s - i; i += o) + this.process(a, i); + continue; + } + r.set(t.subarray(i, i + c), this.pos), this.pos += c, i += c, this.pos === o && (this.process(n, 0), this.pos = 0); + } + return this.length += t.length, this.roundClean(), this; } - /** - * Decodes a partial signature from bytes - * @param bytes - 32-byte array containing s value - */ - static decode(t) { - if (t.length !== 32) - throw new Ln("Invalid partial signature length"); - if (qt(t) >= wt.CURVE().n) - throw new Ln("s value overflows curve order"); - const r = new Uint8Array(33); - return new es(t, r); + digestInto(t) { + Ur(this), ed(t, this), this.finished = !0; + const { buffer: n, view: r, blockLen: o, isLE: s } = this; + let { pos: i } = this; + n[i++] = 128, Rr(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(r, 0), i = 0); + for (let l = i; l < o; l++) + n[l] = 0; + r.setBigUint64(o - 8, BigInt(this.length * 8), s), this.process(r, 0); + const c = vo(t), a = this.outputLen; + if (a % 4) + throw new Error("_sha2: outputLen must be aligned to 32bit"); + const u = a / 4, f = this.get(); + if (u > f.length) + throw new Error("_sha2: outputLen bigger than state"); + for (let l = 0; l < u; l++) + c.setUint32(4 * l, f[l], s); } -} -function Yf(e, t, n, r, o, s) { - let i; - if (s?.taprootTweak !== void 0) { - const { preTweakedKey: u } = ts(ao(r)); - i = Nt.utils.taggedHash("TapTweak", u.subarray(1), s.taprootTweak); + digest() { + const { buffer: t, outputLen: n } = this; + this.digestInto(t); + const r = t.slice(0, n); + return this.destroy(), r; + } + _cloneInto(t) { + t ||= new this.constructor(), t.set(...this.get()); + const { blockLen: n, buffer: r, length: o, finished: s, destroyed: i, pos: c } = this; + return t.destroyed = i, t.finished = s, t.length = o, t.pos = c, o % n && t.buffer.set(r), t; + } + clone() { + return this._cloneInto(); + } +} +const we = /* @__PURE__ */ Uint32Array.from([ + 1779033703, + 3144134277, + 1013904242, + 2773480762, + 1359893119, + 2600822924, + 528734635, + 1541459225 +]), qd = /* @__PURE__ */ Uint32Array.from([ + 1116352408, + 1899447441, + 3049323471, + 3921009573, + 961987163, + 1508970993, + 2453635748, + 2870763221, + 3624381080, + 310598401, + 607225278, + 1426881987, + 1925078388, + 2162078206, + 2614888103, + 3248222580, + 3835390401, + 4022224774, + 264347078, + 604807628, + 770255983, + 1249150122, + 1555081692, + 1996064986, + 2554220882, + 2821834349, + 2952996808, + 3210313671, + 3336571891, + 3584528711, + 113926993, + 338241895, + 666307205, + 773529912, + 1294757372, + 1396182291, + 1695183700, + 1986661051, + 2177026350, + 2456956037, + 2730485921, + 2820302411, + 3259730800, + 3345764771, + 3516065817, + 3600352804, + 4094571909, + 275423344, + 430227734, + 506948616, + 659060556, + 883997877, + 958139571, + 1322822218, + 1537002063, + 1747873779, + 1955562222, + 2024104815, + 2227730452, + 2361852424, + 2428436474, + 2756734187, + 3204031479, + 3329325298 +]), ye = /* @__PURE__ */ new Uint32Array(64); +class jd extends Gd { + constructor(t) { + super(64, t, 8, !1); + } + get() { + const { A: t, B: n, C: r, D: o, E: s, F: i, G: c, H: a } = this; + return [t, n, r, o, s, i, c, a]; + } + // prettier-ignore + set(t, n, r, o, s, i, c, a) { + this.A = t | 0, this.B = n | 0, this.C = r | 0, this.D = o | 0, this.E = s | 0, this.F = i | 0, this.G = c | 0, this.H = a | 0; + } + process(t, n) { + for (let l = 0; l < 16; l++, n += 4) + ye[l] = t.getUint32(n, !1); + for (let l = 16; l < 64; l++) { + const d = ye[l - 15], h = ye[l - 2], w = jt(d, 7) ^ jt(d, 18) ^ d >>> 3, g = jt(h, 17) ^ jt(h, 19) ^ h >>> 10; + ye[l] = g + ye[l - 7] + w + ye[l - 16] | 0; + } + let { A: r, B: o, C: s, D: i, E: c, F: a, G: u, H: f } = this; + for (let l = 0; l < 64; l++) { + const d = jt(c, 6) ^ jt(c, 11) ^ jt(c, 25), h = f + d + Wd(c, a, u) + qd[l] + ye[l] | 0, g = (jt(r, 2) ^ jt(r, 13) ^ jt(r, 22)) + zd(r, o, s) | 0; + f = u, u = a, a = c, c = i + h | 0, i = s, s = o, o = r, r = h + g | 0; + } + r = r + this.A | 0, o = o + this.B | 0, s = s + this.C | 0, i = i + this.D | 0, c = c + this.E | 0, a = a + this.F | 0, u = u + this.G | 0, f = f + this.H | 0, this.set(r, o, s, i, c, a, u, f); + } + roundClean() { + Rr(ye); + } + destroy() { + this.set(0, 0, 0, 0, 0, 0, 0, 0), Rr(this.buffer); + } +} +class Yd extends jd { + // We cannot use array here since array allows indexing by variable + // which means optimizer/compiler cannot use registers. + A = we[0] | 0; + B = we[1] | 0; + C = we[2] | 0; + D = we[3] | 0; + E = we[4] | 0; + F = we[5] | 0; + G = we[6] | 0; + H = we[7] | 0; + constructor() { + super(32); + } +} +const rs = /* @__PURE__ */ rd( + () => new Yd(), + /* @__PURE__ */ od(1) +); +/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ +const St = /* @__PURE__ */ BigInt(0), bt = /* @__PURE__ */ BigInt(1), Ve = /* @__PURE__ */ BigInt(2), pu = /* @__PURE__ */ BigInt(3), gu = /* @__PURE__ */ BigInt(4), wu = /* @__PURE__ */ BigInt(5), Zd = /* @__PURE__ */ BigInt(7), yu = /* @__PURE__ */ BigInt(8), Xd = /* @__PURE__ */ BigInt(9), mu = /* @__PURE__ */ BigInt(16); +function Dt(e, t) { + const n = e % t; + return n >= St ? n : t + n; +} +function Nt(e, t, n) { + let r = e; + for (; t-- > St; ) + r *= r, r %= n; + return r; +} +function ec(e, t) { + if (e === St) + throw new Error("invert: expected non-zero number"); + if (t <= St) + throw new Error("invert: expected positive modulus, got " + t); + let n = Dt(e, t), r = t, o = St, s = bt; + for (; n !== St; ) { + const c = r / n, a = r % n, u = o - s * c; + r = n, n = a, o = s, s = u; + } + if (r !== bt) + throw new Error("invert: does not exist"); + return Dt(o, t); +} +function Xs(e, t, n) { + if (!e.eql(e.sqr(t), n)) + throw new Error("Cannot find square root"); +} +function bu(e, t) { + const n = (e.ORDER + bt) / gu, r = e.pow(t, n); + return Xs(e, r, t), r; +} +function Qd(e, t) { + const n = (e.ORDER - wu) / yu, r = e.mul(t, Ve), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, Ve), o), c = e.mul(s, e.sub(i, e.ONE)); + return Xs(e, c, t), c; +} +function Jd(e) { + const t = uo(e), n = Eu(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + Zd) / mu; + return (c, a) => { + let u = c.pow(a, i), f = c.mul(u, r); + const l = c.mul(u, o), d = c.mul(u, s), h = c.eql(c.sqr(f), a), w = c.eql(c.sqr(l), a); + u = c.cmov(u, f, h), f = c.cmov(d, l, w); + const g = c.eql(c.sqr(f), a), y = c.cmov(u, f, g); + return Xs(c, y, a), y; + }; +} +function Eu(e) { + if (e < pu) + throw new Error("sqrt is not defined for small field"); + let t = e - bt, n = 0; + for (; t % Ve === St; ) + t /= Ve, n++; + let r = Ve; + const o = uo(e); + for (; nc(o, r) === 1; ) + if (r++ > 1e3) + throw new Error("Cannot find square root: probably non-prime P"); + if (n === 1) + return bu; + let s = o.pow(r, t); + const i = (t + bt) / Ve; + return function(a, u) { + if (a.is0(u)) + return u; + if (nc(a, u) !== 1) + throw new Error("Cannot find square root"); + let f = n, l = a.mul(a.ONE, s), d = a.pow(u, t), h = a.pow(u, i); + for (; !a.eql(d, a.ONE); ) { + if (a.is0(d)) + return a.ZERO; + let w = 1, g = a.sqr(d); + for (; !a.eql(g, a.ONE); ) + if (w++, g = a.sqr(g), w === f) + throw new Error("Cannot find square root"); + const y = bt << BigInt(f - w - 1), S = a.pow(l, y); + f = w, l = a.sqr(S), d = a.mul(d, l), h = a.mul(h, S); + } + return h; + }; +} +function th(e) { + return e % gu === pu ? bu : e % yu === wu ? Qd : e % mu === Xd ? Jd(e) : Eu(e); +} +const eh = [ + "create", + "isValid", + "is0", + "neg", + "inv", + "sqrt", + "sqr", + "eql", + "add", + "sub", + "mul", + "pow", + "div", + "addN", + "subN", + "mulN", + "sqrN" +]; +function nh(e) { + const t = { + ORDER: "bigint", + BYTES: "number", + BITS: "number" + }, n = eh.reduce((r, o) => (r[o] = "function", r), t); + return Gs(e, n), e; +} +function rh(e, t, n) { + if (n < St) + throw new Error("invalid exponent, negatives unsupported"); + if (n === St) + return e.ONE; + if (n === bt) + return t; + let r = e.ONE, o = t; + for (; n > St; ) + n & bt && (r = e.mul(r, o)), o = e.sqr(o), n >>= bt; + return r; +} +function xu(e, t, n = !1) { + const r = new Array(t.length).fill(n ? e.ZERO : void 0), o = t.reduce((i, c, a) => e.is0(c) ? i : (r[a] = i, e.mul(i, c)), e.ONE), s = e.inv(o); + return t.reduceRight((i, c, a) => e.is0(c) ? i : (r[a] = e.mul(i, r[a]), e.mul(i, c)), s), r; +} +function nc(e, t) { + const n = (e.ORDER - bt) / Ve, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); + if (!o && !s && !i) + throw new Error("invalid Legendre symbol result"); + return o ? 1 : s ? 0 : -1; +} +function oh(e, t) { + t !== void 0 && Fe(t); + const n = t !== void 0 ? t : e.toString(2).length, r = Math.ceil(n / 8); + return { nBitLength: n, nByteLength: r }; +} +class sh { + ORDER; + BITS; + BYTES; + isLE; + ZERO = St; + ONE = bt; + _lengths; + _sqrt; + // cached sqrt + _mod; + constructor(t, n = {}) { + if (t <= St) + throw new Error("invalid field: expected ORDER > 0, got " + t); + let r; + this.isLE = !1, n != null && typeof n == "object" && (typeof n.BITS == "number" && (r = n.BITS), typeof n.sqrt == "function" && (this.sqrt = n.sqrt), typeof n.isLE == "boolean" && (this.isLE = n.isLE), n.allowedLengths && (this._lengths = n.allowedLengths?.slice()), typeof n.modFromBytes == "boolean" && (this._mod = n.modFromBytes)); + const { nBitLength: o, nByteLength: s } = oh(t, r); + if (s > 2048) + throw new Error("invalid field: expected ORDER of <= 2048 bytes"); + this.ORDER = t, this.BITS = o, this.BYTES = s, this._sqrt = void 0, Object.preventExtensions(this); + } + create(t) { + return Dt(t, this.ORDER); + } + isValid(t) { + if (typeof t != "bigint") + throw new Error("invalid field element: expected bigint, got " + typeof t); + return St <= t && t < this.ORDER; + } + is0(t) { + return t === St; + } + // is valid and invertible + isValidNot0(t) { + return !this.is0(t) && this.isValid(t); + } + isOdd(t) { + return (t & bt) === bt; + } + neg(t) { + return Dt(-t, this.ORDER); + } + eql(t, n) { + return t === n; + } + sqr(t) { + return Dt(t * t, this.ORDER); + } + add(t, n) { + return Dt(t + n, this.ORDER); + } + sub(t, n) { + return Dt(t - n, this.ORDER); + } + mul(t, n) { + return Dt(t * n, this.ORDER); + } + pow(t, n) { + return rh(this, t, n); + } + div(t, n) { + return Dt(t * ec(n, this.ORDER), this.ORDER); + } + // Same as above, but doesn't normalize + sqrN(t) { + return t * t; + } + addN(t, n) { + return t + n; + } + subN(t, n) { + return t - n; + } + mulN(t, n) { + return t * n; + } + inv(t) { + return ec(t, this.ORDER); + } + sqrt(t) { + return this._sqrt || (this._sqrt = th(this.ORDER)), this._sqrt(this, t); + } + toBytes(t) { + return this.isLE ? Va(t, this.BYTES) : Ws(t, this.BYTES); + } + fromBytes(t, n = !1) { + et(t); + const { _lengths: r, BYTES: o, isLE: s, ORDER: i, _mod: c } = this; + if (r) { + if (!r.includes(t.length) || t.length > o) + throw new Error("Field.fromBytes: expected " + r + " bytes, got " + t.length); + const u = new Uint8Array(o); + u.set(t, s ? 0 : u.length - t.length), t = u; + } + if (t.length !== o) + throw new Error("Field.fromBytes: expected " + o + " bytes, got " + t.length); + let a = s ? Ha(t) : bn(t); + if (c && (a = Dt(a, i)), !n && !this.isValid(a)) + throw new Error("invalid field element: outside of range 0..ORDER"); + return a; + } + // TODO: we don't need it here, move out to separate fn + invertBatch(t) { + return xu(this, t); + } + // We can't move this out because Fp6, Fp12 implement it + // and it's unclear what to return in there. + cmov(t, n, r) { + return r ? n : t; + } +} +function uo(e, t = {}) { + return new sh(e, t); +} +function Su(e) { + if (typeof e != "bigint") + throw new Error("field order must be bigint"); + const t = e.toString(2).length; + return Math.ceil(t / 8); +} +function vu(e) { + const t = Su(e); + return t + Math.ceil(t / 2); +} +function Tu(e, t, n = !1) { + et(e); + const r = e.length, o = Su(t), s = vu(t); + if (r < 16 || r < s || r > 1024) + throw new Error("expected " + s + "-1024 bytes of input, got " + r); + const i = n ? Ha(e) : bn(e), c = Dt(i, t - bt) + bt; + return n ? Va(c, o) : Ws(c, o); +} +/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ +const hn = /* @__PURE__ */ BigInt(0), De = /* @__PURE__ */ BigInt(1); +function Vr(e, t) { + const n = t.negate(); + return e ? n : t; +} +function rc(e, t) { + const n = xu(e.Fp, t.map((r) => r.Z)); + return t.map((r, o) => e.fromAffine(r.toAffine(n[o]))); +} +function Au(e, t) { + if (!Number.isSafeInteger(e) || e <= 0 || e > t) + throw new Error("invalid window size, expected [1.." + t + "], got W=" + e); +} +function Ao(e, t) { + Au(e, t); + const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = zs(e), i = BigInt(e); + return { windows: n, windowSize: r, mask: s, maxNumber: o, shiftBy: i }; +} +function oc(e, t, n) { + const { windowSize: r, mask: o, maxNumber: s, shiftBy: i } = n; + let c = Number(e & o), a = e >> i; + c > r && (c -= s, a += De); + const u = t * r, f = u + Math.abs(c) - 1, l = c === 0, d = c < 0, h = t % 2 !== 0; + return { nextN: a, offset: f, isZero: l, isNeg: d, isNegF: h, offsetF: u }; +} +const Io = /* @__PURE__ */ new WeakMap(), Iu = /* @__PURE__ */ new WeakMap(); +function ko(e) { + return Iu.get(e) || 1; +} +function sc(e) { + if (e !== hn) + throw new Error("invalid wNAF"); +} +class ih { + BASE; + ZERO; + Fn; + bits; + // Parametrized with a given Point class (not individual point) + constructor(t, n) { + this.BASE = t.BASE, this.ZERO = t.ZERO, this.Fn = t.Fn, this.bits = n; + } + // non-const time multiplication ladder + _unsafeLadder(t, n, r = this.ZERO) { + let o = t; + for (; n > hn; ) + n & De && (r = r.add(o)), o = o.double(), n >>= De; + return r; + } + /** + * Creates a wNAF precomputation window. Used for caching. + * Default window size is set by `utils.precompute()` and is equal to 8. + * Number of precomputed points depends on the curve size: + * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where: + * - 𝑊 is the window size + * - 𝑛 is the bitlength of the curve order. + * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224. + * @param point Point instance + * @param W window size + * @returns precomputed point tables flattened to a single array + */ + precomputeWindow(t, n) { + const { windows: r, windowSize: o } = Ao(n, this.bits), s = []; + let i = t, c = i; + for (let a = 0; a < r; a++) { + c = i, s.push(c); + for (let u = 1; u < o; u++) + c = c.add(i), s.push(c); + i = c.double(); + } + return s; + } + /** + * Implements ec multiplication using precomputed tables and w-ary non-adjacent form. + * More compact implementation: + * https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541 + * @returns real and fake (for const-time) points + */ + wNAF(t, n, r) { + if (!this.Fn.isValid(r)) + throw new Error("invalid scalar"); + let o = this.ZERO, s = this.BASE; + const i = Ao(t, this.bits); + for (let c = 0; c < i.windows; c++) { + const { nextN: a, offset: u, isZero: f, isNeg: l, isNegF: d, offsetF: h } = oc(r, c, i); + r = a, f ? s = s.add(Vr(d, n[h])) : o = o.add(Vr(l, n[u])); + } + return sc(r), { p: o, f: s }; + } + /** + * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form. + * @param acc accumulator point to add result of multiplication + * @returns point + */ + wNAFUnsafe(t, n, r, o = this.ZERO) { + const s = Ao(t, this.bits); + for (let i = 0; i < s.windows && r !== hn; i++) { + const { nextN: c, offset: a, isZero: u, isNeg: f } = oc(r, i, s); + if (r = c, !u) { + const l = n[a]; + o = o.add(f ? l.negate() : l); + } + } + return sc(r), o; + } + getPrecomputes(t, n, r) { + let o = Io.get(n); + return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), Io.set(n, o))), o; + } + cached(t, n, r) { + const o = ko(t); + return this.wNAF(o, this.getPrecomputes(o, t, r), n); + } + unsafe(t, n, r, o) { + const s = ko(t); + return s === 1 ? this._unsafeLadder(t, n, o) : this.wNAFUnsafe(s, this.getPrecomputes(s, t, r), n, o); + } + // We calculate precomputes for elliptic curve point multiplication + // using windowed method. This specifies window size and + // stores precomputed values. Usually only base point would be precomputed. + createCache(t, n) { + Au(n, this.bits), Iu.set(t, n), Io.delete(t); + } + hasCache(t) { + return ko(t) !== 1; + } +} +function ch(e, t, n, r) { + let o = t, s = e.ZERO, i = e.ZERO; + for (; n > hn || r > hn; ) + n & De && (s = s.add(o)), r & De && (i = i.add(o)), o = o.double(), n >>= De, r >>= De; + return { p1: s, p2: i }; +} +function ic(e, t, n) { + if (t) { + if (t.ORDER !== e) + throw new Error("Field.ORDER must match order: Fp == p, Fn == n"); + return nh(t), t; + } else + return uo(e, { isLE: n }); +} +function ah(e, t, n = {}, r) { + if (r === void 0 && (r = e === "edwards"), !t || typeof t != "object") + throw new Error(`expected valid ${e} CURVE object`); + for (const a of ["p", "n", "h"]) { + const u = t[a]; + if (!(typeof u == "bigint" && u > hn)) + throw new Error(`CURVE.${a} must be positive bigint`); + } + const o = ic(t.p, n.Fp, r), s = ic(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; + for (const a of c) + if (!o.isValid(t[a])) + throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`); + return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: o, Fn: s }; +} +function ku(e, t) { + return function(r) { + const o = e(r); + return { secretKey: o, publicKey: t(o) }; + }; +} +class Bu { + oHash; + iHash; + blockLen; + outputLen; + finished = !1; + destroyed = !1; + constructor(t, n) { + if (La(t), et(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") + throw new Error("Expected instance of class which extends utils.Hash"); + this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen; + const r = this.blockLen, o = new Uint8Array(r); + o.set(n.length > r ? t.create().update(n).digest() : n); + for (let s = 0; s < o.length; s++) + o[s] ^= 54; + this.iHash.update(o), this.oHash = t.create(); + for (let s = 0; s < o.length; s++) + o[s] ^= 106; + this.oHash.update(o), Rr(o); + } + update(t) { + return Ur(this), this.iHash.update(t), this; + } + digestInto(t) { + Ur(this), et(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); + } + digest() { + const t = new Uint8Array(this.oHash.outputLen); + return this.digestInto(t), t; + } + _cloneInto(t) { + t ||= Object.create(Object.getPrototypeOf(this), {}); + const { oHash: n, iHash: r, finished: o, destroyed: s, blockLen: i, outputLen: c } = this; + return t = t, t.finished = o, t.destroyed = s, t.blockLen = i, t.outputLen = c, t.oHash = n._cloneInto(t.oHash), t.iHash = r._cloneInto(t.iHash), t; + } + clone() { + return this._cloneInto(); + } + destroy() { + this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy(); + } +} +const Ou = (e, t, n) => new Bu(e, t).update(n).digest(); +Ou.create = (e, t) => new Bu(e, t); +/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ +const cc = (e, t) => (e + (e >= 0 ? t : -t) / $u) / t; +function uh(e, t, n) { + const [[r, o], [s, i]] = t, c = cc(i * e, n), a = cc(-o * e, n); + let u = e - c * r - a * s, f = -c * o - a * i; + const l = u < ce, d = f < ce; + l && (u = -u), d && (f = -f); + const h = zs(Math.ceil(ud(n) / 2)) + tn; + if (u < ce || u >= h || f < ce || f >= h) + throw new Error("splitScalar (endomorphism): failed, k=" + e); + return { k1neg: l, k1: u, k2neg: d, k2: f }; +} +function os(e) { + if (!["compact", "recovered", "der"].includes(e)) + throw new Error('Signature format must be "compact", "recovered", or "der"'); + return e; +} +function Bo(e, t) { + const n = {}; + for (let r of Object.keys(t)) + n[r] = e[r] === void 0 ? t[r] : e[r]; + return Lr(n.lowS, "lowS"), Lr(n.prehash, "prehash"), n.format !== void 0 && os(n.format), n; +} +class fh extends Error { + constructor(t = "") { + super(t); + } +} +const be = { + // asn.1 DER encoding utils + Err: fh, + // Basic building block is TLV (Tag-Length-Value) + _tlv: { + encode: (e, t) => { + const { Err: n } = be; + if (e < 0 || e > 256) + throw new n("tlv.encode: wrong tag"); + if (t.length & 1) + throw new n("tlv.encode: unpadded data"); + const r = t.length / 2, o = rr(r); + if (o.length / 2 & 128) + throw new n("tlv.encode: long form length too big"); + const s = r > 127 ? rr(o.length / 2 | 128) : ""; + return rr(e) + s + o + t; + }, + // v - value, l - left bytes (unparsed) + decode(e, t) { + const { Err: n } = be; + let r = 0; + if (e < 0 || e > 256) + throw new n("tlv.encode: wrong tag"); + if (t.length < 2 || t[r++] !== e) + throw new n("tlv.decode: wrong tlv"); + const o = t[r++], s = !!(o & 128); + let i = 0; + if (!s) + i = o; + else { + const a = o & 127; + if (!a) + throw new n("tlv.decode(long): indefinite length not supported"); + if (a > 4) + throw new n("tlv.decode(long): byte length is too big"); + const u = t.subarray(r, r + a); + if (u.length !== a) + throw new n("tlv.decode: length bytes not complete"); + if (u[0] === 0) + throw new n("tlv.decode(long): zero leftmost byte"); + for (const f of u) + i = i << 8 | f; + if (r += a, i < 128) + throw new n("tlv.decode(long): not minimal encoding"); + } + const c = t.subarray(r, r + i); + if (c.length !== i) + throw new n("tlv.decode: wrong value length"); + return { v: c, l: t.subarray(r + i) }; + } + }, + // https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag, + // since we always use positive integers here. It must always be empty: + // - add zero byte if exists + // - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding) + _int: { + encode(e) { + const { Err: t } = be; + if (e < ce) + throw new t("integer: negative integers are not allowed"); + let n = rr(e); + if (Number.parseInt(n[0], 16) & 8 && (n = "00" + n), n.length & 1) + throw new t("unexpected DER parsing assertion: unpadded hex"); + return n; + }, + decode(e) { + const { Err: t } = be; + if (e[0] & 128) + throw new t("invalid signature integer: negative"); + if (e[0] === 0 && !(e[1] & 128)) + throw new t("invalid signature integer: unnecessary leading zero"); + return bn(e); + } + }, + toSig(e) { + const { Err: t, _int: n, _tlv: r } = be, o = et(e, void 0, "signature"), { v: s, l: i } = r.decode(48, o); + if (i.length) + throw new t("invalid signature: left bytes after parsing"); + const { v: c, l: a } = r.decode(2, s), { v: u, l: f } = r.decode(2, a); + if (f.length) + throw new t("invalid signature: left bytes after parsing"); + return { r: n.decode(c), s: n.decode(u) }; + }, + hexFromSig(e) { + const { _tlv: t, _int: n } = be, r = t.encode(2, n.encode(e.r)), o = t.encode(2, n.encode(e.s)), s = r + o; + return t.encode(48, s); + } +}, ce = BigInt(0), tn = BigInt(1), $u = BigInt(2), or = BigInt(3), lh = BigInt(4); +function dh(e, t = {}) { + const n = ah("weierstrass", e, t), { Fp: r, Fn: o } = n; + let s = n.CURVE; + const { h: i, n: c } = s; + Gs(t, {}, { + allowInfinityPoint: "boolean", + clearCofactor: "function", + isTorsionFree: "function", + fromBytes: "function", + toBytes: "function", + endo: "object" + }); + const { endo: a } = t; + if (a && (!r.is0(s.a) || typeof a.beta != "bigint" || !Array.isArray(a.basises))) + throw new Error('invalid endo: expected "beta": bigint and "basises": array'); + const u = Ru(r, o); + function f() { + if (!r.isOdd) + throw new Error("compression is not supported: Field does not have .isOdd()"); + } + function l(_, b, m) { + const { x: p, y: E } = b.toAffine(), A = r.toBytes(p); + if (Lr(m, "isCompressed"), m) { + f(); + const k = !r.isOdd(E); + return Xt(Uu(k), A); + } else + return Xt(Uint8Array.of(4), A, r.toBytes(E)); + } + function d(_) { + et(_, void 0, "Point"); + const { publicKey: b, publicKeyUncompressed: m } = u, p = _.length, E = _[0], A = _.subarray(1); + if (p === b && (E === 2 || E === 3)) { + const k = r.fromBytes(A); + if (!r.isValid(k)) + throw new Error("bad point: is not on curve, wrong x"); + const I = g(k); + let T; + try { + T = r.sqrt(I); + } catch (F) { + const H = F instanceof Error ? ": " + F.message : ""; + throw new Error("bad point: is not on curve, sqrt error" + H); + } + f(); + const B = r.isOdd(T); + return (E & 1) === 1 !== B && (T = r.neg(T)), { x: k, y: T }; + } else if (p === m && E === 4) { + const k = r.BYTES, I = r.fromBytes(A.subarray(0, k)), T = r.fromBytes(A.subarray(k, k * 2)); + if (!y(I, T)) + throw new Error("bad point: is not on curve"); + return { x: I, y: T }; + } else + throw new Error(`bad point: got length ${p}, expected compressed=${b} or uncompressed=${m}`); + } + const h = t.toBytes || l, w = t.fromBytes || d; + function g(_) { + const b = r.sqr(_), m = r.mul(b, _); + return r.add(r.add(m, r.mul(_, s.a)), s.b); + } + function y(_, b) { + const m = r.sqr(b), p = g(_); + return r.eql(m, p); + } + if (!y(s.Gx, s.Gy)) + throw new Error("bad curve params: generator point"); + const S = r.mul(r.pow(s.a, or), lh), v = r.mul(r.sqr(s.b), BigInt(27)); + if (r.is0(r.add(S, v))) + throw new Error("bad curve params: a or b"); + function O(_, b, m = !1) { + if (!r.isValid(b) || m && r.is0(b)) + throw new Error(`bad point coordinate ${_}`); + return b; + } + function N(_) { + if (!(_ instanceof C)) + throw new Error("Weierstrass Point expected"); + } + function U(_) { + if (!a || !a.basises) + throw new Error("no endo"); + return uh(_, a.basises, o.ORDER); + } + const W = Gi((_, b) => { + const { X: m, Y: p, Z: E } = _; + if (r.eql(E, r.ONE)) + return { x: m, y: p }; + const A = _.is0(); + b == null && (b = A ? r.ONE : r.inv(E)); + const k = r.mul(m, b), I = r.mul(p, b), T = r.mul(E, b); + if (A) + return { x: r.ZERO, y: r.ZERO }; + if (!r.eql(T, r.ONE)) + throw new Error("invZ was invalid"); + return { x: k, y: I }; + }), x = Gi((_) => { + if (_.is0()) { + if (t.allowInfinityPoint && !r.is0(_.Y)) + return; + throw new Error("bad point: ZERO"); + } + const { x: b, y: m } = _.toAffine(); + if (!r.isValid(b) || !r.isValid(m)) + throw new Error("bad point: x or y not field elements"); + if (!y(b, m)) + throw new Error("bad point: equation left != right"); + if (!_.isTorsionFree()) + throw new Error("bad point: not in prime-order subgroup"); + return !0; + }); + function Q(_, b, m, p, E) { + return m = new C(r.mul(m.X, _), m.Y, m.Z), b = Vr(p, b), m = Vr(E, m), b.add(m); + } + class C { + // base / generator point + static BASE = new C(s.Gx, s.Gy, r.ONE); + // zero / infinity / identity point + static ZERO = new C(r.ZERO, r.ONE, r.ZERO); + // 0, 1, 0 + // math field + static Fp = r; + // scalar field + static Fn = o; + X; + Y; + Z; + /** Does NOT validate if the point is valid. Use `.assertValidity()`. */ + constructor(b, m, p) { + this.X = O("x", b), this.Y = O("y", m, !0), this.Z = O("z", p), Object.freeze(this); + } + static CURVE() { + return s; + } + /** Does NOT validate if the point is valid. Use `.assertValidity()`. */ + static fromAffine(b) { + const { x: m, y: p } = b || {}; + if (!b || !r.isValid(m) || !r.isValid(p)) + throw new Error("invalid affine point"); + if (b instanceof C) + throw new Error("projective point not allowed"); + return r.is0(m) && r.is0(p) ? C.ZERO : new C(m, p, r.ONE); + } + static fromBytes(b) { + const m = C.fromAffine(w(et(b, void 0, "point"))); + return m.assertValidity(), m; + } + static fromHex(b) { + return C.fromBytes(Nr(b)); + } + get x() { + return this.toAffine().x; + } + get y() { + return this.toAffine().y; + } + /** + * + * @param windowSize + * @param isLazy true will defer table computation until the first multiplication + * @returns + */ + precompute(b = 8, m = !0) { + return gt.createCache(this, b), m || this.multiply(or), this; + } + // TODO: return `this` + /** A point on curve is valid if it conforms to equation. */ + assertValidity() { + x(this); + } + hasEvenY() { + const { y: b } = this.toAffine(); + if (!r.isOdd) + throw new Error("Field doesn't support isOdd"); + return !r.isOdd(b); + } + /** Compare one point to another. */ + equals(b) { + N(b); + const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b, T = r.eql(r.mul(m, I), r.mul(A, E)), B = r.eql(r.mul(p, I), r.mul(k, E)); + return T && B; + } + /** Flips point to one corresponding to (x, -y) in Affine coordinates. */ + negate() { + return new C(this.X, r.neg(this.Y), this.Z); + } + // Renes-Costello-Batina exception-free doubling formula. + // There is 30% faster Jacobian formula, but it is not complete. + // https://eprint.iacr.org/2015/1060, algorithm 3 + // Cost: 8M + 3S + 3*a + 2*b3 + 15add. + double() { + const { a: b, b: m } = s, p = r.mul(m, or), { X: E, Y: A, Z: k } = this; + let I = r.ZERO, T = r.ZERO, B = r.ZERO, R = r.mul(E, E), F = r.mul(A, A), H = r.mul(k, k), L = r.mul(E, A); + return L = r.add(L, L), B = r.mul(E, k), B = r.add(B, B), I = r.mul(b, B), T = r.mul(p, H), T = r.add(I, T), I = r.sub(F, T), T = r.add(F, T), T = r.mul(I, T), I = r.mul(L, I), B = r.mul(p, B), H = r.mul(b, H), L = r.sub(R, H), L = r.mul(b, L), L = r.add(L, B), B = r.add(R, R), R = r.add(B, R), R = r.add(R, H), R = r.mul(R, L), T = r.add(T, R), H = r.mul(A, k), H = r.add(H, H), R = r.mul(H, L), I = r.sub(I, R), B = r.mul(H, F), B = r.add(B, B), B = r.add(B, B), new C(I, T, B); + } + // Renes-Costello-Batina exception-free addition formula. + // There is 30% faster Jacobian formula, but it is not complete. + // https://eprint.iacr.org/2015/1060, algorithm 1 + // Cost: 12M + 0S + 3*a + 3*b3 + 23add. + add(b) { + N(b); + const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b; + let T = r.ZERO, B = r.ZERO, R = r.ZERO; + const F = s.a, H = r.mul(s.b, or); + let L = r.mul(m, A), D = r.mul(p, k), q = r.mul(E, I), st = r.add(m, p), M = r.add(A, k); + st = r.mul(st, M), M = r.add(L, D), st = r.sub(st, M), M = r.add(m, E); + let Y = r.add(A, I); + return M = r.mul(M, Y), Y = r.add(L, q), M = r.sub(M, Y), Y = r.add(p, E), T = r.add(k, I), Y = r.mul(Y, T), T = r.add(D, q), Y = r.sub(Y, T), R = r.mul(F, M), T = r.mul(H, q), R = r.add(T, R), T = r.sub(D, R), R = r.add(D, R), B = r.mul(T, R), D = r.add(L, L), D = r.add(D, L), q = r.mul(F, q), M = r.mul(H, M), D = r.add(D, q), q = r.sub(L, q), q = r.mul(F, q), M = r.add(M, q), L = r.mul(D, M), B = r.add(B, L), L = r.mul(Y, M), T = r.mul(st, T), T = r.sub(T, L), L = r.mul(st, D), R = r.mul(Y, R), R = r.add(R, L), new C(T, B, R); + } + subtract(b) { + return this.add(b.negate()); + } + is0() { + return this.equals(C.ZERO); + } + /** + * Constant time multiplication. + * Uses wNAF method. Windowed method may be 10% faster, + * but takes 2x longer to generate and consumes 2x memory. + * Uses precomputes when available. + * Uses endomorphism for Koblitz curves. + * @param scalar by which the point would be multiplied + * @returns New point + */ + multiply(b) { + const { endo: m } = t; + if (!o.isValidNot0(b)) + throw new Error("invalid scalar: out of range"); + let p, E; + const A = (k) => gt.cached(this, k, (I) => rc(C, I)); + if (m) { + const { k1neg: k, k1: I, k2neg: T, k2: B } = U(b), { p: R, f: F } = A(I), { p: H, f: L } = A(B); + E = F.add(L), p = Q(m.beta, R, H, k, T); + } else { + const { p: k, f: I } = A(b); + p = k, E = I; + } + return rc(C, [p, E])[0]; + } + /** + * Non-constant-time multiplication. Uses double-and-add algorithm. + * It's faster, but should only be used when you don't care about + * an exposed secret key e.g. sig verification, which works over *public* keys. + */ + multiplyUnsafe(b) { + const { endo: m } = t, p = this; + if (!o.isValid(b)) + throw new Error("invalid scalar: out of range"); + if (b === ce || p.is0()) + return C.ZERO; + if (b === tn) + return p; + if (gt.hasCache(this)) + return this.multiply(b); + if (m) { + const { k1neg: E, k1: A, k2neg: k, k2: I } = U(b), { p1: T, p2: B } = ch(C, p, A, I); + return Q(m.beta, T, B, E, k); + } else + return gt.unsafe(p, b); + } + /** + * Converts Projective point to affine (x, y) coordinates. + * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch + */ + toAffine(b) { + return W(this, b); + } + /** + * Checks whether Point is free of torsion elements (is in prime subgroup). + * Always torsion-free for cofactor=1 curves. + */ + isTorsionFree() { + const { isTorsionFree: b } = t; + return i === tn ? !0 : b ? b(C, this) : gt.unsafe(this, c).is0(); + } + clearCofactor() { + const { clearCofactor: b } = t; + return i === tn ? this : b ? b(C, this) : this.multiplyUnsafe(i); + } + isSmallOrder() { + return this.multiplyUnsafe(i).is0(); + } + toBytes(b = !0) { + return Lr(b, "isCompressed"), this.assertValidity(), h(C, this, b); + } + toHex(b = !0) { + return so(this.toBytes(b)); + } + toString() { + return ``; + } + } + const Pt = o.BITS, gt = new ih(C, t.endo ? Math.ceil(Pt / 2) : Pt); + return C.BASE.precompute(8), C; +} +function Uu(e) { + return Uint8Array.of(e ? 2 : 3); +} +function Ru(e, t) { + return { + secretKey: t.BYTES, + publicKey: 1 + e.BYTES, + publicKeyUncompressed: 1 + 2 * e.BYTES, + publicKeyHasPrefix: !0, + signature: 2 * t.BYTES + }; +} +function hh(e, t = {}) { + const { Fn: n } = e, r = t.randomBytes || io, o = Object.assign(Ru(e.Fp, n), { seed: vu(n.ORDER) }); + function s(h) { + try { + const w = n.fromBytes(h); + return n.isValidNot0(w); + } catch { + return !1; + } + } + function i(h, w) { + const { publicKey: g, publicKeyUncompressed: y } = o; + try { + const S = h.length; + return w === !0 && S !== g || w === !1 && S !== y ? !1 : !!e.fromBytes(h); + } catch { + return !1; + } + } + function c(h = r(o.seed)) { + return Tu(et(h, o.seed, "seed"), n.ORDER); + } + function a(h, w = !0) { + return e.BASE.multiply(n.fromBytes(h)).toBytes(w); + } + function u(h) { + const { secretKey: w, publicKey: g, publicKeyUncompressed: y } = o; + if (!Ks(h) || "_lengths" in n && n._lengths || w === g) + return; + const S = et(h, void 0, "key").length; + return S === g || S === y; + } + function f(h, w, g = !0) { + if (u(h) === !0) + throw new Error("first arg must be private key"); + if (u(w) === !1) + throw new Error("second arg must be public key"); + const y = n.fromBytes(h); + return e.fromBytes(w).multiply(y).toBytes(g); + } + const l = { + isValidSecretKey: s, + isValidPublicKey: i, + randomSecretKey: c + }, d = ku(c, a); + return Object.freeze({ getPublicKey: a, getSharedSecret: f, keygen: d, Point: e, utils: l, lengths: o }); +} +function ph(e, t, n = {}) { + La(t), Gs(n, {}, { + hmac: "function", + lowS: "boolean", + randomBytes: "function", + bits2int: "function", + bits2int_modN: "function" + }), n = Object.assign({}, n); + const r = n.randomBytes || io, o = n.hmac || ((m, p) => Ou(t, m, p)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: l, utils: d, lengths: h } = hh(e, n), w = { + prehash: !0, + lowS: typeof n.lowS == "boolean" ? n.lowS : !0, + format: "compact", + extraEntropy: !1 + }, g = c * $u < s.ORDER; + function y(m) { + const p = c >> tn; + return m > p; + } + function S(m, p) { + if (!i.isValidNot0(p)) + throw new Error(`invalid signature ${m}: out of range 1..Point.Fn.ORDER`); + return p; + } + function v() { + if (g) + throw new Error('"recovered" sig type is not supported for cofactor >2 curves'); + } + function O(m, p) { + os(p); + const E = h.signature, A = p === "compact" ? E : p === "recovered" ? E + 1 : void 0; + return et(m, A); + } + class N { + r; + s; + recovery; + constructor(p, E, A) { + if (this.r = S("r", p), this.s = S("s", E), A != null) { + if (v(), ![0, 1, 2, 3].includes(A)) + throw new Error("invalid recovery id"); + this.recovery = A; + } + Object.freeze(this); + } + static fromBytes(p, E = w.format) { + O(p, E); + let A; + if (E === "der") { + const { r: B, s: R } = be.toSig(et(p)); + return new N(B, R); + } + E === "recovered" && (A = p[0], E = "compact", p = p.subarray(1)); + const k = h.signature / 2, I = p.subarray(0, k), T = p.subarray(k, k * 2); + return new N(i.fromBytes(I), i.fromBytes(T), A); + } + static fromHex(p, E) { + return this.fromBytes(Nr(p), E); + } + assertRecovery() { + const { recovery: p } = this; + if (p == null) + throw new Error("invalid recovery id: must be present"); + return p; + } + addRecoveryBit(p) { + return new N(this.r, this.s, p); + } + recoverPublicKey(p) { + const { r: E, s: A } = this, k = this.assertRecovery(), I = k === 2 || k === 3 ? E + c : E; + if (!s.isValid(I)) + throw new Error("invalid recovery id: sig.r+curve.n != R.x"); + const T = s.toBytes(I), B = e.fromBytes(Xt(Uu((k & 1) === 0), T)), R = i.inv(I), F = W(et(p, void 0, "msgHash")), H = i.create(-F * R), L = i.create(A * R), D = e.BASE.multiplyUnsafe(H).add(B.multiplyUnsafe(L)); + if (D.is0()) + throw new Error("invalid recovery: point at infinify"); + return D.assertValidity(), D; + } + // Signatures should be low-s, to prevent malleability. + hasHighS() { + return y(this.s); + } + toBytes(p = w.format) { + if (os(p), p === "der") + return Nr(be.hexFromSig(this)); + const { r: E, s: A } = this, k = i.toBytes(E), I = i.toBytes(A); + return p === "recovered" ? (v(), Xt(Uint8Array.of(this.assertRecovery()), k, I)) : Xt(k, I); + } + toHex(p) { + return so(this.toBytes(p)); + } + } + const U = n.bits2int || function(p) { + if (p.length > 8192) + throw new Error("input is too large"); + const E = bn(p), A = p.length * 8 - a; + return A > 0 ? E >> BigInt(A) : E; + }, W = n.bits2int_modN || function(p) { + return i.create(U(p)); + }, x = zs(a); + function Q(m) { + return ad("num < 2^" + a, m, ce, x), i.toBytes(m); + } + function C(m, p) { + return et(m, void 0, "message"), p ? et(t(m), void 0, "prehashed message") : m; + } + function Pt(m, p, E) { + const { lowS: A, prehash: k, extraEntropy: I } = Bo(E, w); + m = C(m, k); + const T = W(m), B = i.fromBytes(p); + if (!i.isValidNot0(B)) + throw new Error("invalid private key"); + const R = [Q(B), Q(T)]; + if (I != null && I !== !1) { + const D = I === !0 ? r(h.secretKey) : I; + R.push(et(D, void 0, "extraEntropy")); + } + const F = Xt(...R), H = T; + function L(D) { + const q = U(D); + if (!i.isValidNot0(q)) + return; + const st = i.inv(q), M = e.BASE.multiply(q).toAffine(), Y = i.create(M.x); + if (Y === ce) + return; + const ee = i.create(st * i.create(H + Y * B)); + if (ee === ce) + return; + let En = (M.x === Y ? 0 : 2) | Number(M.y & tn), xn = ee; + return A && y(ee) && (xn = i.neg(ee), En ^= 1), new N(Y, xn, g ? void 0 : En); + } + return { seed: F, k2sig: L }; + } + function gt(m, p, E = {}) { + const { seed: A, k2sig: k } = Pt(m, p, E); + return fd(t.outputLen, i.BYTES, o)(A, k).toBytes(E.format); + } + function _(m, p, E, A = {}) { + const { lowS: k, prehash: I, format: T } = Bo(A, w); + if (E = et(E, void 0, "publicKey"), p = C(p, I), !Ks(m)) { + const B = m instanceof N ? ", use sig.toBytes()" : ""; + throw new Error("verify expects Uint8Array signature" + B); + } + O(m, T); + try { + const B = N.fromBytes(m, T), R = e.fromBytes(E); + if (k && B.hasHighS()) + return !1; + const { r: F, s: H } = B, L = W(p), D = i.inv(H), q = i.create(L * D), st = i.create(F * D), M = e.BASE.multiplyUnsafe(q).add(R.multiplyUnsafe(st)); + return M.is0() ? !1 : i.create(M.x) === F; + } catch { + return !1; + } + } + function b(m, p, E = {}) { + const { prehash: A } = Bo(E, w); + return p = C(p, A), N.fromBytes(m, "recovered").recoverPublicKey(p).toBytes(); + } + return Object.freeze({ + keygen: u, + getPublicKey: f, + getSharedSecret: l, + utils: d, + lengths: h, + Point: e, + sign: gt, + verify: _, + recoverPublicKey: b, + Signature: N, + hash: t + }); +} +/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ +const fo = { + p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"), + n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"), + h: BigInt(1), + a: BigInt(0), + b: BigInt(7), + Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"), + Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8") +}, gh = { + beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"), + basises: [ + [BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")], + [BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")] + ] +}, wh = /* @__PURE__ */ BigInt(0), ss = /* @__PURE__ */ BigInt(2); +function yh(e) { + const t = fo.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, l = Nt(f, n, t) * f % t, d = Nt(l, n, t) * f % t, h = Nt(d, ss, t) * u % t, w = Nt(h, o, t) * h % t, g = Nt(w, s, t) * w % t, y = Nt(g, c, t) * g % t, S = Nt(y, a, t) * y % t, v = Nt(S, c, t) * g % t, O = Nt(v, n, t) * f % t, N = Nt(O, i, t) * w % t, U = Nt(N, r, t) * u % t, W = Nt(U, ss, t); + if (!Dr.eql(Dr.sqr(W), e)) + throw new Error("Cannot find square root"); + return W; +} +const Dr = uo(fo.p, { sqrt: yh }), qe = /* @__PURE__ */ dh(fo, { + Fp: Dr, + endo: gh +}), ac = /* @__PURE__ */ ph(qe, rs), uc = {}; +function Mr(e, ...t) { + let n = uc[e]; + if (n === void 0) { + const r = rs(id(e)); + n = Xt(r, r), uc[e] = n; + } + return rs(Xt(n, ...t)); +} +const Qs = (e) => e.toBytes(!0).slice(1), Js = (e) => e % ss === wh; +function is(e) { + const { Fn: t, BASE: n } = qe, r = t.fromBytes(e), o = n.multiply(r); + return { scalar: Js(o.y) ? r : t.neg(r), bytes: Qs(o) }; +} +function Nu(e) { + const t = Dr; + if (!t.isValidNot0(e)) + throw new Error("invalid x: Fail if x ≥ p"); + const n = t.create(e * e), r = t.create(n * e + BigInt(7)); + let o = t.sqrt(r); + Js(o) || (o = t.neg(o)); + const s = qe.fromAffine({ x: e, y: o }); + return s.assertValidity(), s; +} +const Un = bn; +function Lu(...e) { + return qe.Fn.create(Un(Mr("BIP0340/challenge", ...e))); +} +function fc(e) { + return is(e).bytes; +} +function mh(e, t, n = io(32)) { + const { Fn: r } = qe, o = et(e, void 0, "message"), { bytes: s, scalar: i } = is(t), c = et(n, 32, "auxRand"), a = r.toBytes(i ^ Un(Mr("BIP0340/aux", c))), u = Mr("BIP0340/nonce", a, s, o), { bytes: f, scalar: l } = is(u), d = Lu(f, s, o), h = new Uint8Array(64); + if (h.set(f, 0), h.set(r.toBytes(r.create(l + d * i)), 32), !Cu(h, o, s)) + throw new Error("sign: Invalid signature produced"); + return h; +} +function Cu(e, t, n) { + const { Fp: r, Fn: o, BASE: s } = qe, i = et(e, 64, "signature"), c = et(t, void 0, "message"), a = et(n, 32, "publicKey"); + try { + const u = Nu(Un(a)), f = Un(i.subarray(0, 32)); + if (!r.isValidNot0(f)) + return !1; + const l = Un(i.subarray(32, 64)); + if (!o.isValidNot0(l)) + return !1; + const d = Lu(o.toBytes(f), Qs(u), c), h = s.multiplyUnsafe(l).add(u.multiplyUnsafe(o.neg(d))), { x: w, y: g } = h.toAffine(); + return !(h.is0() || !Js(g) || w !== f); + } catch { + return !1; + } +} +const ti = /* @__PURE__ */ (() => { + const n = (r = io(48)) => Tu(r, fo.n); + return { + keygen: ku(n, fc), + getPublicKey: fc, + sign: mh, + verify: Cu, + Point: qe, + utils: { + randomSecretKey: n, + taggedHash: Mr, + lift_x: Nu, + pointToBytes: Qs + }, + lengths: { + secretKey: 32, + publicKey: 32, + publicKeyHasPrefix: !1, + signature: 64, + seed: 48 + } + }; +})(); +function ei(e, t, n = {}) { + e = Zo(e); + const { aggPublicKey: r } = Xo(e); + if (!n.taprootTweak) + return { + preTweakedKey: r.toBytes(!0), + finalKey: r.toBytes(!0) + }; + const o = ti.utils.taggedHash("TapTweak", r.toBytes(!0).subarray(1), n.taprootTweak ?? new Uint8Array(0)), { aggPublicKey: s } = Xo(e, [o], [!0]); + return { + preTweakedKey: r.toBytes(!0), + finalKey: s.toBytes(!0) + }; +} +class sr extends Error { + constructor(t) { + super(t), this.name = "PartialSignatureError"; + } +} +class ni { + constructor(t, n) { + if (this.s = t, this.R = n, t.length !== 32) + throw new sr("Invalid s length"); + if (n.length !== 33) + throw new sr("Invalid R length"); + } + /** + * Encodes the partial signature into bytes + * Returns a 32-byte array containing just the s value + */ + encode() { + return new Uint8Array(this.s); + } + /** + * Decodes a partial signature from bytes + * @param bytes - 32-byte array containing s value + */ + static decode(t) { + if (t.length !== 32) + throw new sr("Invalid partial signature length"); + if (bn(t) >= Et.CURVE().n) + throw new sr("s value overflows curve order"); + const r = new Uint8Array(33); + return new ni(t, r); + } +} +function bh(e, t, n, r, o, s) { + let i; + if (s?.taprootTweak !== void 0) { + const { preTweakedKey: u } = ei(Zo(r)); + i = ti.utils.taggedHash("TapTweak", u.subarray(1), s.taprootTweak); } - const a = new hf(n, ao(r), o, i ? [i] : void 0, i ? [!0] : void 0).sign(e, t); - return es.decode(a); + const a = new Ql(n, Zo(r), o, i ? [i] : void 0, i ? [!0] : void 0).sign(e, t); + return ni.decode(a); } -var Mr, ti; -function Zf() { - if (ti) return Mr; - ti = 1; +var Oo, lc; +function Eh() { + if (lc) return Oo; + lc = 1; const e = 4294967295, t = 1 << 31, n = 9, r = 65535, o = 1 << 22, s = r, i = 1 << n, c = r << n; function a(f) { return f & t ? {} : f & o ? { @@ -5447,14 +7086,14 @@ function Zf() { if (f > r) throw new TypeError("Expected Number blocks <= " + s); return f; } - return Mr = { decode: a, encode: u }, Mr; + return Oo = { decode: a, encode: u }, Oo; } -var go = Zf(), vt; +var cs = Eh(), Ot; (function(e) { e.VtxoTaprootTree = "taptree", e.VtxoTreeExpiry = "expiry", e.Cosigner = "cosigner", e.ConditionWitness = "condition"; -})(vt || (vt = {})); -const ns = 222; -function Xf(e, t, n, r) { +})(Ot || (Ot = {})); +const ri = 222; +function xh(e, t, n, r) { e.updateInput(t, { unknown: [ ...e.getInput(t)?.unknown ?? [], @@ -5462,7 +7101,7 @@ function Xf(e, t, n, r) { ] }); } -function wo(e, t, n) { +function as(e, t, n) { const r = e.getInput(t)?.unknown ?? [], o = []; for (const s of r) { const i = n.decode(s); @@ -5470,76 +7109,76 @@ function wo(e, t, n) { } return o; } -const Xc = { - key: vt.VtxoTaprootTree, +const _u = { + key: Ot.VtxoTaprootTree, encode: (e) => [ { - type: ns, - key: Or[vt.VtxoTaprootTree] + type: ri, + key: lo[Ot.VtxoTaprootTree] }, e ], - decode: (e) => rs(() => os(e[0], vt.VtxoTaprootTree) ? e[1] : null) -}, Qf = { - key: vt.ConditionWitness, + decode: (e) => oi(() => si(e[0], Ot.VtxoTaprootTree) ? e[1] : null) +}, Sh = { + key: Ot.ConditionWitness, encode: (e) => [ { - type: ns, - key: Or[vt.ConditionWitness] + type: ri, + key: lo[Ot.ConditionWitness] }, - dn.encode(e) + Cn.encode(e) ], - decode: (e) => rs(() => os(e[0], vt.ConditionWitness) ? dn.decode(e[1]) : null) -}, yo = { - key: vt.Cosigner, + decode: (e) => oi(() => si(e[0], Ot.ConditionWitness) ? Cn.decode(e[1]) : null) +}, us = { + key: Ot.Cosigner, encode: (e) => [ { - type: ns, + type: ri, key: new Uint8Array([ - ...Or[vt.Cosigner], + ...lo[Ot.Cosigner], e.index ]) }, e.key ], - decode: (e) => rs(() => os(e[0], vt.Cosigner) ? { + decode: (e) => oi(() => si(e[0], Ot.Cosigner) ? { index: e[0].key[e[0].key.length - 1], key: e[1] } : null) }; -vt.VtxoTreeExpiry; -const Or = Object.fromEntries(Object.values(vt).map((e) => [ +Ot.VtxoTreeExpiry; +const lo = Object.fromEntries(Object.values(Ot).map((e) => [ e, new TextEncoder().encode(e) -])), rs = (e) => { +])), oi = (e) => { try { return e(); } catch { return null; } }; -function os(e, t) { - const n = S.encode(Or[t]); - return S.encode(new Uint8Array([e.type, ...e.key])).includes(n); +function si(e, t) { + const n = $.encode(lo[t]); + return $.encode(new Uint8Array([e.type, ...e.key])).includes(n); } -const Pn = new Error("missing vtxo graph"); -class yn { +const ir = new Error("missing vtxo graph"); +class Dn { constructor(t) { this.secretKey = t, this.myNonces = null, this.aggregateNonces = null, this.graph = null, this.scriptRoot = null, this.rootSharedOutputAmount = null; } static random() { - const t = Jr(); - return new yn(t); + const t = Mo(); + return new Dn(t); } async init(t, n, r) { this.graph = t, this.scriptRoot = n, this.rootSharedOutputAmount = r; } async getPublicKey() { - return Ft.getPublicKey(this.secretKey); + return ac.getPublicKey(this.secretKey); } async getNonces() { if (!this.graph) - throw Pn; + throw ir; this.myNonces || (this.myNonces = this.generateNonces()); const t = /* @__PURE__ */ new Map(); for (const [n, r] of this.myNonces) @@ -5548,7 +7187,7 @@ class yn { } async aggregatedNonces(t, n) { if (!this.graph) - throw Pn; + throw ir; if (this.aggregateNonces || (this.aggregateNonces = /* @__PURE__ */ new Map()), this.myNonces || await this.getNonces(), this.aggregateNonces.has(t)) return { hasAllNonces: this.aggregateNonces.size === this.myNonces?.size @@ -5557,12 +7196,12 @@ class yn { if (!r) throw new Error(`missing nonce for txid ${t}`); const o = await this.getPublicKey(); - n.set(S.encode(o.subarray(1)), r); + n.set($.encode(o.subarray(1)), r); const s = this.graph.find(t); if (!s) throw new Error(`missing tx for txid ${t}`); - const i = wo(s.root, 0, yo).map( - (u) => S.encode(u.key.subarray(1)) + const i = as(s.root, 0, us).map( + (u) => $.encode(u.key.subarray(1)) // xonly pubkey ), c = []; for (const u of i) { @@ -5571,14 +7210,14 @@ class yn { throw new Error(`missing nonce for cosigner ${u}`); c.push(f.pubNonce); } - const a = gf(c); + const a = td(c); return this.aggregateNonces.set(t, { pubNonce: a }), { hasAllNonces: this.aggregateNonces.size === this.myNonces?.size }; } async sign() { if (!this.graph) - throw Pn; + throw ir; if (!this.aggregateNonces) throw new Error("nonces not set"); if (!this.myNonces) @@ -5592,17 +7231,17 @@ class yn { } generateNonces() { if (!this.graph) - throw Pn; - const t = /* @__PURE__ */ new Map(), n = Ft.getPublicKey(this.secretKey); + throw ir; + const t = /* @__PURE__ */ new Map(), n = ac.getPublicKey(this.secretKey); for (const r of this.graph.iterator()) { - const o = pf(n); + const o = Jl(n); t.set(r.txid, o); } return t; } signPartial(t) { if (!this.graph || !this.scriptRoot || !this.rootSharedOutputAmount) - throw yn.NOT_INITIALIZED; + throw Dn.NOT_INITIALIZED; if (!this.myNonces || !this.aggregateNonces) throw new Error("session not properly initialized"); const n = this.myNonces.get(t.txid); @@ -5611,28 +7250,28 @@ class yn { const r = this.aggregateNonces.get(t.txid); if (!r) throw new Error("missing aggregate nonce"); - const o = [], s = [], i = wo(t.root, 0, yo).map((u) => u.key), { finalKey: c } = ts(i, !0, { + const o = [], s = [], i = as(t.root, 0, us).map((u) => u.key), { finalKey: c } = ei(i, !0, { taprootTweak: this.scriptRoot }); for (let u = 0; u < t.root.inputsLength; u++) { - const f = Jf(c, this.graph, this.rootSharedOutputAmount, t.root); + const f = vh(c, this.graph, this.rootSharedOutputAmount, t.root); o.push(f.amount), s.push(f.script); } const a = t.root.preimageWitnessV1( 0, // always first input s, - Ae.DEFAULT, + Ke.DEFAULT, o ); - return Yf(n.secNonce, this.secretKey, r.pubNonce, i, a, { + return bh(n.secNonce, this.secretKey, r.pubNonce, i, a, { taprootTweak: this.scriptRoot }); } } -yn.NOT_INITIALIZED = new Error("session not initialized, call init method"); -function Jf(e, t, n, r) { - const o = L.encode(["OP_1", e.slice(1)]); +Dn.NOT_INITIALIZED = new Error("session not initialized, call init method"); +function vh(e, t, n, r) { + const o = K.encode(["OP_1", e.slice(1)]); if (r.id === t.txid) return { amount: n, @@ -5641,7 +7280,7 @@ function Jf(e, t, n, r) { const s = r.getInput(0); if (!s.txid) throw new Error("missing parent input txid"); - const i = S.encode(s.txid), c = t.find(i); + const i = $.encode(s.txid), c = t.find(i); if (!c) throw new Error("parent tx not found"); if (s.index === void 0) @@ -5656,19 +7295,19 @@ function Jf(e, t, n, r) { script: o }; } -const ei = Object.values(Ae).filter((e) => typeof e == "number"); -class un { +const dc = Object.values(Ke).filter((e) => typeof e == "number"); +class Rn { constructor(t) { - this.key = t || Jr(); + this.key = t || Mo(); } static fromPrivateKey(t) { - return new un(t); + return new Rn(t); } static fromHex(t) { - return new un(S.decode(t)); + return new Rn($.decode(t)); } static fromRandomBytes() { - return new un(Jr()); + return new Rn(Mo()); } /** * Export the private key as a hex string. @@ -5676,13 +7315,13 @@ class un { * @returns The private key as a hex string */ toHex() { - return S.encode(this.key); + return $.encode(this.key); } async sign(t, n) { const r = t.clone(); if (!n) { try { - if (!r.sign(this.key, ei)) + if (!r.sign(this.key, dc)) throw new Error("Failed to sign transaction"); } catch (o) { if (!(o instanceof Error && o.message.includes("No inputs signed"))) throw o; @@ -5690,24 +7329,24 @@ class un { return r; } for (const o of n) - if (!r.signIdx(this.key, o, ei)) + if (!r.signIdx(this.key, o, dc)) throw new Error(`Failed to sign input #${o}`); return r; } compressedPublicKey() { - return Promise.resolve(cc(this.key, !0)); + return Promise.resolve(ma(this.key, !0)); } xOnlyPublicKey() { - return Promise.resolve(Ho(this.key)); + return Promise.resolve(Rs(this.key)); } signerSession() { - return yn.random(); + return Dn.random(); } async signMessage(t, n = "schnorr") { - return n === "ecdsa" ? Vf(t, this.key, { prehash: !1 }) : Gf.sign(t, this.key); + return n === "ecdsa" ? Nd(t, this.key, { prehash: !1 }) : Dd.sign(t, this.key); } } -class qe { +class pn { constructor(t, n, r, o = 0) { if (this.serverPubKey = t, this.vtxoTaprootKey = n, this.hrp = r, this.version = o, t.length !== 32) throw new Error("Invalid server public key length, expected 32 bytes, got " + t.length); @@ -5715,42 +7354,42 @@ class qe { throw new Error("Invalid vtxo taproot public key length, expected 32 bytes, got " + n.length); } static decode(t) { - const n = Ne.decodeUnsafe(t, 1023); + const n = je.decodeUnsafe(t, 1023); if (!n) throw new Error("Invalid address"); - const r = new Uint8Array(Ne.fromWords(n.words)); + const r = new Uint8Array(je.fromWords(n.words)); if (r.length !== 65) throw new Error("Invalid data length, expected 65 bytes, got " + r.length); const o = r[0], s = r.slice(1, 33), i = r.slice(33, 65); - return new qe(s, i, n.prefix, o); + return new pn(s, i, n.prefix, o); } encode() { const t = new Uint8Array(65); t[0] = this.version, t.set(this.serverPubKey, 1), t.set(this.vtxoTaprootKey, 33); - const n = Ne.toWords(t); - return Ne.encode(this.hrp, n, 1023); + const n = je.toWords(t); + return je.encode(this.hrp, n, 1023); } // pkScript is the script that should be used to send non-dust funds to the address get pkScript() { - return L.encode(["OP_1", this.vtxoTaprootKey]); + return K.encode(["OP_1", this.vtxoTaprootKey]); } // subdustPkScript is the script that should be used to send sub-dust funds to the address get subdustPkScript() { - return L.encode(["RETURN", this.vtxoTaprootKey]); + return K.encode(["RETURN", this.vtxoTaprootKey]); } } -const fr = Fo(void 0, !0); -var it; +const Kr = Cs(void 0, !0); +var ft; (function(e) { e.Multisig = "multisig", e.CSVMultisig = "csv-multisig", e.ConditionCSVMultisig = "condition-csv-multisig", e.ConditionMultisig = "condition-multisig", e.CLTVMultisig = "cltv-multisig"; -})(it || (it = {})); -function Qc(e) { +})(ft || (ft = {})); +function Pu(e) { const t = [ - Dt, - kt, - mn, - lr, - xn + Gt, + $t, + Mn, + Fr, + Kn ]; for (const n of t) try { @@ -5758,9 +7397,9 @@ function Qc(e) { } catch { continue; } - throw new Error(`Failed to decode: script ${S.encode(e)} is not a valid tapscript`); + throw new Error(`Failed to decode: script ${$.encode(e)} is not a valid tapscript`); } -var Dt; +var Gt; (function(e) { let t; (function(c) { @@ -5774,17 +7413,17 @@ var Dt; throw new Error(`Invalid pubkey length: expected 32, got ${u.length}`); if (c.type || (c.type = t.CHECKSIG), c.type === t.CHECKSIGADD) return { - type: it.Multisig, + type: ft.Multisig, params: c, - script: ef(c.pubkeys.length, c.pubkeys).script + script: Ml(c.pubkeys.length, c.pubkeys).script }; const a = []; for (let u = 0; u < c.pubkeys.length; u++) a.push(c.pubkeys[u]), u < c.pubkeys.length - 1 ? a.push("CHECKSIGVERIFY") : a.push("CHECKSIG"); return { - type: it.Multisig, + type: ft.Multisig, params: c, - script: L.encode(a) + script: K.encode(a) }; } e.encode = n; @@ -5803,7 +7442,7 @@ var Dt; } e.decode = r; function o(c) { - const a = L.decode(c), u = []; + const a = K.decode(c), u = []; let f = !1; for (let d = 0; d < a.length; d++) { const h = a[d]; @@ -5829,16 +7468,16 @@ var Dt; pubkeys: u, type: t.CHECKSIGADD }); - if (S.encode(l.script) !== S.encode(c)) + if ($.encode(l.script) !== $.encode(c)) throw new Error("Invalid script format: script reconstruction mismatch"); return { - type: it.Multisig, + type: ft.Multisig, params: { pubkeys: u, type: t.CHECKSIGADD }, script: c }; } function s(c) { - const a = L.decode(c), u = []; + const a = K.decode(c), u = []; for (let l = 0; l < a.length; l++) { const d = a[l]; if (typeof d != "string" && typeof d != "number") { @@ -5858,35 +7497,35 @@ var Dt; if (u.length === 0) throw new Error("Invalid script: must have at least 1 pubkey"); const f = n({ pubkeys: u, type: t.CHECKSIG }); - if (S.encode(f.script) !== S.encode(c)) + if ($.encode(f.script) !== $.encode(c)) throw new Error("Invalid script format: script reconstruction mismatch"); return { - type: it.Multisig, + type: ft.Multisig, params: { pubkeys: u, type: t.CHECKSIG }, script: c }; } function i(c) { - return c.type === it.Multisig; + return c.type === ft.Multisig; } e.is = i; -})(Dt || (Dt = {})); -var kt; +})(Gt || (Gt = {})); +var $t; (function(e) { function t(o) { for (const u of o.pubkeys) if (u.length !== 32) throw new Error(`Invalid pubkey length: expected 32, got ${u.length}`); - const s = fr.encode(BigInt(go.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }))), i = [ + const s = Kr.encode(BigInt(cs.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }))), i = [ s.length === 1 ? s[0] : s, "CHECKSEQUENCEVERIFY", "DROP" - ], c = Dt.encode(o), a = new Uint8Array([ - ...L.encode(i), + ], c = Gt.encode(o), a = new Uint8Array([ + ...K.encode(i), ...c.script ]); return { - type: it.CSVMultisig, + type: ft.CSVMultisig, params: o, script: a }; @@ -5895,7 +7534,7 @@ var kt; function n(o) { if (o.length === 0) throw new Error("Failed to decode: script is empty"); - const s = L.decode(o); + const s = K.decode(o); if (s.length < 3) throw new Error("Invalid script: too short (expected at least 3)"); const i = s[0]; @@ -5903,23 +7542,23 @@ var kt; throw new Error("Invalid script: expected sequence number"); if (s[1] !== "CHECKSEQUENCEVERIFY" || s[2] !== "DROP") throw new Error("Invalid script: expected CHECKSEQUENCEVERIFY DROP"); - const c = new Uint8Array(L.encode(s.slice(3))); + const c = new Uint8Array(K.encode(s.slice(3))); let a; try { - a = Dt.decode(c); + a = Gt.decode(c); } catch (h) { throw new Error(`Invalid multisig script: ${h instanceof Error ? h.message : String(h)}`); } let u; - typeof i == "number" ? u = i : u = Number(fr.decode(i)); - const f = go.decode(u), l = f.blocks !== void 0 ? { type: "blocks", value: BigInt(f.blocks) } : { type: "seconds", value: BigInt(f.seconds) }, d = t({ + typeof i == "number" ? u = i : u = Number(Kr.decode(i)); + const f = cs.decode(u), l = f.blocks !== void 0 ? { type: "blocks", value: BigInt(f.blocks) } : { type: "seconds", value: BigInt(f.seconds) }, d = t({ timelock: l, ...a.params }); - if (S.encode(d.script) !== S.encode(o)) + if ($.encode(d.script) !== $.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { - type: it.CSVMultisig, + type: ft.CSVMultisig, params: { timelock: l, ...a.params @@ -5929,20 +7568,20 @@ var kt; } e.decode = n; function r(o) { - return o.type === it.CSVMultisig; + return o.type === ft.CSVMultisig; } e.is = r; -})(kt || (kt = {})); -var mn; +})($t || ($t = {})); +var Mn; (function(e) { function t(o) { const s = new Uint8Array([ ...o.conditionScript, - ...L.encode(["VERIFY"]), - ...kt.encode(o).script + ...K.encode(["VERIFY"]), + ...$t.encode(o).script ]); return { - type: it.ConditionCSVMultisig, + type: ft.ConditionCSVMultisig, params: o, script: s }; @@ -5951,7 +7590,7 @@ var mn; function n(o) { if (o.length === 0) throw new Error("Failed to decode: script is empty"); - const s = L.decode(o); + const s = K.decode(o); if (s.length < 1) throw new Error("Invalid script: too short (expected at least 1)"); let i = -1; @@ -5959,10 +7598,10 @@ var mn; s[l] === "VERIFY" && (i = l); if (i === -1) throw new Error("Invalid script: missing VERIFY operation"); - const c = new Uint8Array(L.encode(s.slice(0, i))), a = new Uint8Array(L.encode(s.slice(i + 1))); + const c = new Uint8Array(K.encode(s.slice(0, i))), a = new Uint8Array(K.encode(s.slice(i + 1))); let u; try { - u = kt.decode(a); + u = $t.decode(a); } catch (l) { throw new Error(`Invalid CSV multisig script: ${l instanceof Error ? l.message : String(l)}`); } @@ -5970,10 +7609,10 @@ var mn; conditionScript: c, ...u.params }); - if (S.encode(f.script) !== S.encode(o)) + if ($.encode(f.script) !== $.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { - type: it.ConditionCSVMultisig, + type: ft.ConditionCSVMultisig, params: { conditionScript: c, ...u.params @@ -5983,20 +7622,20 @@ var mn; } e.decode = n; function r(o) { - return o.type === it.ConditionCSVMultisig; + return o.type === ft.ConditionCSVMultisig; } e.is = r; -})(mn || (mn = {})); -var lr; +})(Mn || (Mn = {})); +var Fr; (function(e) { function t(o) { const s = new Uint8Array([ ...o.conditionScript, - ...L.encode(["VERIFY"]), - ...Dt.encode(o).script + ...K.encode(["VERIFY"]), + ...Gt.encode(o).script ]); return { - type: it.ConditionMultisig, + type: ft.ConditionMultisig, params: o, script: s }; @@ -6005,7 +7644,7 @@ var lr; function n(o) { if (o.length === 0) throw new Error("Failed to decode: script is empty"); - const s = L.decode(o); + const s = K.decode(o); if (s.length < 1) throw new Error("Invalid script: too short (expected at least 1)"); let i = -1; @@ -6013,10 +7652,10 @@ var lr; s[l] === "VERIFY" && (i = l); if (i === -1) throw new Error("Invalid script: missing VERIFY operation"); - const c = new Uint8Array(L.encode(s.slice(0, i))), a = new Uint8Array(L.encode(s.slice(i + 1))); + const c = new Uint8Array(K.encode(s.slice(0, i))), a = new Uint8Array(K.encode(s.slice(i + 1))); let u; try { - u = Dt.decode(a); + u = Gt.decode(a); } catch (l) { throw new Error(`Invalid multisig script: ${l instanceof Error ? l.message : String(l)}`); } @@ -6024,10 +7663,10 @@ var lr; conditionScript: c, ...u.params }); - if (S.encode(f.script) !== S.encode(o)) + if ($.encode(f.script) !== $.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { - type: it.ConditionMultisig, + type: ft.ConditionMultisig, params: { conditionScript: c, ...u.params @@ -6037,23 +7676,23 @@ var lr; } e.decode = n; function r(o) { - return o.type === it.ConditionMultisig; + return o.type === ft.ConditionMultisig; } e.is = r; -})(lr || (lr = {})); -var xn; +})(Fr || (Fr = {})); +var Kn; (function(e) { function t(o) { - const s = fr.encode(o.absoluteTimelock), i = [ + const s = Kr.encode(o.absoluteTimelock), i = [ s.length === 1 ? s[0] : s, "CHECKLOCKTIMEVERIFY", "DROP" - ], c = L.encode(i), a = new Uint8Array([ + ], c = K.encode(i), a = new Uint8Array([ ...c, - ...Dt.encode(o).script + ...Gt.encode(o).script ]); return { - type: it.CLTVMultisig, + type: ft.CLTVMultisig, params: o, script: a }; @@ -6062,7 +7701,7 @@ var xn; function n(o) { if (o.length === 0) throw new Error("Failed to decode: script is empty"); - const s = L.decode(o); + const s = K.decode(o); if (s.length < 3) throw new Error("Invalid script: too short (expected at least 3)"); const i = s[0]; @@ -6070,21 +7709,21 @@ var xn; throw new Error("Invalid script: expected locktime number"); if (s[1] !== "CHECKLOCKTIMEVERIFY" || s[2] !== "DROP") throw new Error("Invalid script: expected CHECKLOCKTIMEVERIFY DROP"); - const c = new Uint8Array(L.encode(s.slice(3))); + const c = new Uint8Array(K.encode(s.slice(3))); let a; try { - a = Dt.decode(c); + a = Gt.decode(c); } catch (l) { throw new Error(`Invalid multisig script: ${l instanceof Error ? l.message : String(l)}`); } - const u = fr.decode(i), f = t({ + const u = Kr.decode(i), f = t({ absoluteTimelock: u, ...a.params }); - if (S.encode(f.script) !== S.encode(o)) + if ($.encode(f.script) !== $.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { - type: it.CLTVMultisig, + type: ft.CLTVMultisig, params: { absoluteTimelock: u, ...a.params @@ -6094,50 +7733,50 @@ var xn; } e.decode = n; function r(o) { - return o.type === it.CLTVMultisig; + return o.type === ft.CLTVMultisig; } e.is = r; -})(xn || (xn = {})); -const ni = hn.tapTree[2]; -function fn(e) { +})(Kn || (Kn = {})); +const hc = _n.tapTree[2]; +function Nn(e) { return e[1].subarray(0, e[1].length - 1); } -class Ut { +class _t { static decode(t) { - const r = ni.decode(t).map((o) => o.script); - return new Ut(r); + const r = hc.decode(t).map((o) => o.script); + return new _t(r); } constructor(t) { this.scripts = t; - const n = t.length % 2 !== 0 ? t.slice().reverse() : t, r = gc(n.map((s) => ({ + const n = t.length % 2 !== 0 ? t.slice().reverse() : t, r = Ia(n.map((s) => ({ script: s, - leafVersion: pn - }))), o = tf(Ko, r, void 0, !0); + leafVersion: Pn + }))), o = Dl(Ls, r, void 0, !0); if (!o.tapLeafScript || o.tapLeafScript.length !== t.length) throw new Error("invalid scripts"); this.leaves = o.tapLeafScript, this.tweakedPublicKey = o.tweakedPubkey; } encode() { - return ni.encode(this.scripts.map((n) => ({ + return hc.encode(this.scripts.map((n) => ({ depth: 1, - version: pn, + version: Pn, script: n }))); } address(t, n) { - return new qe(n, this.tweakedPublicKey, t); + return new pn(n, this.tweakedPublicKey, t); } get pkScript() { - return L.encode(["OP_1", this.tweakedPublicKey]); + return K.encode(["OP_1", this.tweakedPublicKey]); } onchainAddress(t) { - return ke(t).encode({ + return Me(t).encode({ type: "tr", pubkey: this.tweakedPublicKey }); } findLeaf(t) { - const n = this.leaves.find((r) => S.encode(fn(r)) === t); + const n = this.leaves.find((r) => $.encode(Nn(r)) === t); if (!n) throw new Error(`leaf '${t}' not found`); return n; @@ -6146,12 +7785,12 @@ class Ut { const t = []; for (const n of this.leaves) try { - const r = kt.decode(fn(n)); + const r = $t.decode(Nn(n)); t.push(r); continue; } catch { try { - const o = mn.decode(fn(n)); + const o = Mn.decode(Nn(n)); t.push(o); } catch { continue; @@ -6160,38 +7799,38 @@ class Ut { return t; } } -var ri; +var pc; (function(e) { - class t extends Ut { + class t extends _t { constructor(o) { n(o); - const { sender: s, receiver: i, server: c, preimageHash: a, refundLocktime: u, unilateralClaimDelay: f, unilateralRefundDelay: l, unilateralRefundWithoutReceiverDelay: d } = o, h = tl(a), w = lr.encode({ + const { sender: s, receiver: i, server: c, preimageHash: a, refundLocktime: u, unilateralClaimDelay: f, unilateralRefundDelay: l, unilateralRefundWithoutReceiverDelay: d } = o, h = Th(a), w = Fr.encode({ conditionScript: h, pubkeys: [i, c] - }).script, p = Dt.encode({ + }).script, g = Gt.encode({ pubkeys: [s, i, c] - }).script, m = xn.encode({ + }).script, y = Kn.encode({ absoluteTimelock: u, pubkeys: [s, c] - }).script, x = mn.encode({ + }).script, S = Mn.encode({ conditionScript: h, timelock: f, pubkeys: [i] - }).script, b = kt.encode({ + }).script, v = $t.encode({ timelock: l, pubkeys: [s, i] - }).script, v = kt.encode({ + }).script, O = $t.encode({ timelock: d, pubkeys: [s] }).script; super([ w, - p, - m, - x, - b, - v - ]), this.options = o, this.claimScript = S.encode(w), this.refundScript = S.encode(p), this.refundWithoutReceiverScript = S.encode(m), this.unilateralClaimScript = S.encode(x), this.unilateralRefundScript = S.encode(b), this.unilateralRefundWithoutReceiverScript = S.encode(v); + g, + y, + S, + v, + O + ]), this.options = o, this.claimScript = $.encode(w), this.refundScript = $.encode(g), this.refundWithoutReceiverScript = $.encode(y), this.unilateralClaimScript = $.encode(S), this.unilateralRefundScript = $.encode(v), this.unilateralRefundWithoutReceiverScript = $.encode(O); } claim() { return this.findLeaf(this.claimScript); @@ -6244,21 +7883,21 @@ var ri; if (l.type === "seconds" && l.value < 512n) throw new Error("seconds timelock must be greater or equal to 512"); } -})(ri || (ri = {})); -function tl(e) { - return L.encode(["HASH160", e, "EQUAL"]); +})(pc || (pc = {})); +function Th(e) { + return K.encode(["HASH160", e, "EQUAL"]); } -var dr; +var Wr; (function(e) { - class t extends Ut { + class t extends _t { constructor(r) { - const { pubKey: o, serverPubKey: s, csvTimelock: i = t.DEFAULT_TIMELOCK } = r, c = Dt.encode({ + const { pubKey: o, serverPubKey: s, csvTimelock: i = t.DEFAULT_TIMELOCK } = r, c = Gt.encode({ pubkeys: [o, s] - }).script, a = kt.encode({ + }).script, a = $t.encode({ timelock: i, pubkeys: [o] }).script; - super([c, a]), this.options = r, this.forfeitScript = S.encode(c), this.exitScript = S.encode(a); + super([c, a]), this.options = r, this.forfeitScript = $.encode(c), this.exitScript = $.encode(a); } forfeit() { return this.findLeaf(this.forfeitScript); @@ -6271,34 +7910,34 @@ var dr; value: 144n, type: "blocks" }, e.Script = t; -})(dr || (dr = {})); -var bn; +})(Wr || (Wr = {})); +var Fn; (function(e) { e.TxSent = "SENT", e.TxReceived = "RECEIVED"; -})(bn || (bn = {})); -function te(e) { +})(Fn || (Fn = {})); +function ae(e) { return !e.isSpent; } -function mo(e) { - return e.virtualStatus.state === "swept" && te(e); +function fs(e) { + return e.virtualStatus.state === "swept" && ae(e); } -function Jc(e, t) { +function Hu(e, t) { return e.value < t; } -function ta(e, t, n) { +function Vu(e, t, n) { const r = []; let o = [...t]; for (const i of [...e, ...t]) { if (i.virtualStatus.state !== "preconfirmed" && i.virtualStatus.commitmentTxIds && i.virtualStatus.commitmentTxIds.some((h) => n.has(h))) continue; - const c = el(o, i); - o = oi(o, c); - const a = _n(c); + const c = Ah(o, i); + o = gc(o, c); + const a = cr(c); if (i.value <= a) continue; - const u = nl(o, i); - o = oi(o, u); - const f = _n(u); + const u = Ih(o, i); + o = gc(o, u); + const f = cr(u); if (i.value <= f) continue; const l = { @@ -6310,7 +7949,7 @@ function ta(e, t, n) { i.virtualStatus.state === "preconfirmed" && (l.arkTxid = i.txid, i.spentBy && (d = !0)), r.push({ key: l, amount: i.value - a - f, - type: bn.TxReceived, + type: Fn.TxReceived, createdAt: i.createdAt.getTime(), settled: d }); @@ -6329,10 +7968,10 @@ function ta(e, t, n) { s.set(i.arkTxId, [...c, i]); } for (const [i, c] of s) { - const a = rl([...e, ...t], i), u = _n(a), f = _n(c); + const a = kh([...e, ...t], i), u = cr(a), f = cr(c); if (f <= u) continue; - const l = ol(a, c), d = { + const l = Bh(a, c), d = { commitmentTxid: l.virtualStatus.commitmentTxIds?.[0] || "", boardingTxid: "", arkTxid: "" @@ -6340,29 +7979,29 @@ function ta(e, t, n) { l.virtualStatus.state === "preconfirmed" && (d.arkTxid = l.txid), r.push({ key: d, amount: f - u, - type: bn.TxSent, + type: Fn.TxSent, createdAt: l.createdAt.getTime(), settled: !0 }); } return r; } -function el(e, t) { +function Ah(e, t) { return t.virtualStatus.state === "preconfirmed" ? [] : e.filter((n) => n.settledBy ? t.virtualStatus.commitmentTxIds?.includes(n.settledBy) ?? !1 : !1); } -function nl(e, t) { +function Ih(e, t) { return e.filter((n) => n.arkTxId ? n.arkTxId === t.txid : !1); } -function rl(e, t) { +function kh(e, t) { return e.filter((n) => n.virtualStatus.state !== "preconfirmed" && n.virtualStatus.commitmentTxIds?.includes(t) ? !0 : n.txid === t); } -function _n(e) { +function cr(e) { return e.reduce((t, n) => t + n.value, 0); } -function ol(e, t) { +function Bh(e, t) { return e.length === 0 ? t[0] : e[0]; } -function oi(e, t) { +function gc(e, t) { return e.filter((n) => { for (const r of t) if (n.txid === r.txid && n.vout === r.vout) @@ -6370,32 +8009,32 @@ function oi(e, t) { return !0; }); } -const sl = (e) => il[e], il = { - bitcoin: rn(We, "ark"), - testnet: rn(Rn, "tark"), - signet: rn(Rn, "tark"), - mutinynet: rn(Rn, "tark"), - regtest: rn({ - ...Rn, +const Oh = (e) => $h[e], $h = { + bitcoin: In(un, "ark"), + testnet: In(tr, "tark"), + signet: In(tr, "tark"), + mutinynet: In(tr, "tark"), + regtest: In({ + ...tr, bech32: "bcrt", pubKeyHash: 111, scriptHash: 196 }, "tark") }; -function rn(e, t) { +function In(e, t) { return { ...e, hrp: t }; } -const cl = { +const Uh = { bitcoin: "https://mempool.space/api", testnet: "https://mempool.space/testnet/api", signet: "https://mempool.space/signet/api", mutinynet: "https://mutinynet.com/api", regtest: "http://localhost:3000" }; -class al { +class Rh { constructor(t, n) { this.baseUrl = t, this.pollingInterval = n?.pollingInterval ?? 15e3, this.forcePolling = n?.forcePolling ?? !1; } @@ -6490,7 +8129,7 @@ class al { "confirmed", "removed" ]) - l[d][h] && u.push(...l[d][h].filter(fl)); + l[d][h] && u.push(...l[d][h].filter(Lh)); u.length > 0 && n(u); } catch (u) { console.error("Failed to process WebSocket message:", u); @@ -6508,7 +8147,7 @@ class al { if (!t.ok) throw new Error(`Failed to get chain tip: ${t.statusText}`); const n = await t.json(); - if (!ul(n)) + if (!Nh(n)) throw new Error(`Invalid chain tip: ${JSON.stringify(n)}`); if (n.length === 0) throw new Error("No chain tip found"); @@ -6548,13 +8187,13 @@ class al { return n.text(); } } -function ul(e) { +function Nh(e) { return Array.isArray(e) && e.every((t) => { t && typeof t == "object" && typeof t.id == "string" && t.id.length > 0 && typeof t.height == "number" && t.height >= 0 && typeof t.mediantime == "number" && t.mediantime > 0; }); } -const fl = (e) => typeof e.txid == "string" && Array.isArray(e.vout) && e.vout.every((t) => typeof t.scriptpubkey_address == "string" && typeof t.value == "number") && typeof e.status == "object" && typeof e.status.confirmed == "boolean"; -async function* xo(e) { +const Lh = (e) => typeof e.txid == "string" && Array.isArray(e.vout) && e.vout.every((t) => typeof t.scriptpubkey_address == "string" && typeof t.value == "number") && typeof e.status == "object" && typeof e.status.confirmed == "boolean"; +async function* ls(e) { const t = [], n = []; let r = null, o = null; const s = (c) => { @@ -6583,12 +8222,12 @@ async function* xo(e) { e.removeEventListener("message", s), e.removeEventListener("error", i); } } -class ll extends Error { +class Ch extends Error { constructor(t, n, r, o) { super(n), this.code = t, this.message = n, this.name = r, this.metadata = o; } } -function dl(e) { +function _h(e) { try { if (!(e instanceof Error)) return; @@ -6606,21 +8245,21 @@ function dl(e) { continue; const i = n.name; let c; - return "metadata" in n && hl(n.metadata) && (c = n.metadata), new ll(o, s, i, c); + return "metadata" in n && Ph(n.metadata) && (c = n.metadata), new Ch(o, s, i, c); } return; } catch { return; } } -function hl(e) { +function Ph(e) { return typeof e == "object" && e !== null && !Array.isArray(e); } -var z; +var J; (function(e) { e.BatchStarted = "batch_started", e.BatchFinalization = "batch_finalization", e.BatchFinalized = "batch_finalized", e.BatchFailed = "batch_failed", e.TreeSigningStarted = "tree_signing_started", e.TreeNonces = "tree_nonces", e.TreeTx = "tree_tx", e.TreeSignature = "tree_signature"; -})(z || (z = {})); -class ea { +})(J || (J = {})); +class Du { constructor(t) { this.serverUrl = t; } @@ -6628,7 +8267,7 @@ class ea { const t = `${this.serverUrl}/v1/info`, n = await fetch(t); if (!n.ok) { const o = await n.text(); - Kt(o, `Failed to get server info: ${n.statusText}`); + Yt(o, `Failed to get server info: ${n.statusText}`); } const r = await n.json(); return { @@ -6674,7 +8313,7 @@ class ea { }); if (!o.ok) { const i = await o.text(); - Kt(i, `Failed to submit virtual transaction: ${i}`); + Yt(i, `Failed to submit virtual transaction: ${i}`); } const s = await o.json(); return { @@ -6696,7 +8335,7 @@ class ea { }); if (!o.ok) { const s = await o.text(); - Kt(s, `Failed to finalize offchain transaction: ${s}`); + Yt(s, `Failed to finalize offchain transaction: ${s}`); } } async registerIntent(t) { @@ -6714,7 +8353,7 @@ class ea { }); if (!r.ok) { const s = await r.text(); - Kt(s, `Failed to register intent: ${s}`); + Yt(s, `Failed to register intent: ${s}`); } return (await r.json()).intentId; } @@ -6733,7 +8372,7 @@ class ea { }); if (!r.ok) { const o = await r.text(); - Kt(o, `Failed to delete intent: ${o}`); + Yt(o, `Failed to delete intent: ${o}`); } } async confirmRegistration(t) { @@ -6748,7 +8387,7 @@ class ea { }); if (!r.ok) { const o = await r.text(); - Kt(o, `Failed to confirm registration: ${o}`); + Yt(o, `Failed to confirm registration: ${o}`); } } async submitTreeNonces(t, n, r) { @@ -6760,12 +8399,12 @@ class ea { body: JSON.stringify({ batchId: t, pubkey: n, - treeNonces: pl(r) + treeNonces: Hh(r) }) }); if (!s.ok) { const i = await s.text(); - Kt(i, `Failed to submit tree nonces: ${i}`); + Yt(i, `Failed to submit tree nonces: ${i}`); } } async submitTreeSignatures(t, n, r) { @@ -6777,12 +8416,12 @@ class ea { body: JSON.stringify({ batchId: t, pubkey: n, - treeSignatures: gl(r) + treeSignatures: Vh(r) }) }); if (!s.ok) { const i = await s.text(); - Kt(i, `Failed to submit tree signatures: ${i}`); + Yt(i, `Failed to submit tree signatures: ${i}`); } } async submitSignedForfeitTxs(t, n) { @@ -6798,7 +8437,7 @@ class ea { }); if (!o.ok) { const s = await o.text(); - Kt(s, `Failed to submit forfeit transactions: ${o.statusText}`); + Yt(s, `Failed to submit forfeit transactions: ${o.statusText}`); } } async *getEventStream(t, n) { @@ -6810,7 +8449,7 @@ class ea { }; t?.addEventListener("abort", i); try { - for await (const c of xo(s)) { + for await (const c of ls(s)) { if (t?.aborted) break; try { @@ -6826,7 +8465,7 @@ class ea { } catch (s) { if (s instanceof Error && s.name === "AbortError") break; - if (bo(s)) { + if (ds(s)) { console.debug("Timeout error ignored"); continue; } @@ -6842,7 +8481,7 @@ class ea { }; t?.addEventListener("abort", o); try { - for await (const s of xo(r)) { + for await (const s of ls(r)) { if (t?.aborted) break; try { @@ -6858,7 +8497,7 @@ class ea { } catch (r) { if (r instanceof Error && r.name === "AbortError") break; - if (bo(r)) { + if (ds(r)) { console.debug("Timeout error ignored"); continue; } @@ -6875,39 +8514,39 @@ class ea { }); if (!r.ok) { const s = await r.text(); - Kt(s, `Failed to get pending transactions: ${s}`); + Yt(s, `Failed to get pending transactions: ${s}`); } return (await r.json()).pendingTxs; } parseSettlementEvent(t) { if (t.batchStarted) return { - type: z.BatchStarted, + type: J.BatchStarted, id: t.batchStarted.id, intentIdHashes: t.batchStarted.intentIdHashes, batchExpiry: BigInt(t.batchStarted.batchExpiry) }; if (t.batchFinalization) return { - type: z.BatchFinalization, + type: J.BatchFinalization, id: t.batchFinalization.id, commitmentTx: t.batchFinalization.commitmentTx }; if (t.batchFinalized) return { - type: z.BatchFinalized, + type: J.BatchFinalized, id: t.batchFinalized.id, commitmentTxid: t.batchFinalized.commitmentTxid }; if (t.batchFailed) return { - type: z.BatchFailed, + type: J.BatchFailed, id: t.batchFailed.id, reason: t.batchFailed.reason }; if (t.treeSigningStarted) return { - type: z.TreeSigningStarted, + type: J.TreeSigningStarted, id: t.treeSigningStarted.id, cosignersPublicKeys: t.treeSigningStarted.cosignersPubkeys, unsignedCommitmentTx: t.treeSigningStarted.unsignedCommitmentTx @@ -6916,17 +8555,17 @@ class ea { return null; if (t.treeNonces) return { - type: z.TreeNonces, + type: J.TreeNonces, id: t.treeNonces.id, topic: t.treeNonces.topic, txid: t.treeNonces.txid, - nonces: wl(t.treeNonces.nonces) + nonces: Dh(t.treeNonces.nonces) // pubkey -> public nonce }; if (t.treeTx) { const n = Object.fromEntries(Object.entries(t.treeTx.children).map(([r, o]) => [parseInt(r), o])); return { - type: z.TreeTx, + type: J.TreeTx, id: t.treeTx.id, topic: t.treeTx.topic, batchIndex: t.treeTx.batchIndex, @@ -6938,7 +8577,7 @@ class ea { }; } return t.treeSignature ? { - type: z.TreeSignature, + type: J.TreeSignature, id: t.treeSignature.id, topic: t.treeSignature.topic, batchIndex: t.treeSignature.batchIndex, @@ -6951,45 +8590,45 @@ class ea { commitmentTx: { txid: t.commitmentTx.txid, tx: t.commitmentTx.tx, - spentVtxos: t.commitmentTx.spentVtxos.map(Vn), - spendableVtxos: t.commitmentTx.spendableVtxos.map(Vn), + spentVtxos: t.commitmentTx.spentVtxos.map(ar), + spendableVtxos: t.commitmentTx.spendableVtxos.map(ar), checkpointTxs: t.commitmentTx.checkpointTxs } } : t.arkTx ? { arkTx: { txid: t.arkTx.txid, tx: t.arkTx.tx, - spentVtxos: t.arkTx.spentVtxos.map(Vn), - spendableVtxos: t.arkTx.spendableVtxos.map(Vn), + spentVtxos: t.arkTx.spentVtxos.map(ar), + spendableVtxos: t.arkTx.spendableVtxos.map(ar), checkpointTxs: t.arkTx.checkpointTxs } } : (t.heartbeat || console.warn("Unknown transaction notification type:", t), null); } } -function pl(e) { +function Hh(e) { const t = {}; for (const [n, r] of e) - t[n] = S.encode(r.pubNonce); + t[n] = $.encode(r.pubNonce); return t; } -function gl(e) { +function Vh(e) { const t = {}; for (const [n, r] of e) - t[n] = S.encode(r.encode()); + t[n] = $.encode(r.encode()); return t; } -function wl(e) { +function Dh(e) { return new Map(Object.entries(e).map(([t, n]) => { if (typeof n != "string") throw new Error("invalid nonce"); - return [t, { pubNonce: S.decode(n) }]; + return [t, { pubNonce: $.decode(n) }]; })); } -function bo(e) { +function ds(e) { const t = (n) => n instanceof Error ? n.name === "TypeError" && n.message === "Failed to fetch" || n.name === "HeadersTimeoutError" || n.name === "BodyTimeoutError" || n.code === "UND_ERR_HEADERS_TIMEOUT" || n.code === "UND_ERR_BODY_TIMEOUT" : !1; return t(e) || t(e.cause); } -function Vn(e) { +function ar(e) { return { outpoint: { txid: e.outpoint.txid, @@ -7009,17 +8648,17 @@ function Vn(e) { arkTxid: e.arkTxid }; } -function Kt(e, t) { +function Yt(e, t) { const n = new Error(e); - throw dl(n) ?? new Error(t); + throw _h(n) ?? new Error(t); } -const yl = 0n, ml = new Uint8Array([81, 2, 78, 115]), ss = { - script: ml, - amount: yl +const Mh = 0n, Kh = new Uint8Array([81, 2, 78, 115]), ii = { + script: Kh, + amount: Mh }; -S.encode(ss.script); -function xl(e, t, n) { - const r = new he({ +$.encode(ii.script); +function Fh(e, t, n) { + const r = new ke({ version: 3, lockTime: n }); @@ -7032,39 +8671,39 @@ function xl(e, t, n) { return r.addOutput({ script: t, amount: o - }), r.addOutput(ss), r; + }), r.addOutput(ii), r; } -const bl = new Error("invalid settlement transaction outputs"), El = new Error("empty tree"), Sl = new Error("invalid number of inputs"), Kr = new Error("wrong settlement txid"), Tl = new Error("invalid amount"), vl = new Error("no leaves"), kl = new Error("invalid taproot script"), si = new Error("invalid round transaction outputs"), Al = new Error("wrong commitment txid"), Il = new Error("missing cosigners public keys"), Fr = 0, ii = 1; -function Bl(e, t) { +const Wh = new Error("invalid settlement transaction outputs"), zh = new Error("empty tree"), Gh = new Error("invalid number of inputs"), $o = new Error("wrong settlement txid"), qh = new Error("invalid amount"), jh = new Error("no leaves"), Yh = new Error("invalid taproot script"), wc = new Error("invalid round transaction outputs"), Zh = new Error("wrong commitment txid"), Xh = new Error("missing cosigners public keys"), Uo = 0, yc = 1; +function Qh(e, t) { if (t.validate(), t.root.inputsLength !== 1) - throw Sl; - const n = t.root.getInput(0), r = ee.fromPSBT(Et.decode(e)); - if (r.outputsLength <= ii) - throw bl; + throw Gh; + const n = t.root.getInput(0), r = ue.fromPSBT(It.decode(e)); + if (r.outputsLength <= yc) + throw Wh; const o = r.id; - if (!n.txid || S.encode(n.txid) !== o || n.index !== ii) - throw Kr; + if (!n.txid || $.encode(n.txid) !== o || n.index !== yc) + throw $o; } -function Ol(e, t, n) { - if (t.outputsLength < Fr + 1) - throw si; - const r = t.getOutput(Fr)?.amount; +function Jh(e, t, n) { + if (t.outputsLength < Uo + 1) + throw wc; + const r = t.getOutput(Uo)?.amount; if (!r) - throw si; + throw wc; if (!e.root) - throw El; + throw zh; const o = e.root.getInput(0), s = t.id; - if (!o.txid || S.encode(o.txid) !== s || o.index !== Fr) - throw Al; + if (!o.txid || $.encode(o.txid) !== s || o.index !== Uo) + throw Zh; let i = 0n; for (let a = 0; a < e.root.outputsLength; a++) { const u = e.root.getOutput(a); u?.amount && (i += u.amount); } if (i !== r) - throw Tl; + throw qh; if (e.leaves().length === 0) - throw vl; + throw jh; e.validate(); for (const a of e.iterator()) for (const [u, f] of a.children) { @@ -7074,34 +8713,34 @@ function Ol(e, t, n) { const d = l.script.slice(2); if (d.length !== 32) throw new Error(`parent output ${u} has invalid script`); - const h = wo(f.root, 0, yo); + const h = as(f.root, 0, us); if (h.length === 0) - throw Il; - const w = h.map((m) => m.key), { finalKey: p } = ts(w, !0, { + throw Xh; + const w = h.map((y) => y.key), { finalKey: g } = ei(w, !0, { taprootTweak: n }); - if (!p || S.encode(p.slice(1)) !== S.encode(d)) - throw kl; + if (!g || $.encode(g.slice(1)) !== $.encode(d)) + throw Yh; } } -function Ul(e, t, n) { - const r = e.map((s) => Nl(s, n)); +function tp(e, t, n) { + const r = e.map((s) => ep(s, n)); return { - arkTx: na(r.map((s) => s.input), t), + arkTx: Mu(r.map((s) => s.input), t), checkpoints: r.map((s) => s.tx) }; } -function na(e, t) { +function Mu(e, t) { let n = 0n; for (const o of e) { - const s = Qc(fn(o.tapLeafScript)); - if (xn.is(s)) { - if (n !== 0n && ci(n) !== ci(s.params.absoluteTimelock)) + const s = Pu(Nn(o.tapLeafScript)); + if (Kn.is(s)) { + if (n !== 0n && mc(n) !== mc(s.params.absoluteTimelock)) throw new Error("cannot mix seconds and blocks locktime"); s.params.absoluteTimelock > n && (n = s.params.absoluteTimelock); } } - const r = new he({ + const r = new ke({ version: 3, lockTime: Number(n) }); @@ -7109,27 +8748,27 @@ function na(e, t) { r.addInput({ txid: s.txid, index: s.vout, - sequence: n ? qo - 1 : void 0, + sequence: n ? Ds - 1 : void 0, witnessUtxo: { - script: Ut.decode(s.tapTree).pkScript, + script: _t.decode(s.tapTree).pkScript, amount: BigInt(s.value) }, tapLeafScript: [s.tapLeafScript] - }), Xf(r, o, Xc, s.tapTree); + }), xh(r, o, _u, s.tapTree); for (const o of t) r.addOutput(o); - return r.addOutput(ss), r; + return r.addOutput(ii), r; } -function Nl(e, t) { - const n = Qc(fn(e.tapLeafScript)), r = new Ut([ +function ep(e, t) { + const n = Pu(Nn(e.tapLeafScript)), r = new _t([ t.script, n.script - ]), o = na([e], [ + ]), o = Mu([e], [ { amount: BigInt(e.value), script: r.pkScript } - ]), s = r.findLeaf(S.encode(n.script)), i = { + ]), s = r.findLeaf($.encode(n.script)), i = { txid: o.id, vout: 0, value: e.value, @@ -7141,11 +8780,11 @@ function Nl(e, t) { input: i }; } -const Rl = 500000000n; -function ci(e) { - return e >= Rl; +const np = 500000000n; +function mc(e) { + return e >= np; } -function Cl(e, t) { +function rp(e, t) { if (!e.status.block_time) return !1; if (t.value === 0n) @@ -7155,66 +8794,66 @@ function Cl(e, t) { const n = BigInt(Math.floor(Date.now() / 1e3)); return BigInt(Math.floor(e.status.block_time)) + t.value <= n; } -const $l = { +const op = { thresholdPercentage: 10 }; -class ot { - constructor(t, n, r = ot.DefaultHRP) { +class at { + constructor(t, n, r = at.DefaultHRP) { this.preimage = t, this.value = n, this.HRP = r, this.vout = 0; - const o = ht(this.preimage); - this.vtxoScript = new Ut([_l(o)]); + const o = wt(this.preimage); + this.vtxoScript = new _t([cp(o)]); const s = this.vtxoScript.leaves[0]; - this.txid = S.encode(new Uint8Array(o).reverse()), this.tapTree = this.vtxoScript.encode(), this.forfeitTapLeafScript = s, this.intentTapLeafScript = s, this.value = n, this.status = { confirmed: !0 }, this.extraWitness = [this.preimage]; + this.txid = $.encode(new Uint8Array(o).reverse()), this.tapTree = this.vtxoScript.encode(), this.forfeitTapLeafScript = s, this.intentTapLeafScript = s, this.value = n, this.status = { confirmed: !0 }, this.extraWitness = [this.preimage]; } encode() { - const t = new Uint8Array(ot.Length); - return t.set(this.preimage, 0), Ll(t, this.value, this.preimage.length), t; + const t = new Uint8Array(at.Length); + return t.set(this.preimage, 0), sp(t, this.value, this.preimage.length), t; } - static decode(t, n = ot.DefaultHRP) { - if (t.length !== ot.Length) - throw new Error(`invalid data length: expected ${ot.Length} bytes, got ${t.length}`); - const r = t.subarray(0, ot.PreimageLength), o = Pl(t, ot.PreimageLength); - return new ot(r, o, n); + static decode(t, n = at.DefaultHRP) { + if (t.length !== at.Length) + throw new Error(`invalid data length: expected ${at.Length} bytes, got ${t.length}`); + const r = t.subarray(0, at.PreimageLength), o = ip(t, at.PreimageLength); + return new at(r, o, n); } - static fromString(t, n = ot.DefaultHRP) { + static fromString(t, n = at.DefaultHRP) { if (t = t.trim(), !t.startsWith(n)) throw new Error(`invalid human-readable part: expected ${n} prefix (note '${t}')`); - const r = t.slice(n.length), o = Yr.decode(r); + const r = t.slice(n.length), o = Po.decode(r); if (o.length === 0) throw new Error("failed to decode base58 string"); - return ot.decode(o, n); + return at.decode(o, n); } toString() { - return this.HRP + Yr.encode(this.encode()); + return this.HRP + Po.encode(this.encode()); } } -ot.DefaultHRP = "arknote"; -ot.PreimageLength = 32; -ot.ValueLength = 4; -ot.Length = ot.PreimageLength + ot.ValueLength; -ot.FakeOutpointIndex = 0; -function Ll(e, t, n) { +at.DefaultHRP = "arknote"; +at.PreimageLength = 32; +at.ValueLength = 4; +at.Length = at.PreimageLength + at.ValueLength; +at.FakeOutpointIndex = 0; +function sp(e, t, n) { new DataView(e.buffer, e.byteOffset + n, 4).setUint32(0, t, !1); } -function Pl(e, t) { +function ip(e, t) { return new DataView(e.buffer, e.byteOffset + t, 4).getUint32(0, !1); } -function _l(e) { - return L.encode(["SHA256", e, "EQUAL"]); +function cp(e) { + return K.encode(["SHA256", e, "EQUAL"]); } -var hr; +var zr; (function(e) { function t(n, r, o = []) { if (r.length == 0) throw new Error("intent proof requires at least one input"); - Fl(r), Gl(o); - const s = zl(n, r[0].witnessUtxo.script); - return jl(s, r, o); + hp(r), gp(o); + const s = wp(n, r[0].witnessUtxo.script); + return yp(s, r, o); } e.create = t; -})(hr || (hr = {})); -const Vl = new Uint8Array([ct.RETURN]), Dl = new Uint8Array(32).fill(0), Hl = 4294967295, Ml = "ark-intent-proof-message"; -function Kl(e) { +})(zr || (zr = {})); +const ap = new Uint8Array([lt.RETURN]), up = new Uint8Array(32).fill(0), fp = 4294967295, lp = "ark-intent-proof-message"; +function dp(e) { if (e.index === void 0) throw new Error("intent proof input requires index"); if (e.txid === void 0) @@ -7223,37 +8862,37 @@ function Kl(e) { throw new Error("intent proof input requires witness utxo"); return !0; } -function Fl(e) { - return e.forEach(Kl), !0; +function hp(e) { + return e.forEach(dp), !0; } -function Wl(e) { +function pp(e) { if (e.amount === void 0) throw new Error("intent proof output requires amount"); if (e.script === void 0) throw new Error("intent proof output requires script"); return !0; } -function Gl(e) { - return e.forEach(Wl), !0; +function gp(e) { + return e.forEach(pp), !0; } -function zl(e, t) { - const n = ql(e), r = new he({ +function wp(e, t) { + const n = mp(e), r = new ke({ version: 0 }); return r.addInput({ - txid: Dl, + txid: up, // zero hash - index: Hl, + index: fp, sequence: 0 }), r.addOutput({ amount: 0n, script: t }), r.updateInput(0, { - finalScriptSig: L.encode(["OP_0", n]) + finalScriptSig: K.encode(["OP_0", n]) }), r; } -function jl(e, t, n) { - const r = t[0], o = new he({ +function yp(e, t, n) { + const r = t[0], o = new ke({ version: 2, lockTime: 0 }); @@ -7265,19 +8904,19 @@ function jl(e, t, n) { script: r.witnessUtxo.script, amount: 0n }, - sighashType: Ae.ALL + sighashType: Ke.ALL }); for (const [s, i] of t.entries()) o.addInput({ ...i, - sighashType: Ae.ALL + sighashType: Ke.ALL }), i.unknown?.length && o.updateInput(s + 1, { unknown: i.unknown }); n.length === 0 && (n = [ { amount: 0n, - script: Vl + script: ap } ]); for (const s of n) @@ -7287,18 +8926,18 @@ function jl(e, t, n) { }); return o; } -function ql(e) { - return Nt.utils.taggedHash(Ml, new TextEncoder().encode(e)); +function mp(e) { + return ti.utils.taggedHash(lp, new TextEncoder().encode(e)); } -var Eo; +var hs; (function(e) { e[e.INDEXER_TX_TYPE_UNSPECIFIED = 0] = "INDEXER_TX_TYPE_UNSPECIFIED", e[e.INDEXER_TX_TYPE_RECEIVED = 1] = "INDEXER_TX_TYPE_RECEIVED", e[e.INDEXER_TX_TYPE_SENT = 2] = "INDEXER_TX_TYPE_SENT"; -})(Eo || (Eo = {})); -var _e; +})(hs || (hs = {})); +var en; (function(e) { e.UNSPECIFIED = "INDEXER_CHAINED_TX_TYPE_UNSPECIFIED", e.COMMITMENT = "INDEXER_CHAINED_TX_TYPE_COMMITMENT", e.ARK = "INDEXER_CHAINED_TX_TYPE_ARK", e.TREE = "INDEXER_CHAINED_TX_TYPE_TREE", e.CHECKPOINT = "INDEXER_CHAINED_TX_TYPE_CHECKPOINT"; -})(_e || (_e = {})); -class ra { +})(en || (en = {})); +class Ku { constructor(t) { this.serverUrl = t; } @@ -7310,7 +8949,7 @@ class ra { if (!s.ok) throw new Error(`Failed to fetch vtxo tree: ${s.statusText}`); const i = await s.json(); - if (!Rt.isVtxoTreeResponse(i)) + if (!Ht.isVtxoTreeResponse(i)) throw new Error("Invalid vtxo tree data received"); return i.vtxoTree.forEach((c) => { c.children = Object.fromEntries(Object.entries(c.children).map(([a, u]) => [ @@ -7327,7 +8966,7 @@ class ra { if (!s.ok) throw new Error(`Failed to fetch vtxo tree leaves: ${s.statusText}`); const i = await s.json(); - if (!Rt.isVtxoTreeLeavesResponse(i)) + if (!Ht.isVtxoTreeLeavesResponse(i)) throw new Error("Invalid vtxos tree leaves data received"); return i; } @@ -7336,7 +8975,7 @@ class ra { if (!r.ok) throw new Error(`Failed to fetch batch sweep transactions: ${r.statusText}`); const o = await r.json(); - if (!Rt.isBatchSweepTransactionsResponse(o)) + if (!Ht.isBatchSweepTransactionsResponse(o)) throw new Error("Invalid batch sweep transactions data received"); return o; } @@ -7345,7 +8984,7 @@ class ra { if (!r.ok) throw new Error(`Failed to fetch commitment tx: ${r.statusText}`); const o = await r.json(); - if (!Rt.isCommitmentTx(o)) + if (!Ht.isCommitmentTx(o)) throw new Error("Invalid commitment tx data received"); return o; } @@ -7357,7 +8996,7 @@ class ra { if (!s.ok) throw new Error(`Failed to fetch commitment tx connectors: ${s.statusText}`); const i = await s.json(); - if (!Rt.isConnectorsResponse(i)) + if (!Ht.isConnectorsResponse(i)) throw new Error("Invalid commitment tx connectors data received"); return i.connectors.forEach((c) => { c.children = Object.fromEntries(Object.entries(c.children).map(([a, u]) => [ @@ -7374,7 +9013,7 @@ class ra { if (!s.ok) throw new Error(`Failed to fetch commitment tx forfeitTxs: ${s.statusText}`); const i = await s.json(); - if (!Rt.isForfeitTxsResponse(i)) + if (!Ht.isForfeitTxsResponse(i)) throw new Error("Invalid commitment tx forfeitTxs data received"); return i; } @@ -7387,7 +9026,7 @@ class ra { }; n?.addEventListener("abort", s); try { - for await (const i of xo(o)) { + for await (const i of ls(o)) { if (n?.aborted) break; try { @@ -7395,9 +9034,9 @@ class ra { c.event && (yield { txid: c.event.txid, scripts: c.event.scripts || [], - newVtxos: (c.event.newVtxos || []).map(Dn), - spentVtxos: (c.event.spentVtxos || []).map(Dn), - sweptVtxos: (c.event.sweptVtxos || []).map(Dn), + newVtxos: (c.event.newVtxos || []).map(ur), + spentVtxos: (c.event.spentVtxos || []).map(ur), + sweptVtxos: (c.event.sweptVtxos || []).map(ur), tx: c.event.tx, checkpointTxs: c.event.checkpointTxs }); @@ -7411,7 +9050,7 @@ class ra { } catch (o) { if (o instanceof Error && o.name === "AbortError") break; - if (bo(o)) { + if (ds(o)) { console.debug("Timeout error ignored"); continue; } @@ -7426,7 +9065,7 @@ class ra { if (!s.ok) throw new Error(`Failed to fetch virtual txs: ${s.statusText}`); const i = await s.json(); - if (!Rt.isVirtualTxsResponse(i)) + if (!Ht.isVirtualTxsResponse(i)) throw new Error("Invalid virtual txs data received"); return i; } @@ -7438,7 +9077,7 @@ class ra { if (!s.ok) throw new Error(`Failed to fetch vtxo chain: ${s.statusText}`); const i = await s.json(); - if (!Rt.isVtxoChainResponse(i)) + if (!Ht.isVtxoChainResponse(i)) throw new Error("Invalid vtxo chain data received"); return i; } @@ -7458,10 +9097,10 @@ class ra { if (!o.ok) throw new Error(`Failed to fetch vtxos: ${o.statusText}`); const s = await o.json(); - if (!Rt.isVtxosResponse(s)) + if (!Ht.isVtxosResponse(s)) throw new Error("Invalid vtxos data received"); return { - vtxos: s.vtxos.map(Dn), + vtxos: s.vtxos.map(ur), page: s.page }; } @@ -7496,7 +9135,7 @@ class ra { } } } -function Dn(e) { +function ur(e) { return { txid: e.outpoint.txid, vout: e.outpoint.vout, @@ -7517,91 +9156,91 @@ function Dn(e) { isSpent: e.isSpent }; } -var Rt; +var Ht; (function(e) { - function t(y) { - return typeof y == "object" && typeof y.totalOutputAmount == "string" && typeof y.totalOutputVtxos == "number" && typeof y.expiresAt == "string" && typeof y.swept == "boolean"; + function t(x) { + return typeof x == "object" && typeof x.totalOutputAmount == "string" && typeof x.totalOutputVtxos == "number" && typeof x.expiresAt == "string" && typeof x.swept == "boolean"; } - function n(y) { - return typeof y == "object" && typeof y.txid == "string" && typeof y.expiresAt == "string" && Object.values(_e).includes(y.type) && Array.isArray(y.spends) && y.spends.every((tt) => typeof tt == "string"); + function n(x) { + return typeof x == "object" && typeof x.txid == "string" && typeof x.expiresAt == "string" && Object.values(en).includes(x.type) && Array.isArray(x.spends) && x.spends.every((Q) => typeof Q == "string"); } - function r(y) { - return typeof y == "object" && typeof y.startedAt == "string" && typeof y.endedAt == "string" && typeof y.totalInputAmount == "string" && typeof y.totalInputVtxos == "number" && typeof y.totalOutputAmount == "string" && typeof y.totalOutputVtxos == "number" && typeof y.batches == "object" && Object.values(y.batches).every(t); + function r(x) { + return typeof x == "object" && typeof x.startedAt == "string" && typeof x.endedAt == "string" && typeof x.totalInputAmount == "string" && typeof x.totalInputVtxos == "number" && typeof x.totalOutputAmount == "string" && typeof x.totalOutputVtxos == "number" && typeof x.batches == "object" && Object.values(x.batches).every(t); } e.isCommitmentTx = r; - function o(y) { - return typeof y == "object" && typeof y.txid == "string" && typeof y.vout == "number"; + function o(x) { + return typeof x == "object" && typeof x.txid == "string" && typeof x.vout == "number"; } e.isOutpoint = o; - function s(y) { - return Array.isArray(y) && y.every(o); + function s(x) { + return Array.isArray(x) && x.every(o); } e.isOutpointArray = s; - function i(y) { - return typeof y == "object" && typeof y.txid == "string" && typeof y.children == "object" && Object.values(y.children).every(f) && Object.keys(y.children).every((tt) => Number.isInteger(Number(tt))); + function i(x) { + return typeof x == "object" && typeof x.txid == "string" && typeof x.children == "object" && Object.values(x.children).every(f) && Object.keys(x.children).every((Q) => Number.isInteger(Number(Q))); } - function c(y) { - return Array.isArray(y) && y.every(i); + function c(x) { + return Array.isArray(x) && x.every(i); } e.isTxsArray = c; - function a(y) { - return typeof y == "object" && typeof y.amount == "string" && typeof y.createdAt == "string" && typeof y.isSettled == "boolean" && typeof y.settledBy == "string" && Object.values(Eo).includes(y.type) && (!y.commitmentTxid && typeof y.virtualTxid == "string" || typeof y.commitmentTxid == "string" && !y.virtualTxid); + function a(x) { + return typeof x == "object" && typeof x.amount == "string" && typeof x.createdAt == "string" && typeof x.isSettled == "boolean" && typeof x.settledBy == "string" && Object.values(hs).includes(x.type) && (!x.commitmentTxid && typeof x.virtualTxid == "string" || typeof x.commitmentTxid == "string" && !x.virtualTxid); } - function u(y) { - return Array.isArray(y) && y.every(a); + function u(x) { + return Array.isArray(x) && x.every(a); } e.isTxHistoryRecordArray = u; - function f(y) { - return typeof y == "string" && y.length === 64; + function f(x) { + return typeof x == "string" && x.length === 64; } - function l(y) { - return Array.isArray(y) && y.every(f); + function l(x) { + return Array.isArray(x) && x.every(f); } e.isTxidArray = l; - function d(y) { - return typeof y == "object" && o(y.outpoint) && typeof y.createdAt == "string" && (y.expiresAt === null || typeof y.expiresAt == "string") && typeof y.amount == "string" && typeof y.script == "string" && typeof y.isPreconfirmed == "boolean" && typeof y.isSwept == "boolean" && typeof y.isUnrolled == "boolean" && typeof y.isSpent == "boolean" && (!y.spentBy || typeof y.spentBy == "string") && (!y.settledBy || typeof y.settledBy == "string") && (!y.arkTxid || typeof y.arkTxid == "string") && Array.isArray(y.commitmentTxids) && y.commitmentTxids.every(f); + function d(x) { + return typeof x == "object" && o(x.outpoint) && typeof x.createdAt == "string" && (x.expiresAt === null || typeof x.expiresAt == "string") && typeof x.amount == "string" && typeof x.script == "string" && typeof x.isPreconfirmed == "boolean" && typeof x.isSwept == "boolean" && typeof x.isUnrolled == "boolean" && typeof x.isSpent == "boolean" && (!x.spentBy || typeof x.spentBy == "string") && (!x.settledBy || typeof x.settledBy == "string") && (!x.arkTxid || typeof x.arkTxid == "string") && Array.isArray(x.commitmentTxids) && x.commitmentTxids.every(f); } - function h(y) { - return typeof y == "object" && typeof y.current == "number" && typeof y.next == "number" && typeof y.total == "number"; + function h(x) { + return typeof x == "object" && typeof x.current == "number" && typeof x.next == "number" && typeof x.total == "number"; } - function w(y) { - return typeof y == "object" && Array.isArray(y.vtxoTree) && y.vtxoTree.every(i) && (!y.page || h(y.page)); + function w(x) { + return typeof x == "object" && Array.isArray(x.vtxoTree) && x.vtxoTree.every(i) && (!x.page || h(x.page)); } e.isVtxoTreeResponse = w; - function p(y) { - return typeof y == "object" && Array.isArray(y.leaves) && y.leaves.every(o) && (!y.page || h(y.page)); + function g(x) { + return typeof x == "object" && Array.isArray(x.leaves) && x.leaves.every(o) && (!x.page || h(x.page)); } - e.isVtxoTreeLeavesResponse = p; - function m(y) { - return typeof y == "object" && Array.isArray(y.connectors) && y.connectors.every(i) && (!y.page || h(y.page)); + e.isVtxoTreeLeavesResponse = g; + function y(x) { + return typeof x == "object" && Array.isArray(x.connectors) && x.connectors.every(i) && (!x.page || h(x.page)); } - e.isConnectorsResponse = m; - function x(y) { - return typeof y == "object" && Array.isArray(y.txids) && y.txids.every(f) && (!y.page || h(y.page)); + e.isConnectorsResponse = y; + function S(x) { + return typeof x == "object" && Array.isArray(x.txids) && x.txids.every(f) && (!x.page || h(x.page)); } - e.isForfeitTxsResponse = x; - function b(y) { - return typeof y == "object" && Array.isArray(y.sweptBy) && y.sweptBy.every(f); + e.isForfeitTxsResponse = S; + function v(x) { + return typeof x == "object" && Array.isArray(x.sweptBy) && x.sweptBy.every(f); } - e.isSweptCommitmentTxResponse = b; - function v(y) { - return typeof y == "object" && Array.isArray(y.sweptBy) && y.sweptBy.every(f); + e.isSweptCommitmentTxResponse = v; + function O(x) { + return typeof x == "object" && Array.isArray(x.sweptBy) && x.sweptBy.every(f); } - e.isBatchSweepTransactionsResponse = v; - function O(y) { - return typeof y == "object" && Array.isArray(y.txs) && y.txs.every((tt) => typeof tt == "string") && (!y.page || h(y.page)); + e.isBatchSweepTransactionsResponse = O; + function N(x) { + return typeof x == "object" && Array.isArray(x.txs) && x.txs.every((Q) => typeof Q == "string") && (!x.page || h(x.page)); } - e.isVirtualTxsResponse = O; - function A(y) { - return typeof y == "object" && Array.isArray(y.chain) && y.chain.every(n) && (!y.page || h(y.page)); + e.isVirtualTxsResponse = N; + function U(x) { + return typeof x == "object" && Array.isArray(x.chain) && x.chain.every(n) && (!x.page || h(x.page)); } - e.isVtxoChainResponse = A; - function F(y) { - return typeof y == "object" && Array.isArray(y.vtxos) && y.vtxos.every(d) && (!y.page || h(y.page)); + e.isVtxoChainResponse = U; + function W(x) { + return typeof x == "object" && Array.isArray(x.vtxos) && x.vtxos.every(d) && (!x.page || h(x.page)); } - e.isVtxosResponse = F; -})(Rt || (Rt = {})); -class So { + e.isVtxosResponse = W; +})(Ht || (Ht = {})); +class ps { constructor(t, n = /* @__PURE__ */ new Map()) { this.root = t, this.children = n; } @@ -7610,14 +9249,14 @@ class So { throw new Error("empty chunks"); const n = /* @__PURE__ */ new Map(); for (const s of t) { - const i = Zl(s), c = i.tx.id; + const i = Ep(s), c = i.tx.id; n.set(c, i); } const r = []; for (const [s] of n) { let i = !1; for (const [c, a] of n) - if (c !== s && (i = Yl(a, s), i)) + if (c !== s && (i = bp(a, s), i)) break; if (!i) { r.push(s); @@ -7628,7 +9267,7 @@ class So { throw new Error("no root chunk found"); if (r.length > 1) throw new Error(`multiple root chunks found: ${r.join(", ")}`); - const o = oa(r[0], n); + const o = Fu(r[0], n); if (!o) throw new Error(`chunk not found for root txid: ${r[0]}`); if (o.nbOfNodes() !== t.length) @@ -7654,7 +9293,7 @@ class So { throw new Error(`output index ${r} is out of bounds (nb of outputs: ${t})`); o.validate(); const s = o.root.getInput(0), i = this.root.id; - if (!s.txid || S.encode(s.txid) !== i || s.index !== r) + if (!s.txid || $.encode(s.txid) !== i || s.index !== r) throw new Error(`input of child ${r} is not the output of the parent`); let c = 0n; for (let u = 0; u < o.root.outputsLength; u++) { @@ -7709,24 +9348,24 @@ class So { yield this; } } -function Yl(e, t) { +function bp(e, t) { return Object.values(e.children).includes(t); } -function oa(e, t) { +function Fu(e, t) { const n = t.get(e); if (!n) return null; const r = n.tx, o = /* @__PURE__ */ new Map(); for (const [s, i] of Object.entries(n.children)) { - const c = parseInt(s), a = oa(i, t); + const c = parseInt(s), a = Fu(i, t); a && o.set(c, a); } - return new So(r, o); + return new ps(r, o); } -function Zl(e) { - return { tx: ee.fromPSBT(Et.decode(e.tx)), children: e.children }; +function Ep(e) { + return { tx: ue.fromPSBT(It.decode(e.tx)), children: e.children }; } -class Xl { +class xp { constructor() { this.store = /* @__PURE__ */ new Map(); } @@ -7743,39 +9382,39 @@ class Xl { this.store.clear(); } } -const pr = (e) => e ? S.encode(e) : void 0, Ye = (e) => e ? S.decode(e) : void 0, gr = ([e, t]) => ({ - cb: S.encode(Wt.encode(e)), - s: S.encode(t) -}), ai = (e) => ({ +const Gr = (e) => e ? $.encode(e) : void 0, gn = (e) => e ? $.decode(e) : void 0, qr = ([e, t]) => ({ + cb: $.encode(Zt.encode(e)), + s: $.encode(t) +}), bc = (e) => ({ ...e, - tapTree: pr(e.tapTree), - forfeitTapLeafScript: gr(e.forfeitTapLeafScript), - intentTapLeafScript: gr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(pr) -}), ui = (e) => ({ + tapTree: Gr(e.tapTree), + forfeitTapLeafScript: qr(e.forfeitTapLeafScript), + intentTapLeafScript: qr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(Gr) +}), Ec = (e) => ({ ...e, - tapTree: pr(e.tapTree), - forfeitTapLeafScript: gr(e.forfeitTapLeafScript), - intentTapLeafScript: gr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(pr) -}), wr = (e) => { - const t = Wt.decode(Ye(e.cb)), n = Ye(e.s); + tapTree: Gr(e.tapTree), + forfeitTapLeafScript: qr(e.forfeitTapLeafScript), + intentTapLeafScript: qr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(Gr) +}), jr = (e) => { + const t = Zt.decode(gn(e.cb)), n = gn(e.s); return [t, n]; -}, Ql = (e) => ({ +}, Sp = (e) => ({ ...e, createdAt: new Date(e.createdAt), - tapTree: Ye(e.tapTree), - forfeitTapLeafScript: wr(e.forfeitTapLeafScript), - intentTapLeafScript: wr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(Ye) -}), Jl = (e) => ({ + tapTree: gn(e.tapTree), + forfeitTapLeafScript: jr(e.forfeitTapLeafScript), + intentTapLeafScript: jr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(gn) +}), vp = (e) => ({ ...e, - tapTree: Ye(e.tapTree), - forfeitTapLeafScript: wr(e.forfeitTapLeafScript), - intentTapLeafScript: wr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(Ye) + tapTree: gn(e.tapTree), + forfeitTapLeafScript: jr(e.forfeitTapLeafScript), + intentTapLeafScript: jr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(gn) }); -class To { +class gs { constructor(t) { this.storage = t, this.cache = { vtxos: /* @__PURE__ */ new Map(), @@ -7793,7 +9432,7 @@ class To { if (!r) return this.cache.vtxos.set(t, []), []; try { - const s = JSON.parse(r).map(Ql); + const s = JSON.parse(r).map(Sp); return this.cache.vtxos.set(t, s.slice()), s.slice(); } catch (o) { return console.error(`Failed to parse VTXOs for address ${t}:`, o), this.cache.vtxos.set(t, []), []; @@ -7805,11 +9444,11 @@ class To { const s = r.findIndex((i) => i.txid === o.txid && i.vout === o.vout); s !== -1 ? r[s] = o : r.push(o); } - this.cache.vtxos.set(t, r.slice()), await this.storage.setItem(`vtxos:${t}`, JSON.stringify(r.map(ai))); + this.cache.vtxos.set(t, r.slice()), await this.storage.setItem(`vtxos:${t}`, JSON.stringify(r.map(bc))); } async removeVtxo(t, n) { const r = await this.getVtxos(t), [o, s] = n.split(":"), i = r.filter((c) => !(c.txid === o && c.vout === parseInt(s, 10))); - this.cache.vtxos.set(t, i.slice()), await this.storage.setItem(`vtxos:${t}`, JSON.stringify(i.map(ai))); + this.cache.vtxos.set(t, i.slice()), await this.storage.setItem(`vtxos:${t}`, JSON.stringify(i.map(bc))); } async clearVtxos(t) { this.cache.vtxos.set(t, []), await this.storage.removeItem(`vtxos:${t}`); @@ -7822,7 +9461,7 @@ class To { if (!r) return this.cache.utxos.set(t, []), []; try { - const s = JSON.parse(r).map(Jl); + const s = JSON.parse(r).map(vp); return this.cache.utxos.set(t, s.slice()), s.slice(); } catch (o) { return console.error(`Failed to parse UTXOs for address ${t}:`, o), this.cache.utxos.set(t, []), []; @@ -7833,11 +9472,11 @@ class To { n.forEach((o) => { const s = r.findIndex((i) => i.txid === o.txid && i.vout === o.vout); s !== -1 ? r[s] = o : r.push(o); - }), this.cache.utxos.set(t, r.slice()), await this.storage.setItem(`utxos:${t}`, JSON.stringify(r.map(ui))); + }), this.cache.utxos.set(t, r.slice()), await this.storage.setItem(`utxos:${t}`, JSON.stringify(r.map(Ec))); } async removeUtxo(t, n) { const r = await this.getUtxos(t), [o, s] = n.split(":"), i = r.filter((c) => !(c.txid === o && c.vout === parseInt(s, 10))); - this.cache.utxos.set(t, i.slice()), await this.storage.setItem(`utxos:${t}`, JSON.stringify(i.map(ui))); + this.cache.utxos.set(t, i.slice()), await this.storage.setItem(`utxos:${t}`, JSON.stringify(i.map(Ec))); } async clearUtxos(t) { this.cache.utxos.set(t, []), await this.storage.removeItem(`utxos:${t}`); @@ -7884,7 +9523,7 @@ class To { this.cache.walletState = t, await this.storage.setItem("wallet:state", JSON.stringify(t)); } } -class td { +class Tp { constructor(t) { this.cache = /* @__PURE__ */ new Map(), this.storage = t; } @@ -7964,7 +9603,7 @@ class td { await this.storage.clear(), this.cache.clear(); } } -function Ve(e, t) { +function nn(e, t) { return { ...t, forfeitTapLeafScript: e.offchainTapscript.forfeit(), @@ -7972,7 +9611,7 @@ function Ve(e, t) { tapTree: e.offchainTapscript.encode() }; } -function vo(e, t) { +function ws(e, t) { return { ...t, forfeitTapLeafScript: e.boardingTapscript.forfeit(), @@ -7980,12 +9619,12 @@ function vo(e, t) { tapTree: e.boardingTapscript.encode() }; } -class Ze { - constructor(t, n, r, o, s, i, c, a, u, f, l, d, h, w, p, m) { - this.identity = t, this.network = n, this.networkName = r, this.onchainProvider = o, this.arkProvider = s, this.indexerProvider = i, this.arkServerPublicKey = c, this.offchainTapscript = a, this.boardingTapscript = u, this.serverUnrollScript = f, this.forfeitOutputScript = l, this.forfeitPubkey = d, this.dustAmount = h, this.walletRepository = w, this.contractRepository = p, this.renewalConfig = { - enabled: m?.enabled ?? !1, - ...$l, - ...m +class wn { + constructor(t, n, r, o, s, i, c, a, u, f, l, d, h, w, g, y) { + this.identity = t, this.network = n, this.networkName = r, this.onchainProvider = o, this.arkProvider = s, this.indexerProvider = i, this.arkServerPublicKey = c, this.offchainTapscript = a, this.boardingTapscript = u, this.serverUnrollScript = f, this.forfeitOutputScript = l, this.forfeitPubkey = d, this.dustAmount = h, this.walletRepository = w, this.contractRepository = g, this.renewalConfig = { + enabled: y?.enabled ?? !1, + ...op, + ...y }; } static async create(t) { @@ -7995,34 +9634,34 @@ class Ze { const r = t.arkProvider || (() => { if (!t.arkServerUrl) throw new Error("Either arkProvider or arkServerUrl must be provided"); - return new ea(t.arkServerUrl); + return new Du(t.arkServerUrl); })(), o = t.arkServerUrl || r.serverUrl; if (!o) throw new Error("Could not determine arkServerUrl from provider"); - const s = t.indexerUrl || o, i = t.indexerProvider || new ra(s), c = await r.getInfo(), a = sl(c.network), u = t.esploraUrl || cl[c.network], f = t.onchainProvider || new al(u), l = { + const s = t.indexerUrl || o, i = t.indexerProvider || new Ku(s), c = await r.getInfo(), a = Oh(c.network), u = t.esploraUrl || Uh[c.network], f = t.onchainProvider || new Rh(u), l = { value: c.unilateralExitDelay, type: c.unilateralExitDelay < 512n ? "blocks" : "seconds" }, d = { value: c.boardingExitDelay, type: c.boardingExitDelay < 512n ? "blocks" : "seconds" - }, h = S.decode(c.signerPubkey).slice(1), w = new dr.Script({ + }, h = $.decode(c.signerPubkey).slice(1), w = new Wr.Script({ pubKey: n, serverPubKey: h, csvTimelock: l - }), p = new dr.Script({ + }), g = new Wr.Script({ pubKey: n, serverPubKey: h, csvTimelock: d - }), m = w; - let x; + }), y = w; + let S; try { - const tt = S.decode(c.checkpointTapscript); - x = kt.decode(tt); + const Q = $.decode(c.checkpointTapscript); + S = $t.decode(Q); } catch { throw new Error("Invalid checkpointTapscript from server"); } - const b = S.decode(c.forfeitPubkey).slice(1), v = ke(a).decode(c.forfeitAddress), O = st.encode(v), A = t.storage || new Xl(), F = new To(A), y = new td(A); - return new Ze(t.identity, a, c.network, f, r, i, h, m, p, x, O, b, c.dust, F, y, t.renewalConfig); + const v = $.decode(c.forfeitPubkey).slice(1), O = Me(a).decode(c.forfeitAddress), N = ut.encode(O), U = t.storage || new xp(), W = new gs(U), x = new Tp(U); + return new wn(t.identity, a, c.network, f, r, i, h, y, g, S, N, v, c.dust, W, x, t.renewalConfig); } get arkAddress() { return this.offchainTapscript.address(this.network.hrp, this.arkServerPublicKey); @@ -8042,7 +9681,7 @@ class Ze { for (const f of t) f.status.confirmed ? r += f.value : o += f.value; let s = 0, i = 0, c = 0; - s = n.filter((f) => f.virtualStatus.state === "settled").reduce((f, l) => f + l.value, 0), i = n.filter((f) => f.virtualStatus.state === "preconfirmed").reduce((f, l) => f + l.value, 0), c = n.filter((f) => te(f) && f.virtualStatus.state === "swept").reduce((f, l) => f + l.value, 0); + s = n.filter((f) => f.virtualStatus.state === "settled").reduce((f, l) => f + l.value, 0), i = n.filter((f) => f.virtualStatus.state === "preconfirmed").reduce((f, l) => f + l.value, 0), c = n.filter((f) => ae(f) && f.virtualStatus.state === "swept").reduce((f, l) => f + l.value, 0); const a = r + o, u = s + i + c; return { boarding: { @@ -8058,14 +9697,14 @@ class Ze { }; } async getVtxos(t) { - const n = await this.getAddress(), o = (await this.getVirtualCoins(t)).map((s) => Ve(this, s)); + const n = await this.getAddress(), o = (await this.getVirtualCoins(t)).map((s) => nn(this, s)); return await this.walletRepository.saveVtxos(n, o), o; } async getVirtualCoins(t = { withRecoverable: !0, withUnrolled: !1 }) { - const n = [S.encode(this.offchainTapscript.pkScript)], o = (await this.indexerProvider.getVtxos({ scripts: n })).vtxos; - let s = o.filter(te); - if (t.withRecoverable || (s = s.filter((i) => !mo(i))), t.withUnrolled) { - const i = o.filter((c) => !te(c)); + const n = [$.encode(this.offchainTapscript.pkScript)], o = (await this.indexerProvider.getVtxos({ scripts: n })).vtxos; + let s = o.filter(ae); + if (t.withRecoverable || (s = s.filter((i) => !fs(i))), t.withUnrolled) { + const i = o.filter((c) => !ae(c)); s.push(...i.filter((c) => c.isUnrolled)); } return s; @@ -8074,11 +9713,11 @@ class Ze { if (!this.indexerProvider) return []; const t = await this.indexerProvider.getVtxos({ - scripts: [S.encode(this.offchainTapscript.pkScript)] + scripts: [$.encode(this.offchainTapscript.pkScript)] }), { boardingTxs: n, commitmentsToIgnore: r } = await this.getBoardingTxs(), o = [], s = []; for (const a of t.vtxos) - te(a) ? o.push(a) : s.push(a); - const i = ta(o, s, r), c = [...n, ...i]; + ae(a) ? o.push(a) : s.push(a); + const i = Vu(o, s, r), c = [...n, ...i]; return c.sort( // place createdAt = 0 (unconfirmed txs) first, then descending (a, u) => a.createdAt === 0 ? -1 : u.createdAt === 0 ? 1 : u.createdAt - a.createdAt @@ -8117,7 +9756,7 @@ class Ze { arkTxid: "" }, amount: c.value, - type: bn.TxReceived, + type: Fn.TxReceived, settled: c.virtualStatus.state === "spent", createdAt: c.status.block_time ? new Date(c.status.block_time * 1e3).getTime() : 0 }; @@ -8129,20 +9768,20 @@ class Ze { }; } async getBoardingUtxos() { - const t = await this.getBoardingAddress(), r = (await this.onchainProvider.getCoins(t)).map((o) => vo(this, o)); + const t = await this.getBoardingAddress(), r = (await this.onchainProvider.getCoins(t)).map((o) => ws(this, o)); return await this.walletRepository.saveUtxos(t, r), r; } async sendBitcoin(t) { if (t.amount <= 0) throw new Error("Amount must be positive"); - if (!nd(t.address)) + if (!Ip(t.address)) throw new Error("Invalid Ark address " + t.address); const n = await this.getVirtualCoins({ withRecoverable: !1 - }), r = rd(n, t.amount), o = this.offchainTapscript.forfeit(); + }), r = kp(n, t.amount), o = this.offchainTapscript.forfeit(); if (!o) throw new Error("Selected leaf not found"); - const s = qe.decode(t.address), c = [ + const s = pn.decode(t.address), c = [ { script: BigInt(t.amount) < this.dustAmount ? s.subdustPkScript : s.pkScript, amount: BigInt(t.amount) @@ -8156,14 +9795,14 @@ class Ze { }); } const a = this.offchainTapscript.encode(); - let u = Ul(r.inputs.map((w) => ({ + let u = tp(r.inputs.map((w) => ({ ...w, tapLeafScript: o, tapTree: a })), c, this.serverUnrollScript); - const f = await this.identity.sign(u.arkTx), { arkTxid: l, signedCheckpointTxs: d } = await this.arkProvider.submitTx(Et.encode(f.toPSBT()), u.checkpoints.map((w) => Et.encode(w.toPSBT()))), h = await Promise.all(d.map(async (w) => { - const p = ee.fromPSBT(Et.decode(w)), m = await this.identity.sign(p); - return Et.encode(m.toPSBT()); + const f = await this.identity.sign(u.arkTx), { arkTxid: l, signedCheckpointTxs: d } = await this.arkProvider.submitTx(It.encode(f.toPSBT()), u.checkpoints.map((w) => It.encode(w.toPSBT()))), h = await Promise.all(d.map(async (w) => { + const g = ue.fromPSBT(It.decode(w)), y = await this.identity.sign(g); + return It.encode(y.toPSBT()); })); return await this.arkProvider.finalizeTx(l, h), l; } @@ -8172,22 +9811,22 @@ class Ze { for (const d of t.inputs) if (typeof d == "string") try { - ot.fromString(d); + at.fromString(d); } catch { throw new Error(`Invalid arknote "${d}"`); } } if (!t) { let d = 0; - const w = kt.decode(S.decode(this.boardingTapscript.exitScript)).params.timelock, p = (await this.getBoardingUtxos()).filter((b) => !Cl(b, w)); - d += p.reduce((b, v) => b + v.value, 0); - const m = await this.getVtxos({ withRecoverable: !0 }); - d += m.reduce((b, v) => b + v.value, 0); - const x = [...p, ...m]; - if (x.length === 0) + const w = $t.decode($.decode(this.boardingTapscript.exitScript)).params.timelock, g = (await this.getBoardingUtxos()).filter((v) => !rp(v, w)); + d += g.reduce((v, O) => v + O.value, 0); + const y = await this.getVtxos({ withRecoverable: !0 }); + d += y.reduce((v, O) => v + O.value, 0); + const S = [...g, ...y]; + if (S.length === 0) throw new Error("No inputs found"); t = { - inputs: x, + inputs: S, outputs: [ { address: await this.getAddress(), @@ -8201,10 +9840,10 @@ class Ze { for (const [d, h] of t.outputs.entries()) { let w; try { - w = qe.decode(h.address).pkScript, s = !0; + w = pn.decode(h.address).pkScript, s = !0; } catch { - const p = ke(this.network).decode(h.address); - w = st.encode(p), r.push(d); + const g = Me(this.network).decode(h.address); + w = ut.encode(g), r.push(d); } o.push({ amount: h.amount, @@ -8213,7 +9852,7 @@ class Ze { } let i; const c = []; - s && (i = this.identity.signerSession(), c.push(S.encode(await i.getPublicKey()))); + s && (i = this.identity.signerSession(), c.push($.encode(await i.getPublicKey()))); const [a, u] = await Promise.all([ this.makeRegisterIntentSignature(t.inputs, o, r, c), this.makeDeleteIntentSignature(t.inputs) @@ -8222,90 +9861,90 @@ class Ze { let d; const h = [ ...c, - ...t.inputs.map((A) => `${A.txid}:${A.vout}`) + ...t.inputs.map((U) => `${U.txid}:${U.vout}`) ], w = this.arkProvider.getEventStream(l.signal, h); - let p, m; - const x = [], b = []; - let v, O; - for await (const A of w) - switch (n && n(A), A.type) { + let g, y; + const S = [], v = []; + let O, N; + for await (const U of w) + switch (n && n(U), U.type) { // the settlement failed - case z.BatchFailed: - throw new Error(A.reason); - case z.BatchStarted: + case J.BatchFailed: + throw new Error(U.reason); + case J.BatchStarted: if (d !== void 0) continue; - const F = await this.handleBatchStartedEvent(A, f, this.forfeitPubkey, this.forfeitOutputScript); - F.skip || (d = A.type, m = F.sweepTapTreeRoot, p = F.roundId, s || (d = z.TreeNonces)); + const W = await this.handleBatchStartedEvent(U, f, this.forfeitPubkey, this.forfeitOutputScript); + W.skip || (d = U.type, y = W.sweepTapTreeRoot, g = W.roundId, s || (d = J.TreeNonces)); break; - case z.TreeTx: - if (d !== z.BatchStarted && d !== z.TreeNonces) + case J.TreeTx: + if (d !== J.BatchStarted && d !== J.TreeNonces) continue; - if (A.batchIndex === 0) - x.push(A.chunk); - else if (A.batchIndex === 1) - b.push(A.chunk); + if (U.batchIndex === 0) + S.push(U.chunk); + else if (U.batchIndex === 1) + v.push(U.chunk); else - throw new Error(`Invalid batch index: ${A.batchIndex}`); + throw new Error(`Invalid batch index: ${U.batchIndex}`); break; - case z.TreeSignature: - if (d !== z.TreeNonces || !s) + case J.TreeSignature: + if (d !== J.TreeNonces || !s) continue; - if (!v) + if (!O) throw new Error("Vtxo graph not set, something went wrong"); - if (A.batchIndex === 0) { - const y = S.decode(A.signature); - v.update(A.txid, (tt) => { - tt.updateInput(0, { - tapKeySig: y + if (U.batchIndex === 0) { + const x = $.decode(U.signature); + O.update(U.txid, (Q) => { + Q.updateInput(0, { + tapKeySig: x }); }); } break; // the server has started the signing process of the vtxo tree transactions // the server expects the partial musig2 nonces for each tx - case z.TreeSigningStarted: - if (d !== z.BatchStarted) + case J.TreeSigningStarted: + if (d !== J.BatchStarted) continue; if (s) { if (!i) throw new Error("Signing session not set"); - if (!m) + if (!y) throw new Error("Sweep tap tree root not set"); - if (x.length === 0) + if (S.length === 0) throw new Error("unsigned vtxo graph not received"); - v = So.create(x), await this.handleSettlementSigningEvent(A, m, i, v); + O = ps.create(S), await this.handleSettlementSigningEvent(U, y, i, O); } - d = A.type; + d = U.type; break; // the musig2 nonces of the vtxo tree transactions are generated // the server expects now the partial musig2 signatures - case z.TreeNonces: - if (d !== z.TreeSigningStarted) + case J.TreeNonces: + if (d !== J.TreeSigningStarted) continue; if (s) { if (!i) throw new Error("Signing session not set"); - await this.handleSettlementTreeNoncesEvent(A, i) && (d = A.type); + await this.handleSettlementTreeNoncesEvent(U, i) && (d = U.type); break; } - d = A.type; + d = U.type; break; // the vtxo tree is signed, craft, sign and submit forfeit transactions // if any boarding utxos are involved, the settlement tx is also signed - case z.BatchFinalization: - if (d !== z.TreeNonces) + case J.BatchFinalization: + if (d !== J.TreeNonces) continue; if (!this.forfeitOutputScript) throw new Error("Forfeit output script not set"); - b.length > 0 && (O = So.create(b), Bl(A.commitmentTx, O)), await this.handleSettlementFinalizationEvent(A, t.inputs, this.forfeitOutputScript, O), d = A.type; + v.length > 0 && (N = ps.create(v), Qh(U.commitmentTx, N)), await this.handleSettlementFinalizationEvent(U, t.inputs, this.forfeitOutputScript, N), d = U.type; break; // the settlement is done, last event to be received - case z.BatchFinalized: - if (d !== z.BatchFinalization) + case J.BatchFinalized: + if (d !== J.BatchFinalization) continue; - if (A.id === p) - return l.abort(), A.commitmentTxid; + if (U.id === g) + return l.abort(), U.commitmentTxid; } } catch (d) { l.abort(); @@ -8335,7 +9974,7 @@ class Ze { } if (this.indexerProvider && n) { const c = this.offchainTapscript, a = await this.indexerProvider.subscribeForScripts([ - S.encode(c.pkScript) + $.encode(c.pkScript) ]), u = new AbortController(), f = this.indexerProvider.getSubscription(a, u.signal); s = async () => { u.abort(), await this.indexerProvider?.unsubscribeForScripts(a); @@ -8344,8 +9983,8 @@ class Ze { for await (const l of f) l.newVtxos?.length > 0 && t({ type: "vtxo", - newVtxos: l.newVtxos.map((d) => Ve(this, d)), - spentVtxos: l.spentVtxos.map((d) => Ve(this, d)) + newVtxos: l.newVtxos.map((d) => nn(this, d)), + spentVtxos: l.spentVtxos.map((d) => nn(this, d)) }); } catch (l) { console.error("Subscription error:", l); @@ -8357,7 +9996,7 @@ class Ze { }; } async handleBatchStartedEvent(t, n, r, o) { - const s = new TextEncoder().encode(n), i = ht(s), c = S.encode(i); + const s = new TextEncoder().encode(n), i = wt(s), c = $.encode(i); let a = !0; for (const l of t.intentIdHashes) if (l === c) { @@ -8367,13 +10006,13 @@ class Ze { } if (a) return { skip: a }; - const u = kt.encode({ + const u = $t.encode({ timelock: { value: t.batchExpiry, type: t.batchExpiry >= 512n ? "seconds" : "blocks" }, pubkeys: [r] - }).script, f = cn(u); + }).script, f = On(u); return { roundId: t.id, sweepTapTreeRoot: f, @@ -8383,80 +10022,80 @@ class Ze { } // validates the vtxo tree, creates a signing session and generates the musig2 nonces async handleSettlementSigningEvent(t, n, r, o) { - const s = ee.fromPSBT(Et.decode(t.unsignedCommitmentTx)); - Ol(o, s, n); + const s = ue.fromPSBT(It.decode(t.unsignedCommitmentTx)); + Jh(o, s, n); const i = s.getOutput(0); if (!i?.amount) throw new Error("Shared output not found"); r.init(o, n, i.amount); - const c = S.encode(await r.getPublicKey()), a = await r.getNonces(); + const c = $.encode(await r.getPublicKey()), a = await r.getNonces(); await this.arkProvider.submitTreeNonces(t.id, c, a); } async handleSettlementTreeNoncesEvent(t, n) { const { hasAllNonces: r } = await n.aggregatedNonces(t.txid, t.nonces); if (!r) return !1; - const o = await n.sign(), s = S.encode(await n.getPublicKey()); + const o = await n.sign(), s = $.encode(await n.getPublicKey()); return await this.arkProvider.submitTreeSignatures(t.id, s, o), !0; } async handleSettlementFinalizationEvent(t, n, r, o) { const s = [], i = await this.getVirtualCoins(); - let c = ee.fromPSBT(Et.decode(t.commitmentTx)), a = !1, u = 0; + let c = ue.fromPSBT(It.decode(t.commitmentTx)), a = !1, u = 0; const f = o?.leaves() || []; for (const l of n) { - const d = i.find((v) => v.txid === l.txid && v.vout === l.vout); + const d = i.find((O) => O.txid === l.txid && O.vout === l.vout); if (!d) { - for (let v = 0; v < c.inputsLength; v++) { - const O = c.getInput(v); - if (!O.txid || O.index === void 0) + for (let O = 0; O < c.inputsLength; O++) { + const N = c.getInput(O); + if (!N.txid || N.index === void 0) throw new Error("The server returned incomplete data. No settlement input found in the PSBT"); - if (S.encode(O.txid) === l.txid && O.index === l.vout) { - c.updateInput(v, { + if ($.encode(N.txid) === l.txid && N.index === l.vout) { + c.updateInput(O, { tapLeafScript: [l.forfeitTapLeafScript] }), c = await this.identity.sign(c, [ - v + O ]), a = !0; break; } } continue; } - if (mo(d) || Jc(d, this.dustAmount)) + if (fs(d) || Hu(d, this.dustAmount)) continue; if (f.length === 0) throw new Error("connectors not received"); if (u >= f.length) throw new Error("not enough connectors received"); - const h = f[u], w = h.id, p = h.getOutput(0); - if (!p) + const h = f[u], w = h.id, g = h.getOutput(0); + if (!g) throw new Error("connector output not found"); - const m = p.amount, x = p.script; - if (!m || !x) + const y = g.amount, S = g.script; + if (!y || !S) throw new Error("invalid connector output"); u++; - let b = xl([ + let v = Fh([ { txid: l.txid, index: l.vout, witnessUtxo: { amount: BigInt(d.value), - script: Ut.decode(l.tapTree).pkScript + script: _t.decode(l.tapTree).pkScript }, - sighashType: Ae.DEFAULT, + sighashType: Ke.DEFAULT, tapLeafScript: [l.forfeitTapLeafScript] }, { txid: w, index: 0, witnessUtxo: { - amount: m, - script: x + amount: y, + script: S } } ], r); - b = await this.identity.sign(b, [0]), s.push(Et.encode(b.toPSBT())); + v = await this.identity.sign(v, [0]), s.push(It.encode(v.toPSBT())); } - (s.length > 0 || a) && await this.arkProvider.submitSignedForfeitTxs(s, a ? Et.encode(c.toPSBT()) : void 0); + (s.length > 0 || a) && await this.arkProvider.submitSignedForfeitTxs(s, a ? It.encode(c.toPSBT()) : void 0); } async makeRegisterIntentSignature(t, n, r, o) { const s = Math.floor(Date.now() / 1e3), i = this.prepareIntentProofInputs(t), c = { @@ -8466,9 +10105,9 @@ class Ze { expire_at: s + 120, // valid for 2 minutes cosigners_public_keys: o - }, a = JSON.stringify(c, null, 0), u = hr.create(a, i, n), f = await this.identity.sign(u); + }, a = JSON.stringify(c, null, 0), u = zr.create(a, i, n), f = await this.identity.sign(u); return { - proof: Et.encode(f.toPSBT()), + proof: It.encode(f.toPSBT()), message: a }; } @@ -8477,18 +10116,18 @@ class Ze { type: "delete", expire_at: n + 120 // valid for 2 minutes - }, s = JSON.stringify(o, null, 0), i = hr.create(s, r, []), c = await this.identity.sign(i); + }, s = JSON.stringify(o, null, 0), i = zr.create(s, r, []), c = await this.identity.sign(i); return { - proof: Et.encode(c.toPSBT()), + proof: It.encode(c.toPSBT()), message: s }; } prepareIntentProofInputs(t) { const n = []; for (const r of t) { - const o = Ut.decode(r.tapTree), s = ed(r), i = [Xc.encode(r.tapTree)]; - r.extraWitness && i.push(Qf.encode(r.extraWitness)), n.push({ - txid: S.decode(r.txid), + const o = _t.decode(r.tapTree), s = Ap(r), i = [_u.encode(r.tapTree)]; + r.extraWitness && i.push(Sh.encode(r.extraWitness)), n.push({ + txid: $.decode(r.txid), index: r.vout, witnessUtxo: { amount: BigInt(r.value), @@ -8502,24 +10141,24 @@ class Ze { return n; } } -Ze.MIN_FEE_RATE = 1; -function ed(e) { +wn.MIN_FEE_RATE = 1; +function Ap(e) { let t; try { - const n = e.intentTapLeafScript[1], r = n.subarray(0, n.length - 1), o = kt.decode(r).params; - t = go.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }); + const n = e.intentTapLeafScript[1], r = n.subarray(0, n.length - 1), o = $t.decode(r).params; + t = cs.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }); } catch { } return t; } -function nd(e) { +function Ip(e) { try { - return qe.decode(e), !0; + return pn.decode(e), !0; } catch { return !1; } } -function rd(e, t) { +function kp(e, t) { const n = [...e].sort((i, c) => { const a = i.virtualStatus.batchExpiry || Number.MAX_SAFE_INTEGER, u = c.virtualStatus.batchExpiry || Number.MAX_SAFE_INTEGER; return a !== u ? a - u : c.value - i.value; @@ -8538,223 +10177,223 @@ function rd(e, t) { changeAmount: s }; } -function fi() { +function xc() { const e = crypto.getRandomValues(new Uint8Array(16)); - return S.encode(e); + return $.encode(e); } -var N; +var V; (function(e) { - e.walletInitialized = (g) => ({ + e.walletInitialized = (p) => ({ type: "WALLET_INITIALIZED", success: !0, - id: g + id: p }); - function t(g, E) { + function t(p, E) { return { type: "ERROR", success: !1, message: E, - id: g + id: p }; } e.error = t; - function n(g, E) { + function n(p, E) { return { type: "SETTLE_EVENT", success: !0, event: E, - id: g + id: p }; } e.settleEvent = n; - function r(g, E) { + function r(p, E) { return { type: "SETTLE_SUCCESS", success: !0, txid: E, - id: g + id: p }; } e.settleSuccess = r; - function o(g) { - return g.type === "SETTLE_SUCCESS" && g.success; + function o(p) { + return p.type === "SETTLE_SUCCESS" && p.success; } e.isSettleSuccess = o; - function s(g) { - return g.type === "ADDRESS" && g.success === !0; + function s(p) { + return p.type === "ADDRESS" && p.success === !0; } e.isAddress = s; - function i(g) { - return g.type === "BOARDING_ADDRESS" && g.success === !0; + function i(p) { + return p.type === "BOARDING_ADDRESS" && p.success === !0; } e.isBoardingAddress = i; - function c(g, E) { + function c(p, E) { return { type: "ADDRESS", success: !0, address: E, - id: g + id: p }; } e.address = c; - function a(g, E) { + function a(p, E) { return { type: "BOARDING_ADDRESS", success: !0, address: E, - id: g + id: p }; } e.boardingAddress = a; - function u(g) { - return g.type === "BALANCE" && g.success === !0; + function u(p) { + return p.type === "BALANCE" && p.success === !0; } e.isBalance = u; - function f(g, E) { + function f(p, E) { return { type: "BALANCE", success: !0, balance: E, - id: g + id: p }; } e.balance = f; - function l(g) { - return g.type === "VTXOS" && g.success === !0; + function l(p) { + return p.type === "VTXOS" && p.success === !0; } e.isVtxos = l; - function d(g, E) { + function d(p, E) { return { type: "VTXOS", success: !0, vtxos: E, - id: g + id: p }; } e.vtxos = d; - function h(g) { - return g.type === "VIRTUAL_COINS" && g.success === !0; + function h(p) { + return p.type === "VIRTUAL_COINS" && p.success === !0; } e.isVirtualCoins = h; - function w(g, E) { + function w(p, E) { return { type: "VIRTUAL_COINS", success: !0, virtualCoins: E, - id: g + id: p }; } e.virtualCoins = w; - function p(g) { - return g.type === "BOARDING_UTXOS" && g.success === !0; + function g(p) { + return p.type === "BOARDING_UTXOS" && p.success === !0; } - e.isBoardingUtxos = p; - function m(g, E) { + e.isBoardingUtxos = g; + function y(p, E) { return { type: "BOARDING_UTXOS", success: !0, boardingUtxos: E, - id: g + id: p }; } - e.boardingUtxos = m; - function x(g) { - return g.type === "SEND_BITCOIN_SUCCESS" && g.success === !0; + e.boardingUtxos = y; + function S(p) { + return p.type === "SEND_BITCOIN_SUCCESS" && p.success === !0; } - e.isSendBitcoinSuccess = x; - function b(g, E) { + e.isSendBitcoinSuccess = S; + function v(p, E) { return { type: "SEND_BITCOIN_SUCCESS", success: !0, txid: E, - id: g + id: p }; } - e.sendBitcoinSuccess = b; - function v(g) { - return g.type === "TRANSACTION_HISTORY" && g.success === !0; + e.sendBitcoinSuccess = v; + function O(p) { + return p.type === "TRANSACTION_HISTORY" && p.success === !0; } - e.isTransactionHistory = v; - function O(g, E) { + e.isTransactionHistory = O; + function N(p, E) { return { type: "TRANSACTION_HISTORY", success: !0, transactions: E, - id: g + id: p }; } - e.transactionHistory = O; - function A(g) { - return g.type === "WALLET_STATUS" && g.success === !0; + e.transactionHistory = N; + function U(p) { + return p.type === "WALLET_STATUS" && p.success === !0; } - e.isWalletStatus = A; - function F(g, E, U) { + e.isWalletStatus = U; + function W(p, E, A) { return { type: "WALLET_STATUS", success: !0, status: { walletInitialized: E, - xOnlyPublicKey: U + xOnlyPublicKey: A }, - id: g + id: p }; } - e.walletStatus = F; - function y(g) { - return g.type === "CLEAR_RESPONSE"; + e.walletStatus = W; + function x(p) { + return p.type === "CLEAR_RESPONSE"; } - e.isClearResponse = y; - function tt(g, E) { + e.isClearResponse = x; + function Q(p, E) { return { type: "CLEAR_RESPONSE", success: E, - id: g + id: p }; } - e.clearResponse = tt; - function H(g) { - return g.type === "WALLET_RELOADED"; + e.clearResponse = Q; + function C(p) { + return p.type === "WALLET_RELOADED"; } - e.isWalletReloaded = H; - function Ue(g, E) { + e.isWalletReloaded = C; + function Pt(p, E) { return { type: "WALLET_RELOADED", success: E, - id: g + id: p }; } - e.walletReloaded = Ue; - function Yt(g) { - return g.type === "VTXO_UPDATE"; + e.walletReloaded = Pt; + function gt(p) { + return p.type === "VTXO_UPDATE"; } - e.isVtxoUpdate = Yt; - function K(g, E) { + e.isVtxoUpdate = gt; + function _(p, E) { return { type: "VTXO_UPDATE", - id: fi(), + id: xc(), // spontaneous update, not tied to a request success: !0, spentVtxos: E, - newVtxos: g + newVtxos: p }; } - e.vtxoUpdate = K; - function k(g) { - return g.type === "UTXO_UPDATE"; + e.vtxoUpdate = _; + function b(p) { + return p.type === "UTXO_UPDATE"; } - e.isUtxoUpdate = k; - function T(g) { + e.isUtxoUpdate = b; + function m(p) { return { type: "UTXO_UPDATE", - id: fi(), + id: xc(), // spontaneous update, not tied to a request success: !0, - coins: g + coins: p }; } - e.utxoUpdate = T; -})(N || (N = {})); -class od { + e.utxoUpdate = m; +})(V || (V = {})); +class Bp { constructor(t, n = 1) { this.db = null, this.dbName = t, this.version = n; } @@ -8821,118 +10460,118 @@ class od { } } } -const sd = "arkade-service-worker"; -class j { +const Op = "arkade-service-worker"; +class tt { constructor(t, n, r, o, s, i) { this.hasWitness = t, this.inputCount = n, this.outputCount = r, this.inputSize = o, this.inputWitnessSize = s, this.outputSize = i; } static create() { - return new j(!1, 0, 0, 0, 0, 0); + return new tt(!1, 0, 0, 0, 0, 0); } addP2AInput() { - return this.inputCount++, this.inputSize += j.INPUT_SIZE, this; + return this.inputCount++, this.inputSize += tt.INPUT_SIZE, this; } addKeySpendInput(t = !0) { - return this.inputCount++, this.inputWitnessSize += 65 + (t ? 0 : 1), this.inputSize += j.INPUT_SIZE, this.hasWitness = !0, this; + return this.inputCount++, this.inputWitnessSize += 65 + (t ? 0 : 1), this.inputSize += tt.INPUT_SIZE, this.hasWitness = !0, this; } addP2PKHInput() { - return this.inputCount++, this.inputWitnessSize++, this.inputSize += j.INPUT_SIZE + j.P2PKH_SCRIPT_SIG_SIZE, this; + return this.inputCount++, this.inputWitnessSize++, this.inputSize += tt.INPUT_SIZE + tt.P2PKH_SCRIPT_SIG_SIZE, this; } addTapscriptInput(t, n, r) { - const o = 1 + j.BASE_CONTROL_BLOCK_SIZE + 1 + n + 1 + r; - return this.inputCount++, this.inputWitnessSize += t + o, this.inputSize += j.INPUT_SIZE, this.hasWitness = !0, this.inputCount++, this; + const o = 1 + tt.BASE_CONTROL_BLOCK_SIZE + 1 + n + 1 + r; + return this.inputCount++, this.inputWitnessSize += t + o, this.inputSize += tt.INPUT_SIZE, this.hasWitness = !0, this.inputCount++, this; } addP2WKHOutput() { - return this.outputCount++, this.outputSize += j.OUTPUT_SIZE + j.P2WKH_OUTPUT_SIZE, this; + return this.outputCount++, this.outputSize += tt.OUTPUT_SIZE + tt.P2WKH_OUTPUT_SIZE, this; } addP2TROutput() { - return this.outputCount++, this.outputSize += j.OUTPUT_SIZE + j.P2TR_OUTPUT_SIZE, this; + return this.outputCount++, this.outputSize += tt.OUTPUT_SIZE + tt.P2TR_OUTPUT_SIZE, this; } vsize() { const t = (i) => i < 253 ? 1 : i < 65535 ? 3 : i < 4294967295 ? 5 : 9, n = t(this.inputCount), r = t(this.outputCount); - let s = (j.BASE_TX_SIZE + n + this.inputSize + r + this.outputSize) * j.WITNESS_SCALE_FACTOR; - return this.hasWitness && (s += j.WITNESS_HEADER_SIZE + this.inputWitnessSize), id(s); - } -} -j.P2PKH_SCRIPT_SIG_SIZE = 108; -j.INPUT_SIZE = 41; -j.BASE_CONTROL_BLOCK_SIZE = 33; -j.OUTPUT_SIZE = 9; -j.P2WKH_OUTPUT_SIZE = 22; -j.BASE_TX_SIZE = 10; -j.WITNESS_HEADER_SIZE = 2; -j.WITNESS_SCALE_FACTOR = 4; -j.P2TR_OUTPUT_SIZE = 34; -const id = (e) => { - const t = BigInt(Math.ceil(e / j.WITNESS_SCALE_FACTOR)); + let s = (tt.BASE_TX_SIZE + n + this.inputSize + r + this.outputSize) * tt.WITNESS_SCALE_FACTOR; + return this.hasWitness && (s += tt.WITNESS_HEADER_SIZE + this.inputWitnessSize), $p(s); + } +} +tt.P2PKH_SCRIPT_SIG_SIZE = 108; +tt.INPUT_SIZE = 41; +tt.BASE_CONTROL_BLOCK_SIZE = 33; +tt.OUTPUT_SIZE = 9; +tt.P2WKH_OUTPUT_SIZE = 22; +tt.BASE_TX_SIZE = 10; +tt.WITNESS_HEADER_SIZE = 2; +tt.WITNESS_SCALE_FACTOR = 4; +tt.P2TR_OUTPUT_SIZE = 34; +const $p = (e) => { + const t = BigInt(Math.ceil(e / tt.WITNESS_SCALE_FACTOR)); return { value: t, fee: (n) => n * t }; }; -var pt; +var yt; (function(e) { - function t(p) { - return typeof p == "object" && p !== null && "type" in p; + function t(g) { + return typeof g == "object" && g !== null && "type" in g; } e.isBase = t; - function n(p) { - return p.type === "INIT_WALLET" && "arkServerUrl" in p && typeof p.arkServerUrl == "string" && "privateKey" in p && typeof p.privateKey == "string" && ("arkServerPublicKey" in p ? p.arkServerPublicKey === void 0 || typeof p.arkServerPublicKey == "string" : !0); + function n(g) { + return g.type === "INIT_WALLET" && "arkServerUrl" in g && typeof g.arkServerUrl == "string" && "privateKey" in g && typeof g.privateKey == "string" && ("arkServerPublicKey" in g ? g.arkServerPublicKey === void 0 || typeof g.arkServerPublicKey == "string" : !0); } e.isInitWallet = n; - function r(p) { - return p.type === "SETTLE"; + function r(g) { + return g.type === "SETTLE"; } e.isSettle = r; - function o(p) { - return p.type === "GET_ADDRESS"; + function o(g) { + return g.type === "GET_ADDRESS"; } e.isGetAddress = o; - function s(p) { - return p.type === "GET_BOARDING_ADDRESS"; + function s(g) { + return g.type === "GET_BOARDING_ADDRESS"; } e.isGetBoardingAddress = s; - function i(p) { - return p.type === "GET_BALANCE"; + function i(g) { + return g.type === "GET_BALANCE"; } e.isGetBalance = i; - function c(p) { - return p.type === "GET_VTXOS"; + function c(g) { + return g.type === "GET_VTXOS"; } e.isGetVtxos = c; - function a(p) { - return p.type === "GET_VIRTUAL_COINS"; + function a(g) { + return g.type === "GET_VIRTUAL_COINS"; } e.isGetVirtualCoins = a; - function u(p) { - return p.type === "GET_BOARDING_UTXOS"; + function u(g) { + return g.type === "GET_BOARDING_UTXOS"; } e.isGetBoardingUtxos = u; - function f(p) { - return p.type === "SEND_BITCOIN" && "params" in p && p.params !== null && typeof p.params == "object" && "address" in p.params && typeof p.params.address == "string" && "amount" in p.params && typeof p.params.amount == "number"; + function f(g) { + return g.type === "SEND_BITCOIN" && "params" in g && g.params !== null && typeof g.params == "object" && "address" in g.params && typeof g.params.address == "string" && "amount" in g.params && typeof g.params.amount == "number"; } e.isSendBitcoin = f; - function l(p) { - return p.type === "GET_TRANSACTION_HISTORY"; + function l(g) { + return g.type === "GET_TRANSACTION_HISTORY"; } e.isGetTransactionHistory = l; - function d(p) { - return p.type === "GET_STATUS"; + function d(g) { + return g.type === "GET_STATUS"; } e.isGetStatus = d; - function h(p) { - return p.type === "CLEAR"; + function h(g) { + return g.type === "CLEAR"; } e.isClear = h; - function w(p) { - return p.type === "RELOAD_WALLET"; + function w(g) { + return g.type === "RELOAD_WALLET"; } e.isReloadWallet = w; -})(pt || (pt = {})); -class cd { - constructor(t = sd, n = 1, r = () => { +})(yt || (yt = {})); +class Up { + constructor(t = Op, n = 1, r = () => { }) { - this.dbName = t, this.dbVersion = n, this.messageCallback = r, this.storage = new od(t, n), this.walletRepository = new To(this.storage); + this.dbName = t, this.dbVersion = n, this.messageCallback = r, this.storage = new Bp(t, n), this.walletRepository = new gs(this.storage); } /** * Get spendable vtxos for the current wallet address @@ -8941,7 +10580,7 @@ class cd { if (!this.wallet) return []; const t = await this.wallet.getAddress(); - return (await this.walletRepository.getVtxos(t)).filter(te); + return (await this.walletRepository.getVtxos(t)).filter(ae); } /** * Get swept vtxos for the current wallet address @@ -8960,8 +10599,8 @@ class cd { return { spendable: [], spent: [] }; const t = await this.wallet.getAddress(), n = await this.walletRepository.getVtxos(t); return { - spendable: n.filter(te), - spent: n.filter((r) => !te(r)) + spendable: n.filter(ae), + spent: n.filter((r) => !ae(r)) }; } /** @@ -8980,7 +10619,7 @@ class cd { try { const { boardingTxs: n, commitmentsToIgnore: r } = await this.wallet.getBoardingTxs(), { spendable: o, spent: s } = await this.getAllVtxos(); console.log("getTransactionHistory - vtxosToTxs:", o); - const i = ta(o, s, r); + const i = Vu(o, s, r); t = [...n, ...i], t.sort( // place createdAt = 0 (unconfirmed txs) first, then descending (c, a) => c.createdAt === 0 ? -1 : a.createdAt === 0 ? 1 : a.createdAt - c.createdAt @@ -9000,7 +10639,7 @@ class cd { })); } async clear() { - this.incomingFundsSubscription && this.incomingFundsSubscription(), await this.storage.clear(), this.walletRepository = new To(this.storage), this.wallet = void 0, this.arkProvider = void 0, this.indexerProvider = void 0; + this.incomingFundsSubscription && this.incomingFundsSubscription(), await this.storage.clear(), this.walletRepository = new gs(this.storage), this.wallet = void 0, this.arkProvider = void 0, this.indexerProvider = void 0; } async reload() { await this.onWalletInitialized(); @@ -9008,144 +10647,144 @@ class cd { async onWalletInitialized() { if (!this.wallet || !this.arkProvider || !this.indexerProvider || !this.wallet.offchainTapscript || !this.wallet.boardingTapscript) return; - const t = S.encode(this.wallet.offchainTapscript.pkScript), r = (await this.indexerProvider.getVtxos({ + const t = $.encode(this.wallet.offchainTapscript.pkScript), r = (await this.indexerProvider.getVtxos({ scripts: [t] - })).vtxos.map((a) => Ve(this.wallet, a)), o = await this.wallet.getAddress(); + })).vtxos.map((a) => nn(this.wallet, a)), o = await this.wallet.getAddress(); await this.walletRepository.saveVtxos(o, r); const s = await this.wallet.getBoardingAddress(), i = await this.wallet.onchainProvider.getCoins(s); - await this.walletRepository.saveUtxos(s, i.map((a) => vo(this.wallet, a))); + await this.walletRepository.saveUtxos(s, i.map((a) => ws(this.wallet, a))); const c = await this.getTransactionHistory(); c && await this.walletRepository.saveTransactions(o, c), this.incomingFundsSubscription && this.incomingFundsSubscription(), this.incomingFundsSubscription = await this.wallet.notifyIncomingFunds(async (a) => { if (a.type === "vtxo") { - const u = a.newVtxos.length > 0 ? a.newVtxos.map((l) => Ve(this.wallet, l)) : [], f = a.spentVtxos.length > 0 ? a.spentVtxos.map((l) => Ve(this.wallet, l)) : []; + const u = a.newVtxos.length > 0 ? a.newVtxos.map((l) => nn(this.wallet, l)) : [], f = a.spentVtxos.length > 0 ? a.spentVtxos.map((l) => nn(this.wallet, l)) : []; if ([...u, ...f].length === 0) return; await this.walletRepository.saveVtxos(o, [ ...u, ...f - ]), await this.sendMessageToAllClients(N.vtxoUpdate(u, f)); + ]), await this.sendMessageToAllClients(V.vtxoUpdate(u, f)); } if (a.type === "utxo") { - const u = a.coins.map((l) => vo(this.wallet, l)), f = await this.wallet?.getBoardingAddress(); - await this.walletRepository.clearUtxos(f), await this.walletRepository.saveUtxos(f, u), await this.sendMessageToAllClients(N.utxoUpdate(u)); + const u = a.coins.map((l) => ws(this.wallet, l)), f = await this.wallet?.getBoardingAddress(); + await this.walletRepository.clearUtxos(f), await this.walletRepository.saveUtxos(f, u), await this.sendMessageToAllClients(V.utxoUpdate(u)); } }); } async handleClear(t) { - await this.clear(), pt.isBase(t.data) && t.source?.postMessage(N.clearResponse(t.data.id, !0)); + await this.clear(), yt.isBase(t.data) && t.source?.postMessage(V.clearResponse(t.data.id, !0)); } async handleInitWallet(t) { const n = t.data; - if (!pt.isInitWallet(n)) { - console.error("Invalid INIT_WALLET message format", n), t.source?.postMessage(N.error(n.id, "Invalid INIT_WALLET message format")); + if (!yt.isInitWallet(n)) { + console.error("Invalid INIT_WALLET message format", n), t.source?.postMessage(V.error(n.id, "Invalid INIT_WALLET message format")); return; } if (!n.privateKey) { const r = "Missing privateKey"; - t.source?.postMessage(N.error(n.id, r)), console.error(r); + t.source?.postMessage(V.error(n.id, r)), console.error(r); return; } try { - const { arkServerPublicKey: r, arkServerUrl: o, privateKey: s } = n, i = un.fromHex(s); - this.arkProvider = new ea(o), this.indexerProvider = new ra(o), this.wallet = await Ze.create({ + const { arkServerPublicKey: r, arkServerUrl: o, privateKey: s } = n, i = Rn.fromHex(s); + this.arkProvider = new Du(o), this.indexerProvider = new Ku(o), this.wallet = await wn.create({ identity: i, arkServerUrl: o, arkServerPublicKey: r, storage: this.storage // Use unified storage for wallet too - }), t.source?.postMessage(N.walletInitialized(n.id)), await this.onWalletInitialized(); + }), t.source?.postMessage(V.walletInitialized(n.id)), await this.onWalletInitialized(); } catch (r) { console.error("Error initializing wallet:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleSettle(t) { const n = t.data; - if (!pt.isSettle(n)) { - console.error("Invalid SETTLE message format", n), t.source?.postMessage(N.error(n.id, "Invalid SETTLE message format")); + if (!yt.isSettle(n)) { + console.error("Invalid SETTLE message format", n), t.source?.postMessage(V.error(n.id, "Invalid SETTLE message format")); return; } try { if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } const r = await this.wallet.settle(n.params, (o) => { - t.source?.postMessage(N.settleEvent(n.id, o)); + t.source?.postMessage(V.settleEvent(n.id, o)); }); - t.source?.postMessage(N.settleSuccess(n.id, r)); + t.source?.postMessage(V.settleSuccess(n.id, r)); } catch (r) { console.error("Error settling:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleSendBitcoin(t) { const n = t.data; - if (!pt.isSendBitcoin(n)) { - console.error("Invalid SEND_BITCOIN message format", n), t.source?.postMessage(N.error(n.id, "Invalid SEND_BITCOIN message format")); + if (!yt.isSendBitcoin(n)) { + console.error("Invalid SEND_BITCOIN message format", n), t.source?.postMessage(V.error(n.id, "Invalid SEND_BITCOIN message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { const r = await this.wallet.sendBitcoin(n.params); - t.source?.postMessage(N.sendBitcoinSuccess(n.id, r)); + t.source?.postMessage(V.sendBitcoinSuccess(n.id, r)); } catch (r) { console.error("Error sending bitcoin:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetAddress(t) { const n = t.data; - if (!pt.isGetAddress(n)) { - console.error("Invalid GET_ADDRESS message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_ADDRESS message format")); + if (!yt.isGetAddress(n)) { + console.error("Invalid GET_ADDRESS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_ADDRESS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { const r = await this.wallet.getAddress(); - t.source?.postMessage(N.address(n.id, r)); + t.source?.postMessage(V.address(n.id, r)); } catch (r) { console.error("Error getting address:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetBoardingAddress(t) { const n = t.data; - if (!pt.isGetBoardingAddress(n)) { - console.error("Invalid GET_BOARDING_ADDRESS message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_BOARDING_ADDRESS message format")); + if (!yt.isGetBoardingAddress(n)) { + console.error("Invalid GET_BOARDING_ADDRESS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_BOARDING_ADDRESS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { const r = await this.wallet.getBoardingAddress(); - t.source?.postMessage(N.boardingAddress(n.id, r)); + t.source?.postMessage(V.boardingAddress(n.id, r)); } catch (r) { console.error("Error getting boarding address:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetBalance(t) { const n = t.data; - if (!pt.isGetBalance(n)) { - console.error("Invalid GET_BALANCE message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_BALANCE message format")); + if (!yt.isGetBalance(n)) { + console.error("Invalid GET_BALANCE message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_BALANCE message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { @@ -9161,9 +10800,9 @@ class cd { for (const h of o) h.virtualStatus.state === "settled" ? a += h.value : h.virtualStatus.state === "preconfirmed" && (u += h.value); for (const h of s) - te(h) && (f += h.value); + ae(h) && (f += h.value); const l = i + c, d = a + u + f; - t.source?.postMessage(N.balance(n.id, { + t.source?.postMessage(V.balance(n.id, { boarding: { confirmed: i, unconfirmed: c, @@ -9178,79 +10817,79 @@ class cd { } catch (r) { console.error("Error getting balance:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetVtxos(t) { const n = t.data; - if (!pt.isGetVtxos(n)) { - console.error("Invalid GET_VTXOS message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_VTXOS message format")); + if (!yt.isGetVtxos(n)) { + console.error("Invalid GET_VTXOS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_VTXOS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { - const r = await this.getSpendableVtxos(), o = this.wallet.dustAmount, i = n.filter?.withRecoverable ?? !1 ? r : r.filter((c) => !(o != null && Jc(c, o) || mo(c))); - t.source?.postMessage(N.vtxos(n.id, i)); + const r = await this.getSpendableVtxos(), o = this.wallet.dustAmount, i = n.filter?.withRecoverable ?? !1 ? r : r.filter((c) => !(o != null && Hu(c, o) || fs(c))); + t.source?.postMessage(V.vtxos(n.id, i)); } catch (r) { console.error("Error getting vtxos:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetBoardingUtxos(t) { const n = t.data; - if (!pt.isGetBoardingUtxos(n)) { - console.error("Invalid GET_BOARDING_UTXOS message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_BOARDING_UTXOS message format")); + if (!yt.isGetBoardingUtxos(n)) { + console.error("Invalid GET_BOARDING_UTXOS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_BOARDING_UTXOS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { const r = await this.getAllBoardingUtxos(); - t.source?.postMessage(N.boardingUtxos(n.id, r)); + t.source?.postMessage(V.boardingUtxos(n.id, r)); } catch (r) { console.error("Error getting boarding utxos:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetTransactionHistory(t) { const n = t.data; - if (!pt.isGetTransactionHistory(n)) { - console.error("Invalid GET_TRANSACTION_HISTORY message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_TRANSACTION_HISTORY message format")); + if (!yt.isGetTransactionHistory(n)) { + console.error("Invalid GET_TRANSACTION_HISTORY message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_TRANSACTION_HISTORY message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); return; } try { const r = await this.getTransactionHistory(); - t.source?.postMessage(N.transactionHistory(n.id, r)); + t.source?.postMessage(V.transactionHistory(n.id, r)); } catch (r) { console.error("Error getting transaction history:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(N.error(n.id, o)); + t.source?.postMessage(V.error(n.id, o)); } } async handleGetStatus(t) { const n = t.data; - if (!pt.isGetStatus(n)) { - console.error("Invalid GET_STATUS message format", n), t.source?.postMessage(N.error(n.id, "Invalid GET_STATUS message format")); + if (!yt.isGetStatus(n)) { + console.error("Invalid GET_STATUS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_STATUS message format")); return; } const r = this.wallet ? await this.wallet.identity.xOnlyPublicKey() : void 0; - t.source?.postMessage(N.walletStatus(n.id, this.wallet !== void 0, r)); + t.source?.postMessage(V.walletStatus(n.id, this.wallet !== void 0, r)); } async handleMessage(t) { this.messageCallback(t); const n = t.data; - if (!pt.isBase(n)) { + if (!yt.isBase(n)) { console.warn("Invalid message format", JSON.stringify(n)); return; } @@ -9304,7 +10943,7 @@ class cd { break; } default: - t.source?.postMessage(N.error(n.id, "Unknown message type")); + t.source?.postMessage(V.error(n.id, "Unknown message type")); } } async sendMessageToAllClients(t) { @@ -9316,22 +10955,22 @@ class cd { } async handleReloadWallet(t) { const n = t.data; - if (!pt.isReloadWallet(n)) { - console.error("Invalid RELOAD_WALLET message format", n), t.source?.postMessage(N.error(n.id, "Invalid RELOAD_WALLET message format")); + if (!yt.isReloadWallet(n)) { + console.error("Invalid RELOAD_WALLET message format", n), t.source?.postMessage(V.error(n.id, "Invalid RELOAD_WALLET message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(N.walletReloaded(n.id, !1)); + console.error("Wallet not initialized"), t.source?.postMessage(V.walletReloaded(n.id, !1)); return; } try { - await this.onWalletInitialized(), t.source?.postMessage(N.walletReloaded(n.id, !0)); + await this.onWalletInitialized(), t.source?.postMessage(V.walletReloaded(n.id, !0)); } catch (r) { - console.error("Error reloading wallet:", r), t.source?.postMessage(N.walletReloaded(n.id, !1)); + console.error("Error reloading wallet:", r), t.source?.postMessage(V.walletReloaded(n.id, !1)); } } } -var li; +var Sc; (function(e) { let t; (function(o) { @@ -9354,13 +10993,13 @@ var li; const i = this.toUnroll.chain; for (let u = i.length - 1; u >= 0; u--) { const f = i[u]; - if (!(f.type === _e.COMMITMENT || f.type === _e.UNSPECIFIED)) + if (!(f.type === en.COMMITMENT || f.type === en.UNSPECIFIED)) try { if (!(await this.explorer.getTxStatus(f.txid)).confirmed) return { type: t.WAIT, txid: f.txid, - do: fd(this.explorer, f.txid) + do: Lp(this.explorer, f.txid) }; } catch { s = f; @@ -9378,8 +11017,8 @@ var li; ]); if (c.txs.length === 0) throw new Error(`Tx ${s.txid} not found`); - const a = he.fromPSBT(Et.decode(c.txs[0])); - if (s.type === _e.TREE) { + const a = ke.fromPSBT(It.decode(c.txs[0])); + if (s.type === en.TREE) { const u = a.getInput(0); if (!u) throw new Error("Input not found"); @@ -9394,7 +11033,7 @@ var li; return { type: t.UNROLL, tx: a, - do: ud(this.bumper, this.explorer, a) + do: Np(this.bumper, this.explorer, a) }; } /** @@ -9404,7 +11043,7 @@ var li; async *[Symbol.asyncIterator]() { let s; do { - s !== void 0 && await ad(1e3); + s !== void 0 && await Rp(1e3); const i = await this.next(); await i.do(), yield i, s = i.type; } while (s !== t.DONE); @@ -9414,60 +11053,60 @@ var li; async function r(o, s, i) { const c = await o.onchainProvider.getChainTip(); let a = await o.getVtxos({ withUnrolled: !0 }); - if (a = a.filter((m) => s.includes(m.txid)), a.length === 0) + if (a = a.filter((y) => s.includes(y.txid)), a.length === 0) throw new Error("No vtxos to complete unroll"); const u = []; let f = 0n; - const l = j.create(); - for (const m of a) { - if (!m.isUnrolled) - throw new Error(`Vtxo ${m.txid}:${m.vout} is not fully unrolled, use unroll first`); - const x = await o.onchainProvider.getTxStatus(m.txid); - if (!x.confirmed) - throw new Error(`tx ${m.txid} is not confirmed`); - const b = ld({ height: x.blockHeight, time: x.blockTime }, c, m); - if (!b) - throw new Error(`no available exit path found for vtxo ${m.txid}:${m.vout}`); - const v = Ut.decode(m.tapTree).findLeaf(S.encode(b.script)); + const l = tt.create(); + for (const y of a) { + if (!y.isUnrolled) + throw new Error(`Vtxo ${y.txid}:${y.vout} is not fully unrolled, use unroll first`); + const S = await o.onchainProvider.getTxStatus(y.txid); + if (!S.confirmed) + throw new Error(`tx ${y.txid} is not confirmed`); + const v = Cp({ height: S.blockHeight, time: S.blockTime }, c, y); if (!v) - throw new Error(`spending leaf not found for vtxo ${m.txid}:${m.vout}`); - f += BigInt(m.value), u.push({ - txid: m.txid, - index: m.vout, - tapLeafScript: [v], + throw new Error(`no available exit path found for vtxo ${y.txid}:${y.vout}`); + const O = _t.decode(y.tapTree).findLeaf($.encode(v.script)); + if (!O) + throw new Error(`spending leaf not found for vtxo ${y.txid}:${y.vout}`); + f += BigInt(y.value), u.push({ + txid: y.txid, + index: y.vout, + tapLeafScript: [O], sequence: 4294967294, witnessUtxo: { - amount: BigInt(m.value), - script: Ut.decode(m.tapTree).pkScript + amount: BigInt(y.value), + script: _t.decode(y.tapTree).pkScript }, - sighashType: Ae.DEFAULT - }), l.addTapscriptInput(64, v[1].length, Wt.encode(v[0]).length); + sighashType: Ke.DEFAULT + }), l.addTapscriptInput(64, O[1].length, Zt.encode(O[0]).length); } - const d = new he({ version: 2 }); - for (const m of u) - d.addInput(m); + const d = new ke({ version: 2 }); + for (const y of u) + d.addInput(y); l.addP2TROutput(); let h = await o.onchainProvider.getFeeRate(); - (!h || h < Ze.MIN_FEE_RATE) && (h = Ze.MIN_FEE_RATE); + (!h || h < wn.MIN_FEE_RATE) && (h = wn.MIN_FEE_RATE); const w = l.vsize().fee(BigInt(h)); if (w > f) throw new Error("fee amount is greater than the total amount"); d.addOutputAddress(i, f - w); - const p = await o.identity.sign(d); - return p.finalize(), await o.onchainProvider.broadcastTransaction(p.hex), p.id; + const g = await o.identity.sign(d); + return g.finalize(), await o.onchainProvider.broadcastTransaction(g.hex), g.id; } e.completeUnroll = r; -})(li || (li = {})); -function ad(e) { +})(Sc || (Sc = {})); +function Rp(e) { return new Promise((t) => setTimeout(t, e)); } -function ud(e, t, n) { +function Np(e, t, n) { return async () => { const [r, o] = await e.bumpP2A(n); await t.broadcastTransaction(r, o); }; } -function fd(e, t) { +function Lp(e, t) { return () => new Promise((n, r) => { const o = setInterval(async () => { try { @@ -9478,8 +11117,8 @@ function fd(e, t) { }, 5e3); }); } -function ld(e, t, n) { - const r = Ut.decode(n.tapTree).exitPaths(); +function Cp(e, t, n) { + const r = _t.decode(n.tapTree).exitPaths(); for (const o of r) if (o.params.timelock.type === "blocks") { if (t.height >= e.height + Number(o.params.timelock.value)) @@ -9487,17 +11126,17 @@ function ld(e, t, n) { } else if (t.time >= e.time + Number(o.params.timelock.value)) return o; } -const sa = new cd(); -sa.start().catch(console.error); -const ia = "arkade-cache-v1"; +const Wu = new Up(); +Wu.start().catch(console.error); +const zu = "arkade-cache-v1"; self.addEventListener("install", (e) => { - e.waitUntil(caches.open(ia)), self.skipWaiting(); + e.waitUntil(caches.open(zu)), self.skipWaiting(); }); self.addEventListener("activate", (e) => { e.waitUntil( caches.keys().then((t) => Promise.all( t.map((n) => { - if (n !== ia) + if (n !== zu) return caches.delete(n); }) )) @@ -9511,5 +11150,5 @@ self.addEventListener("activate", (e) => { }), self.clients.claim(); }); self.addEventListener("message", (e) => { - e.data && e.data.type === "RELOAD_WALLET" && e.waitUntil(sa.reload().catch(console.error)); + e.data && e.data.type === "RELOAD_WALLET" && e.waitUntil(Wu.reload().catch(console.error)); }); diff --git a/src/lib/urls.ts b/src/lib/urls.ts index b9dac8c13..ee6d61b6f 100644 --- a/src/lib/urls.ts +++ b/src/lib/urls.ts @@ -1,5 +1,6 @@ import { Network } from '@arkade-os/boltz-swap' +// Default urls to be provided const DEFAULT_ARK_SERVER_URLS: Partial> = {} const DEFAULT_BOLTZ_SERVER_URLS: Partial> = { @@ -8,127 +9,152 @@ const DEFAULT_BOLTZ_SERVER_URLS: Partial> = { regtest: 'http://localhost:9069', } +// Default urls to be provided const DEFAULT_ESPLORA_SERVER_URLS: Partial> = {} +// Default urls to be provided const DEFAULT_INDEXER_SERVER_URLS: Partial> = {} export function getArkUrl(network: Network): string { - const defaultUrl = DEFAULT_ARK_SERVER_URLS[network] + let arkUrl = DEFAULT_ARK_SERVER_URLS[network] if (network === 'bitcoin') { - return importArkBitcoinUrl() ?? defaultUrl + arkUrl = importArkBitcoinUrl() } if (network === 'mutinynet') { - return importArkMutinyUrl() ?? defaultUrl + arkUrl = importArkMutinyUrl() } if (network === 'regtest') { - return importArkRegTestUrl() ?? defaultUrl + arkUrl = importArkRegTestUrl() } - return importArkSigNetUrl() ?? defaultUrl + if (network === 'signet') { + arkUrl = importArkSigNetUrl() + } + if (!arkUrl) { + throw new Error(`No url found for ark on ${network} network`) + } + return arkUrl } export function getBoltzUrl(network: Network): string { - const defaultUrl = DEFAULT_BOLTZ_SERVER_URLS[network] - + let boltztUrl = DEFAULT_BOLTZ_SERVER_URLS[network] if (network === 'bitcoin') { - return importBoltzBitcoinUrl() ?? defaultUrl + boltztUrl = importBoltzBitcoinUrl() } if (network === 'mutinynet') { - return importBoltzMutinyUrl() ?? defaultUrl + boltztUrl = importBoltzMutinyUrl() } if (network === 'regtest') { - return importBoltzRegTestUrl() ?? defaultUrl + boltztUrl = importBoltzRegTestUrl() + } + if (network === 'signet') { + boltztUrl = importBoltzSigNetUrl() + } + if (!boltztUrl) { + throw new Error(`No url found for boltz on ${network} network`) } - return importBoltzSigNetUrl() ?? defaultUrl + return boltztUrl } export function getEsploraUrl(network: Network): string { - const defaultUrl = DEFAULT_ESPLORA_SERVER_URLS[network] + let esploraUrl = DEFAULT_ESPLORA_SERVER_URLS[network] if (network === 'bitcoin') { - return importEsploraBitcoinUrl() ?? defaultUrl + esploraUrl = importEsploraBitcoinUrl() } if (network === 'mutinynet') { - return importEsploraMutinyUrl() ?? defaultUrl + esploraUrl = importEsploraMutinyUrl() } if (network === 'regtest') { - return importEsploraRegTestUrl() ?? defaultUrl + esploraUrl = importEsploraRegTestUrl() } - return importEsploraSigNetUrl() ?? defaultUrl + if (network === 'signet') { + esploraUrl = importEsploraSigNetUrl() + } + if (!esploraUrl) { + throw new Error(`No url found for esplora on ${network} network`) + } + return esploraUrl } export function getIndexerUrl(network: Network): string { - const defaultUrl = DEFAULT_INDEXER_SERVER_URLS[network] + let indexerUrl = DEFAULT_INDEXER_SERVER_URLS[network] if (network === 'bitcoin') { - return importIndexerBitcoinUrl() ?? defaultUrl + indexerUrl = importIndexerBitcoinUrl() } if (network === 'mutinynet') { - return importIndexerMutinyUrl() ?? defaultUrl + indexerUrl = importIndexerMutinyUrl() } if (network === 'regtest') { - return importIndexerRegTestUrl() ?? defaultUrl + indexerUrl = importIndexerRegTestUrl() + } + if (network === 'signet') { + indexerUrl = importIndexerSigNetUrl() + } + if (!indexerUrl) { + throw new Error(`No url found for indexer on ${network} network`) } - return importIndexerSigNetUrl() ?? defaultUrl + return indexerUrl } -function importArkBitcoinUrl(): string { - return import.meta.env.VITE_ARK_SERVER_URL_BITCOIN ?? '' +function importArkBitcoinUrl(): string | undefined { + return import.meta.env.VITE_ARK_SERVER_URL_BITCOIN } -function importArkMutinyUrl(): string { - return import.meta.env.VITE_ARK_SERVER_URL_MUTINYNET ?? '' +function importArkMutinyUrl(): string | undefined { + return import.meta.env.VITE_ARK_SERVER_URL_MUTINYNET } -function importArkSigNetUrl(): string { - return import.meta.env.VITE_ARK_SERVER_URL_SIGNET ?? '' +function importArkSigNetUrl(): string | undefined { + return import.meta.env.VITE_ARK_SERVER_URL_SIGNET } -function importArkRegTestUrl(): string { - return import.meta.env.VITE_ARK_SERVER_URL_REGTEST ?? '' +function importArkRegTestUrl(): string | undefined { + return import.meta.env.VITE_ARK_SERVER_URL_REGTEST } -function importBoltzBitcoinUrl(): string { - return import.meta.env.VITE_BOLTZ_URL_BITCOIN ?? '' +function importBoltzBitcoinUrl(): string | undefined { + return import.meta.env.VITE_BOLTZ_URL_BITCOIN } -function importBoltzMutinyUrl(): string { - return import.meta.env.VITE_BOLTZ_URL_MUTINYNET ?? '' +function importBoltzMutinyUrl(): string | undefined { + return import.meta.env.VITE_BOLTZ_URL_MUTINYNET } -function importBoltzSigNetUrl(): string { - return import.meta.env.VITE_BOLTZ_URL_SIGNET ?? '' +function importBoltzSigNetUrl(): string | undefined { + return import.meta.env.VITE_BOLTZ_URL_SIGNET } -function importBoltzRegTestUrl(): string { - return import.meta.env.VITE_BOLTZ_URL_REGTEST ?? '' +function importBoltzRegTestUrl(): string | undefined { + return import.meta.env.VITE_BOLTZ_URL_REGTEST } -function importEsploraBitcoinUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_BITCOIN ?? '' +function importEsploraBitcoinUrl(): string | undefined { + return import.meta.env.VITE_ESPLORA_URL_BITCOIN } -function importEsploraMutinyUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_MUTINYNET ?? '' +function importEsploraMutinyUrl(): string | undefined { + return import.meta.env.VITE_ESPLORA_URL_MUTINYNET } -function importEsploraSigNetUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_SIGNET ?? '' +function importEsploraSigNetUrl(): string | undefined { + return import.meta.env.VITE_ESPLORA_URL_SIGNET } -function importEsploraRegTestUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_REGTEST ?? '' +function importEsploraRegTestUrl(): string | undefined { + return import.meta.env.VITE_ESPLORA_URL_REGTEST } -function importIndexerBitcoinUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_BITCOIN ?? '' +function importIndexerBitcoinUrl(): string | undefined { + return import.meta.env.VITE_INDEXER_URL_BITCOIN } -function importIndexerMutinyUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_MUTINYNET ?? '' +function importIndexerMutinyUrl(): string | undefined { + return import.meta.env.VITE_INDEXER_URL_MUTINYNET } -function importIndexerSigNetUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_SIGNET ?? '' +function importIndexerSigNetUrl(): string | undefined { + return import.meta.env.VITE_INDEXER_URL_SIGNET } -function importIndexerRegTestUrl(): string { - return import.meta.env.VITE_ESPLORA_URL_REGTEST ?? '' +function importIndexerRegTestUrl(): string | undefined { + return import.meta.env.VITE_INDEXER_URL_REGTEST } From fce75eb0add35fd769a634b524cad23d6419723c Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Tue, 21 Oct 2025 22:40:38 +0200 Subject: [PATCH 4/8] Fix inverted fallback logic --- src/lib/urls.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lib/urls.ts b/src/lib/urls.ts index ee6d61b6f..4b02950f3 100644 --- a/src/lib/urls.ts +++ b/src/lib/urls.ts @@ -18,16 +18,16 @@ const DEFAULT_INDEXER_SERVER_URLS: Partial> = {} export function getArkUrl(network: Network): string { let arkUrl = DEFAULT_ARK_SERVER_URLS[network] if (network === 'bitcoin') { - arkUrl = importArkBitcoinUrl() + arkUrl = importArkBitcoinUrl() || arkUrl } if (network === 'mutinynet') { - arkUrl = importArkMutinyUrl() + arkUrl = importArkMutinyUrl() || arkUrl } if (network === 'regtest') { - arkUrl = importArkRegTestUrl() + arkUrl = importArkRegTestUrl() || arkUrl } if (network === 'signet') { - arkUrl = importArkSigNetUrl() + arkUrl = importArkSigNetUrl() || arkUrl } if (!arkUrl) { throw new Error(`No url found for ark on ${network} network`) @@ -38,16 +38,16 @@ export function getArkUrl(network: Network): string { export function getBoltzUrl(network: Network): string { let boltztUrl = DEFAULT_BOLTZ_SERVER_URLS[network] if (network === 'bitcoin') { - boltztUrl = importBoltzBitcoinUrl() + boltztUrl = importBoltzBitcoinUrl() || boltztUrl } if (network === 'mutinynet') { - boltztUrl = importBoltzMutinyUrl() + boltztUrl = importBoltzMutinyUrl() || boltztUrl } if (network === 'regtest') { - boltztUrl = importBoltzRegTestUrl() + boltztUrl = importBoltzRegTestUrl() || boltztUrl } if (network === 'signet') { - boltztUrl = importBoltzSigNetUrl() + boltztUrl = importBoltzSigNetUrl() || boltztUrl } if (!boltztUrl) { throw new Error(`No url found for boltz on ${network} network`) @@ -58,16 +58,16 @@ export function getBoltzUrl(network: Network): string { export function getEsploraUrl(network: Network): string { let esploraUrl = DEFAULT_ESPLORA_SERVER_URLS[network] if (network === 'bitcoin') { - esploraUrl = importEsploraBitcoinUrl() + esploraUrl = importEsploraBitcoinUrl() || esploraUrl } if (network === 'mutinynet') { - esploraUrl = importEsploraMutinyUrl() + esploraUrl = importEsploraMutinyUrl() || esploraUrl } if (network === 'regtest') { - esploraUrl = importEsploraRegTestUrl() + esploraUrl = importEsploraRegTestUrl() || esploraUrl } if (network === 'signet') { - esploraUrl = importEsploraSigNetUrl() + esploraUrl = importEsploraSigNetUrl() || esploraUrl } if (!esploraUrl) { throw new Error(`No url found for esplora on ${network} network`) @@ -78,16 +78,16 @@ export function getEsploraUrl(network: Network): string { export function getIndexerUrl(network: Network): string { let indexerUrl = DEFAULT_INDEXER_SERVER_URLS[network] if (network === 'bitcoin') { - indexerUrl = importIndexerBitcoinUrl() + indexerUrl = importIndexerBitcoinUrl() || indexerUrl } if (network === 'mutinynet') { - indexerUrl = importIndexerMutinyUrl() + indexerUrl = importIndexerMutinyUrl() || indexerUrl } if (network === 'regtest') { - indexerUrl = importIndexerRegTestUrl() + indexerUrl = importIndexerRegTestUrl() || indexerUrl } if (network === 'signet') { - indexerUrl = importIndexerSigNetUrl() + indexerUrl = importIndexerSigNetUrl() || indexerUrl } if (!indexerUrl) { throw new Error(`No url found for indexer on ${network} network`) From b625651c4e0aeba4072e4ab3be698f5cd7acb58a Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Tue, 21 Oct 2025 23:00:32 +0200 Subject: [PATCH 5/8] Change boltztUrl to boltzUrl --- src/lib/urls.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/urls.ts b/src/lib/urls.ts index 4b02950f3..ff9261cbb 100644 --- a/src/lib/urls.ts +++ b/src/lib/urls.ts @@ -36,23 +36,23 @@ export function getArkUrl(network: Network): string { } export function getBoltzUrl(network: Network): string { - let boltztUrl = DEFAULT_BOLTZ_SERVER_URLS[network] + let boltzUrl = DEFAULT_BOLTZ_SERVER_URLS[network] if (network === 'bitcoin') { - boltztUrl = importBoltzBitcoinUrl() || boltztUrl + boltzUrl = importBoltzBitcoinUrl() || boltzUrl } if (network === 'mutinynet') { - boltztUrl = importBoltzMutinyUrl() || boltztUrl + boltzUrl = importBoltzMutinyUrl() || boltzUrl } if (network === 'regtest') { - boltztUrl = importBoltzRegTestUrl() || boltztUrl + boltzUrl = importBoltzRegTestUrl() || boltzUrl } if (network === 'signet') { - boltztUrl = importBoltzSigNetUrl() || boltztUrl + boltzUrl = importBoltzSigNetUrl() || boltzUrl } - if (!boltztUrl) { + if (!boltzUrl) { throw new Error(`No url found for boltz on ${network} network`) } - return boltztUrl + return boltzUrl } export function getEsploraUrl(network: Network): string { From 0e8159a9600c1204ad0527b17662b230c802d523 Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Wed, 22 Oct 2025 11:50:11 +0200 Subject: [PATCH 6/8] Improve url resolution logic and add storage fallback --- src/lib/types.ts | 3 + src/lib/urls.ts | 143 ++++++++++++++++++++------------------- src/providers/config.tsx | 3 + src/vite-env.d.ts | 2 + 4 files changed, 81 insertions(+), 70 deletions(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index d0924ab7c..9ab141051 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -12,6 +12,9 @@ export type Config = { } } aspUrl: string + boltzUrl: string + esploraUrl: string + indexerUrl: string currencyDisplay: CurrencyDisplay fiat: Fiats nostr: boolean diff --git a/src/lib/urls.ts b/src/lib/urls.ts index ff9261cbb..8830b6d16 100644 --- a/src/lib/urls.ts +++ b/src/lib/urls.ts @@ -1,6 +1,9 @@ import { Network } from '@arkade-os/boltz-swap' +import { readConfigFromStorage } from './storage' -// Default urls to be provided +type Service = 'ark' | 'boltz' | 'esplora' | 'indexer' + +// TODO: Default urls to be provided const DEFAULT_ARK_SERVER_URLS: Partial> = {} const DEFAULT_BOLTZ_SERVER_URLS: Partial> = { @@ -9,90 +12,90 @@ const DEFAULT_BOLTZ_SERVER_URLS: Partial> = { regtest: 'http://localhost:9069', } -// Default urls to be provided +// TODO: Default urls to be provided const DEFAULT_ESPLORA_SERVER_URLS: Partial> = {} -// Default urls to be provided +// TODO: Default urls to be provided const DEFAULT_INDEXER_SERVER_URLS: Partial> = {} +const ENV_URLS_IMPORTERS = { + ark: { + bitcoin: importArkBitcoinUrl, + mutinynet: importArkMutinyUrl, + regtest: importArkRegTestUrl, + signet: importArkSigNetUrl, + }, + boltz: { + bitcoin: importBoltzBitcoinUrl, + mutinynet: importBoltzMutinyUrl, + regtest: importBoltzRegTestUrl, + signet: importBoltzSigNetUrl, + }, + esplora: { + bitcoin: importEsploraBitcoinUrl, + mutinynet: importEsploraMutinyUrl, + regtest: importEsploraRegTestUrl, + signet: importEsploraSigNetUrl, + }, + indexer: { + bitcoin: importIndexerBitcoinUrl, + mutinynet: importIndexerMutinyUrl, + regtest: importIndexerRegTestUrl, + signet: importIndexerSigNetUrl, + }, +} + +const STORAGE_URLS_IMPORTERS = { + ark: importArkUrlFromStorage, + boltz: importBoltzUrlFromStorage, + esplora: importEsploraUrlFromStorage, + indexer: importIndexerUrlFromStorage, +} + export function getArkUrl(network: Network): string { - let arkUrl = DEFAULT_ARK_SERVER_URLS[network] - if (network === 'bitcoin') { - arkUrl = importArkBitcoinUrl() || arkUrl - } - if (network === 'mutinynet') { - arkUrl = importArkMutinyUrl() || arkUrl - } - if (network === 'regtest') { - arkUrl = importArkRegTestUrl() || arkUrl - } - if (network === 'signet') { - arkUrl = importArkSigNetUrl() || arkUrl - } - if (!arkUrl) { - throw new Error(`No url found for ark on ${network} network`) - } - return arkUrl + return resolveUrl(network, 'ark', DEFAULT_ARK_SERVER_URLS) } export function getBoltzUrl(network: Network): string { - let boltzUrl = DEFAULT_BOLTZ_SERVER_URLS[network] - if (network === 'bitcoin') { - boltzUrl = importBoltzBitcoinUrl() || boltzUrl - } - if (network === 'mutinynet') { - boltzUrl = importBoltzMutinyUrl() || boltzUrl - } - if (network === 'regtest') { - boltzUrl = importBoltzRegTestUrl() || boltzUrl - } - if (network === 'signet') { - boltzUrl = importBoltzSigNetUrl() || boltzUrl - } - if (!boltzUrl) { - throw new Error(`No url found for boltz on ${network} network`) - } - return boltzUrl + return resolveUrl(network, 'boltz', DEFAULT_BOLTZ_SERVER_URLS) } export function getEsploraUrl(network: Network): string { - let esploraUrl = DEFAULT_ESPLORA_SERVER_URLS[network] - if (network === 'bitcoin') { - esploraUrl = importEsploraBitcoinUrl() || esploraUrl - } - if (network === 'mutinynet') { - esploraUrl = importEsploraMutinyUrl() || esploraUrl - } - if (network === 'regtest') { - esploraUrl = importEsploraRegTestUrl() || esploraUrl - } - if (network === 'signet') { - esploraUrl = importEsploraSigNetUrl() || esploraUrl - } - if (!esploraUrl) { - throw new Error(`No url found for esplora on ${network} network`) - } - return esploraUrl + return resolveUrl(network, 'esplora', DEFAULT_ESPLORA_SERVER_URLS) } export function getIndexerUrl(network: Network): string { - let indexerUrl = DEFAULT_INDEXER_SERVER_URLS[network] - if (network === 'bitcoin') { - indexerUrl = importIndexerBitcoinUrl() || indexerUrl - } - if (network === 'mutinynet') { - indexerUrl = importIndexerMutinyUrl() || indexerUrl - } - if (network === 'regtest') { - indexerUrl = importIndexerRegTestUrl() || indexerUrl - } - if (network === 'signet') { - indexerUrl = importIndexerSigNetUrl() || indexerUrl - } - if (!indexerUrl) { - throw new Error(`No url found for indexer on ${network} network`) + return resolveUrl(network, 'indexer', DEFAULT_INDEXER_SERVER_URLS) +} + +function resolveUrl(network: Network, service: Service, defaults: Partial>): string { + const envImporter = ENV_URLS_IMPORTERS[service][network] + const storageImporter = STORAGE_URLS_IMPORTERS[service] + let url = storageImporter() || envImporter() || defaults[network] + if (!url) { + throw new Error(`No url found for ${service} on ${network} network`) } - return indexerUrl + return url +} + +function importArkUrlFromStorage(): string | undefined { + const config = readConfigFromStorage() + return config?.aspUrl +} + +function importBoltzUrlFromStorage(): string | undefined { + const config = readConfigFromStorage() + return config?.boltzUrl +} + +function importEsploraUrlFromStorage(): string | undefined { + const config = readConfigFromStorage() + return config?.esploraUrl +} + +function importIndexerUrlFromStorage(): string | undefined { + const config = readConfigFromStorage() + return config?.indexerUrl } function importArkBitcoinUrl(): string | undefined { diff --git a/src/providers/config.tsx b/src/providers/config.tsx index a5220d762..1e5bdd065 100644 --- a/src/providers/config.tsx +++ b/src/providers/config.tsx @@ -6,6 +6,9 @@ import { Config, CurrencyDisplay, Fiats, Themes, Unit } from '../lib/types' const defaultConfig: Config = { apps: { boltz: { connected: true } }, aspUrl: defaultArkServer(), + boltzUrl: '', // TODO: Provide default url for Boltz + esploraUrl: '', // TODO: Provide default url for Esplora + indexerUrl: '', // TODO: Provide default url for Indexer currencyDisplay: CurrencyDisplay.Both, fiat: Fiats.USD, nostr: false, diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 02b56df1f..bd4430231 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -5,9 +5,11 @@ interface ImportMetaEnv { readonly BASE_URL: string readonly VITE_SENTRY_DSN?: string readonly VITE_ARK_SERVER_URL_BITCOIN?: string + readonly VITE_ARK_SERVER?: string readonly VITE_ARK_SERVER_URL_MUTINYNET?: string readonly VITE_ARK_SERVER_URL_REGTEST?: string readonly VITE_ARK_SERVER_URL_SIGNET?: string + readonly VITE_BOLTZ_URL?: string readonly VITE_BOLTZ_URL_BITCOIN?: string readonly VITE_BOLTZ_URL_MUTINYNET?: string readonly VITE_BOLTZ_URL_REGTEST?: string From bf721b6aaec9436b0b6ba287e7a1d0aeedd59893 Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Wed, 22 Oct 2025 12:48:59 +0200 Subject: [PATCH 7/8] Add legacy ENV vars --- src/lib/urls.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/lib/urls.ts b/src/lib/urls.ts index 8830b6d16..04f588040 100644 --- a/src/lib/urls.ts +++ b/src/lib/urls.ts @@ -18,7 +18,10 @@ const DEFAULT_ESPLORA_SERVER_URLS: Partial> = {} // TODO: Default urls to be provided const DEFAULT_INDEXER_SERVER_URLS: Partial> = {} -const ENV_URLS_IMPORTERS = { +type EnvUrlImporters = { [S in Service]: { [N in Network]: () => string | undefined } } +type StorageUrlImporters = { [S in Service]: () => string | undefined } + +const ENV_URL_IMPORTERS: EnvUrlImporters = { ark: { bitcoin: importArkBitcoinUrl, mutinynet: importArkMutinyUrl, @@ -43,14 +46,14 @@ const ENV_URLS_IMPORTERS = { regtest: importIndexerRegTestUrl, signet: importIndexerSigNetUrl, }, -} +} as const -const STORAGE_URLS_IMPORTERS = { +const STORAGE_URL_IMPORTERS: StorageUrlImporters = { ark: importArkUrlFromStorage, boltz: importBoltzUrlFromStorage, esplora: importEsploraUrlFromStorage, indexer: importIndexerUrlFromStorage, -} +} as const export function getArkUrl(network: Network): string { return resolveUrl(network, 'ark', DEFAULT_ARK_SERVER_URLS) @@ -69,15 +72,24 @@ export function getIndexerUrl(network: Network): string { } function resolveUrl(network: Network, service: Service, defaults: Partial>): string { - const envImporter = ENV_URLS_IMPORTERS[service][network] - const storageImporter = STORAGE_URLS_IMPORTERS[service] - let url = storageImporter() || envImporter() || defaults[network] + const envImporter = ENV_URL_IMPORTERS[service][network] + const storageImporter = STORAGE_URL_IMPORTERS[service] + let url = envImporter() || legacyImporter(service) || storageImporter() || defaults[network] if (!url) { throw new Error(`No url found for ${service} on ${network} network`) } return url } +function legacyImporter(service: Service): string | undefined { + switch (service) { + case 'ark': + return import.meta.env.VITE_ARK_SERVER + case 'boltz': + return import.meta.env.VITE_BOLTZ_URL + } +} + function importArkUrlFromStorage(): string | undefined { const config = readConfigFromStorage() return config?.aspUrl From b896e104a91b7e6349d22e9d6c586118333111cc Mon Sep 17 00:00:00 2001 From: Shubert Munthali Date: Thu, 27 Nov 2025 20:49:01 +0200 Subject: [PATCH 8/8] Add testnet url --- public/wallet-service-worker.mjs | 5590 +++++++++++++++--------------- src/lib/urls.ts | 21 + src/providers/lightning.tsx | 2 +- src/vite-env.d.ts | 4 + 4 files changed, 2878 insertions(+), 2739 deletions(-) diff --git a/public/wallet-service-worker.mjs b/public/wallet-service-worker.mjs index 8436e7169..a8832b95c 100644 --- a/public/wallet-service-worker.mjs +++ b/public/wallet-service-worker.mjs @@ -1,33 +1,33 @@ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -function ys(e) { +function Ts(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function Te(e, t = "") { +function Ae(e, t = "") { if (!Number.isSafeInteger(e) || e < 0) { const n = t && `"${t}" `; throw new Error(`${n}expected integer >= 0, got ${e}`); } } function G(e, t, n = "") { - const r = ys(e), o = e?.length, s = t !== void 0; + const r = Ts(e), o = e?.length, s = t !== void 0; if (!r || s && o !== t) { const i = n && `"${n}" `, c = s ? ` of length ${t}` : "", a = r ? `length=${o}` : `type=${typeof e}`; throw new Error(i + "expected Uint8Array" + c + ", got " + a); } return e; } -function vc(e) { +function Bc(e) { if (typeof e != "function" || typeof e.create != "function") throw new Error("Hash must wrapped by utils.createHasher"); - Te(e.outputLen), Te(e.blockLen); + Ae(e.outputLen), Ae(e.blockLen); } -function yr(e, t = !0) { +function Er(e, t = !0) { if (e.destroyed) throw new Error("Hash instance has been destroyed"); if (t && e.finished) throw new Error("Hash#digest() has already been called"); } -function Gu(e, t) { +function Ju(e, t) { G(e, void 0, "digestInto() output"); const n = t.outputLen; if (e.length < n) @@ -37,44 +37,44 @@ function rn(...e) { for (let t = 0; t < e.length; t++) e[t].fill(0); } -function ho(e) { +function wo(e) { return new DataView(e.buffer, e.byteOffset, e.byteLength); } -function qt(e, t) { +function jt(e, t) { return e << 32 - t | e >>> t; } -function Zn(e, t) { +function Xn(e, t) { return e << t | e >>> 32 - t >>> 0; } -const Tc = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", qu = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); -function Yr(e) { - if (G(e), Tc) +const Oc = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", tf = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); +function Xr(e) { + if (G(e), Oc) return e.toHex(); let t = ""; for (let n = 0; n < e.length; n++) - t += qu[e[n]]; + t += tf[e[n]]; return t; } -const ne = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; -function ci(e) { - if (e >= ne._0 && e <= ne._9) - return e - ne._0; - if (e >= ne.A && e <= ne.F) - return e - (ne.A - 10); - if (e >= ne.a && e <= ne.f) - return e - (ne.a - 10); +const re = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; +function di(e) { + if (e >= re._0 && e <= re._9) + return e - re._0; + if (e >= re.A && e <= re.F) + return e - (re.A - 10); + if (e >= re.a && e <= re.f) + return e - (re.a - 10); } -function mr(e) { +function xr(e) { if (typeof e != "string") throw new Error("hex string expected, got " + typeof e); - if (Tc) + if (Oc) return Uint8Array.fromHex(e); const t = e.length, n = t / 2; if (t % 2) throw new Error("hex string expected, got unpadded hex of length " + t); const r = new Uint8Array(n); for (let o = 0, s = 0; o < n; o++, s += 2) { - const i = ci(e.charCodeAt(s)), c = ci(e.charCodeAt(s + 1)); + const i = di(e.charCodeAt(s)), c = di(e.charCodeAt(s + 1)); if (i === void 0 || c === void 0) { const a = e[s] + e[s + 1]; throw new Error('hex string expected, got non-hex character "' + a + '" at index ' + s); @@ -83,7 +83,7 @@ function mr(e) { } return r; } -function Kt(...e) { +function Ft(...e) { let t = 0; for (let r = 0; r < e.length; r++) { const o = e[r]; @@ -96,26 +96,26 @@ function Kt(...e) { } return n; } -function Ac(e, t = {}) { +function Uc(e, t = {}) { const n = (o, s) => e(s).update(o).digest(), r = e(void 0); return n.outputLen = r.outputLen, n.blockLen = r.blockLen, n.create = (o) => e(o), Object.assign(n, t), Object.freeze(n); } -function Wn(e = 32) { +function zn(e = 32) { const t = typeof globalThis == "object" ? globalThis.crypto : null; if (typeof t?.getRandomValues != "function") throw new Error("crypto.getRandomValues must be defined"); return t.getRandomValues(new Uint8Array(e)); } -const ju = (e) => ({ +const ef = (e) => ({ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, e]) }); -function Yu(e, t, n) { +function nf(e, t, n) { return e & t ^ ~e & n; } -function Zu(e, t, n) { +function rf(e, t, n) { return e & t ^ e & n ^ t & n; } -let Ic = class { +let $c = class { blockLen; outputLen; padOffset; @@ -128,15 +128,15 @@ let Ic = class { pos = 0; destroyed = !1; constructor(t, n, r, o) { - this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = ho(this.buffer); + this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = wo(this.buffer); } update(t) { - yr(this), G(t); + Er(this), G(t); const { view: n, buffer: r, blockLen: o } = this, s = t.length; for (let i = 0; i < s; ) { const c = Math.min(o - this.pos, s - i); if (c === o) { - const a = ho(t); + const a = wo(t); for (; o <= s - i; i += o) this.process(a, i); continue; @@ -146,21 +146,21 @@ let Ic = class { return this.length += t.length, this.roundClean(), this; } digestInto(t) { - yr(this), Gu(t, this), this.finished = !0; + Er(this), Ju(t, this), this.finished = !0; const { buffer: n, view: r, blockLen: o, isLE: s } = this; let { pos: i } = this; n[i++] = 128, rn(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(r, 0), i = 0); - for (let l = i; l < o; l++) - n[l] = 0; + for (let d = i; d < o; d++) + n[d] = 0; r.setBigUint64(o - 8, BigInt(this.length * 8), s), this.process(r, 0); - const c = ho(t), a = this.outputLen; + const c = wo(t), a = this.outputLen; if (a % 4) throw new Error("_sha2: outputLen must be aligned to 32bit"); const u = a / 4, f = this.get(); if (u > f.length) throw new Error("_sha2: outputLen bigger than state"); - for (let l = 0; l < u; l++) - c.setUint32(4 * l, f[l], s); + for (let d = 0; d < u; d++) + c.setUint32(4 * d, f[d], s); } digest() { const { buffer: t, outputLen: n } = this; @@ -186,7 +186,7 @@ const he = /* @__PURE__ */ Uint32Array.from([ 2600822924, 528734635, 1541459225 -]), Xu = /* @__PURE__ */ Uint32Array.from([ +]), of = /* @__PURE__ */ Uint32Array.from([ 1116352408, 1899447441, 3049323471, @@ -252,7 +252,7 @@ const he = /* @__PURE__ */ Uint32Array.from([ 3204031479, 3329325298 ]), pe = /* @__PURE__ */ new Uint32Array(64); -let Qu = class extends Ic { +let sf = class extends $c { constructor(t) { super(64, t, 8, !1); } @@ -265,15 +265,15 @@ let Qu = class extends Ic { this.A = t | 0, this.B = n | 0, this.C = r | 0, this.D = o | 0, this.E = s | 0, this.F = i | 0, this.G = c | 0, this.H = a | 0; } process(t, n) { - for (let l = 0; l < 16; l++, n += 4) - pe[l] = t.getUint32(n, !1); - for (let l = 16; l < 64; l++) { - const d = pe[l - 15], h = pe[l - 2], w = qt(d, 7) ^ qt(d, 18) ^ d >>> 3, g = qt(h, 17) ^ qt(h, 19) ^ h >>> 10; - pe[l] = g + pe[l - 7] + w + pe[l - 16] | 0; + for (let d = 0; d < 16; d++, n += 4) + pe[d] = t.getUint32(n, !1); + for (let d = 16; d < 64; d++) { + const l = pe[d - 15], h = pe[d - 2], w = jt(l, 7) ^ jt(l, 18) ^ l >>> 3, g = jt(h, 17) ^ jt(h, 19) ^ h >>> 10; + pe[d] = g + pe[d - 7] + w + pe[d - 16] | 0; } let { A: r, B: o, C: s, D: i, E: c, F: a, G: u, H: f } = this; - for (let l = 0; l < 64; l++) { - const d = qt(c, 6) ^ qt(c, 11) ^ qt(c, 25), h = f + d + Yu(c, a, u) + Xu[l] + pe[l] | 0, g = (qt(r, 2) ^ qt(r, 13) ^ qt(r, 22)) + Zu(r, o, s) | 0; + for (let d = 0; d < 64; d++) { + const l = jt(c, 6) ^ jt(c, 11) ^ jt(c, 25), h = f + l + nf(c, a, u) + of[d] + pe[d] | 0, g = (jt(r, 2) ^ jt(r, 13) ^ jt(r, 22)) + rf(r, o, s) | 0; f = u, u = a, a = c, c = i + h | 0, i = s, s = o, o = r, r = h + g | 0; } r = r + this.A | 0, o = o + this.B | 0, s = s + this.C | 0, i = i + this.D | 0, c = c + this.E | 0, a = a + this.F | 0, u = u + this.G | 0, f = f + this.H | 0, this.set(r, o, s, i, c, a, u, f); @@ -284,7 +284,7 @@ let Qu = class extends Ic { destroy() { this.set(0, 0, 0, 0, 0, 0, 0, 0), rn(this.buffer); } -}, Ju = class extends Qu { +}, cf = class extends sf { // We cannot use array here since array allows indexing by variable // which means optimizer/compiler cannot use registers. A = he[0] | 0; @@ -299,53 +299,53 @@ let Qu = class extends Ic { super(32); } }; -const wt = /* @__PURE__ */ Ac( - () => new Ju(), - /* @__PURE__ */ ju(1) +const yt = /* @__PURE__ */ Uc( + () => new cf(), + /* @__PURE__ */ ef(1) ); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const ms = /* @__PURE__ */ BigInt(0), Ro = /* @__PURE__ */ BigInt(1); -function br(e, t = "") { +const vs = /* @__PURE__ */ BigInt(0), Vo = /* @__PURE__ */ BigInt(1); +function Sr(e, t = "") { if (typeof e != "boolean") { const n = t && `"${t}" `; throw new Error(n + "expected boolean, got type=" + typeof e); } return e; } -function kc(e) { +function Rc(e) { if (typeof e == "bigint") { - if (!fr(e)) + if (!hr(e)) throw new Error("positive bigint expected, got " + e); } else - Te(e); + Ae(e); return e; } -function Xn(e) { - const t = kc(e).toString(16); +function Qn(e) { + const t = Rc(e).toString(16); return t.length & 1 ? "0" + t : t; } -function Bc(e) { +function Nc(e) { if (typeof e != "string") throw new Error("hex string expected, got " + typeof e); - return e === "" ? ms : BigInt("0x" + e); + return e === "" ? vs : BigInt("0x" + e); } -function le(e) { - return Bc(Yr(e)); +function de(e) { + return Nc(Xr(e)); } -function Oc(e) { - return Bc(Yr(tf(G(e)).reverse())); +function Lc(e) { + return Nc(Xr(af(G(e)).reverse())); } -function zn(e, t) { - Te(t), e = kc(e); - const n = mr(e.toString(16).padStart(t * 2, "0")); +function Gn(e, t) { + Ae(t), e = Rc(e); + const n = xr(e.toString(16).padStart(t * 2, "0")); if (n.length !== t) throw new Error("number too large"); return n; } -function $c(e, t) { - return zn(e, t).reverse(); +function _c(e, t) { + return Gn(e, t).reverse(); } -function Ln(e, t) { +function Cn(e, t) { if (e.length !== t.length) return !1; let n = 0; @@ -353,10 +353,10 @@ function Ln(e, t) { n |= e[r] ^ t[r]; return n === 0; } -function tf(e) { +function af(e) { return Uint8Array.from(e); } -function ef(e) { +function uf(e) { return Uint8Array.from(e, (t, n) => { const r = t.charCodeAt(0); if (t.length !== 1 || r > 127) @@ -364,51 +364,51 @@ function ef(e) { return r; }); } -const fr = (e) => typeof e == "bigint" && ms <= e; -function nf(e, t, n) { - return fr(e) && fr(t) && fr(n) && t <= e && e < n; +const hr = (e) => typeof e == "bigint" && vs <= e; +function ff(e, t, n) { + return hr(e) && hr(t) && hr(n) && t <= e && e < n; } -function Uc(e, t, n, r) { - if (!nf(t, n, r)) +function Cc(e, t, n, r) { + if (!ff(t, n, r)) throw new Error("expected valid " + e + ": " + n + " <= n < " + r + ", got " + t); } -function rf(e) { +function df(e) { let t; - for (t = 0; e > ms; e >>= Ro, t += 1) + for (t = 0; e > vs; e >>= Vo, t += 1) ; return t; } -const bs = (e) => (Ro << BigInt(e)) - Ro; -function of(e, t, n) { - if (Te(e, "hashLen"), Te(t, "qByteLen"), typeof n != "function") +const As = (e) => (Vo << BigInt(e)) - Vo; +function lf(e, t, n) { + if (Ae(e, "hashLen"), Ae(t, "qByteLen"), typeof n != "function") throw new Error("hmacFn must be a function"); const r = (y) => new Uint8Array(y), o = Uint8Array.of(), s = Uint8Array.of(0), i = Uint8Array.of(1), c = 1e3; let a = r(e), u = r(e), f = 0; - const l = () => { + const d = () => { a.fill(1), u.fill(0), f = 0; - }, d = (...y) => n(u, Kt(a, ...y)), h = (y = o) => { - u = d(s, y), a = d(), y.length !== 0 && (u = d(i, y), a = d()); + }, l = (...y) => n(u, Ft(a, ...y)), h = (y = o) => { + u = l(s, y), a = l(), y.length !== 0 && (u = l(i, y), a = l()); }, w = () => { if (f++ >= c) throw new Error("drbg: tried max amount of iterations"); let y = 0; const S = []; for (; y < t; ) { - a = d(); - const v = a.slice(); - S.push(v), y += a.length; + a = l(); + const T = a.slice(); + S.push(T), y += a.length; } - return Kt(...S); + return Ft(...S); }; return (y, S) => { - l(), h(y); - let v; - for (; !(v = S(w())); ) + d(), h(y); + let T; + for (; !(T = S(w())); ) h(); - return l(), v; + return d(), T; }; } -function Es(e, t = {}, n = {}) { +function Is(e, t = {}, n = {}) { if (!e || typeof e != "object") throw new Error("expected valid options object"); function r(s, i, c) { @@ -422,7 +422,7 @@ function Es(e, t = {}, n = {}) { const o = (s, i) => Object.entries(s).forEach(([c, a]) => r(c, a, i)); o(t, !1), o(n, !0); } -function ai(e) { +function li(e) { const t = /* @__PURE__ */ new WeakMap(); return (n, ...r) => { const o = t.get(n); @@ -433,91 +433,91 @@ function ai(e) { }; } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const xt = /* @__PURE__ */ BigInt(0), mt = /* @__PURE__ */ BigInt(1), Ce = /* @__PURE__ */ BigInt(2), Rc = /* @__PURE__ */ BigInt(3), Nc = /* @__PURE__ */ BigInt(4), Lc = /* @__PURE__ */ BigInt(5), sf = /* @__PURE__ */ BigInt(7), Cc = /* @__PURE__ */ BigInt(8), cf = /* @__PURE__ */ BigInt(9), _c = /* @__PURE__ */ BigInt(16); -function Vt(e, t) { +const St = /* @__PURE__ */ BigInt(0), bt = /* @__PURE__ */ BigInt(1), Ce = /* @__PURE__ */ BigInt(2), Pc = /* @__PURE__ */ BigInt(3), Vc = /* @__PURE__ */ BigInt(4), Hc = /* @__PURE__ */ BigInt(5), hf = /* @__PURE__ */ BigInt(7), Dc = /* @__PURE__ */ BigInt(8), pf = /* @__PURE__ */ BigInt(9), Mc = /* @__PURE__ */ BigInt(16); +function Ht(e, t) { const n = e % t; - return n >= xt ? n : t + n; + return n >= St ? n : t + n; } function Rt(e, t, n) { let r = e; - for (; t-- > xt; ) + for (; t-- > St; ) r *= r, r %= n; return r; } -function ui(e, t) { - if (e === xt) +function hi(e, t) { + if (e === St) throw new Error("invert: expected non-zero number"); - if (t <= xt) + if (t <= St) throw new Error("invert: expected positive modulus, got " + t); - let n = Vt(e, t), r = t, o = xt, s = mt; - for (; n !== xt; ) { + let n = Ht(e, t), r = t, o = St, s = bt; + for (; n !== St; ) { const c = r / n, a = r % n, u = o - s * c; r = n, n = a, o = s, s = u; } - if (r !== mt) + if (r !== bt) throw new Error("invert: does not exist"); - return Vt(o, t); + return Ht(o, t); } -function xs(e, t, n) { +function ks(e, t, n) { if (!e.eql(e.sqr(t), n)) throw new Error("Cannot find square root"); } -function Pc(e, t) { - const n = (e.ORDER + mt) / Nc, r = e.pow(t, n); - return xs(e, r, t), r; +function Kc(e, t) { + const n = (e.ORDER + bt) / Vc, r = e.pow(t, n); + return ks(e, r, t), r; } -function af(e, t) { - const n = (e.ORDER - Lc) / Cc, r = e.mul(t, Ce), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, Ce), o), c = e.mul(s, e.sub(i, e.ONE)); - return xs(e, c, t), c; +function gf(e, t) { + const n = (e.ORDER - Hc) / Dc, r = e.mul(t, Ce), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, Ce), o), c = e.mul(s, e.sub(i, e.ONE)); + return ks(e, c, t), c; } -function uf(e) { - const t = Zr(e), n = Hc(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + sf) / _c; +function wf(e) { + const t = Qr(e), n = Fc(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + hf) / Mc; return (c, a) => { let u = c.pow(a, i), f = c.mul(u, r); - const l = c.mul(u, o), d = c.mul(u, s), h = c.eql(c.sqr(f), a), w = c.eql(c.sqr(l), a); - u = c.cmov(u, f, h), f = c.cmov(d, l, w); + const d = c.mul(u, o), l = c.mul(u, s), h = c.eql(c.sqr(f), a), w = c.eql(c.sqr(d), a); + u = c.cmov(u, f, h), f = c.cmov(l, d, w); const g = c.eql(c.sqr(f), a), y = c.cmov(u, f, g); - return xs(c, y, a), y; + return ks(c, y, a), y; }; } -function Hc(e) { - if (e < Rc) +function Fc(e) { + if (e < Pc) throw new Error("sqrt is not defined for small field"); - let t = e - mt, n = 0; - for (; t % Ce === xt; ) + let t = e - bt, n = 0; + for (; t % Ce === St; ) t /= Ce, n++; let r = Ce; - const o = Zr(e); - for (; fi(o, r) === 1; ) + const o = Qr(e); + for (; pi(o, r) === 1; ) if (r++ > 1e3) throw new Error("Cannot find square root: probably non-prime P"); if (n === 1) - return Pc; + return Kc; let s = o.pow(r, t); - const i = (t + mt) / Ce; + const i = (t + bt) / Ce; return function(a, u) { if (a.is0(u)) return u; - if (fi(a, u) !== 1) + if (pi(a, u) !== 1) throw new Error("Cannot find square root"); - let f = n, l = a.mul(a.ONE, s), d = a.pow(u, t), h = a.pow(u, i); - for (; !a.eql(d, a.ONE); ) { - if (a.is0(d)) + let f = n, d = a.mul(a.ONE, s), l = a.pow(u, t), h = a.pow(u, i); + for (; !a.eql(l, a.ONE); ) { + if (a.is0(l)) return a.ZERO; - let w = 1, g = a.sqr(d); + let w = 1, g = a.sqr(l); for (; !a.eql(g, a.ONE); ) if (w++, g = a.sqr(g), w === f) throw new Error("Cannot find square root"); - const y = mt << BigInt(f - w - 1), S = a.pow(l, y); - f = w, l = a.sqr(S), d = a.mul(d, l), h = a.mul(h, S); + const y = bt << BigInt(f - w - 1), S = a.pow(d, y); + f = w, d = a.sqr(S), l = a.mul(l, d), h = a.mul(h, S); } return h; }; } -function ff(e) { - return e % Nc === Rc ? Pc : e % Cc === Lc ? af : e % _c === cf ? uf(e) : Hc(e); +function yf(e) { + return e % Vc === Pc ? Kc : e % Dc === Hc ? gf : e % Mc === pf ? wf(e) : Fc(e); } -const lf = [ +const mf = [ "create", "isValid", "is0", @@ -536,103 +536,103 @@ const lf = [ "mulN", "sqrN" ]; -function df(e) { +function bf(e) { const t = { ORDER: "bigint", BYTES: "number", BITS: "number" - }, n = lf.reduce((r, o) => (r[o] = "function", r), t); - return Es(e, n), e; + }, n = mf.reduce((r, o) => (r[o] = "function", r), t); + return Is(e, n), e; } -function hf(e, t, n) { - if (n < xt) +function Ef(e, t, n) { + if (n < St) throw new Error("invalid exponent, negatives unsupported"); - if (n === xt) + if (n === St) return e.ONE; - if (n === mt) + if (n === bt) return t; let r = e.ONE, o = t; - for (; n > xt; ) - n & mt && (r = e.mul(r, o)), o = e.sqr(o), n >>= mt; + for (; n > St; ) + n & bt && (r = e.mul(r, o)), o = e.sqr(o), n >>= bt; return r; } -function Vc(e, t, n = !1) { +function Wc(e, t, n = !1) { const r = new Array(t.length).fill(n ? e.ZERO : void 0), o = t.reduce((i, c, a) => e.is0(c) ? i : (r[a] = i, e.mul(i, c)), e.ONE), s = e.inv(o); return t.reduceRight((i, c, a) => e.is0(c) ? i : (r[a] = e.mul(i, r[a]), e.mul(i, c)), s), r; } -function fi(e, t) { - const n = (e.ORDER - mt) / Ce, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); +function pi(e, t) { + const n = (e.ORDER - bt) / Ce, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); if (!o && !s && !i) throw new Error("invalid Legendre symbol result"); return o ? 1 : s ? 0 : -1; } -function pf(e, t) { - t !== void 0 && Te(t); +function xf(e, t) { + t !== void 0 && Ae(t); const n = t !== void 0 ? t : e.toString(2).length, r = Math.ceil(n / 8); return { nBitLength: n, nByteLength: r }; } -let gf = class { +let Sf = class { ORDER; BITS; BYTES; isLE; - ZERO = xt; - ONE = mt; + ZERO = St; + ONE = bt; _lengths; _sqrt; // cached sqrt _mod; constructor(t, n = {}) { - if (t <= xt) + if (t <= St) throw new Error("invalid field: expected ORDER > 0, got " + t); let r; this.isLE = !1, n != null && typeof n == "object" && (typeof n.BITS == "number" && (r = n.BITS), typeof n.sqrt == "function" && (this.sqrt = n.sqrt), typeof n.isLE == "boolean" && (this.isLE = n.isLE), n.allowedLengths && (this._lengths = n.allowedLengths?.slice()), typeof n.modFromBytes == "boolean" && (this._mod = n.modFromBytes)); - const { nBitLength: o, nByteLength: s } = pf(t, r); + const { nBitLength: o, nByteLength: s } = xf(t, r); if (s > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes"); this.ORDER = t, this.BITS = o, this.BYTES = s, this._sqrt = void 0, Object.preventExtensions(this); } create(t) { - return Vt(t, this.ORDER); + return Ht(t, this.ORDER); } isValid(t) { if (typeof t != "bigint") throw new Error("invalid field element: expected bigint, got " + typeof t); - return xt <= t && t < this.ORDER; + return St <= t && t < this.ORDER; } is0(t) { - return t === xt; + return t === St; } // is valid and invertible isValidNot0(t) { return !this.is0(t) && this.isValid(t); } isOdd(t) { - return (t & mt) === mt; + return (t & bt) === bt; } neg(t) { - return Vt(-t, this.ORDER); + return Ht(-t, this.ORDER); } eql(t, n) { return t === n; } sqr(t) { - return Vt(t * t, this.ORDER); + return Ht(t * t, this.ORDER); } add(t, n) { - return Vt(t + n, this.ORDER); + return Ht(t + n, this.ORDER); } sub(t, n) { - return Vt(t - n, this.ORDER); + return Ht(t - n, this.ORDER); } mul(t, n) { - return Vt(t * n, this.ORDER); + return Ht(t * n, this.ORDER); } pow(t, n) { - return hf(this, t, n); + return Ef(this, t, n); } div(t, n) { - return Vt(t * ui(n, this.ORDER), this.ORDER); + return Ht(t * hi(n, this.ORDER), this.ORDER); } // Same as above, but doesn't normalize sqrN(t) { @@ -648,13 +648,13 @@ let gf = class { return t * n; } inv(t) { - return ui(t, this.ORDER); + return hi(t, this.ORDER); } sqrt(t) { - return this._sqrt || (this._sqrt = ff(this.ORDER)), this._sqrt(this, t); + return this._sqrt || (this._sqrt = yf(this.ORDER)), this._sqrt(this, t); } toBytes(t) { - return this.isLE ? $c(t, this.BYTES) : zn(t, this.BYTES); + return this.isLE ? _c(t, this.BYTES) : Gn(t, this.BYTES); } fromBytes(t, n = !1) { G(t); @@ -667,14 +667,14 @@ let gf = class { } if (t.length !== o) throw new Error("Field.fromBytes: expected " + o + " bytes, got " + t.length); - let a = s ? Oc(t) : le(t); - if (c && (a = Vt(a, i)), !n && !this.isValid(a)) + let a = s ? Lc(t) : de(t); + if (c && (a = Ht(a, i)), !n && !this.isValid(a)) throw new Error("invalid field element: outside of range 0..ORDER"); return a; } // TODO: we don't need it here, move out to separate fn invertBatch(t) { - return Vc(this, t); + return Wc(this, t); } // We can't move this out because Fp6, Fp12 implement it // and it's unclear what to return in there. @@ -682,62 +682,62 @@ let gf = class { return r ? n : t; } }; -function Zr(e, t = {}) { - return new gf(e, t); +function Qr(e, t = {}) { + return new Sf(e, t); } -function Dc(e) { +function zc(e) { if (typeof e != "bigint") throw new Error("field order must be bigint"); const t = e.toString(2).length; return Math.ceil(t / 8); } -function Mc(e) { - const t = Dc(e); +function Gc(e) { + const t = zc(e); return t + Math.ceil(t / 2); } -function Kc(e, t, n = !1) { +function qc(e, t, n = !1) { G(e); - const r = e.length, o = Dc(t), s = Mc(t); + const r = e.length, o = zc(t), s = Gc(t); if (r < 16 || r < s || r > 1024) throw new Error("expected " + s + "-1024 bytes of input, got " + r); - const i = n ? Oc(e) : le(e), c = Vt(i, t - mt) + mt; - return n ? $c(c, o) : zn(c, o); + const i = n ? Lc(e) : de(e), c = Ht(i, t - bt) + bt; + return n ? _c(c, o) : Gn(c, o); } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const on = /* @__PURE__ */ BigInt(0), _e = /* @__PURE__ */ BigInt(1); -function Er(e, t) { +const on = /* @__PURE__ */ BigInt(0), Pe = /* @__PURE__ */ BigInt(1); +function Tr(e, t) { const n = t.negate(); return e ? n : t; } -function li(e, t) { - const n = Vc(e.Fp, t.map((r) => r.Z)); +function gi(e, t) { + const n = Wc(e.Fp, t.map((r) => r.Z)); return t.map((r, o) => e.fromAffine(r.toAffine(n[o]))); } -function Fc(e, t) { +function jc(e, t) { if (!Number.isSafeInteger(e) || e <= 0 || e > t) throw new Error("invalid window size, expected [1.." + t + "], got W=" + e); } -function po(e, t) { - Fc(e, t); - const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = bs(e), i = BigInt(e); +function yo(e, t) { + jc(e, t); + const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = As(e), i = BigInt(e); return { windows: n, windowSize: r, mask: s, maxNumber: o, shiftBy: i }; } -function di(e, t, n) { +function wi(e, t, n) { const { windowSize: r, mask: o, maxNumber: s, shiftBy: i } = n; let c = Number(e & o), a = e >> i; - c > r && (c -= s, a += _e); - const u = t * r, f = u + Math.abs(c) - 1, l = c === 0, d = c < 0, h = t % 2 !== 0; - return { nextN: a, offset: f, isZero: l, isNeg: d, isNegF: h, offsetF: u }; + c > r && (c -= s, a += Pe); + const u = t * r, f = u + Math.abs(c) - 1, d = c === 0, l = c < 0, h = t % 2 !== 0; + return { nextN: a, offset: f, isZero: d, isNeg: l, isNegF: h, offsetF: u }; } -const go = /* @__PURE__ */ new WeakMap(), Wc = /* @__PURE__ */ new WeakMap(); -function wo(e) { - return Wc.get(e) || 1; +const mo = /* @__PURE__ */ new WeakMap(), Yc = /* @__PURE__ */ new WeakMap(); +function bo(e) { + return Yc.get(e) || 1; } -function hi(e) { +function yi(e) { if (e !== on) throw new Error("invalid wNAF"); } -let wf = class { +let Tf = class { BASE; ZERO; Fn; @@ -750,7 +750,7 @@ let wf = class { _unsafeLadder(t, n, r = this.ZERO) { let o = t; for (; n > on; ) - n & _e && (r = r.add(o)), o = o.double(), n >>= _e; + n & Pe && (r = r.add(o)), o = o.double(), n >>= Pe; return r; } /** @@ -766,7 +766,7 @@ let wf = class { * @returns precomputed point tables flattened to a single array */ precomputeWindow(t, n) { - const { windows: r, windowSize: o } = po(n, this.bits), s = []; + const { windows: r, windowSize: o } = yo(n, this.bits), s = []; let i = t, c = i; for (let a = 0; a < r; a++) { c = i, s.push(c); @@ -786,12 +786,12 @@ let wf = class { if (!this.Fn.isValid(r)) throw new Error("invalid scalar"); let o = this.ZERO, s = this.BASE; - const i = po(t, this.bits); + const i = yo(t, this.bits); for (let c = 0; c < i.windows; c++) { - const { nextN: a, offset: u, isZero: f, isNeg: l, isNegF: d, offsetF: h } = di(r, c, i); - r = a, f ? s = s.add(Er(d, n[h])) : o = o.add(Er(l, n[u])); + const { nextN: a, offset: u, isZero: f, isNeg: d, isNegF: l, offsetF: h } = wi(r, c, i); + r = a, f ? s = s.add(Tr(l, n[h])) : o = o.add(Tr(d, n[u])); } - return hi(r), { p: o, f: s }; + return yi(r), { p: o, f: s }; } /** * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form. @@ -799,53 +799,53 @@ let wf = class { * @returns point */ wNAFUnsafe(t, n, r, o = this.ZERO) { - const s = po(t, this.bits); + const s = yo(t, this.bits); for (let i = 0; i < s.windows && r !== on; i++) { - const { nextN: c, offset: a, isZero: u, isNeg: f } = di(r, i, s); + const { nextN: c, offset: a, isZero: u, isNeg: f } = wi(r, i, s); if (r = c, !u) { - const l = n[a]; - o = o.add(f ? l.negate() : l); + const d = n[a]; + o = o.add(f ? d.negate() : d); } } - return hi(r), o; + return yi(r), o; } getPrecomputes(t, n, r) { - let o = go.get(n); - return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), go.set(n, o))), o; + let o = mo.get(n); + return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), mo.set(n, o))), o; } cached(t, n, r) { - const o = wo(t); + const o = bo(t); return this.wNAF(o, this.getPrecomputes(o, t, r), n); } unsafe(t, n, r, o) { - const s = wo(t); + const s = bo(t); return s === 1 ? this._unsafeLadder(t, n, o) : this.wNAFUnsafe(s, this.getPrecomputes(s, t, r), n, o); } // We calculate precomputes for elliptic curve point multiplication // using windowed method. This specifies window size and // stores precomputed values. Usually only base point would be precomputed. createCache(t, n) { - Fc(n, this.bits), Wc.set(t, n), go.delete(t); + jc(n, this.bits), Yc.set(t, n), mo.delete(t); } hasCache(t) { - return wo(t) !== 1; + return bo(t) !== 1; } }; -function yf(e, t, n, r) { +function vf(e, t, n, r) { let o = t, s = e.ZERO, i = e.ZERO; for (; n > on || r > on; ) - n & _e && (s = s.add(o)), r & _e && (i = i.add(o)), o = o.double(), n >>= _e, r >>= _e; + n & Pe && (s = s.add(o)), r & Pe && (i = i.add(o)), o = o.double(), n >>= Pe, r >>= Pe; return { p1: s, p2: i }; } -function pi(e, t, n) { +function mi(e, t, n) { if (t) { if (t.ORDER !== e) throw new Error("Field.ORDER must match order: Fp == p, Fn == n"); - return df(t), t; + return bf(t), t; } else - return Zr(e, { isLE: n }); + return Qr(e, { isLE: n }); } -function mf(e, t, n = {}, r) { +function Af(e, t, n = {}, r) { if (r === void 0 && (r = e === "edwards"), !t || typeof t != "object") throw new Error(`expected valid ${e} CURVE object`); for (const a of ["p", "n", "h"]) { @@ -853,19 +853,19 @@ function mf(e, t, n = {}, r) { if (!(typeof u == "bigint" && u > on)) throw new Error(`CURVE.${a} must be positive bigint`); } - const o = pi(t.p, n.Fp, r), s = pi(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; + const o = mi(t.p, n.Fp, r), s = mi(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; for (const a of c) if (!o.isValid(t[a])) throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`); return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: o, Fn: s }; } -function zc(e, t) { +function Zc(e, t) { return function(r) { const o = e(r); return { secretKey: o, publicKey: t(o) }; }; } -let Gc = class { +let Xc = class { oHash; iHash; blockLen; @@ -873,7 +873,7 @@ let Gc = class { finished = !1; destroyed = !1; constructor(t, n) { - if (vc(t), G(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") + if (Bc(t), G(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") throw new Error("Expected instance of class which extends utils.Hash"); this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen; const r = this.blockLen, o = new Uint8Array(r); @@ -886,10 +886,10 @@ let Gc = class { this.oHash.update(o), rn(o); } update(t) { - return yr(this), this.iHash.update(t), this; + return Er(this), this.iHash.update(t), this; } digestInto(t) { - yr(this), G(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); + Er(this), G(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); } digest() { const t = new Uint8Array(this.oHash.outputLen); @@ -907,39 +907,39 @@ let Gc = class { this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy(); } }; -const qc = (e, t, n) => new Gc(e, t).update(n).digest(); -qc.create = (e, t) => new Gc(e, t); +const Qc = (e, t, n) => new Xc(e, t).update(n).digest(); +Qc.create = (e, t) => new Xc(e, t); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const gi = (e, t) => (e + (e >= 0 ? t : -t) / jc) / t; -function bf(e, t, n) { - const [[r, o], [s, i]] = t, c = gi(i * e, n), a = gi(-o * e, n); +const bi = (e, t) => (e + (e >= 0 ? t : -t) / Jc) / t; +function If(e, t, n) { + const [[r, o], [s, i]] = t, c = bi(i * e, n), a = bi(-o * e, n); let u = e - c * r - a * s, f = -c * o - a * i; - const l = u < se, d = f < se; - l && (u = -u), d && (f = -f); - const h = bs(Math.ceil(rf(n) / 2)) + Xe; - if (u < se || u >= h || f < se || f >= h) + const d = u < ie, l = f < ie; + d && (u = -u), l && (f = -f); + const h = As(Math.ceil(df(n) / 2)) + Qe; + if (u < ie || u >= h || f < ie || f >= h) throw new Error("splitScalar (endomorphism): failed, k=" + e); - return { k1neg: l, k1: u, k2neg: d, k2: f }; + return { k1neg: d, k1: u, k2neg: l, k2: f }; } -function No(e) { +function Ho(e) { if (!["compact", "recovered", "der"].includes(e)) throw new Error('Signature format must be "compact", "recovered", or "der"'); return e; } -function yo(e, t) { +function Eo(e, t) { const n = {}; for (let r of Object.keys(t)) n[r] = e[r] === void 0 ? t[r] : e[r]; - return br(n.lowS, "lowS"), br(n.prehash, "prehash"), n.format !== void 0 && No(n.format), n; + return Sr(n.lowS, "lowS"), Sr(n.prehash, "prehash"), n.format !== void 0 && Ho(n.format), n; } -let Ef = class extends Error { +let kf = class extends Error { constructor(t = "") { super(t); } }; const me = { // asn.1 DER encoding utils - Err: Ef, + Err: kf, // Basic building block is TLV (Tag-Length-Value) _tlv: { encode: (e, t) => { @@ -948,11 +948,11 @@ const me = { throw new n("tlv.encode: wrong tag"); if (t.length & 1) throw new n("tlv.encode: unpadded data"); - const r = t.length / 2, o = Xn(r); + const r = t.length / 2, o = Qn(r); if (o.length / 2 & 128) throw new n("tlv.encode: long form length too big"); - const s = r > 127 ? Xn(o.length / 2 | 128) : ""; - return Xn(e) + s + o + t; + const s = r > 127 ? Qn(o.length / 2 | 128) : ""; + return Qn(e) + s + o + t; }, // v - value, l - left bytes (unparsed) decode(e, t) { @@ -995,9 +995,9 @@ const me = { _int: { encode(e) { const { Err: t } = me; - if (e < se) + if (e < ie) throw new t("integer: negative integers are not allowed"); - let n = Xn(e); + let n = Qn(e); if (Number.parseInt(n[0], 16) & 8 && (n = "00" + n), n.length & 1) throw new t("unexpected DER parsing assertion: unpadded hex"); return n; @@ -1008,7 +1008,7 @@ const me = { throw new t("invalid signature integer: negative"); if (e[0] === 0 && !(e[1] & 128)) throw new t("invalid signature integer: unnecessary leading zero"); - return le(e); + return de(e); } }, toSig(e) { @@ -1024,12 +1024,12 @@ const me = { const { _tlv: t, _int: n } = me, r = t.encode(2, n.encode(e.r)), o = t.encode(2, n.encode(e.s)), s = r + o; return t.encode(48, s); } -}, se = BigInt(0), Xe = BigInt(1), jc = BigInt(2), Qn = BigInt(3), xf = BigInt(4); -function Sf(e, t = {}) { - const n = mf("weierstrass", e, t), { Fp: r, Fn: o } = n; +}, ie = BigInt(0), Qe = BigInt(1), Jc = BigInt(2), Jn = BigInt(3), Bf = BigInt(4); +function Of(e, t = {}) { + const n = Af("weierstrass", e, t), { Fp: r, Fn: o } = n; let s = n.CURVE; const { h: i, n: c } = s; - Es(t, {}, { + Is(t, {}, { allowInfinityPoint: "boolean", clearCofactor: "function", isTorsionFree: "function", @@ -1040,109 +1040,109 @@ function Sf(e, t = {}) { const { endo: a } = t; if (a && (!r.is0(s.a) || typeof a.beta != "bigint" || !Array.isArray(a.basises))) throw new Error('invalid endo: expected "beta": bigint and "basises": array'); - const u = Zc(r, o); + const u = ea(r, o); function f() { if (!r.isOdd) throw new Error("compression is not supported: Field does not have .isOdd()"); } - function l(_, b, m) { + function d(C, b, m) { const { x: p, y: E } = b.toAffine(), A = r.toBytes(p); - if (br(m, "isCompressed"), m) { + if (Sr(m, "isCompressed"), m) { f(); - const k = !r.isOdd(E); - return Kt(Yc(k), A); + const B = !r.isOdd(E); + return Ft(ta(B), A); } else - return Kt(Uint8Array.of(4), A, r.toBytes(E)); + return Ft(Uint8Array.of(4), A, r.toBytes(E)); } - function d(_) { - G(_, void 0, "Point"); - const { publicKey: b, publicKeyUncompressed: m } = u, p = _.length, E = _[0], A = _.subarray(1); + function l(C) { + G(C, void 0, "Point"); + const { publicKey: b, publicKeyUncompressed: m } = u, p = C.length, E = C[0], A = C.subarray(1); if (p === b && (E === 2 || E === 3)) { - const k = r.fromBytes(A); - if (!r.isValid(k)) + const B = r.fromBytes(A); + if (!r.isValid(B)) throw new Error("bad point: is not on curve, wrong x"); - const I = g(k); - let T; + const I = g(B); + let v; try { - T = r.sqrt(I); - } catch (F) { - const H = F instanceof Error ? ": " + F.message : ""; - throw new Error("bad point: is not on curve, sqrt error" + H); + v = r.sqrt(I); + } catch (W) { + const V = W instanceof Error ? ": " + W.message : ""; + throw new Error("bad point: is not on curve, sqrt error" + V); } f(); - const B = r.isOdd(T); - return (E & 1) === 1 !== B && (T = r.neg(T)), { x: k, y: T }; + const O = r.isOdd(v); + return (E & 1) === 1 !== O && (v = r.neg(v)), { x: B, y: v }; } else if (p === m && E === 4) { - const k = r.BYTES, I = r.fromBytes(A.subarray(0, k)), T = r.fromBytes(A.subarray(k, k * 2)); - if (!y(I, T)) + const B = r.BYTES, I = r.fromBytes(A.subarray(0, B)), v = r.fromBytes(A.subarray(B, B * 2)); + if (!y(I, v)) throw new Error("bad point: is not on curve"); - return { x: I, y: T }; + return { x: I, y: v }; } else throw new Error(`bad point: got length ${p}, expected compressed=${b} or uncompressed=${m}`); } - const h = t.toBytes || l, w = t.fromBytes || d; - function g(_) { - const b = r.sqr(_), m = r.mul(b, _); - return r.add(r.add(m, r.mul(_, s.a)), s.b); + const h = t.toBytes || d, w = t.fromBytes || l; + function g(C) { + const b = r.sqr(C), m = r.mul(b, C); + return r.add(r.add(m, r.mul(C, s.a)), s.b); } - function y(_, b) { - const m = r.sqr(b), p = g(_); + function y(C, b) { + const m = r.sqr(b), p = g(C); return r.eql(m, p); } if (!y(s.Gx, s.Gy)) throw new Error("bad curve params: generator point"); - const S = r.mul(r.pow(s.a, Qn), xf), v = r.mul(r.sqr(s.b), BigInt(27)); - if (r.is0(r.add(S, v))) + const S = r.mul(r.pow(s.a, Jn), Bf), T = r.mul(r.sqr(s.b), BigInt(27)); + if (r.is0(r.add(S, T))) throw new Error("bad curve params: a or b"); - function O(_, b, m = !1) { + function k(C, b, m = !1) { if (!r.isValid(b) || m && r.is0(b)) - throw new Error(`bad point coordinate ${_}`); + throw new Error(`bad point coordinate ${C}`); return b; } - function N(_) { - if (!(_ instanceof C)) + function N(C) { + if (!(C instanceof L)) throw new Error("Weierstrass Point expected"); } - function U(_) { + function $(C) { if (!a || !a.basises) throw new Error("no endo"); - return bf(_, a.basises, o.ORDER); + return If(C, a.basises, o.ORDER); } - const W = ai((_, b) => { - const { X: m, Y: p, Z: E } = _; + const F = li((C, b) => { + const { X: m, Y: p, Z: E } = C; if (r.eql(E, r.ONE)) return { x: m, y: p }; - const A = _.is0(); + const A = C.is0(); b == null && (b = A ? r.ONE : r.inv(E)); - const k = r.mul(m, b), I = r.mul(p, b), T = r.mul(E, b); + const B = r.mul(m, b), I = r.mul(p, b), v = r.mul(E, b); if (A) return { x: r.ZERO, y: r.ZERO }; - if (!r.eql(T, r.ONE)) + if (!r.eql(v, r.ONE)) throw new Error("invZ was invalid"); - return { x: k, y: I }; - }), x = ai((_) => { - if (_.is0()) { - if (t.allowInfinityPoint && !r.is0(_.Y)) + return { x: B, y: I }; + }), x = li((C) => { + if (C.is0()) { + if (t.allowInfinityPoint && !r.is0(C.Y)) return; throw new Error("bad point: ZERO"); } - const { x: b, y: m } = _.toAffine(); + const { x: b, y: m } = C.toAffine(); if (!r.isValid(b) || !r.isValid(m)) throw new Error("bad point: x or y not field elements"); if (!y(b, m)) throw new Error("bad point: equation left != right"); - if (!_.isTorsionFree()) + if (!C.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup"); return !0; }); - function Q(_, b, m, p, E) { - return m = new C(r.mul(m.X, _), m.Y, m.Z), b = Er(p, b), m = Er(E, m), b.add(m); + function Y(C, b, m, p, E) { + return m = new L(r.mul(m.X, C), m.Y, m.Z), b = Tr(p, b), m = Tr(E, m), b.add(m); } - class C { + class L { // base / generator point - static BASE = new C(s.Gx, s.Gy, r.ONE); + static BASE = new L(s.Gx, s.Gy, r.ONE); // zero / infinity / identity point - static ZERO = new C(r.ZERO, r.ONE, r.ZERO); + static ZERO = new L(r.ZERO, r.ONE, r.ZERO); // 0, 1, 0 // math field static Fp = r; @@ -1153,7 +1153,7 @@ function Sf(e, t = {}) { Z; /** Does NOT validate if the point is valid. Use `.assertValidity()`. */ constructor(b, m, p) { - this.X = O("x", b), this.Y = O("y", m, !0), this.Z = O("z", p), Object.freeze(this); + this.X = k("x", b), this.Y = k("y", m, !0), this.Z = k("z", p), Object.freeze(this); } static CURVE() { return s; @@ -1163,16 +1163,16 @@ function Sf(e, t = {}) { const { x: m, y: p } = b || {}; if (!b || !r.isValid(m) || !r.isValid(p)) throw new Error("invalid affine point"); - if (b instanceof C) + if (b instanceof L) throw new Error("projective point not allowed"); - return r.is0(m) && r.is0(p) ? C.ZERO : new C(m, p, r.ONE); + return r.is0(m) && r.is0(p) ? L.ZERO : new L(m, p, r.ONE); } static fromBytes(b) { - const m = C.fromAffine(w(G(b, void 0, "point"))); + const m = L.fromAffine(w(G(b, void 0, "point"))); return m.assertValidity(), m; } static fromHex(b) { - return C.fromBytes(mr(b)); + return L.fromBytes(xr(b)); } get x() { return this.toAffine().x; @@ -1187,7 +1187,7 @@ function Sf(e, t = {}) { * @returns */ precompute(b = 8, m = !0) { - return gt.createCache(this, b), m || this.multiply(Qn), this; + return gt.createCache(this, b), m || this.multiply(Jn), this; } // TODO: return `this` /** A point on curve is valid if it conforms to equation. */ @@ -1203,21 +1203,21 @@ function Sf(e, t = {}) { /** Compare one point to another. */ equals(b) { N(b); - const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b, T = r.eql(r.mul(m, I), r.mul(A, E)), B = r.eql(r.mul(p, I), r.mul(k, E)); - return T && B; + const { X: m, Y: p, Z: E } = this, { X: A, Y: B, Z: I } = b, v = r.eql(r.mul(m, I), r.mul(A, E)), O = r.eql(r.mul(p, I), r.mul(B, E)); + return v && O; } /** Flips point to one corresponding to (x, -y) in Affine coordinates. */ negate() { - return new C(this.X, r.neg(this.Y), this.Z); + return new L(this.X, r.neg(this.Y), this.Z); } // Renes-Costello-Batina exception-free doubling formula. // There is 30% faster Jacobian formula, but it is not complete. // https://eprint.iacr.org/2015/1060, algorithm 3 // Cost: 8M + 3S + 3*a + 2*b3 + 15add. double() { - const { a: b, b: m } = s, p = r.mul(m, Qn), { X: E, Y: A, Z: k } = this; - let I = r.ZERO, T = r.ZERO, B = r.ZERO, R = r.mul(E, E), F = r.mul(A, A), H = r.mul(k, k), L = r.mul(E, A); - return L = r.add(L, L), B = r.mul(E, k), B = r.add(B, B), I = r.mul(b, B), T = r.mul(p, H), T = r.add(I, T), I = r.sub(F, T), T = r.add(F, T), T = r.mul(I, T), I = r.mul(L, I), B = r.mul(p, B), H = r.mul(b, H), L = r.sub(R, H), L = r.mul(b, L), L = r.add(L, B), B = r.add(R, R), R = r.add(B, R), R = r.add(R, H), R = r.mul(R, L), T = r.add(T, R), H = r.mul(A, k), H = r.add(H, H), R = r.mul(H, L), I = r.sub(I, R), B = r.mul(H, F), B = r.add(B, B), B = r.add(B, B), new C(I, T, B); + const { a: b, b: m } = s, p = r.mul(m, Jn), { X: E, Y: A, Z: B } = this; + let I = r.ZERO, v = r.ZERO, O = r.ZERO, R = r.mul(E, E), W = r.mul(A, A), V = r.mul(B, B), _ = r.mul(E, A); + return _ = r.add(_, _), O = r.mul(E, B), O = r.add(O, O), I = r.mul(b, O), v = r.mul(p, V), v = r.add(I, v), I = r.sub(W, v), v = r.add(W, v), v = r.mul(I, v), I = r.mul(_, I), O = r.mul(p, O), V = r.mul(b, V), _ = r.sub(R, V), _ = r.mul(b, _), _ = r.add(_, O), O = r.add(R, R), R = r.add(O, R), R = r.add(R, V), R = r.mul(R, _), v = r.add(v, R), V = r.mul(A, B), V = r.add(V, V), R = r.mul(V, _), I = r.sub(I, R), O = r.mul(V, W), O = r.add(O, O), O = r.add(O, O), new L(I, v, O); } // Renes-Costello-Batina exception-free addition formula. // There is 30% faster Jacobian formula, but it is not complete. @@ -1225,19 +1225,19 @@ function Sf(e, t = {}) { // Cost: 12M + 0S + 3*a + 3*b3 + 23add. add(b) { N(b); - const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b; - let T = r.ZERO, B = r.ZERO, R = r.ZERO; - const F = s.a, H = r.mul(s.b, Qn); - let L = r.mul(m, A), D = r.mul(p, k), q = r.mul(E, I), st = r.add(m, p), M = r.add(A, k); - st = r.mul(st, M), M = r.add(L, D), st = r.sub(st, M), M = r.add(m, E); - let Y = r.add(A, I); - return M = r.mul(M, Y), Y = r.add(L, q), M = r.sub(M, Y), Y = r.add(p, E), T = r.add(k, I), Y = r.mul(Y, T), T = r.add(D, q), Y = r.sub(Y, T), R = r.mul(F, M), T = r.mul(H, q), R = r.add(T, R), T = r.sub(D, R), R = r.add(D, R), B = r.mul(T, R), D = r.add(L, L), D = r.add(D, L), q = r.mul(F, q), M = r.mul(H, M), D = r.add(D, q), q = r.sub(L, q), q = r.mul(F, q), M = r.add(M, q), L = r.mul(D, M), B = r.add(B, L), L = r.mul(Y, M), T = r.mul(st, T), T = r.sub(T, L), L = r.mul(st, D), R = r.mul(Y, R), R = r.add(R, L), new C(T, B, R); + const { X: m, Y: p, Z: E } = this, { X: A, Y: B, Z: I } = b; + let v = r.ZERO, O = r.ZERO, R = r.ZERO; + const W = s.a, V = r.mul(s.b, Jn); + let _ = r.mul(m, A), D = r.mul(p, B), q = r.mul(E, I), st = r.add(m, p), M = r.add(A, B); + st = r.mul(st, M), M = r.add(_, D), st = r.sub(st, M), M = r.add(m, E); + let Z = r.add(A, I); + return M = r.mul(M, Z), Z = r.add(_, q), M = r.sub(M, Z), Z = r.add(p, E), v = r.add(B, I), Z = r.mul(Z, v), v = r.add(D, q), Z = r.sub(Z, v), R = r.mul(W, M), v = r.mul(V, q), R = r.add(v, R), v = r.sub(D, R), R = r.add(D, R), O = r.mul(v, R), D = r.add(_, _), D = r.add(D, _), q = r.mul(W, q), M = r.mul(V, M), D = r.add(D, q), q = r.sub(_, q), q = r.mul(W, q), M = r.add(M, q), _ = r.mul(D, M), O = r.add(O, _), _ = r.mul(Z, M), v = r.mul(st, v), v = r.sub(v, _), _ = r.mul(st, D), R = r.mul(Z, R), R = r.add(R, _), new L(v, O, R); } subtract(b) { return this.add(b.negate()); } is0() { - return this.equals(C.ZERO); + return this.equals(L.ZERO); } /** * Constant time multiplication. @@ -1253,15 +1253,15 @@ function Sf(e, t = {}) { if (!o.isValidNot0(b)) throw new Error("invalid scalar: out of range"); let p, E; - const A = (k) => gt.cached(this, k, (I) => li(C, I)); + const A = (B) => gt.cached(this, B, (I) => gi(L, I)); if (m) { - const { k1neg: k, k1: I, k2neg: T, k2: B } = U(b), { p: R, f: F } = A(I), { p: H, f: L } = A(B); - E = F.add(L), p = Q(m.beta, R, H, k, T); + const { k1neg: B, k1: I, k2neg: v, k2: O } = $(b), { p: R, f: W } = A(I), { p: V, f: _ } = A(O); + E = W.add(_), p = Y(m.beta, R, V, B, v); } else { - const { p: k, f: I } = A(b); - p = k, E = I; + const { p: B, f: I } = A(b); + p = B, E = I; } - return li(C, [p, E])[0]; + return gi(L, [p, E])[0]; } /** * Non-constant-time multiplication. Uses double-and-add algorithm. @@ -1272,15 +1272,15 @@ function Sf(e, t = {}) { const { endo: m } = t, p = this; if (!o.isValid(b)) throw new Error("invalid scalar: out of range"); - if (b === se || p.is0()) - return C.ZERO; - if (b === Xe) + if (b === ie || p.is0()) + return L.ZERO; + if (b === Qe) return p; if (gt.hasCache(this)) return this.multiply(b); if (m) { - const { k1neg: E, k1: A, k2neg: k, k2: I } = U(b), { p1: T, p2: B } = yf(C, p, A, I); - return Q(m.beta, T, B, E, k); + const { k1neg: E, k1: A, k2neg: B, k2: I } = $(b), { p1: v, p2: O } = vf(L, p, A, I); + return Y(m.beta, v, O, E, B); } else return gt.unsafe(p, b); } @@ -1289,7 +1289,7 @@ function Sf(e, t = {}) { * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch */ toAffine(b) { - return W(this, b); + return F(this, b); } /** * Checks whether Point is free of torsion elements (is in prime subgroup). @@ -1297,32 +1297,32 @@ function Sf(e, t = {}) { */ isTorsionFree() { const { isTorsionFree: b } = t; - return i === Xe ? !0 : b ? b(C, this) : gt.unsafe(this, c).is0(); + return i === Qe ? !0 : b ? b(L, this) : gt.unsafe(this, c).is0(); } clearCofactor() { const { clearCofactor: b } = t; - return i === Xe ? this : b ? b(C, this) : this.multiplyUnsafe(i); + return i === Qe ? this : b ? b(L, this) : this.multiplyUnsafe(i); } isSmallOrder() { return this.multiplyUnsafe(i).is0(); } toBytes(b = !0) { - return br(b, "isCompressed"), this.assertValidity(), h(C, this, b); + return Sr(b, "isCompressed"), this.assertValidity(), h(L, this, b); } toHex(b = !0) { - return Yr(this.toBytes(b)); + return Xr(this.toBytes(b)); } toString() { return ``; } } - const Pt = o.BITS, gt = new wf(C, t.endo ? Math.ceil(Pt / 2) : Pt); - return C.BASE.precompute(8), C; + const Pt = o.BITS, gt = new Tf(L, t.endo ? Math.ceil(Pt / 2) : Pt); + return L.BASE.precompute(8), L; } -function Yc(e) { +function ta(e) { return Uint8Array.of(e ? 2 : 3); } -function Zc(e, t) { +function ea(e, t) { return { secretKey: t.BYTES, publicKey: 1 + e.BYTES, @@ -1331,8 +1331,8 @@ function Zc(e, t) { signature: 2 * t.BYTES }; } -function vf(e, t = {}) { - const { Fn: n } = e, r = t.randomBytes || Wn, o = Object.assign(Zc(e.Fp, n), { seed: Mc(n.ORDER) }); +function Uf(e, t = {}) { + const { Fn: n } = e, r = t.randomBytes || zn, o = Object.assign(ea(e.Fp, n), { seed: Gc(n.ORDER) }); function s(h) { try { const w = n.fromBytes(h); @@ -1351,14 +1351,14 @@ function vf(e, t = {}) { } } function c(h = r(o.seed)) { - return Kc(G(h, o.seed, "seed"), n.ORDER); + return qc(G(h, o.seed, "seed"), n.ORDER); } function a(h, w = !0) { return e.BASE.multiply(n.fromBytes(h)).toBytes(w); } function u(h) { const { secretKey: w, publicKey: g, publicKeyUncompressed: y } = o; - if (!ys(h) || "_lengths" in n && n._lengths || w === g) + if (!Ts(h) || "_lengths" in n && n._lengths || w === g) return; const S = G(h, void 0, "key").length; return S === g || S === y; @@ -1371,29 +1371,29 @@ function vf(e, t = {}) { const y = n.fromBytes(h); return e.fromBytes(w).multiply(y).toBytes(g); } - const l = { + const d = { isValidSecretKey: s, isValidPublicKey: i, randomSecretKey: c - }, d = zc(c, a); - return Object.freeze({ getPublicKey: a, getSharedSecret: f, keygen: d, Point: e, utils: l, lengths: o }); + }, l = Zc(c, a); + return Object.freeze({ getPublicKey: a, getSharedSecret: f, keygen: l, Point: e, utils: d, lengths: o }); } -function Tf(e, t, n = {}) { - vc(t), Es(n, {}, { +function $f(e, t, n = {}) { + Bc(t), Is(n, {}, { hmac: "function", lowS: "boolean", randomBytes: "function", bits2int: "function", bits2int_modN: "function" }), n = Object.assign({}, n); - const r = n.randomBytes || Wn, o = n.hmac || ((m, p) => qc(t, m, p)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: l, utils: d, lengths: h } = vf(e, n), w = { + const r = n.randomBytes || zn, o = n.hmac || ((m, p) => Qc(t, m, p)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: d, utils: l, lengths: h } = Uf(e, n), w = { prehash: !0, lowS: typeof n.lowS == "boolean" ? n.lowS : !0, format: "compact", extraEntropy: !1 - }, g = c * jc < s.ORDER; + }, g = c * Jc < s.ORDER; function y(m) { - const p = c >> Xe; + const p = c >> Qe; return m > p; } function S(m, p) { @@ -1401,12 +1401,12 @@ function Tf(e, t, n = {}) { throw new Error(`invalid signature ${m}: out of range 1..Point.Fn.ORDER`); return p; } - function v() { + function T() { if (g) throw new Error('"recovered" sig type is not supported for cofactor >2 curves'); } - function O(m, p) { - No(p); + function k(m, p) { + Ho(p); const E = h.signature, A = p === "compact" ? E : p === "recovered" ? E + 1 : void 0; return G(m, A); } @@ -1416,25 +1416,25 @@ function Tf(e, t, n = {}) { recovery; constructor(p, E, A) { if (this.r = S("r", p), this.s = S("s", E), A != null) { - if (v(), ![0, 1, 2, 3].includes(A)) + if (T(), ![0, 1, 2, 3].includes(A)) throw new Error("invalid recovery id"); this.recovery = A; } Object.freeze(this); } static fromBytes(p, E = w.format) { - O(p, E); + k(p, E); let A; if (E === "der") { - const { r: B, s: R } = me.toSig(G(p)); - return new N(B, R); + const { r: O, s: R } = me.toSig(G(p)); + return new N(O, R); } E === "recovered" && (A = p[0], E = "compact", p = p.subarray(1)); - const k = h.signature / 2, I = p.subarray(0, k), T = p.subarray(k, k * 2); - return new N(i.fromBytes(I), i.fromBytes(T), A); + const B = h.signature / 2, I = p.subarray(0, B), v = p.subarray(B, B * 2); + return new N(i.fromBytes(I), i.fromBytes(v), A); } static fromHex(p, E) { - return this.fromBytes(mr(p), E); + return this.fromBytes(xr(p), E); } assertRecovery() { const { recovery: p } = this; @@ -1446,10 +1446,10 @@ function Tf(e, t, n = {}) { return new N(this.r, this.s, p); } recoverPublicKey(p) { - const { r: E, s: A } = this, k = this.assertRecovery(), I = k === 2 || k === 3 ? E + c : E; + const { r: E, s: A } = this, B = this.assertRecovery(), I = B === 2 || B === 3 ? E + c : E; if (!s.isValid(I)) throw new Error("invalid recovery id: sig.r+curve.n != R.x"); - const T = s.toBytes(I), B = e.fromBytes(Kt(Yc((k & 1) === 0), T)), R = i.inv(I), F = W(G(p, void 0, "msgHash")), H = i.create(-F * R), L = i.create(A * R), D = e.BASE.multiplyUnsafe(H).add(B.multiplyUnsafe(L)); + const v = s.toBytes(I), O = e.fromBytes(Ft(ta((B & 1) === 0), v)), R = i.inv(I), W = F(G(p, void 0, "msgHash")), V = i.create(-W * R), _ = i.create(A * R), D = e.BASE.multiplyUnsafe(V).add(O.multiplyUnsafe(_)); if (D.is0()) throw new Error("invalid recovery: point at infinify"); return D.assertValidity(), D; @@ -1459,97 +1459,97 @@ function Tf(e, t, n = {}) { return y(this.s); } toBytes(p = w.format) { - if (No(p), p === "der") - return mr(me.hexFromSig(this)); - const { r: E, s: A } = this, k = i.toBytes(E), I = i.toBytes(A); - return p === "recovered" ? (v(), Kt(Uint8Array.of(this.assertRecovery()), k, I)) : Kt(k, I); + if (Ho(p), p === "der") + return xr(me.hexFromSig(this)); + const { r: E, s: A } = this, B = i.toBytes(E), I = i.toBytes(A); + return p === "recovered" ? (T(), Ft(Uint8Array.of(this.assertRecovery()), B, I)) : Ft(B, I); } toHex(p) { - return Yr(this.toBytes(p)); + return Xr(this.toBytes(p)); } } - const U = n.bits2int || function(p) { + const $ = n.bits2int || function(p) { if (p.length > 8192) throw new Error("input is too large"); - const E = le(p), A = p.length * 8 - a; + const E = de(p), A = p.length * 8 - a; return A > 0 ? E >> BigInt(A) : E; - }, W = n.bits2int_modN || function(p) { - return i.create(U(p)); - }, x = bs(a); - function Q(m) { - return Uc("num < 2^" + a, m, se, x), i.toBytes(m); + }, F = n.bits2int_modN || function(p) { + return i.create($(p)); + }, x = As(a); + function Y(m) { + return Cc("num < 2^" + a, m, ie, x), i.toBytes(m); } - function C(m, p) { + function L(m, p) { return G(m, void 0, "message"), p ? G(t(m), void 0, "prehashed message") : m; } function Pt(m, p, E) { - const { lowS: A, prehash: k, extraEntropy: I } = yo(E, w); - m = C(m, k); - const T = W(m), B = i.fromBytes(p); - if (!i.isValidNot0(B)) + const { lowS: A, prehash: B, extraEntropy: I } = Eo(E, w); + m = L(m, B); + const v = F(m), O = i.fromBytes(p); + if (!i.isValidNot0(O)) throw new Error("invalid private key"); - const R = [Q(B), Q(T)]; + const R = [Y(O), Y(v)]; if (I != null && I !== !1) { const D = I === !0 ? r(h.secretKey) : I; R.push(G(D, void 0, "extraEntropy")); } - const F = Kt(...R), H = T; - function L(D) { - const q = U(D); + const W = Ft(...R), V = v; + function _(D) { + const q = $(D); if (!i.isValidNot0(q)) return; - const st = i.inv(q), M = e.BASE.multiply(q).toAffine(), Y = i.create(M.x); - if (Y === se) + const st = i.inv(q), M = e.BASE.multiply(q).toAffine(), Z = i.create(M.x); + if (Z === ie) return; - const ee = i.create(st * i.create(H + Y * B)); - if (ee === se) + const ne = i.create(st * i.create(V + Z * O)); + if (ne === ie) return; - let En = (M.x === Y ? 0 : 2) | Number(M.y & Xe), xn = ee; - return A && y(ee) && (xn = i.neg(ee), En ^= 1), new N(Y, xn, g ? void 0 : En); + let xn = (M.x === Z ? 0 : 2) | Number(M.y & Qe), Sn = ne; + return A && y(ne) && (Sn = i.neg(ne), xn ^= 1), new N(Z, Sn, g ? void 0 : xn); } - return { seed: F, k2sig: L }; + return { seed: W, k2sig: _ }; } function gt(m, p, E = {}) { - const { seed: A, k2sig: k } = Pt(m, p, E); - return of(t.outputLen, i.BYTES, o)(A, k).toBytes(E.format); + const { seed: A, k2sig: B } = Pt(m, p, E); + return lf(t.outputLen, i.BYTES, o)(A, B).toBytes(E.format); } - function _(m, p, E, A = {}) { - const { lowS: k, prehash: I, format: T } = yo(A, w); - if (E = G(E, void 0, "publicKey"), p = C(p, I), !ys(m)) { - const B = m instanceof N ? ", use sig.toBytes()" : ""; - throw new Error("verify expects Uint8Array signature" + B); + function C(m, p, E, A = {}) { + const { lowS: B, prehash: I, format: v } = Eo(A, w); + if (E = G(E, void 0, "publicKey"), p = L(p, I), !Ts(m)) { + const O = m instanceof N ? ", use sig.toBytes()" : ""; + throw new Error("verify expects Uint8Array signature" + O); } - O(m, T); + k(m, v); try { - const B = N.fromBytes(m, T), R = e.fromBytes(E); - if (k && B.hasHighS()) + const O = N.fromBytes(m, v), R = e.fromBytes(E); + if (B && O.hasHighS()) return !1; - const { r: F, s: H } = B, L = W(p), D = i.inv(H), q = i.create(L * D), st = i.create(F * D), M = e.BASE.multiplyUnsafe(q).add(R.multiplyUnsafe(st)); - return M.is0() ? !1 : i.create(M.x) === F; + const { r: W, s: V } = O, _ = F(p), D = i.inv(V), q = i.create(_ * D), st = i.create(W * D), M = e.BASE.multiplyUnsafe(q).add(R.multiplyUnsafe(st)); + return M.is0() ? !1 : i.create(M.x) === W; } catch { return !1; } } function b(m, p, E = {}) { - const { prehash: A } = yo(E, w); - return p = C(p, A), N.fromBytes(m, "recovered").recoverPublicKey(p).toBytes(); + const { prehash: A } = Eo(E, w); + return p = L(p, A), N.fromBytes(m, "recovered").recoverPublicKey(p).toBytes(); } return Object.freeze({ keygen: u, getPublicKey: f, - getSharedSecret: l, - utils: d, + getSharedSecret: d, + utils: l, lengths: h, Point: e, sign: gt, - verify: _, + verify: C, recoverPublicKey: b, Signature: N, hash: t }); } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const Xr = { +const Jr = { p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"), n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"), h: BigInt(1), @@ -1557,87 +1557,87 @@ const Xr = { b: BigInt(7), Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"), Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8") -}, Af = { +}, Rf = { beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"), basises: [ [BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")], [BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")] ] -}, If = /* @__PURE__ */ BigInt(0), Lo = /* @__PURE__ */ BigInt(2); -function kf(e) { - const t = Xr.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, l = Rt(f, n, t) * f % t, d = Rt(l, n, t) * f % t, h = Rt(d, Lo, t) * u % t, w = Rt(h, o, t) * h % t, g = Rt(w, s, t) * w % t, y = Rt(g, c, t) * g % t, S = Rt(y, a, t) * y % t, v = Rt(S, c, t) * g % t, O = Rt(v, n, t) * f % t, N = Rt(O, i, t) * w % t, U = Rt(N, r, t) * u % t, W = Rt(U, Lo, t); - if (!xr.eql(xr.sqr(W), e)) +}, Nf = /* @__PURE__ */ BigInt(0), Do = /* @__PURE__ */ BigInt(2); +function Lf(e) { + const t = Jr.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, d = Rt(f, n, t) * f % t, l = Rt(d, n, t) * f % t, h = Rt(l, Do, t) * u % t, w = Rt(h, o, t) * h % t, g = Rt(w, s, t) * w % t, y = Rt(g, c, t) * g % t, S = Rt(y, a, t) * y % t, T = Rt(S, c, t) * g % t, k = Rt(T, n, t) * f % t, N = Rt(k, i, t) * w % t, $ = Rt(N, r, t) * u % t, F = Rt($, Do, t); + if (!vr.eql(vr.sqr(F), e)) throw new Error("Cannot find square root"); - return W; -} -const xr = Zr(Xr.p, { sqrt: kf }), Ge = /* @__PURE__ */ Sf(Xr, { - Fp: xr, - endo: Af -}), xe = /* @__PURE__ */ Tf(Ge, wt), wi = {}; -function Sr(e, ...t) { - let n = wi[e]; + return F; +} +const vr = Qr(Jr.p, { sqrt: Lf }), qe = /* @__PURE__ */ Of(Jr, { + Fp: vr, + endo: Rf +}), Se = /* @__PURE__ */ $f(qe, yt), Ei = {}; +function Ar(e, ...t) { + let n = Ei[e]; if (n === void 0) { - const r = wt(ef(e)); - n = Kt(r, r), wi[e] = n; + const r = yt(uf(e)); + n = Ft(r, r), Ei[e] = n; } - return wt(Kt(n, ...t)); + return yt(Ft(n, ...t)); } -const Ss = (e) => e.toBytes(!0).slice(1), vs = (e) => e % Lo === If; -function Co(e) { - const { Fn: t, BASE: n } = Ge, r = t.fromBytes(e), o = n.multiply(r); - return { scalar: vs(o.y) ? r : t.neg(r), bytes: Ss(o) }; +const Bs = (e) => e.toBytes(!0).slice(1), Os = (e) => e % Do === Nf; +function Mo(e) { + const { Fn: t, BASE: n } = qe, r = t.fromBytes(e), o = n.multiply(r); + return { scalar: Os(o.y) ? r : t.neg(r), bytes: Bs(o) }; } -function Xc(e) { - const t = xr; +function na(e) { + const t = vr; if (!t.isValidNot0(e)) throw new Error("invalid x: Fail if x ≥ p"); const n = t.create(e * e), r = t.create(n * e + BigInt(7)); let o = t.sqrt(r); - vs(o) || (o = t.neg(o)); - const s = Ge.fromAffine({ x: e, y: o }); + Os(o) || (o = t.neg(o)); + const s = qe.fromAffine({ x: e, y: o }); return s.assertValidity(), s; } -const Bn = le; -function Qc(...e) { - return Ge.Fn.create(Bn(Sr("BIP0340/challenge", ...e))); +const On = de; +function ra(...e) { + return qe.Fn.create(On(Ar("BIP0340/challenge", ...e))); } -function yi(e) { - return Co(e).bytes; +function xi(e) { + return Mo(e).bytes; } -function Bf(e, t, n = Wn(32)) { - const { Fn: r } = Ge, o = G(e, void 0, "message"), { bytes: s, scalar: i } = Co(t), c = G(n, 32, "auxRand"), a = r.toBytes(i ^ Bn(Sr("BIP0340/aux", c))), u = Sr("BIP0340/nonce", a, s, o), { bytes: f, scalar: l } = Co(u), d = Qc(f, s, o), h = new Uint8Array(64); - if (h.set(f, 0), h.set(r.toBytes(r.create(l + d * i)), 32), !Jc(h, o, s)) +function _f(e, t, n = zn(32)) { + const { Fn: r } = qe, o = G(e, void 0, "message"), { bytes: s, scalar: i } = Mo(t), c = G(n, 32, "auxRand"), a = r.toBytes(i ^ On(Ar("BIP0340/aux", c))), u = Ar("BIP0340/nonce", a, s, o), { bytes: f, scalar: d } = Mo(u), l = ra(f, s, o), h = new Uint8Array(64); + if (h.set(f, 0), h.set(r.toBytes(r.create(d + l * i)), 32), !oa(h, o, s)) throw new Error("sign: Invalid signature produced"); return h; } -function Jc(e, t, n) { - const { Fp: r, Fn: o, BASE: s } = Ge, i = G(e, 64, "signature"), c = G(t, void 0, "message"), a = G(n, 32, "publicKey"); +function oa(e, t, n) { + const { Fp: r, Fn: o, BASE: s } = qe, i = G(e, 64, "signature"), c = G(t, void 0, "message"), a = G(n, 32, "publicKey"); try { - const u = Xc(Bn(a)), f = Bn(i.subarray(0, 32)); + const u = na(On(a)), f = On(i.subarray(0, 32)); if (!r.isValidNot0(f)) return !1; - const l = Bn(i.subarray(32, 64)); - if (!o.isValidNot0(l)) + const d = On(i.subarray(32, 64)); + if (!o.isValidNot0(d)) return !1; - const d = Qc(o.toBytes(f), Ss(u), c), h = s.multiplyUnsafe(l).add(u.multiplyUnsafe(o.neg(d))), { x: w, y: g } = h.toAffine(); - return !(h.is0() || !vs(g) || w !== f); + const l = ra(o.toBytes(f), Bs(u), c), h = s.multiplyUnsafe(d).add(u.multiplyUnsafe(o.neg(l))), { x: w, y: g } = h.toAffine(); + return !(h.is0() || !Os(g) || w !== f); } catch { return !1; } } -const de = /* @__PURE__ */ (() => { - const n = (r = Wn(48)) => Kc(r, Xr.n); +const le = /* @__PURE__ */ (() => { + const n = (r = zn(48)) => qc(r, Jr.n); return { - keygen: zc(n, yi), - getPublicKey: yi, - sign: Bf, - verify: Jc, - Point: Ge, + keygen: Zc(n, xi), + getPublicKey: xi, + sign: _f, + verify: oa, + Point: qe, utils: { randomSecretKey: n, - taggedHash: Sr, - lift_x: Xc, - pointToBytes: Ss + taggedHash: Ar, + lift_x: na, + pointToBytes: Bs }, lengths: { secretKey: 32, @@ -1647,7 +1647,7 @@ const de = /* @__PURE__ */ (() => { seed: 48 } }; -})(), Of = /* @__PURE__ */ Uint8Array.from([ +})(), Cf = /* @__PURE__ */ Uint8Array.from([ 7, 4, 13, @@ -1664,36 +1664,36 @@ const de = /* @__PURE__ */ (() => { 14, 11, 8 -]), ta = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), $f = ta.map((e) => (9 * e + 5) % 16), ea = /* @__PURE__ */ (() => { - const n = [[ta], [$f]]; +]), sa = Uint8Array.from(new Array(16).fill(0).map((e, t) => t)), Pf = sa.map((e) => (9 * e + 5) % 16), ia = /* @__PURE__ */ (() => { + const n = [[sa], [Pf]]; for (let r = 0; r < 4; r++) for (let o of n) - o.push(o[r].map((s) => Of[s])); + o.push(o[r].map((s) => Cf[s])); return n; -})(), na = ea[0], ra = ea[1], oa = /* @__PURE__ */ [ +})(), ca = ia[0], aa = ia[1], ua = /* @__PURE__ */ [ [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8], [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7], [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9], [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6], [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5] -].map((e) => Uint8Array.from(e)), Uf = /* @__PURE__ */ na.map((e, t) => e.map((n) => oa[t][n])), Rf = /* @__PURE__ */ ra.map((e, t) => e.map((n) => oa[t][n])), Nf = /* @__PURE__ */ Uint32Array.from([ +].map((e) => Uint8Array.from(e)), Vf = /* @__PURE__ */ ca.map((e, t) => e.map((n) => ua[t][n])), Hf = /* @__PURE__ */ aa.map((e, t) => e.map((n) => ua[t][n])), Df = /* @__PURE__ */ Uint32Array.from([ 0, 1518500249, 1859775393, 2400959708, 2840853838 -]), Lf = /* @__PURE__ */ Uint32Array.from([ +]), Mf = /* @__PURE__ */ Uint32Array.from([ 1352829926, 1548603684, 1836072691, 2053994217, 0 ]); -function mi(e, t, n, r) { +function Si(e, t, n, r) { return e === 0 ? t ^ n ^ r : e === 1 ? t & n | ~t & r : e === 2 ? (t | ~n) ^ r : e === 3 ? t & r | n & ~r : t ^ (n | ~r); } -const Jn = /* @__PURE__ */ new Uint32Array(16); -class Cf extends Ic { +const tr = /* @__PURE__ */ new Uint32Array(16); +class Kf extends $c { h0 = 1732584193; h1 = -271733879; h2 = -1732584194; @@ -1711,84 +1711,84 @@ class Cf extends Ic { } process(t, n) { for (let h = 0; h < 16; h++, n += 4) - Jn[h] = t.getUint32(n, !0); - let r = this.h0 | 0, o = r, s = this.h1 | 0, i = s, c = this.h2 | 0, a = c, u = this.h3 | 0, f = u, l = this.h4 | 0, d = l; + tr[h] = t.getUint32(n, !0); + let r = this.h0 | 0, o = r, s = this.h1 | 0, i = s, c = this.h2 | 0, a = c, u = this.h3 | 0, f = u, d = this.h4 | 0, l = d; for (let h = 0; h < 5; h++) { - const w = 4 - h, g = Nf[h], y = Lf[h], S = na[h], v = ra[h], O = Uf[h], N = Rf[h]; - for (let U = 0; U < 16; U++) { - const W = Zn(r + mi(h, s, c, u) + Jn[S[U]] + g, O[U]) + l | 0; - r = l, l = u, u = Zn(c, 10) | 0, c = s, s = W; + const w = 4 - h, g = Df[h], y = Mf[h], S = ca[h], T = aa[h], k = Vf[h], N = Hf[h]; + for (let $ = 0; $ < 16; $++) { + const F = Xn(r + Si(h, s, c, u) + tr[S[$]] + g, k[$]) + d | 0; + r = d, d = u, u = Xn(c, 10) | 0, c = s, s = F; } - for (let U = 0; U < 16; U++) { - const W = Zn(o + mi(w, i, a, f) + Jn[v[U]] + y, N[U]) + d | 0; - o = d, d = f, f = Zn(a, 10) | 0, a = i, i = W; + for (let $ = 0; $ < 16; $++) { + const F = Xn(o + Si(w, i, a, f) + tr[T[$]] + y, N[$]) + l | 0; + o = l, l = f, f = Xn(a, 10) | 0, a = i, i = F; } } - this.set(this.h1 + c + f | 0, this.h2 + u + d | 0, this.h3 + l + o | 0, this.h4 + r + i | 0, this.h0 + s + a | 0); + this.set(this.h1 + c + f | 0, this.h2 + u + l | 0, this.h3 + d + o | 0, this.h4 + r + i | 0, this.h0 + s + a | 0); } roundClean() { - rn(Jn); + rn(tr); } destroy() { this.destroyed = !0, rn(this.buffer), this.set(0, 0, 0, 0, 0); } } -const _f = /* @__PURE__ */ Ac(() => new Cf()); +const Ff = /* @__PURE__ */ Uc(() => new Kf()); /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ function sn(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function sa(e) { +function fa(e) { if (!sn(e)) throw new Error("Uint8Array expected"); } -function ia(e, t) { +function da(e, t) { return Array.isArray(t) ? t.length === 0 ? !0 : e ? t.every((n) => typeof n == "string") : t.every((n) => Number.isSafeInteger(n)) : !1; } -function Ts(e) { +function Us(e) { if (typeof e != "function") throw new Error("function expected"); return !0; } -function Ae(e, t) { +function Ie(e, t) { if (typeof t != "string") throw new Error(`${e}: string expected`); return !0; } -function yn(e) { +function mn(e) { if (!Number.isSafeInteger(e)) throw new Error(`invalid integer: ${e}`); } -function vr(e) { +function Ir(e) { if (!Array.isArray(e)) throw new Error("array expected"); } -function Tr(e, t) { - if (!ia(!0, t)) +function kr(e, t) { + if (!da(!0, t)) throw new Error(`${e}: array of strings expected`); } -function As(e, t) { - if (!ia(!1, t)) +function $s(e, t) { + if (!da(!1, t)) throw new Error(`${e}: array of numbers expected`); } // @__NO_SIDE_EFFECTS__ -function Gn(...e) { +function qn(...e) { const t = (s) => s, n = (s, i) => (c) => s(i(c)), r = e.map((s) => s.encode).reduceRight(n, t), o = e.map((s) => s.decode).reduce(n, t); return { encode: r, decode: o }; } // @__NO_SIDE_EFFECTS__ -function Qr(e) { +function to(e) { const t = typeof e == "string" ? e.split("") : e, n = t.length; - Tr("alphabet", t); + kr("alphabet", t); const r = new Map(t.map((o, s) => [o, s])); return { - encode: (o) => (vr(o), o.map((s) => { + encode: (o) => (Ir(o), o.map((s) => { if (!Number.isSafeInteger(s) || s < 0 || s >= n) throw new Error(`alphabet.encode: digit index outside alphabet "${s}". Allowed: ${e}`); return t[s]; })), - decode: (o) => (vr(o), o.map((s) => { - Ae("alphabet.decode", s); + decode: (o) => (Ir(o), o.map((s) => { + Ie("alphabet.decode", s); const i = r.get(s); if (i === void 0) throw new Error(`Unknown letter: "${s}". Allowed: ${e}`); @@ -1797,22 +1797,22 @@ function Qr(e) { }; } // @__NO_SIDE_EFFECTS__ -function Jr(e = "") { - return Ae("join", e), { - encode: (t) => (Tr("join.decode", t), t.join(e)), - decode: (t) => (Ae("join.decode", t), t.split(e)) +function eo(e = "") { + return Ie("join", e), { + encode: (t) => (kr("join.decode", t), t.join(e)), + decode: (t) => (Ie("join.decode", t), t.split(e)) }; } // @__NO_SIDE_EFFECTS__ -function Pf(e, t = "=") { - return yn(e), Ae("padding", t), { +function Wf(e, t = "=") { + return mn(e), Ie("padding", t), { encode(n) { - for (Tr("padding.encode", n); n.length * e % 8; ) + for (kr("padding.encode", n); n.length * e % 8; ) n.push(t); return n; }, decode(n) { - Tr("padding.decode", n); + kr("padding.decode", n); let r = n.length; if (r * e % 8) throw new Error("padding: invalid, string should have whole number of bytes"); @@ -1824,32 +1824,32 @@ function Pf(e, t = "=") { }; } // @__NO_SIDE_EFFECTS__ -function Hf(e) { - return Ts(e), { encode: (t) => t, decode: (t) => e(t) }; +function zf(e) { + return Us(e), { encode: (t) => t, decode: (t) => e(t) }; } -function bi(e, t, n) { +function Ti(e, t, n) { if (t < 2) throw new Error(`convertRadix: invalid from=${t}, base cannot be less than 2`); if (n < 2) throw new Error(`convertRadix: invalid to=${n}, base cannot be less than 2`); - if (vr(e), !e.length) + if (Ir(e), !e.length) return []; let r = 0; const o = [], s = Array.from(e, (c) => { - if (yn(c), c < 0 || c >= t) + if (mn(c), c < 0 || c >= t) throw new Error(`invalid integer: ${c}`); return c; }), i = s.length; for (; ; ) { let c = 0, a = !0; for (let u = r; u < i; u++) { - const f = s[u], l = t * c, d = l + f; - if (!Number.isSafeInteger(d) || l / t !== c || d - f !== l) + const f = s[u], d = t * c, l = d + f; + if (!Number.isSafeInteger(l) || d / t !== c || l - f !== d) throw new Error("convertRadix: carry overflow"); - const h = d / n; - c = d % n; + const h = l / n; + c = l % n; const w = Math.floor(h); - if (s[u] = w, !Number.isSafeInteger(w) || w * n + c !== d) + if (s[u] = w, !Number.isSafeInteger(w) || w * n + c !== l) throw new Error("convertRadix: carry overflow"); if (a) w ? a = !1 : r = u; @@ -1862,29 +1862,29 @@ function bi(e, t, n) { o.push(0); return o.reverse(); } -const ca = (e, t) => t === 0 ? e : ca(t, e % t), Ar = /* @__NO_SIDE_EFFECTS__ */ (e, t) => e + (t - ca(e, t)), lr = /* @__PURE__ */ (() => { +const la = (e, t) => t === 0 ? e : la(t, e % t), Br = /* @__NO_SIDE_EFFECTS__ */ (e, t) => e + (t - la(e, t)), pr = /* @__PURE__ */ (() => { let e = []; for (let t = 0; t < 40; t++) e.push(2 ** t); return e; })(); -function _o(e, t, n, r) { - if (vr(e), t <= 0 || t > 32) +function Ko(e, t, n, r) { + if (Ir(e), t <= 0 || t > 32) throw new Error(`convertRadix2: wrong from=${t}`); if (n <= 0 || n > 32) throw new Error(`convertRadix2: wrong to=${n}`); - if (/* @__PURE__ */ Ar(t, n) > 32) - throw new Error(`convertRadix2: carry overflow from=${t} to=${n} carryBits=${/* @__PURE__ */ Ar(t, n)}`); + if (/* @__PURE__ */ Br(t, n) > 32) + throw new Error(`convertRadix2: carry overflow from=${t} to=${n} carryBits=${/* @__PURE__ */ Br(t, n)}`); let o = 0, s = 0; - const i = lr[t], c = lr[n] - 1, a = []; + const i = pr[t], c = pr[n] - 1, a = []; for (const u of e) { - if (yn(u), u >= i) + if (mn(u), u >= i) throw new Error(`convertRadix2: invalid data word=${u} from=${t}`); if (o = o << t | u, s + t > 32) throw new Error(`convertRadix2: carry overflow pos=${s} from=${t}`); for (s += t; s >= n; s -= n) a.push((o >> s - n & c) >>> 0); - const f = lr[s]; + const f = pr[s]; if (f === void 0) throw new Error("invalid carry"); o &= f - 1; @@ -1896,43 +1896,43 @@ function _o(e, t, n, r) { return r && s > 0 && a.push(o >>> 0), a; } // @__NO_SIDE_EFFECTS__ -function Vf(e) { - yn(e); +function Gf(e) { + mn(e); const t = 2 ** 8; return { encode: (n) => { if (!sn(n)) throw new Error("radix.encode input should be Uint8Array"); - return bi(Array.from(n), t, e); + return Ti(Array.from(n), t, e); }, - decode: (n) => (As("radix.decode", n), Uint8Array.from(bi(n, e, t))) + decode: (n) => ($s("radix.decode", n), Uint8Array.from(Ti(n, e, t))) }; } // @__NO_SIDE_EFFECTS__ -function Is(e, t = !1) { - if (yn(e), e <= 0 || e > 32) +function Rs(e, t = !1) { + if (mn(e), e <= 0 || e > 32) throw new Error("radix2: bits should be in (0..32]"); - if (/* @__PURE__ */ Ar(8, e) > 32 || /* @__PURE__ */ Ar(e, 8) > 32) + if (/* @__PURE__ */ Br(8, e) > 32 || /* @__PURE__ */ Br(e, 8) > 32) throw new Error("radix2: carry overflow"); return { encode: (n) => { if (!sn(n)) throw new Error("radix2.encode input should be Uint8Array"); - return _o(Array.from(n), 8, e, !t); + return Ko(Array.from(n), 8, e, !t); }, - decode: (n) => (As("radix2.decode", n), Uint8Array.from(_o(n, e, 8, t))) + decode: (n) => ($s("radix2.decode", n), Uint8Array.from(Ko(n, e, 8, t))) }; } -function Ei(e) { - return Ts(e), function(...t) { +function vi(e) { + return Us(e), function(...t) { try { return e.apply(null, t); } catch { } }; } -function Df(e, t) { - return yn(e), Ts(t), { +function qf(e, t) { + return mn(e), Us(t), { encode(n) { if (!sn(n)) throw new Error("checksum.encode: input should be Uint8Array"); @@ -1950,66 +1950,66 @@ function Df(e, t) { } }; } -const Mf = typeof Uint8Array.from([]).toBase64 == "function" && typeof Uint8Array.fromBase64 == "function", Kf = (e, t) => { - Ae("base64", e); +const jf = typeof Uint8Array.from([]).toBase64 == "function" && typeof Uint8Array.fromBase64 == "function", Yf = (e, t) => { + Ie("base64", e); const n = /^[A-Za-z0-9=+/]+$/, r = "base64"; if (e.length > 0 && !n.test(e)) throw new Error("invalid base64"); return Uint8Array.fromBase64(e, { alphabet: r, lastChunkHandling: "strict" }); -}, It = Mf ? { +}, wt = jf ? { encode(e) { - return sa(e), e.toBase64(); + return fa(e), e.toBase64(); }, decode(e) { - return Kf(e); + return Yf(e); } -} : /* @__PURE__ */ Gn(/* @__PURE__ */ Is(6), /* @__PURE__ */ Qr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */ Pf(6), /* @__PURE__ */ Jr("")), Ff = /* @__NO_SIDE_EFFECTS__ */ (e) => /* @__PURE__ */ Gn(/* @__PURE__ */ Vf(58), /* @__PURE__ */ Qr(e), /* @__PURE__ */ Jr("")), Po = /* @__PURE__ */ Ff("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"), Wf = (e) => /* @__PURE__ */ Gn(Df(4, (t) => e(e(t))), Po), Ho = /* @__PURE__ */ Gn(/* @__PURE__ */ Qr("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), /* @__PURE__ */ Jr("")), xi = [996825010, 642813549, 513874426, 1027748829, 705979059]; -function Sn(e) { +} : /* @__PURE__ */ qn(/* @__PURE__ */ Rs(6), /* @__PURE__ */ to("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), /* @__PURE__ */ Wf(6), /* @__PURE__ */ eo("")), Zf = /* @__NO_SIDE_EFFECTS__ */ (e) => /* @__PURE__ */ qn(/* @__PURE__ */ Gf(58), /* @__PURE__ */ to(e), /* @__PURE__ */ eo("")), Fo = /* @__PURE__ */ Zf("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"), Xf = (e) => /* @__PURE__ */ qn(qf(4, (t) => e(e(t))), Fo), Wo = /* @__PURE__ */ qn(/* @__PURE__ */ to("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), /* @__PURE__ */ eo("")), Ai = [996825010, 642813549, 513874426, 1027748829, 705979059]; +function Tn(e) { const t = e >> 25; let n = (e & 33554431) << 5; - for (let r = 0; r < xi.length; r++) - (t >> r & 1) === 1 && (n ^= xi[r]); + for (let r = 0; r < Ai.length; r++) + (t >> r & 1) === 1 && (n ^= Ai[r]); return n; } -function Si(e, t, n = 1) { +function Ii(e, t, n = 1) { const r = e.length; let o = 1; for (let s = 0; s < r; s++) { const i = e.charCodeAt(s); if (i < 33 || i > 126) throw new Error(`Invalid prefix (${e})`); - o = Sn(o) ^ i >> 5; + o = Tn(o) ^ i >> 5; } - o = Sn(o); + o = Tn(o); for (let s = 0; s < r; s++) - o = Sn(o) ^ e.charCodeAt(s) & 31; + o = Tn(o) ^ e.charCodeAt(s) & 31; for (let s of t) - o = Sn(o) ^ s; + o = Tn(o) ^ s; for (let s = 0; s < 6; s++) - o = Sn(o); - return o ^= n, Ho.encode(_o([o % lr[30]], 30, 5, !1)); + o = Tn(o); + return o ^= n, Wo.encode(Ko([o % pr[30]], 30, 5, !1)); } // @__NO_SIDE_EFFECTS__ -function aa(e) { - const t = e === "bech32" ? 1 : 734539939, n = /* @__PURE__ */ Is(5), r = n.decode, o = n.encode, s = Ei(r); - function i(l, d, h = 90) { - Ae("bech32.encode prefix", l), sn(d) && (d = Array.from(d)), As("bech32.encode", d); - const w = l.length; +function ha(e) { + const t = e === "bech32" ? 1 : 734539939, n = /* @__PURE__ */ Rs(5), r = n.decode, o = n.encode, s = vi(r); + function i(d, l, h = 90) { + Ie("bech32.encode prefix", d), sn(l) && (l = Array.from(l)), $s("bech32.encode", l); + const w = d.length; if (w === 0) throw new TypeError(`Invalid prefix length ${w}`); - const g = w + 7 + d.length; + const g = w + 7 + l.length; if (h !== !1 && g > h) throw new TypeError(`Length ${g} exceeds limit ${h}`); - const y = l.toLowerCase(), S = Si(y, d, t); - return `${y}1${Ho.encode(d)}${S}`; - } - function c(l, d = 90) { - Ae("bech32.decode input", l); - const h = l.length; - if (h < 8 || d !== !1 && h > d) - throw new TypeError(`invalid string length: ${h} (${l}). Expected (8..${d})`); - const w = l.toLowerCase(); - if (l !== w && l !== l.toUpperCase()) + const y = d.toLowerCase(), S = Ii(y, l, t); + return `${y}1${Wo.encode(l)}${S}`; + } + function c(d, l = 90) { + Ie("bech32.decode input", d); + const h = d.length; + if (h < 8 || l !== !1 && h > l) + throw new TypeError(`invalid string length: ${h} (${d}). Expected (8..${l})`); + const w = d.toLowerCase(); + if (d !== w && d !== d.toUpperCase()) throw new Error("String must be lowercase or uppercase"); const g = w.lastIndexOf("1"); if (g === 0 || g === -1) @@ -2017,18 +2017,18 @@ function aa(e) { const y = w.slice(0, g), S = w.slice(g + 1); if (S.length < 6) throw new Error("Data must be at least 6 characters long"); - const v = Ho.decode(S).slice(0, -6), O = Si(y, v, t); - if (!S.endsWith(O)) - throw new Error(`Invalid checksum in ${l}: expected "${O}"`); - return { prefix: y, words: v }; + const T = Wo.decode(S).slice(0, -6), k = Ii(y, T, t); + if (!S.endsWith(k)) + throw new Error(`Invalid checksum in ${d}: expected "${k}"`); + return { prefix: y, words: T }; } - const a = Ei(c); - function u(l) { - const { prefix: d, words: h } = c(l, !1); - return { prefix: d, words: h, bytes: r(h) }; + const a = vi(c); + function u(d) { + const { prefix: l, words: h } = c(d, !1); + return { prefix: l, words: h, bytes: r(h) }; } - function f(l, d) { - return i(l, o(d)); + function f(d, l) { + return i(d, o(l)); } return { encode: i, @@ -2041,21 +2041,21 @@ function aa(e) { toWords: o }; } -const Vo = /* @__PURE__ */ aa("bech32"), je = /* @__PURE__ */ aa("bech32m"), zf = { +const zo = /* @__PURE__ */ ha("bech32"), Ye = /* @__PURE__ */ ha("bech32m"), Qf = { encode: (e) => new TextDecoder().decode(e), decode: (e) => new TextEncoder().encode(e) -}, Gf = typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", qf = { +}, Jf = typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", td = { encode(e) { - return sa(e), e.toHex(); + return fa(e), e.toHex(); }, decode(e) { - return Ae("hex", e), Uint8Array.fromHex(e); + return Ie("hex", e), Uint8Array.fromHex(e); } -}, $ = Gf ? qf : /* @__PURE__ */ Gn(/* @__PURE__ */ Is(4), /* @__PURE__ */ Qr("0123456789abcdef"), /* @__PURE__ */ Jr(""), /* @__PURE__ */ Hf((e) => { +}, U = Jf ? td : /* @__PURE__ */ qn(/* @__PURE__ */ Rs(4), /* @__PURE__ */ to("0123456789abcdef"), /* @__PURE__ */ eo(""), /* @__PURE__ */ zf((e) => { if (typeof e != "string" || e.length % 2 !== 0) throw new TypeError(`hex.decode: expected string, got ${typeof e} with length ${e.length}`); return e.toLowerCase(); -})), ot = /* @__PURE__ */ Uint8Array.of(), ua = /* @__PURE__ */ Uint8Array.of(0); +})), ot = /* @__PURE__ */ Uint8Array.of(), pa = /* @__PURE__ */ Uint8Array.of(0); function cn(e, t) { if (e.length !== t.length) return !1; @@ -2064,14 +2064,14 @@ function cn(e, t) { return !1; return !0; } -function Ct(e) { +function _t(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function jf(...e) { +function ed(...e) { let t = 0; for (let r = 0; r < e.length; r++) { const o = e[r]; - if (!Ct(o)) + if (!_t(o)) throw new Error("Uint8Array expected"); t += o.length; } @@ -2082,25 +2082,25 @@ function jf(...e) { } return n; } -const fa = (e) => new DataView(e.buffer, e.byteOffset, e.byteLength); -function qn(e) { +const ga = (e) => new DataView(e.buffer, e.byteOffset, e.byteLength); +function jn(e) { return Object.prototype.toString.call(e) === "[object Object]"; } -function Qt(e) { +function Jt(e) { return Number.isSafeInteger(e); } -const ks = { +const Ns = { equalBytes: cn, - isBytes: Ct, - concatBytes: jf -}, la = (e) => { - if (e !== null && typeof e != "string" && !Ft(e) && !Ct(e) && !Qt(e)) + isBytes: _t, + concatBytes: ed +}, wa = (e) => { + if (e !== null && typeof e != "string" && !Wt(e) && !_t(e) && !Jt(e)) throw new Error(`lengthCoder: expected null | number | Uint8Array | CoderType, got ${e} (${typeof e})`); return { encodeStream(t, n) { if (e === null) return; - if (Ft(e)) + if (Wt(e)) return e.encodeStream(t, n); let r; if (typeof e == "number" ? r = e : typeof e == "string" && (r = fe.resolve(t.stack, e)), typeof r == "bigint" && (r = Number(r)), r === void 0 || r !== n) @@ -2108,22 +2108,22 @@ const ks = { }, decodeStream(t) { let n; - if (Ft(e) ? n = Number(e.decodeStream(t)) : typeof e == "number" ? n = e : typeof e == "string" && (n = fe.resolve(t.stack, e)), typeof n == "bigint" && (n = Number(n)), typeof n != "number") + if (Wt(e) ? n = Number(e.decodeStream(t)) : typeof e == "number" ? n = e : typeof e == "string" && (n = fe.resolve(t.stack, e)), typeof n == "bigint" && (n = Number(n)), typeof n != "number") throw t.err(`Wrong length: ${n}`); return n; } }; -}, dt = { +}, lt = { BITS: 32, FULL_MASK: -1 >>> 0, // 1<<32 will overflow len: (e) => Math.ceil(e / 32), - create: (e) => new Uint32Array(dt.len(e)), + create: (e) => new Uint32Array(lt.len(e)), clean: (e) => e.fill(0), debug: (e) => Array.from(e).map((t) => (t >>> 0).toString(2).padStart(32, "0")), checkLen: (e, t) => { - if (dt.len(t) !== e.length) - throw new Error(`wrong length=${e.length}. Expected: ${dt.len(t)}`); + if (lt.len(t) !== e.length) + throw new Error(`wrong length=${e.length}. Expected: ${lt.len(t)}`); }, chunkLen: (e, t, n) => { if (t < 0) @@ -2137,14 +2137,14 @@ const ks = { mask: 1 << 32 - (e + t) % 32 - 1 }), indices: (e, t, n = !1) => { - dt.checkLen(e, t); - const { FULL_MASK: r, BITS: o } = dt, s = o - t % o, i = s ? r >>> s << s : r, c = []; + lt.checkLen(e, t); + const { FULL_MASK: r, BITS: o } = lt, s = o - t % o, i = s ? r >>> s << s : r, c = []; for (let a = 0; a < e.length; a++) { let u = e[a]; if (n && (u = ~u), a === e.length - 1 && (u &= i), u !== 0) for (let f = 0; f < o; f++) { - const l = 1 << o - f - 1; - u & l && c.push(a * o + f); + const d = 1 << o - f - 1; + u & d && c.push(a * o + f); } } return c; @@ -2156,19 +2156,19 @@ const ks = { n === void 0 || r !== n.pos + n.length ? t.push(n = { pos: r, length: 1 }) : n.length += 1; return t; }, - rangeDebug: (e, t, n = !1) => `[${dt.range(dt.indices(e, t, n)).map((r) => `(${r.pos}/${r.length})`).join(", ")}]`, + rangeDebug: (e, t, n = !1) => `[${lt.range(lt.indices(e, t, n)).map((r) => `(${r.pos}/${r.length})`).join(", ")}]`, setRange: (e, t, n, r, o = !0) => { - dt.chunkLen(t, n, r); - const { FULL_MASK: s, BITS: i } = dt, c = n % i ? Math.floor(n / i) : void 0, a = n + r, u = a % i ? Math.floor(a / i) : void 0; + lt.chunkLen(t, n, r); + const { FULL_MASK: s, BITS: i } = lt, c = n % i ? Math.floor(n / i) : void 0, a = n + r, u = a % i ? Math.floor(a / i) : void 0; if (c !== void 0 && c === u) - return dt.set(e, c, s >>> i - r << i - r - n, o); - if (c !== void 0 && !dt.set(e, c, s >>> n % i, o)) + return lt.set(e, c, s >>> i - r << i - r - n, o); + if (c !== void 0 && !lt.set(e, c, s >>> n % i, o)) return !1; - const f = c !== void 0 ? c + 1 : n / i, l = u !== void 0 ? u : a / i; - for (let d = f; d < l; d++) - if (!dt.set(e, d, s, o)) + const f = c !== void 0 ? c + 1 : n / i, d = u !== void 0 ? u : a / i; + for (let l = f; l < d; l++) + if (!lt.set(e, l, s, o)) return !1; - return !(u !== void 0 && c !== u && !dt.set(e, u, s << i - a % i, o)); + return !(u !== void 0 && c !== u && !lt.set(e, u, s << i - a % i, o)); } }, fe = { /** @@ -2209,7 +2209,7 @@ const ks = { return s; } }; -class Bs { +class Ls { pos = 0; data; opts; @@ -2222,16 +2222,16 @@ class Bs { // bitset view; constructor(t, n = {}, r = [], o = void 0, s = 0) { - this.data = t, this.opts = n, this.stack = r, this.parent = o, this.parentOffset = s, this.view = fa(t); + this.data = t, this.opts = n, this.stack = r, this.parent = o, this.parentOffset = s, this.view = ga(t); } /** Internal method for pointers. */ _enablePointers() { if (this.parent) return this.parent._enablePointers(); - this.bs || (this.bs = dt.create(this.data.length), dt.setRange(this.bs, this.data.length, 0, this.pos, this.opts.allowMultipleReads)); + this.bs || (this.bs = lt.create(this.data.length), lt.setRange(this.bs, this.data.length, 0, this.pos, this.opts.allowMultipleReads)); } markBytesBS(t, n) { - return this.parent ? this.parent.markBytesBS(this.parentOffset + t, n) : !n || !this.bs ? !0 : dt.setRange(this.bs, this.data.length, t, n, !1); + return this.parent ? this.parent.markBytesBS(this.parentOffset + t, n) : !n || !this.bs ? !0 : lt.setRange(this.bs, this.data.length, t, n, !1); } markBytes(t) { const n = this.pos; @@ -2261,17 +2261,17 @@ class Bs { finish() { if (!this.opts.allowUnreadBytes) { if (this.bitPos) - throw this.err(`${this.bitPos} bits left after unpack: ${$.encode(this.data.slice(this.pos))}`); + throw this.err(`${this.bitPos} bits left after unpack: ${U.encode(this.data.slice(this.pos))}`); if (this.bs && !this.parent) { - const t = dt.indices(this.bs, this.data.length, !0); + const t = lt.indices(this.bs, this.data.length, !0); if (t.length) { - const n = dt.range(t).map(({ pos: r, length: o }) => `(${r}/${o})[${$.encode(this.data.subarray(r, r + o))}]`).join(", "); + const n = lt.range(t).map(({ pos: r, length: o }) => `(${r}/${o})[${U.encode(this.data.subarray(r, r + o))}]`).join(", "); throw this.err(`unread byte ranges: ${n} (total=${this.data.length})`); } else return; } if (!this.isEnd()) - throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${$.encode(this.data.slice(this.pos))}`); + throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${U.encode(this.data.slice(this.pos))}`); } } // User methods @@ -2281,7 +2281,7 @@ class Bs { offsetReader(t) { if (t > this.data.length) throw this.err("offsetReader: Unexpected end of buffer"); - return new Bs(this.absBytes(t), this.opts, this.stack, this, t); + return new Ls(this.absBytes(t), this.opts, this.stack, this, t); } bytes(t, n = !1) { if (this.bitPos) @@ -2323,7 +2323,7 @@ class Bs { return n >>> 0; } find(t, n = this.pos) { - if (!Ct(t)) + if (!_t(t)) throw this.err(`find: needle is not bytes! ${t}`); if (this.bitPos) throw this.err("findByte: bitPos not empty"); @@ -2337,7 +2337,7 @@ class Bs { } } } -class Yf { +class nd { pos = 0; stack; // We could have a single buffer here and re-alloc it with @@ -2351,7 +2351,7 @@ class Yf { view; finished = !1; constructor(t = []) { - this.stack = t, this.view = fa(this.viewBuf); + this.stack = t, this.view = ga(this.viewBuf); } pushObj(t, n) { return fe.pushObj(this.stack, t, n); @@ -2359,7 +2359,7 @@ class Yf { writeView(t, n) { if (this.finished) throw this.err("buffer: finished"); - if (!Qt(t) || t > 8) + if (!Jt(t) || t > 8) throw new Error(`wrong writeView length=${t}`); n(this.view), this.bytes(this.viewBuf.slice(0, t)), this.viewBuf.fill(0); } @@ -2416,8 +2416,8 @@ class Yf { } } } -const Do = (e) => Uint8Array.from(e).reverse(); -function Zf(e, t, n) { +const Go = (e) => Uint8Array.from(e).reverse(); +function rd(e, t, n) { if (n) { const r = 2n ** (t - 1n); if (e < -r || e >= r) @@ -2425,28 +2425,28 @@ function Zf(e, t, n) { } else if (0n > e || e >= 2n ** t) throw new Error(`value out of unsigned bounds. Expected 0 <= ${e} < ${2n ** t}`); } -function da(e) { +function ya(e) { return { // NOTE: we cannot export validate here, since it is likely mistake. encodeStream: e.encodeStream, decodeStream: e.decodeStream, size: e.size, encode: (t) => { - const n = new Yf(); + const n = new nd(); return e.encodeStream(n, t), n.finish(); }, decode: (t, n = {}) => { - const r = new Bs(t, n), o = e.decodeStream(r); + const r = new Ls(t, n), o = e.decodeStream(r); return r.finish(), o; } }; } -function Tt(e, t) { - if (!Ft(e)) +function At(e, t) { + if (!Wt(e)) throw new Error(`validate: invalid inner value ${e}`); if (typeof t != "function") throw new Error("validate: fn should be function"); - return da({ + return ya({ size: e.size, encodeStream: (n, r) => { let o; @@ -2467,14 +2467,14 @@ function Tt(e, t) { } }); } -const At = (e) => { - const t = da(e); - return e.validate ? Tt(t, e.validate) : t; -}, to = (e) => qn(e) && typeof e.decode == "function" && typeof e.encode == "function"; -function Ft(e) { - return qn(e) && to(e) && typeof e.encodeStream == "function" && typeof e.decodeStream == "function" && (e.size === void 0 || Qt(e.size)); +const It = (e) => { + const t = ya(e); + return e.validate ? At(t, e.validate) : t; +}, no = (e) => jn(e) && typeof e.decode == "function" && typeof e.encode == "function"; +function Wt(e) { + return jn(e) && no(e) && typeof e.encodeStream == "function" && typeof e.decodeStream == "function" && (e.size === void 0 || Jt(e.size)); } -function Xf() { +function od() { return { encode: (e) => { if (!Array.isArray(e)) @@ -2491,13 +2491,13 @@ function Xf() { return t; }, decode: (e) => { - if (!qn(e)) + if (!jn(e)) throw new Error(`expected plain object, got ${e}`); return Object.entries(e); } }; } -const Qf = { +const sd = { encode: (e) => { if (typeof e != "bigint") throw new Error(`expected bigint, got ${typeof e}`); @@ -2506,17 +2506,17 @@ const Qf = { return Number(e); }, decode: (e) => { - if (!Qt(e)) + if (!Jt(e)) throw new Error("element is not a safe integer"); return BigInt(e); } }; -function Jf(e) { - if (!qn(e)) +function id(e) { + if (!jn(e)) throw new Error("plain object expected"); return { encode: (t) => { - if (!Qt(t) || !(t in e)) + if (!Jt(t) || !(t in e)) throw new Error(`wrong value ${t}`); return e[t]; }, @@ -2527,8 +2527,8 @@ function Jf(e) { } }; } -function tl(e, t = !1) { - if (!Qt(e)) +function cd(e, t = !1) { + if (!Jt(e)) throw new Error(`decimal/precision: wrong value ${e}`); if (typeof t != "boolean") throw new Error(`decimal/round: expected boolean, got ${typeof t}`); @@ -2558,16 +2558,16 @@ function tl(e, t = !1) { const i = r.slice(0, s), c = r.slice(s + 1).replace(/0+$/, ""), a = BigInt(i) * n; if (!t && c.length > e) throw new Error(`fractional part cannot be represented with this precision (num=${r}, prec=${e})`); - const u = Math.min(c.length, e), f = BigInt(c.slice(0, u)) * 10n ** BigInt(e - u), l = a + f; - return o ? -l : l; + const u = Math.min(c.length, e), f = BigInt(c.slice(0, u)) * 10n ** BigInt(e - u), d = a + f; + return o ? -d : d; } }; } -function el(e) { +function ad(e) { if (!Array.isArray(e)) throw new Error(`expected array, got ${typeof e}`); for (const t of e) - if (!to(t)) + if (!no(t)) throw new Error(`wrong base coder ${t}`); return { encode: (t) => { @@ -2588,12 +2588,12 @@ function el(e) { } }; } -const ha = (e) => { - if (!to(e)) +const ma = (e) => { + if (!no(e)) throw new Error("BaseCoder expected"); return { encode: e.decode, decode: e.encode }; -}, eo = { dict: Xf, numberBigint: Qf, tsEnum: Jf, decimal: tl, match: el, reverse: ha }, Os = (e, t = !1, n = !1, r = !0) => { - if (!Qt(e)) +}, ro = { dict: od, numberBigint: sd, tsEnum: id, decimal: cd, match: ad, reverse: ma }, _s = (e, t = !1, n = !1, r = !0) => { + if (!Jt(e)) throw new Error(`bigint/size: wrong value ${e}`); if (typeof t != "boolean") throw new Error(`bigint/le: expected boolean, got ${typeof t}`); @@ -2602,7 +2602,7 @@ const ha = (e) => { if (typeof r != "boolean") throw new Error(`bigint/sized: expected boolean, got ${typeof r}`); const o = BigInt(e), s = 2n ** (8n * o - 1n); - return At({ + return It({ size: r ? e : void 0, encodeStream: (i, c) => { n && c < 0 && (c = c | s); @@ -2619,7 +2619,7 @@ const ha = (e) => { i.bytes(t ? u.reverse() : u); }, decodeStream: (i) => { - const c = i.bytes(r ? e : Math.min(e, i.leftBytes)), a = t ? c : Do(c); + const c = i.bytes(r ? e : Math.min(e, i.leftBytes)), a = t ? c : Go(c); let u = 0n; for (let f = 0; f < a.length; f++) u |= BigInt(a[f]) << 8n * BigInt(f); @@ -2628,10 +2628,10 @@ const ha = (e) => { validate: (i) => { if (typeof i != "bigint") throw new Error(`bigint: invalid value: ${i}`); - return Zf(i, 8n * o, !!n), i; + return rd(i, 8n * o, !!n), i; } }); -}, pa = /* @__PURE__ */ Os(32, !1), dr = /* @__PURE__ */ Os(8, !0), nl = /* @__PURE__ */ Os(8, !0, !0), rl = (e, t) => At({ +}, ba = /* @__PURE__ */ _s(32, !1), gr = /* @__PURE__ */ _s(8, !0), ud = /* @__PURE__ */ _s(8, !0, !0), fd = (e, t) => It({ size: e, encodeStream: (n, r) => n.writeView(e, (o) => t.write(o, r)), decodeStream: (n) => n.readView(e, t.read), @@ -2640,46 +2640,46 @@ const ha = (e) => { throw new Error(`viewCoder: expected number, got ${typeof n}`); return t.validate && t.validate(n), n; } -}), jn = (e, t, n) => { +}), Yn = (e, t, n) => { const r = e * 8, o = 2 ** (r - 1), s = (a) => { - if (!Qt(a)) + if (!Jt(a)) throw new Error(`sintView: value is not safe integer: ${a}`); if (a < -o || a >= o) throw new Error(`sintView: value out of bounds. Expected ${-o} <= ${a} < ${o}`); }, i = 2 ** r, c = (a) => { - if (!Qt(a)) + if (!Jt(a)) throw new Error(`uintView: value is not safe integer: ${a}`); if (0 > a || a >= i) throw new Error(`uintView: value out of bounds. Expected 0 <= ${a} < ${i}`); }; - return rl(e, { + return fd(e, { write: n.write, read: n.read, validate: t ? s : c }); -}, Z = /* @__PURE__ */ jn(4, !1, { +}, X = /* @__PURE__ */ Yn(4, !1, { read: (e, t) => e.getUint32(t, !0), write: (e, t) => e.setUint32(0, t, !0) -}), ol = /* @__PURE__ */ jn(4, !1, { +}), dd = /* @__PURE__ */ Yn(4, !1, { read: (e, t) => e.getUint32(t, !1), write: (e, t) => e.setUint32(0, t, !1) -}), Ye = /* @__PURE__ */ jn(4, !0, { +}), Ze = /* @__PURE__ */ Yn(4, !0, { read: (e, t) => e.getInt32(t, !0), write: (e, t) => e.setInt32(0, t, !0) -}), vi = /* @__PURE__ */ jn(2, !1, { +}), ki = /* @__PURE__ */ Yn(2, !1, { read: (e, t) => e.getUint16(t, !0), write: (e, t) => e.setUint16(0, t, !0) -}), Se = /* @__PURE__ */ jn(1, !1, { +}), Te = /* @__PURE__ */ Yn(1, !1, { read: (e, t) => e.getUint8(t), write: (e, t) => e.setUint8(0, t) }), rt = (e, t = !1) => { if (typeof t != "boolean") throw new Error(`bytes/le: expected boolean, got ${typeof t}`); - const n = la(e), r = Ct(e); - return At({ + const n = wa(e), r = _t(e); + return It({ size: typeof e == "number" ? e : void 0, encodeStream: (o, s) => { - r || n.encodeStream(o, s.length), o.bytes(t ? Do(s) : s), r && o.bytes(e); + r || n.encodeStream(o, s.length), o.bytes(t ? Go(s) : s), r && o.bytes(e); }, decodeStream: (o) => { let s; @@ -2690,30 +2690,30 @@ const ha = (e) => { s = o.bytes(i - o.pos), o.bytes(e.length); } else s = o.bytes(e === null ? o.leftBytes : n.decodeStream(o)); - return t ? Do(s) : s; + return t ? Go(s) : s; }, validate: (o) => { - if (!Ct(o)) + if (!_t(o)) throw new Error(`bytes: invalid value ${o}`); return o; } }); }; -function sl(e, t) { - if (!Ft(t)) +function ld(e, t) { + if (!Wt(t)) throw new Error(`prefix: invalid inner value ${t}`); - return Ie(rt(e), ha(t)); + return ke(rt(e), ma(t)); } -const $s = (e, t = !1) => Tt(Ie(rt(e, t), zf), (n) => { +const Cs = (e, t = !1) => At(ke(rt(e, t), Qf), (n) => { if (typeof n != "string") throw new Error(`expected string, got ${typeof n}`); return n; -}), il = (e, t = { isLE: !1, with0x: !1 }) => { - let n = Ie(rt(e, t.isLE), $); +}), hd = (e, t = { isLE: !1, with0x: !1 }) => { + let n = ke(rt(e, t.isLE), U); const r = t.with0x; if (typeof r != "boolean") throw new Error(`hex/with0x: expected boolean, got ${typeof r}`); - return r && (n = Ie(n, { + return r && (n = ke(n, { encode: (o) => `0x${o}`, decode: (o) => { if (!o.startsWith("0x")) @@ -2722,12 +2722,12 @@ const $s = (e, t = !1) => Tt(Ie(rt(e, t), zf), (n) => { } })), n; }; -function Ie(e, t) { - if (!Ft(e)) +function ke(e, t) { + if (!Wt(e)) throw new Error(`apply: invalid inner value ${e}`); - if (!to(t)) + if (!no(t)) throw new Error(`apply: invalid base value ${e}`); - return At({ + return It({ size: e.size, encodeStream: (n, r) => { let o; @@ -2748,12 +2748,12 @@ function Ie(e, t) { } }); } -const cl = (e, t = !1) => { - if (!Ct(e)) +const pd = (e, t = !1) => { + if (!_t(e)) throw new Error(`flag/flagValue: expected Uint8Array, got ${typeof e}`); if (typeof t != "boolean") throw new Error(`flag/xor: expected boolean, got ${typeof t}`); - return At({ + return It({ size: e.length, encodeStream: (n, r) => { !!r !== t && n.bytes(e); @@ -2769,10 +2769,10 @@ const cl = (e, t = !1) => { } }); }; -function al(e, t, n) { - if (!Ft(t)) +function gd(e, t, n) { + if (!Wt(t)) throw new Error(`flagged: invalid inner value ${t}`); - return At({ + return It({ encodeStream: (r, o) => { fe.resolve(r.stack, e) && t.encodeStream(r, o); }, @@ -2783,17 +2783,17 @@ function al(e, t, n) { } }); } -function Us(e, t, n = !0) { - if (!Ft(e)) +function Ps(e, t, n = !0) { + if (!Wt(e)) throw new Error(`magic: invalid inner value ${e}`); if (typeof n != "boolean") throw new Error(`magic: expected boolean, got ${typeof n}`); - return At({ + return It({ size: e.size, encodeStream: (r, o) => e.encodeStream(r, t), decodeStream: (r) => { const o = e.decodeStream(r); - if (n && typeof o != "object" && o !== t || Ct(t) && !cn(t, o)) + if (n && typeof o != "object" && o !== t || _t(t) && !cn(t, o)) throw r.err(`magic: invalid value: ${o} !== ${t}`); }, validate: (r) => { @@ -2803,25 +2803,25 @@ function Us(e, t, n = !0) { } }); } -function ga(e) { +function Ea(e) { let t = 0; for (const n of e) { if (n.size === void 0) return; - if (!Qt(n.size)) + if (!Jt(n.size)) throw new Error(`sizeof: wrong element size=${t}`); t += n.size; } return t; } function pt(e) { - if (!qn(e)) + if (!jn(e)) throw new Error(`struct: expected plain object, got ${e}`); for (const t in e) - if (!Ft(e[t])) + if (!Wt(e[t])) throw new Error(`struct: field ${t} is not CoderType`); - return At({ - size: ga(Object.values(e)), + return It({ + size: Ea(Object.values(e)), encodeStream: (t, n) => { t.pushObj(n, (r) => { for (const o in e) @@ -2842,14 +2842,14 @@ function pt(e) { } }); } -function ul(e) { +function wd(e) { if (!Array.isArray(e)) throw new Error(`Packed.Tuple: got ${typeof e} instead of array`); for (let t = 0; t < e.length; t++) - if (!Ft(e[t])) + if (!Wt(e[t])) throw new Error(`tuple: field ${t} is not CoderType`); - return At({ - size: ga(e), + return It({ + size: Ea(e), encodeStream: (t, n) => { if (!Array.isArray(n)) throw t.err(`tuple: invalid value ${n}`); @@ -2875,19 +2875,19 @@ function ul(e) { }); } function vt(e, t) { - if (!Ft(t)) + if (!Wt(t)) throw new Error(`array: invalid inner value ${t}`); - const n = la(typeof e == "string" ? `../${e}` : e); - return At({ + const n = wa(typeof e == "string" ? `../${e}` : e); + return It({ size: typeof e == "number" && t.size ? e * t.size : void 0, encodeStream: (r, o) => { const s = r; s.pushObj(o, (i) => { - Ct(e) || n.encodeStream(r, o.length); + _t(e) || n.encodeStream(r, o.length); for (let c = 0; c < o.length; c++) i(`${c}`, () => { const a = o[c], u = r.pos; - if (t.encodeStream(r, a), Ct(e)) { + if (t.encodeStream(r, a), _t(e)) { if (e.length > s.pos - u) return; const f = s.finish(!1).subarray(u, s.pos); @@ -2895,7 +2895,7 @@ function vt(e, t) { throw s.err(`array: inner element encoding same as separator. elm=${a} data=${f}`); } }); - }), Ct(e) && r.bytes(e); + }), _t(e) && r.bytes(e); }, decodeStream: (r) => { const o = []; @@ -2903,7 +2903,7 @@ function vt(e, t) { if (e === null) for (let i = 0; !r.isEnd() && (s(`${i}`, () => o.push(t.decodeStream(r))), !(t.size && r.leftBytes < t.size)); i++) ; - else if (Ct(e)) + else if (_t(e)) for (let i = 0; ; i++) { if (cn(r.bytes(e.length, !0), e)) { r.bytes(e.length); @@ -2926,19 +2926,19 @@ function vt(e, t) { } }); } -const mn = xe.Point, Ti = mn.Fn, wa = mn.Fn.ORDER, Yn = (e) => e % 2n === 0n, nt = ks.isBytes, Ee = ks.concatBytes, ct = ks.equalBytes, ya = (e) => _f(wt(e)), ge = (...e) => wt(wt(Ee(...e))), Mo = de.utils.randomSecretKey, Rs = de.getPublicKey, ma = xe.getPublicKey, Ai = (e) => e.r < wa / 2n; -function fl(e, t, n = !1) { - let r = xe.Signature.fromBytes(xe.sign(e, t, { prehash: !1 })); - if (n && !Ai(r)) { +const bn = Se.Point, Bi = bn.Fn, xa = bn.Fn.ORDER, Zn = (e) => e % 2n === 0n, nt = Ns.isBytes, Ee = Ns.concatBytes, ct = Ns.equalBytes, Sa = (e) => Ff(yt(e)), ge = (...e) => yt(yt(Ee(...e))), qo = le.utils.randomSecretKey, Vs = le.getPublicKey, Ta = Se.getPublicKey, Oi = (e) => e.r < xa / 2n; +function yd(e, t, n = !1) { + let r = Se.Signature.fromBytes(Se.sign(e, t, { prehash: !1 })); + if (n && !Oi(r)) { const o = new Uint8Array(32); let s = 0; - for (; !Ai(r); ) - if (o.set(Z.encode(s++)), r = xe.Signature.fromBytes(xe.sign(e, t, { prehash: !1, extraEntropy: o })), s > 4294967295) + for (; !Oi(r); ) + if (o.set(X.encode(s++)), r = Se.Signature.fromBytes(Se.sign(e, t, { prehash: !1, extraEntropy: o })), s > 4294967295) throw new Error("lowR counter overflow: report the error"); } return r.toBytes("der"); } -const Ii = de.sign, Ns = de.utils.taggedHash, kt = { +const Ui = le.sign, Hs = le.utils.taggedHash, kt = { ecdsa: 0, schnorr: 1 }; @@ -2947,40 +2947,40 @@ function an(e, t) { if (t === kt.ecdsa) { if (n === 32) throw new Error("Expected non-Schnorr key"); - return mn.fromBytes(e), e; + return bn.fromBytes(e), e; } else if (t === kt.schnorr) { if (n !== 32) throw new Error("Expected 32-byte Schnorr key"); - return de.utils.lift_x(le(e)), e; + return le.utils.lift_x(de(e)), e; } else throw new Error("Unknown key type"); } -function ba(e, t) { - const r = de.utils.taggedHash("TapTweak", e, t), o = le(r); - if (o >= wa) +function va(e, t) { + const r = le.utils.taggedHash("TapTweak", e, t), o = de(r); + if (o >= xa) throw new Error("tweak higher than curve order"); return o; } -function ll(e, t = Uint8Array.of()) { - const n = de.utils, r = le(e), o = mn.BASE.multiply(r), s = Yn(o.y) ? r : Ti.neg(r), i = n.pointToBytes(o), c = ba(i, t); - return zn(Ti.add(s, c), 32); +function md(e, t = Uint8Array.of()) { + const n = le.utils, r = de(e), o = bn.BASE.multiply(r), s = Zn(o.y) ? r : Bi.neg(r), i = n.pointToBytes(o), c = va(i, t); + return Gn(Bi.add(s, c), 32); } -function Ko(e, t) { - const n = de.utils, r = ba(e, t), s = n.lift_x(le(e)).add(mn.BASE.multiply(r)), i = Yn(s.y) ? 0 : 1; +function jo(e, t) { + const n = le.utils, r = va(e, t), s = n.lift_x(de(e)).add(bn.BASE.multiply(r)), i = Zn(s.y) ? 0 : 1; return [n.pointToBytes(s), i]; } -const Ls = wt(mn.BASE.toBytes(!1)), un = { +const Ds = yt(bn.BASE.toBytes(!1)), un = { bech32: "bc", pubKeyHash: 0, scriptHash: 5, wif: 128 -}, tr = { +}, er = { bech32: "tb", pubKeyHash: 111, scriptHash: 196, wif: 239 }; -function Ir(e, t) { +function Or(e, t) { if (!nt(e) || !nt(t)) throw new Error(`cmp: wrong type a=${typeof e} b=${typeof t}`); const n = Math.min(e.length, t.length); @@ -2989,7 +2989,7 @@ function Ir(e, t) { return Math.sign(e[r] - t[r]); return Math.sign(e.length - t.length); } -function Ea(e) { +function Aa(e) { const t = {}; for (const n in e) { if (t[e[n]] !== void 0) @@ -2998,7 +2998,7 @@ function Ea(e) { } return t; } -const lt = { +const dt = { OP_0: 0, PUSHDATA1: 76, PUSHDATA2: 77, @@ -3121,9 +3121,9 @@ const lt = { CHECKSIGADD: 186, // Invalid INVALID: 255 -}, dl = Ea(lt); -function Cs(e = 6, t = !1) { - return At({ +}, bd = Aa(dt); +function Ms(e = 6, t = !1) { + return It({ encodeStream: (n, r) => { if (r === 0n) return; @@ -3150,63 +3150,63 @@ function Cs(e = 6, t = !1) { } }); } -function hl(e, t = 4, n = !0) { +function Ed(e, t = 4, n = !0) { if (typeof e == "number") return e; if (nt(e)) try { - const r = Cs(t, n).decode(e); + const r = Ms(t, n).decode(e); return r > Number.MAX_SAFE_INTEGER ? void 0 : Number(r); } catch { return; } } -const K = At({ +const K = It({ encodeStream: (e, t) => { for (let n of t) { if (typeof n == "string") { - if (lt[n] === void 0) + if (dt[n] === void 0) throw new Error(`Unknown opcode=${n}`); - e.byte(lt[n]); + e.byte(dt[n]); continue; } else if (typeof n == "number") { if (n === 0) { e.byte(0); continue; } else if (1 <= n && n <= 16) { - e.byte(lt.OP_1 - 1 + n); + e.byte(dt.OP_1 - 1 + n); continue; } } - if (typeof n == "number" && (n = Cs().encode(BigInt(n))), !nt(n)) + if (typeof n == "number" && (n = Ms().encode(BigInt(n))), !nt(n)) throw new Error(`Wrong Script OP=${n} (${typeof n})`); const r = n.length; - r < lt.PUSHDATA1 ? e.byte(r) : r <= 255 ? (e.byte(lt.PUSHDATA1), e.byte(r)) : r <= 65535 ? (e.byte(lt.PUSHDATA2), e.bytes(vi.encode(r))) : (e.byte(lt.PUSHDATA4), e.bytes(Z.encode(r))), e.bytes(n); + r < dt.PUSHDATA1 ? e.byte(r) : r <= 255 ? (e.byte(dt.PUSHDATA1), e.byte(r)) : r <= 65535 ? (e.byte(dt.PUSHDATA2), e.bytes(ki.encode(r))) : (e.byte(dt.PUSHDATA4), e.bytes(X.encode(r))), e.bytes(n); } }, decodeStream: (e) => { const t = []; for (; !e.isEnd(); ) { const n = e.byte(); - if (lt.OP_0 < n && n <= lt.PUSHDATA4) { + if (dt.OP_0 < n && n <= dt.PUSHDATA4) { let r; - if (n < lt.PUSHDATA1) + if (n < dt.PUSHDATA1) r = n; - else if (n === lt.PUSHDATA1) - r = Se.decodeStream(e); - else if (n === lt.PUSHDATA2) - r = vi.decodeStream(e); - else if (n === lt.PUSHDATA4) - r = Z.decodeStream(e); + else if (n === dt.PUSHDATA1) + r = Te.decodeStream(e); + else if (n === dt.PUSHDATA2) + r = ki.decodeStream(e); + else if (n === dt.PUSHDATA4) + r = X.decodeStream(e); else throw new Error("Should be not possible"); t.push(e.bytes(r)); } else if (n === 0) t.push(0); - else if (lt.OP_1 <= n && n <= lt.OP_16) - t.push(n - (lt.OP_1 - 1)); + else if (dt.OP_1 <= n && n <= dt.OP_16) + t.push(n - (dt.OP_1 - 1)); else { - const r = dl[n]; + const r = bd[n]; if (r === void 0) throw new Error(`Unknown opcode=${n.toString(16)}`); t.push(r); @@ -3214,15 +3214,15 @@ const K = At({ } return t; } -}), ki = { +}), $i = { 253: [253, 2, 253n, 65535n], 254: [254, 4, 65536n, 4294967295n], 255: [255, 8, 4294967296n, 18446744073709551615n] -}, no = At({ +}, oo = It({ encodeStream: (e, t) => { if (typeof t == "number" && (t = BigInt(t)), 0n <= t && t <= 252n) return e.byte(Number(t)); - for (const [n, r, o, s] of Object.values(ki)) + for (const [n, r, o, s] of Object.values($i)) if (!(o > t || t > s)) { e.byte(n); for (let i = 0; i < r; i++) @@ -3235,7 +3235,7 @@ const K = At({ const t = e.byte(); if (t <= 252) return BigInt(t); - const [n, r, o] = ki[t]; + const [n, r, o] = $i[t]; let s = 0n; for (let i = 0; i < r; i++) s |= BigInt(e.byte()) << 8n * BigInt(i); @@ -3243,98 +3243,98 @@ const K = At({ throw e.err(`Wrong CompactSize(${8 * r})`); return s; } -}), Wt = Ie(no, eo.numberBigint), Mt = rt(no), Cn = vt(Wt, Mt), kr = (e) => vt(no, e), xa = pt({ +}), zt = ke(oo, ro.numberBigint), Mt = rt(oo), Pn = vt(zt, Mt), Ur = (e) => vt(oo, e), Ia = pt({ txid: rt(32, !0), // hash(prev_tx), - index: Z, + index: X, // output number of previous tx finalScriptSig: Mt, // btc merges input and output script, executes it. If ok = tx passes - sequence: Z + sequence: X // ? -}), Pe = pt({ amount: dr, script: Mt }), pl = pt({ - version: Ye, - segwitFlag: cl(new Uint8Array([0, 1])), - inputs: kr(xa), - outputs: kr(Pe), - witnesses: al("segwitFlag", vt("inputs/length", Cn)), +}), Ve = pt({ amount: gr, script: Mt }), xd = pt({ + version: Ze, + segwitFlag: pd(new Uint8Array([0, 1])), + inputs: Ur(Ia), + outputs: Ur(Ve), + witnesses: gd("segwitFlag", vt("inputs/length", Pn)), // < 500000000 Block number at which this transaction is unlocked // >= 500000000 UNIX timestamp at which this transaction is unlocked // Handled as part of PSBTv2 - lockTime: Z + lockTime: X }); -function gl(e) { +function Sd(e) { if (e.segwitFlag && e.witnesses && !e.witnesses.length) throw new Error("Segwit flag with empty witnesses array"); return e; } -const Qe = Tt(pl, gl), kn = pt({ - version: Ye, - inputs: kr(xa), - outputs: kr(Pe), - lockTime: Z -}), Fo = Tt(rt(null), (e) => an(e, kt.ecdsa)), Br = Tt(rt(32), (e) => an(e, kt.schnorr)), Bi = Tt(rt(null), (e) => { +const Je = At(xd, Sd), Bn = pt({ + version: Ze, + inputs: Ur(Ia), + outputs: Ur(Ve), + lockTime: X +}), Yo = At(rt(null), (e) => an(e, kt.ecdsa)), $r = At(rt(32), (e) => an(e, kt.schnorr)), Ri = At(rt(null), (e) => { if (e.length !== 64 && e.length !== 65) throw new Error("Schnorr signature should be 64 or 65 bytes long"); return e; -}), ro = pt({ - fingerprint: ol, - path: vt(null, Z) -}), Sa = pt({ - hashes: vt(Wt, rt(32)), - der: ro -}), wl = rt(78), yl = pt({ pubKey: Br, leafHash: rt(32) }), ml = pt({ - version: Se, +}), so = pt({ + fingerprint: dd, + path: vt(null, X) +}), ka = pt({ + hashes: vt(zt, rt(32)), + der: so +}), Td = rt(78), vd = pt({ pubKey: $r, leafHash: rt(32) }), Ad = pt({ + version: Te, // With parity :( internalKey: rt(32), merklePath: vt(null, rt(32)) -}), Zt = Tt(ml, (e) => { +}), Xt = At(Ad, (e) => { if (e.merklePath.length > 128) throw new Error("TaprootControlBlock: merklePath should be of length 0..128 (inclusive)"); return e; -}), bl = vt(null, pt({ - depth: Se, - version: Se, +}), Id = vt(null, pt({ + depth: Te, + version: Te, script: Mt -})), it = rt(null), Oi = rt(20), vn = rt(32), _s = { - unsignedTx: [0, !1, kn, [0], [0], !1], - xpub: [1, wl, ro, [], [0, 2], !1], - txVersion: [2, !1, Z, [2], [2], !1], - fallbackLocktime: [3, !1, Z, [], [2], !1], - inputCount: [4, !1, Wt, [2], [2], !1], - outputCount: [5, !1, Wt, [2], [2], !1], - txModifiable: [6, !1, Se, [], [2], !1], +})), it = rt(null), Ni = rt(20), vn = rt(32), Ks = { + unsignedTx: [0, !1, Bn, [0], [0], !1], + xpub: [1, Td, so, [], [0, 2], !1], + txVersion: [2, !1, X, [2], [2], !1], + fallbackLocktime: [3, !1, X, [], [2], !1], + inputCount: [4, !1, zt, [2], [2], !1], + outputCount: [5, !1, zt, [2], [2], !1], + txModifiable: [6, !1, Te, [], [2], !1], // TODO: bitfield - version: [251, !1, Z, [], [0, 2], !1], + version: [251, !1, X, [], [0, 2], !1], proprietary: [252, it, it, [], [0, 2], !1] -}, oo = { - nonWitnessUtxo: [0, !1, Qe, [], [0, 2], !1], - witnessUtxo: [1, !1, Pe, [], [0, 2], !1], - partialSig: [2, Fo, it, [], [0, 2], !1], - sighashType: [3, !1, Z, [], [0, 2], !1], +}, io = { + nonWitnessUtxo: [0, !1, Je, [], [0, 2], !1], + witnessUtxo: [1, !1, Ve, [], [0, 2], !1], + partialSig: [2, Yo, it, [], [0, 2], !1], + sighashType: [3, !1, X, [], [0, 2], !1], redeemScript: [4, !1, it, [], [0, 2], !1], witnessScript: [5, !1, it, [], [0, 2], !1], - bip32Derivation: [6, Fo, ro, [], [0, 2], !1], + bip32Derivation: [6, Yo, so, [], [0, 2], !1], finalScriptSig: [7, !1, it, [], [0, 2], !1], - finalScriptWitness: [8, !1, Cn, [], [0, 2], !1], + finalScriptWitness: [8, !1, Pn, [], [0, 2], !1], porCommitment: [9, !1, it, [], [0, 2], !1], - ripemd160: [10, Oi, it, [], [0, 2], !1], + ripemd160: [10, Ni, it, [], [0, 2], !1], sha256: [11, vn, it, [], [0, 2], !1], - hash160: [12, Oi, it, [], [0, 2], !1], + hash160: [12, Ni, it, [], [0, 2], !1], hash256: [13, vn, it, [], [0, 2], !1], txid: [14, !1, vn, [2], [2], !0], - index: [15, !1, Z, [2], [2], !0], - sequence: [16, !1, Z, [], [2], !0], - requiredTimeLocktime: [17, !1, Z, [], [2], !1], - requiredHeightLocktime: [18, !1, Z, [], [2], !1], - tapKeySig: [19, !1, Bi, [], [0, 2], !1], - tapScriptSig: [20, yl, Bi, [], [0, 2], !1], - tapLeafScript: [21, Zt, it, [], [0, 2], !1], - tapBip32Derivation: [22, vn, Sa, [], [0, 2], !1], - tapInternalKey: [23, !1, Br, [], [0, 2], !1], + index: [15, !1, X, [2], [2], !0], + sequence: [16, !1, X, [], [2], !0], + requiredTimeLocktime: [17, !1, X, [], [2], !1], + requiredHeightLocktime: [18, !1, X, [], [2], !1], + tapKeySig: [19, !1, Ri, [], [0, 2], !1], + tapScriptSig: [20, vd, Ri, [], [0, 2], !1], + tapLeafScript: [21, Xt, it, [], [0, 2], !1], + tapBip32Derivation: [22, vn, ka, [], [0, 2], !1], + tapInternalKey: [23, !1, $r, [], [0, 2], !1], tapMerkleRoot: [24, !1, vn, [], [0, 2], !1], proprietary: [252, it, it, [], [0, 2], !1] -}, El = [ +}, kd = [ "txid", "sequence", "index", @@ -3343,40 +3343,40 @@ const Qe = Tt(pl, gl), kn = pt({ "finalScriptSig", "finalScriptWitness", "unknown" -], xl = [ +], Bd = [ "partialSig", "finalScriptSig", "finalScriptWitness", "tapKeySig", "tapScriptSig" -], _n = { +], Vn = { redeemScript: [0, !1, it, [], [0, 2], !1], witnessScript: [1, !1, it, [], [0, 2], !1], - bip32Derivation: [2, Fo, ro, [], [0, 2], !1], - amount: [3, !1, nl, [2], [2], !0], + bip32Derivation: [2, Yo, so, [], [0, 2], !1], + amount: [3, !1, ud, [2], [2], !0], script: [4, !1, it, [2], [2], !0], - tapInternalKey: [5, !1, Br, [], [0, 2], !1], - tapTree: [6, !1, bl, [], [0, 2], !1], - tapBip32Derivation: [7, Br, Sa, [], [0, 2], !1], + tapInternalKey: [5, !1, $r, [], [0, 2], !1], + tapTree: [6, !1, Id, [], [0, 2], !1], + tapBip32Derivation: [7, $r, ka, [], [0, 2], !1], proprietary: [252, it, it, [], [0, 2], !1] -}, Sl = [], $i = vt(ua, pt({ +}, Od = [], Li = vt(pa, pt({ // := WHERE keylen = len(keytype)+len(keydata) - key: sl(Wt, pt({ type: Wt, key: rt(null) })), + key: ld(zt, pt({ type: zt, key: rt(null) })), // := - value: rt(Wt) + value: rt(zt) })); -function Wo(e) { +function Zo(e) { const [t, n, r, o, s, i] = e; return { type: t, kc: n, vc: r, reqInc: o, allowInc: s, silentIgnore: i }; } -pt({ type: Wt, key: rt(null) }); -function Ps(e) { +pt({ type: zt, key: rt(null) }); +function Fs(e) { const t = {}; for (const n in e) { const [r, o, s] = e[n]; t[r] = [n, o, s]; } - return At({ + return It({ encodeStream: (n, r) => { let o = []; for (const s in e) { @@ -3387,31 +3387,31 @@ function Ps(e) { if (!a) o.push({ key: { type: c, key: ot }, value: u.encode(i) }); else { - const f = i.map(([l, d]) => [ - a.encode(l), - u.encode(d) + const f = i.map(([d, l]) => [ + a.encode(d), + u.encode(l) ]); - f.sort((l, d) => Ir(l[0], d[0])); - for (const [l, d] of f) - o.push({ key: { key: l, type: c }, value: d }); + f.sort((d, l) => Or(d[0], l[0])); + for (const [d, l] of f) + o.push({ key: { key: d, type: c }, value: l }); } } if (r.unknown) { - r.unknown.sort((s, i) => Ir(s[0].key, i[0].key)); + r.unknown.sort((s, i) => Or(s[0].key, i[0].key)); for (const [s, i] of r.unknown) o.push({ key: s, value: i }); } - $i.encodeStream(n, o); + Li.encodeStream(n, o); }, decodeStream: (n) => { - const r = $i.decodeStream(n), o = {}, s = {}; + const r = Li.decodeStream(n), o = {}, s = {}; for (const i of r) { let c = "unknown", a = i.key.key, u = i.value; if (t[i.key.type]) { - const [f, l, d] = t[i.key.type]; - if (c = f, !l && a.length) - throw new Error(`PSBT: Non-empty key for ${c} (key=${$.encode(a)} value=${$.encode(u)}`); - if (a = l ? l.decode(a) : void 0, u = d.decode(u), !l) { + const [f, d, l] = t[i.key.type]; + if (c = f, !d && a.length) + throw new Error(`PSBT: Non-empty key for ${c} (key=${U.encode(a)} value=${U.encode(u)}`); + if (a = d ? d.decode(a) : void 0, u = l.decode(u), !d) { if (o[c]) throw new Error(`PSBT: Same keys: ${c} (key=${a} value=${u})`); o[c] = u, s[c] = !0; @@ -3427,7 +3427,7 @@ function Ps(e) { } }); } -const Hs = Tt(Ps(oo), (e) => { +const Ws = At(Fs(io), (e) => { if (e.finalScriptWitness && !e.finalScriptWitness.length) throw new Error("validateInput: empty finalScriptWitness"); if (e.partialSig && !e.partialSig.length) @@ -3450,12 +3450,12 @@ const Hs = Tt(Ps(oo), (e) => { throw new Error("validateInput: tapLeafScript version has parity bit!"); } return e; -}), Vs = Tt(Ps(_n), (e) => { +}), zs = At(Fs(Vn), (e) => { if (e.bip32Derivation) for (const [t] of e.bip32Derivation) an(t, kt.ecdsa); return e; -}), va = Tt(Ps(_s), (e) => { +}), Ba = At(Fs(Ks), (e) => { if ((e.version || 0) === 0) { if (!e.unsignedTx) throw new Error("PSBTv0: missing unsignedTx"); @@ -3464,43 +3464,43 @@ const Hs = Tt(Ps(oo), (e) => { throw new Error("PSBTv0: input scriptSig found in unsignedTx"); } return e; -}), vl = pt({ - magic: Us($s(new Uint8Array([255])), "psbt"), - global: va, - inputs: vt("global/unsignedTx/inputs/length", Hs), - outputs: vt(null, Vs) -}), Tl = pt({ - magic: Us($s(new Uint8Array([255])), "psbt"), - global: va, - inputs: vt("global/inputCount", Hs), - outputs: vt("global/outputCount", Vs) +}), Ud = pt({ + magic: Ps(Cs(new Uint8Array([255])), "psbt"), + global: Ba, + inputs: vt("global/unsignedTx/inputs/length", Ws), + outputs: vt(null, zs) +}), $d = pt({ + magic: Ps(Cs(new Uint8Array([255])), "psbt"), + global: Ba, + inputs: vt("global/inputCount", Ws), + outputs: vt("global/outputCount", zs) }); pt({ - magic: Us($s(new Uint8Array([255])), "psbt"), - items: vt(null, Ie(vt(ua, ul([il(Wt), rt(no)])), eo.dict())) + magic: Ps(Cs(new Uint8Array([255])), "psbt"), + items: vt(null, ke(vt(pa, wd([hd(zt), rt(oo)])), ro.dict())) }); -function mo(e, t, n) { +function xo(e, t, n) { for (const r in n) { if (r === "unknown" || !t[r]) continue; - const { allowInc: o } = Wo(t[r]); + const { allowInc: o } = Zo(t[r]); if (!o.includes(e)) throw new Error(`PSBTv${e}: field ${r} is not allowed`); } for (const r in t) { - const { reqInc: o } = Wo(t[r]); + const { reqInc: o } = Zo(t[r]); if (o.includes(e) && n[r] === void 0) throw new Error(`PSBTv${e}: missing required field ${r}`); } } -function Ui(e, t, n) { +function _i(e, t, n) { const r = {}; for (const o in n) { const s = o; if (s !== "unknown") { if (!t[s]) continue; - const { allowInc: i, silentIgnore: c } = Wo(t[s]); + const { allowInc: i, silentIgnore: c } = Zo(t[s]); if (!i.includes(e)) { if (c) continue; @@ -3511,13 +3511,13 @@ function Ui(e, t, n) { } return r; } -function Ta(e) { +function Oa(e) { const t = e && e.global && e.global.version || 0; - mo(t, _s, e.global); + xo(t, Ks, e.global); for (const i of e.inputs) - mo(t, oo, i); + xo(t, io, i); for (const i of e.outputs) - mo(t, _n, i); + xo(t, Vn, i); const n = t ? e.global.inputCount : e.global.unsignedTx.inputs.length; if (e.inputs.length < n) throw new Error("Not enough inputs"); @@ -3532,16 +3532,16 @@ function Ta(e) { throw new Error(`Unexpected outputs left in tx=${s}`); return e; } -function zo(e, t, n, r, o) { +function Xo(e, t, n, r, o) { const s = { ...n, ...t }; for (const i in e) { - const c = i, [a, u, f] = e[c], l = r && !r.includes(i); + const c = i, [a, u, f] = e[c], d = r && !r.includes(i); if (t[i] === void 0 && i in t) { - if (l) + if (d) throw new Error(`Cannot remove signed field=${i}`); delete s[i]; } else if (u) { - const d = n && n[i] ? n[i] : []; + const l = n && n[i] ? n[i] : []; let h = t[c]; if (h) { if (!Array.isArray(h)) @@ -3550,37 +3550,37 @@ function zo(e, t, n, r, o) { if (y.length !== 2) throw new Error(`keyMap(${i}): KV pairs should be [k, v][]`); return [ - typeof y[0] == "string" ? u.decode($.decode(y[0])) : y[0], - typeof y[1] == "string" ? f.decode($.decode(y[1])) : y[1] + typeof y[0] == "string" ? u.decode(U.decode(y[0])) : y[0], + typeof y[1] == "string" ? f.decode(U.decode(y[1])) : y[1] ]; }); - const w = {}, g = (y, S, v) => { + const w = {}, g = (y, S, T) => { if (w[y] === void 0) { - w[y] = [S, v]; + w[y] = [S, T]; return; } - const O = $.encode(f.encode(w[y][1])), N = $.encode(f.encode(v)); - if (O !== N) - throw new Error(`keyMap(${c}): same key=${y} oldVal=${O} newVal=${N}`); + const k = U.encode(f.encode(w[y][1])), N = U.encode(f.encode(T)); + if (k !== N) + throw new Error(`keyMap(${c}): same key=${y} oldVal=${k} newVal=${N}`); }; - for (const [y, S] of d) { - const v = $.encode(u.encode(y)); - g(v, y, S); + for (const [y, S] of l) { + const T = U.encode(u.encode(y)); + g(T, y, S); } for (const [y, S] of h) { - const v = $.encode(u.encode(y)); + const T = U.encode(u.encode(y)); if (S === void 0) { - if (l) + if (d) throw new Error(`Cannot remove signed field=${c}/${y}`); - delete w[v]; + delete w[T]; } else - g(v, y, S); + g(T, y, S); } s[c] = Object.values(w); } } else if (typeof s[i] == "string") - s[i] = f.decode($.decode(s[i])); - else if (l && i in t && n && n[i] !== void 0 && !ct(f.encode(t[i]), f.encode(n[i]))) + s[i] = f.decode(U.decode(s[i])); + else if (d && i in t && n && n[i] !== void 0 && !ct(f.encode(t[i]), f.encode(n[i]))) throw new Error(`Cannot change signed field=${i}`); } for (const i in s) @@ -3591,54 +3591,54 @@ function zo(e, t, n, r, o) { } return s; } -const Ri = Tt(vl, Ta), Ni = Tt(Tl, Ta), Al = { +const Ci = At(Ud, Oa), Pi = At($d, Oa), Rd = { encode(e) { - if (!(e.length !== 2 || e[0] !== 1 || !nt(e[1]) || $.encode(e[1]) !== "4e73")) + if (!(e.length !== 2 || e[0] !== 1 || !nt(e[1]) || U.encode(e[1]) !== "4e73")) return { type: "p2a", script: K.encode(e) }; }, decode: (e) => { if (e.type === "p2a") - return [1, $.decode("4e73")]; + return [1, U.decode("4e73")]; } }; -function Ze(e, t) { +function Xe(e, t) { try { return an(e, t), !0; } catch { return !1; } } -const Il = { +const Nd = { encode(e) { - if (!(e.length !== 2 || !nt(e[0]) || !Ze(e[0], kt.ecdsa) || e[1] !== "CHECKSIG")) + if (!(e.length !== 2 || !nt(e[0]) || !Xe(e[0], kt.ecdsa) || e[1] !== "CHECKSIG")) return { type: "pk", pubkey: e[0] }; }, decode: (e) => e.type === "pk" ? [e.pubkey, "CHECKSIG"] : void 0 -}, kl = { +}, Ld = { encode(e) { if (!(e.length !== 5 || e[0] !== "DUP" || e[1] !== "HASH160" || !nt(e[2])) && !(e[3] !== "EQUALVERIFY" || e[4] !== "CHECKSIG")) return { type: "pkh", hash: e[2] }; }, decode: (e) => e.type === "pkh" ? ["DUP", "HASH160", e.hash, "EQUALVERIFY", "CHECKSIG"] : void 0 -}, Bl = { +}, _d = { encode(e) { if (!(e.length !== 3 || e[0] !== "HASH160" || !nt(e[1]) || e[2] !== "EQUAL")) return { type: "sh", hash: e[1] }; }, decode: (e) => e.type === "sh" ? ["HASH160", e.hash, "EQUAL"] : void 0 -}, Ol = { +}, Cd = { encode(e) { if (!(e.length !== 2 || e[0] !== 0 || !nt(e[1])) && e[1].length === 32) return { type: "wsh", hash: e[1] }; }, decode: (e) => e.type === "wsh" ? [0, e.hash] : void 0 -}, $l = { +}, Pd = { encode(e) { if (!(e.length !== 2 || e[0] !== 0 || !nt(e[1])) && e[1].length === 20) return { type: "wpkh", hash: e[1] }; }, decode: (e) => e.type === "wpkh" ? [0, e.hash] : void 0 -}, Ul = { +}, Vd = { encode(e) { const t = e.length - 1; if (e[t] !== "CHECKMULTISIG") @@ -3656,13 +3656,13 @@ const Il = { }, // checkmultisig(n, ..pubkeys, m) decode: (e) => e.type === "ms" ? [e.m, ...e.pubkeys, e.pubkeys.length, "CHECKMULTISIG"] : void 0 -}, Rl = { +}, Hd = { encode(e) { if (!(e.length !== 2 || e[0] !== 1 || !nt(e[1]))) return { type: "tr", pubkey: e[1] }; }, decode: (e) => e.type === "tr" ? [1, e.pubkey] : void 0 -}, Nl = { +}, Dd = { encode(e) { const t = e.length - 1; if (e[t] !== "CHECKSIG") @@ -3689,12 +3689,12 @@ const Il = { t.push(e.pubkeys[n], "CHECKSIGVERIFY"); return t.push(e.pubkeys[e.pubkeys.length - 1], "CHECKSIG"), t; } -}, Ll = { +}, Md = { encode(e) { const t = e.length - 1; if (e[t] !== "NUMEQUAL" || e[1] !== "CHECKSIG") return; - const n = [], r = hl(e[t - 1]); + const n = [], r = Ed(e[t - 1]); if (typeof r == "number") { for (let o = 0; o < t - 1; o++) { const s = e[o]; @@ -3718,45 +3718,45 @@ const Il = { t.push(e.pubkeys[n], "CHECKSIGADD"); return t.push(e.m, "NUMEQUAL"), t; } -}, Cl = { +}, Kd = { encode(e) { return { type: "unknown", script: K.encode(e) }; }, decode: (e) => e.type === "unknown" ? K.decode(e.script) : void 0 -}, _l = [ - Al, - Il, - kl, - Bl, - Ol, - $l, - Ul, - Rl, - Nl, - Ll, - Cl -], Pl = Ie(K, eo.match(_l)), ut = Tt(Pl, (e) => { - if (e.type === "pk" && !Ze(e.pubkey, kt.ecdsa)) +}, Fd = [ + Rd, + Nd, + Ld, + _d, + Cd, + Pd, + Vd, + Hd, + Dd, + Md, + Kd +], Wd = ke(K, ro.match(Fd)), ut = At(Wd, (e) => { + if (e.type === "pk" && !Xe(e.pubkey, kt.ecdsa)) throw new Error("OutScript/pk: wrong key"); if ((e.type === "pkh" || e.type === "sh" || e.type === "wpkh") && (!nt(e.hash) || e.hash.length !== 20)) throw new Error(`OutScript/${e.type}: wrong hash`); if (e.type === "wsh" && (!nt(e.hash) || e.hash.length !== 32)) throw new Error("OutScript/wsh: wrong hash"); - if (e.type === "tr" && (!nt(e.pubkey) || !Ze(e.pubkey, kt.schnorr))) + if (e.type === "tr" && (!nt(e.pubkey) || !Xe(e.pubkey, kt.schnorr))) throw new Error("OutScript/tr: wrong taproot public key"); if ((e.type === "ms" || e.type === "tr_ns" || e.type === "tr_ms") && !Array.isArray(e.pubkeys)) throw new Error("OutScript/multisig: wrong pubkeys array"); if (e.type === "ms") { const t = e.pubkeys.length; for (const n of e.pubkeys) - if (!Ze(n, kt.ecdsa)) + if (!Xe(n, kt.ecdsa)) throw new Error("OutScript/multisig: wrong pubkey"); if (e.m <= 0 || t > 16 || e.m > t) throw new Error("OutScript/multisig: invalid params"); } if (e.type === "tr_ns" || e.type === "tr_ms") { for (const t of e.pubkeys) - if (!Ze(t, kt.schnorr)) + if (!Xe(t, kt.schnorr)) throw new Error(`OutScript/${e.type}: wrong pubkey`); } if (e.type === "tr_ms") { @@ -3766,8 +3766,8 @@ const Il = { } return e; }); -function Li(e, t) { - if (!ct(e.hash, wt(t))) +function Vi(e, t) { + if (!ct(e.hash, yt(t))) throw new Error("checkScript: wsh wrong witnessScript hash"); const n = ut.decode(t); if (n.type === "tr" || n.type === "tr_ns" || n.type === "tr_ms") @@ -3775,13 +3775,13 @@ function Li(e, t) { if (n.type === "wpkh" || n.type === "sh") throw new Error(`checkScript: P2${n.type} cannot be wrapped in P2WSH`); } -function Aa(e, t, n) { +function Ua(e, t, n) { if (e) { const r = ut.decode(e); if (r.type === "tr_ns" || r.type === "tr_ms" || r.type === "ms" || r.type == "pk") throw new Error(`checkScript: non-wrapped ${r.type}`); if (r.type === "sh" && t) { - if (!ct(r.hash, ya(t))) + if (!ct(r.hash, Sa(t))) throw new Error("checkScript: sh wrong redeemScript hash"); const o = ut.decode(t); if (o.type === "tr" || o.type === "tr_ns" || o.type === "tr_ms") @@ -3789,23 +3789,23 @@ function Aa(e, t, n) { if (o.type === "sh") throw new Error("checkScript: P2SH cannot be wrapped in P2SH"); } - r.type === "wsh" && n && Li(r, n); + r.type === "wsh" && n && Vi(r, n); } if (t) { const r = ut.decode(t); - r.type === "wsh" && n && Li(r, n); + r.type === "wsh" && n && Vi(r, n); } } -function Hl(e) { +function zd(e) { const t = {}; for (const n of e) { - const r = $.encode(n); + const r = U.encode(n); if (t[r]) - throw new Error(`Multisig: non-uniq pubkey: ${e.map($.encode)}`); + throw new Error(`Multisig: non-uniq pubkey: ${e.map(U.encode)}`); t[r] = !0; } } -function Vl(e, t, n = !1, r) { +function Gd(e, t, n = !1, r) { const o = ut.decode(e); if (o.type === "unknown" && n) return; @@ -3814,13 +3814,13 @@ function Vl(e, t, n = !1, r) { const s = o; if (!n && s.pubkeys) for (const i of s.pubkeys) { - if (ct(i, Ls)) + if (ct(i, Ds)) throw new Error("Unspendable taproot key in leaf script"); if (ct(i, t)) throw new Error("Using P2TR with leaf script with same key as internal key is not supported"); } } -function Ia(e) { +function $a(e) { const t = Array.from(e); for (; t.length >= 2; ) { t.sort((i, c) => (c.weight || 1) - (i.weight || 1)); @@ -3835,7 +3835,7 @@ function Ia(e) { const n = t[0]; return n?.childs || n; } -function Go(e, t = []) { +function Qo(e, t = []) { if (!e) throw new Error("taprootAddPath: empty tree"); if (e.type === "leaf") @@ -3846,80 +3846,80 @@ function Go(e, t = []) { ...e, path: t, // Left element has right hash in path and otherwise - left: Go(e.left, [e.right.hash, ...t]), - right: Go(e.right, [e.left.hash, ...t]) + left: Qo(e.left, [e.right.hash, ...t]), + right: Qo(e.right, [e.left.hash, ...t]) }; } -function qo(e) { +function Jo(e) { if (!e) throw new Error("taprootAddPath: empty tree"); if (e.type === "leaf") return [e]; if (e.type !== "branch") throw new Error(`taprootWalkTree: wrong type=${e}`); - return [...qo(e.left), ...qo(e.right)]; + return [...Jo(e.left), ...Jo(e.right)]; } -function jo(e, t, n = !1, r) { +function ts(e, t, n = !1, r) { if (!e) throw new Error("taprootHashTree: empty tree"); if (Array.isArray(e) && e.length === 1 && (e = e[0]), !Array.isArray(e)) { const { leafVersion: a, script: u } = e; if (e.tapLeafScript || e.tapMerkleRoot && !ct(e.tapMerkleRoot, ot)) throw new Error("P2TR: tapRoot leafScript cannot have tree"); - const f = typeof u == "string" ? $.decode(u) : u; + const f = typeof u == "string" ? U.decode(u) : u; if (!nt(f)) throw new Error(`checkScript: wrong script type=${f}`); - return Vl(f, t, n), { + return Gd(f, t, n), { type: "leaf", version: a, script: f, - hash: On(f, a) + hash: Un(f, a) }; } - if (e.length !== 2 && (e = Ia(e)), e.length !== 2) + if (e.length !== 2 && (e = $a(e)), e.length !== 2) throw new Error("hashTree: non binary tree!"); - const o = jo(e[0], t, n), s = jo(e[1], t, n); + const o = ts(e[0], t, n), s = ts(e[1], t, n); let [i, c] = [o.hash, s.hash]; - return Ir(c, i) === -1 && ([i, c] = [c, i]), { type: "branch", left: o, right: s, hash: Ns("TapBranch", i, c) }; + return Or(c, i) === -1 && ([i, c] = [c, i]), { type: "branch", left: o, right: s, hash: Hs("TapBranch", i, c) }; } -const Pn = 192, On = (e, t = Pn) => Ns("TapLeaf", new Uint8Array([t]), Mt.encode(e)); -function Dl(e, t, n = un, r = !1, o) { +const Hn = 192, Un = (e, t = Hn) => Hs("TapLeaf", new Uint8Array([t]), Mt.encode(e)); +function qd(e, t, n = un, r = !1, o) { if (!e && !t) throw new Error("p2tr: should have pubKey or scriptTree (or both)"); - const s = typeof e == "string" ? $.decode(e) : e || Ls; - if (!Ze(s, kt.schnorr)) + const s = typeof e == "string" ? U.decode(e) : e || Ds; + if (!Xe(s, kt.schnorr)) throw new Error("p2tr: non-schnorr pubkey"); if (t) { - let i = Go(jo(t, s, r)); - const c = i.hash, [a, u] = Ko(s, c), f = qo(i).map((l) => ({ - ...l, - controlBlock: Zt.encode({ - version: (l.version || Pn) + u, + let i = Qo(ts(t, s, r)); + const c = i.hash, [a, u] = jo(s, c), f = Jo(i).map((d) => ({ + ...d, + controlBlock: Xt.encode({ + version: (d.version || Hn) + u, internalKey: s, - merklePath: l.path + merklePath: d.path }) })); return { type: "tr", script: ut.encode({ type: "tr", pubkey: a }), - address: Me(n).encode({ type: "tr", pubkey: a }), + address: Ke(n).encode({ type: "tr", pubkey: a }), // For tests tweakedPubkey: a, // PSBT stuff tapInternalKey: s, leaves: f, - tapLeafScript: f.map((l) => [ - Zt.decode(l.controlBlock), - Ee(l.script, new Uint8Array([l.version || Pn])) + tapLeafScript: f.map((d) => [ + Xt.decode(d.controlBlock), + Ee(d.script, new Uint8Array([d.version || Hn])) ]), tapMerkleRoot: c }; } else { - const i = Ko(s, ot)[0]; + const i = jo(s, ot)[0]; return { type: "tr", script: ut.encode({ type: "tr", pubkey: i }), - address: Me(n).encode({ type: "tr", pubkey: i }), + address: Ke(n).encode({ type: "tr", pubkey: i }), // For tests tweakedPubkey: i, // PSBT stuff @@ -3927,14 +3927,14 @@ function Dl(e, t, n = un, r = !1, o) { }; } } -function Ml(e, t, n = !1) { - return n || Hl(t), { +function jd(e, t, n = !1) { + return n || zd(t), { type: "tr_ms", script: ut.encode({ type: "tr_ms", pubkeys: t, m: e }) }; } -const ka = Wf(wt); -function Ba(e, t) { +const Ra = Xf(yt); +function Na(e, t) { if (t.length < 2 || t.length > 40) throw new Error("Witness: invalid length"); if (e > 16) @@ -3942,28 +3942,28 @@ function Ba(e, t) { if (e === 0 && !(t.length === 20 || t.length === 32)) throw new Error("Witness: invalid length for version"); } -function bo(e, t, n = un) { - Ba(e, t); - const r = e === 0 ? Vo : je; +function So(e, t, n = un) { + Na(e, t); + const r = e === 0 ? zo : Ye; return r.encode(n.bech32, [e].concat(r.toWords(t))); } -function Ci(e, t) { - return ka.encode(Ee(Uint8Array.from(t), e)); +function Hi(e, t) { + return Ra.encode(Ee(Uint8Array.from(t), e)); } -function Me(e = un) { +function Ke(e = un) { return { encode(t) { const { type: n } = t; if (n === "wpkh") - return bo(0, t.hash, e); + return So(0, t.hash, e); if (n === "wsh") - return bo(0, t.hash, e); + return So(0, t.hash, e); if (n === "tr") - return bo(1, t.pubkey, e); + return So(1, t.pubkey, e); if (n === "pkh") - return Ci(t.hash, [e.pubKeyHash]); + return Hi(t.hash, [e.pubKeyHash]); if (n === "sh") - return Ci(t.hash, [e.scriptHash]); + return Hi(t.hash, [e.scriptHash]); throw new Error(`Unknown address type=${n}`); }, decode(t) { @@ -3972,16 +3972,16 @@ function Me(e = un) { if (e.bech32 && t.toLowerCase().startsWith(`${e.bech32}1`)) { let r; try { - if (r = Vo.decode(t), r.words[0] !== 0) + if (r = zo.decode(t), r.words[0] !== 0) throw new Error(`bech32: wrong version=${r.words[0]}`); } catch { - if (r = je.decode(t), r.words[0] === 0) + if (r = Ye.decode(t), r.words[0] === 0) throw new Error(`bech32m: wrong version=${r.words[0]}`); } if (r.prefix !== e.bech32) throw new Error(`wrong bech32 prefix=${r.prefix}`); - const [o, ...s] = r.words, i = Vo.fromWords(s); - if (Ba(o, i), o === 0 && i.length === 32) + const [o, ...s] = r.words, i = zo.fromWords(s); + if (Na(o, i), o === 0 && i.length === 32) return { type: "wsh", hash: i }; if (o === 0 && i.length === 20) return { type: "wpkh", hash: i }; @@ -3989,7 +3989,7 @@ function Me(e = un) { return { type: "tr", pubkey: i }; throw new Error("Unknown witness program"); } - const n = ka.decode(t); + const n = Ra.decode(t); if (n.length !== 21) throw new Error("Invalid base58 address"); if (n[0] === e.pubKeyHash) @@ -4003,15 +4003,15 @@ function Me(e = un) { } }; } -const er = new Uint8Array(32), Kl = { +const nr = new Uint8Array(32), Yd = { amount: 0xffffffffffffffffn, script: ot -}, Fl = (e) => Math.ceil(e / 4), Wl = 8, zl = 2, Re = 0, Ds = 4294967295; -eo.decimal(Wl); +}, Zd = (e) => Math.ceil(e / 4), Xd = 8, Qd = 2, Ne = 0, Gs = 4294967295; +ro.decimal(Xd); const $n = (e, t) => e === void 0 ? t : e; -function Or(e) { +function Rr(e) { if (Array.isArray(e)) - return e.map((t) => Or(t)); + return e.map((t) => Rr(t)); if (nt(e)) return Uint8Array.from(e); if (["number", "bigint", "boolean", "string", "undefined"].includes(typeof e)) @@ -4019,7 +4019,7 @@ function Or(e) { if (e === null) return e; if (typeof e == "object") - return Object.fromEntries(Object.entries(e).map(([t, n]) => [t, Or(n)])); + return Object.fromEntries(Object.entries(e).map(([t, n]) => [t, Rr(n)])); throw new Error(`cloneDeep: unknown type=${e} (${typeof e})`); } const j = { @@ -4028,7 +4028,7 @@ const j = { NONE: 2, SINGLE: 3, ANYONECANPAY: 128 -}, Ke = { +}, Fe = { DEFAULT: j.DEFAULT, ALL: j.ALL, NONE: j.NONE, @@ -4037,38 +4037,38 @@ const j = { ALL_ANYONECANPAY: j.ALL | j.ANYONECANPAY, NONE_ANYONECANPAY: j.NONE | j.ANYONECANPAY, SINGLE_ANYONECANPAY: j.SINGLE | j.ANYONECANPAY -}, Gl = Ea(Ke); -function ql(e, t, n, r = ot) { - return ct(n, t) && (e = ll(e, r), t = Rs(e)), { privKey: e, pubKey: t }; +}, Jd = Aa(Fe); +function tl(e, t, n, r = ot) { + return ct(n, t) && (e = md(e, r), t = Vs(e)), { privKey: e, pubKey: t }; } -function Ne(e) { +function Le(e) { if (e.script === void 0 || e.amount === void 0) throw new Error("Transaction/output: script and amount required"); return { script: e.script, amount: e.amount }; } -function Tn(e) { +function An(e) { if (e.txid === void 0 || e.index === void 0) throw new Error("Transaction/input: txid and index required"); return { txid: e.txid, index: e.index, - sequence: $n(e.sequence, Ds), + sequence: $n(e.sequence, Gs), finalScriptSig: $n(e.finalScriptSig, ot) }; } -function Eo(e) { +function To(e) { for (const t in e) { const n = t; - El.includes(n) || delete e[n]; + kd.includes(n) || delete e[n]; } } -const xo = pt({ txid: rt(32, !0), index: Z }); -function jl(e) { - if (typeof e != "number" || typeof Gl[e] != "string") +const vo = pt({ txid: rt(32, !0), index: X }); +function el(e) { + if (typeof e != "number" || typeof Jd[e] != "string") throw new Error(`Invalid SigHash=${e}`); return e; } -function _i(e) { +function Di(e) { const t = e & 31; return { isAny: !!(e & j.ANYONECANPAY), @@ -4076,19 +4076,19 @@ function _i(e) { isSingle: t === j.SINGLE }; } -function Yl(e) { +function nl(e) { if (e !== void 0 && {}.toString.call(e) !== "[object Object]") throw new Error(`Wrong object type for transaction options: ${e}`); const t = { ...e, // Defaults - version: $n(e.version, zl), + version: $n(e.version, Qd), lockTime: $n(e.lockTime, 0), PSBTVersion: $n(e.PSBTVersion, 0) }; if (typeof t.allowUnknowInput < "u" && (e.allowUnknownInputs = t.allowUnknowInput), typeof t.allowUnknowOutput < "u" && (e.allowUnknownOutputs = t.allowUnknowOutput), typeof t.lockTime != "number") throw new Error("Transaction lock time should be number"); - if (Z.encode(t.lockTime), t.PSBTVersion !== 0 && t.PSBTVersion !== 2) + if (X.encode(t.lockTime), t.PSBTVersion !== 0 && t.PSBTVersion !== 2) throw new Error(`Unknown PSBT version ${t.PSBTVersion}`); for (const n of [ "allowUnknownVersion", @@ -4118,7 +4118,7 @@ function Yl(e) { } return Object.freeze(t); } -function Pi(e) { +function Mi(e) { if (e.nonWitnessUtxo && e.index !== void 0) { const t = e.nonWitnessUtxo.outputs.length - 1; if (e.index > t) @@ -4129,18 +4129,18 @@ function Pi(e) { if (e.txid) { if (e.nonWitnessUtxo.outputs.length - 1 < e.index) throw new Error("nonWitnessUtxo: incorect output index"); - const o = ue.fromRaw(Qe.encode(e.nonWitnessUtxo), { + const o = Kt.fromRaw(Je.encode(e.nonWitnessUtxo), { allowUnknownOutputs: !0, disableScriptCheck: !0, allowUnknownInputs: !0 - }), s = $.encode(e.txid); + }), s = U.encode(e.txid); if (o.isFinal && o.id !== s) throw new Error(`nonWitnessUtxo: wrong txid, exp=${s} got=${o.id}`); } } return e; } -function hr(e) { +function wr(e) { if (e.nonWitnessUtxo) { if (e.index === void 0) throw new Error("Unknown input index"); @@ -4151,17 +4151,17 @@ function hr(e) { throw new Error("Cannot find previous output info"); } } -function Hi(e, t, n, r = !1, o = !1) { +function Ki(e, t, n, r = !1, o = !1) { let { nonWitnessUtxo: s, txid: i } = e; - typeof s == "string" && (s = $.decode(s)), nt(s) && (s = Qe.decode(s)), !("nonWitnessUtxo" in e) && s === void 0 && (s = t?.nonWitnessUtxo), typeof i == "string" && (i = $.decode(i)), i === void 0 && (i = t?.txid); + typeof s == "string" && (s = U.decode(s)), nt(s) && (s = Je.decode(s)), !("nonWitnessUtxo" in e) && s === void 0 && (s = t?.nonWitnessUtxo), typeof i == "string" && (i = U.decode(i)), i === void 0 && (i = t?.txid); let c = { ...t, ...e, nonWitnessUtxo: s, txid: i }; - !("nonWitnessUtxo" in e) && c.nonWitnessUtxo === void 0 && delete c.nonWitnessUtxo, c.sequence === void 0 && (c.sequence = Ds), c.tapMerkleRoot === null && delete c.tapMerkleRoot, c = zo(oo, c, t, n, o), Hs.encode(c); + !("nonWitnessUtxo" in e) && c.nonWitnessUtxo === void 0 && delete c.nonWitnessUtxo, c.sequence === void 0 && (c.sequence = Gs), c.tapMerkleRoot === null && delete c.tapMerkleRoot, c = Xo(io, c, t, n, o), Ws.encode(c); let a; - return c.nonWitnessUtxo && c.index !== void 0 ? a = c.nonWitnessUtxo.outputs[c.index] : c.witnessUtxo && (a = c.witnessUtxo), a && !r && Aa(a && a.script, c.redeemScript, c.witnessScript), c; + return c.nonWitnessUtxo && c.index !== void 0 ? a = c.nonWitnessUtxo.outputs[c.index] : c.witnessUtxo && (a = c.witnessUtxo), a && !r && Ua(a && a.script, c.redeemScript, c.witnessScript), c; } -function Vi(e, t = !1) { +function Fi(e, t = !1) { let n = "legacy", r = j.ALL; - const o = hr(e), s = ut.decode(o.script); + const o = wr(e), s = ut.decode(o.script); let i = s.type, c = s; const a = [s]; if (s.type === "tr") @@ -4177,19 +4177,19 @@ function Vi(e, t = !1) { if ((s.type === "wpkh" || s.type === "wsh") && (n = "segwit"), s.type === "sh") { if (!e.redeemScript) throw new Error("inputType: sh without redeemScript"); - let d = ut.decode(e.redeemScript); - (d.type === "wpkh" || d.type === "wsh") && (n = "segwit"), a.push(d), c = d, i += `-${d.type}`; + let l = ut.decode(e.redeemScript); + (l.type === "wpkh" || l.type === "wsh") && (n = "segwit"), a.push(l), c = l, i += `-${l.type}`; } if (c.type === "wsh") { if (!e.witnessScript) throw new Error("inputType: wsh without witnessScript"); - let d = ut.decode(e.witnessScript); - d.type === "wsh" && (n = "segwit"), a.push(d), c = d, i += `-${d.type}`; + let l = ut.decode(e.witnessScript); + l.type === "wsh" && (n = "segwit"), a.push(l), c = l, i += `-${l.type}`; } const u = a[a.length - 1]; if (u.type === "sh" || u.type === "wsh") throw new Error("inputType: sh/wsh cannot be terminal type"); - const f = ut.encode(u), l = { + const f = ut.encode(u), d = { type: i, txType: n, last: u, @@ -4199,10 +4199,10 @@ function Vi(e, t = !1) { }; if (n === "legacy" && !t && !e.nonWitnessUtxo) throw new Error("Transaction/sign: legacy input without nonWitnessUtxo, can result in attack that forces paying higher fees. Pass allowLegacyWitnessUtxo=true, if you sure"); - return l; + return d; } } -let ue = class pr { +let Kt = class yr { global = {}; inputs = []; // use getInput() @@ -4210,12 +4210,12 @@ let ue = class pr { // use getOutput() opts; constructor(t = {}) { - const n = this.opts = Yl(t); - n.lockTime !== Re && (this.global.fallbackLocktime = n.lockTime), this.global.txVersion = n.version; + const n = this.opts = nl(t); + n.lockTime !== Ne && (this.global.fallbackLocktime = n.lockTime), this.global.txVersion = n.version; } // Import static fromRaw(t, n = {}) { - const r = Qe.decode(t), o = new pr({ ...n, version: r.version, lockTime: r.lockTime }); + const r = Je.decode(t), o = new yr({ ...n, version: r.version, lockTime: r.lockTime }); for (const s of r.outputs) o.addOutput(s); if (o.outputs = r.outputs, o.inputs = r.inputs, r.witnesses) @@ -4227,45 +4227,45 @@ let ue = class pr { static fromPSBT(t, n = {}) { let r; try { - r = Ri.decode(t); - } catch (l) { + r = Ci.decode(t); + } catch (d) { try { - r = Ni.decode(t); + r = Pi.decode(t); } catch { - throw l; + throw d; } } const o = r.global.version || 0; if (o !== 0 && o !== 2) throw new Error(`Wrong PSBT version=${o}`); - const s = r.global.unsignedTx, i = o === 0 ? s?.version : r.global.txVersion, c = o === 0 ? s?.lockTime : r.global.fallbackLocktime, a = new pr({ ...n, version: i, lockTime: c, PSBTVersion: o }), u = o === 0 ? s?.inputs.length : r.global.inputCount; - a.inputs = r.inputs.slice(0, u).map((l, d) => Pi({ + const s = r.global.unsignedTx, i = o === 0 ? s?.version : r.global.txVersion, c = o === 0 ? s?.lockTime : r.global.fallbackLocktime, a = new yr({ ...n, version: i, lockTime: c, PSBTVersion: o }), u = o === 0 ? s?.inputs.length : r.global.inputCount; + a.inputs = r.inputs.slice(0, u).map((d, l) => Mi({ finalScriptSig: ot, - ...r.global.unsignedTx?.inputs[d], - ...l + ...r.global.unsignedTx?.inputs[l], + ...d })); const f = o === 0 ? s?.outputs.length : r.global.outputCount; - return a.outputs = r.outputs.slice(0, f).map((l, d) => ({ - ...l, - ...r.global.unsignedTx?.outputs[d] - })), a.global = { ...r.global, txVersion: i }, c !== Re && (a.global.fallbackLocktime = c), a; + return a.outputs = r.outputs.slice(0, f).map((d, l) => ({ + ...d, + ...r.global.unsignedTx?.outputs[l] + })), a.global = { ...r.global, txVersion: i }, c !== Ne && (a.global.fallbackLocktime = c), a; } toPSBT(t = this.opts.PSBTVersion) { if (t !== 0 && t !== 2) throw new Error(`Wrong PSBT version=${t}`); - const n = this.inputs.map((s) => Pi(Ui(t, oo, s))); + const n = this.inputs.map((s) => Mi(_i(t, io, s))); for (const s of n) s.partialSig && !s.partialSig.length && delete s.partialSig, s.finalScriptSig && !s.finalScriptSig.length && delete s.finalScriptSig, s.finalScriptWitness && !s.finalScriptWitness.length && delete s.finalScriptWitness; - const r = this.outputs.map((s) => Ui(t, _n, s)), o = { ...this.global }; - return t === 0 ? (o.unsignedTx = kn.decode(kn.encode({ + const r = this.outputs.map((s) => _i(t, Vn, s)), o = { ...this.global }; + return t === 0 ? (o.unsignedTx = Bn.decode(Bn.encode({ version: this.version, lockTime: this.lockTime, - inputs: this.inputs.map(Tn).map((s) => ({ + inputs: this.inputs.map(An).map((s) => ({ ...s, finalScriptSig: ot })), - outputs: this.outputs.map(Ne) - })), delete o.fallbackLocktime, delete o.txVersion) : (o.version = t, o.txVersion = this.version, o.inputCount = this.inputs.length, o.outputCount = this.outputs.length, o.fallbackLocktime && o.fallbackLocktime === Re && delete o.fallbackLocktime), this.opts.bip174jsCompat && (n.length || n.push({}), r.length || r.push({})), (t === 0 ? Ri : Ni).encode({ + outputs: this.outputs.map(Le) + })), delete o.fallbackLocktime, delete o.txVersion) : (o.version = t, o.txVersion = this.version, o.inputCount = this.inputs.length, o.outputCount = this.outputs.length, o.fallbackLocktime && o.fallbackLocktime === Ne && delete o.fallbackLocktime), this.opts.bip174jsCompat && (n.length || n.push({}), r.length || r.push({})), (t === 0 ? Ci : Pi).encode({ global: o, inputs: n, outputs: r @@ -4273,10 +4273,10 @@ let ue = class pr { } // BIP370 lockTime (https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#determining-lock-time) get lockTime() { - let t = Re, n = 0, r = Re, o = 0; + let t = Ne, n = 0, r = Ne, o = 0; for (const s of this.inputs) s.requiredHeightLocktime && (t = Math.max(t, s.requiredHeightLocktime), n++), s.requiredTimeLocktime && (r = Math.max(r, s.requiredTimeLocktime), o++); - return n && n >= o ? t : r !== Re ? r : this.global.fallbackLocktime || Re; + return n && n >= o ? t : r !== Ne ? r : this.global.fallbackLocktime || Ne; } get version() { if (this.global.txVersion === void 0) @@ -4329,27 +4329,27 @@ let ue = class pr { if (!this.isFinal) throw new Error("Transaction is not finalized"); let t = 32; - const n = this.outputs.map(Ne); - t += 4 * Wt.encode(this.outputs.length).length; + const n = this.outputs.map(Le); + t += 4 * zt.encode(this.outputs.length).length; for (const r of n) t += 32 + 4 * Mt.encode(r.script).length; - this.hasWitnesses && (t += 2), t += 4 * Wt.encode(this.inputs.length).length; + this.hasWitnesses && (t += 2), t += 4 * zt.encode(this.inputs.length).length; for (const r of this.inputs) - t += 160 + 4 * Mt.encode(r.finalScriptSig || ot).length, this.hasWitnesses && r.finalScriptWitness && (t += Cn.encode(r.finalScriptWitness).length); + t += 160 + 4 * Mt.encode(r.finalScriptSig || ot).length, this.hasWitnesses && r.finalScriptWitness && (t += Pn.encode(r.finalScriptWitness).length); return t; } get vsize() { - return Fl(this.weight); + return Zd(this.weight); } toBytes(t = !1, n = !1) { - return Qe.encode({ + return Je.encode({ version: this.version, lockTime: this.lockTime, - inputs: this.inputs.map(Tn).map((r) => ({ + inputs: this.inputs.map(An).map((r) => ({ ...r, finalScriptSig: t && r.finalScriptSig || ot })), - outputs: this.outputs.map(Ne), + outputs: this.outputs.map(Le), witnesses: this.inputs.map((r) => r.finalScriptWitness || []), segwitFlag: n && this.hasWitnesses }); @@ -4358,13 +4358,13 @@ let ue = class pr { return this.toBytes(!1, !1); } get hex() { - return $.encode(this.toBytes(!0, this.hasWitnesses)); + return U.encode(this.toBytes(!0, this.hasWitnesses)); } get hash() { - return $.encode(ge(this.toBytes(!0))); + return U.encode(ge(this.toBytes(!0))); } get id() { - return $.encode(ge(this.toBytes(!0)).reverse()); + return U.encode(ge(this.toBytes(!0)).reverse()); } // Input stuff checkInputIdx(t) { @@ -4372,7 +4372,7 @@ let ue = class pr { throw new Error(`Wrong input index=${t}`); } getInput(t) { - return this.checkInputIdx(t), Or(this.inputs[t]); + return this.checkInputIdx(t), Rr(this.inputs[t]); } get inputsLength() { return this.inputs.length; @@ -4381,16 +4381,16 @@ let ue = class pr { addInput(t, n = !1) { if (!n && !this.signStatus().addInput) throw new Error("Tx has signed inputs, cannot add new one"); - return this.inputs.push(Hi(t, void 0, void 0, this.opts.disableScriptCheck)), this.inputs.length - 1; + return this.inputs.push(Ki(t, void 0, void 0, this.opts.disableScriptCheck)), this.inputs.length - 1; } updateInput(t, n, r = !1) { this.checkInputIdx(t); let o; if (!r) { const s = this.signStatus(); - (!s.addInput || s.inputs.includes(t)) && (o = xl); + (!s.addInput || s.inputs.includes(t)) && (o = Bd); } - this.inputs[t] = Hi(n, this.inputs[t], o, this.opts.disableScriptCheck, this.opts.allowUnknown); + this.inputs[t] = Ki(n, this.inputs[t], o, this.opts.disableScriptCheck, this.opts.allowUnknown); } // Output stuff checkOutputIdx(t) { @@ -4398,12 +4398,12 @@ let ue = class pr { throw new Error(`Wrong output index=${t}`); } getOutput(t) { - return this.checkOutputIdx(t), Or(this.outputs[t]); + return this.checkOutputIdx(t), Rr(this.outputs[t]); } getOutputAddress(t, n = un) { const r = this.getOutput(t); if (r.script) - return Me(n).encode(ut.decode(r.script)); + return Ke(n).encode(ut.decode(r.script)); } get outputsLength() { return this.outputs.length; @@ -4412,11 +4412,11 @@ let ue = class pr { let { amount: o, script: s } = t; if (o === void 0 && (o = n?.amount), typeof o != "bigint") throw new Error(`Wrong amount type, should be of type bigint in sats, but got ${o} of type ${typeof o}`); - typeof s == "string" && (s = $.decode(s)), s === void 0 && (s = n?.script); + typeof s == "string" && (s = U.decode(s)), s === void 0 && (s = n?.script); let i = { ...n, ...t, amount: o, script: s }; - if (i.amount === void 0 && delete i.amount, i = zo(_n, i, n, r, this.opts.allowUnknown), Vs.encode(i), i.script && !this.opts.allowUnknownOutputs && ut.decode(i.script).type === "unknown") + if (i.amount === void 0 && delete i.amount, i = Xo(Vn, i, n, r, this.opts.allowUnknown), zs.encode(i), i.script && !this.opts.allowUnknownOutputs && ut.decode(i.script).type === "unknown") throw new Error("Transaction/output: unknown output script type, there is a chance that input is unspendable. Pass allowUnknownOutputs=true, if you sure"); - return this.opts.disableScriptCheck || Aa(i.script, i.redeemScript, i.witnessScript), i; + return this.opts.disableScriptCheck || Ua(i.script, i.redeemScript, i.witnessScript), i; } addOutput(t, n = !1) { if (!n && !this.signStatus().addOutput) @@ -4428,23 +4428,23 @@ let ue = class pr { let o; if (!r) { const s = this.signStatus(); - (!s.addOutput || s.outputs.includes(t)) && (o = Sl); + (!s.addOutput || s.outputs.includes(t)) && (o = Od); } this.outputs[t] = this.normalizeOutput(n, this.outputs[t], o); } addOutputAddress(t, n, r = un) { - return this.addOutput({ script: ut.encode(Me(r).decode(t)), amount: n }); + return this.addOutput({ script: ut.encode(Ke(r).decode(t)), amount: n }); } // Utils get fee() { let t = 0n; for (const r of this.inputs) { - const o = hr(r); + const o = wr(r); if (!o) throw new Error("Empty input amount"); t += o.amount; } - const n = this.outputs.map(Ne); + const n = this.outputs.map(Le); for (const r of n) t -= r.amount; return t; @@ -4454,38 +4454,38 @@ let ue = class pr { // There is optimization opportunity to re-use hashes for multiple inputs for witness v0/v1, // but we are trying to be less complicated for audit purpose for now. preimageLegacy(t, n, r) { - const { isAny: o, isNone: s, isSingle: i } = _i(r); + const { isAny: o, isNone: s, isSingle: i } = Di(r); if (t < 0 || !Number.isSafeInteger(t)) throw new Error(`Invalid input idx=${t}`); if (i && t >= this.outputs.length || t >= this.inputs.length) - return pa.encode(1n); + return ba.encode(1n); n = K.encode(K.decode(n).filter((f) => f !== "CODESEPARATOR")); - let c = this.inputs.map(Tn).map((f, l) => ({ + let c = this.inputs.map(An).map((f, d) => ({ ...f, - finalScriptSig: l === t ? n : ot + finalScriptSig: d === t ? n : ot })); - o ? c = [c[t]] : (s || i) && (c = c.map((f, l) => ({ + o ? c = [c[t]] : (s || i) && (c = c.map((f, d) => ({ ...f, - sequence: l === t ? f.sequence : 0 + sequence: d === t ? f.sequence : 0 }))); - let a = this.outputs.map(Ne); - s ? a = [] : i && (a = a.slice(0, t).fill(Kl).concat([a[t]])); - const u = Qe.encode({ + let a = this.outputs.map(Le); + s ? a = [] : i && (a = a.slice(0, t).fill(Yd).concat([a[t]])); + const u = Je.encode({ lockTime: this.lockTime, version: this.version, segwitFlag: !1, inputs: c, outputs: a }); - return ge(u, Ye.encode(r)); + return ge(u, Ze.encode(r)); } preimageWitnessV0(t, n, r, o) { - const { isAny: s, isNone: i, isSingle: c } = _i(r); - let a = er, u = er, f = er; - const l = this.inputs.map(Tn), d = this.outputs.map(Ne); - s || (a = ge(...l.map(xo.encode))), !s && !c && !i && (u = ge(...l.map((w) => Z.encode(w.sequence)))), !c && !i ? f = ge(...d.map(Pe.encode)) : c && t < d.length && (f = ge(Pe.encode(d[t]))); - const h = l[t]; - return ge(Ye.encode(this.version), a, u, rt(32, !0).encode(h.txid), Z.encode(h.index), Mt.encode(n), dr.encode(o), Z.encode(h.sequence), f, Z.encode(this.lockTime), Z.encode(r)); + const { isAny: s, isNone: i, isSingle: c } = Di(r); + let a = nr, u = nr, f = nr; + const d = this.inputs.map(An), l = this.outputs.map(Le); + s || (a = ge(...d.map(vo.encode))), !s && !c && !i && (u = ge(...d.map((w) => X.encode(w.sequence)))), !c && !i ? f = ge(...l.map(Ve.encode)) : c && t < l.length && (f = ge(Ve.encode(l[t]))); + const h = d[t]; + return ge(Ze.encode(this.version), a, u, rt(32, !0).encode(h.txid), X.encode(h.index), Mt.encode(n), gr.encode(o), X.encode(h.sequence), f, X.encode(this.lockTime), X.encode(r)); } preimageWitnessV1(t, n, r, o, s = -1, i, c = 192, a) { if (!Array.isArray(o) || this.inputs.length !== o.length) @@ -4493,38 +4493,38 @@ let ue = class pr { if (!Array.isArray(n) || this.inputs.length !== n.length) throw new Error(`Invalid prevOutScript array=${n}`); const u = [ - Se.encode(0), - Se.encode(r), + Te.encode(0), + Te.encode(r), // U8 sigHash - Ye.encode(this.version), - Z.encode(this.lockTime) - ], f = r === j.DEFAULT ? j.ALL : r & 3, l = r & j.ANYONECANPAY, d = this.inputs.map(Tn), h = this.outputs.map(Ne); - l !== j.ANYONECANPAY && u.push(...[ - d.map(xo.encode), - o.map(dr.encode), + Ze.encode(this.version), + X.encode(this.lockTime) + ], f = r === j.DEFAULT ? j.ALL : r & 3, d = r & j.ANYONECANPAY, l = this.inputs.map(An), h = this.outputs.map(Le); + d !== j.ANYONECANPAY && u.push(...[ + l.map(vo.encode), + o.map(gr.encode), n.map(Mt.encode), - d.map((g) => Z.encode(g.sequence)) - ].map((g) => wt(Ee(...g)))), f === j.ALL && u.push(wt(Ee(...h.map(Pe.encode)))); + l.map((g) => X.encode(g.sequence)) + ].map((g) => yt(Ee(...g)))), f === j.ALL && u.push(yt(Ee(...h.map(Ve.encode)))); const w = (a ? 1 : 0) | (i ? 2 : 0); - if (u.push(new Uint8Array([w])), l === j.ANYONECANPAY) { - const g = d[t]; - u.push(xo.encode(g), dr.encode(o[t]), Mt.encode(n[t]), Z.encode(g.sequence)); + if (u.push(new Uint8Array([w])), d === j.ANYONECANPAY) { + const g = l[t]; + u.push(vo.encode(g), gr.encode(o[t]), Mt.encode(n[t]), X.encode(g.sequence)); } else - u.push(Z.encode(t)); - return w & 1 && u.push(wt(Mt.encode(a || ot))), f === j.SINGLE && u.push(t < h.length ? wt(Pe.encode(h[t])) : er), i && u.push(On(i, c), Se.encode(0), Ye.encode(s)), Ns("TapSighash", ...u); + u.push(X.encode(t)); + return w & 1 && u.push(yt(Mt.encode(a || ot))), f === j.SINGLE && u.push(t < h.length ? yt(Ve.encode(h[t])) : nr), i && u.push(Un(i, c), Te.encode(0), Ze.encode(s)), Hs("TapSighash", ...u); } // Signer can be privateKey OR instance of bip32 HD stuff signIdx(t, n, r, o) { this.checkInputIdx(n); - const s = this.inputs[n], i = Vi(s, this.opts.allowLegacyWitnessUtxo); + const s = this.inputs[n], i = Fi(s, this.opts.allowLegacyWitnessUtxo); if (!nt(t)) { if (!s.bip32Derivation || !s.bip32Derivation.length) throw new Error("bip32Derivation: empty"); - const f = s.bip32Derivation.filter((d) => d[1].fingerprint == t.fingerprint).map(([d, { path: h }]) => { + const f = s.bip32Derivation.filter((l) => l[1].fingerprint == t.fingerprint).map(([l, { path: h }]) => { let w = t; for (const g of h) w = w.deriveChild(g); - if (!ct(w.publicKey, d)) + if (!ct(w.publicKey, l)) throw new Error("bip32Derivation: wrong pubKey"); if (!w.privateKey) throw new Error("bip32Derivation: no privateKey"); @@ -4532,49 +4532,49 @@ let ue = class pr { }); if (!f.length) throw new Error(`bip32Derivation: no items with fingerprint=${t.fingerprint}`); - let l = !1; - for (const d of f) - this.signIdx(d.privateKey, n) && (l = !0); - return l; + let d = !1; + for (const l of f) + this.signIdx(l.privateKey, n) && (d = !0); + return d; } - r ? r.forEach(jl) : r = [i.defaultSighash]; + r ? r.forEach(el) : r = [i.defaultSighash]; const c = i.sighash; if (!r.includes(c)) throw new Error(`Input with not allowed sigHash=${c}. Allowed: ${r.join(", ")}`); const { sigOutputs: a } = this.inputSighash(n); if (a === j.SINGLE && n >= this.outputs.length) throw new Error(`Input with sighash SINGLE, but there is no output with corresponding index=${n}`); - const u = hr(s); + const u = wr(s); if (i.txType === "taproot") { - const f = this.inputs.map(hr), l = f.map((y) => y.script), d = f.map((y) => y.amount); - let h = !1, w = Rs(t), g = s.tapMerkleRoot || ot; + const f = this.inputs.map(wr), d = f.map((y) => y.script), l = f.map((y) => y.amount); + let h = !1, w = Vs(t), g = s.tapMerkleRoot || ot; if (s.tapInternalKey) { - const { pubKey: y, privKey: S } = ql(t, w, s.tapInternalKey, g), [v, O] = Ko(s.tapInternalKey, g); - if (ct(v, y)) { - const N = this.preimageWitnessV1(n, l, c, d), U = Ee(Ii(N, S, o), c !== j.DEFAULT ? new Uint8Array([c]) : ot); - this.updateInput(n, { tapKeySig: U }, !0), h = !0; + const { pubKey: y, privKey: S } = tl(t, w, s.tapInternalKey, g), [T] = jo(s.tapInternalKey, g); + if (ct(T, y)) { + const k = this.preimageWitnessV1(n, d, c, l), N = Ee(Ui(k, S, o), c !== j.DEFAULT ? new Uint8Array([c]) : ot); + this.updateInput(n, { tapKeySig: N }, !0), h = !0; } } if (s.tapLeafScript) { s.tapScriptSig = s.tapScriptSig || []; for (const [y, S] of s.tapLeafScript) { - const v = S.subarray(0, -1), O = K.decode(v), N = S[S.length - 1], U = On(v, N); - if (O.findIndex((C) => nt(C) && ct(C, w)) === -1) + const T = S.subarray(0, -1), k = K.decode(T), N = S[S.length - 1], $ = Un(T, N); + if (k.findIndex((L) => nt(L) && ct(L, w)) === -1) continue; - const x = this.preimageWitnessV1(n, l, c, d, void 0, v, N), Q = Ee(Ii(x, t, o), c !== j.DEFAULT ? new Uint8Array([c]) : ot); - this.updateInput(n, { tapScriptSig: [[{ pubKey: w, leafHash: U }, Q]] }, !0), h = !0; + const x = this.preimageWitnessV1(n, d, c, l, void 0, T, N), Y = Ee(Ui(x, t, o), c !== j.DEFAULT ? new Uint8Array([c]) : ot); + this.updateInput(n, { tapScriptSig: [[{ pubKey: w, leafHash: $ }, Y]] }, !0), h = !0; } } if (!h) throw new Error("No taproot scripts signed"); return !0; } else { - const f = ma(t); - let l = !1; - const d = ya(f); + const f = Ta(t); + let d = !1; + const l = Sa(f); for (const g of K.decode(i.lastScript)) - nt(g) && (ct(g, f) || ct(g, d)) && (l = !0); - if (!l) + nt(g) && (ct(g, f) || ct(g, l)) && (d = !0); + if (!d) throw new Error(`Input script doesn't have pubKey: ${i.lastScript}`); let h; if (i.txType === "legacy") @@ -4584,7 +4584,7 @@ let ue = class pr { i.last.type === "wpkh" && (g = ut.encode({ type: "pkh", hash: i.last.hash })), h = this.preimageWitnessV0(n, g, c, u.amount); } else throw new Error(`Transaction/sign: unknown tx type: ${i.txType}`); - const w = fl(h, t, this.opts.lowR); + const w = yd(h, t, this.opts.lowR); this.updateInput(n, { partialSig: [[f, Ee(w, new Uint8Array([c]))]] }, !0); @@ -4612,69 +4612,69 @@ let ue = class pr { finalizeIdx(t) { if (this.checkInputIdx(t), this.fee < 0n) throw new Error("Outputs spends more than inputs amount"); - const n = this.inputs[t], r = Vi(n, this.opts.allowLegacyWitnessUtxo); + const n = this.inputs[t], r = Fi(n, this.opts.allowLegacyWitnessUtxo); if (r.txType === "taproot") { if (n.tapKeySig) n.finalScriptWitness = [n.tapKeySig]; else if (n.tapLeafScript && n.tapScriptSig) { - const a = n.tapLeafScript.sort((u, f) => Zt.encode(u[0]).length - Zt.encode(f[0]).length); + const a = n.tapLeafScript.sort((u, f) => Xt.encode(u[0]).length - Xt.encode(f[0]).length); for (const [u, f] of a) { - const l = f.slice(0, -1), d = f[f.length - 1], h = ut.decode(l), w = On(l, d), g = n.tapScriptSig.filter((S) => ct(S[0].leafHash, w)); + const d = f.slice(0, -1), l = f[f.length - 1], h = ut.decode(d), w = Un(d, l), g = n.tapScriptSig.filter((S) => ct(S[0].leafHash, w)); let y = []; if (h.type === "tr_ms") { - const S = h.m, v = h.pubkeys; - let O = 0; - for (const N of v) { - const U = g.findIndex((W) => ct(W[0].pubKey, N)); - if (O === S || U === -1) { + const S = h.m, T = h.pubkeys; + let k = 0; + for (const N of T) { + const $ = g.findIndex((F) => ct(F[0].pubKey, N)); + if (k === S || $ === -1) { y.push(ot); continue; } - y.push(g[U][1]), O++; + y.push(g[$][1]), k++; } - if (O !== S) + if (k !== S) continue; } else if (h.type === "tr_ns") { for (const S of h.pubkeys) { - const v = g.findIndex((O) => ct(O[0].pubKey, S)); - v !== -1 && y.push(g[v][1]); + const T = g.findIndex((k) => ct(k[0].pubKey, S)); + T !== -1 && y.push(g[T][1]); } if (y.length !== h.pubkeys.length) continue; } else if (h.type === "unknown" && this.opts.allowUnknownInputs) { - const S = K.decode(l); - if (y = g.map(([{ pubKey: v }, O]) => { - const N = S.findIndex((U) => nt(U) && ct(U, v)); + const S = K.decode(d); + if (y = g.map(([{ pubKey: T }, k]) => { + const N = S.findIndex(($) => nt($) && ct($, T)); if (N === -1) throw new Error("finalize/taproot: cannot find position of pubkey in script"); - return { signature: O, pos: N }; - }).sort((v, O) => v.pos - O.pos).map((v) => v.signature), !y.length) + return { signature: k, pos: N }; + }).sort((T, k) => T.pos - k.pos).map((T) => T.signature), !y.length) continue; } else { const S = this.opts.customScripts; if (S) - for (const v of S) { - if (!v.finalizeTaproot) + for (const T of S) { + if (!T.finalizeTaproot) continue; - const O = K.decode(l), N = v.encode(O); + const k = K.decode(d), N = T.encode(k); if (N === void 0) continue; - const U = v.finalizeTaproot(l, N, g); - if (U) { - n.finalScriptWitness = U.concat(Zt.encode(u)), n.finalScriptSig = ot, Eo(n); + const $ = T.finalizeTaproot(d, N, g); + if ($) { + n.finalScriptWitness = $.concat(Xt.encode(u)), n.finalScriptSig = ot, To(n); return; } } throw new Error("Finalize: Unknown tapLeafScript"); } - n.finalScriptWitness = y.reverse().concat([l, Zt.encode(u)]); + n.finalScriptWitness = y.reverse().concat([d, Xt.encode(u)]); break; } if (!n.finalScriptWitness) throw new Error("finalize/taproot: empty witness"); } else throw new Error("finalize/taproot: unknown input"); - n.finalScriptSig = ot, Eo(n); + n.finalScriptSig = ot, To(n); return; } if (!n.partialSig || !n.partialSig.length) @@ -4683,9 +4683,9 @@ let ue = class pr { if (r.last.type === "ms") { const a = r.last.m, u = r.last.pubkeys; let f = []; - for (const l of u) { - const d = n.partialSig.find((h) => ct(l, h[0])); - d && f.push(d[1]); + for (const d of u) { + const l = n.partialSig.find((h) => ct(d, h[0])); + l && f.push(l[1]); } if (f = f.slice(0, a), f.length !== a) throw new Error(`Multisig: wrong signatures count, m=${a} n=${u.length} signatures=${f.length}`); @@ -4705,9 +4705,9 @@ let ue = class pr { if (nt(a)) return a; throw new Error(`Wrong witness op=${a}`); - })), s = s.concat(r.lastScript)), r.txType === "segwit" && (c = s), r.type.startsWith("sh-wsh-") ? i = K.encode([K.encode([0, wt(r.lastScript)])]) : r.type.startsWith("sh-") ? i = K.encode([...K.decode(o), r.lastScript]) : r.type.startsWith("wsh-") || r.txType !== "segwit" && (i = o), !i && !c) + })), s = s.concat(r.lastScript)), r.txType === "segwit" && (c = s), r.type.startsWith("sh-wsh-") ? i = K.encode([K.encode([0, yt(r.lastScript)])]) : r.type.startsWith("sh-") ? i = K.encode([...K.decode(o), r.lastScript]) : r.type.startsWith("wsh-") || r.txType !== "segwit" && (i = o), !i && !c) throw new Error("Unknown error finalizing input"); - i && (n.finalScriptSig = i), c && (n.finalScriptWitness = c), Eo(n); + i && (n.finalScriptSig = i), c && (n.finalScriptWitness = c), To(n); } finalize() { for (let t = 0; t < this.inputs.length; t++) @@ -4729,10 +4729,10 @@ let ue = class pr { for (const o of ["inputs", "outputs"]) if (this[o].length !== t[o].length) throw new Error(`Transaction/combine: different ${o} length this=${this[o].length} other=${t[o].length}`); - const n = this.global.unsignedTx ? kn.encode(this.global.unsignedTx) : ot, r = t.global.unsignedTx ? kn.encode(t.global.unsignedTx) : ot; + const n = this.global.unsignedTx ? Bn.encode(this.global.unsignedTx) : ot, r = t.global.unsignedTx ? Bn.encode(t.global.unsignedTx) : ot; if (!ct(n, r)) throw new Error("Transaction/combine: different unsigned tx"); - this.global = zo(_s, this.global, t.global, void 0, this.opts.allowUnknown); + this.global = Xo(Ks, this.global, t.global, void 0, this.opts.allowUnknown); for (let o = 0; o < this.inputs.length; o++) this.updateInput(o, t.inputs[o], !0); for (let o = 0; o < this.outputs.length; o++) @@ -4740,47 +4740,47 @@ let ue = class pr { return this; } clone() { - return pr.fromPSBT(this.toPSBT(this.opts.PSBTVersion), this.opts); + return yr.fromPSBT(this.toPSBT(this.opts.PSBTVersion), this.opts); } }; -class ke extends ue { +class Be extends Kt { constructor(t) { - super(So(t)); + super(Ao(t)); } static fromPSBT(t, n) { - return ue.fromPSBT(t, So(n)); + return Kt.fromPSBT(t, Ao(n)); } static fromRaw(t, n) { - return ue.fromRaw(t, So(n)); + return Kt.fromRaw(t, Ao(n)); } } -ke.ARK_TX_OPTS = { +Be.ARK_TX_OPTS = { allowUnknown: !0, allowUnknownOutputs: !0, allowUnknownInputs: !0 }; -function So(e) { - return { ...ke.ARK_TX_OPTS, ...e }; +function Ao(e) { + return { ...Be.ARK_TX_OPTS, ...e }; } -class Ms extends Error { +class qs extends Error { idx; // Indice of participant constructor(t, n) { super(n), this.idx = t; } } -const { taggedHash: Oa, pointToBytes: nr } = de.utils, zt = xe.Point, z = zt.Fn, Jt = xe.lengths.publicKey, Yo = new Uint8Array(Jt), Di = Ie(rt(33), { - decode: (e) => Hn(e) ? Yo : e.toBytes(!0), - encode: (e) => Ln(e, Yo) ? zt.ZERO : zt.fromBytes(e) -}), Mi = Tt(pa, (e) => (Uc("n", e, 1n, z.ORDER), e)), Je = pt({ R1: Di, R2: Di }), $a = pt({ k1: Mi, k2: Mi, publicKey: rt(Jt) }); -function Ki(e, ...t) { +const { taggedHash: La, pointToBytes: rr } = le.utils, Gt = Se.Point, z = Gt.Fn, te = Se.lengths.publicKey, es = new Uint8Array(te), Wi = ke(rt(33), { + decode: (e) => Dn(e) ? es : e.toBytes(!0), + encode: (e) => Cn(e, es) ? Gt.ZERO : Gt.fromBytes(e) +}), zi = At(ba, (e) => (Cc("n", e, 1n, z.ORDER), e)), tn = pt({ R1: Wi, R2: Wi }), _a = pt({ k1: zi, k2: zi, publicKey: rt(te) }); +function Gi(e, ...t) { } function Lt(e, ...t) { if (!Array.isArray(e)) throw new Error("expected array"); e.forEach((n) => G(n, ...t)); } -function Fi(e) { +function qi(e) { if (!Array.isArray(e)) throw new Error("expected array"); e.forEach((t, n) => { @@ -4788,80 +4788,80 @@ function Fi(e) { throw new Error("expected boolean in xOnly array, got" + t + "(" + n + ")"); }); } -const $r = (e, ...t) => z.create(z.fromBytes(Oa(e, ...t), !0)), An = (e, t) => Yn(e.y) ? t : z.neg(t); +const Nr = (e, ...t) => z.create(z.fromBytes(La(e, ...t), !0)), In = (e, t) => Zn(e.y) ? t : z.neg(t); function He(e) { - return zt.BASE.multiply(e); + return Gt.BASE.multiply(e); } -function Hn(e) { - return e.equals(zt.ZERO); +function Dn(e) { + return e.equals(Gt.ZERO); } -function Zo(e) { - return Lt(e, Jt), e.sort(Ir); +function ns(e) { + return Lt(e, te), e.sort(Or); } -function Ua(e) { - Lt(e, Jt); +function Ca(e) { + Lt(e, te); for (let t = 1; t < e.length; t++) - if (!Ln(e[t], e[0])) + if (!Cn(e[t], e[0])) return e[t]; - return Yo; + return es; } -function Ra(e) { - return Lt(e, Jt), Oa("KeyAgg list", ...e); +function Pa(e) { + return Lt(e, te), La("KeyAgg list", ...e); } -function Na(e, t, n) { - return G(e, Jt), G(t, Jt), Ln(e, t) ? 1n : $r("KeyAgg coefficient", n, e); +function Va(e, t, n) { + return G(e, te), G(t, te), Cn(e, t) ? 1n : Nr("KeyAgg coefficient", n, e); } -function Xo(e, t = [], n = []) { - if (Lt(e, Jt), Lt(t, 32), t.length !== n.length) +function rs(e, t = [], n = []) { + if (Lt(e, te), Lt(t, 32), t.length !== n.length) throw new Error("The tweaks and isXonly arrays must have the same length"); - const r = Ua(e), o = Ra(e); - let s = zt.ZERO; + const r = Ca(e), o = Pa(e); + let s = Gt.ZERO; for (let a = 0; a < e.length; a++) { let u; try { - u = zt.fromBytes(e[a]); + u = Gt.fromBytes(e[a]); } catch { - throw new Ms(a, "pubkey"); + throw new qs(a, "pubkey"); } - s = s.add(u.multiply(Na(e[a], r, o))); + s = s.add(u.multiply(Va(e[a], r, o))); } let i = z.ONE, c = z.ZERO; for (let a = 0; a < t.length; a++) { - const u = n[a] && !Yn(s.y) ? z.neg(z.ONE) : z.ONE, f = z.fromBytes(t[a]); - if (s = s.multiply(u).add(He(f)), Hn(s)) + const u = n[a] && !Zn(s.y) ? z.neg(z.ONE) : z.ONE, f = z.fromBytes(t[a]); + if (s = s.multiply(u).add(He(f)), Dn(s)) throw new Error("The result of tweaking cannot be infinity"); i = z.mul(u, i), c = z.add(f, z.mul(u, c)); } return { aggPublicKey: s, gAcc: i, tweakAcc: c }; } -const Wi = (e, t, n, r, o, s) => $r("MuSig/nonce", e, new Uint8Array([t.length]), t, new Uint8Array([n.length]), n, o, zn(s.length, 4), s, new Uint8Array([r])); -function Zl(e, t, n = new Uint8Array(0), r, o = new Uint8Array(0), s = Wn(32)) { - if (G(e, Jt), Ki(t, 32), G(n), ![0, 32].includes(n.length)) +const ji = (e, t, n, r, o, s) => Nr("MuSig/nonce", e, new Uint8Array([t.length]), t, new Uint8Array([n.length]), n, o, Gn(s.length, 4), s, new Uint8Array([r])); +function rl(e, t, n = new Uint8Array(0), r, o = new Uint8Array(0), s = zn(32)) { + if (G(e, te), Gi(t, 32), G(n), ![0, 32].includes(n.length)) throw new Error("wrong aggPublicKey"); - Ki(), G(o), G(s, 32); - const i = Uint8Array.of(0), c = Wi(s, e, n, 0, i, o), a = Wi(s, e, n, 1, i, o); + Gi(), G(o), G(s, 32); + const i = Uint8Array.of(0), c = ji(s, e, n, 0, i, o), a = ji(s, e, n, 1, i, o); return { - secret: $a.encode({ k1: c, k2: a, publicKey: e }), - public: Je.encode({ R1: He(c), R2: He(a) }) + secret: _a.encode({ k1: c, k2: a, publicKey: e }), + public: tn.encode({ R1: He(c), R2: He(a) }) }; } -function Xl(e) { +function ol(e) { Lt(e, 66); - let t = zt.ZERO, n = zt.ZERO; + let t = Gt.ZERO, n = Gt.ZERO; for (let r = 0; r < e.length; r++) { const o = e[r]; try { - const { R1: s, R2: i } = Je.decode(o); - if (Hn(s) || Hn(i)) + const { R1: s, R2: i } = tn.decode(o); + if (Dn(s) || Dn(i)) throw new Error("infinity point"); t = t.add(s), n = n.add(i); } catch { - throw new Ms(r, "pubnonce"); + throw new qs(r, "pubnonce"); } } - return Je.encode({ R1: t, R2: n }); + return tn.encode({ R1: t, R2: n }); } -class Ql { +class sl { publicKeys; Q; gAcc; @@ -4885,12 +4885,12 @@ class Ql { * @throws {Error} If the input is invalid, such as wrong array sizes or lengths. */ constructor(t, n, r, o = [], s = []) { - if (Lt(n, 33), Lt(o, 32), Fi(s), G(r), o.length !== s.length) + if (Lt(n, 33), Lt(o, 32), qi(s), G(r), o.length !== s.length) throw new Error("The tweaks and isXonly arrays must have the same length"); - const { aggPublicKey: i, gAcc: c, tweakAcc: a } = Xo(n, o, s), { R1: u, R2: f } = Je.decode(t); - this.publicKeys = n, this.Q = i, this.gAcc = c, this.tweakAcc = a, this.b = $r("MuSig/noncecoef", t, nr(i), r); - const l = u.add(f.multiply(this.b)); - this.R = Hn(l) ? zt.BASE : l, this.e = $r("BIP0340/challenge", nr(this.R), nr(i), r), this.tweaks = o, this.isXonly = s, this.L = Ra(n), this.secondKey = Ua(n); + const { aggPublicKey: i, gAcc: c, tweakAcc: a } = rs(n, o, s), { R1: u, R2: f } = tn.decode(t); + this.publicKeys = n, this.Q = i, this.gAcc = c, this.tweakAcc = a, this.b = Nr("MuSig/noncecoef", t, rr(i), r); + const d = u.add(f.multiply(this.b)); + this.R = Dn(d) ? Gt.BASE : d, this.e = Nr("BIP0340/challenge", rr(this.R), rr(i), r), this.tweaks = o, this.isXonly = s, this.L = Pa(n), this.secondKey = Ca(n); } /** * Calculates the key aggregation coefficient for a given point. @@ -4901,16 +4901,16 @@ class Ql { */ getSessionKeyAggCoeff(t) { const { publicKeys: n } = this, r = t.toBytes(!0); - if (!n.some((s) => Ln(s, r))) + if (!n.some((s) => Cn(s, r))) throw new Error("The signer's pubkey must be included in the list of pubkeys"); - return Na(r, this.secondKey, this.L); + return Va(r, this.secondKey, this.L); } partialSigVerifyInternal(t, n, r) { const { Q: o, gAcc: s, b: i, R: c, e: a } = this, u = z.fromBytes(t, !0); if (!z.isValid(u)) return !1; - const { R1: f, R2: l } = Je.decode(n), d = f.add(l.multiply(i)), h = Yn(c.y) ? d : d.negate(), w = zt.fromBytes(r), g = this.getSessionKeyAggCoeff(w), y = z.mul(An(o, 1n), s), S = He(u), v = h.add(w.multiply(z.mul(a, z.mul(g, y)))); - return S.equals(v); + const { R1: f, R2: d } = tn.decode(n), l = f.add(d.multiply(i)), h = Zn(c.y) ? l : l.negate(), w = Gt.fromBytes(r), g = this.getSessionKeyAggCoeff(w), y = z.mul(In(o, 1n), s), S = He(u), T = h.add(w.multiply(z.mul(a, z.mul(g, y)))); + return S.equals(T); } /** * Generates a partial signature for a given message, secret nonce, secret key, and session context. @@ -4924,27 +4924,27 @@ class Ql { sign(t, n, r = !1) { if (G(n, 32), typeof r != "boolean") throw new Error("expected boolean"); - const { Q: o, gAcc: s, b: i, R: c, e: a } = this, { k1: u, k2: f, publicKey: l } = $a.decode(t); + const { Q: o, gAcc: s, b: i, R: c, e: a } = this, { k1: u, k2: f, publicKey: d } = _a.decode(t); if (t.fill(0, 0, 64), !z.isValid(u)) throw new Error("wrong k1"); if (!z.isValid(f)) throw new Error("wrong k1"); - const d = An(c, u), h = An(c, f), w = z.fromBytes(n); + const l = In(c, u), h = In(c, f), w = z.fromBytes(n); if (z.is0(w)) throw new Error("wrong d_"); const g = He(w), y = g.toBytes(!0); - if (!Ln(y, l)) + if (!Cn(y, d)) throw new Error("Public key does not match nonceGen argument"); - const S = this.getSessionKeyAggCoeff(g), v = An(o, 1n), O = z.mul(v, z.mul(s, w)), N = z.add(d, z.add(z.mul(i, h), z.mul(a, z.mul(S, O)))), U = z.toBytes(N); + const S = this.getSessionKeyAggCoeff(g), T = In(o, 1n), k = z.mul(T, z.mul(s, w)), N = z.add(l, z.add(z.mul(i, h), z.mul(a, z.mul(S, k)))), $ = z.toBytes(N); if (!r) { - const W = Je.encode({ + const F = tn.encode({ R1: He(u), R2: He(f) }); - if (!this.partialSigVerifyInternal(U, W, y)) + if (!this.partialSigVerifyInternal($, F, y)) throw new Error("Partial signature verification failed"); } - return U; + return $; } /** * Verifies a partial signature against the aggregate public key and other session parameters. @@ -4960,7 +4960,7 @@ class Ql { */ partialSigVerify(t, n, r) { const { publicKeys: o, tweaks: s, isXonly: i } = this; - if (G(t, 32), Lt(n, 66), Lt(o, Jt), Lt(s, 32), Fi(i), Te(r), n.length !== o.length) + if (G(t, 32), Lt(n, 66), Lt(o, te), Lt(s, 32), qi(i), Ae(r), n.length !== o.length) throw new Error("The pubNonces and publicKeys arrays must have the same length"); if (s.length !== i.length) throw new Error("The tweaks and isXonly arrays must have the same length"); @@ -4982,94 +4982,94 @@ class Ql { for (let a = 0; a < t.length; a++) { const u = z.fromBytes(t[a], !0); if (!z.isValid(u)) - throw new Ms(a, "psig"); + throw new qs(a, "psig"); i = z.add(i, u); } - const c = An(n, 1n); - return i = z.add(i, z.mul(s, z.mul(c, r))), Kt(nr(o), z.toBytes(i)); + const c = In(n, 1n); + return i = z.add(i, z.mul(s, z.mul(c, r))), Ft(rr(o), z.toBytes(i)); } } -function Jl(e) { - const t = Zl(e); +function il(e) { + const t = rl(e); return { secNonce: t.secret, pubNonce: t.public }; } -function td(e) { - return Xl(e); +function cl(e) { + return ol(e); } /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -function Ks(e) { +function js(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } -function Fe(e, t = "") { +function We(e, t = "") { if (!Number.isSafeInteger(e) || e < 0) { const n = t && `"${t}" `; throw new Error(`${n}expected integer >0, got ${e}`); } } function et(e, t, n = "") { - const r = Ks(e), o = e?.length, s = t !== void 0; + const r = js(e), o = e?.length, s = t !== void 0; if (!r || s && o !== t) { const i = n && `"${n}" `, c = s ? ` of length ${t}` : "", a = r ? `length=${o}` : `type=${typeof e}`; throw new Error(i + "expected Uint8Array" + c + ", got " + a); } return e; } -function La(e) { +function Ha(e) { if (typeof e != "function" || typeof e.create != "function") throw new Error("Hash must wrapped by utils.createHasher"); - Fe(e.outputLen), Fe(e.blockLen); + We(e.outputLen), We(e.blockLen); } -function Ur(e, t = !0) { +function Lr(e, t = !0) { if (e.destroyed) throw new Error("Hash instance has been destroyed"); if (t && e.finished) throw new Error("Hash#digest() has already been called"); } -function ed(e, t) { +function al(e, t) { et(e, void 0, "digestInto() output"); const n = t.outputLen; if (e.length < n) throw new Error('"digestInto() output" expected to be of length >=' + n); } -function Rr(...e) { +function _r(...e) { for (let t = 0; t < e.length; t++) e[t].fill(0); } -function vo(e) { +function Io(e) { return new DataView(e.buffer, e.byteOffset, e.byteLength); } -function jt(e, t) { +function Yt(e, t) { return e << 32 - t | e >>> t; } -const Ca = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", nd = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); -function so(e) { - if (et(e), Ca) +const Da = /* @ts-ignore */ typeof Uint8Array.from([]).toHex == "function" && typeof Uint8Array.fromHex == "function", ul = /* @__PURE__ */ Array.from({ length: 256 }, (e, t) => t.toString(16).padStart(2, "0")); +function co(e) { + if (et(e), Da) return e.toHex(); let t = ""; for (let n = 0; n < e.length; n++) - t += nd[e[n]]; + t += ul[e[n]]; return t; } -const re = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; -function zi(e) { - if (e >= re._0 && e <= re._9) - return e - re._0; - if (e >= re.A && e <= re.F) - return e - (re.A - 10); - if (e >= re.a && e <= re.f) - return e - (re.a - 10); +const oe = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; +function Yi(e) { + if (e >= oe._0 && e <= oe._9) + return e - oe._0; + if (e >= oe.A && e <= oe.F) + return e - (oe.A - 10); + if (e >= oe.a && e <= oe.f) + return e - (oe.a - 10); } -function Nr(e) { +function Cr(e) { if (typeof e != "string") throw new Error("hex string expected, got " + typeof e); - if (Ca) + if (Da) return Uint8Array.fromHex(e); const t = e.length, n = t / 2; if (t % 2) throw new Error("hex string expected, got unpadded hex of length " + t); const r = new Uint8Array(n); for (let o = 0, s = 0; o < n; o++, s += 2) { - const i = zi(e.charCodeAt(s)), c = zi(e.charCodeAt(s + 1)); + const i = Yi(e.charCodeAt(s)), c = Yi(e.charCodeAt(s + 1)); if (i === void 0 || c === void 0) { const a = e[s] + e[s + 1]; throw new Error('hex string expected, got non-hex character "' + a + '" at index ' + s); @@ -5078,7 +5078,7 @@ function Nr(e) { } return r; } -function Xt(...e) { +function Qt(...e) { let t = 0; for (let r = 0; r < e.length; r++) { const o = e[r]; @@ -5091,65 +5091,65 @@ function Xt(...e) { } return n; } -function rd(e, t = {}) { +function fl(e, t = {}) { const n = (o, s) => e(s).update(o).digest(), r = e(void 0); return n.outputLen = r.outputLen, n.blockLen = r.blockLen, n.create = (o) => e(o), Object.assign(n, t), Object.freeze(n); } -function io(e = 32) { +function ao(e = 32) { const t = typeof globalThis == "object" ? globalThis.crypto : null; if (typeof t?.getRandomValues != "function") throw new Error("crypto.getRandomValues must be defined"); return t.getRandomValues(new Uint8Array(e)); } -const od = (e) => ({ +const dl = (e) => ({ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, e]) }); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const Fs = /* @__PURE__ */ BigInt(0), Qo = /* @__PURE__ */ BigInt(1); -function Lr(e, t = "") { +const Ys = /* @__PURE__ */ BigInt(0), os = /* @__PURE__ */ BigInt(1); +function Pr(e, t = "") { if (typeof e != "boolean") { const n = t && `"${t}" `; throw new Error(n + "expected boolean, got type=" + typeof e); } return e; } -function _a(e) { +function Ma(e) { if (typeof e == "bigint") { - if (!gr(e)) + if (!mr(e)) throw new Error("positive bigint expected, got " + e); } else - Fe(e); + We(e); return e; } -function rr(e) { - const t = _a(e).toString(16); +function or(e) { + const t = Ma(e).toString(16); return t.length & 1 ? "0" + t : t; } -function Pa(e) { +function Ka(e) { if (typeof e != "string") throw new Error("hex string expected, got " + typeof e); - return e === "" ? Fs : BigInt("0x" + e); + return e === "" ? Ys : BigInt("0x" + e); } -function bn(e) { - return Pa(so(e)); +function En(e) { + return Ka(co(e)); } -function Ha(e) { - return Pa(so(sd(et(e)).reverse())); +function Fa(e) { + return Ka(co(ll(et(e)).reverse())); } -function Ws(e, t) { - Fe(t), e = _a(e); - const n = Nr(e.toString(16).padStart(t * 2, "0")); +function Zs(e, t) { + We(t), e = Ma(e); + const n = Cr(e.toString(16).padStart(t * 2, "0")); if (n.length !== t) throw new Error("number too large"); return n; } -function Va(e, t) { - return Ws(e, t).reverse(); +function Wa(e, t) { + return Zs(e, t).reverse(); } -function sd(e) { +function ll(e) { return Uint8Array.from(e); } -function id(e) { +function hl(e) { return Uint8Array.from(e, (t, n) => { const r = t.charCodeAt(0); if (t.length !== 1 || r > 127) @@ -5157,51 +5157,51 @@ function id(e) { return r; }); } -const gr = (e) => typeof e == "bigint" && Fs <= e; -function cd(e, t, n) { - return gr(e) && gr(t) && gr(n) && t <= e && e < n; +const mr = (e) => typeof e == "bigint" && Ys <= e; +function pl(e, t, n) { + return mr(e) && mr(t) && mr(n) && t <= e && e < n; } -function ad(e, t, n, r) { - if (!cd(t, n, r)) +function gl(e, t, n, r) { + if (!pl(t, n, r)) throw new Error("expected valid " + e + ": " + n + " <= n < " + r + ", got " + t); } -function ud(e) { +function wl(e) { let t; - for (t = 0; e > Fs; e >>= Qo, t += 1) + for (t = 0; e > Ys; e >>= os, t += 1) ; return t; } -const zs = (e) => (Qo << BigInt(e)) - Qo; -function fd(e, t, n) { - if (Fe(e, "hashLen"), Fe(t, "qByteLen"), typeof n != "function") +const Xs = (e) => (os << BigInt(e)) - os; +function yl(e, t, n) { + if (We(e, "hashLen"), We(t, "qByteLen"), typeof n != "function") throw new Error("hmacFn must be a function"); const r = (y) => new Uint8Array(y), o = Uint8Array.of(), s = Uint8Array.of(0), i = Uint8Array.of(1), c = 1e3; let a = r(e), u = r(e), f = 0; - const l = () => { + const d = () => { a.fill(1), u.fill(0), f = 0; - }, d = (...y) => n(u, Xt(a, ...y)), h = (y = o) => { - u = d(s, y), a = d(), y.length !== 0 && (u = d(i, y), a = d()); + }, l = (...y) => n(u, Qt(a, ...y)), h = (y = o) => { + u = l(s, y), a = l(), y.length !== 0 && (u = l(i, y), a = l()); }, w = () => { if (f++ >= c) throw new Error("drbg: tried max amount of iterations"); let y = 0; const S = []; for (; y < t; ) { - a = d(); - const v = a.slice(); - S.push(v), y += a.length; + a = l(); + const T = a.slice(); + S.push(T), y += a.length; } - return Xt(...S); + return Qt(...S); }; return (y, S) => { - l(), h(y); - let v; - for (; !(v = S(w())); ) + d(), h(y); + let T; + for (; !(T = S(w())); ) h(); - return l(), v; + return d(), T; }; } -function Gs(e, t = {}, n = {}) { +function Qs(e, t = {}, n = {}) { if (!e || typeof e != "object") throw new Error("expected valid options object"); function r(s, i, c) { @@ -5215,7 +5215,7 @@ function Gs(e, t = {}, n = {}) { const o = (s, i) => Object.entries(s).forEach(([c, a]) => r(c, a, i)); o(t, !1), o(n, !0); } -function Gi(e) { +function Zi(e) { const t = /* @__PURE__ */ new WeakMap(); return (n, ...r) => { const o = t.get(n); @@ -5226,7 +5226,7 @@ function Gi(e) { }; } /*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) */ -const Da = { +const za = { p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn, n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n, h: 1n, @@ -5234,104 +5234,104 @@ const Da = { b: 7n, Gx: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n, Gy: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n -}, { p: ve, n: Be, Gx: ld, Gy: dd, b: Ma } = Da, ht = 32, We = 64, Cr = { +}, { p: ve, n: Oe, Gx: ml, Gy: bl, b: Ga } = za, ht = 32, ze = 64, Vr = { publicKey: ht + 1, - publicKeyUncompressed: We + 1, - signature: We, + publicKeyUncompressed: ze + 1, + signature: ze, seed: ht + ht / 2 -}, hd = (...e) => { +}, El = (...e) => { "captureStackTrace" in Error && typeof Error.captureStackTrace == "function" && Error.captureStackTrace(...e); -}, X = (e = "") => { +}, Q = (e = "") => { const t = new Error(e); - throw hd(t, X), t; -}, pd = (e) => typeof e == "bigint", gd = (e) => typeof e == "string", wd = (e) => e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array", Bt = (e, t, n = "") => { - const r = wd(e), o = e?.length, s = t !== void 0; + throw El(t, Q), t; +}, xl = (e) => typeof e == "bigint", Sl = (e) => typeof e == "string", Tl = (e) => e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array", Bt = (e, t, n = "") => { + const r = Tl(e), o = e?.length, s = t !== void 0; if (!r || s && o !== t) { const i = n && `"${n}" `, c = s ? ` of length ${t}` : "", a = r ? `length=${o}` : `type=${typeof e}`; - X(i + "expected Uint8Array" + c + ", got " + a); + Q(i + "expected Uint8Array" + c + ", got " + a); } return e; -}, Oe = (e) => new Uint8Array(e), Ka = (e, t) => e.toString(16).padStart(t, "0"), Fa = (e) => Array.from(Bt(e)).map((t) => Ka(t, 2)).join(""), oe = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, qi = (e) => { - if (e >= oe._0 && e <= oe._9) - return e - oe._0; - if (e >= oe.A && e <= oe.F) - return e - (oe.A - 10); - if (e >= oe.a && e <= oe.f) - return e - (oe.a - 10); -}, Wa = (e) => { +}, Ue = (e) => new Uint8Array(e), qa = (e, t) => e.toString(16).padStart(t, "0"), ja = (e) => Array.from(Bt(e)).map((t) => qa(t, 2)).join(""), se = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }, Xi = (e) => { + if (e >= se._0 && e <= se._9) + return e - se._0; + if (e >= se.A && e <= se.F) + return e - (se.A - 10); + if (e >= se.a && e <= se.f) + return e - (se.a - 10); +}, Ya = (e) => { const t = "hex invalid"; - if (!gd(e)) - return X(t); + if (!Sl(e)) + return Q(t); const n = e.length, r = n / 2; if (n % 2) - return X(t); - const o = Oe(r); + return Q(t); + const o = Ue(r); for (let s = 0, i = 0; s < r; s++, i += 2) { - const c = qi(e.charCodeAt(i)), a = qi(e.charCodeAt(i + 1)); + const c = Xi(e.charCodeAt(i)), a = Xi(e.charCodeAt(i + 1)); if (c === void 0 || a === void 0) - return X(t); + return Q(t); o[s] = c * 16 + a; } return o; -}, za = () => globalThis?.crypto, ji = () => za()?.subtle ?? X("crypto.subtle must be defined, consider polyfill"), te = (...e) => { - const t = Oe(e.reduce((r, o) => r + Bt(o).length, 0)); +}, Za = () => globalThis?.crypto, Qi = () => Za()?.subtle ?? Q("crypto.subtle must be defined, consider polyfill"), ee = (...e) => { + const t = Ue(e.reduce((r, o) => r + Bt(o).length, 0)); let n = 0; return e.forEach((r) => { t.set(r, n), n += r.length; }), t; -}, co = (e = ht) => za().getRandomValues(Oe(e)), Vn = BigInt, ze = (e, t, n, r = "bad number: out of range") => pd(e) && t <= e && e < n ? e : X(r), P = (e, t = ve) => { +}, uo = (e = ht) => Za().getRandomValues(Ue(e)), Mn = BigInt, Ge = (e, t, n, r = "bad number: out of range") => xl(e) && t <= e && e < n ? e : Q(r), P = (e, t = ve) => { const n = e % t; return n >= 0n ? n : t + n; -}, ie = (e) => P(e, Be), Ga = (e, t) => { - (e === 0n || t <= 0n) && X("no inverse n=" + e + " mod=" + t); +}, ce = (e) => P(e, Oe), Xa = (e, t) => { + (e === 0n || t <= 0n) && Q("no inverse n=" + e + " mod=" + t); let n = P(e, t), r = t, o = 0n, s = 1n; for (; n !== 0n; ) { const i = r / n, c = r % n, a = o - s * i; r = n, n = c, o = s, s = a; } - return r === 1n ? P(o, t) : X("no inverse"); -}, qs = (e) => { - const t = js[e]; - return typeof t != "function" && X("hashes." + e + " not set"), t; -}, To = (e) => e instanceof Et ? e : X("Point expected"), qa = (e) => P(P(e * e) * e + Ma), Yi = (e) => ze(e, 0n, ve), wr = (e) => ze(e, 1n, ve), Jo = (e) => ze(e, 1n, Be), fn = (e) => (e & 1n) === 0n, ao = (e) => Uint8Array.of(e), yd = (e) => ao(fn(e) ? 2 : 3), ja = (e) => { - const t = qa(wr(e)); + return r === 1n ? P(o, t) : Q("no inverse"); +}, Qa = (e) => { + const t = lo[e]; + return typeof t != "function" && Q("hashes." + e + " not set"), t; +}, ko = (e) => e instanceof xt ? e : Q("Point expected"), Ja = (e) => P(P(e * e) * e + Ga), Ji = (e) => Ge(e, 0n, ve), br = (e) => Ge(e, 1n, ve), ss = (e) => Ge(e, 1n, Oe), fn = (e) => (e & 1n) === 0n, fo = (e) => Uint8Array.of(e), vl = (e) => fo(fn(e) ? 2 : 3), tu = (e) => { + const t = Ja(br(e)); let n = 1n; for (let r = t, o = (ve + 1n) / 4n; o > 0n; o >>= 1n) o & 1n && (n = n * r % ve), r = r * r % ve; - return P(n * n) === t ? n : X("sqrt invalid"); + return P(n * n) === t ? n : Q("sqrt invalid"); }; -class Et { +class xt { static BASE; static ZERO; X; Y; Z; constructor(t, n, r) { - this.X = Yi(t), this.Y = wr(n), this.Z = Yi(r), Object.freeze(this); + this.X = Ji(t), this.Y = br(n), this.Z = Ji(r), Object.freeze(this); } static CURVE() { - return Da; + return za; } /** Create 3d xyz point from 2d xy. (0, 0) => (0, 1, 0), not (0, 0, 1) */ static fromAffine(t) { const { x: n, y: r } = t; - return n === 0n && r === 0n ? Le : new Et(n, r, 1n); + return n === 0n && r === 0n ? _e : new xt(n, r, 1n); } /** Convert Uint8Array or hex string to Point. */ static fromBytes(t) { Bt(t); - const { publicKey: n, publicKeyUncompressed: r } = Cr; + const { publicKey: n, publicKeyUncompressed: r } = Vr; let o; - const s = t.length, i = t[0], c = t.subarray(1), a = ln(c, 0, ht); + const s = t.length, i = t[0], c = t.subarray(1), a = dn(c, 0, ht); if (s === n && (i === 2 || i === 3)) { - let u = ja(a); + let u = tu(a); const f = fn(u); - fn(Vn(i)) !== f && (u = P(-u)), o = new Et(a, u, 1n); + fn(Mn(i)) !== f && (u = P(-u)), o = new xt(a, u, 1n); } - return s === r && i === 4 && (o = new Et(a, ln(c, ht, We), 1n)), o ? o.assertValidity() : X("bad point: not on curve"); + return s === r && i === 4 && (o = new xt(a, dn(c, ht, ze), 1n)), o ? o.assertValidity() : Q("bad point: not on curve"); } static fromHex(t) { - return Et.fromBytes(Wa(t)); + return xt.fromBytes(Ya(t)); } get x() { return this.toAffine().x; @@ -5341,15 +5341,15 @@ class Et { } /** Equality check: compare points P&Q. */ equals(t) { - const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = To(t), a = P(n * c), u = P(s * o), f = P(r * c), l = P(i * o); - return a === u && f === l; + const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = ko(t), a = P(n * c), u = P(s * o), f = P(r * c), d = P(i * o); + return a === u && f === d; } is0() { - return this.equals(Le); + return this.equals(_e); } /** Flip point over y coordinate. */ negate() { - return new Et(this.X, P(-this.Y), this.Z); + return new xt(this.X, P(-this.Y), this.Z); } /** Point doubling: P+P, complete formula. */ double() { @@ -5362,16 +5362,16 @@ class Et { */ // prettier-ignore add(t) { - const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = To(t), a = 0n, u = Ma; - let f = 0n, l = 0n, d = 0n; + const { X: n, Y: r, Z: o } = this, { X: s, Y: i, Z: c } = ko(t), a = 0n, u = Ga; + let f = 0n, d = 0n, l = 0n; const h = P(u * 3n); - let w = P(n * s), g = P(r * i), y = P(o * c), S = P(n + r), v = P(s + i); - S = P(S * v), v = P(w + g), S = P(S - v), v = P(n + o); - let O = P(s + c); - return v = P(v * O), O = P(w + y), v = P(v - O), O = P(r + o), f = P(i + c), O = P(O * f), f = P(g + y), O = P(O - f), d = P(a * v), f = P(h * y), d = P(f + d), f = P(g - d), d = P(g + d), l = P(f * d), g = P(w + w), g = P(g + w), y = P(a * y), v = P(h * v), g = P(g + y), y = P(w - y), y = P(a * y), v = P(v + y), w = P(g * v), l = P(l + w), w = P(O * v), f = P(S * f), f = P(f - w), w = P(S * g), d = P(O * d), d = P(d + w), new Et(f, l, d); + let w = P(n * s), g = P(r * i), y = P(o * c), S = P(n + r), T = P(s + i); + S = P(S * T), T = P(w + g), S = P(S - T), T = P(n + o); + let k = P(s + c); + return T = P(T * k), k = P(w + y), T = P(T - k), k = P(r + o), f = P(i + c), k = P(k * f), f = P(g + y), k = P(k - f), l = P(a * T), f = P(h * y), l = P(f + l), f = P(g - l), l = P(g + l), d = P(f * l), g = P(w + w), g = P(g + w), y = P(a * y), T = P(h * T), g = P(g + y), y = P(w - y), y = P(a * y), T = P(T + y), w = P(g * T), d = P(d + w), w = P(k * T), f = P(S * f), f = P(f - w), w = P(S * g), l = P(k * l), l = P(l + w), new xt(f, d, l); } subtract(t) { - return this.add(To(t).negate()); + return this.add(ko(t).negate()); } /** * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n. @@ -5382,12 +5382,12 @@ class Et { */ multiply(t, n = !0) { if (!n && t === 0n) - return Le; - if (Jo(t), t === 1n) + return _e; + if (ss(t), t === 1n) return this; if (this.equals($e)) - return Fd(t).p; - let r = Le, o = $e; + return Zl(t).p; + let r = _e, o = $e; for (let s = this; t > 0n; s = s.double(), t >>= 1n) t & 1n ? r = r.add(s) : n && (o = o.add(s)); return r; @@ -5398,210 +5398,210 @@ class Et { /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */ toAffine() { const { X: t, Y: n, Z: r } = this; - if (this.equals(Le)) + if (this.equals(_e)) return { x: 0n, y: 0n }; if (r === 1n) return { x: t, y: n }; - const o = Ga(r, ve); - return P(r * o) !== 1n && X("inverse invalid"), { x: P(t * o), y: P(n * o) }; + const o = Xa(r, ve); + return P(r * o) !== 1n && Q("inverse invalid"), { x: P(t * o), y: P(n * o) }; } /** Checks if the point is valid and on-curve. */ assertValidity() { const { x: t, y: n } = this.toAffine(); - return wr(t), wr(n), P(n * n) === qa(t) ? this : X("bad point: not on curve"); + return br(t), br(n), P(n * n) === Ja(t) ? this : Q("bad point: not on curve"); } /** Converts point to 33/65-byte Uint8Array. */ toBytes(t = !0) { - const { x: n, y: r } = this.assertValidity().toAffine(), o = Ut(n); - return t ? te(yd(r), o) : te(ao(4), o, Ut(r)); + const { x: n, y: r } = this.assertValidity().toAffine(), o = $t(n); + return t ? ee(vl(r), o) : ee(fo(4), o, $t(r)); } toHex(t) { - return Fa(this.toBytes(t)); - } -} -const $e = new Et(ld, dd, 1n), Le = new Et(0n, 1n, 0n); -Et.BASE = $e; -Et.ZERO = Le; -const md = (e, t, n) => $e.multiply(t, !1).add(e.multiply(n, !1)).assertValidity(), Ue = (e) => Vn("0x" + (Fa(e) || "0")), ln = (e, t, n) => Ue(e.subarray(t, n)), bd = 2n ** 256n, Ut = (e) => Wa(Ka(ze(e, 0n, bd), We)), Ya = (e) => { - const t = Ue(Bt(e, ht, "secret key")); - return ze(t, 1n, Be, "invalid secret key: outside of range"); -}, Za = (e) => e > Be >> 1n, Ed = (e) => { - [0, 1, 2, 3].includes(e) || X("recovery id must be valid and present"); -}, xd = (e) => { - e != null && !Zi.includes(e) && X(`Signature format must be one of: ${Zi.join(", ")}`), e === Qa && X('Signature format "der" is not supported: switch to noble-curves'); -}, Sd = (e, t = dn) => { - xd(t); - const n = Cr.signature, r = n + 1; + return ja(this.toBytes(t)); + } +} +const $e = new xt(ml, bl, 1n), _e = new xt(0n, 1n, 0n); +xt.BASE = $e; +xt.ZERO = _e; +const Al = (e, t, n) => $e.multiply(t, !1).add(e.multiply(n, !1)).assertValidity(), Re = (e) => Mn("0x" + (ja(e) || "0")), dn = (e, t, n) => Re(e.subarray(t, n)), Il = 2n ** 256n, $t = (e) => Ya(qa(Ge(e, 0n, Il), ze)), eu = (e) => { + const t = Re(Bt(e, ht, "secret key")); + return Ge(t, 1n, Oe, "invalid secret key: outside of range"); +}, nu = (e) => e > Oe >> 1n, kl = (e) => { + [0, 1, 2, 3].includes(e) || Q("recovery id must be valid and present"); +}, Bl = (e) => { + e != null && !tc.includes(e) && Q(`Signature format must be one of: ${tc.join(", ")}`), e === ou && Q('Signature format "der" is not supported: switch to noble-curves'); +}, Ol = (e, t = ln) => { + Bl(t); + const n = Vr.signature, r = n + 1; let o = `Signature format "${t}" expects Uint8Array with length `; - t === dn && e.length !== n && X(o + n), t === Pr && e.length !== r && X(o + r); + t === ln && e.length !== n && Q(o + n), t === Dr && e.length !== r && Q(o + r); }; -class _r { +class Hr { r; s; recovery; constructor(t, n, r) { - this.r = Jo(t), this.s = Jo(n), r != null && (this.recovery = r), Object.freeze(this); + this.r = ss(t), this.s = ss(n), r != null && (this.recovery = r), Object.freeze(this); } - static fromBytes(t, n = dn) { - Sd(t, n); + static fromBytes(t, n = ln) { + Ol(t, n); let r; - n === Pr && (r = t[0], t = t.subarray(1)); - const o = ln(t, 0, ht), s = ln(t, ht, We); - return new _r(o, s, r); + n === Dr && (r = t[0], t = t.subarray(1)); + const o = dn(t, 0, ht), s = dn(t, ht, ze); + return new Hr(o, s, r); } addRecoveryBit(t) { - return new _r(this.r, this.s, t); + return new Hr(this.r, this.s, t); } hasHighS() { - return Za(this.s); + return nu(this.s); } - toBytes(t = dn) { - const { r: n, s: r, recovery: o } = this, s = te(Ut(n), Ut(r)); - return t === Pr ? (Ed(o), te(Uint8Array.of(o), s)) : s; + toBytes(t = ln) { + const { r: n, s: r, recovery: o } = this, s = ee($t(n), $t(r)); + return t === Dr ? (kl(o), ee(Uint8Array.of(o), s)) : s; } } -const Xa = (e) => { +const ru = (e) => { const t = e.length * 8 - 256; - t > 1024 && X("msg invalid"); - const n = Ue(e); - return t > 0 ? n >> Vn(t) : n; -}, vd = (e) => ie(Xa(Bt(e))), dn = "compact", Pr = "recovered", Qa = "der", Zi = [dn, Pr, Qa], Xi = { + t > 1024 && Q("msg invalid"); + const n = Re(e); + return t > 0 ? n >> Mn(t) : n; +}, Ul = (e) => ce(ru(Bt(e))), ln = "compact", Dr = "recovered", ou = "der", tc = [ln, Dr, ou], ec = { lowS: !0, prehash: !0, - format: dn, + format: ln, extraEntropy: !1 -}, Qi = "SHA-256", js = { +}, nc = "SHA-256", lo = { hmacSha256Async: async (e, t) => { - const n = ji(), r = "HMAC", o = await n.importKey("raw", e, { name: r, hash: { name: Qi } }, !1, ["sign"]); - return Oe(await n.sign(r, o, t)); + const n = Qi(), r = "HMAC", o = await n.importKey("raw", e, { name: r, hash: { name: nc } }, !1, ["sign"]); + return Ue(await n.sign(r, o, t)); }, hmacSha256: void 0, - sha256Async: async (e) => Oe(await ji().digest(Qi, e)), + sha256Async: async (e) => Ue(await Qi().digest(nc, e)), sha256: void 0 -}, Td = (e, t, n) => (Bt(e, void 0, "message"), t.prehash ? n ? js.sha256Async(e) : qs("sha256")(e) : e), Ad = Oe(0), Id = ao(0), kd = ao(1), Bd = 1e3, Od = "drbg: tried max amount of iterations", $d = (e, t) => { - let n = Oe(ht), r = Oe(ht), o = 0; +}, $l = (e, t, n) => (Bt(e, void 0, "message"), t.prehash ? n ? lo.sha256Async(e) : Qa("sha256")(e) : e), Rl = Ue(0), Nl = fo(0), Ll = fo(1), _l = 1e3, Cl = "drbg: tried max amount of iterations", Pl = async (e, t) => { + let n = Ue(ht), r = Ue(ht), o = 0; const s = () => { n.fill(1), r.fill(0); - }, i = (...f) => qs("hmacSha256")(r, te(n, ...f)), c = (f = Ad) => { - r = i(Id, f), n = i(), f.length !== 0 && (r = i(kd, f), n = i()); - }, a = () => (o++ >= Bd && X(Od), n = i(), n); - s(), c(e); + }, i = (...f) => lo.hmacSha256Async(r, ee(n, ...f)), c = async (f = Rl) => { + r = await i(Nl, f), n = await i(), f.length !== 0 && (r = await i(Ll, f), n = await i()); + }, a = async () => (o++ >= _l && Q(Cl), n = await i(), n); + s(), await c(e); let u; - for (; !(u = t(a())); ) - c(); + for (; !(u = t(await a())); ) + await c(); return s(), u; -}, Ud = (e, t, n, r) => { +}, Vl = (e, t, n, r) => { let { lowS: o, extraEntropy: s } = n; - const i = Ut, c = vd(e), a = i(c), u = Ya(t), f = [i(u), a]; + const i = $t, c = Ul(e), a = i(c), u = eu(t), f = [i(u), a]; if (s != null && s !== !1) { - const w = s === !0 ? co(ht) : s; + const w = s === !0 ? uo(ht) : s; f.push(Bt(w, void 0, "extraEntropy")); } - const l = te(...f), d = c; - return r(l, (w) => { - const g = Xa(w); - if (!(1n <= g && g < Be)) + const d = ee(...f), l = c; + return r(d, (w) => { + const g = ru(w); + if (!(1n <= g && g < Oe)) return; - const y = Ga(g, Be), S = $e.multiply(g).toAffine(), v = ie(S.x); - if (v === 0n) + const y = Xa(g, Oe), S = $e.multiply(g).toAffine(), T = ce(S.x); + if (T === 0n) return; - const O = ie(y * ie(d + v * u)); - if (O === 0n) + const k = ce(y * ce(l + T * u)); + if (k === 0n) return; - let N = (S.x === v ? 0 : 2) | Number(S.y & 1n), U = O; - return o && Za(O) && (U = ie(-O), N ^= 1), new _r(v, U, N).toBytes(n.format); + let N = (S.x === T ? 0 : 2) | Number(S.y & 1n), $ = k; + return o && nu(k) && ($ = ce(-k), N ^= 1), new Hr(T, $, N).toBytes(n.format); }); -}, Rd = (e) => { +}, Hl = (e) => { const t = {}; - return Object.keys(Xi).forEach((n) => { - t[n] = e[n] ?? Xi[n]; + return Object.keys(ec).forEach((n) => { + t[n] = e[n] ?? ec[n]; }), t; -}, Nd = (e, t, n = {}) => (n = Rd(n), e = Td(e, n, !1), Ud(e, t, n, $d)), Ld = (e = co(Cr.seed)) => { - Bt(e), (e.length < Cr.seed || e.length > 1024) && X("expected 40-1024b"); - const t = P(Ue(e), Be - 1n); - return Ut(t + 1n); -}, Cd = (e) => (t) => { - const n = Ld(t); +}, Dl = async (e, t, n = {}) => (n = Hl(n), e = await $l(e, n, !0), Vl(e, t, n, Pl)), Ml = (e = uo(Vr.seed)) => { + Bt(e), (e.length < Vr.seed || e.length > 1024) && Q("expected 40-1024b"); + const t = P(Re(e), Oe - 1n); + return $t(t + 1n); +}, Kl = (e) => (t) => { + const n = Ml(t); return { secretKey: n, publicKey: e(n) }; -}, Ja = (e) => Uint8Array.from("BIP0340/" + e, (t) => t.charCodeAt(0)), tu = "aux", eu = "nonce", nu = "challenge", ts = (e, ...t) => { - const n = qs("sha256"), r = n(Ja(e)); - return n(te(r, r, ...t)); -}, es = async (e, ...t) => { - const n = js.sha256Async, r = await n(Ja(e)); - return await n(te(r, r, ...t)); -}, Ys = (e) => { - const t = Ya(e), n = $e.multiply(t), { x: r, y: o } = n.assertValidity().toAffine(), s = fn(o) ? t : ie(-t), i = Ut(r); +}, su = (e) => Uint8Array.from("BIP0340/" + e, (t) => t.charCodeAt(0)), iu = "aux", cu = "nonce", au = "challenge", is = (e, ...t) => { + const n = Qa("sha256"), r = n(su(e)); + return n(ee(r, r, ...t)); +}, cs = async (e, ...t) => { + const n = lo.sha256Async, r = await n(su(e)); + return await n(ee(r, r, ...t)); +}, Js = (e) => { + const t = eu(e), n = $e.multiply(t), { x: r, y: o } = n.assertValidity().toAffine(), s = fn(o) ? t : ce(-t), i = $t(r); return { d: s, px: i }; -}, Zs = (e) => ie(Ue(e)), ru = (...e) => Zs(ts(nu, ...e)), ou = async (...e) => Zs(await es(nu, ...e)), su = (e) => Ys(e).px, _d = Cd(su), iu = (e, t, n) => { - const { px: r, d: o } = Ys(t); +}, ti = (e) => ce(Re(e)), uu = (...e) => ti(is(au, ...e)), fu = async (...e) => ti(await cs(au, ...e)), du = (e) => Js(e).px, Fl = Kl(du), lu = (e, t, n) => { + const { px: r, d: o } = Js(t); return { m: Bt(e), px: r, d: o, a: Bt(n, ht) }; -}, cu = (e) => { - const t = Zs(e); - t === 0n && X("sign failed: k is zero"); - const { px: n, d: r } = Ys(Ut(t)); +}, hu = (e) => { + const t = ti(e); + t === 0n && Q("sign failed: k is zero"); + const { px: n, d: r } = Js($t(t)); return { rx: n, k: r }; -}, au = (e, t, n, r) => te(t, Ut(ie(e + n * r))), uu = "invalid signature produced", Pd = (e, t, n = co(ht)) => { - const { m: r, px: o, d: s, a: i } = iu(e, t, n), c = ts(tu, i), a = Ut(s ^ Ue(c)), u = ts(eu, a, o, r), { rx: f, k: l } = cu(u), d = ru(f, o, r), h = au(l, f, d, s); - return lu(h, r, o) || X(uu), h; -}, Hd = async (e, t, n = co(ht)) => { - const { m: r, px: o, d: s, a: i } = iu(e, t, n), c = await es(tu, i), a = Ut(s ^ Ue(c)), u = await es(eu, a, o, r), { rx: f, k: l } = cu(u), d = await ou(f, o, r), h = au(l, f, d, s); - return await du(h, r, o) || X(uu), h; -}, Vd = (e, t) => e instanceof Promise ? e.then(t) : t(e), fu = (e, t, n, r) => { - const o = Bt(e, We, "signature"), s = Bt(t, void 0, "message"), i = Bt(n, ht, "publicKey"); +}, pu = (e, t, n, r) => ee(t, $t(ce(e + n * r))), gu = "invalid signature produced", Wl = (e, t, n = uo(ht)) => { + const { m: r, px: o, d: s, a: i } = lu(e, t, n), c = is(iu, i), a = $t(s ^ Re(c)), u = is(cu, a, o, r), { rx: f, k: d } = hu(u), l = uu(f, o, r), h = pu(d, f, l, s); + return yu(h, r, o) || Q(gu), h; +}, zl = async (e, t, n = uo(ht)) => { + const { m: r, px: o, d: s, a: i } = lu(e, t, n), c = await cs(iu, i), a = $t(s ^ Re(c)), u = await cs(cu, a, o, r), { rx: f, k: d } = hu(u), l = await fu(f, o, r), h = pu(d, f, l, s); + return await mu(h, r, o) || Q(gu), h; +}, Gl = (e, t) => e instanceof Promise ? e.then(t) : t(e), wu = (e, t, n, r) => { + const o = Bt(e, ze, "signature"), s = Bt(t, void 0, "message"), i = Bt(n, ht, "publicKey"); try { - const c = Ue(i), a = ja(c), u = fn(a) ? a : P(-a), f = new Et(c, u, 1n).assertValidity(), l = Ut(f.toAffine().x), d = ln(o, 0, ht); - ze(d, 1n, ve); - const h = ln(o, ht, We); - ze(h, 1n, Be); - const w = te(Ut(d), l, s); - return Vd(r(w), (g) => { - const { x: y, y: S } = md(f, h, ie(-g)).toAffine(); - return !(!fn(S) || y !== d); + const c = Re(i), a = tu(c), u = fn(a) ? a : P(-a), f = new xt(c, u, 1n).assertValidity(), d = $t(f.toAffine().x), l = dn(o, 0, ht); + Ge(l, 1n, ve); + const h = dn(o, ht, ze); + Ge(h, 1n, Oe); + const w = ee($t(l), d, s); + return Gl(r(w), (g) => { + const { x: y, y: S } = Al(f, h, ce(-g)).toAffine(); + return !(!fn(S) || y !== l); }); } catch { return !1; } -}, lu = (e, t, n) => fu(e, t, n, ru), du = async (e, t, n) => fu(e, t, n, ou), Dd = { - keygen: _d, - getPublicKey: su, - sign: Pd, - verify: lu, - signAsync: Hd, - verifyAsync: du -}, Hr = 8, Md = 256, hu = Math.ceil(Md / Hr) + 1, ns = 2 ** (Hr - 1), Kd = () => { +}, yu = (e, t, n) => wu(e, t, n, uu), mu = async (e, t, n) => wu(e, t, n, fu), ql = { + keygen: Fl, + getPublicKey: du, + sign: Wl, + verify: yu, + signAsync: zl, + verifyAsync: mu +}, Mr = 8, jl = 256, bu = Math.ceil(jl / Mr) + 1, as = 2 ** (Mr - 1), Yl = () => { const e = []; let t = $e, n = t; - for (let r = 0; r < hu; r++) { + for (let r = 0; r < bu; r++) { n = t, e.push(n); - for (let o = 1; o < ns; o++) + for (let o = 1; o < as; o++) n = n.add(t), e.push(n); t = n.double(); } return e; }; -let Ji; -const tc = (e, t) => { +let rc; +const oc = (e, t) => { const n = t.negate(); return e ? n : t; -}, Fd = (e) => { - const t = Ji || (Ji = Kd()); - let n = Le, r = $e; - const o = 2 ** Hr, s = o, i = Vn(o - 1), c = Vn(Hr); - for (let a = 0; a < hu; a++) { +}, Zl = (e) => { + const t = rc || (rc = Yl()); + let n = _e, r = $e; + const o = 2 ** Mr, s = o, i = Mn(o - 1), c = Mn(Mr); + for (let a = 0; a < bu; a++) { let u = Number(e & i); - e >>= c, u > ns && (u -= s, e += 1n); - const f = a * ns, l = f, d = f + Math.abs(u) - 1, h = a % 2 !== 0, w = u < 0; - u === 0 ? r = r.add(tc(h, t[l])) : n = n.add(tc(w, t[d])); + e >>= c, u > as && (u -= s, e += 1n); + const f = a * as, d = f, l = f + Math.abs(u) - 1, h = a % 2 !== 0, w = u < 0; + u === 0 ? r = r.add(oc(h, t[d])) : n = n.add(oc(w, t[l])); } - return e !== 0n && X("invalid wnaf"), { p: n, f: r }; + return e !== 0n && Q("invalid wnaf"), { p: n, f: r }; }; -function Wd(e, t, n) { +function Xl(e, t, n) { return e & t ^ ~e & n; } -function zd(e, t, n) { +function Ql(e, t, n) { return e & t ^ e & n ^ t & n; } -class Gd { +class Jl { blockLen; outputLen; padOffset; @@ -5614,15 +5614,15 @@ class Gd { pos = 0; destroyed = !1; constructor(t, n, r, o) { - this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = vo(this.buffer); + this.blockLen = t, this.outputLen = n, this.padOffset = r, this.isLE = o, this.buffer = new Uint8Array(t), this.view = Io(this.buffer); } update(t) { - Ur(this), et(t); + Lr(this), et(t); const { view: n, buffer: r, blockLen: o } = this, s = t.length; for (let i = 0; i < s; ) { const c = Math.min(o - this.pos, s - i); if (c === o) { - const a = vo(t); + const a = Io(t); for (; o <= s - i; i += o) this.process(a, i); continue; @@ -5632,21 +5632,21 @@ class Gd { return this.length += t.length, this.roundClean(), this; } digestInto(t) { - Ur(this), ed(t, this), this.finished = !0; + Lr(this), al(t, this), this.finished = !0; const { buffer: n, view: r, blockLen: o, isLE: s } = this; let { pos: i } = this; - n[i++] = 128, Rr(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(r, 0), i = 0); - for (let l = i; l < o; l++) - n[l] = 0; + n[i++] = 128, _r(this.buffer.subarray(i)), this.padOffset > o - i && (this.process(r, 0), i = 0); + for (let d = i; d < o; d++) + n[d] = 0; r.setBigUint64(o - 8, BigInt(this.length * 8), s), this.process(r, 0); - const c = vo(t), a = this.outputLen; + const c = Io(t), a = this.outputLen; if (a % 4) throw new Error("_sha2: outputLen must be aligned to 32bit"); const u = a / 4, f = this.get(); if (u > f.length) throw new Error("_sha2: outputLen bigger than state"); - for (let l = 0; l < u; l++) - c.setUint32(4 * l, f[l], s); + for (let d = 0; d < u; d++) + c.setUint32(4 * d, f[d], s); } digest() { const { buffer: t, outputLen: n } = this; @@ -5672,7 +5672,7 @@ const we = /* @__PURE__ */ Uint32Array.from([ 2600822924, 528734635, 1541459225 -]), qd = /* @__PURE__ */ Uint32Array.from([ +]), th = /* @__PURE__ */ Uint32Array.from([ 1116352408, 1899447441, 3049323471, @@ -5738,7 +5738,7 @@ const we = /* @__PURE__ */ Uint32Array.from([ 3204031479, 3329325298 ]), ye = /* @__PURE__ */ new Uint32Array(64); -class jd extends Gd { +class eh extends Jl { constructor(t) { super(64, t, 8, !1); } @@ -5751,27 +5751,27 @@ class jd extends Gd { this.A = t | 0, this.B = n | 0, this.C = r | 0, this.D = o | 0, this.E = s | 0, this.F = i | 0, this.G = c | 0, this.H = a | 0; } process(t, n) { - for (let l = 0; l < 16; l++, n += 4) - ye[l] = t.getUint32(n, !1); - for (let l = 16; l < 64; l++) { - const d = ye[l - 15], h = ye[l - 2], w = jt(d, 7) ^ jt(d, 18) ^ d >>> 3, g = jt(h, 17) ^ jt(h, 19) ^ h >>> 10; - ye[l] = g + ye[l - 7] + w + ye[l - 16] | 0; + for (let d = 0; d < 16; d++, n += 4) + ye[d] = t.getUint32(n, !1); + for (let d = 16; d < 64; d++) { + const l = ye[d - 15], h = ye[d - 2], w = Yt(l, 7) ^ Yt(l, 18) ^ l >>> 3, g = Yt(h, 17) ^ Yt(h, 19) ^ h >>> 10; + ye[d] = g + ye[d - 7] + w + ye[d - 16] | 0; } let { A: r, B: o, C: s, D: i, E: c, F: a, G: u, H: f } = this; - for (let l = 0; l < 64; l++) { - const d = jt(c, 6) ^ jt(c, 11) ^ jt(c, 25), h = f + d + Wd(c, a, u) + qd[l] + ye[l] | 0, g = (jt(r, 2) ^ jt(r, 13) ^ jt(r, 22)) + zd(r, o, s) | 0; + for (let d = 0; d < 64; d++) { + const l = Yt(c, 6) ^ Yt(c, 11) ^ Yt(c, 25), h = f + l + Xl(c, a, u) + th[d] + ye[d] | 0, g = (Yt(r, 2) ^ Yt(r, 13) ^ Yt(r, 22)) + Ql(r, o, s) | 0; f = u, u = a, a = c, c = i + h | 0, i = s, s = o, o = r, r = h + g | 0; } r = r + this.A | 0, o = o + this.B | 0, s = s + this.C | 0, i = i + this.D | 0, c = c + this.E | 0, a = a + this.F | 0, u = u + this.G | 0, f = f + this.H | 0, this.set(r, o, s, i, c, a, u, f); } roundClean() { - Rr(ye); + _r(ye); } destroy() { - this.set(0, 0, 0, 0, 0, 0, 0, 0), Rr(this.buffer); + this.set(0, 0, 0, 0, 0, 0, 0, 0), _r(this.buffer); } } -class Yd extends jd { +class nh extends eh { // We cannot use array here since array allows indexing by variable // which means optimizer/compiler cannot use registers. A = we[0] | 0; @@ -5786,96 +5786,96 @@ class Yd extends jd { super(32); } } -const rs = /* @__PURE__ */ rd( - () => new Yd(), - /* @__PURE__ */ od(1) +const us = /* @__PURE__ */ fl( + () => new nh(), + /* @__PURE__ */ dl(1) ); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const St = /* @__PURE__ */ BigInt(0), bt = /* @__PURE__ */ BigInt(1), Ve = /* @__PURE__ */ BigInt(2), pu = /* @__PURE__ */ BigInt(3), gu = /* @__PURE__ */ BigInt(4), wu = /* @__PURE__ */ BigInt(5), Zd = /* @__PURE__ */ BigInt(7), yu = /* @__PURE__ */ BigInt(8), Xd = /* @__PURE__ */ BigInt(9), mu = /* @__PURE__ */ BigInt(16); +const Tt = /* @__PURE__ */ BigInt(0), Et = /* @__PURE__ */ BigInt(1), De = /* @__PURE__ */ BigInt(2), Eu = /* @__PURE__ */ BigInt(3), xu = /* @__PURE__ */ BigInt(4), Su = /* @__PURE__ */ BigInt(5), rh = /* @__PURE__ */ BigInt(7), Tu = /* @__PURE__ */ BigInt(8), oh = /* @__PURE__ */ BigInt(9), vu = /* @__PURE__ */ BigInt(16); function Dt(e, t) { const n = e % t; - return n >= St ? n : t + n; + return n >= Tt ? n : t + n; } function Nt(e, t, n) { let r = e; - for (; t-- > St; ) + for (; t-- > Tt; ) r *= r, r %= n; return r; } -function ec(e, t) { - if (e === St) +function sc(e, t) { + if (e === Tt) throw new Error("invert: expected non-zero number"); - if (t <= St) + if (t <= Tt) throw new Error("invert: expected positive modulus, got " + t); - let n = Dt(e, t), r = t, o = St, s = bt; - for (; n !== St; ) { + let n = Dt(e, t), r = t, o = Tt, s = Et; + for (; n !== Tt; ) { const c = r / n, a = r % n, u = o - s * c; r = n, n = a, o = s, s = u; } - if (r !== bt) + if (r !== Et) throw new Error("invert: does not exist"); return Dt(o, t); } -function Xs(e, t, n) { +function ei(e, t, n) { if (!e.eql(e.sqr(t), n)) throw new Error("Cannot find square root"); } -function bu(e, t) { - const n = (e.ORDER + bt) / gu, r = e.pow(t, n); - return Xs(e, r, t), r; +function Au(e, t) { + const n = (e.ORDER + Et) / xu, r = e.pow(t, n); + return ei(e, r, t), r; } -function Qd(e, t) { - const n = (e.ORDER - wu) / yu, r = e.mul(t, Ve), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, Ve), o), c = e.mul(s, e.sub(i, e.ONE)); - return Xs(e, c, t), c; +function sh(e, t) { + const n = (e.ORDER - Su) / Tu, r = e.mul(t, De), o = e.pow(r, n), s = e.mul(t, o), i = e.mul(e.mul(s, De), o), c = e.mul(s, e.sub(i, e.ONE)); + return ei(e, c, t), c; } -function Jd(e) { - const t = uo(e), n = Eu(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + Zd) / mu; +function ih(e) { + const t = ho(e), n = Iu(e), r = n(t, t.neg(t.ONE)), o = n(t, r), s = n(t, t.neg(r)), i = (e + rh) / vu; return (c, a) => { let u = c.pow(a, i), f = c.mul(u, r); - const l = c.mul(u, o), d = c.mul(u, s), h = c.eql(c.sqr(f), a), w = c.eql(c.sqr(l), a); - u = c.cmov(u, f, h), f = c.cmov(d, l, w); + const d = c.mul(u, o), l = c.mul(u, s), h = c.eql(c.sqr(f), a), w = c.eql(c.sqr(d), a); + u = c.cmov(u, f, h), f = c.cmov(l, d, w); const g = c.eql(c.sqr(f), a), y = c.cmov(u, f, g); - return Xs(c, y, a), y; + return ei(c, y, a), y; }; } -function Eu(e) { - if (e < pu) +function Iu(e) { + if (e < Eu) throw new Error("sqrt is not defined for small field"); - let t = e - bt, n = 0; - for (; t % Ve === St; ) - t /= Ve, n++; - let r = Ve; - const o = uo(e); - for (; nc(o, r) === 1; ) + let t = e - Et, n = 0; + for (; t % De === Tt; ) + t /= De, n++; + let r = De; + const o = ho(e); + for (; ic(o, r) === 1; ) if (r++ > 1e3) throw new Error("Cannot find square root: probably non-prime P"); if (n === 1) - return bu; + return Au; let s = o.pow(r, t); - const i = (t + bt) / Ve; + const i = (t + Et) / De; return function(a, u) { if (a.is0(u)) return u; - if (nc(a, u) !== 1) + if (ic(a, u) !== 1) throw new Error("Cannot find square root"); - let f = n, l = a.mul(a.ONE, s), d = a.pow(u, t), h = a.pow(u, i); - for (; !a.eql(d, a.ONE); ) { - if (a.is0(d)) + let f = n, d = a.mul(a.ONE, s), l = a.pow(u, t), h = a.pow(u, i); + for (; !a.eql(l, a.ONE); ) { + if (a.is0(l)) return a.ZERO; - let w = 1, g = a.sqr(d); + let w = 1, g = a.sqr(l); for (; !a.eql(g, a.ONE); ) if (w++, g = a.sqr(g), w === f) throw new Error("Cannot find square root"); - const y = bt << BigInt(f - w - 1), S = a.pow(l, y); - f = w, l = a.sqr(S), d = a.mul(d, l), h = a.mul(h, S); + const y = Et << BigInt(f - w - 1), S = a.pow(d, y); + f = w, d = a.sqr(S), l = a.mul(l, d), h = a.mul(h, S); } return h; }; } -function th(e) { - return e % gu === pu ? bu : e % yu === wu ? Qd : e % mu === Xd ? Jd(e) : Eu(e); +function ch(e) { + return e % xu === Eu ? Au : e % Tu === Su ? sh : e % vu === oh ? ih(e) : Iu(e); } -const eh = [ +const ah = [ "create", "isValid", "is0", @@ -5894,58 +5894,58 @@ const eh = [ "mulN", "sqrN" ]; -function nh(e) { +function uh(e) { const t = { ORDER: "bigint", BYTES: "number", BITS: "number" - }, n = eh.reduce((r, o) => (r[o] = "function", r), t); - return Gs(e, n), e; + }, n = ah.reduce((r, o) => (r[o] = "function", r), t); + return Qs(e, n), e; } -function rh(e, t, n) { - if (n < St) +function fh(e, t, n) { + if (n < Tt) throw new Error("invalid exponent, negatives unsupported"); - if (n === St) + if (n === Tt) return e.ONE; - if (n === bt) + if (n === Et) return t; let r = e.ONE, o = t; - for (; n > St; ) - n & bt && (r = e.mul(r, o)), o = e.sqr(o), n >>= bt; + for (; n > Tt; ) + n & Et && (r = e.mul(r, o)), o = e.sqr(o), n >>= Et; return r; } -function xu(e, t, n = !1) { +function ku(e, t, n = !1) { const r = new Array(t.length).fill(n ? e.ZERO : void 0), o = t.reduce((i, c, a) => e.is0(c) ? i : (r[a] = i, e.mul(i, c)), e.ONE), s = e.inv(o); return t.reduceRight((i, c, a) => e.is0(c) ? i : (r[a] = e.mul(i, r[a]), e.mul(i, c)), s), r; } -function nc(e, t) { - const n = (e.ORDER - bt) / Ve, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); +function ic(e, t) { + const n = (e.ORDER - Et) / De, r = e.pow(t, n), o = e.eql(r, e.ONE), s = e.eql(r, e.ZERO), i = e.eql(r, e.neg(e.ONE)); if (!o && !s && !i) throw new Error("invalid Legendre symbol result"); return o ? 1 : s ? 0 : -1; } -function oh(e, t) { - t !== void 0 && Fe(t); +function dh(e, t) { + t !== void 0 && We(t); const n = t !== void 0 ? t : e.toString(2).length, r = Math.ceil(n / 8); return { nBitLength: n, nByteLength: r }; } -class sh { +class lh { ORDER; BITS; BYTES; isLE; - ZERO = St; - ONE = bt; + ZERO = Tt; + ONE = Et; _lengths; _sqrt; // cached sqrt _mod; constructor(t, n = {}) { - if (t <= St) + if (t <= Tt) throw new Error("invalid field: expected ORDER > 0, got " + t); let r; this.isLE = !1, n != null && typeof n == "object" && (typeof n.BITS == "number" && (r = n.BITS), typeof n.sqrt == "function" && (this.sqrt = n.sqrt), typeof n.isLE == "boolean" && (this.isLE = n.isLE), n.allowedLengths && (this._lengths = n.allowedLengths?.slice()), typeof n.modFromBytes == "boolean" && (this._mod = n.modFromBytes)); - const { nBitLength: o, nByteLength: s } = oh(t, r); + const { nBitLength: o, nByteLength: s } = dh(t, r); if (s > 2048) throw new Error("invalid field: expected ORDER of <= 2048 bytes"); this.ORDER = t, this.BITS = o, this.BYTES = s, this._sqrt = void 0, Object.preventExtensions(this); @@ -5956,17 +5956,17 @@ class sh { isValid(t) { if (typeof t != "bigint") throw new Error("invalid field element: expected bigint, got " + typeof t); - return St <= t && t < this.ORDER; + return Tt <= t && t < this.ORDER; } is0(t) { - return t === St; + return t === Tt; } // is valid and invertible isValidNot0(t) { return !this.is0(t) && this.isValid(t); } isOdd(t) { - return (t & bt) === bt; + return (t & Et) === Et; } neg(t) { return Dt(-t, this.ORDER); @@ -5987,10 +5987,10 @@ class sh { return Dt(t * n, this.ORDER); } pow(t, n) { - return rh(this, t, n); + return fh(this, t, n); } div(t, n) { - return Dt(t * ec(n, this.ORDER), this.ORDER); + return Dt(t * sc(n, this.ORDER), this.ORDER); } // Same as above, but doesn't normalize sqrN(t) { @@ -6006,13 +6006,13 @@ class sh { return t * n; } inv(t) { - return ec(t, this.ORDER); + return sc(t, this.ORDER); } sqrt(t) { - return this._sqrt || (this._sqrt = th(this.ORDER)), this._sqrt(this, t); + return this._sqrt || (this._sqrt = ch(this.ORDER)), this._sqrt(this, t); } toBytes(t) { - return this.isLE ? Va(t, this.BYTES) : Ws(t, this.BYTES); + return this.isLE ? Wa(t, this.BYTES) : Zs(t, this.BYTES); } fromBytes(t, n = !1) { et(t); @@ -6025,14 +6025,14 @@ class sh { } if (t.length !== o) throw new Error("Field.fromBytes: expected " + o + " bytes, got " + t.length); - let a = s ? Ha(t) : bn(t); + let a = s ? Fa(t) : En(t); if (c && (a = Dt(a, i)), !n && !this.isValid(a)) throw new Error("invalid field element: outside of range 0..ORDER"); return a; } // TODO: we don't need it here, move out to separate fn invertBatch(t) { - return xu(this, t); + return ku(this, t); } // We can't move this out because Fp6, Fp12 implement it // and it's unclear what to return in there. @@ -6040,62 +6040,62 @@ class sh { return r ? n : t; } } -function uo(e, t = {}) { - return new sh(e, t); +function ho(e, t = {}) { + return new lh(e, t); } -function Su(e) { +function Bu(e) { if (typeof e != "bigint") throw new Error("field order must be bigint"); const t = e.toString(2).length; return Math.ceil(t / 8); } -function vu(e) { - const t = Su(e); +function Ou(e) { + const t = Bu(e); return t + Math.ceil(t / 2); } -function Tu(e, t, n = !1) { +function Uu(e, t, n = !1) { et(e); - const r = e.length, o = Su(t), s = vu(t); + const r = e.length, o = Bu(t), s = Ou(t); if (r < 16 || r < s || r > 1024) throw new Error("expected " + s + "-1024 bytes of input, got " + r); - const i = n ? Ha(e) : bn(e), c = Dt(i, t - bt) + bt; - return n ? Va(c, o) : Ws(c, o); + const i = n ? Fa(e) : En(e), c = Dt(i, t - Et) + Et; + return n ? Wa(c, o) : Zs(c, o); } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const hn = /* @__PURE__ */ BigInt(0), De = /* @__PURE__ */ BigInt(1); -function Vr(e, t) { +const hn = /* @__PURE__ */ BigInt(0), Me = /* @__PURE__ */ BigInt(1); +function Kr(e, t) { const n = t.negate(); return e ? n : t; } -function rc(e, t) { - const n = xu(e.Fp, t.map((r) => r.Z)); +function cc(e, t) { + const n = ku(e.Fp, t.map((r) => r.Z)); return t.map((r, o) => e.fromAffine(r.toAffine(n[o]))); } -function Au(e, t) { +function $u(e, t) { if (!Number.isSafeInteger(e) || e <= 0 || e > t) throw new Error("invalid window size, expected [1.." + t + "], got W=" + e); } -function Ao(e, t) { - Au(e, t); - const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = zs(e), i = BigInt(e); +function Bo(e, t) { + $u(e, t); + const n = Math.ceil(t / e) + 1, r = 2 ** (e - 1), o = 2 ** e, s = Xs(e), i = BigInt(e); return { windows: n, windowSize: r, mask: s, maxNumber: o, shiftBy: i }; } -function oc(e, t, n) { +function ac(e, t, n) { const { windowSize: r, mask: o, maxNumber: s, shiftBy: i } = n; let c = Number(e & o), a = e >> i; - c > r && (c -= s, a += De); - const u = t * r, f = u + Math.abs(c) - 1, l = c === 0, d = c < 0, h = t % 2 !== 0; - return { nextN: a, offset: f, isZero: l, isNeg: d, isNegF: h, offsetF: u }; + c > r && (c -= s, a += Me); + const u = t * r, f = u + Math.abs(c) - 1, d = c === 0, l = c < 0, h = t % 2 !== 0; + return { nextN: a, offset: f, isZero: d, isNeg: l, isNegF: h, offsetF: u }; } -const Io = /* @__PURE__ */ new WeakMap(), Iu = /* @__PURE__ */ new WeakMap(); -function ko(e) { - return Iu.get(e) || 1; +const Oo = /* @__PURE__ */ new WeakMap(), Ru = /* @__PURE__ */ new WeakMap(); +function Uo(e) { + return Ru.get(e) || 1; } -function sc(e) { +function uc(e) { if (e !== hn) throw new Error("invalid wNAF"); } -class ih { +class hh { BASE; ZERO; Fn; @@ -6108,7 +6108,7 @@ class ih { _unsafeLadder(t, n, r = this.ZERO) { let o = t; for (; n > hn; ) - n & De && (r = r.add(o)), o = o.double(), n >>= De; + n & Me && (r = r.add(o)), o = o.double(), n >>= Me; return r; } /** @@ -6124,7 +6124,7 @@ class ih { * @returns precomputed point tables flattened to a single array */ precomputeWindow(t, n) { - const { windows: r, windowSize: o } = Ao(n, this.bits), s = []; + const { windows: r, windowSize: o } = Bo(n, this.bits), s = []; let i = t, c = i; for (let a = 0; a < r; a++) { c = i, s.push(c); @@ -6144,12 +6144,12 @@ class ih { if (!this.Fn.isValid(r)) throw new Error("invalid scalar"); let o = this.ZERO, s = this.BASE; - const i = Ao(t, this.bits); + const i = Bo(t, this.bits); for (let c = 0; c < i.windows; c++) { - const { nextN: a, offset: u, isZero: f, isNeg: l, isNegF: d, offsetF: h } = oc(r, c, i); - r = a, f ? s = s.add(Vr(d, n[h])) : o = o.add(Vr(l, n[u])); + const { nextN: a, offset: u, isZero: f, isNeg: d, isNegF: l, offsetF: h } = ac(r, c, i); + r = a, f ? s = s.add(Kr(l, n[h])) : o = o.add(Kr(d, n[u])); } - return sc(r), { p: o, f: s }; + return uc(r), { p: o, f: s }; } /** * Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form. @@ -6157,53 +6157,53 @@ class ih { * @returns point */ wNAFUnsafe(t, n, r, o = this.ZERO) { - const s = Ao(t, this.bits); + const s = Bo(t, this.bits); for (let i = 0; i < s.windows && r !== hn; i++) { - const { nextN: c, offset: a, isZero: u, isNeg: f } = oc(r, i, s); + const { nextN: c, offset: a, isZero: u, isNeg: f } = ac(r, i, s); if (r = c, !u) { - const l = n[a]; - o = o.add(f ? l.negate() : l); + const d = n[a]; + o = o.add(f ? d.negate() : d); } } - return sc(r), o; + return uc(r), o; } getPrecomputes(t, n, r) { - let o = Io.get(n); - return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), Io.set(n, o))), o; + let o = Oo.get(n); + return o || (o = this.precomputeWindow(n, t), t !== 1 && (typeof r == "function" && (o = r(o)), Oo.set(n, o))), o; } cached(t, n, r) { - const o = ko(t); + const o = Uo(t); return this.wNAF(o, this.getPrecomputes(o, t, r), n); } unsafe(t, n, r, o) { - const s = ko(t); + const s = Uo(t); return s === 1 ? this._unsafeLadder(t, n, o) : this.wNAFUnsafe(s, this.getPrecomputes(s, t, r), n, o); } // We calculate precomputes for elliptic curve point multiplication // using windowed method. This specifies window size and // stores precomputed values. Usually only base point would be precomputed. createCache(t, n) { - Au(n, this.bits), Iu.set(t, n), Io.delete(t); + $u(n, this.bits), Ru.set(t, n), Oo.delete(t); } hasCache(t) { - return ko(t) !== 1; + return Uo(t) !== 1; } } -function ch(e, t, n, r) { +function ph(e, t, n, r) { let o = t, s = e.ZERO, i = e.ZERO; for (; n > hn || r > hn; ) - n & De && (s = s.add(o)), r & De && (i = i.add(o)), o = o.double(), n >>= De, r >>= De; + n & Me && (s = s.add(o)), r & Me && (i = i.add(o)), o = o.double(), n >>= Me, r >>= Me; return { p1: s, p2: i }; } -function ic(e, t, n) { +function fc(e, t, n) { if (t) { if (t.ORDER !== e) throw new Error("Field.ORDER must match order: Fp == p, Fn == n"); - return nh(t), t; + return uh(t), t; } else - return uo(e, { isLE: n }); + return ho(e, { isLE: n }); } -function ah(e, t, n = {}, r) { +function gh(e, t, n = {}, r) { if (r === void 0 && (r = e === "edwards"), !t || typeof t != "object") throw new Error(`expected valid ${e} CURVE object`); for (const a of ["p", "n", "h"]) { @@ -6211,19 +6211,19 @@ function ah(e, t, n = {}, r) { if (!(typeof u == "bigint" && u > hn)) throw new Error(`CURVE.${a} must be positive bigint`); } - const o = ic(t.p, n.Fp, r), s = ic(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; + const o = fc(t.p, n.Fp, r), s = fc(t.n, n.Fn, r), c = ["Gx", "Gy", "a", "b"]; for (const a of c) if (!o.isValid(t[a])) throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`); return t = Object.freeze(Object.assign({}, t)), { CURVE: t, Fp: o, Fn: s }; } -function ku(e, t) { +function Nu(e, t) { return function(r) { const o = e(r); return { secretKey: o, publicKey: t(o) }; }; } -class Bu { +class Lu { oHash; iHash; blockLen; @@ -6231,7 +6231,7 @@ class Bu { finished = !1; destroyed = !1; constructor(t, n) { - if (La(t), et(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") + if (Ha(t), et(n, void 0, "key"), this.iHash = t.create(), typeof this.iHash.update != "function") throw new Error("Expected instance of class which extends utils.Hash"); this.blockLen = this.iHash.blockLen, this.outputLen = this.iHash.outputLen; const r = this.blockLen, o = new Uint8Array(r); @@ -6241,13 +6241,13 @@ class Bu { this.iHash.update(o), this.oHash = t.create(); for (let s = 0; s < o.length; s++) o[s] ^= 106; - this.oHash.update(o), Rr(o); + this.oHash.update(o), _r(o); } update(t) { - return Ur(this), this.iHash.update(t), this; + return Lr(this), this.iHash.update(t), this; } digestInto(t) { - Ur(this), et(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); + Lr(this), et(t, this.outputLen, "output"), this.finished = !0, this.iHash.digestInto(t), this.oHash.update(t), this.oHash.digestInto(t), this.destroy(); } digest() { const t = new Uint8Array(this.oHash.outputLen); @@ -6265,39 +6265,39 @@ class Bu { this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy(); } } -const Ou = (e, t, n) => new Bu(e, t).update(n).digest(); -Ou.create = (e, t) => new Bu(e, t); +const _u = (e, t, n) => new Lu(e, t).update(n).digest(); +_u.create = (e, t) => new Lu(e, t); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const cc = (e, t) => (e + (e >= 0 ? t : -t) / $u) / t; -function uh(e, t, n) { - const [[r, o], [s, i]] = t, c = cc(i * e, n), a = cc(-o * e, n); +const dc = (e, t) => (e + (e >= 0 ? t : -t) / Cu) / t; +function wh(e, t, n) { + const [[r, o], [s, i]] = t, c = dc(i * e, n), a = dc(-o * e, n); let u = e - c * r - a * s, f = -c * o - a * i; - const l = u < ce, d = f < ce; - l && (u = -u), d && (f = -f); - const h = zs(Math.ceil(ud(n) / 2)) + tn; - if (u < ce || u >= h || f < ce || f >= h) + const d = u < ae, l = f < ae; + d && (u = -u), l && (f = -f); + const h = Xs(Math.ceil(wl(n) / 2)) + en; + if (u < ae || u >= h || f < ae || f >= h) throw new Error("splitScalar (endomorphism): failed, k=" + e); - return { k1neg: l, k1: u, k2neg: d, k2: f }; + return { k1neg: d, k1: u, k2neg: l, k2: f }; } -function os(e) { +function fs(e) { if (!["compact", "recovered", "der"].includes(e)) throw new Error('Signature format must be "compact", "recovered", or "der"'); return e; } -function Bo(e, t) { +function $o(e, t) { const n = {}; for (let r of Object.keys(t)) n[r] = e[r] === void 0 ? t[r] : e[r]; - return Lr(n.lowS, "lowS"), Lr(n.prehash, "prehash"), n.format !== void 0 && os(n.format), n; + return Pr(n.lowS, "lowS"), Pr(n.prehash, "prehash"), n.format !== void 0 && fs(n.format), n; } -class fh extends Error { +class yh extends Error { constructor(t = "") { super(t); } } const be = { // asn.1 DER encoding utils - Err: fh, + Err: yh, // Basic building block is TLV (Tag-Length-Value) _tlv: { encode: (e, t) => { @@ -6306,11 +6306,11 @@ const be = { throw new n("tlv.encode: wrong tag"); if (t.length & 1) throw new n("tlv.encode: unpadded data"); - const r = t.length / 2, o = rr(r); + const r = t.length / 2, o = or(r); if (o.length / 2 & 128) throw new n("tlv.encode: long form length too big"); - const s = r > 127 ? rr(o.length / 2 | 128) : ""; - return rr(e) + s + o + t; + const s = r > 127 ? or(o.length / 2 | 128) : ""; + return or(e) + s + o + t; }, // v - value, l - left bytes (unparsed) decode(e, t) { @@ -6353,9 +6353,9 @@ const be = { _int: { encode(e) { const { Err: t } = be; - if (e < ce) + if (e < ae) throw new t("integer: negative integers are not allowed"); - let n = rr(e); + let n = or(e); if (Number.parseInt(n[0], 16) & 8 && (n = "00" + n), n.length & 1) throw new t("unexpected DER parsing assertion: unpadded hex"); return n; @@ -6366,7 +6366,7 @@ const be = { throw new t("invalid signature integer: negative"); if (e[0] === 0 && !(e[1] & 128)) throw new t("invalid signature integer: unnecessary leading zero"); - return bn(e); + return En(e); } }, toSig(e) { @@ -6382,12 +6382,12 @@ const be = { const { _tlv: t, _int: n } = be, r = t.encode(2, n.encode(e.r)), o = t.encode(2, n.encode(e.s)), s = r + o; return t.encode(48, s); } -}, ce = BigInt(0), tn = BigInt(1), $u = BigInt(2), or = BigInt(3), lh = BigInt(4); -function dh(e, t = {}) { - const n = ah("weierstrass", e, t), { Fp: r, Fn: o } = n; +}, ae = BigInt(0), en = BigInt(1), Cu = BigInt(2), sr = BigInt(3), mh = BigInt(4); +function bh(e, t = {}) { + const n = gh("weierstrass", e, t), { Fp: r, Fn: o } = n; let s = n.CURVE; const { h: i, n: c } = s; - Gs(t, {}, { + Qs(t, {}, { allowInfinityPoint: "boolean", clearCofactor: "function", isTorsionFree: "function", @@ -6398,109 +6398,109 @@ function dh(e, t = {}) { const { endo: a } = t; if (a && (!r.is0(s.a) || typeof a.beta != "bigint" || !Array.isArray(a.basises))) throw new Error('invalid endo: expected "beta": bigint and "basises": array'); - const u = Ru(r, o); + const u = Vu(r, o); function f() { if (!r.isOdd) throw new Error("compression is not supported: Field does not have .isOdd()"); } - function l(_, b, m) { + function d(C, b, m) { const { x: p, y: E } = b.toAffine(), A = r.toBytes(p); - if (Lr(m, "isCompressed"), m) { + if (Pr(m, "isCompressed"), m) { f(); - const k = !r.isOdd(E); - return Xt(Uu(k), A); + const B = !r.isOdd(E); + return Qt(Pu(B), A); } else - return Xt(Uint8Array.of(4), A, r.toBytes(E)); + return Qt(Uint8Array.of(4), A, r.toBytes(E)); } - function d(_) { - et(_, void 0, "Point"); - const { publicKey: b, publicKeyUncompressed: m } = u, p = _.length, E = _[0], A = _.subarray(1); + function l(C) { + et(C, void 0, "Point"); + const { publicKey: b, publicKeyUncompressed: m } = u, p = C.length, E = C[0], A = C.subarray(1); if (p === b && (E === 2 || E === 3)) { - const k = r.fromBytes(A); - if (!r.isValid(k)) + const B = r.fromBytes(A); + if (!r.isValid(B)) throw new Error("bad point: is not on curve, wrong x"); - const I = g(k); - let T; + const I = g(B); + let v; try { - T = r.sqrt(I); - } catch (F) { - const H = F instanceof Error ? ": " + F.message : ""; - throw new Error("bad point: is not on curve, sqrt error" + H); + v = r.sqrt(I); + } catch (W) { + const V = W instanceof Error ? ": " + W.message : ""; + throw new Error("bad point: is not on curve, sqrt error" + V); } f(); - const B = r.isOdd(T); - return (E & 1) === 1 !== B && (T = r.neg(T)), { x: k, y: T }; + const O = r.isOdd(v); + return (E & 1) === 1 !== O && (v = r.neg(v)), { x: B, y: v }; } else if (p === m && E === 4) { - const k = r.BYTES, I = r.fromBytes(A.subarray(0, k)), T = r.fromBytes(A.subarray(k, k * 2)); - if (!y(I, T)) + const B = r.BYTES, I = r.fromBytes(A.subarray(0, B)), v = r.fromBytes(A.subarray(B, B * 2)); + if (!y(I, v)) throw new Error("bad point: is not on curve"); - return { x: I, y: T }; + return { x: I, y: v }; } else throw new Error(`bad point: got length ${p}, expected compressed=${b} or uncompressed=${m}`); } - const h = t.toBytes || l, w = t.fromBytes || d; - function g(_) { - const b = r.sqr(_), m = r.mul(b, _); - return r.add(r.add(m, r.mul(_, s.a)), s.b); + const h = t.toBytes || d, w = t.fromBytes || l; + function g(C) { + const b = r.sqr(C), m = r.mul(b, C); + return r.add(r.add(m, r.mul(C, s.a)), s.b); } - function y(_, b) { - const m = r.sqr(b), p = g(_); + function y(C, b) { + const m = r.sqr(b), p = g(C); return r.eql(m, p); } if (!y(s.Gx, s.Gy)) throw new Error("bad curve params: generator point"); - const S = r.mul(r.pow(s.a, or), lh), v = r.mul(r.sqr(s.b), BigInt(27)); - if (r.is0(r.add(S, v))) + const S = r.mul(r.pow(s.a, sr), mh), T = r.mul(r.sqr(s.b), BigInt(27)); + if (r.is0(r.add(S, T))) throw new Error("bad curve params: a or b"); - function O(_, b, m = !1) { + function k(C, b, m = !1) { if (!r.isValid(b) || m && r.is0(b)) - throw new Error(`bad point coordinate ${_}`); + throw new Error(`bad point coordinate ${C}`); return b; } - function N(_) { - if (!(_ instanceof C)) + function N(C) { + if (!(C instanceof L)) throw new Error("Weierstrass Point expected"); } - function U(_) { + function $(C) { if (!a || !a.basises) throw new Error("no endo"); - return uh(_, a.basises, o.ORDER); + return wh(C, a.basises, o.ORDER); } - const W = Gi((_, b) => { - const { X: m, Y: p, Z: E } = _; + const F = Zi((C, b) => { + const { X: m, Y: p, Z: E } = C; if (r.eql(E, r.ONE)) return { x: m, y: p }; - const A = _.is0(); + const A = C.is0(); b == null && (b = A ? r.ONE : r.inv(E)); - const k = r.mul(m, b), I = r.mul(p, b), T = r.mul(E, b); + const B = r.mul(m, b), I = r.mul(p, b), v = r.mul(E, b); if (A) return { x: r.ZERO, y: r.ZERO }; - if (!r.eql(T, r.ONE)) + if (!r.eql(v, r.ONE)) throw new Error("invZ was invalid"); - return { x: k, y: I }; - }), x = Gi((_) => { - if (_.is0()) { - if (t.allowInfinityPoint && !r.is0(_.Y)) + return { x: B, y: I }; + }), x = Zi((C) => { + if (C.is0()) { + if (t.allowInfinityPoint && !r.is0(C.Y)) return; throw new Error("bad point: ZERO"); } - const { x: b, y: m } = _.toAffine(); + const { x: b, y: m } = C.toAffine(); if (!r.isValid(b) || !r.isValid(m)) throw new Error("bad point: x or y not field elements"); if (!y(b, m)) throw new Error("bad point: equation left != right"); - if (!_.isTorsionFree()) + if (!C.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup"); return !0; }); - function Q(_, b, m, p, E) { - return m = new C(r.mul(m.X, _), m.Y, m.Z), b = Vr(p, b), m = Vr(E, m), b.add(m); + function Y(C, b, m, p, E) { + return m = new L(r.mul(m.X, C), m.Y, m.Z), b = Kr(p, b), m = Kr(E, m), b.add(m); } - class C { + class L { // base / generator point - static BASE = new C(s.Gx, s.Gy, r.ONE); + static BASE = new L(s.Gx, s.Gy, r.ONE); // zero / infinity / identity point - static ZERO = new C(r.ZERO, r.ONE, r.ZERO); + static ZERO = new L(r.ZERO, r.ONE, r.ZERO); // 0, 1, 0 // math field static Fp = r; @@ -6511,7 +6511,7 @@ function dh(e, t = {}) { Z; /** Does NOT validate if the point is valid. Use `.assertValidity()`. */ constructor(b, m, p) { - this.X = O("x", b), this.Y = O("y", m, !0), this.Z = O("z", p), Object.freeze(this); + this.X = k("x", b), this.Y = k("y", m, !0), this.Z = k("z", p), Object.freeze(this); } static CURVE() { return s; @@ -6521,16 +6521,16 @@ function dh(e, t = {}) { const { x: m, y: p } = b || {}; if (!b || !r.isValid(m) || !r.isValid(p)) throw new Error("invalid affine point"); - if (b instanceof C) + if (b instanceof L) throw new Error("projective point not allowed"); - return r.is0(m) && r.is0(p) ? C.ZERO : new C(m, p, r.ONE); + return r.is0(m) && r.is0(p) ? L.ZERO : new L(m, p, r.ONE); } static fromBytes(b) { - const m = C.fromAffine(w(et(b, void 0, "point"))); + const m = L.fromAffine(w(et(b, void 0, "point"))); return m.assertValidity(), m; } static fromHex(b) { - return C.fromBytes(Nr(b)); + return L.fromBytes(Cr(b)); } get x() { return this.toAffine().x; @@ -6545,7 +6545,7 @@ function dh(e, t = {}) { * @returns */ precompute(b = 8, m = !0) { - return gt.createCache(this, b), m || this.multiply(or), this; + return gt.createCache(this, b), m || this.multiply(sr), this; } // TODO: return `this` /** A point on curve is valid if it conforms to equation. */ @@ -6561,21 +6561,21 @@ function dh(e, t = {}) { /** Compare one point to another. */ equals(b) { N(b); - const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b, T = r.eql(r.mul(m, I), r.mul(A, E)), B = r.eql(r.mul(p, I), r.mul(k, E)); - return T && B; + const { X: m, Y: p, Z: E } = this, { X: A, Y: B, Z: I } = b, v = r.eql(r.mul(m, I), r.mul(A, E)), O = r.eql(r.mul(p, I), r.mul(B, E)); + return v && O; } /** Flips point to one corresponding to (x, -y) in Affine coordinates. */ negate() { - return new C(this.X, r.neg(this.Y), this.Z); + return new L(this.X, r.neg(this.Y), this.Z); } // Renes-Costello-Batina exception-free doubling formula. // There is 30% faster Jacobian formula, but it is not complete. // https://eprint.iacr.org/2015/1060, algorithm 3 // Cost: 8M + 3S + 3*a + 2*b3 + 15add. double() { - const { a: b, b: m } = s, p = r.mul(m, or), { X: E, Y: A, Z: k } = this; - let I = r.ZERO, T = r.ZERO, B = r.ZERO, R = r.mul(E, E), F = r.mul(A, A), H = r.mul(k, k), L = r.mul(E, A); - return L = r.add(L, L), B = r.mul(E, k), B = r.add(B, B), I = r.mul(b, B), T = r.mul(p, H), T = r.add(I, T), I = r.sub(F, T), T = r.add(F, T), T = r.mul(I, T), I = r.mul(L, I), B = r.mul(p, B), H = r.mul(b, H), L = r.sub(R, H), L = r.mul(b, L), L = r.add(L, B), B = r.add(R, R), R = r.add(B, R), R = r.add(R, H), R = r.mul(R, L), T = r.add(T, R), H = r.mul(A, k), H = r.add(H, H), R = r.mul(H, L), I = r.sub(I, R), B = r.mul(H, F), B = r.add(B, B), B = r.add(B, B), new C(I, T, B); + const { a: b, b: m } = s, p = r.mul(m, sr), { X: E, Y: A, Z: B } = this; + let I = r.ZERO, v = r.ZERO, O = r.ZERO, R = r.mul(E, E), W = r.mul(A, A), V = r.mul(B, B), _ = r.mul(E, A); + return _ = r.add(_, _), O = r.mul(E, B), O = r.add(O, O), I = r.mul(b, O), v = r.mul(p, V), v = r.add(I, v), I = r.sub(W, v), v = r.add(W, v), v = r.mul(I, v), I = r.mul(_, I), O = r.mul(p, O), V = r.mul(b, V), _ = r.sub(R, V), _ = r.mul(b, _), _ = r.add(_, O), O = r.add(R, R), R = r.add(O, R), R = r.add(R, V), R = r.mul(R, _), v = r.add(v, R), V = r.mul(A, B), V = r.add(V, V), R = r.mul(V, _), I = r.sub(I, R), O = r.mul(V, W), O = r.add(O, O), O = r.add(O, O), new L(I, v, O); } // Renes-Costello-Batina exception-free addition formula. // There is 30% faster Jacobian formula, but it is not complete. @@ -6583,19 +6583,19 @@ function dh(e, t = {}) { // Cost: 12M + 0S + 3*a + 3*b3 + 23add. add(b) { N(b); - const { X: m, Y: p, Z: E } = this, { X: A, Y: k, Z: I } = b; - let T = r.ZERO, B = r.ZERO, R = r.ZERO; - const F = s.a, H = r.mul(s.b, or); - let L = r.mul(m, A), D = r.mul(p, k), q = r.mul(E, I), st = r.add(m, p), M = r.add(A, k); - st = r.mul(st, M), M = r.add(L, D), st = r.sub(st, M), M = r.add(m, E); - let Y = r.add(A, I); - return M = r.mul(M, Y), Y = r.add(L, q), M = r.sub(M, Y), Y = r.add(p, E), T = r.add(k, I), Y = r.mul(Y, T), T = r.add(D, q), Y = r.sub(Y, T), R = r.mul(F, M), T = r.mul(H, q), R = r.add(T, R), T = r.sub(D, R), R = r.add(D, R), B = r.mul(T, R), D = r.add(L, L), D = r.add(D, L), q = r.mul(F, q), M = r.mul(H, M), D = r.add(D, q), q = r.sub(L, q), q = r.mul(F, q), M = r.add(M, q), L = r.mul(D, M), B = r.add(B, L), L = r.mul(Y, M), T = r.mul(st, T), T = r.sub(T, L), L = r.mul(st, D), R = r.mul(Y, R), R = r.add(R, L), new C(T, B, R); + const { X: m, Y: p, Z: E } = this, { X: A, Y: B, Z: I } = b; + let v = r.ZERO, O = r.ZERO, R = r.ZERO; + const W = s.a, V = r.mul(s.b, sr); + let _ = r.mul(m, A), D = r.mul(p, B), q = r.mul(E, I), st = r.add(m, p), M = r.add(A, B); + st = r.mul(st, M), M = r.add(_, D), st = r.sub(st, M), M = r.add(m, E); + let Z = r.add(A, I); + return M = r.mul(M, Z), Z = r.add(_, q), M = r.sub(M, Z), Z = r.add(p, E), v = r.add(B, I), Z = r.mul(Z, v), v = r.add(D, q), Z = r.sub(Z, v), R = r.mul(W, M), v = r.mul(V, q), R = r.add(v, R), v = r.sub(D, R), R = r.add(D, R), O = r.mul(v, R), D = r.add(_, _), D = r.add(D, _), q = r.mul(W, q), M = r.mul(V, M), D = r.add(D, q), q = r.sub(_, q), q = r.mul(W, q), M = r.add(M, q), _ = r.mul(D, M), O = r.add(O, _), _ = r.mul(Z, M), v = r.mul(st, v), v = r.sub(v, _), _ = r.mul(st, D), R = r.mul(Z, R), R = r.add(R, _), new L(v, O, R); } subtract(b) { return this.add(b.negate()); } is0() { - return this.equals(C.ZERO); + return this.equals(L.ZERO); } /** * Constant time multiplication. @@ -6611,15 +6611,15 @@ function dh(e, t = {}) { if (!o.isValidNot0(b)) throw new Error("invalid scalar: out of range"); let p, E; - const A = (k) => gt.cached(this, k, (I) => rc(C, I)); + const A = (B) => gt.cached(this, B, (I) => cc(L, I)); if (m) { - const { k1neg: k, k1: I, k2neg: T, k2: B } = U(b), { p: R, f: F } = A(I), { p: H, f: L } = A(B); - E = F.add(L), p = Q(m.beta, R, H, k, T); + const { k1neg: B, k1: I, k2neg: v, k2: O } = $(b), { p: R, f: W } = A(I), { p: V, f: _ } = A(O); + E = W.add(_), p = Y(m.beta, R, V, B, v); } else { - const { p: k, f: I } = A(b); - p = k, E = I; + const { p: B, f: I } = A(b); + p = B, E = I; } - return rc(C, [p, E])[0]; + return cc(L, [p, E])[0]; } /** * Non-constant-time multiplication. Uses double-and-add algorithm. @@ -6630,15 +6630,15 @@ function dh(e, t = {}) { const { endo: m } = t, p = this; if (!o.isValid(b)) throw new Error("invalid scalar: out of range"); - if (b === ce || p.is0()) - return C.ZERO; - if (b === tn) + if (b === ae || p.is0()) + return L.ZERO; + if (b === en) return p; if (gt.hasCache(this)) return this.multiply(b); if (m) { - const { k1neg: E, k1: A, k2neg: k, k2: I } = U(b), { p1: T, p2: B } = ch(C, p, A, I); - return Q(m.beta, T, B, E, k); + const { k1neg: E, k1: A, k2neg: B, k2: I } = $(b), { p1: v, p2: O } = ph(L, p, A, I); + return Y(m.beta, v, O, E, B); } else return gt.unsafe(p, b); } @@ -6647,7 +6647,7 @@ function dh(e, t = {}) { * @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch */ toAffine(b) { - return W(this, b); + return F(this, b); } /** * Checks whether Point is free of torsion elements (is in prime subgroup). @@ -6655,32 +6655,32 @@ function dh(e, t = {}) { */ isTorsionFree() { const { isTorsionFree: b } = t; - return i === tn ? !0 : b ? b(C, this) : gt.unsafe(this, c).is0(); + return i === en ? !0 : b ? b(L, this) : gt.unsafe(this, c).is0(); } clearCofactor() { const { clearCofactor: b } = t; - return i === tn ? this : b ? b(C, this) : this.multiplyUnsafe(i); + return i === en ? this : b ? b(L, this) : this.multiplyUnsafe(i); } isSmallOrder() { return this.multiplyUnsafe(i).is0(); } toBytes(b = !0) { - return Lr(b, "isCompressed"), this.assertValidity(), h(C, this, b); + return Pr(b, "isCompressed"), this.assertValidity(), h(L, this, b); } toHex(b = !0) { - return so(this.toBytes(b)); + return co(this.toBytes(b)); } toString() { return ``; } } - const Pt = o.BITS, gt = new ih(C, t.endo ? Math.ceil(Pt / 2) : Pt); - return C.BASE.precompute(8), C; + const Pt = o.BITS, gt = new hh(L, t.endo ? Math.ceil(Pt / 2) : Pt); + return L.BASE.precompute(8), L; } -function Uu(e) { +function Pu(e) { return Uint8Array.of(e ? 2 : 3); } -function Ru(e, t) { +function Vu(e, t) { return { secretKey: t.BYTES, publicKey: 1 + e.BYTES, @@ -6689,8 +6689,8 @@ function Ru(e, t) { signature: 2 * t.BYTES }; } -function hh(e, t = {}) { - const { Fn: n } = e, r = t.randomBytes || io, o = Object.assign(Ru(e.Fp, n), { seed: vu(n.ORDER) }); +function Eh(e, t = {}) { + const { Fn: n } = e, r = t.randomBytes || ao, o = Object.assign(Vu(e.Fp, n), { seed: Ou(n.ORDER) }); function s(h) { try { const w = n.fromBytes(h); @@ -6709,14 +6709,14 @@ function hh(e, t = {}) { } } function c(h = r(o.seed)) { - return Tu(et(h, o.seed, "seed"), n.ORDER); + return Uu(et(h, o.seed, "seed"), n.ORDER); } function a(h, w = !0) { return e.BASE.multiply(n.fromBytes(h)).toBytes(w); } function u(h) { const { secretKey: w, publicKey: g, publicKeyUncompressed: y } = o; - if (!Ks(h) || "_lengths" in n && n._lengths || w === g) + if (!js(h) || "_lengths" in n && n._lengths || w === g) return; const S = et(h, void 0, "key").length; return S === g || S === y; @@ -6729,29 +6729,29 @@ function hh(e, t = {}) { const y = n.fromBytes(h); return e.fromBytes(w).multiply(y).toBytes(g); } - const l = { + const d = { isValidSecretKey: s, isValidPublicKey: i, randomSecretKey: c - }, d = ku(c, a); - return Object.freeze({ getPublicKey: a, getSharedSecret: f, keygen: d, Point: e, utils: l, lengths: o }); + }, l = Nu(c, a); + return Object.freeze({ getPublicKey: a, getSharedSecret: f, keygen: l, Point: e, utils: d, lengths: o }); } -function ph(e, t, n = {}) { - La(t), Gs(n, {}, { +function xh(e, t, n = {}) { + Ha(t), Qs(n, {}, { hmac: "function", lowS: "boolean", randomBytes: "function", bits2int: "function", bits2int_modN: "function" }), n = Object.assign({}, n); - const r = n.randomBytes || io, o = n.hmac || ((m, p) => Ou(t, m, p)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: l, utils: d, lengths: h } = hh(e, n), w = { + const r = n.randomBytes || ao, o = n.hmac || ((m, p) => _u(t, m, p)), { Fp: s, Fn: i } = e, { ORDER: c, BITS: a } = i, { keygen: u, getPublicKey: f, getSharedSecret: d, utils: l, lengths: h } = Eh(e, n), w = { prehash: !0, lowS: typeof n.lowS == "boolean" ? n.lowS : !0, format: "compact", extraEntropy: !1 - }, g = c * $u < s.ORDER; + }, g = c * Cu < s.ORDER; function y(m) { - const p = c >> tn; + const p = c >> en; return m > p; } function S(m, p) { @@ -6759,12 +6759,12 @@ function ph(e, t, n = {}) { throw new Error(`invalid signature ${m}: out of range 1..Point.Fn.ORDER`); return p; } - function v() { + function T() { if (g) throw new Error('"recovered" sig type is not supported for cofactor >2 curves'); } - function O(m, p) { - os(p); + function k(m, p) { + fs(p); const E = h.signature, A = p === "compact" ? E : p === "recovered" ? E + 1 : void 0; return et(m, A); } @@ -6774,25 +6774,25 @@ function ph(e, t, n = {}) { recovery; constructor(p, E, A) { if (this.r = S("r", p), this.s = S("s", E), A != null) { - if (v(), ![0, 1, 2, 3].includes(A)) + if (T(), ![0, 1, 2, 3].includes(A)) throw new Error("invalid recovery id"); this.recovery = A; } Object.freeze(this); } static fromBytes(p, E = w.format) { - O(p, E); + k(p, E); let A; if (E === "der") { - const { r: B, s: R } = be.toSig(et(p)); - return new N(B, R); + const { r: O, s: R } = be.toSig(et(p)); + return new N(O, R); } E === "recovered" && (A = p[0], E = "compact", p = p.subarray(1)); - const k = h.signature / 2, I = p.subarray(0, k), T = p.subarray(k, k * 2); - return new N(i.fromBytes(I), i.fromBytes(T), A); + const B = h.signature / 2, I = p.subarray(0, B), v = p.subarray(B, B * 2); + return new N(i.fromBytes(I), i.fromBytes(v), A); } static fromHex(p, E) { - return this.fromBytes(Nr(p), E); + return this.fromBytes(Cr(p), E); } assertRecovery() { const { recovery: p } = this; @@ -6804,10 +6804,10 @@ function ph(e, t, n = {}) { return new N(this.r, this.s, p); } recoverPublicKey(p) { - const { r: E, s: A } = this, k = this.assertRecovery(), I = k === 2 || k === 3 ? E + c : E; + const { r: E, s: A } = this, B = this.assertRecovery(), I = B === 2 || B === 3 ? E + c : E; if (!s.isValid(I)) throw new Error("invalid recovery id: sig.r+curve.n != R.x"); - const T = s.toBytes(I), B = e.fromBytes(Xt(Uu((k & 1) === 0), T)), R = i.inv(I), F = W(et(p, void 0, "msgHash")), H = i.create(-F * R), L = i.create(A * R), D = e.BASE.multiplyUnsafe(H).add(B.multiplyUnsafe(L)); + const v = s.toBytes(I), O = e.fromBytes(Qt(Pu((B & 1) === 0), v)), R = i.inv(I), W = F(et(p, void 0, "msgHash")), V = i.create(-W * R), _ = i.create(A * R), D = e.BASE.multiplyUnsafe(V).add(O.multiplyUnsafe(_)); if (D.is0()) throw new Error("invalid recovery: point at infinify"); return D.assertValidity(), D; @@ -6817,97 +6817,97 @@ function ph(e, t, n = {}) { return y(this.s); } toBytes(p = w.format) { - if (os(p), p === "der") - return Nr(be.hexFromSig(this)); - const { r: E, s: A } = this, k = i.toBytes(E), I = i.toBytes(A); - return p === "recovered" ? (v(), Xt(Uint8Array.of(this.assertRecovery()), k, I)) : Xt(k, I); + if (fs(p), p === "der") + return Cr(be.hexFromSig(this)); + const { r: E, s: A } = this, B = i.toBytes(E), I = i.toBytes(A); + return p === "recovered" ? (T(), Qt(Uint8Array.of(this.assertRecovery()), B, I)) : Qt(B, I); } toHex(p) { - return so(this.toBytes(p)); + return co(this.toBytes(p)); } } - const U = n.bits2int || function(p) { + const $ = n.bits2int || function(p) { if (p.length > 8192) throw new Error("input is too large"); - const E = bn(p), A = p.length * 8 - a; + const E = En(p), A = p.length * 8 - a; return A > 0 ? E >> BigInt(A) : E; - }, W = n.bits2int_modN || function(p) { - return i.create(U(p)); - }, x = zs(a); - function Q(m) { - return ad("num < 2^" + a, m, ce, x), i.toBytes(m); + }, F = n.bits2int_modN || function(p) { + return i.create($(p)); + }, x = Xs(a); + function Y(m) { + return gl("num < 2^" + a, m, ae, x), i.toBytes(m); } - function C(m, p) { + function L(m, p) { return et(m, void 0, "message"), p ? et(t(m), void 0, "prehashed message") : m; } function Pt(m, p, E) { - const { lowS: A, prehash: k, extraEntropy: I } = Bo(E, w); - m = C(m, k); - const T = W(m), B = i.fromBytes(p); - if (!i.isValidNot0(B)) + const { lowS: A, prehash: B, extraEntropy: I } = $o(E, w); + m = L(m, B); + const v = F(m), O = i.fromBytes(p); + if (!i.isValidNot0(O)) throw new Error("invalid private key"); - const R = [Q(B), Q(T)]; + const R = [Y(O), Y(v)]; if (I != null && I !== !1) { const D = I === !0 ? r(h.secretKey) : I; R.push(et(D, void 0, "extraEntropy")); } - const F = Xt(...R), H = T; - function L(D) { - const q = U(D); + const W = Qt(...R), V = v; + function _(D) { + const q = $(D); if (!i.isValidNot0(q)) return; - const st = i.inv(q), M = e.BASE.multiply(q).toAffine(), Y = i.create(M.x); - if (Y === ce) + const st = i.inv(q), M = e.BASE.multiply(q).toAffine(), Z = i.create(M.x); + if (Z === ae) return; - const ee = i.create(st * i.create(H + Y * B)); - if (ee === ce) + const ne = i.create(st * i.create(V + Z * O)); + if (ne === ae) return; - let En = (M.x === Y ? 0 : 2) | Number(M.y & tn), xn = ee; - return A && y(ee) && (xn = i.neg(ee), En ^= 1), new N(Y, xn, g ? void 0 : En); + let xn = (M.x === Z ? 0 : 2) | Number(M.y & en), Sn = ne; + return A && y(ne) && (Sn = i.neg(ne), xn ^= 1), new N(Z, Sn, g ? void 0 : xn); } - return { seed: F, k2sig: L }; + return { seed: W, k2sig: _ }; } function gt(m, p, E = {}) { - const { seed: A, k2sig: k } = Pt(m, p, E); - return fd(t.outputLen, i.BYTES, o)(A, k).toBytes(E.format); + const { seed: A, k2sig: B } = Pt(m, p, E); + return yl(t.outputLen, i.BYTES, o)(A, B).toBytes(E.format); } - function _(m, p, E, A = {}) { - const { lowS: k, prehash: I, format: T } = Bo(A, w); - if (E = et(E, void 0, "publicKey"), p = C(p, I), !Ks(m)) { - const B = m instanceof N ? ", use sig.toBytes()" : ""; - throw new Error("verify expects Uint8Array signature" + B); + function C(m, p, E, A = {}) { + const { lowS: B, prehash: I, format: v } = $o(A, w); + if (E = et(E, void 0, "publicKey"), p = L(p, I), !js(m)) { + const O = m instanceof N ? ", use sig.toBytes()" : ""; + throw new Error("verify expects Uint8Array signature" + O); } - O(m, T); + k(m, v); try { - const B = N.fromBytes(m, T), R = e.fromBytes(E); - if (k && B.hasHighS()) + const O = N.fromBytes(m, v), R = e.fromBytes(E); + if (B && O.hasHighS()) return !1; - const { r: F, s: H } = B, L = W(p), D = i.inv(H), q = i.create(L * D), st = i.create(F * D), M = e.BASE.multiplyUnsafe(q).add(R.multiplyUnsafe(st)); - return M.is0() ? !1 : i.create(M.x) === F; + const { r: W, s: V } = O, _ = F(p), D = i.inv(V), q = i.create(_ * D), st = i.create(W * D), M = e.BASE.multiplyUnsafe(q).add(R.multiplyUnsafe(st)); + return M.is0() ? !1 : i.create(M.x) === W; } catch { return !1; } } function b(m, p, E = {}) { - const { prehash: A } = Bo(E, w); - return p = C(p, A), N.fromBytes(m, "recovered").recoverPublicKey(p).toBytes(); + const { prehash: A } = $o(E, w); + return p = L(p, A), N.fromBytes(m, "recovered").recoverPublicKey(p).toBytes(); } return Object.freeze({ keygen: u, getPublicKey: f, - getSharedSecret: l, - utils: d, + getSharedSecret: d, + utils: l, lengths: h, Point: e, sign: gt, - verify: _, + verify: C, recoverPublicKey: b, Signature: N, hash: t }); } /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -const fo = { +const po = { p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"), n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"), h: BigInt(1), @@ -6915,87 +6915,87 @@ const fo = { b: BigInt(7), Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"), Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8") -}, gh = { +}, Sh = { beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"), basises: [ [BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")], [BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")] ] -}, wh = /* @__PURE__ */ BigInt(0), ss = /* @__PURE__ */ BigInt(2); -function yh(e) { - const t = fo.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, l = Nt(f, n, t) * f % t, d = Nt(l, n, t) * f % t, h = Nt(d, ss, t) * u % t, w = Nt(h, o, t) * h % t, g = Nt(w, s, t) * w % t, y = Nt(g, c, t) * g % t, S = Nt(y, a, t) * y % t, v = Nt(S, c, t) * g % t, O = Nt(v, n, t) * f % t, N = Nt(O, i, t) * w % t, U = Nt(N, r, t) * u % t, W = Nt(U, ss, t); - if (!Dr.eql(Dr.sqr(W), e)) +}, Th = /* @__PURE__ */ BigInt(0), ds = /* @__PURE__ */ BigInt(2); +function vh(e) { + const t = po.p, n = BigInt(3), r = BigInt(6), o = BigInt(11), s = BigInt(22), i = BigInt(23), c = BigInt(44), a = BigInt(88), u = e * e * e % t, f = u * u * e % t, d = Nt(f, n, t) * f % t, l = Nt(d, n, t) * f % t, h = Nt(l, ds, t) * u % t, w = Nt(h, o, t) * h % t, g = Nt(w, s, t) * w % t, y = Nt(g, c, t) * g % t, S = Nt(y, a, t) * y % t, T = Nt(S, c, t) * g % t, k = Nt(T, n, t) * f % t, N = Nt(k, i, t) * w % t, $ = Nt(N, r, t) * u % t, F = Nt($, ds, t); + if (!Fr.eql(Fr.sqr(F), e)) throw new Error("Cannot find square root"); - return W; -} -const Dr = uo(fo.p, { sqrt: yh }), qe = /* @__PURE__ */ dh(fo, { - Fp: Dr, - endo: gh -}), ac = /* @__PURE__ */ ph(qe, rs), uc = {}; -function Mr(e, ...t) { - let n = uc[e]; + return F; +} +const Fr = ho(po.p, { sqrt: vh }), je = /* @__PURE__ */ bh(po, { + Fp: Fr, + endo: Sh +}), lc = /* @__PURE__ */ xh(je, us), hc = {}; +function Wr(e, ...t) { + let n = hc[e]; if (n === void 0) { - const r = rs(id(e)); - n = Xt(r, r), uc[e] = n; + const r = us(hl(e)); + n = Qt(r, r), hc[e] = n; } - return rs(Xt(n, ...t)); + return us(Qt(n, ...t)); } -const Qs = (e) => e.toBytes(!0).slice(1), Js = (e) => e % ss === wh; -function is(e) { - const { Fn: t, BASE: n } = qe, r = t.fromBytes(e), o = n.multiply(r); - return { scalar: Js(o.y) ? r : t.neg(r), bytes: Qs(o) }; +const ni = (e) => e.toBytes(!0).slice(1), ri = (e) => e % ds === Th; +function ls(e) { + const { Fn: t, BASE: n } = je, r = t.fromBytes(e), o = n.multiply(r); + return { scalar: ri(o.y) ? r : t.neg(r), bytes: ni(o) }; } -function Nu(e) { - const t = Dr; +function Hu(e) { + const t = Fr; if (!t.isValidNot0(e)) throw new Error("invalid x: Fail if x ≥ p"); const n = t.create(e * e), r = t.create(n * e + BigInt(7)); let o = t.sqrt(r); - Js(o) || (o = t.neg(o)); - const s = qe.fromAffine({ x: e, y: o }); + ri(o) || (o = t.neg(o)); + const s = je.fromAffine({ x: e, y: o }); return s.assertValidity(), s; } -const Un = bn; -function Lu(...e) { - return qe.Fn.create(Un(Mr("BIP0340/challenge", ...e))); +const Rn = En; +function Du(...e) { + return je.Fn.create(Rn(Wr("BIP0340/challenge", ...e))); } -function fc(e) { - return is(e).bytes; +function pc(e) { + return ls(e).bytes; } -function mh(e, t, n = io(32)) { - const { Fn: r } = qe, o = et(e, void 0, "message"), { bytes: s, scalar: i } = is(t), c = et(n, 32, "auxRand"), a = r.toBytes(i ^ Un(Mr("BIP0340/aux", c))), u = Mr("BIP0340/nonce", a, s, o), { bytes: f, scalar: l } = is(u), d = Lu(f, s, o), h = new Uint8Array(64); - if (h.set(f, 0), h.set(r.toBytes(r.create(l + d * i)), 32), !Cu(h, o, s)) +function Ah(e, t, n = ao(32)) { + const { Fn: r } = je, o = et(e, void 0, "message"), { bytes: s, scalar: i } = ls(t), c = et(n, 32, "auxRand"), a = r.toBytes(i ^ Rn(Wr("BIP0340/aux", c))), u = Wr("BIP0340/nonce", a, s, o), { bytes: f, scalar: d } = ls(u), l = Du(f, s, o), h = new Uint8Array(64); + if (h.set(f, 0), h.set(r.toBytes(r.create(d + l * i)), 32), !Mu(h, o, s)) throw new Error("sign: Invalid signature produced"); return h; } -function Cu(e, t, n) { - const { Fp: r, Fn: o, BASE: s } = qe, i = et(e, 64, "signature"), c = et(t, void 0, "message"), a = et(n, 32, "publicKey"); +function Mu(e, t, n) { + const { Fp: r, Fn: o, BASE: s } = je, i = et(e, 64, "signature"), c = et(t, void 0, "message"), a = et(n, 32, "publicKey"); try { - const u = Nu(Un(a)), f = Un(i.subarray(0, 32)); + const u = Hu(Rn(a)), f = Rn(i.subarray(0, 32)); if (!r.isValidNot0(f)) return !1; - const l = Un(i.subarray(32, 64)); - if (!o.isValidNot0(l)) + const d = Rn(i.subarray(32, 64)); + if (!o.isValidNot0(d)) return !1; - const d = Lu(o.toBytes(f), Qs(u), c), h = s.multiplyUnsafe(l).add(u.multiplyUnsafe(o.neg(d))), { x: w, y: g } = h.toAffine(); - return !(h.is0() || !Js(g) || w !== f); + const l = Du(o.toBytes(f), ni(u), c), h = s.multiplyUnsafe(d).add(u.multiplyUnsafe(o.neg(l))), { x: w, y: g } = h.toAffine(); + return !(h.is0() || !ri(g) || w !== f); } catch { return !1; } } -const ti = /* @__PURE__ */ (() => { - const n = (r = io(48)) => Tu(r, fo.n); +const oi = /* @__PURE__ */ (() => { + const n = (r = ao(48)) => Uu(r, po.n); return { - keygen: ku(n, fc), - getPublicKey: fc, - sign: mh, - verify: Cu, - Point: qe, + keygen: Nu(n, pc), + getPublicKey: pc, + sign: Ah, + verify: Mu, + Point: je, utils: { randomSecretKey: n, - taggedHash: Mr, - lift_x: Nu, - pointToBytes: Qs + taggedHash: Wr, + lift_x: Hu, + pointToBytes: ni }, lengths: { secretKey: 32, @@ -7006,31 +7006,31 @@ const ti = /* @__PURE__ */ (() => { } }; })(); -function ei(e, t, n = {}) { - e = Zo(e); - const { aggPublicKey: r } = Xo(e); +function si(e, t, n = {}) { + e = ns(e); + const { aggPublicKey: r } = rs(e); if (!n.taprootTweak) return { preTweakedKey: r.toBytes(!0), finalKey: r.toBytes(!0) }; - const o = ti.utils.taggedHash("TapTweak", r.toBytes(!0).subarray(1), n.taprootTweak ?? new Uint8Array(0)), { aggPublicKey: s } = Xo(e, [o], [!0]); + const o = oi.utils.taggedHash("TapTweak", r.toBytes(!0).subarray(1), n.taprootTweak ?? new Uint8Array(0)), { aggPublicKey: s } = rs(e, [o], [!0]); return { preTweakedKey: r.toBytes(!0), finalKey: s.toBytes(!0) }; } -class sr extends Error { +class ir extends Error { constructor(t) { super(t), this.name = "PartialSignatureError"; } } -class ni { +class ii { constructor(t, n) { if (this.s = t, this.R = n, t.length !== 32) - throw new sr("Invalid s length"); + throw new ir("Invalid s length"); if (n.length !== 33) - throw new sr("Invalid R length"); + throw new ir("Invalid R length"); } /** * Encodes the partial signature into bytes @@ -7045,26 +7045,26 @@ class ni { */ static decode(t) { if (t.length !== 32) - throw new sr("Invalid partial signature length"); - if (bn(t) >= Et.CURVE().n) - throw new sr("s value overflows curve order"); + throw new ir("Invalid partial signature length"); + if (En(t) >= xt.CURVE().n) + throw new ir("s value overflows curve order"); const r = new Uint8Array(33); - return new ni(t, r); + return new ii(t, r); } } -function bh(e, t, n, r, o, s) { +function Ih(e, t, n, r, o, s) { let i; if (s?.taprootTweak !== void 0) { - const { preTweakedKey: u } = ei(Zo(r)); - i = ti.utils.taggedHash("TapTweak", u.subarray(1), s.taprootTweak); + const { preTweakedKey: u } = si(ns(r)); + i = oi.utils.taggedHash("TapTweak", u.subarray(1), s.taprootTweak); } - const a = new Ql(n, Zo(r), o, i ? [i] : void 0, i ? [!0] : void 0).sign(e, t); - return ni.decode(a); + const a = new sl(n, ns(r), o, i ? [i] : void 0, i ? [!0] : void 0).sign(e, t); + return ii.decode(a); } -var Oo, lc; -function Eh() { - if (lc) return Oo; - lc = 1; +var Ro, gc; +function kh() { + if (gc) return Ro; + gc = 1; const e = 4294967295, t = 1 << 31, n = 9, r = 65535, o = 1 << 22, s = r, i = 1 << n, c = r << n; function a(f) { return f & t ? {} : f & o ? { @@ -7073,27 +7073,27 @@ function Eh() { blocks: f & r }; } - function u({ blocks: f, seconds: l }) { - if (f !== void 0 && l !== void 0) throw new TypeError("Cannot encode blocks AND seconds"); - if (f === void 0 && l === void 0) return e; - if (l !== void 0) { - if (!Number.isFinite(l)) throw new TypeError("Expected Number seconds"); - if (l > c) throw new TypeError("Expected Number seconds <= " + c); - if (l % i !== 0) throw new TypeError("Expected Number seconds as a multiple of " + i); - return o | l >> n; + function u({ blocks: f, seconds: d }) { + if (f !== void 0 && d !== void 0) throw new TypeError("Cannot encode blocks AND seconds"); + if (f === void 0 && d === void 0) return e; + if (d !== void 0) { + if (!Number.isFinite(d)) throw new TypeError("Expected Number seconds"); + if (d > c) throw new TypeError("Expected Number seconds <= " + c); + if (d % i !== 0) throw new TypeError("Expected Number seconds as a multiple of " + i); + return o | d >> n; } if (!Number.isFinite(f)) throw new TypeError("Expected Number blocks"); if (f > r) throw new TypeError("Expected Number blocks <= " + s); return f; } - return Oo = { decode: a, encode: u }, Oo; + return Ro = { decode: a, encode: u }, Ro; } -var cs = Eh(), Ot; +var hs = kh(), Ot; (function(e) { e.VtxoTaprootTree = "taptree", e.VtxoTreeExpiry = "expiry", e.Cosigner = "cosigner", e.ConditionWitness = "condition"; })(Ot || (Ot = {})); -const ri = 222; -function xh(e, t, n, r) { +const ci = 222; +function Bh(e, t, n, r) { e.updateInput(t, { unknown: [ ...e.getInput(t)?.unknown ?? [], @@ -7101,7 +7101,7 @@ function xh(e, t, n, r) { ] }); } -function as(e, t, n) { +function ps(e, t, n) { const r = e.getInput(t)?.unknown ?? [], o = []; for (const s of r) { const i = n.decode(s); @@ -7109,76 +7109,76 @@ function as(e, t, n) { } return o; } -const _u = { +const Ku = { key: Ot.VtxoTaprootTree, encode: (e) => [ { - type: ri, - key: lo[Ot.VtxoTaprootTree] + type: ci, + key: go[Ot.VtxoTaprootTree] }, e ], - decode: (e) => oi(() => si(e[0], Ot.VtxoTaprootTree) ? e[1] : null) -}, Sh = { + decode: (e) => ai(() => ui(e[0], Ot.VtxoTaprootTree) ? e[1] : null) +}, Oh = { key: Ot.ConditionWitness, encode: (e) => [ { - type: ri, - key: lo[Ot.ConditionWitness] + type: ci, + key: go[Ot.ConditionWitness] }, - Cn.encode(e) + Pn.encode(e) ], - decode: (e) => oi(() => si(e[0], Ot.ConditionWitness) ? Cn.decode(e[1]) : null) -}, us = { + decode: (e) => ai(() => ui(e[0], Ot.ConditionWitness) ? Pn.decode(e[1]) : null) +}, gs = { key: Ot.Cosigner, encode: (e) => [ { - type: ri, + type: ci, key: new Uint8Array([ - ...lo[Ot.Cosigner], + ...go[Ot.Cosigner], e.index ]) }, e.key ], - decode: (e) => oi(() => si(e[0], Ot.Cosigner) ? { + decode: (e) => ai(() => ui(e[0], Ot.Cosigner) ? { index: e[0].key[e[0].key.length - 1], key: e[1] } : null) }; Ot.VtxoTreeExpiry; -const lo = Object.fromEntries(Object.values(Ot).map((e) => [ +const go = Object.fromEntries(Object.values(Ot).map((e) => [ e, new TextEncoder().encode(e) -])), oi = (e) => { +])), ai = (e) => { try { return e(); } catch { return null; } }; -function si(e, t) { - const n = $.encode(lo[t]); - return $.encode(new Uint8Array([e.type, ...e.key])).includes(n); +function ui(e, t) { + const n = U.encode(go[t]); + return U.encode(new Uint8Array([e.type, ...e.key])).includes(n); } -const ir = new Error("missing vtxo graph"); -class Dn { +const cr = new Error("missing vtxo graph"); +class Kn { constructor(t) { this.secretKey = t, this.myNonces = null, this.aggregateNonces = null, this.graph = null, this.scriptRoot = null, this.rootSharedOutputAmount = null; } static random() { - const t = Mo(); - return new Dn(t); + const t = qo(); + return new Kn(t); } async init(t, n, r) { this.graph = t, this.scriptRoot = n, this.rootSharedOutputAmount = r; } async getPublicKey() { - return ac.getPublicKey(this.secretKey); + return lc.getPublicKey(this.secretKey); } async getNonces() { if (!this.graph) - throw ir; + throw cr; this.myNonces || (this.myNonces = this.generateNonces()); const t = /* @__PURE__ */ new Map(); for (const [n, r] of this.myNonces) @@ -7187,7 +7187,7 @@ class Dn { } async aggregatedNonces(t, n) { if (!this.graph) - throw ir; + throw cr; if (this.aggregateNonces || (this.aggregateNonces = /* @__PURE__ */ new Map()), this.myNonces || await this.getNonces(), this.aggregateNonces.has(t)) return { hasAllNonces: this.aggregateNonces.size === this.myNonces?.size @@ -7196,12 +7196,12 @@ class Dn { if (!r) throw new Error(`missing nonce for txid ${t}`); const o = await this.getPublicKey(); - n.set($.encode(o.subarray(1)), r); + n.set(U.encode(o.subarray(1)), r); const s = this.graph.find(t); if (!s) throw new Error(`missing tx for txid ${t}`); - const i = as(s.root, 0, us).map( - (u) => $.encode(u.key.subarray(1)) + const i = ps(s.root, 0, gs).map( + (u) => U.encode(u.key.subarray(1)) // xonly pubkey ), c = []; for (const u of i) { @@ -7210,14 +7210,14 @@ class Dn { throw new Error(`missing nonce for cosigner ${u}`); c.push(f.pubNonce); } - const a = td(c); + const a = cl(c); return this.aggregateNonces.set(t, { pubNonce: a }), { hasAllNonces: this.aggregateNonces.size === this.myNonces?.size }; } async sign() { if (!this.graph) - throw ir; + throw cr; if (!this.aggregateNonces) throw new Error("nonces not set"); if (!this.myNonces) @@ -7231,17 +7231,17 @@ class Dn { } generateNonces() { if (!this.graph) - throw ir; - const t = /* @__PURE__ */ new Map(), n = ac.getPublicKey(this.secretKey); + throw cr; + const t = /* @__PURE__ */ new Map(), n = lc.getPublicKey(this.secretKey); for (const r of this.graph.iterator()) { - const o = Jl(n); + const o = il(n); t.set(r.txid, o); } return t; } signPartial(t) { if (!this.graph || !this.scriptRoot || !this.rootSharedOutputAmount) - throw Dn.NOT_INITIALIZED; + throw Kn.NOT_INITIALIZED; if (!this.myNonces || !this.aggregateNonces) throw new Error("session not properly initialized"); const n = this.myNonces.get(t.txid); @@ -7250,27 +7250,27 @@ class Dn { const r = this.aggregateNonces.get(t.txid); if (!r) throw new Error("missing aggregate nonce"); - const o = [], s = [], i = as(t.root, 0, us).map((u) => u.key), { finalKey: c } = ei(i, !0, { + const o = [], s = [], i = ps(t.root, 0, gs).map((u) => u.key), { finalKey: c } = si(i, !0, { taprootTweak: this.scriptRoot }); for (let u = 0; u < t.root.inputsLength; u++) { - const f = vh(c, this.graph, this.rootSharedOutputAmount, t.root); + const f = Uh(c, this.graph, this.rootSharedOutputAmount, t.root); o.push(f.amount), s.push(f.script); } const a = t.root.preimageWitnessV1( 0, // always first input s, - Ke.DEFAULT, + Fe.DEFAULT, o ); - return bh(n.secNonce, this.secretKey, r.pubNonce, i, a, { + return Ih(n.secNonce, this.secretKey, r.pubNonce, i, a, { taprootTweak: this.scriptRoot }); } } -Dn.NOT_INITIALIZED = new Error("session not initialized, call init method"); -function vh(e, t, n, r) { +Kn.NOT_INITIALIZED = new Error("session not initialized, call init method"); +function Uh(e, t, n, r) { const o = K.encode(["OP_1", e.slice(1)]); if (r.id === t.txid) return { @@ -7280,7 +7280,7 @@ function vh(e, t, n, r) { const s = r.getInput(0); if (!s.txid) throw new Error("missing parent input txid"); - const i = $.encode(s.txid), c = t.find(i); + const i = U.encode(s.txid), c = t.find(i); if (!c) throw new Error("parent tx not found"); if (s.index === void 0) @@ -7295,19 +7295,19 @@ function vh(e, t, n, r) { script: o }; } -const dc = Object.values(Ke).filter((e) => typeof e == "number"); -class Rn { +const wc = Object.values(Fe).filter((e) => typeof e == "number"); +class Nn { constructor(t) { - this.key = t || Mo(); + this.key = t || qo(); } static fromPrivateKey(t) { - return new Rn(t); + return new Nn(t); } static fromHex(t) { - return new Rn($.decode(t)); + return new Nn(U.decode(t)); } static fromRandomBytes() { - return new Rn(Mo()); + return new Nn(qo()); } /** * Export the private key as a hex string. @@ -7315,13 +7315,13 @@ class Rn { * @returns The private key as a hex string */ toHex() { - return $.encode(this.key); + return U.encode(this.key); } async sign(t, n) { const r = t.clone(); if (!n) { try { - if (!r.sign(this.key, dc)) + if (!r.sign(this.key, wc)) throw new Error("Failed to sign transaction"); } catch (o) { if (!(o instanceof Error && o.message.includes("No inputs signed"))) throw o; @@ -7329,21 +7329,21 @@ class Rn { return r; } for (const o of n) - if (!r.signIdx(this.key, o, dc)) + if (!r.signIdx(this.key, o, wc)) throw new Error(`Failed to sign input #${o}`); return r; } compressedPublicKey() { - return Promise.resolve(ma(this.key, !0)); + return Promise.resolve(Ta(this.key, !0)); } xOnlyPublicKey() { - return Promise.resolve(Rs(this.key)); + return Promise.resolve(Vs(this.key)); } signerSession() { - return Dn.random(); + return Kn.random(); } async signMessage(t, n = "schnorr") { - return n === "ecdsa" ? Nd(t, this.key, { prehash: !1 }) : Dd.sign(t, this.key); + return n === "ecdsa" ? Dl(t, this.key, { prehash: !1 }) : ql.signAsync(t, this.key); } } class pn { @@ -7354,10 +7354,10 @@ class pn { throw new Error("Invalid vtxo taproot public key length, expected 32 bytes, got " + n.length); } static decode(t) { - const n = je.decodeUnsafe(t, 1023); + const n = Ye.decodeUnsafe(t, 1023); if (!n) throw new Error("Invalid address"); - const r = new Uint8Array(je.fromWords(n.words)); + const r = new Uint8Array(Ye.fromWords(n.words)); if (r.length !== 65) throw new Error("Invalid data length, expected 65 bytes, got " + r.length); const o = r[0], s = r.slice(1, 33), i = r.slice(33, 65); @@ -7366,8 +7366,8 @@ class pn { encode() { const t = new Uint8Array(65); t[0] = this.version, t.set(this.serverPubKey, 1), t.set(this.vtxoTaprootKey, 33); - const n = je.toWords(t); - return je.encode(this.hrp, n, 1023); + const n = Ye.toWords(t); + return Ye.encode(this.hrp, n, 1023); } // pkScript is the script that should be used to send non-dust funds to the address get pkScript() { @@ -7378,18 +7378,18 @@ class pn { return K.encode(["RETURN", this.vtxoTaprootKey]); } } -const Kr = Cs(void 0, !0); +const zr = Ms(void 0, !0); var ft; (function(e) { e.Multisig = "multisig", e.CSVMultisig = "csv-multisig", e.ConditionCSVMultisig = "condition-csv-multisig", e.ConditionMultisig = "condition-multisig", e.CLTVMultisig = "cltv-multisig"; })(ft || (ft = {})); -function Pu(e) { +function Fu(e) { const t = [ - Gt, - $t, - Mn, - Fr, - Kn + qt, + Ut, + Fn, + Gr, + Wn ]; for (const n of t) try { @@ -7397,9 +7397,9 @@ function Pu(e) { } catch { continue; } - throw new Error(`Failed to decode: script ${$.encode(e)} is not a valid tapscript`); + throw new Error(`Failed to decode: script ${U.encode(e)} is not a valid tapscript`); } -var Gt; +var qt; (function(e) { let t; (function(c) { @@ -7415,7 +7415,7 @@ var Gt; return { type: ft.Multisig, params: c, - script: Ml(c.pubkeys.length, c.pubkeys).script + script: jd(c.pubkeys.length, c.pubkeys).script }; const a = []; for (let u = 0; u < c.pubkeys.length; u++) @@ -7444,17 +7444,17 @@ var Gt; function o(c) { const a = K.decode(c), u = []; let f = !1; - for (let d = 0; d < a.length; d++) { - const h = a[d]; + for (let l = 0; l < a.length; l++) { + const h = a[l]; if (typeof h != "string" && typeof h != "number") { if (h.length !== 32) throw new Error(`Invalid pubkey length: expected 32, got ${h.length}`); - if (u.push(h), d + 1 >= a.length || a[d + 1] !== "CHECKSIGADD" && a[d + 1] !== "CHECKSIG") + if (u.push(h), l + 1 >= a.length || a[l + 1] !== "CHECKSIGADD" && a[l + 1] !== "CHECKSIG") throw new Error("Expected CHECKSIGADD or CHECKSIG after pubkey"); - d++; + l++; continue; } - if (d === a.length - 1) { + if (l === a.length - 1) { if (h !== "NUMEQUAL") throw new Error("Expected NUMEQUAL at end of script"); f = !0; @@ -7464,11 +7464,11 @@ var Gt; throw new Error("Missing NUMEQUAL operation"); if (u.length === 0) throw new Error("Invalid script: must have at least 1 pubkey"); - const l = n({ + const d = n({ pubkeys: u, type: t.CHECKSIGADD }); - if ($.encode(l.script) !== $.encode(c)) + if (U.encode(d.script) !== U.encode(c)) throw new Error("Invalid script format: script reconstruction mismatch"); return { type: ft.Multisig, @@ -7478,26 +7478,26 @@ var Gt; } function s(c) { const a = K.decode(c), u = []; - for (let l = 0; l < a.length; l++) { - const d = a[l]; - if (typeof d != "string" && typeof d != "number") { - if (d.length !== 32) - throw new Error(`Invalid pubkey length: expected 32, got ${d.length}`); - if (u.push(d), l + 1 >= a.length) + for (let d = 0; d < a.length; d++) { + const l = a[d]; + if (typeof l != "string" && typeof l != "number") { + if (l.length !== 32) + throw new Error(`Invalid pubkey length: expected 32, got ${l.length}`); + if (u.push(l), d + 1 >= a.length) throw new Error("Unexpected end of script"); - const h = a[l + 1]; + const h = a[d + 1]; if (h !== "CHECKSIGVERIFY" && h !== "CHECKSIG") throw new Error("Expected CHECKSIGVERIFY or CHECKSIG after pubkey"); - if (l === a.length - 2 && h !== "CHECKSIG") + if (d === a.length - 2 && h !== "CHECKSIG") throw new Error("Last operation must be CHECKSIG"); - l++; + d++; continue; } } if (u.length === 0) throw new Error("Invalid script: must have at least 1 pubkey"); const f = n({ pubkeys: u, type: t.CHECKSIG }); - if ($.encode(f.script) !== $.encode(c)) + if (U.encode(f.script) !== U.encode(c)) throw new Error("Invalid script format: script reconstruction mismatch"); return { type: ft.Multisig, @@ -7509,18 +7509,18 @@ var Gt; return c.type === ft.Multisig; } e.is = i; -})(Gt || (Gt = {})); -var $t; +})(qt || (qt = {})); +var Ut; (function(e) { function t(o) { for (const u of o.pubkeys) if (u.length !== 32) throw new Error(`Invalid pubkey length: expected 32, got ${u.length}`); - const s = Kr.encode(BigInt(cs.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }))), i = [ + const s = zr.encode(BigInt(hs.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }))), i = [ s.length === 1 ? s[0] : s, "CHECKSEQUENCEVERIFY", "DROP" - ], c = Gt.encode(o), a = new Uint8Array([ + ], c = qt.encode(o), a = new Uint8Array([ ...K.encode(i), ...c.script ]); @@ -7545,22 +7545,22 @@ var $t; const c = new Uint8Array(K.encode(s.slice(3))); let a; try { - a = Gt.decode(c); + a = qt.decode(c); } catch (h) { throw new Error(`Invalid multisig script: ${h instanceof Error ? h.message : String(h)}`); } let u; - typeof i == "number" ? u = i : u = Number(Kr.decode(i)); - const f = cs.decode(u), l = f.blocks !== void 0 ? { type: "blocks", value: BigInt(f.blocks) } : { type: "seconds", value: BigInt(f.seconds) }, d = t({ - timelock: l, + typeof i == "number" ? u = i : u = Number(zr.decode(i)); + const f = hs.decode(u), d = f.blocks !== void 0 ? { type: "blocks", value: BigInt(f.blocks) } : { type: "seconds", value: BigInt(f.seconds) }, l = t({ + timelock: d, ...a.params }); - if ($.encode(d.script) !== $.encode(o)) + if (U.encode(l.script) !== U.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { type: ft.CSVMultisig, params: { - timelock: l, + timelock: d, ...a.params }, script: o @@ -7571,14 +7571,14 @@ var $t; return o.type === ft.CSVMultisig; } e.is = r; -})($t || ($t = {})); -var Mn; +})(Ut || (Ut = {})); +var Fn; (function(e) { function t(o) { const s = new Uint8Array([ ...o.conditionScript, ...K.encode(["VERIFY"]), - ...$t.encode(o).script + ...Ut.encode(o).script ]); return { type: ft.ConditionCSVMultisig, @@ -7594,22 +7594,22 @@ var Mn; if (s.length < 1) throw new Error("Invalid script: too short (expected at least 1)"); let i = -1; - for (let l = s.length - 1; l >= 0; l--) - s[l] === "VERIFY" && (i = l); + for (let d = s.length - 1; d >= 0; d--) + s[d] === "VERIFY" && (i = d); if (i === -1) throw new Error("Invalid script: missing VERIFY operation"); const c = new Uint8Array(K.encode(s.slice(0, i))), a = new Uint8Array(K.encode(s.slice(i + 1))); let u; try { - u = $t.decode(a); - } catch (l) { - throw new Error(`Invalid CSV multisig script: ${l instanceof Error ? l.message : String(l)}`); + u = Ut.decode(a); + } catch (d) { + throw new Error(`Invalid CSV multisig script: ${d instanceof Error ? d.message : String(d)}`); } const f = t({ conditionScript: c, ...u.params }); - if ($.encode(f.script) !== $.encode(o)) + if (U.encode(f.script) !== U.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { type: ft.ConditionCSVMultisig, @@ -7625,14 +7625,14 @@ var Mn; return o.type === ft.ConditionCSVMultisig; } e.is = r; -})(Mn || (Mn = {})); -var Fr; +})(Fn || (Fn = {})); +var Gr; (function(e) { function t(o) { const s = new Uint8Array([ ...o.conditionScript, ...K.encode(["VERIFY"]), - ...Gt.encode(o).script + ...qt.encode(o).script ]); return { type: ft.ConditionMultisig, @@ -7648,22 +7648,22 @@ var Fr; if (s.length < 1) throw new Error("Invalid script: too short (expected at least 1)"); let i = -1; - for (let l = s.length - 1; l >= 0; l--) - s[l] === "VERIFY" && (i = l); + for (let d = s.length - 1; d >= 0; d--) + s[d] === "VERIFY" && (i = d); if (i === -1) throw new Error("Invalid script: missing VERIFY operation"); const c = new Uint8Array(K.encode(s.slice(0, i))), a = new Uint8Array(K.encode(s.slice(i + 1))); let u; try { - u = Gt.decode(a); - } catch (l) { - throw new Error(`Invalid multisig script: ${l instanceof Error ? l.message : String(l)}`); + u = qt.decode(a); + } catch (d) { + throw new Error(`Invalid multisig script: ${d instanceof Error ? d.message : String(d)}`); } const f = t({ conditionScript: c, ...u.params }); - if ($.encode(f.script) !== $.encode(o)) + if (U.encode(f.script) !== U.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { type: ft.ConditionMultisig, @@ -7679,17 +7679,17 @@ var Fr; return o.type === ft.ConditionMultisig; } e.is = r; -})(Fr || (Fr = {})); -var Kn; +})(Gr || (Gr = {})); +var Wn; (function(e) { function t(o) { - const s = Kr.encode(o.absoluteTimelock), i = [ + const s = zr.encode(o.absoluteTimelock), i = [ s.length === 1 ? s[0] : s, "CHECKLOCKTIMEVERIFY", "DROP" ], c = K.encode(i), a = new Uint8Array([ ...c, - ...Gt.encode(o).script + ...qt.encode(o).script ]); return { type: ft.CLTVMultisig, @@ -7712,15 +7712,15 @@ var Kn; const c = new Uint8Array(K.encode(s.slice(3))); let a; try { - a = Gt.decode(c); - } catch (l) { - throw new Error(`Invalid multisig script: ${l instanceof Error ? l.message : String(l)}`); + a = qt.decode(c); + } catch (d) { + throw new Error(`Invalid multisig script: ${d instanceof Error ? d.message : String(d)}`); } - const u = Kr.decode(i), f = t({ + const u = zr.decode(i), f = t({ absoluteTimelock: u, ...a.params }); - if ($.encode(f.script) !== $.encode(o)) + if (U.encode(f.script) !== U.encode(o)) throw new Error("Invalid script format: script reconstruction mismatch"); return { type: ft.CLTVMultisig, @@ -7736,30 +7736,30 @@ var Kn; return o.type === ft.CLTVMultisig; } e.is = r; -})(Kn || (Kn = {})); -const hc = _n.tapTree[2]; -function Nn(e) { +})(Wn || (Wn = {})); +const yc = Vn.tapTree[2]; +function Ln(e) { return e[1].subarray(0, e[1].length - 1); } -class _t { +class Ct { static decode(t) { - const r = hc.decode(t).map((o) => o.script); - return new _t(r); + const r = yc.decode(t).map((o) => o.script); + return new Ct(r); } constructor(t) { this.scripts = t; - const n = t.length % 2 !== 0 ? t.slice().reverse() : t, r = Ia(n.map((s) => ({ + const n = t.length % 2 !== 0 ? t.slice().reverse() : t, r = $a(n.map((s) => ({ script: s, - leafVersion: Pn - }))), o = Dl(Ls, r, void 0, !0); + leafVersion: Hn + }))), o = qd(Ds, r, void 0, !0); if (!o.tapLeafScript || o.tapLeafScript.length !== t.length) throw new Error("invalid scripts"); this.leaves = o.tapLeafScript, this.tweakedPublicKey = o.tweakedPubkey; } encode() { - return hc.encode(this.scripts.map((n) => ({ + return yc.encode(this.scripts.map((n) => ({ depth: 1, - version: Pn, + version: Hn, script: n }))); } @@ -7770,13 +7770,13 @@ class _t { return K.encode(["OP_1", this.tweakedPublicKey]); } onchainAddress(t) { - return Me(t).encode({ + return Ke(t).encode({ type: "tr", pubkey: this.tweakedPublicKey }); } findLeaf(t) { - const n = this.leaves.find((r) => $.encode(Nn(r)) === t); + const n = this.leaves.find((r) => U.encode(Ln(r)) === t); if (!n) throw new Error(`leaf '${t}' not found`); return n; @@ -7785,12 +7785,12 @@ class _t { const t = []; for (const n of this.leaves) try { - const r = $t.decode(Nn(n)); + const r = Ut.decode(Ln(n)); t.push(r); continue; } catch { try { - const o = Mn.decode(Nn(n)); + const o = Fn.decode(Ln(n)); t.push(o); } catch { continue; @@ -7799,28 +7799,28 @@ class _t { return t; } } -var pc; +var mc; (function(e) { - class t extends _t { + class t extends Ct { constructor(o) { n(o); - const { sender: s, receiver: i, server: c, preimageHash: a, refundLocktime: u, unilateralClaimDelay: f, unilateralRefundDelay: l, unilateralRefundWithoutReceiverDelay: d } = o, h = Th(a), w = Fr.encode({ + const { sender: s, receiver: i, server: c, preimageHash: a, refundLocktime: u, unilateralClaimDelay: f, unilateralRefundDelay: d, unilateralRefundWithoutReceiverDelay: l } = o, h = $h(a), w = Gr.encode({ conditionScript: h, pubkeys: [i, c] - }).script, g = Gt.encode({ + }).script, g = qt.encode({ pubkeys: [s, i, c] - }).script, y = Kn.encode({ + }).script, y = Wn.encode({ absoluteTimelock: u, pubkeys: [s, c] - }).script, S = Mn.encode({ + }).script, S = Fn.encode({ conditionScript: h, timelock: f, pubkeys: [i] - }).script, v = $t.encode({ - timelock: l, - pubkeys: [s, i] - }).script, O = $t.encode({ + }).script, T = Ut.encode({ timelock: d, + pubkeys: [s, i] + }).script, k = Ut.encode({ + timelock: l, pubkeys: [s] }).script; super([ @@ -7828,9 +7828,9 @@ var pc; g, y, S, - v, - O - ]), this.options = o, this.claimScript = $.encode(w), this.refundScript = $.encode(g), this.refundWithoutReceiverScript = $.encode(y), this.unilateralClaimScript = $.encode(S), this.unilateralRefundScript = $.encode(v), this.unilateralRefundWithoutReceiverScript = $.encode(O); + T, + k + ]), this.options = o, this.claimScript = U.encode(w), this.refundScript = U.encode(g), this.refundWithoutReceiverScript = U.encode(y), this.unilateralClaimScript = U.encode(S), this.unilateralRefundScript = U.encode(T), this.unilateralRefundWithoutReceiverScript = U.encode(k); } claim() { return this.findLeaf(this.claimScript); @@ -7853,7 +7853,7 @@ var pc; } e.Script = t; function n(r) { - const { sender: o, receiver: s, server: i, preimageHash: c, refundLocktime: a, unilateralClaimDelay: u, unilateralRefundDelay: f, unilateralRefundWithoutReceiverDelay: l } = r; + const { sender: o, receiver: s, server: i, preimageHash: c, refundLocktime: a, unilateralClaimDelay: u, unilateralRefundDelay: f, unilateralRefundWithoutReceiverDelay: d } = r; if (!c || c.length !== 20) throw new Error("preimage hash must be 20 bytes"); if (!s || s.length !== 32) @@ -7876,28 +7876,28 @@ var pc; throw new Error("seconds timelock must be multiple of 512"); if (f.type === "seconds" && f.value < 512n) throw new Error("seconds timelock must be greater or equal to 512"); - if (!l || typeof l.value != "bigint" || l.value <= 0n) + if (!d || typeof d.value != "bigint" || d.value <= 0n) throw new Error("unilateral refund without receiver delay must greater than 0"); - if (l.type === "seconds" && l.value % 512n !== 0n) + if (d.type === "seconds" && d.value % 512n !== 0n) throw new Error("seconds timelock must be multiple of 512"); - if (l.type === "seconds" && l.value < 512n) + if (d.type === "seconds" && d.value < 512n) throw new Error("seconds timelock must be greater or equal to 512"); } -})(pc || (pc = {})); -function Th(e) { +})(mc || (mc = {})); +function $h(e) { return K.encode(["HASH160", e, "EQUAL"]); } -var Wr; +var qr; (function(e) { - class t extends _t { + class t extends Ct { constructor(r) { - const { pubKey: o, serverPubKey: s, csvTimelock: i = t.DEFAULT_TIMELOCK } = r, c = Gt.encode({ + const { pubKey: o, serverPubKey: s, csvTimelock: i = t.DEFAULT_TIMELOCK } = r, c = qt.encode({ pubkeys: [o, s] - }).script, a = $t.encode({ + }).script, a = Ut.encode({ timelock: i, pubkeys: [o] }).script; - super([c, a]), this.options = r, this.forfeitScript = $.encode(c), this.exitScript = $.encode(a); + super([c, a]), this.options = r, this.forfeitScript = U.encode(c), this.exitScript = U.encode(a); } forfeit() { return this.findLeaf(this.forfeitScript); @@ -7910,48 +7910,48 @@ var Wr; value: 144n, type: "blocks" }, e.Script = t; -})(Wr || (Wr = {})); -var Fn; +})(qr || (qr = {})); +var gn; (function(e) { e.TxSent = "SENT", e.TxReceived = "RECEIVED"; -})(Fn || (Fn = {})); -function ae(e) { +})(gn || (gn = {})); +function ue(e) { return !e.isSpent; } -function fs(e) { - return e.virtualStatus.state === "swept" && ae(e); +function ws(e) { + return e.virtualStatus.state === "swept" && ue(e); } -function Hu(e, t) { +function Wu(e, t) { return e.value < t; } -function Vu(e, t, n) { +function zu(e, t, n) { const r = []; let o = [...t]; for (const i of [...e, ...t]) { if (i.virtualStatus.state !== "preconfirmed" && i.virtualStatus.commitmentTxIds && i.virtualStatus.commitmentTxIds.some((h) => n.has(h))) continue; - const c = Ah(o, i); - o = gc(o, c); - const a = cr(c); + const c = Rh(o, i); + o = bc(o, c); + const a = ar(c); if (i.value <= a) continue; - const u = Ih(o, i); - o = gc(o, u); - const f = cr(u); + const u = Nh(o, i); + o = bc(o, u); + const f = ar(u); if (i.value <= f) continue; - const l = { - commitmentTxid: i.spentBy || "", + const d = { + commitmentTxid: "", boardingTxid: "", arkTxid: "" }; - let d = i.virtualStatus.state !== "preconfirmed"; - i.virtualStatus.state === "preconfirmed" && (l.arkTxid = i.txid, i.spentBy && (d = !0)), r.push({ - key: l, + let l = i.virtualStatus.state !== "preconfirmed"; + i.virtualStatus.state === "preconfirmed" ? (d.arkTxid = i.txid, i.spentBy && (l = !0)) : d.commitmentTxid = i.virtualStatus.commitmentTxIds?.[0] || "", r.push({ + key: d, amount: i.value - a - f, - type: Fn.TxReceived, + type: gn.TxReceived, createdAt: i.createdAt.getTime(), - settled: d + settled: l }); } const s = /* @__PURE__ */ new Map(); @@ -7968,40 +7968,40 @@ function Vu(e, t, n) { s.set(i.arkTxId, [...c, i]); } for (const [i, c] of s) { - const a = kh([...e, ...t], i), u = cr(a), f = cr(c); + const a = Lh([...e, ...t], i), u = ar(a), f = ar(c); if (f <= u) continue; - const l = Bh(a, c), d = { - commitmentTxid: l.virtualStatus.commitmentTxIds?.[0] || "", + const d = _h(a, c), l = { + commitmentTxid: "", boardingTxid: "", arkTxid: "" }; - l.virtualStatus.state === "preconfirmed" && (d.arkTxid = l.txid), r.push({ - key: d, + d.virtualStatus.state === "preconfirmed" ? l.arkTxid = u === 0 ? d.arkTxId : d.txid : l.commitmentTxid = d.virtualStatus.commitmentTxIds?.[0] || "", r.push({ + key: l, amount: f - u, - type: Fn.TxSent, - createdAt: l.createdAt.getTime(), + type: gn.TxSent, + createdAt: d.createdAt.getTime(), settled: !0 }); } return r; } -function Ah(e, t) { +function Rh(e, t) { return t.virtualStatus.state === "preconfirmed" ? [] : e.filter((n) => n.settledBy ? t.virtualStatus.commitmentTxIds?.includes(n.settledBy) ?? !1 : !1); } -function Ih(e, t) { +function Nh(e, t) { return e.filter((n) => n.arkTxId ? n.arkTxId === t.txid : !1); } -function kh(e, t) { +function Lh(e, t) { return e.filter((n) => n.virtualStatus.state !== "preconfirmed" && n.virtualStatus.commitmentTxIds?.includes(t) ? !0 : n.txid === t); } -function cr(e) { +function ar(e) { return e.reduce((t, n) => t + n.value, 0); } -function Bh(e, t) { +function _h(e, t) { return e.length === 0 ? t[0] : e[0]; } -function gc(e, t) { +function bc(e, t) { return e.filter((n) => { for (const r of t) if (n.txid === r.txid && n.vout === r.vout) @@ -8009,32 +8009,32 @@ function gc(e, t) { return !0; }); } -const Oh = (e) => $h[e], $h = { - bitcoin: In(un, "ark"), - testnet: In(tr, "tark"), - signet: In(tr, "tark"), - mutinynet: In(tr, "tark"), - regtest: In({ - ...tr, +const Ch = (e) => Ph[e], Ph = { + bitcoin: kn(un, "ark"), + testnet: kn(er, "tark"), + signet: kn(er, "tark"), + mutinynet: kn(er, "tark"), + regtest: kn({ + ...er, bech32: "bcrt", pubKeyHash: 111, scriptHash: 196 }, "tark") }; -function In(e, t) { +function kn(e, t) { return { ...e, hrp: t }; } -const Uh = { +const Vh = { bitcoin: "https://mempool.space/api", testnet: "https://mempool.space/testnet/api", signet: "https://mempool.space/signet/api", mutinynet: "https://mutinynet.com/api", regtest: "http://localhost:3000" }; -class Rh { +class Hh { constructor(t, n) { this.baseUrl = t, this.pollingInterval = n?.pollingInterval ?? 15e3, this.forcePolling = n?.forcePolling ?? !1; } @@ -8095,13 +8095,13 @@ class Rh { async watchAddresses(t, n) { let r = null; const o = this.baseUrl.replace(/^http(s)?:/, "ws$1:") + "/v1/ws", s = async () => { - const a = async () => (await Promise.all(t.map((h) => this.getTransactions(h)))).flat(), u = await a(), f = (d) => `${d.txid}_${d.status.block_time}`, l = new Set(u.map(f)); + const a = async () => (await Promise.all(t.map((h) => this.getTransactions(h)))).flat(), u = await a(), f = (l) => `${l.txid}_${l.status.block_time}`, d = new Set(u.map(f)); r = setInterval(async () => { try { - const h = (await a()).filter((w) => !l.has(f(w))); - h.length > 0 && (h.forEach((w) => l.add(f(w))), n(h)); - } catch (d) { - console.error("Error in polling mechanism:", d); + const h = (await a()).filter((w) => !d.has(f(w))); + h.length > 0 && (h.forEach((w) => d.add(f(w))), n(h)); + } catch (l) { + console.error("Error in polling mechanism:", l); } }, this.pollingInterval); }; @@ -8122,14 +8122,14 @@ class Rh { const u = [], f = JSON.parse(a.data.toString()); if (!f["multi-address-transactions"]) return; - const l = f["multi-address-transactions"]; - for (const d in l) + const d = f["multi-address-transactions"]; + for (const l in d) for (const h of [ "mempool", "confirmed", "removed" ]) - l[d][h] && u.push(...l[d][h].filter(Lh)); + d[l][h] && u.push(...d[l][h].filter(Mh)); u.length > 0 && n(u); } catch (u) { console.error("Failed to process WebSocket message:", u); @@ -8147,7 +8147,7 @@ class Rh { if (!t.ok) throw new Error(`Failed to get chain tip: ${t.statusText}`); const n = await t.json(); - if (!Nh(n)) + if (!Dh(n)) throw new Error(`Invalid chain tip: ${JSON.stringify(n)}`); if (n.length === 0) throw new Error("No chain tip found"); @@ -8187,13 +8187,13 @@ class Rh { return n.text(); } } -function Nh(e) { +function Dh(e) { return Array.isArray(e) && e.every((t) => { t && typeof t == "object" && typeof t.id == "string" && t.id.length > 0 && typeof t.height == "number" && t.height >= 0 && typeof t.mediantime == "number" && t.mediantime > 0; }); } -const Lh = (e) => typeof e.txid == "string" && Array.isArray(e.vout) && e.vout.every((t) => typeof t.scriptpubkey_address == "string" && typeof t.value == "number") && typeof e.status == "object" && typeof e.status.confirmed == "boolean"; -async function* ls(e) { +const Mh = (e) => typeof e.txid == "string" && Array.isArray(e.vout) && e.vout.every((t) => typeof t.scriptpubkey_address == "string" && typeof t.value == "number") && typeof e.status == "object" && typeof e.status.confirmed == "boolean"; +async function* ys(e) { const t = [], n = []; let r = null, o = null; const s = (c) => { @@ -8222,12 +8222,12 @@ async function* ls(e) { e.removeEventListener("message", s), e.removeEventListener("error", i); } } -class Ch extends Error { +class Gu extends Error { constructor(t, n, r, o) { super(n), this.code = t, this.message = n, this.name = r, this.metadata = o; } } -function _h(e) { +function Kh(e) { try { if (!(e instanceof Error)) return; @@ -8245,21 +8245,21 @@ function _h(e) { continue; const i = n.name; let c; - return "metadata" in n && Ph(n.metadata) && (c = n.metadata), new Ch(o, s, i, c); + return "metadata" in n && Fh(n.metadata) && (c = n.metadata), new Gu(o, s, i, c); } return; } catch { return; } } -function Ph(e) { +function Fh(e) { return typeof e == "object" && e !== null && !Array.isArray(e); } var J; (function(e) { e.BatchStarted = "batch_started", e.BatchFinalization = "batch_finalization", e.BatchFinalized = "batch_finalized", e.BatchFailed = "batch_failed", e.TreeSigningStarted = "tree_signing_started", e.TreeNonces = "tree_nonces", e.TreeTx = "tree_tx", e.TreeSignature = "tree_signature"; })(J || (J = {})); -class Du { +class qu { constructor(t) { this.serverUrl = t; } @@ -8267,7 +8267,7 @@ class Du { const t = `${this.serverUrl}/v1/info`, n = await fetch(t); if (!n.ok) { const o = await n.text(); - Yt(o, `Failed to get server info: ${n.statusText}`); + Zt(o, `Failed to get server info: ${n.statusText}`); } const r = await n.json(); return { @@ -8279,7 +8279,14 @@ class Du { })) ?? [], digest: r.digest ?? "", dust: BigInt(r.dust ?? 0), - fees: r.fees, + fees: { + intentFee: { + ...r.fees?.intentFee, + onchainInput: BigInt(r.fees?.intentFee?.onchainInput ?? 0), + onchainOutput: BigInt(r.fees?.intentFee?.onchainOutput ?? 0) + }, + txFeeRate: r?.fees?.txFeeRate ?? "" + }, forfeitAddress: r.forfeitAddress ?? "", forfeitPubkey: r.forfeitPubkey ?? "", network: r.network ?? "", @@ -8313,7 +8320,7 @@ class Du { }); if (!o.ok) { const i = await o.text(); - Yt(i, `Failed to submit virtual transaction: ${i}`); + Zt(i, `Failed to submit virtual transaction: ${i}`); } const s = await o.json(); return { @@ -8335,7 +8342,7 @@ class Du { }); if (!o.ok) { const s = await o.text(); - Yt(s, `Failed to finalize offchain transaction: ${s}`); + Zt(s, `Failed to finalize offchain transaction: ${s}`); } } async registerIntent(t) { @@ -8353,7 +8360,7 @@ class Du { }); if (!r.ok) { const s = await r.text(); - Yt(s, `Failed to register intent: ${s}`); + Zt(s, `Failed to register intent: ${s}`); } return (await r.json()).intentId; } @@ -8364,7 +8371,7 @@ class Du { "Content-Type": "application/json" }, body: JSON.stringify({ - proof: { + intent: { proof: t.proof, message: t.message } @@ -8372,7 +8379,7 @@ class Du { }); if (!r.ok) { const o = await r.text(); - Yt(o, `Failed to delete intent: ${o}`); + Zt(o, `Failed to delete intent: ${o}`); } } async confirmRegistration(t) { @@ -8387,7 +8394,7 @@ class Du { }); if (!r.ok) { const o = await r.text(); - Yt(o, `Failed to confirm registration: ${o}`); + Zt(o, `Failed to confirm registration: ${o}`); } } async submitTreeNonces(t, n, r) { @@ -8399,12 +8406,12 @@ class Du { body: JSON.stringify({ batchId: t, pubkey: n, - treeNonces: Hh(r) + treeNonces: Wh(r) }) }); if (!s.ok) { const i = await s.text(); - Yt(i, `Failed to submit tree nonces: ${i}`); + Zt(i, `Failed to submit tree nonces: ${i}`); } } async submitTreeSignatures(t, n, r) { @@ -8416,12 +8423,12 @@ class Du { body: JSON.stringify({ batchId: t, pubkey: n, - treeSignatures: Vh(r) + treeSignatures: zh(r) }) }); if (!s.ok) { const i = await s.text(); - Yt(i, `Failed to submit tree signatures: ${i}`); + Zt(i, `Failed to submit tree signatures: ${i}`); } } async submitSignedForfeitTxs(t, n) { @@ -8437,7 +8444,7 @@ class Du { }); if (!o.ok) { const s = await o.text(); - Yt(s, `Failed to submit forfeit transactions: ${o.statusText}`); + Zt(s, `Failed to submit forfeit transactions: ${o.statusText}`); } } async *getEventStream(t, n) { @@ -8449,7 +8456,7 @@ class Du { }; t?.addEventListener("abort", i); try { - for await (const c of ls(s)) { + for await (const c of ys(s)) { if (t?.aborted) break; try { @@ -8465,7 +8472,7 @@ class Du { } catch (s) { if (s instanceof Error && s.name === "AbortError") break; - if (ds(s)) { + if (ms(s)) { console.debug("Timeout error ignored"); continue; } @@ -8481,7 +8488,7 @@ class Du { }; t?.addEventListener("abort", o); try { - for await (const s of ls(r)) { + for await (const s of ys(r)) { if (t?.aborted) break; try { @@ -8497,7 +8504,7 @@ class Du { } catch (r) { if (r instanceof Error && r.name === "AbortError") break; - if (ds(r)) { + if (ms(r)) { console.debug("Timeout error ignored"); continue; } @@ -8514,7 +8521,7 @@ class Du { }); if (!r.ok) { const s = await r.text(); - Yt(s, `Failed to get pending transactions: ${s}`); + Zt(s, `Failed to get pending transactions: ${s}`); } return (await r.json()).pendingTxs; } @@ -8559,7 +8566,7 @@ class Du { id: t.treeNonces.id, topic: t.treeNonces.topic, txid: t.treeNonces.txid, - nonces: Dh(t.treeNonces.nonces) + nonces: Gh(t.treeNonces.nonces) // pubkey -> public nonce }; if (t.treeTx) { @@ -8590,45 +8597,45 @@ class Du { commitmentTx: { txid: t.commitmentTx.txid, tx: t.commitmentTx.tx, - spentVtxos: t.commitmentTx.spentVtxos.map(ar), - spendableVtxos: t.commitmentTx.spendableVtxos.map(ar), + spentVtxos: t.commitmentTx.spentVtxos.map(ur), + spendableVtxos: t.commitmentTx.spendableVtxos.map(ur), checkpointTxs: t.commitmentTx.checkpointTxs } } : t.arkTx ? { arkTx: { txid: t.arkTx.txid, tx: t.arkTx.tx, - spentVtxos: t.arkTx.spentVtxos.map(ar), - spendableVtxos: t.arkTx.spendableVtxos.map(ar), + spentVtxos: t.arkTx.spentVtxos.map(ur), + spendableVtxos: t.arkTx.spendableVtxos.map(ur), checkpointTxs: t.arkTx.checkpointTxs } } : (t.heartbeat || console.warn("Unknown transaction notification type:", t), null); } } -function Hh(e) { +function Wh(e) { const t = {}; for (const [n, r] of e) - t[n] = $.encode(r.pubNonce); + t[n] = U.encode(r.pubNonce); return t; } -function Vh(e) { +function zh(e) { const t = {}; for (const [n, r] of e) - t[n] = $.encode(r.encode()); + t[n] = U.encode(r.encode()); return t; } -function Dh(e) { +function Gh(e) { return new Map(Object.entries(e).map(([t, n]) => { if (typeof n != "string") throw new Error("invalid nonce"); - return [t, { pubNonce: $.decode(n) }]; + return [t, { pubNonce: U.decode(n) }]; })); } -function ds(e) { +function ms(e) { const t = (n) => n instanceof Error ? n.name === "TypeError" && n.message === "Failed to fetch" || n.name === "HeadersTimeoutError" || n.name === "BodyTimeoutError" || n.code === "UND_ERR_HEADERS_TIMEOUT" || n.code === "UND_ERR_BODY_TIMEOUT" : !1; return t(e) || t(e.cause); } -function ar(e) { +function ur(e) { return { outpoint: { txid: e.outpoint.txid, @@ -8648,17 +8655,17 @@ function ar(e) { arkTxid: e.arkTxid }; } -function Yt(e, t) { +function Zt(e, t) { const n = new Error(e); - throw _h(n) ?? new Error(t); + throw Kh(n) ?? new Error(t); } -const Mh = 0n, Kh = new Uint8Array([81, 2, 78, 115]), ii = { - script: Kh, - amount: Mh +const qh = 0n, jh = new Uint8Array([81, 2, 78, 115]), fi = { + script: jh, + amount: qh }; -$.encode(ii.script); -function Fh(e, t, n) { - const r = new ke({ +U.encode(fi.script); +function Yh(e, t, n) { + const r = new Be({ version: 3, lockTime: n }); @@ -8671,76 +8678,86 @@ function Fh(e, t, n) { return r.addOutput({ script: t, amount: o - }), r.addOutput(ii), r; + }), r.addOutput(fi), r; } -const Wh = new Error("invalid settlement transaction outputs"), zh = new Error("empty tree"), Gh = new Error("invalid number of inputs"), $o = new Error("wrong settlement txid"), qh = new Error("invalid amount"), jh = new Error("no leaves"), Yh = new Error("invalid taproot script"), wc = new Error("invalid round transaction outputs"), Zh = new Error("wrong commitment txid"), Xh = new Error("missing cosigners public keys"), Uo = 0, yc = 1; -function Qh(e, t) { +const Zh = new Error("invalid settlement transaction outputs"), Xh = new Error("empty tree"), Qh = new Error("invalid number of inputs"), No = new Error("wrong settlement txid"), Jh = new Error("invalid amount"), tp = new Error("no leaves"), ep = new Error("invalid taproot script"), Ec = new Error("invalid round transaction outputs"), np = new Error("wrong commitment txid"), rp = new Error("missing cosigners public keys"), Lo = 0, xc = 1; +function op(e, t) { if (t.validate(), t.root.inputsLength !== 1) - throw Gh; - const n = t.root.getInput(0), r = ue.fromPSBT(It.decode(e)); - if (r.outputsLength <= yc) - throw Wh; + throw Qh; + const n = t.root.getInput(0), r = Kt.fromPSBT(wt.decode(e)); + if (r.outputsLength <= xc) + throw Zh; const o = r.id; - if (!n.txid || $.encode(n.txid) !== o || n.index !== yc) - throw $o; + if (!n.txid || U.encode(n.txid) !== o || n.index !== xc) + throw No; } -function Jh(e, t, n) { - if (t.outputsLength < Uo + 1) - throw wc; - const r = t.getOutput(Uo)?.amount; +function sp(e, t, n) { + if (t.outputsLength < Lo + 1) + throw Ec; + const r = t.getOutput(Lo)?.amount; if (!r) - throw wc; + throw Ec; if (!e.root) - throw zh; + throw Xh; const o = e.root.getInput(0), s = t.id; - if (!o.txid || $.encode(o.txid) !== s || o.index !== Uo) - throw Zh; + if (!o.txid || U.encode(o.txid) !== s || o.index !== Lo) + throw np; let i = 0n; for (let a = 0; a < e.root.outputsLength; a++) { const u = e.root.getOutput(a); u?.amount && (i += u.amount); } if (i !== r) - throw qh; + throw Jh; if (e.leaves().length === 0) - throw jh; + throw tp; e.validate(); for (const a of e.iterator()) for (const [u, f] of a.children) { - const l = a.root.getOutput(u); - if (!l?.script) + const d = a.root.getOutput(u); + if (!d?.script) throw new Error(`parent output ${u} not found`); - const d = l.script.slice(2); - if (d.length !== 32) + const l = d.script.slice(2); + if (l.length !== 32) throw new Error(`parent output ${u} has invalid script`); - const h = as(f.root, 0, us); + const h = ps(f.root, 0, gs); if (h.length === 0) - throw Xh; - const w = h.map((y) => y.key), { finalKey: g } = ei(w, !0, { + throw rp; + const w = h.map((y) => y.key), { finalKey: g } = si(w, !0, { taprootTweak: n }); - if (!g || $.encode(g.slice(1)) !== $.encode(d)) - throw Yh; + if (!g || U.encode(g.slice(1)) !== U.encode(l)) + throw ep; } } -function tp(e, t, n) { - const r = e.map((s) => ep(s, n)); +function ip(e, t, n) { + let r = !1; + for (const [i, c] of t.entries()) { + if (!c.script) + throw new Error(`missing output script ${i}`); + if (K.decode(c.script)[0] === "RETURN") { + if (r) + throw new Error("multiple OP_RETURN outputs"); + r = !0; + } + } + const o = e.map((i) => cp(i, n)); return { - arkTx: Mu(r.map((s) => s.input), t), - checkpoints: r.map((s) => s.tx) + arkTx: ju(o.map((i) => i.input), t), + checkpoints: o.map((i) => i.tx) }; } -function Mu(e, t) { +function ju(e, t) { let n = 0n; for (const o of e) { - const s = Pu(Nn(o.tapLeafScript)); - if (Kn.is(s)) { - if (n !== 0n && mc(n) !== mc(s.params.absoluteTimelock)) + const s = Fu(Ln(o.tapLeafScript)); + if (Wn.is(s)) { + if (n !== 0n && Sc(n) !== Sc(s.params.absoluteTimelock)) throw new Error("cannot mix seconds and blocks locktime"); s.params.absoluteTimelock > n && (n = s.params.absoluteTimelock); } } - const r = new ke({ + const r = new Be({ version: 3, lockTime: Number(n) }); @@ -8748,27 +8765,27 @@ function Mu(e, t) { r.addInput({ txid: s.txid, index: s.vout, - sequence: n ? Ds - 1 : void 0, + sequence: n ? Gs - 1 : void 0, witnessUtxo: { - script: _t.decode(s.tapTree).pkScript, + script: Ct.decode(s.tapTree).pkScript, amount: BigInt(s.value) }, tapLeafScript: [s.tapLeafScript] - }), xh(r, o, _u, s.tapTree); + }), Bh(r, o, Ku, s.tapTree); for (const o of t) r.addOutput(o); - return r.addOutput(ii), r; + return r.addOutput(fi), r; } -function ep(e, t) { - const n = Pu(Nn(e.tapLeafScript)), r = new _t([ +function cp(e, t) { + const n = Fu(Ln(e.tapLeafScript)), r = new Ct([ t.script, n.script - ]), o = Mu([e], [ + ]), o = ju([e], [ { amount: BigInt(e.value), script: r.pkScript } - ]), s = r.findLeaf($.encode(n.script)), i = { + ]), s = r.findLeaf(U.encode(n.script)), i = { txid: o.id, vout: 0, value: e.value, @@ -8780,11 +8797,11 @@ function ep(e, t) { input: i }; } -const np = 500000000n; -function mc(e) { - return e >= np; +const ap = 500000000n; +function Sc(e) { + return e >= ap; } -function rp(e, t) { +function up(e, t) { if (!e.status.block_time) return !1; if (t.value === 0n) @@ -8794,37 +8811,38 @@ function rp(e, t) { const n = BigInt(Math.floor(Date.now() / 1e3)); return BigInt(Math.floor(e.status.block_time)) + t.value <= n; } -const op = { - thresholdPercentage: 10 +const fp = 4320 * 60 * 1e3, dp = { + thresholdMs: fp + // 3 days }; class at { constructor(t, n, r = at.DefaultHRP) { this.preimage = t, this.value = n, this.HRP = r, this.vout = 0; - const o = wt(this.preimage); - this.vtxoScript = new _t([cp(o)]); + const o = yt(this.preimage); + this.vtxoScript = new Ct([pp(o)]); const s = this.vtxoScript.leaves[0]; - this.txid = $.encode(new Uint8Array(o).reverse()), this.tapTree = this.vtxoScript.encode(), this.forfeitTapLeafScript = s, this.intentTapLeafScript = s, this.value = n, this.status = { confirmed: !0 }, this.extraWitness = [this.preimage]; + this.txid = U.encode(new Uint8Array(o).reverse()), this.tapTree = this.vtxoScript.encode(), this.forfeitTapLeafScript = s, this.intentTapLeafScript = s, this.value = n, this.status = { confirmed: !0 }, this.extraWitness = [this.preimage]; } encode() { const t = new Uint8Array(at.Length); - return t.set(this.preimage, 0), sp(t, this.value, this.preimage.length), t; + return t.set(this.preimage, 0), lp(t, this.value, this.preimage.length), t; } static decode(t, n = at.DefaultHRP) { if (t.length !== at.Length) throw new Error(`invalid data length: expected ${at.Length} bytes, got ${t.length}`); - const r = t.subarray(0, at.PreimageLength), o = ip(t, at.PreimageLength); + const r = t.subarray(0, at.PreimageLength), o = hp(t, at.PreimageLength); return new at(r, o, n); } static fromString(t, n = at.DefaultHRP) { if (t = t.trim(), !t.startsWith(n)) throw new Error(`invalid human-readable part: expected ${n} prefix (note '${t}')`); - const r = t.slice(n.length), o = Po.decode(r); + const r = t.slice(n.length), o = Fo.decode(r); if (o.length === 0) throw new Error("failed to decode base58 string"); return at.decode(o, n); } toString() { - return this.HRP + Po.encode(this.encode()); + return this.HRP + Fo.encode(this.encode()); } } at.DefaultHRP = "arknote"; @@ -8832,28 +8850,28 @@ at.PreimageLength = 32; at.ValueLength = 4; at.Length = at.PreimageLength + at.ValueLength; at.FakeOutpointIndex = 0; -function sp(e, t, n) { +function lp(e, t, n) { new DataView(e.buffer, e.byteOffset + n, 4).setUint32(0, t, !1); } -function ip(e, t) { +function hp(e, t) { return new DataView(e.buffer, e.byteOffset + t, 4).getUint32(0, !1); } -function cp(e) { +function pp(e) { return K.encode(["SHA256", e, "EQUAL"]); } -var zr; +var _n; (function(e) { function t(n, r, o = []) { if (r.length == 0) throw new Error("intent proof requires at least one input"); - hp(r), gp(o); - const s = wp(n, r[0].witnessUtxo.script); - return yp(s, r, o); + Ep(r), Sp(o); + const s = Tp(n, r[0].witnessUtxo.script); + return vp(s, r, o); } e.create = t; -})(zr || (zr = {})); -const ap = new Uint8Array([lt.RETURN]), up = new Uint8Array(32).fill(0), fp = 4294967295, lp = "ark-intent-proof-message"; -function dp(e) { +})(_n || (_n = {})); +const gp = new Uint8Array([dt.RETURN]), wp = new Uint8Array(32).fill(0), yp = 4294967295, mp = "ark-intent-proof-message"; +function bp(e) { if (e.index === void 0) throw new Error("intent proof input requires index"); if (e.txid === void 0) @@ -8862,27 +8880,27 @@ function dp(e) { throw new Error("intent proof input requires witness utxo"); return !0; } -function hp(e) { - return e.forEach(dp), !0; +function Ep(e) { + return e.forEach(bp), !0; } -function pp(e) { +function xp(e) { if (e.amount === void 0) throw new Error("intent proof output requires amount"); if (e.script === void 0) throw new Error("intent proof output requires script"); return !0; } -function gp(e) { - return e.forEach(pp), !0; +function Sp(e) { + return e.forEach(xp), !0; } -function wp(e, t) { - const n = mp(e), r = new ke({ +function Tp(e, t) { + const n = Ap(e), r = new Be({ version: 0 }); return r.addInput({ - txid: up, + txid: wp, // zero hash - index: fp, + index: yp, sequence: 0 }), r.addOutput({ amount: 0n, @@ -8891,8 +8909,8 @@ function wp(e, t) { finalScriptSig: K.encode(["OP_0", n]) }), r; } -function yp(e, t, n) { - const r = t[0], o = new ke({ +function vp(e, t, n) { + const r = t[0], o = new Be({ version: 2, lockTime: 0 }); @@ -8904,19 +8922,19 @@ function yp(e, t, n) { script: r.witnessUtxo.script, amount: 0n }, - sighashType: Ke.ALL + sighashType: Fe.ALL }); for (const [s, i] of t.entries()) o.addInput({ ...i, - sighashType: Ke.ALL + sighashType: Fe.ALL }), i.unknown?.length && o.updateInput(s + 1, { unknown: i.unknown }); n.length === 0 && (n = [ { amount: 0n, - script: ap + script: gp } ]); for (const s of n) @@ -8926,18 +8944,18 @@ function yp(e, t, n) { }); return o; } -function mp(e) { - return ti.utils.taggedHash(lp, new TextEncoder().encode(e)); +function Ap(e) { + return oi.utils.taggedHash(mp, new TextEncoder().encode(e)); } -var hs; +var bs; (function(e) { e[e.INDEXER_TX_TYPE_UNSPECIFIED = 0] = "INDEXER_TX_TYPE_UNSPECIFIED", e[e.INDEXER_TX_TYPE_RECEIVED = 1] = "INDEXER_TX_TYPE_RECEIVED", e[e.INDEXER_TX_TYPE_SENT = 2] = "INDEXER_TX_TYPE_SENT"; -})(hs || (hs = {})); -var en; +})(bs || (bs = {})); +var nn; (function(e) { e.UNSPECIFIED = "INDEXER_CHAINED_TX_TYPE_UNSPECIFIED", e.COMMITMENT = "INDEXER_CHAINED_TX_TYPE_COMMITMENT", e.ARK = "INDEXER_CHAINED_TX_TYPE_ARK", e.TREE = "INDEXER_CHAINED_TX_TYPE_TREE", e.CHECKPOINT = "INDEXER_CHAINED_TX_TYPE_CHECKPOINT"; -})(en || (en = {})); -class Ku { +})(nn || (nn = {})); +class Yu { constructor(t) { this.serverUrl = t; } @@ -8949,7 +8967,7 @@ class Ku { if (!s.ok) throw new Error(`Failed to fetch vtxo tree: ${s.statusText}`); const i = await s.json(); - if (!Ht.isVtxoTreeResponse(i)) + if (!Vt.isVtxoTreeResponse(i)) throw new Error("Invalid vtxo tree data received"); return i.vtxoTree.forEach((c) => { c.children = Object.fromEntries(Object.entries(c.children).map(([a, u]) => [ @@ -8966,7 +8984,7 @@ class Ku { if (!s.ok) throw new Error(`Failed to fetch vtxo tree leaves: ${s.statusText}`); const i = await s.json(); - if (!Ht.isVtxoTreeLeavesResponse(i)) + if (!Vt.isVtxoTreeLeavesResponse(i)) throw new Error("Invalid vtxos tree leaves data received"); return i; } @@ -8975,7 +8993,7 @@ class Ku { if (!r.ok) throw new Error(`Failed to fetch batch sweep transactions: ${r.statusText}`); const o = await r.json(); - if (!Ht.isBatchSweepTransactionsResponse(o)) + if (!Vt.isBatchSweepTransactionsResponse(o)) throw new Error("Invalid batch sweep transactions data received"); return o; } @@ -8984,7 +9002,7 @@ class Ku { if (!r.ok) throw new Error(`Failed to fetch commitment tx: ${r.statusText}`); const o = await r.json(); - if (!Ht.isCommitmentTx(o)) + if (!Vt.isCommitmentTx(o)) throw new Error("Invalid commitment tx data received"); return o; } @@ -8996,7 +9014,7 @@ class Ku { if (!s.ok) throw new Error(`Failed to fetch commitment tx connectors: ${s.statusText}`); const i = await s.json(); - if (!Ht.isConnectorsResponse(i)) + if (!Vt.isConnectorsResponse(i)) throw new Error("Invalid commitment tx connectors data received"); return i.connectors.forEach((c) => { c.children = Object.fromEntries(Object.entries(c.children).map(([a, u]) => [ @@ -9013,7 +9031,7 @@ class Ku { if (!s.ok) throw new Error(`Failed to fetch commitment tx forfeitTxs: ${s.statusText}`); const i = await s.json(); - if (!Ht.isForfeitTxsResponse(i)) + if (!Vt.isForfeitTxsResponse(i)) throw new Error("Invalid commitment tx forfeitTxs data received"); return i; } @@ -9026,7 +9044,7 @@ class Ku { }; n?.addEventListener("abort", s); try { - for await (const i of ls(o)) { + for await (const i of ys(o)) { if (n?.aborted) break; try { @@ -9034,9 +9052,9 @@ class Ku { c.event && (yield { txid: c.event.txid, scripts: c.event.scripts || [], - newVtxos: (c.event.newVtxos || []).map(ur), - spentVtxos: (c.event.spentVtxos || []).map(ur), - sweptVtxos: (c.event.sweptVtxos || []).map(ur), + newVtxos: (c.event.newVtxos || []).map(fr), + spentVtxos: (c.event.spentVtxos || []).map(fr), + sweptVtxos: (c.event.sweptVtxos || []).map(fr), tx: c.event.tx, checkpointTxs: c.event.checkpointTxs }); @@ -9050,7 +9068,7 @@ class Ku { } catch (o) { if (o instanceof Error && o.name === "AbortError") break; - if (ds(o)) { + if (ms(o)) { console.debug("Timeout error ignored"); continue; } @@ -9065,7 +9083,7 @@ class Ku { if (!s.ok) throw new Error(`Failed to fetch virtual txs: ${s.statusText}`); const i = await s.json(); - if (!Ht.isVirtualTxsResponse(i)) + if (!Vt.isVirtualTxsResponse(i)) throw new Error("Invalid virtual txs data received"); return i; } @@ -9077,7 +9095,7 @@ class Ku { if (!s.ok) throw new Error(`Failed to fetch vtxo chain: ${s.statusText}`); const i = await s.json(); - if (!Ht.isVtxoChainResponse(i)) + if (!Vt.isVtxoChainResponse(i)) throw new Error("Invalid vtxo chain data received"); return i; } @@ -9097,10 +9115,10 @@ class Ku { if (!o.ok) throw new Error(`Failed to fetch vtxos: ${o.statusText}`); const s = await o.json(); - if (!Ht.isVtxosResponse(s)) + if (!Vt.isVtxosResponse(s)) throw new Error("Invalid vtxos data received"); return { - vtxos: s.vtxos.map(ur), + vtxos: s.vtxos.map(fr), page: s.page }; } @@ -9135,7 +9153,7 @@ class Ku { } } } -function ur(e) { +function fr(e) { return { txid: e.outpoint.txid, vout: e.outpoint.vout, @@ -9156,13 +9174,13 @@ function ur(e) { isSpent: e.isSpent }; } -var Ht; +var Vt; (function(e) { function t(x) { return typeof x == "object" && typeof x.totalOutputAmount == "string" && typeof x.totalOutputVtxos == "number" && typeof x.expiresAt == "string" && typeof x.swept == "boolean"; } function n(x) { - return typeof x == "object" && typeof x.txid == "string" && typeof x.expiresAt == "string" && Object.values(en).includes(x.type) && Array.isArray(x.spends) && x.spends.every((Q) => typeof Q == "string"); + return typeof x == "object" && typeof x.txid == "string" && typeof x.expiresAt == "string" && Object.values(nn).includes(x.type) && Array.isArray(x.spends) && x.spends.every((Y) => typeof Y == "string"); } function r(x) { return typeof x == "object" && typeof x.startedAt == "string" && typeof x.endedAt == "string" && typeof x.totalInputAmount == "string" && typeof x.totalInputVtxos == "number" && typeof x.totalOutputAmount == "string" && typeof x.totalOutputVtxos == "number" && typeof x.batches == "object" && Object.values(x.batches).every(t); @@ -9177,14 +9195,14 @@ var Ht; } e.isOutpointArray = s; function i(x) { - return typeof x == "object" && typeof x.txid == "string" && typeof x.children == "object" && Object.values(x.children).every(f) && Object.keys(x.children).every((Q) => Number.isInteger(Number(Q))); + return typeof x == "object" && typeof x.txid == "string" && typeof x.children == "object" && Object.values(x.children).every(f) && Object.keys(x.children).every((Y) => Number.isInteger(Number(Y))); } function c(x) { return Array.isArray(x) && x.every(i); } e.isTxsArray = c; function a(x) { - return typeof x == "object" && typeof x.amount == "string" && typeof x.createdAt == "string" && typeof x.isSettled == "boolean" && typeof x.settledBy == "string" && Object.values(hs).includes(x.type) && (!x.commitmentTxid && typeof x.virtualTxid == "string" || typeof x.commitmentTxid == "string" && !x.virtualTxid); + return typeof x == "object" && typeof x.amount == "string" && typeof x.createdAt == "string" && typeof x.isSettled == "boolean" && typeof x.settledBy == "string" && Object.values(bs).includes(x.type) && (!x.commitmentTxid && typeof x.virtualTxid == "string" || typeof x.commitmentTxid == "string" && !x.virtualTxid); } function u(x) { return Array.isArray(x) && x.every(a); @@ -9193,11 +9211,11 @@ var Ht; function f(x) { return typeof x == "string" && x.length === 64; } - function l(x) { + function d(x) { return Array.isArray(x) && x.every(f); } - e.isTxidArray = l; - function d(x) { + e.isTxidArray = d; + function l(x) { return typeof x == "object" && o(x.outpoint) && typeof x.createdAt == "string" && (x.expiresAt === null || typeof x.expiresAt == "string") && typeof x.amount == "string" && typeof x.script == "string" && typeof x.isPreconfirmed == "boolean" && typeof x.isSwept == "boolean" && typeof x.isUnrolled == "boolean" && typeof x.isSpent == "boolean" && (!x.spentBy || typeof x.spentBy == "string") && (!x.settledBy || typeof x.settledBy == "string") && (!x.arkTxid || typeof x.arkTxid == "string") && Array.isArray(x.commitmentTxids) && x.commitmentTxids.every(f); } function h(x) { @@ -9219,28 +9237,28 @@ var Ht; return typeof x == "object" && Array.isArray(x.txids) && x.txids.every(f) && (!x.page || h(x.page)); } e.isForfeitTxsResponse = S; - function v(x) { + function T(x) { return typeof x == "object" && Array.isArray(x.sweptBy) && x.sweptBy.every(f); } - e.isSweptCommitmentTxResponse = v; - function O(x) { + e.isSweptCommitmentTxResponse = T; + function k(x) { return typeof x == "object" && Array.isArray(x.sweptBy) && x.sweptBy.every(f); } - e.isBatchSweepTransactionsResponse = O; + e.isBatchSweepTransactionsResponse = k; function N(x) { - return typeof x == "object" && Array.isArray(x.txs) && x.txs.every((Q) => typeof Q == "string") && (!x.page || h(x.page)); + return typeof x == "object" && Array.isArray(x.txs) && x.txs.every((Y) => typeof Y == "string") && (!x.page || h(x.page)); } e.isVirtualTxsResponse = N; - function U(x) { + function $(x) { return typeof x == "object" && Array.isArray(x.chain) && x.chain.every(n) && (!x.page || h(x.page)); } - e.isVtxoChainResponse = U; - function W(x) { - return typeof x == "object" && Array.isArray(x.vtxos) && x.vtxos.every(d) && (!x.page || h(x.page)); + e.isVtxoChainResponse = $; + function F(x) { + return typeof x == "object" && Array.isArray(x.vtxos) && x.vtxos.every(l) && (!x.page || h(x.page)); } - e.isVtxosResponse = W; -})(Ht || (Ht = {})); -class ps { + e.isVtxosResponse = F; +})(Vt || (Vt = {})); +class Es { constructor(t, n = /* @__PURE__ */ new Map()) { this.root = t, this.children = n; } @@ -9249,14 +9267,14 @@ class ps { throw new Error("empty chunks"); const n = /* @__PURE__ */ new Map(); for (const s of t) { - const i = Ep(s), c = i.tx.id; + const i = kp(s), c = i.tx.id; n.set(c, i); } const r = []; for (const [s] of n) { let i = !1; for (const [c, a] of n) - if (c !== s && (i = bp(a, s), i)) + if (c !== s && (i = Ip(a, s), i)) break; if (!i) { r.push(s); @@ -9267,7 +9285,7 @@ class ps { throw new Error("no root chunk found"); if (r.length > 1) throw new Error(`multiple root chunks found: ${r.join(", ")}`); - const o = Fu(r[0], n); + const o = Zu(r[0], n); if (!o) throw new Error(`chunk not found for root txid: ${r[0]}`); if (o.nbOfNodes() !== t.length) @@ -9293,7 +9311,7 @@ class ps { throw new Error(`output index ${r} is out of bounds (nb of outputs: ${t})`); o.validate(); const s = o.root.getInput(0), i = this.root.id; - if (!s.txid || $.encode(s.txid) !== i || s.index !== r) + if (!s.txid || U.encode(s.txid) !== i || s.index !== r) throw new Error(`input of child ${r} is not the output of the parent`); let c = 0n; for (let u = 0; u < o.root.outputsLength; u++) { @@ -9348,24 +9366,24 @@ class ps { yield this; } } -function bp(e, t) { +function Ip(e, t) { return Object.values(e.children).includes(t); } -function Fu(e, t) { +function Zu(e, t) { const n = t.get(e); if (!n) return null; const r = n.tx, o = /* @__PURE__ */ new Map(); for (const [s, i] of Object.entries(n.children)) { - const c = parseInt(s), a = Fu(i, t); + const c = parseInt(s), a = Zu(i, t); a && o.set(c, a); } - return new ps(r, o); + return new Es(r, o); } -function Ep(e) { - return { tx: ue.fromPSBT(It.decode(e.tx)), children: e.children }; +function kp(e) { + return { tx: Kt.fromPSBT(wt.decode(e.tx)), children: e.children }; } -class xp { +class Bp { constructor() { this.store = /* @__PURE__ */ new Map(); } @@ -9382,60 +9400,50 @@ class xp { this.store.clear(); } } -const Gr = (e) => e ? $.encode(e) : void 0, gn = (e) => e ? $.decode(e) : void 0, qr = ([e, t]) => ({ - cb: $.encode(Zt.encode(e)), - s: $.encode(t) -}), bc = (e) => ({ +const dr = (e) => `vtxos:${e}`, lr = (e) => `utxos:${e}`, _o = (e) => `tx:${e}`, Tc = "wallet:state", jr = (e) => e ? U.encode(e) : void 0, wn = (e) => e ? U.decode(e) : void 0, Yr = ([e, t]) => ({ + cb: U.encode(Xt.encode(e)), + s: U.encode(t) +}), vc = (e) => ({ ...e, - tapTree: Gr(e.tapTree), - forfeitTapLeafScript: qr(e.forfeitTapLeafScript), - intentTapLeafScript: qr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(Gr) -}), Ec = (e) => ({ + tapTree: jr(e.tapTree), + forfeitTapLeafScript: Yr(e.forfeitTapLeafScript), + intentTapLeafScript: Yr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(jr) +}), Ac = (e) => ({ ...e, - tapTree: Gr(e.tapTree), - forfeitTapLeafScript: qr(e.forfeitTapLeafScript), - intentTapLeafScript: qr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(Gr) -}), jr = (e) => { - const t = Zt.decode(gn(e.cb)), n = gn(e.s); + tapTree: jr(e.tapTree), + forfeitTapLeafScript: Yr(e.forfeitTapLeafScript), + intentTapLeafScript: Yr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(jr) +}), Zr = (e) => { + const t = Xt.decode(wn(e.cb)), n = wn(e.s); return [t, n]; -}, Sp = (e) => ({ +}, Op = (e) => ({ ...e, createdAt: new Date(e.createdAt), - tapTree: gn(e.tapTree), - forfeitTapLeafScript: jr(e.forfeitTapLeafScript), - intentTapLeafScript: jr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(gn) -}), vp = (e) => ({ + tapTree: wn(e.tapTree), + forfeitTapLeafScript: Zr(e.forfeitTapLeafScript), + intentTapLeafScript: Zr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(wn) +}), Up = (e) => ({ ...e, - tapTree: gn(e.tapTree), - forfeitTapLeafScript: jr(e.forfeitTapLeafScript), - intentTapLeafScript: jr(e.intentTapLeafScript), - extraWitness: e.extraWitness?.map(gn) + tapTree: wn(e.tapTree), + forfeitTapLeafScript: Zr(e.forfeitTapLeafScript), + intentTapLeafScript: Zr(e.intentTapLeafScript), + extraWitness: e.extraWitness?.map(wn) }); -class gs { +class xs { constructor(t) { - this.storage = t, this.cache = { - vtxos: /* @__PURE__ */ new Map(), - utxos: /* @__PURE__ */ new Map(), - transactions: /* @__PURE__ */ new Map(), - walletState: null, - initialized: /* @__PURE__ */ new Set() - }; + this.storage = t; } async getVtxos(t) { - const n = `vtxos:${t}`; - if (this.cache.vtxos.has(t)) - return this.cache.vtxos.get(t); - const r = await this.storage.getItem(n); - if (!r) - return this.cache.vtxos.set(t, []), []; + const n = await this.storage.getItem(dr(t)); + if (!n) + return []; try { - const s = JSON.parse(r).map(Sp); - return this.cache.vtxos.set(t, s.slice()), s.slice(); - } catch (o) { - return console.error(`Failed to parse VTXOs for address ${t}:`, o), this.cache.vtxos.set(t, []), []; + return JSON.parse(n).map(Op); + } catch (r) { + return console.error(`Failed to parse VTXOs for address ${t}:`, r), []; } } async saveVtxos(t, n) { @@ -9444,27 +9452,23 @@ class gs { const s = r.findIndex((i) => i.txid === o.txid && i.vout === o.vout); s !== -1 ? r[s] = o : r.push(o); } - this.cache.vtxos.set(t, r.slice()), await this.storage.setItem(`vtxos:${t}`, JSON.stringify(r.map(bc))); + await this.storage.setItem(dr(t), JSON.stringify(r.map(vc))); } async removeVtxo(t, n) { const r = await this.getVtxos(t), [o, s] = n.split(":"), i = r.filter((c) => !(c.txid === o && c.vout === parseInt(s, 10))); - this.cache.vtxos.set(t, i.slice()), await this.storage.setItem(`vtxos:${t}`, JSON.stringify(i.map(bc))); + await this.storage.setItem(dr(t), JSON.stringify(i.map(vc))); } async clearVtxos(t) { - this.cache.vtxos.set(t, []), await this.storage.removeItem(`vtxos:${t}`); + await this.storage.removeItem(dr(t)); } async getUtxos(t) { - const n = `utxos:${t}`; - if (this.cache.utxos.has(t)) - return this.cache.utxos.get(t); - const r = await this.storage.getItem(n); - if (!r) - return this.cache.utxos.set(t, []), []; + const n = await this.storage.getItem(lr(t)); + if (!n) + return []; try { - const s = JSON.parse(r).map(vp); - return this.cache.utxos.set(t, s.slice()), s.slice(); - } catch (o) { - return console.error(`Failed to parse UTXOs for address ${t}:`, o), this.cache.utxos.set(t, []), []; + return JSON.parse(n).map(Up); + } catch (r) { + return console.error(`Failed to parse UTXOs for address ${t}:`, r), []; } } async saveUtxos(t, n) { @@ -9472,27 +9476,23 @@ class gs { n.forEach((o) => { const s = r.findIndex((i) => i.txid === o.txid && i.vout === o.vout); s !== -1 ? r[s] = o : r.push(o); - }), this.cache.utxos.set(t, r.slice()), await this.storage.setItem(`utxos:${t}`, JSON.stringify(r.map(Ec))); + }), await this.storage.setItem(lr(t), JSON.stringify(r.map(Ac))); } async removeUtxo(t, n) { const r = await this.getUtxos(t), [o, s] = n.split(":"), i = r.filter((c) => !(c.txid === o && c.vout === parseInt(s, 10))); - this.cache.utxos.set(t, i.slice()), await this.storage.setItem(`utxos:${t}`, JSON.stringify(i.map(Ec))); + await this.storage.setItem(lr(t), JSON.stringify(i.map(Ac))); } async clearUtxos(t) { - this.cache.utxos.set(t, []), await this.storage.removeItem(`utxos:${t}`); + await this.storage.removeItem(lr(t)); } async getTransactionHistory(t) { - const n = `tx:${t}`; - if (this.cache.transactions.has(t)) - return this.cache.transactions.get(t); - const r = await this.storage.getItem(n); + const n = _o(t), r = await this.storage.getItem(n); if (!r) - return this.cache.transactions.set(t, []), []; + return []; try { - const o = JSON.parse(r); - return this.cache.transactions.set(t, o), o.slice(); + return JSON.parse(r); } catch (o) { - return console.error(`Failed to parse transactions for address ${t}:`, o), this.cache.transactions.set(t, []), []; + return console.error(`Failed to parse transactions for address ${t}:`, o), []; } } async saveTransactions(t, n) { @@ -9501,109 +9501,96 @@ class gs { const s = r.findIndex((i) => i.key === o.key); s !== -1 ? r[s] = o : r.push(o); } - this.cache.transactions.set(t, r), await this.storage.setItem(`tx:${t}`, JSON.stringify(r)); + await this.storage.setItem(_o(t), JSON.stringify(r)); } async clearTransactions(t) { - this.cache.transactions.set(t, []), await this.storage.removeItem(`tx:${t}`); + await this.storage.removeItem(_o(t)); } async getWalletState() { - if (this.cache.walletState !== null || this.cache.initialized.has("walletState")) - return this.cache.walletState; - const t = await this.storage.getItem("wallet:state"); + const t = await this.storage.getItem(Tc); if (!t) - return this.cache.walletState = null, this.cache.initialized.add("walletState"), null; + return null; try { - const n = JSON.parse(t); - return this.cache.walletState = n, this.cache.initialized.add("walletState"), n; + return JSON.parse(t); } catch (n) { - return console.error("Failed to parse wallet state:", n), this.cache.walletState = null, this.cache.initialized.add("walletState"), null; + return console.error("Failed to parse wallet state:", n), null; } } async saveWalletState(t) { - this.cache.walletState = t, await this.storage.setItem("wallet:state", JSON.stringify(t)); + await this.storage.setItem(Tc, JSON.stringify(t)); } } -class Tp { +const Co = (e, t) => `contract:${e}:${t}`, Po = (e) => `collection:${e}`; +class $p { constructor(t) { - this.cache = /* @__PURE__ */ new Map(), this.storage = t; + this.storage = t; } async getContractData(t, n) { - const r = `contract:${t}:${n}`, o = this.cache.get(r); - if (o !== void 0) - return o; - const s = await this.storage.getItem(r); - if (!s) + const r = await this.storage.getItem(Co(t, n)); + if (!r) return null; try { - const i = JSON.parse(s); - return this.cache.set(r, i), i; - } catch (i) { - return console.error(`Failed to parse contract data for ${t}:${n}:`, i), null; + return JSON.parse(r); + } catch (o) { + return console.error(`Failed to parse contract data for ${t}:${n}:`, o), null; } } async setContractData(t, n, r) { - const o = `contract:${t}:${n}`; try { - await this.storage.setItem(o, JSON.stringify(r)), this.cache.set(o, r); - } catch (s) { - throw console.error(`Failed to persist contract data for ${t}:${n}:`, s), s; + await this.storage.setItem(Co(t, n), JSON.stringify(r)); + } catch (o) { + throw console.error(`Failed to persist contract data for ${t}:${n}:`, o), o; } } async deleteContractData(t, n) { - const r = `contract:${t}:${n}`; try { - await this.storage.removeItem(r), this.cache.delete(r); - } catch (o) { - throw console.error(`Failed to remove contract data for ${t}:${n}:`, o), o; + await this.storage.removeItem(Co(t, n)); + } catch (r) { + throw console.error(`Failed to remove contract data for ${t}:${n}:`, r), r; } } async getContractCollection(t) { - const n = `collection:${t}`, r = this.cache.get(n); - if (r !== void 0) - return r; - const o = await this.storage.getItem(n); - if (!o) - return this.cache.set(n, []), []; + const n = await this.storage.getItem(Po(t)); + if (!n) + return []; try { - const s = JSON.parse(o); - return this.cache.set(n, s), s; - } catch (s) { - return console.error(`Failed to parse contract collection ${t}:`, s), this.cache.set(n, []), []; + return JSON.parse(n); + } catch (r) { + return console.error(`Failed to parse contract collection ${t}:`, r), []; } } async saveToContractCollection(t, n, r) { const o = await this.getContractCollection(t), s = n[r]; if (s == null) throw new Error(`Item is missing required field '${String(r)}'`); - const i = o.findIndex((u) => u[r] === s); + const i = o.findIndex((a) => a[r] === s); let c; i !== -1 ? c = [ ...o.slice(0, i), n, ...o.slice(i + 1) ] : c = [...o, n]; - const a = `collection:${t}`; try { - await this.storage.setItem(a, JSON.stringify(c)), this.cache.set(a, c); - } catch (u) { - throw console.error(`Failed to persist contract collection ${t}:`, u), u; + await this.storage.setItem(Po(t), JSON.stringify(c)); + } catch (a) { + throw console.error(`Failed to persist contract collection ${t}:`, a), a; } } async removeFromContractCollection(t, n, r) { if (n == null) throw new Error(`Invalid id provided for removal: ${String(n)}`); - const s = (await this.getContractCollection(t)).filter((c) => c[r] !== n), i = `collection:${t}`; + const s = (await this.getContractCollection(t)).filter((i) => i[r] !== n); try { - await this.storage.setItem(i, JSON.stringify(s)), this.cache.set(i, s); - } catch (c) { - throw console.error(`Failed to persist contract collection removal for ${t}:`, c), c; + await this.storage.setItem(Po(t), JSON.stringify(s)); + } catch (i) { + throw console.error(`Failed to persist contract collection removal for ${t}:`, i), i; } } async clearContractData() { - await this.storage.clear(), this.cache.clear(); + await this.storage.clear(); } } -function nn(e, t) { +function xe(e, t) { return { ...t, forfeitTapLeafScript: e.offchainTapscript.forfeit(), @@ -9611,7 +9598,7 @@ function nn(e, t) { tapTree: e.offchainTapscript.encode() }; } -function ws(e, t) { +function Ss(e, t) { return { ...t, forfeitTapLeafScript: e.boardingTapscript.forfeit(), @@ -9619,11 +9606,11 @@ function ws(e, t) { tapTree: e.boardingTapscript.encode() }; } -class wn { - constructor(t, n, r, o, s, i, c, a, u, f, l, d, h, w, g, y) { - this.identity = t, this.network = n, this.networkName = r, this.onchainProvider = o, this.arkProvider = s, this.indexerProvider = i, this.arkServerPublicKey = c, this.offchainTapscript = a, this.boardingTapscript = u, this.serverUnrollScript = f, this.forfeitOutputScript = l, this.forfeitPubkey = d, this.dustAmount = h, this.walletRepository = w, this.contractRepository = g, this.renewalConfig = { +class yn { + constructor(t, n, r, o, s, i, c, a, u, f, d, l, h, w, g, y) { + this.identity = t, this.network = n, this.networkName = r, this.onchainProvider = o, this.arkProvider = s, this.indexerProvider = i, this.arkServerPublicKey = c, this.offchainTapscript = a, this.boardingTapscript = u, this.serverUnrollScript = f, this.forfeitOutputScript = d, this.forfeitPubkey = l, this.dustAmount = h, this.walletRepository = w, this.contractRepository = g, this.renewalConfig = { enabled: y?.enabled ?? !1, - ...op, + ...dp, ...y }; } @@ -9634,34 +9621,46 @@ class wn { const r = t.arkProvider || (() => { if (!t.arkServerUrl) throw new Error("Either arkProvider or arkServerUrl must be provided"); - return new Du(t.arkServerUrl); + return new qu(t.arkServerUrl); })(), o = t.arkServerUrl || r.serverUrl; if (!o) throw new Error("Could not determine arkServerUrl from provider"); - const s = t.indexerUrl || o, i = t.indexerProvider || new Ku(s), c = await r.getInfo(), a = Oh(c.network), u = t.esploraUrl || Uh[c.network], f = t.onchainProvider || new Rh(u), l = { + const s = t.indexerUrl || o, i = t.indexerProvider || new Yu(s), c = await r.getInfo(), a = Ch(c.network), u = t.esploraUrl || Vh[c.network], f = t.onchainProvider || new Hh(u); + if (t.exitTimelock) { + const { value: Y, type: L } = t.exitTimelock; + if (Y < 512n && L !== "blocks" || Y >= 512n && L !== "seconds") + throw new Error("invalid exitTimelock"); + } + const d = t.exitTimelock ?? { value: c.unilateralExitDelay, type: c.unilateralExitDelay < 512n ? "blocks" : "seconds" - }, d = { + }; + if (t.boardingTimelock) { + const { value: Y, type: L } = t.boardingTimelock; + if (Y < 512n && L !== "blocks" || Y >= 512n && L !== "seconds") + throw new Error("invalid boardingTimelock"); + } + const l = t.boardingTimelock ?? { value: c.boardingExitDelay, type: c.boardingExitDelay < 512n ? "blocks" : "seconds" - }, h = $.decode(c.signerPubkey).slice(1), w = new Wr.Script({ + }, h = U.decode(c.signerPubkey).slice(1), w = new qr.Script({ pubKey: n, serverPubKey: h, - csvTimelock: l - }), g = new Wr.Script({ + csvTimelock: d + }), g = new qr.Script({ pubKey: n, serverPubKey: h, - csvTimelock: d + csvTimelock: l }), y = w; let S; try { - const Q = $.decode(c.checkpointTapscript); - S = $t.decode(Q); + const Y = U.decode(c.checkpointTapscript); + S = Ut.decode(Y); } catch { throw new Error("Invalid checkpointTapscript from server"); } - const v = $.decode(c.forfeitPubkey).slice(1), O = Me(a).decode(c.forfeitAddress), N = ut.encode(O), U = t.storage || new xp(), W = new gs(U), x = new Tp(U); - return new wn(t.identity, a, c.network, f, r, i, h, y, g, S, N, v, c.dust, W, x, t.renewalConfig); + const T = U.decode(c.forfeitPubkey).slice(1), k = Ke(a).decode(c.forfeitAddress), N = ut.encode(k), $ = t.storage || new Bp(), F = new xs($), x = new $p($); + return new yn(t.identity, a, c.network, f, r, i, h, y, g, S, N, T, c.dust, F, x, t.renewalConfig); } get arkAddress() { return this.offchainTapscript.address(this.network.hrp, this.arkServerPublicKey); @@ -9681,7 +9680,7 @@ class wn { for (const f of t) f.status.confirmed ? r += f.value : o += f.value; let s = 0, i = 0, c = 0; - s = n.filter((f) => f.virtualStatus.state === "settled").reduce((f, l) => f + l.value, 0), i = n.filter((f) => f.virtualStatus.state === "preconfirmed").reduce((f, l) => f + l.value, 0), c = n.filter((f) => ae(f) && f.virtualStatus.state === "swept").reduce((f, l) => f + l.value, 0); + s = n.filter((f) => f.virtualStatus.state === "settled").reduce((f, d) => f + d.value, 0), i = n.filter((f) => f.virtualStatus.state === "preconfirmed").reduce((f, d) => f + d.value, 0), c = n.filter((f) => ue(f) && f.virtualStatus.state === "swept").reduce((f, d) => f + d.value, 0); const a = r + o, u = s + i + c; return { boarding: { @@ -9697,14 +9696,14 @@ class wn { }; } async getVtxos(t) { - const n = await this.getAddress(), o = (await this.getVirtualCoins(t)).map((s) => nn(this, s)); + const n = await this.getAddress(), o = (await this.getVirtualCoins(t)).map((s) => xe(this, s)); return await this.walletRepository.saveVtxos(n, o), o; } async getVirtualCoins(t = { withRecoverable: !0, withUnrolled: !1 }) { - const n = [$.encode(this.offchainTapscript.pkScript)], o = (await this.indexerProvider.getVtxos({ scripts: n })).vtxos; - let s = o.filter(ae); - if (t.withRecoverable || (s = s.filter((i) => !fs(i))), t.withUnrolled) { - const i = o.filter((c) => !ae(c)); + const n = [U.encode(this.offchainTapscript.pkScript)], o = (await this.indexerProvider.getVtxos({ scripts: n })).vtxos; + let s = o.filter(ue); + if (t.withRecoverable || (s = s.filter((i) => !ws(i))), t.withUnrolled) { + const i = o.filter((c) => !ue(c)); s.push(...i.filter((c) => c.isUnrolled)); } return s; @@ -9713,11 +9712,11 @@ class wn { if (!this.indexerProvider) return []; const t = await this.indexerProvider.getVtxos({ - scripts: [$.encode(this.offchainTapscript.pkScript)] + scripts: [U.encode(this.offchainTapscript.pkScript)] }), { boardingTxs: n, commitmentsToIgnore: r } = await this.getBoardingTxs(), o = [], s = []; for (const a of t.vtxos) - ae(a) ? o.push(a) : s.push(a); - const i = Vu(o, s, r), c = [...n, ...i]; + ue(a) ? o.push(a) : s.push(a); + const i = zu(o, s, r), c = [...n, ...i]; return c.sort( // place createdAt = 0 (unconfirmed txs) first, then descending (a, u) => a.createdAt === 0 ? -1 : u.createdAt === 0 ? 1 : u.createdAt - a.createdAt @@ -9729,8 +9728,8 @@ class wn { for (let a = 0; a < c.vout.length; a++) { const u = c.vout[a]; if (u.scriptpubkey_address === r) { - const l = (await this.onchainProvider.getTxOutspends(c.txid))[a]; - l?.spent && n.add(l.txid), t.push({ + const d = (await this.onchainProvider.getTxOutspends(c.txid))[a]; + d?.spent && n.add(d.txid), t.push({ txid: c.txid, vout: a, value: Number(u.value), @@ -9740,8 +9739,8 @@ class wn { }, isUnrolled: !0, virtualStatus: { - state: l?.spent ? "spent" : "settled", - commitmentTxIds: l?.spent ? [l.txid] : void 0 + state: d?.spent ? "spent" : "settled", + commitmentTxIds: d?.spent ? [d.txid] : void 0 }, createdAt: c.status.confirmed ? new Date(c.status.block_time * 1e3) : /* @__PURE__ */ new Date(0) }); @@ -9756,7 +9755,7 @@ class wn { arkTxid: "" }, amount: c.value, - type: Fn.TxReceived, + type: gn.TxReceived, settled: c.virtualStatus.state === "spent", createdAt: c.status.block_time ? new Date(c.status.block_time * 1e3).getTime() : 0 }; @@ -9768,17 +9767,17 @@ class wn { }; } async getBoardingUtxos() { - const t = await this.getBoardingAddress(), r = (await this.onchainProvider.getCoins(t)).map((o) => ws(this, o)); + const t = await this.getBoardingAddress(), r = (await this.onchainProvider.getCoins(t)).map((o) => Ss(this, o)); return await this.walletRepository.saveUtxos(t, r), r; } async sendBitcoin(t) { if (t.amount <= 0) throw new Error("Amount must be positive"); - if (!Ip(t.address)) + if (!Np(t.address)) throw new Error("Invalid Ark address " + t.address); const n = await this.getVirtualCoins({ withRecoverable: !1 - }), r = kp(n, t.amount), o = this.offchainTapscript.forfeit(); + }), r = Lp(n, t.amount), o = this.offchainTapscript.forfeit(); if (!o) throw new Error("Selected leaf not found"); const s = pn.decode(t.address), c = [ @@ -9794,34 +9793,89 @@ class wn { amount: BigInt(r.changeAmount) }); } - const a = this.offchainTapscript.encode(); - let u = tp(r.inputs.map((w) => ({ + const a = this.offchainTapscript.encode(), u = ip(r.inputs.map((w) => ({ ...w, tapLeafScript: o, tapTree: a - })), c, this.serverUnrollScript); - const f = await this.identity.sign(u.arkTx), { arkTxid: l, signedCheckpointTxs: d } = await this.arkProvider.submitTx(It.encode(f.toPSBT()), u.checkpoints.map((w) => It.encode(w.toPSBT()))), h = await Promise.all(d.map(async (w) => { - const g = ue.fromPSBT(It.decode(w)), y = await this.identity.sign(g); - return It.encode(y.toPSBT()); + })), c, this.serverUnrollScript), f = await this.identity.sign(u.arkTx), { arkTxid: d, signedCheckpointTxs: l } = await this.arkProvider.submitTx(wt.encode(f.toPSBT()), u.checkpoints.map((w) => wt.encode(w.toPSBT()))), h = await Promise.all(l.map(async (w) => { + const g = Kt.fromPSBT(wt.decode(w)), y = await this.identity.sign(g); + return wt.encode(y.toPSBT()); })); - return await this.arkProvider.finalizeTx(l, h), l; + await this.arkProvider.finalizeTx(d, h); + try { + const w = [], g = /* @__PURE__ */ new Set(); + let y = Number.MAX_SAFE_INTEGER; + for (const [k, N] of r.inputs.entries()) { + const $ = xe(this, N), F = l[k], x = Kt.fromPSBT(wt.decode(F)); + if (w.push({ + ...$, + virtualStatus: { ...$.virtualStatus, state: "spent" }, + spentBy: x.id, + arkTxId: d, + isSpent: !0 + }), $.virtualStatus.commitmentTxIds) + for (const Y of $.virtualStatus.commitmentTxIds) + g.add(Y); + $.virtualStatus.batchExpiry && (y = Math.min(y, $.virtualStatus.batchExpiry)); + } + const S = Date.now(), T = this.arkAddress.encode(); + if (r.changeAmount > 0n && y !== Number.MAX_SAFE_INTEGER) { + const k = { + txid: d, + vout: c.length - 1, + createdAt: new Date(S), + forfeitTapLeafScript: this.offchainTapscript.forfeit(), + intentTapLeafScript: this.offchainTapscript.exit(), + isUnrolled: !1, + isSpent: !1, + tapTree: this.offchainTapscript.encode(), + value: Number(r.changeAmount), + virtualStatus: { + state: "preconfirmed", + commitmentTxIds: Array.from(g), + batchExpiry: y + }, + status: { + confirmed: !1 + } + }; + await this.walletRepository.saveVtxos(T, [k]); + } + await this.walletRepository.saveVtxos(T, w), await this.walletRepository.saveTransactions(T, [ + { + key: { + boardingTxid: "", + commitmentTxid: "", + arkTxid: d + }, + amount: t.amount, + type: gn.TxSent, + settled: !1, + createdAt: Date.now() + } + ]); + } catch (w) { + console.warn("error saving offchain tx to repository", w); + } finally { + return d; + } } async settle(t, n) { if (t?.inputs) { - for (const d of t.inputs) - if (typeof d == "string") + for (const l of t.inputs) + if (typeof l == "string") try { - at.fromString(d); + at.fromString(l); } catch { - throw new Error(`Invalid arknote "${d}"`); + throw new Error(`Invalid arknote "${l}"`); } } if (!t) { - let d = 0; - const w = $t.decode($.decode(this.boardingTapscript.exitScript)).params.timelock, g = (await this.getBoardingUtxos()).filter((v) => !rp(v, w)); - d += g.reduce((v, O) => v + O.value, 0); + let l = 0; + const w = Ut.decode(U.decode(this.boardingTapscript.exitScript)).params.timelock, g = (await this.getBoardingUtxos()).filter((T) => !up(T, w)); + l += g.reduce((T, k) => T + k.value, 0); const y = await this.getVtxos({ withRecoverable: !0 }); - d += y.reduce((v, O) => v + O.value, 0); + l += y.reduce((T, k) => T + k.value, 0); const S = [...g, ...y]; if (S.length === 0) throw new Error("No inputs found"); @@ -9830,20 +9884,20 @@ class wn { outputs: [ { address: await this.getAddress(), - amount: BigInt(d) + amount: BigInt(l) } ] }; } const r = [], o = []; let s = !1; - for (const [d, h] of t.outputs.entries()) { + for (const [l, h] of t.outputs.entries()) { let w; try { w = pn.decode(h.address).pkScript, s = !0; } catch { - const g = Me(this.network).decode(h.address); - w = ut.encode(g), r.push(d); + const g = Ke(this.network).decode(h.address); + w = ut.encode(g), r.push(l); } o.push({ amount: h.amount, @@ -9852,50 +9906,50 @@ class wn { } let i; const c = []; - s && (i = this.identity.signerSession(), c.push($.encode(await i.getPublicKey()))); + s && (i = this.identity.signerSession(), c.push(U.encode(await i.getPublicKey()))); const [a, u] = await Promise.all([ this.makeRegisterIntentSignature(t.inputs, o, r, c), this.makeDeleteIntentSignature(t.inputs) - ]), f = await this.arkProvider.registerIntent(a), l = new AbortController(); + ]), f = await this.safeRegisterIntent(a), d = new AbortController(); try { - let d; + let l; const h = [ ...c, - ...t.inputs.map((U) => `${U.txid}:${U.vout}`) - ], w = this.arkProvider.getEventStream(l.signal, h); + ...t.inputs.map(($) => `${$.txid}:${$.vout}`) + ], w = this.arkProvider.getEventStream(d.signal, h); let g, y; - const S = [], v = []; - let O, N; - for await (const U of w) - switch (n && n(U), U.type) { + const S = [], T = []; + let k, N; + for await (const $ of w) + switch (n && n($), $.type) { // the settlement failed case J.BatchFailed: - throw new Error(U.reason); + throw new Error($.reason); case J.BatchStarted: - if (d !== void 0) + if (l !== void 0) continue; - const W = await this.handleBatchStartedEvent(U, f, this.forfeitPubkey, this.forfeitOutputScript); - W.skip || (d = U.type, y = W.sweepTapTreeRoot, g = W.roundId, s || (d = J.TreeNonces)); + const F = await this.handleBatchStartedEvent($, f, this.forfeitPubkey, this.forfeitOutputScript); + F.skip || (l = $.type, y = F.sweepTapTreeRoot, g = F.roundId, s || (l = J.TreeNonces)); break; case J.TreeTx: - if (d !== J.BatchStarted && d !== J.TreeNonces) + if (l !== J.BatchStarted && l !== J.TreeNonces) continue; - if (U.batchIndex === 0) - S.push(U.chunk); - else if (U.batchIndex === 1) - v.push(U.chunk); + if ($.batchIndex === 0) + S.push($.chunk); + else if ($.batchIndex === 1) + T.push($.chunk); else - throw new Error(`Invalid batch index: ${U.batchIndex}`); + throw new Error(`Invalid batch index: ${$.batchIndex}`); break; case J.TreeSignature: - if (d !== J.TreeNonces || !s) + if (l !== J.TreeNonces || !s) continue; - if (!O) + if (!k) throw new Error("Vtxo graph not set, something went wrong"); - if (U.batchIndex === 0) { - const x = $.decode(U.signature); - O.update(U.txid, (Q) => { - Q.updateInput(0, { + if ($.batchIndex === 0) { + const x = U.decode($.signature); + k.update($.txid, (Y) => { + Y.updateInput(0, { tapKeySig: x }); }); @@ -9904,7 +9958,7 @@ class wn { // the server has started the signing process of the vtxo tree transactions // the server expects the partial musig2 nonces for each tx case J.TreeSigningStarted: - if (d !== J.BatchStarted) + if (l !== J.BatchStarted) continue; if (s) { if (!i) @@ -9913,46 +9967,47 @@ class wn { throw new Error("Sweep tap tree root not set"); if (S.length === 0) throw new Error("unsigned vtxo graph not received"); - O = ps.create(S), await this.handleSettlementSigningEvent(U, y, i, O); + k = Es.create(S), await this.handleSettlementSigningEvent($, y, i, k); } - d = U.type; + l = $.type; break; // the musig2 nonces of the vtxo tree transactions are generated // the server expects now the partial musig2 signatures case J.TreeNonces: - if (d !== J.TreeSigningStarted) + if (l !== J.TreeSigningStarted) continue; if (s) { if (!i) throw new Error("Signing session not set"); - await this.handleSettlementTreeNoncesEvent(U, i) && (d = U.type); + await this.handleSettlementTreeNoncesEvent($, i) && (l = $.type); break; } - d = U.type; + l = $.type; break; // the vtxo tree is signed, craft, sign and submit forfeit transactions // if any boarding utxos are involved, the settlement tx is also signed case J.BatchFinalization: - if (d !== J.TreeNonces) + if (l !== J.TreeNonces) continue; if (!this.forfeitOutputScript) throw new Error("Forfeit output script not set"); - v.length > 0 && (N = ps.create(v), Qh(U.commitmentTx, N)), await this.handleSettlementFinalizationEvent(U, t.inputs, this.forfeitOutputScript, N), d = U.type; + T.length > 0 && (N = Es.create(T), op($.commitmentTx, N)), await this.handleSettlementFinalizationEvent($, t.inputs, this.forfeitOutputScript, N), l = $.type; break; // the settlement is done, last event to be received case J.BatchFinalized: - if (d !== J.BatchFinalization) + if (l !== J.BatchFinalization) continue; - if (U.id === g) - return l.abort(), U.commitmentTxid; + if ($.id === g) + return d.abort(), $.commitmentTxid; } - } catch (d) { - l.abort(); + } catch (l) { + d.abort(); try { await this.arkProvider.deleteIntent(u); - } catch { + } catch (h) { + console.error("failed to delete intent: ", h); } - throw d; + throw l; } throw new Error("Settlement failed"); } @@ -9963,8 +10018,8 @@ class wn { const c = (a) => a.vout.findIndex((u) => u.scriptpubkey_address === r); o = await this.onchainProvider.watchAddresses([r], (a) => { const u = a.filter((f) => c(f) !== -1).map((f) => { - const { txid: l, status: d } = f, h = c(f), w = Number(f.vout[h].value); - return { txid: l, vout: h, value: w, status: d }; + const { txid: d, status: l } = f, h = c(f), w = Number(f.vout[h].value); + return { txid: d, vout: h, value: w, status: l }; }); t({ type: "utxo", @@ -9974,20 +10029,20 @@ class wn { } if (this.indexerProvider && n) { const c = this.offchainTapscript, a = await this.indexerProvider.subscribeForScripts([ - $.encode(c.pkScript) + U.encode(c.pkScript) ]), u = new AbortController(), f = this.indexerProvider.getSubscription(a, u.signal); s = async () => { u.abort(), await this.indexerProvider?.unsubscribeForScripts(a); }, (async () => { try { - for await (const l of f) - l.newVtxos?.length > 0 && t({ + for await (const d of f) + (d.newVtxos?.length > 0 || d.spentVtxos?.length > 0) && t({ type: "vtxo", - newVtxos: l.newVtxos.map((d) => nn(this, d)), - spentVtxos: l.spentVtxos.map((d) => nn(this, d)) + newVtxos: d.newVtxos.map((l) => xe(this, l)), + spentVtxos: d.spentVtxos.map((l) => xe(this, l)) }); - } catch (l) { - console.error("Subscription error:", l); + } catch (d) { + console.error("Subscription error:", d); } })(); } @@ -9996,23 +10051,23 @@ class wn { }; } async handleBatchStartedEvent(t, n, r, o) { - const s = new TextEncoder().encode(n), i = wt(s), c = $.encode(i); + const s = new TextEncoder().encode(n), i = yt(s), c = U.encode(i); let a = !0; - for (const l of t.intentIdHashes) - if (l === c) { + for (const d of t.intentIdHashes) + if (d === c) { if (!this.arkProvider) throw new Error("Ark provider not configured"); await this.arkProvider.confirmRegistration(n), a = !1; } if (a) return { skip: a }; - const u = $t.encode({ + const u = Ut.encode({ timelock: { value: t.batchExpiry, type: t.batchExpiry >= 512n ? "seconds" : "blocks" }, pubkeys: [r] - }).script, f = On(u); + }).script, f = Un(u); return { roundId: t.id, sweepTapTreeRoot: f, @@ -10022,45 +10077,45 @@ class wn { } // validates the vtxo tree, creates a signing session and generates the musig2 nonces async handleSettlementSigningEvent(t, n, r, o) { - const s = ue.fromPSBT(It.decode(t.unsignedCommitmentTx)); - Jh(o, s, n); + const s = Kt.fromPSBT(wt.decode(t.unsignedCommitmentTx)); + sp(o, s, n); const i = s.getOutput(0); if (!i?.amount) throw new Error("Shared output not found"); r.init(o, n, i.amount); - const c = $.encode(await r.getPublicKey()), a = await r.getNonces(); + const c = U.encode(await r.getPublicKey()), a = await r.getNonces(); await this.arkProvider.submitTreeNonces(t.id, c, a); } async handleSettlementTreeNoncesEvent(t, n) { const { hasAllNonces: r } = await n.aggregatedNonces(t.txid, t.nonces); if (!r) return !1; - const o = await n.sign(), s = $.encode(await n.getPublicKey()); + const o = await n.sign(), s = U.encode(await n.getPublicKey()); return await this.arkProvider.submitTreeSignatures(t.id, s, o), !0; } async handleSettlementFinalizationEvent(t, n, r, o) { const s = [], i = await this.getVirtualCoins(); - let c = ue.fromPSBT(It.decode(t.commitmentTx)), a = !1, u = 0; + let c = Kt.fromPSBT(wt.decode(t.commitmentTx)), a = !1, u = 0; const f = o?.leaves() || []; - for (const l of n) { - const d = i.find((O) => O.txid === l.txid && O.vout === l.vout); - if (!d) { - for (let O = 0; O < c.inputsLength; O++) { - const N = c.getInput(O); + for (const d of n) { + const l = i.find((k) => k.txid === d.txid && k.vout === d.vout); + if (!l) { + for (let k = 0; k < c.inputsLength; k++) { + const N = c.getInput(k); if (!N.txid || N.index === void 0) throw new Error("The server returned incomplete data. No settlement input found in the PSBT"); - if ($.encode(N.txid) === l.txid && N.index === l.vout) { - c.updateInput(O, { - tapLeafScript: [l.forfeitTapLeafScript] + if (U.encode(N.txid) === d.txid && N.index === d.vout) { + c.updateInput(k, { + tapLeafScript: [d.forfeitTapLeafScript] }), c = await this.identity.sign(c, [ - O + k ]), a = !0; break; } } continue; } - if (fs(d) || Hu(d, this.dustAmount)) + if (ws(l) || Wu(l, this.dustAmount)) continue; if (f.length === 0) throw new Error("connectors not received"); @@ -10073,16 +10128,16 @@ class wn { if (!y || !S) throw new Error("invalid connector output"); u++; - let v = Fh([ + let T = Yh([ { - txid: l.txid, - index: l.vout, + txid: d.txid, + index: d.vout, witnessUtxo: { - amount: BigInt(d.value), - script: _t.decode(l.tapTree).pkScript + amount: BigInt(l.value), + script: Ct.decode(d.tapTree).pkScript }, - sighashType: Ke.DEFAULT, - tapLeafScript: [l.forfeitTapLeafScript] + sighashType: Fe.DEFAULT, + tapLeafScript: [d.forfeitTapLeafScript] }, { txid: w, @@ -10093,41 +10148,95 @@ class wn { } } ], r); - v = await this.identity.sign(v, [0]), s.push(It.encode(v.toPSBT())); + T = await this.identity.sign(T, [0]), s.push(wt.encode(T.toPSBT())); + } + (s.length > 0 || a) && await this.arkProvider.submitSignedForfeitTxs(s, a ? wt.encode(c.toPSBT()) : void 0); + } + async safeRegisterIntent(t) { + try { + return this.arkProvider.registerIntent(t); + } catch (n) { + if (n instanceof Gu && n.code === 0 && n.message.includes("duplicated input")) { + const r = await this.getVtxos({ + withRecoverable: !0 + }), o = await this.makeDeleteIntentSignature(r); + return await this.arkProvider.deleteIntent(o), this.arkProvider.registerIntent(t); + } + throw n; } - (s.length > 0 || a) && await this.arkProvider.submitSignedForfeitTxs(s, a ? It.encode(c.toPSBT()) : void 0); } async makeRegisterIntentSignature(t, n, r, o) { - const s = Math.floor(Date.now() / 1e3), i = this.prepareIntentProofInputs(t), c = { + const s = this.prepareIntentProofInputs(t), c = JSON.stringify({ type: "register", onchain_output_indexes: r, - valid_at: s, - expire_at: s + 120, - // valid for 2 minutes + valid_at: 0, + expire_at: 0, cosigners_public_keys: o - }, a = JSON.stringify(c, null, 0), u = zr.create(a, i, n), f = await this.identity.sign(u); + }, null, 0), a = _n.create(c, s, n), u = await this.identity.sign(a); return { - proof: It.encode(f.toPSBT()), - message: a + proof: wt.encode(u.toPSBT()), + message: c }; } async makeDeleteIntentSignature(t) { - const n = Math.floor(Date.now() / 1e3), r = this.prepareIntentProofInputs(t), o = { + const n = this.prepareIntentProofInputs(t), o = JSON.stringify({ type: "delete", - expire_at: n + 120 - // valid for 2 minutes - }, s = JSON.stringify(o, null, 0), i = zr.create(s, r, []), c = await this.identity.sign(i); + expire_at: 0 + }, null, 0), s = _n.create(o, n, []), i = await this.identity.sign(s); + return { + proof: wt.encode(i.toPSBT()), + message: o + }; + } + async makeGetPendingTxIntentSignature(t) { + const n = this.prepareIntentProofInputs(t), o = JSON.stringify({ + type: "get-pending-tx", + expire_at: 0 + }, null, 0), s = _n.create(o, n, []), i = await this.identity.sign(s); return { - proof: It.encode(c.toPSBT()), - message: s + proof: wt.encode(i.toPSBT()), + message: o }; } + /** + * Finalizes pending transactions by retrieving them from the server and finalizing each one. + * @param vtxos - Optional list of VTXOs to use instead of retrieving them from the server + * @returns Array of transaction IDs that were finalized + */ + async finalizePendingTxs(t) { + if (!t || t.length === 0) { + const s = [U.encode(this.offchainTapscript.pkScript)]; + let { vtxos: i } = await this.indexerProvider.getVtxos({ + scripts: s + }); + if (i = i.filter((c) => c.virtualStatus.state !== "swept" && c.virtualStatus.state !== "settled"), i.length === 0) + return { finalized: [], pending: [] }; + t = i.map((c) => xe(this, c)); + } + const r = [], o = []; + for (let s = 0; s < t.length; s += 20) { + const i = t.slice(s, s + 20), c = await this.makeGetPendingTxIntentSignature(i), a = await this.arkProvider.getPendingTxs(c); + for (const u of a) { + o.push(u.arkTxid); + try { + const f = await Promise.all(u.signedCheckpointTxs.map(async (d) => { + const l = Kt.fromPSBT(wt.decode(d)), h = await this.identity.sign(l); + return wt.encode(h.toPSBT()); + })); + await this.arkProvider.finalizeTx(u.arkTxid, f), r.push(u.arkTxid); + } catch (f) { + console.error(`Failed to finalize transaction ${u.arkTxid}:`, f); + } + } + } + return { finalized: r, pending: o }; + } prepareIntentProofInputs(t) { const n = []; for (const r of t) { - const o = _t.decode(r.tapTree), s = Ap(r), i = [_u.encode(r.tapTree)]; - r.extraWitness && i.push(Sh.encode(r.extraWitness)), n.push({ - txid: $.decode(r.txid), + const o = Ct.decode(r.tapTree), s = Rp(r), i = [Ku.encode(r.tapTree)]; + r.extraWitness && i.push(Oh.encode(r.extraWitness)), n.push({ + txid: U.decode(r.txid), index: r.vout, witnessUtxo: { amount: BigInt(r.value), @@ -10141,24 +10250,24 @@ class wn { return n; } } -wn.MIN_FEE_RATE = 1; -function Ap(e) { +yn.MIN_FEE_RATE = 1; +function Rp(e) { let t; try { - const n = e.intentTapLeafScript[1], r = n.subarray(0, n.length - 1), o = $t.decode(r).params; - t = cs.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }); + const n = e.intentTapLeafScript[1], r = n.subarray(0, n.length - 1), o = Ut.decode(r).params; + t = hs.encode(o.timelock.type === "blocks" ? { blocks: Number(o.timelock.value) } : { seconds: Number(o.timelock.value) }); } catch { } return t; } -function Ip(e) { +function Np(e) { try { return pn.decode(e), !0; } catch { return !1; } } -function kp(e, t) { +function Lp(e, t) { const n = [...e].sort((i, c) => { const a = i.virtualStatus.batchExpiry || Number.MAX_SAFE_INTEGER, u = c.virtualStatus.batchExpiry || Number.MAX_SAFE_INTEGER; return a !== u ? a - u : c.value - i.value; @@ -10177,11 +10286,11 @@ function kp(e, t) { changeAmount: s }; } -function xc() { +function Ic() { const e = crypto.getRandomValues(new Uint8Array(16)); - return $.encode(e); + return U.encode(e); } -var V; +var H; (function(e) { e.walletInitialized = (p) => ({ type: "WALLET_INITIALIZED", @@ -10258,11 +10367,11 @@ var V; }; } e.balance = f; - function l(p) { + function d(p) { return p.type === "VTXOS" && p.success === !0; } - e.isVtxos = l; - function d(p, E) { + e.isVtxos = d; + function l(p, E) { return { type: "VTXOS", success: !0, @@ -10270,7 +10379,7 @@ var V; id: p }; } - e.vtxos = d; + e.vtxos = l; function h(p) { return p.type === "VIRTUAL_COINS" && p.success === !0; } @@ -10301,7 +10410,7 @@ var V; return p.type === "SEND_BITCOIN_SUCCESS" && p.success === !0; } e.isSendBitcoinSuccess = S; - function v(p, E) { + function T(p, E) { return { type: "SEND_BITCOIN_SUCCESS", success: !0, @@ -10309,11 +10418,11 @@ var V; id: p }; } - e.sendBitcoinSuccess = v; - function O(p) { + e.sendBitcoinSuccess = T; + function k(p) { return p.type === "TRANSACTION_HISTORY" && p.success === !0; } - e.isTransactionHistory = O; + e.isTransactionHistory = k; function N(p, E) { return { type: "TRANSACTION_HISTORY", @@ -10323,11 +10432,11 @@ var V; }; } e.transactionHistory = N; - function U(p) { + function $(p) { return p.type === "WALLET_STATUS" && p.success === !0; } - e.isWalletStatus = U; - function W(p, E, A) { + e.isWalletStatus = $; + function F(p, E, A) { return { type: "WALLET_STATUS", success: !0, @@ -10338,23 +10447,23 @@ var V; id: p }; } - e.walletStatus = W; + e.walletStatus = F; function x(p) { return p.type === "CLEAR_RESPONSE"; } e.isClearResponse = x; - function Q(p, E) { + function Y(p, E) { return { type: "CLEAR_RESPONSE", success: E, id: p }; } - e.clearResponse = Q; - function C(p) { + e.clearResponse = Y; + function L(p) { return p.type === "WALLET_RELOADED"; } - e.isWalletReloaded = C; + e.isWalletReloaded = L; function Pt(p, E) { return { type: "WALLET_RELOADED", @@ -10367,17 +10476,17 @@ var V; return p.type === "VTXO_UPDATE"; } e.isVtxoUpdate = gt; - function _(p, E) { + function C(p, E) { return { type: "VTXO_UPDATE", - id: xc(), + id: Ic(), // spontaneous update, not tied to a request success: !0, spentVtxos: E, newVtxos: p }; } - e.vtxoUpdate = _; + e.vtxoUpdate = C; function b(p) { return p.type === "UTXO_UPDATE"; } @@ -10385,15 +10494,15 @@ var V; function m(p) { return { type: "UTXO_UPDATE", - id: xc(), + id: Ic(), // spontaneous update, not tied to a request success: !0, coins: p }; } e.utxoUpdate = m; -})(V || (V = {})); -class Bp { +})(H || (H = {})); +class _p { constructor(t, n = 1) { this.db = null, this.dbName = t, this.version = n; } @@ -10460,7 +10569,7 @@ class Bp { } } } -const Op = "arkade-service-worker"; +const Cp = "arkade-service-worker"; class tt { constructor(t, n, r, o, s, i) { this.hasWitness = t, this.inputCount = n, this.outputCount = r, this.inputSize = o, this.inputWitnessSize = s, this.outputSize = i; @@ -10490,7 +10599,7 @@ class tt { vsize() { const t = (i) => i < 253 ? 1 : i < 65535 ? 3 : i < 4294967295 ? 5 : 9, n = t(this.inputCount), r = t(this.outputCount); let s = (tt.BASE_TX_SIZE + n + this.inputSize + r + this.outputSize) * tt.WITNESS_SCALE_FACTOR; - return this.hasWitness && (s += tt.WITNESS_HEADER_SIZE + this.inputWitnessSize), $p(s); + return this.hasWitness && (s += tt.WITNESS_HEADER_SIZE + this.inputWitnessSize), Pp(s); } } tt.P2PKH_SCRIPT_SIG_SIZE = 108; @@ -10502,14 +10611,14 @@ tt.BASE_TX_SIZE = 10; tt.WITNESS_HEADER_SIZE = 2; tt.WITNESS_SCALE_FACTOR = 4; tt.P2TR_OUTPUT_SIZE = 34; -const $p = (e) => { +const Pp = (e) => { const t = BigInt(Math.ceil(e / tt.WITNESS_SCALE_FACTOR)); return { value: t, fee: (n) => n * t }; }; -var yt; +var mt; (function(e) { function t(g) { return typeof g == "object" && g !== null && "type" in g; @@ -10551,14 +10660,14 @@ var yt; return g.type === "SEND_BITCOIN" && "params" in g && g.params !== null && typeof g.params == "object" && "address" in g.params && typeof g.params.address == "string" && "amount" in g.params && typeof g.params.amount == "number"; } e.isSendBitcoin = f; - function l(g) { + function d(g) { return g.type === "GET_TRANSACTION_HISTORY"; } - e.isGetTransactionHistory = l; - function d(g) { + e.isGetTransactionHistory = d; + function l(g) { return g.type === "GET_STATUS"; } - e.isGetStatus = d; + e.isGetStatus = l; function h(g) { return g.type === "CLEAR"; } @@ -10567,11 +10676,11 @@ var yt; return g.type === "RELOAD_WALLET"; } e.isReloadWallet = w; -})(yt || (yt = {})); -class Up { - constructor(t = Op, n = 1, r = () => { +})(mt || (mt = {})); +class Vp { + constructor(t = Cp, n = 1, r = () => { }) { - this.dbName = t, this.dbVersion = n, this.messageCallback = r, this.storage = new Bp(t, n), this.walletRepository = new gs(this.storage); + this.dbName = t, this.dbVersion = n, this.messageCallback = r, this.storage = new _p(t, n), this.walletRepository = new xs(this.storage); } /** * Get spendable vtxos for the current wallet address @@ -10580,7 +10689,7 @@ class Up { if (!this.wallet) return []; const t = await this.wallet.getAddress(); - return (await this.walletRepository.getVtxos(t)).filter(ae); + return (await this.walletRepository.getVtxos(t)).filter(ue); } /** * Get swept vtxos for the current wallet address @@ -10599,8 +10708,8 @@ class Up { return { spendable: [], spent: [] }; const t = await this.wallet.getAddress(), n = await this.walletRepository.getVtxos(t); return { - spendable: n.filter(ae), - spent: n.filter((r) => !ae(r)) + spendable: n.filter(ue), + spent: n.filter((r) => !ue(r)) }; } /** @@ -10617,9 +10726,7 @@ class Up { return []; let t = []; try { - const { boardingTxs: n, commitmentsToIgnore: r } = await this.wallet.getBoardingTxs(), { spendable: o, spent: s } = await this.getAllVtxos(); - console.log("getTransactionHistory - vtxosToTxs:", o); - const i = Vu(o, s, r); + const { boardingTxs: n, commitmentsToIgnore: r } = await this.wallet.getBoardingTxs(), { spendable: o, spent: s } = await this.getAllVtxos(), i = zu(o, s, r); t = [...n, ...i], t.sort( // place createdAt = 0 (unconfirmed txs) first, then descending (c, a) => c.createdAt === 0 ? -1 : a.createdAt === 0 ? 1 : a.createdAt - c.createdAt @@ -10639,7 +10746,7 @@ class Up { })); } async clear() { - this.incomingFundsSubscription && this.incomingFundsSubscription(), await this.storage.clear(), this.walletRepository = new gs(this.storage), this.wallet = void 0, this.arkProvider = void 0, this.indexerProvider = void 0; + this.incomingFundsSubscription && this.incomingFundsSubscription(), await this.storage.clear(), this.walletRepository = new xs(this.storage), this.wallet = void 0, this.arkProvider = void 0, this.indexerProvider = void 0; } async reload() { await this.onWalletInitialized(); @@ -10647,144 +10754,151 @@ class Up { async onWalletInitialized() { if (!this.wallet || !this.arkProvider || !this.indexerProvider || !this.wallet.offchainTapscript || !this.wallet.boardingTapscript) return; - const t = $.encode(this.wallet.offchainTapscript.pkScript), r = (await this.indexerProvider.getVtxos({ + const t = U.encode(this.wallet.offchainTapscript.pkScript), r = (await this.indexerProvider.getVtxos({ scripts: [t] - })).vtxos.map((a) => nn(this.wallet, a)), o = await this.wallet.getAddress(); + })).vtxos.map((a) => xe(this.wallet, a)); + try { + const { finalized: a, pending: u } = await this.wallet.finalizePendingTxs(r.filter((f) => f.virtualStatus.state !== "swept" && f.virtualStatus.state !== "settled")); + console.info(`Recovered ${a.length}/${u.length} pending transactions: ${a.join(", ")}`); + } catch (a) { + console.error("Error recovering pending transactions:", a); + } + const o = await this.wallet.getAddress(); await this.walletRepository.saveVtxos(o, r); const s = await this.wallet.getBoardingAddress(), i = await this.wallet.onchainProvider.getCoins(s); - await this.walletRepository.saveUtxos(s, i.map((a) => ws(this.wallet, a))); + await this.walletRepository.saveUtxos(s, i.map((a) => Ss(this.wallet, a))); const c = await this.getTransactionHistory(); c && await this.walletRepository.saveTransactions(o, c), this.incomingFundsSubscription && this.incomingFundsSubscription(), this.incomingFundsSubscription = await this.wallet.notifyIncomingFunds(async (a) => { if (a.type === "vtxo") { - const u = a.newVtxos.length > 0 ? a.newVtxos.map((l) => nn(this.wallet, l)) : [], f = a.spentVtxos.length > 0 ? a.spentVtxos.map((l) => nn(this.wallet, l)) : []; + const u = a.newVtxos.length > 0 ? a.newVtxos.map((d) => xe(this.wallet, d)) : [], f = a.spentVtxos.length > 0 ? a.spentVtxos.map((d) => xe(this.wallet, d)) : []; if ([...u, ...f].length === 0) return; await this.walletRepository.saveVtxos(o, [ ...u, ...f - ]), await this.sendMessageToAllClients(V.vtxoUpdate(u, f)); + ]), await this.sendMessageToAllClients(H.vtxoUpdate(u, f)); } if (a.type === "utxo") { - const u = a.coins.map((l) => ws(this.wallet, l)), f = await this.wallet?.getBoardingAddress(); - await this.walletRepository.clearUtxos(f), await this.walletRepository.saveUtxos(f, u), await this.sendMessageToAllClients(V.utxoUpdate(u)); + const u = a.coins.map((d) => Ss(this.wallet, d)), f = await this.wallet?.getBoardingAddress(); + await this.walletRepository.clearUtxos(f), await this.walletRepository.saveUtxos(f, u), await this.sendMessageToAllClients(H.utxoUpdate(u)); } }); } async handleClear(t) { - await this.clear(), yt.isBase(t.data) && t.source?.postMessage(V.clearResponse(t.data.id, !0)); + await this.clear(), mt.isBase(t.data) && t.source?.postMessage(H.clearResponse(t.data.id, !0)); } async handleInitWallet(t) { const n = t.data; - if (!yt.isInitWallet(n)) { - console.error("Invalid INIT_WALLET message format", n), t.source?.postMessage(V.error(n.id, "Invalid INIT_WALLET message format")); + if (!mt.isInitWallet(n)) { + console.error("Invalid INIT_WALLET message format", n), t.source?.postMessage(H.error(n.id, "Invalid INIT_WALLET message format")); return; } if (!n.privateKey) { const r = "Missing privateKey"; - t.source?.postMessage(V.error(n.id, r)), console.error(r); + t.source?.postMessage(H.error(n.id, r)), console.error(r); return; } try { - const { arkServerPublicKey: r, arkServerUrl: o, privateKey: s } = n, i = Rn.fromHex(s); - this.arkProvider = new Du(o), this.indexerProvider = new Ku(o), this.wallet = await wn.create({ + const { arkServerPublicKey: r, arkServerUrl: o, privateKey: s } = n, i = Nn.fromHex(s); + this.arkProvider = new qu(o), this.indexerProvider = new Yu(o), this.wallet = await yn.create({ identity: i, arkServerUrl: o, arkServerPublicKey: r, storage: this.storage // Use unified storage for wallet too - }), t.source?.postMessage(V.walletInitialized(n.id)), await this.onWalletInitialized(); + }), t.source?.postMessage(H.walletInitialized(n.id)), await this.onWalletInitialized(); } catch (r) { console.error("Error initializing wallet:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleSettle(t) { const n = t.data; - if (!yt.isSettle(n)) { - console.error("Invalid SETTLE message format", n), t.source?.postMessage(V.error(n.id, "Invalid SETTLE message format")); + if (!mt.isSettle(n)) { + console.error("Invalid SETTLE message format", n), t.source?.postMessage(H.error(n.id, "Invalid SETTLE message format")); return; } try { if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } const r = await this.wallet.settle(n.params, (o) => { - t.source?.postMessage(V.settleEvent(n.id, o)); + t.source?.postMessage(H.settleEvent(n.id, o)); }); - t.source?.postMessage(V.settleSuccess(n.id, r)); + t.source?.postMessage(H.settleSuccess(n.id, r)); } catch (r) { console.error("Error settling:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleSendBitcoin(t) { const n = t.data; - if (!yt.isSendBitcoin(n)) { - console.error("Invalid SEND_BITCOIN message format", n), t.source?.postMessage(V.error(n.id, "Invalid SEND_BITCOIN message format")); + if (!mt.isSendBitcoin(n)) { + console.error("Invalid SEND_BITCOIN message format", n), t.source?.postMessage(H.error(n.id, "Invalid SEND_BITCOIN message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { const r = await this.wallet.sendBitcoin(n.params); - t.source?.postMessage(V.sendBitcoinSuccess(n.id, r)); + t.source?.postMessage(H.sendBitcoinSuccess(n.id, r)); } catch (r) { console.error("Error sending bitcoin:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetAddress(t) { const n = t.data; - if (!yt.isGetAddress(n)) { - console.error("Invalid GET_ADDRESS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_ADDRESS message format")); + if (!mt.isGetAddress(n)) { + console.error("Invalid GET_ADDRESS message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_ADDRESS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { const r = await this.wallet.getAddress(); - t.source?.postMessage(V.address(n.id, r)); + t.source?.postMessage(H.address(n.id, r)); } catch (r) { console.error("Error getting address:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetBoardingAddress(t) { const n = t.data; - if (!yt.isGetBoardingAddress(n)) { - console.error("Invalid GET_BOARDING_ADDRESS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_BOARDING_ADDRESS message format")); + if (!mt.isGetBoardingAddress(n)) { + console.error("Invalid GET_BOARDING_ADDRESS message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_BOARDING_ADDRESS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { const r = await this.wallet.getBoardingAddress(); - t.source?.postMessage(V.boardingAddress(n.id, r)); + t.source?.postMessage(H.boardingAddress(n.id, r)); } catch (r) { console.error("Error getting boarding address:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetBalance(t) { const n = t.data; - if (!yt.isGetBalance(n)) { - console.error("Invalid GET_BALANCE message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_BALANCE message format")); + if (!mt.isGetBalance(n)) { + console.error("Invalid GET_BALANCE message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_BALANCE message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { @@ -10800,96 +10914,96 @@ class Up { for (const h of o) h.virtualStatus.state === "settled" ? a += h.value : h.virtualStatus.state === "preconfirmed" && (u += h.value); for (const h of s) - ae(h) && (f += h.value); - const l = i + c, d = a + u + f; - t.source?.postMessage(V.balance(n.id, { + ue(h) && (f += h.value); + const d = i + c, l = a + u + f; + t.source?.postMessage(H.balance(n.id, { boarding: { confirmed: i, unconfirmed: c, - total: l + total: d }, settled: a, preconfirmed: u, available: a + u, recoverable: f, - total: l + d + total: d + l })); } catch (r) { console.error("Error getting balance:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetVtxos(t) { const n = t.data; - if (!yt.isGetVtxos(n)) { - console.error("Invalid GET_VTXOS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_VTXOS message format")); + if (!mt.isGetVtxos(n)) { + console.error("Invalid GET_VTXOS message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_VTXOS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { - const r = await this.getSpendableVtxos(), o = this.wallet.dustAmount, i = n.filter?.withRecoverable ?? !1 ? r : r.filter((c) => !(o != null && Hu(c, o) || fs(c))); - t.source?.postMessage(V.vtxos(n.id, i)); + const r = await this.getSpendableVtxos(), o = this.wallet.dustAmount, i = n.filter?.withRecoverable ?? !1 ? r : r.filter((c) => !(o != null && Wu(c, o) || ws(c))); + t.source?.postMessage(H.vtxos(n.id, i)); } catch (r) { console.error("Error getting vtxos:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetBoardingUtxos(t) { const n = t.data; - if (!yt.isGetBoardingUtxos(n)) { - console.error("Invalid GET_BOARDING_UTXOS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_BOARDING_UTXOS message format")); + if (!mt.isGetBoardingUtxos(n)) { + console.error("Invalid GET_BOARDING_UTXOS message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_BOARDING_UTXOS message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { const r = await this.getAllBoardingUtxos(); - t.source?.postMessage(V.boardingUtxos(n.id, r)); + t.source?.postMessage(H.boardingUtxos(n.id, r)); } catch (r) { console.error("Error getting boarding utxos:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetTransactionHistory(t) { const n = t.data; - if (!yt.isGetTransactionHistory(n)) { - console.error("Invalid GET_TRANSACTION_HISTORY message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_TRANSACTION_HISTORY message format")); + if (!mt.isGetTransactionHistory(n)) { + console.error("Invalid GET_TRANSACTION_HISTORY message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_TRANSACTION_HISTORY message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.error(n.id, "Wallet not initialized")); + console.error("Wallet not initialized"), t.source?.postMessage(H.error(n.id, "Wallet not initialized")); return; } try { const r = await this.getTransactionHistory(); - t.source?.postMessage(V.transactionHistory(n.id, r)); + t.source?.postMessage(H.transactionHistory(n.id, r)); } catch (r) { console.error("Error getting transaction history:", r); const o = r instanceof Error ? r.message : "Unknown error occurred"; - t.source?.postMessage(V.error(n.id, o)); + t.source?.postMessage(H.error(n.id, o)); } } async handleGetStatus(t) { const n = t.data; - if (!yt.isGetStatus(n)) { - console.error("Invalid GET_STATUS message format", n), t.source?.postMessage(V.error(n.id, "Invalid GET_STATUS message format")); + if (!mt.isGetStatus(n)) { + console.error("Invalid GET_STATUS message format", n), t.source?.postMessage(H.error(n.id, "Invalid GET_STATUS message format")); return; } const r = this.wallet ? await this.wallet.identity.xOnlyPublicKey() : void 0; - t.source?.postMessage(V.walletStatus(n.id, this.wallet !== void 0, r)); + t.source?.postMessage(H.walletStatus(n.id, this.wallet !== void 0, r)); } async handleMessage(t) { this.messageCallback(t); const n = t.data; - if (!yt.isBase(n)) { + if (!mt.isBase(n)) { console.warn("Invalid message format", JSON.stringify(n)); return; } @@ -10943,7 +11057,7 @@ class Up { break; } default: - t.source?.postMessage(V.error(n.id, "Unknown message type")); + t.source?.postMessage(H.error(n.id, "Unknown message type")); } } async sendMessageToAllClients(t) { @@ -10955,22 +11069,22 @@ class Up { } async handleReloadWallet(t) { const n = t.data; - if (!yt.isReloadWallet(n)) { - console.error("Invalid RELOAD_WALLET message format", n), t.source?.postMessage(V.error(n.id, "Invalid RELOAD_WALLET message format")); + if (!mt.isReloadWallet(n)) { + console.error("Invalid RELOAD_WALLET message format", n), t.source?.postMessage(H.error(n.id, "Invalid RELOAD_WALLET message format")); return; } if (!this.wallet) { - console.error("Wallet not initialized"), t.source?.postMessage(V.walletReloaded(n.id, !1)); + console.error("Wallet not initialized"), t.source?.postMessage(H.walletReloaded(n.id, !1)); return; } try { - await this.onWalletInitialized(), t.source?.postMessage(V.walletReloaded(n.id, !0)); + await this.onWalletInitialized(), t.source?.postMessage(H.walletReloaded(n.id, !0)); } catch (r) { - console.error("Error reloading wallet:", r), t.source?.postMessage(V.walletReloaded(n.id, !1)); + console.error("Error reloading wallet:", r), t.source?.postMessage(H.walletReloaded(n.id, !1)); } } } -var Sc; +var kc; (function(e) { let t; (function(o) { @@ -10993,13 +11107,13 @@ var Sc; const i = this.toUnroll.chain; for (let u = i.length - 1; u >= 0; u--) { const f = i[u]; - if (!(f.type === en.COMMITMENT || f.type === en.UNSPECIFIED)) + if (!(f.type === nn.COMMITMENT || f.type === nn.UNSPECIFIED)) try { if (!(await this.explorer.getTxStatus(f.txid)).confirmed) return { type: t.WAIT, txid: f.txid, - do: Lp(this.explorer, f.txid) + do: Mp(this.explorer, f.txid) }; } catch { s = f; @@ -11017,8 +11131,8 @@ var Sc; ]); if (c.txs.length === 0) throw new Error(`Tx ${s.txid} not found`); - const a = ke.fromPSBT(It.decode(c.txs[0])); - if (s.type === en.TREE) { + const a = Be.fromPSBT(wt.decode(c.txs[0])); + if (s.type === nn.TREE) { const u = a.getInput(0); if (!u) throw new Error("Input not found"); @@ -11033,7 +11147,7 @@ var Sc; return { type: t.UNROLL, tx: a, - do: Np(this.bumper, this.explorer, a) + do: Dp(this.bumper, this.explorer, a) }; } /** @@ -11043,7 +11157,7 @@ var Sc; async *[Symbol.asyncIterator]() { let s; do { - s !== void 0 && await Rp(1e3); + s !== void 0 && await Hp(1e3); const i = await this.next(); await i.do(), yield i, s = i.type; } while (s !== t.DONE); @@ -11057,56 +11171,56 @@ var Sc; throw new Error("No vtxos to complete unroll"); const u = []; let f = 0n; - const l = tt.create(); + const d = tt.create(); for (const y of a) { if (!y.isUnrolled) throw new Error(`Vtxo ${y.txid}:${y.vout} is not fully unrolled, use unroll first`); const S = await o.onchainProvider.getTxStatus(y.txid); if (!S.confirmed) throw new Error(`tx ${y.txid} is not confirmed`); - const v = Cp({ height: S.blockHeight, time: S.blockTime }, c, y); - if (!v) + const T = Kp({ height: S.blockHeight, time: S.blockTime }, c, y); + if (!T) throw new Error(`no available exit path found for vtxo ${y.txid}:${y.vout}`); - const O = _t.decode(y.tapTree).findLeaf($.encode(v.script)); - if (!O) + const k = Ct.decode(y.tapTree).findLeaf(U.encode(T.script)); + if (!k) throw new Error(`spending leaf not found for vtxo ${y.txid}:${y.vout}`); f += BigInt(y.value), u.push({ txid: y.txid, index: y.vout, - tapLeafScript: [O], + tapLeafScript: [k], sequence: 4294967294, witnessUtxo: { amount: BigInt(y.value), - script: _t.decode(y.tapTree).pkScript + script: Ct.decode(y.tapTree).pkScript }, - sighashType: Ke.DEFAULT - }), l.addTapscriptInput(64, O[1].length, Zt.encode(O[0]).length); + sighashType: Fe.DEFAULT + }), d.addTapscriptInput(64, k[1].length, Xt.encode(k[0]).length); } - const d = new ke({ version: 2 }); + const l = new Be({ version: 2 }); for (const y of u) - d.addInput(y); - l.addP2TROutput(); + l.addInput(y); + d.addP2TROutput(); let h = await o.onchainProvider.getFeeRate(); - (!h || h < wn.MIN_FEE_RATE) && (h = wn.MIN_FEE_RATE); - const w = l.vsize().fee(BigInt(h)); + (!h || h < yn.MIN_FEE_RATE) && (h = yn.MIN_FEE_RATE); + const w = d.vsize().fee(BigInt(h)); if (w > f) throw new Error("fee amount is greater than the total amount"); - d.addOutputAddress(i, f - w); - const g = await o.identity.sign(d); + l.addOutputAddress(i, f - w); + const g = await o.identity.sign(l); return g.finalize(), await o.onchainProvider.broadcastTransaction(g.hex), g.id; } e.completeUnroll = r; -})(Sc || (Sc = {})); -function Rp(e) { +})(kc || (kc = {})); +function Hp(e) { return new Promise((t) => setTimeout(t, e)); } -function Np(e, t, n) { +function Dp(e, t, n) { return async () => { const [r, o] = await e.bumpP2A(n); await t.broadcastTransaction(r, o); }; } -function Lp(e, t) { +function Mp(e, t) { return () => new Promise((n, r) => { const o = setInterval(async () => { try { @@ -11117,8 +11231,8 @@ function Lp(e, t) { }, 5e3); }); } -function Cp(e, t, n) { - const r = _t.decode(n.tapTree).exitPaths(); +function Kp(e, t, n) { + const r = Ct.decode(n.tapTree).exitPaths(); for (const o of r) if (o.params.timelock.type === "blocks") { if (t.height >= e.height + Number(o.params.timelock.value)) @@ -11126,17 +11240,17 @@ function Cp(e, t, n) { } else if (t.time >= e.time + Number(o.params.timelock.value)) return o; } -const Wu = new Up(); -Wu.start().catch(console.error); -const zu = "arkade-cache-v1"; +const Xu = new Vp(); +Xu.start().catch(console.error); +const Qu = "arkade-cache-v1"; self.addEventListener("install", (e) => { - e.waitUntil(caches.open(zu)), self.skipWaiting(); + e.waitUntil(caches.open(Qu)), self.skipWaiting(); }); self.addEventListener("activate", (e) => { e.waitUntil( caches.keys().then((t) => Promise.all( t.map((n) => { - if (n !== zu) + if (n !== Qu) return caches.delete(n); }) )) @@ -11150,5 +11264,5 @@ self.addEventListener("activate", (e) => { }), self.clients.claim(); }); self.addEventListener("message", (e) => { - e.data && e.data.type === "RELOAD_WALLET" && e.waitUntil(Wu.reload().catch(console.error)); + e.data && e.data.type === "RELOAD_WALLET" && e.waitUntil(Xu.reload().catch(console.error)); }); diff --git a/src/lib/urls.ts b/src/lib/urls.ts index 04f588040..073bcba46 100644 --- a/src/lib/urls.ts +++ b/src/lib/urls.ts @@ -7,6 +7,7 @@ type Service = 'ark' | 'boltz' | 'esplora' | 'indexer' const DEFAULT_ARK_SERVER_URLS: Partial> = {} const DEFAULT_BOLTZ_SERVER_URLS: Partial> = { + bitcoin: 'https://api.ark.boltz.exchange', mutinynet: 'https://api.boltz.mutinynet.arkade.sh', signet: 'https://boltz.signet.arkade.sh', regtest: 'http://localhost:9069', @@ -27,24 +28,28 @@ const ENV_URL_IMPORTERS: EnvUrlImporters = { mutinynet: importArkMutinyUrl, regtest: importArkRegTestUrl, signet: importArkSigNetUrl, + testnet: importArkTestnetUrl, }, boltz: { bitcoin: importBoltzBitcoinUrl, mutinynet: importBoltzMutinyUrl, regtest: importBoltzRegTestUrl, signet: importBoltzSigNetUrl, + testnet: importBoltzTestnetUrl, }, esplora: { bitcoin: importEsploraBitcoinUrl, mutinynet: importEsploraMutinyUrl, regtest: importEsploraRegTestUrl, signet: importEsploraSigNetUrl, + testnet: importEsploraTestnetUrl, }, indexer: { bitcoin: importIndexerBitcoinUrl, mutinynet: importIndexerMutinyUrl, regtest: importIndexerRegTestUrl, signet: importIndexerSigNetUrl, + testnet: importIndexerTestnetUrl, }, } as const @@ -126,6 +131,10 @@ function importArkRegTestUrl(): string | undefined { return import.meta.env.VITE_ARK_SERVER_URL_REGTEST } +function importArkTestnetUrl(): string | undefined { + return import.meta.env.VITE_ARK_SERVER_URL_TESTNET +} + function importBoltzBitcoinUrl(): string | undefined { return import.meta.env.VITE_BOLTZ_URL_BITCOIN } @@ -142,6 +151,10 @@ function importBoltzRegTestUrl(): string | undefined { return import.meta.env.VITE_BOLTZ_URL_REGTEST } +function importBoltzTestnetUrl(): string | undefined { + return import.meta.env.VITE_BOLTZ_URL_TESTNET +} + function importEsploraBitcoinUrl(): string | undefined { return import.meta.env.VITE_ESPLORA_URL_BITCOIN } @@ -158,6 +171,10 @@ function importEsploraRegTestUrl(): string | undefined { return import.meta.env.VITE_ESPLORA_URL_REGTEST } +function importEsploraTestnetUrl(): string | undefined { + return import.meta.env.VITE_ESPLORA_URL_TESTNET +} + function importIndexerBitcoinUrl(): string | undefined { return import.meta.env.VITE_INDEXER_URL_BITCOIN } @@ -173,3 +190,7 @@ function importIndexerSigNetUrl(): string | undefined { function importIndexerRegTestUrl(): string | undefined { return import.meta.env.VITE_INDEXER_URL_REGTEST } + +function importIndexerTestnetUrl(): string | undefined { + return import.meta.env.VITE_INDEXER_URL_TESTNET +} diff --git a/src/providers/lightning.tsx b/src/providers/lightning.tsx index 3fa297dbb..91f74ddcc 100644 --- a/src/providers/lightning.tsx +++ b/src/providers/lightning.tsx @@ -4,7 +4,7 @@ import { AspContext } from './asp' import { WalletContext } from './wallet' import { FeesResponse, isPendingReverseSwap, isReverseClaimableStatus, Network } from '@arkade-os/boltz-swap' import { ConfigContext } from './config' -import { consoleError } from '../lib/logs' +import { consoleError, consoleLog } from '../lib/logs' import { getBoltzUrl } from '../lib/urls' interface LightningContextProps { diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index bd4430231..264e97224 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -9,19 +9,23 @@ interface ImportMetaEnv { readonly VITE_ARK_SERVER_URL_MUTINYNET?: string readonly VITE_ARK_SERVER_URL_REGTEST?: string readonly VITE_ARK_SERVER_URL_SIGNET?: string + readonly VITE_ARK_SERVER_URL_TESTNET?: string readonly VITE_BOLTZ_URL?: string readonly VITE_BOLTZ_URL_BITCOIN?: string readonly VITE_BOLTZ_URL_MUTINYNET?: string readonly VITE_BOLTZ_URL_REGTEST?: string readonly VITE_BOLTZ_URL_SIGNET?: string + readonly VITE_BOLTZ_URL_TESTNET?: string readonly VITE_ESPLORA_URL_BITCOIN?: string readonly VITE_ESPLORA_URL_MUTINYNET?: string readonly VITE_ESPLORA_URL_REGTEST?: string readonly VITE_ESPLORA_URL_SIGNET?: string + readonly VITE_ESPLORA_URL_TESTNET?: string readonly VITE_INDEXER_URL_BITCOIN?: string readonly VITE_INDEXER_URL_MUTINYNET?: string readonly VITE_INDEXER_URL_REGTEST?: string readonly VITE_INDEXER_URL_SIGNET?: string + readonly VITE_INDEXER_URL_TESTNET?: string // Add other env variables as needed }