UNPKG

mochimo-wallet

Version:

Mochimo HD Wallet Implementation with React Integration

1,726 lines (1,724 loc) 470 kB
var Al = Object.defineProperty; var Bl = (e, r, t) => r in e ? Al(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t; var fe = (e, r, t) => Bl(e, typeof r != "symbol" ? r + "" : r, t); import * as Ie from "react"; import Zn, { useMemo as Ca, useCallback as ce, useRef as y0, useState as Bn, useEffect as ko } from "react"; function Cl(e) { return crypto.getRandomValues(new Uint8Array(e)); } function kl(e = 32) { return Cl(e); } function Sn(e) { e.fill(0); } var Cn = /* @__PURE__ */ ((e) => (e[e.BIG_ENDIAN = 0] = "BIG_ENDIAN", e[e.LITTLE_ENDIAN = 1] = "LITTLE_ENDIAN", e))(Cn || {}); class rr { constructor(r) { this.buf = new Uint8Array(r), this.pos = 0, this.byteOrder = 0; } /** * Creates a new ByteBuffer with the given capacity */ static allocate(r) { return new rr(r); } /** * Creates a new ByteBuffer that wraps the given array */ static wrap(r) { const t = new rr(r.length); return t.buf.set(r), t; } /** * Sets this buffer's byte order */ order(r) { return this.byteOrder = r, this; } /** * Sets or gets this buffer's position */ position(r) { if (r === void 0) return this.pos; if (r < 0 || r > this.buf.length) throw new Error("Invalid position, position: " + r + ", length: " + this.buf.length); return this.pos = r, this; } /** * Returns this buffer's capacity */ capacity() { return this.buf.length; } /** * Writes a byte or bytes into this buffer */ put(r, t, n) { if (typeof r == "number") { if (this.pos >= this.buf.length) throw new Error("Buffer overflow"); return this.buf[this.pos++] = r & 255, this; } const o = t || 0, a = n || r.length; if (o < 0 || o > r.length) throw new Error("Invalid offset"); if (a < 0 || o + a > r.length) throw new Error("Invalid length"); if (this.pos + a > this.buf.length) throw new Error("Buffer overflow"); return this.buf.set(r.subarray(o, o + a), this.pos), this.pos += a, this; } /** * Writes an integer into this buffer */ putInt(r) { if (this.pos + 4 > this.buf.length) throw new Error("Buffer overflow"); return this.byteOrder === 0 ? (this.buf[this.pos++] = r >>> 24 & 255, this.buf[this.pos++] = r >>> 16 & 255, this.buf[this.pos++] = r >>> 8 & 255, this.buf[this.pos++] = r & 255) : (this.buf[this.pos++] = r & 255, this.buf[this.pos++] = r >>> 8 & 255, this.buf[this.pos++] = r >>> 16 & 255, this.buf[this.pos++] = r >>> 24 & 255), this; } /** * Gets bytes from the buffer into the destination array */ get(r) { if (this.pos + r.length > this.buf.length) throw new Error("Buffer underflow"); for (let t = 0; t < r.length; t++) r[t] = this.buf[this.pos++]; return this; } /** * Gets a single byte from the buffer */ get_() { if (this.pos >= this.buf.length) throw new Error("Buffer underflow"); return this.buf[this.pos++]; } /** * Returns a copy of the backing array */ array() { return new Uint8Array(this.buf); } /** * Rewinds this buffer. Sets the position to zero */ rewind() { return this.pos = 0, this; } } const ka = "0123456789abcdef"; class Ye { /** * Create a copy of a byte array */ static copyOf(r, t) { const n = new Uint8Array(t); return n.set(r.slice(0, t)), n; } /** * Convert a hexadecimal string to a byte array * @param hex The hexadecimal string to convert */ static hexToBytes(r) { let t = r.toLowerCase(); t.startsWith("0x") && (t = t.slice(2)), t.length % 2 !== 0 && (t = "0" + t); const n = new Uint8Array(t.length / 2); for (let o = 0; o < t.length; o += 2) n[o / 2] = parseInt(t.slice(o, o + 2), 16); return n; } /** * Compares two byte arrays */ static compareBytes(r, t) { if (r.length !== t.length) return !1; for (let n = 0; n < r.length; n++) if (r[n] !== t[n]) return !1; return !0; } /** * Reads little-endian unsigned values from a buffer */ static readLittleEndianUnsigned(r, t = 8) { const n = new Uint8Array(t); r.get(n); let o = 0n; for (let a = t - 1; a >= 0; a--) o = o << 8n | BigInt(n[a]); return o; } /** * Trims address for display */ static trimAddress(r) { return `${r.substring(0, 32)}...${r.substring(r.length - 24)}`; } /** * Converts number to little-endian bytes */ static numberToLittleEndian(r, t) { const n = new Uint8Array(t); let o = r; for (let a = 0; a < t; a++) n[a] = o & 255, o = o >>> 8; return n; } /** * Converts byte array to little-endian */ static bytesToLittleEndian(r) { const t = new Uint8Array(r.length); for (let n = 0; n < r.length; n++) t[n] = r[r.length - 1 - n]; return t; } /** * Fits byte array or string to specified length */ static fit(r, t) { if (typeof r == "string") { const a = BigInt(r), c = new Uint8Array(t); let s = a; for (let u = 0; u < t; u++) c[u] = Number(s & 0xffn), s >>= 8n; return c; } const n = new Uint8Array(t), o = Math.min(r.length, t); return n.set(r.subarray(0, o)), n; } /** * Convert a byte array to its hexadecimal string representation * @param bytes The byte array to convert * @param offset Optional starting offset in the byte array * @param length Optional number of bytes to convert */ static bytesToHex(r, t = 0, n = r.length) { const o = new Array(n * 2); for (let a = 0; a < n; a++) { const c = r[a + t] & 255; o[a * 2] = ka[c >>> 4], o[a * 2 + 1] = ka[c & 15]; } return o.join(""); } /** * Convert a number to a byte array of specified length * @param value The number to convert * @param length The desired length of the resulting byte array */ static toBytes(r, t) { const n = r.toString(16).padStart(t * 2, "0"); return Ye.hexToBytes(n); } /** * Convert a byte array to little-endian format * @param value The byte array to convert * @param offset Optional starting offset * @param length Optional number of bytes to convert */ static toLittleEndian(r, t = 0, n = r.length) { const o = new Uint8Array(n); o.set(r.slice(t, t + n)); for (let a = 0; a < o.length >> 1; a++) { const c = o[a]; o[a] = o[o.length - a - 1], o[o.length - a - 1] = c; } return o; } /** * Clear a byte array by filling it with zeros */ static clear(r) { r.fill(0); } /** * Compare two byte arrays for equality */ static areEqual(r, t) { if (r.length !== t.length) return !1; for (let n = 0; n < r.length; n++) if (r[n] !== t[n]) return !1; return !0; } } function Da(e) { if (!Number.isSafeInteger(e) || e < 0) throw new Error("positive integer expected, got " + e); } function Dl(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } function Jo(e, ...r) { if (!Dl(e)) throw new Error("Uint8Array expected"); if (r.length > 0 && !r.includes(e.length)) throw new Error("Uint8Array expected of length " + r + ", got length=" + e.length); } function Fn(e, r = !0) { if (e.destroyed) throw new Error("Hash instance has been destroyed"); if (r && e.finished) throw new Error("Hash#digest() has already been called"); } function Rs(e, r) { Jo(e); const t = r.outputLen; if (e.length < t) throw new Error("digestInto() expects output buffer of length at least " + t); } /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ const Sl = (e) => new Uint32Array(e.buffer, e.byteOffset, Math.floor(e.byteLength / 4)), v0 = (e) => new DataView(e.buffer, e.byteOffset, e.byteLength), Ve = (e, r) => e << 32 - r | e >>> r, un = (e, r) => e << r | e >>> 32 - r >>> 0, Sa = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68, Fl = (e) => e << 24 & 4278190080 | e << 8 & 16711680 | e >>> 8 & 65280 | e >>> 24 & 255; function Fa(e) { for (let r = 0; r < e.length; r++) e[r] = Fl(e[r]); } function Tl(e) { if (typeof e != "string") throw new Error("utf8ToBytes expected string, got " + typeof e); return new Uint8Array(new TextEncoder().encode(e)); } function Qo(e) { return typeof e == "string" && (e = Tl(e)), Jo(e), e; } class Os { // Safe version that clones internal state clone() { return this._cloneInto(); } } function ea(e) { const r = (n) => e().update(Qo(n)).digest(), t = e(); return r.outputLen = t.outputLen, r.blockLen = t.blockLen, r.create = () => e(), r; } function Rl(e, r, t, n) { if (typeof e.setBigUint64 == "function") return e.setBigUint64(r, t, n); const o = BigInt(32), a = BigInt(4294967295), c = Number(t >> o & a), s = Number(t & a), u = n ? 4 : 0, i = n ? 0 : 4; e.setUint32(r + u, c, n), e.setUint32(r + i, s, n); } const Ol = (e, r, t) => e & r ^ ~e & t, Il = (e, r, t) => e & r ^ e & t ^ r & t; class Is extends Os { constructor(r, t, n, o) { super(), this.blockLen = r, this.outputLen = t, this.padOffset = n, this.isLE = o, this.finished = !1, this.length = 0, this.pos = 0, this.destroyed = !1, this.buffer = new Uint8Array(r), this.view = v0(this.buffer); } update(r) { Fn(this); const { view: t, buffer: n, blockLen: o } = this; r = Qo(r); const a = r.length; for (let c = 0; c < a; ) { const s = Math.min(o - this.pos, a - c); if (s === o) { const u = v0(r); for (; o <= a - c; c += o) this.process(u, c); continue; } n.set(r.subarray(c, c + s), this.pos), this.pos += s, c += s, this.pos === o && (this.process(t, 0), this.pos = 0); } return this.length += r.length, this.roundClean(), this; } digestInto(r) { Fn(this), Rs(r, this), this.finished = !0; const { buffer: t, view: n, blockLen: o, isLE: a } = this; let { pos: c } = this; t[c++] = 128, this.buffer.subarray(c).fill(0), this.padOffset > o - c && (this.process(n, 0), c = 0); for (let h = c; h < o; h++) t[h] = 0; Rl(n, o - 8, BigInt(this.length * 8), a), this.process(n, 0); const s = v0(r), u = this.outputLen; if (u % 4) throw new Error("_sha2: outputLen should be aligned to 32bit"); const i = u / 4, l = this.get(); if (i > l.length) throw new Error("_sha2: outputLen bigger than state"); for (let h = 0; h < i; h++) s.setUint32(4 * h, l[h], a); } digest() { const { buffer: r, outputLen: t } = this; this.digestInto(r); const n = r.slice(0, t); return this.destroy(), n; } _cloneInto(r) { r || (r = new this.constructor()), r.set(...this.get()); const { blockLen: t, buffer: n, length: o, finished: a, destroyed: c, pos: s } = this; return r.length = o, r.pos = s, r.finished = a, r.destroyed = c, o % t && r.buffer.set(n), r; } } const Nl = /* @__PURE__ */ new Uint32Array([ 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 ]), xt = /* @__PURE__ */ new Uint32Array([ 1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225 ]), pt = /* @__PURE__ */ new Uint32Array(64); class Hl extends Is { constructor() { super(64, 32, 8, !1), this.A = xt[0] | 0, this.B = xt[1] | 0, this.C = xt[2] | 0, this.D = xt[3] | 0, this.E = xt[4] | 0, this.F = xt[5] | 0, this.G = xt[6] | 0, this.H = xt[7] | 0; } get() { const { A: r, B: t, C: n, D: o, E: a, F: c, G: s, H: u } = this; return [r, t, n, o, a, c, s, u]; } // prettier-ignore set(r, t, n, o, a, c, s, u) { this.A = r | 0, this.B = t | 0, this.C = n | 0, this.D = o | 0, this.E = a | 0, this.F = c | 0, this.G = s | 0, this.H = u | 0; } process(r, t) { for (let h = 0; h < 16; h++, t += 4) pt[h] = r.getUint32(t, !1); for (let h = 16; h < 64; h++) { const d = pt[h - 15], f = pt[h - 2], x = Ve(d, 7) ^ Ve(d, 18) ^ d >>> 3, w = Ve(f, 17) ^ Ve(f, 19) ^ f >>> 10; pt[h] = w + pt[h - 7] + x + pt[h - 16] | 0; } let { A: n, B: o, C: a, D: c, E: s, F: u, G: i, H: l } = this; for (let h = 0; h < 64; h++) { const d = Ve(s, 6) ^ Ve(s, 11) ^ Ve(s, 25), f = l + d + Ol(s, u, i) + Nl[h] + pt[h] | 0, x = (Ve(n, 2) ^ Ve(n, 13) ^ Ve(n, 22)) + Il(n, o, a) | 0; l = i, i = u, u = s, s = c + f | 0, c = a, a = o, o = n, n = f + x | 0; } n = n + this.A | 0, o = o + this.B | 0, a = a + this.C | 0, c = c + this.D | 0, s = s + this.E | 0, u = u + this.F | 0, i = i + this.G | 0, l = l + this.H | 0, this.set(n, o, a, c, s, u, i, l); } roundClean() { pt.fill(0); } destroy() { this.set(0, 0, 0, 0, 0, 0, 0, 0), this.buffer.fill(0); } } const zl = /* @__PURE__ */ ea(() => new Hl()), fn = /* @__PURE__ */ BigInt(2 ** 32 - 1), Ta = /* @__PURE__ */ BigInt(32); function Ll(e, r = !1) { return r ? { h: Number(e & fn), l: Number(e >> Ta & fn) } : { h: Number(e >> Ta & fn) | 0, l: Number(e & fn) | 0 }; } function Ul(e, r = !1) { let t = new Uint32Array(e.length), n = new Uint32Array(e.length); for (let o = 0; o < e.length; o++) { const { h: a, l: c } = Ll(e[o], r); [t[o], n[o]] = [a, c]; } return [t, n]; } const Pl = (e, r, t) => e << t | r >>> 32 - t, Ml = (e, r, t) => r << t | e >>> 32 - t, Wl = (e, r, t) => r << t - 32 | e >>> 64 - t, jl = (e, r, t) => e << t - 32 | r >>> 64 - t, Ns = [], Hs = [], zs = [], $l = /* @__PURE__ */ BigInt(0), wr = /* @__PURE__ */ BigInt(1), Kl = /* @__PURE__ */ BigInt(2), Vl = /* @__PURE__ */ BigInt(7), ql = /* @__PURE__ */ BigInt(256), Gl = /* @__PURE__ */ BigInt(113); for (let e = 0, r = wr, t = 1, n = 0; e < 24; e++) { [t, n] = [n, (2 * t + 3 * n) % 5], Ns.push(2 * (5 * n + t)), Hs.push((e + 1) * (e + 2) / 2 % 64); let o = $l; for (let a = 0; a < 7; a++) r = (r << wr ^ (r >> Vl) * Gl) % ql, r & Kl && (o ^= wr << (wr << /* @__PURE__ */ BigInt(a)) - wr); zs.push(o); } const [Zl, Yl] = /* @__PURE__ */ Ul(zs, !0), Ra = (e, r, t) => t > 32 ? Wl(e, r, t) : Pl(e, r, t), Oa = (e, r, t) => t > 32 ? jl(e, r, t) : Ml(e, r, t); function Xl(e, r = 24) { const t = new Uint32Array(10); for (let n = 24 - r; n < 24; n++) { for (let c = 0; c < 10; c++) t[c] = e[c] ^ e[c + 10] ^ e[c + 20] ^ e[c + 30] ^ e[c + 40]; for (let c = 0; c < 10; c += 2) { const s = (c + 8) % 10, u = (c + 2) % 10, i = t[u], l = t[u + 1], h = Ra(i, l, 1) ^ t[s], d = Oa(i, l, 1) ^ t[s + 1]; for (let f = 0; f < 50; f += 10) e[c + f] ^= h, e[c + f + 1] ^= d; } let o = e[2], a = e[3]; for (let c = 0; c < 24; c++) { const s = Hs[c], u = Ra(o, a, s), i = Oa(o, a, s), l = Ns[c]; o = e[l], a = e[l + 1], e[l] = u, e[l + 1] = i; } for (let c = 0; c < 50; c += 10) { for (let s = 0; s < 10; s++) t[s] = e[c + s]; for (let s = 0; s < 10; s++) e[c + s] ^= ~t[(s + 2) % 10] & t[(s + 4) % 10]; } e[0] ^= Zl[n], e[1] ^= Yl[n]; } t.fill(0); } class ta extends Os { // NOTE: we accept arguments in bytes instead of bits here. constructor(r, t, n, o = !1, a = 24) { if (super(), this.blockLen = r, this.suffix = t, this.outputLen = n, this.enableXOF = o, this.rounds = a, this.pos = 0, this.posOut = 0, this.finished = !1, this.destroyed = !1, Da(n), 0 >= this.blockLen || this.blockLen >= 200) throw new Error("Sha3 supports only keccak-f1600 function"); this.state = new Uint8Array(200), this.state32 = Sl(this.state); } keccak() { Sa || Fa(this.state32), Xl(this.state32, this.rounds), Sa || Fa(this.state32), this.posOut = 0, this.pos = 0; } update(r) { Fn(this); const { blockLen: t, state: n } = this; r = Qo(r); const o = r.length; for (let a = 0; a < o; ) { const c = Math.min(t - this.pos, o - a); for (let s = 0; s < c; s++) n[this.pos++] ^= r[a++]; this.pos === t && this.keccak(); } return this; } finish() { if (this.finished) return; this.finished = !0; const { state: r, suffix: t, pos: n, blockLen: o } = this; r[n] ^= t, t & 128 && n === o - 1 && this.keccak(), r[o - 1] ^= 128, this.keccak(); } writeInto(r) { Fn(this, !1), Jo(r), this.finish(); const t = this.state, { blockLen: n } = this; for (let o = 0, a = r.length; o < a; ) { this.posOut >= n && this.keccak(); const c = Math.min(n - this.posOut, a - o); r.set(t.subarray(this.posOut, this.posOut + c), o), this.posOut += c, o += c; } return r; } xofInto(r) { if (!this.enableXOF) throw new Error("XOF is not possible for this instance"); return this.writeInto(r); } xof(r) { return Da(r), this.xofInto(new Uint8Array(r)); } digestInto(r) { if (Rs(r, this), this.finished) throw new Error("digest() was already called"); return this.writeInto(r), this.destroy(), r; } digest() { return this.digestInto(new Uint8Array(this.outputLen)); } destroy() { this.destroyed = !0, this.state.fill(0); } _cloneInto(r) { const { blockLen: t, suffix: n, outputLen: o, rounds: a, enableXOF: c } = this; return r || (r = new ta(t, n, o, c, a)), r.state32.set(this.state32), r.pos = this.pos, r.posOut = this.posOut, r.finished = this.finished, r.rounds = a, r.suffix = n, r.outputLen = o, r.enableXOF = c, r.destroyed = this.destroyed, r; } } const Jl = (e, r, t) => ea(() => new ta(r, e, t)), Ql = /* @__PURE__ */ Jl(6, 72, 512 / 8), eu = /* @__PURE__ */ new Uint8Array([7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8]), Ls = /* @__PURE__ */ new Uint8Array(new Array(16).fill(0).map((e, r) => r)), tu = /* @__PURE__ */ Ls.map((e) => (9 * e + 5) % 16); let ra = [Ls], na = [tu]; for (let e = 0; e < 4; e++) for (let r of [ra, na]) r.push(r[e].map((t) => eu[t])); const Us = /* @__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) => new Uint8Array(e)), ru = /* @__PURE__ */ ra.map((e, r) => e.map((t) => Us[r][t])), nu = /* @__PURE__ */ na.map((e, r) => e.map((t) => Us[r][t])), ou = /* @__PURE__ */ new Uint32Array([ 0, 1518500249, 1859775393, 2400959708, 2840853838 ]), au = /* @__PURE__ */ new Uint32Array([ 1352829926, 1548603684, 1836072691, 2053994217, 0 ]); function Ia(e, r, t, n) { return e === 0 ? r ^ t ^ n : e === 1 ? r & t | ~r & n : e === 2 ? (r | ~t) ^ n : e === 3 ? r & n | t & ~n : r ^ (t | ~n); } const dn = /* @__PURE__ */ new Uint32Array(16); class iu extends Is { constructor() { super(64, 20, 8, !0), this.h0 = 1732584193, this.h1 = -271733879, this.h2 = -1732584194, this.h3 = 271733878, this.h4 = -1009589776; } get() { const { h0: r, h1: t, h2: n, h3: o, h4: a } = this; return [r, t, n, o, a]; } set(r, t, n, o, a) { this.h0 = r | 0, this.h1 = t | 0, this.h2 = n | 0, this.h3 = o | 0, this.h4 = a | 0; } process(r, t) { for (let f = 0; f < 16; f++, t += 4) dn[f] = r.getUint32(t, !0); let n = this.h0 | 0, o = n, a = this.h1 | 0, c = a, s = this.h2 | 0, u = s, i = this.h3 | 0, l = i, h = this.h4 | 0, d = h; for (let f = 0; f < 5; f++) { const x = 4 - f, w = ou[f], v = au[f], _ = ra[f], p = na[f], g = ru[f], y = nu[f]; for (let b = 0; b < 16; b++) { const A = un(n + Ia(f, a, s, i) + dn[_[b]] + w, g[b]) + h | 0; n = h, h = i, i = un(s, 10) | 0, s = a, a = A; } for (let b = 0; b < 16; b++) { const A = un(o + Ia(x, c, u, l) + dn[p[b]] + v, y[b]) + d | 0; o = d, d = l, l = un(u, 10) | 0, u = c, c = A; } } this.set(this.h1 + s + l | 0, this.h2 + i + d | 0, this.h3 + h + o | 0, this.h4 + n + c | 0, this.h0 + a + u | 0); } roundClean() { dn.fill(0); } destroy() { this.destroyed = !0, this.buffer.fill(0), this.set(0, 0, 0, 0, 0); } } const su = /* @__PURE__ */ ea(() => new iu()); class je { constructor(r = "sha256") { this.algorithm = r, this.hasher = this.createHasher(r); } createHasher(r) { switch (r.toLowerCase()) { case "sha256": return zl.create(); case "sha3-512": return Ql.create(); case "ripemd160": return su.create(); default: throw new Error(`Unsupported hash algorithm: ${r}`); } } /** * Updates the hash with the given data */ update(r, t = 0, n = r.length) { if (t < 0 || t > r.length) throw new Error("Invalid offset"); if (n < 0 || t + n > r.length) throw new Error("Invalid length"); const o = r.subarray(t, t + n); this.hasher.update(o); } /** * Returns the final hash value */ digest() { const r = this.hasher.digest(); return this.hasher = this.createHasher(this.algorithm), r; } static hash(r, t, n) { const o = new je(); return t !== void 0 && n !== void 0 ? o.update(new Uint8Array(r.subarray(t, t + n))) : o.update(new Uint8Array(r)), o.digest(); } static hashWith(r, t) { const n = new je(r); return n.update(t), n.digest(); } } const Do = class { /** * Set chain address in the address buffer */ static setChainAddr(r, t) { r.position(20), r.putInt(t); } /** * Set hash address in the address buffer */ static setHashAddr(r, t) { r.position(24), r.putInt(t); } /** * Set key and mask in the address buffer */ static setKeyAndMask(r, t) { r.position(28), r.putInt(t); } /** * Convert address buffer to bytes in little-endian format */ static addrToBytes(r) { r.position(0); const t = new Uint8Array(r.capacity()); for (let n = 0; n < t.length; n += 4) { const o = r.get_(), a = r.get_(), c = r.get_(), s = r.get_(); t[n] = s, t[n + 1] = c, t[n + 2] = a, t[n + 3] = o; } return t; } /** * PRF function */ static prf(r, t, n, o) { const a = new Uint8Array(96), c = new Uint8Array(32); c[31] = this.XMSS_HASH_PADDING_PRF, a.set(c, 0), a.set(o, 32), a.set(n, 64); const s = new je(); s.update(a); const u = s.digest(); return r.set(u, t), r; } /** * F hash function */ static thashF(r, t, n, o, a, c) { const s = new Uint8Array(96), u = new Uint8Array(32); u[31] = this.XMSS_HASH_PADDING_F, s.set(u, 0), this.setKeyAndMask(c, 0); let i = this.addrToBytes(c); this.prf(s, 32, i, a), this.setKeyAndMask(c, 1), i = this.addrToBytes(c); const l = new Uint8Array(32); this.prf(l, 0, i, a); for (let f = 0; f < 32; f++) s[64 + f] = n[f + o] ^ l[f]; const h = new je(); h.update(s); const d = h.digest(); r.set(d, t); } }; Do.XMSS_HASH_PADDING_F = 0, Do.XMSS_HASH_PADDING_PRF = 3; let Jt = Do; const Ps = class Er { /** * Gets the tag from an address */ static getTag(r) { if (r.length !== 2208) throw new Error("Invalid address length"); const t = new Uint8Array(Er.TAG_LENGTH); return t.set(r.subarray(r.length - Er.TAG_LENGTH)), t; } /** * Checks if a tag is all zeros */ static isZero(r) { return !r || r.length !== Er.TAG_LENGTH ? !1 : r.every((t) => t === 0); } /** * Validates a tag */ static isValid(r) { return !(!r || r.length !== Er.TAG_LENGTH); } /** * Tags an address with the specified tag */ static tag(r, t) { if (!this.isValid(t)) throw new Error("Invalid tag"); if (r.length !== 2208) throw new Error("Invalid address length"); if (t.length !== 12) throw new Error("Invalid tag length"); const n = new Uint8Array(r); return n.set(t, n.length - t.length), n; } }; Ps.TAG_LENGTH = 12; let Na = Ps; const at = class Ze { /** * Generates chains for WOTS */ static gen_chain(r, t, n, o, a, c, s, u) { r.set(n.subarray(o, o + Ze.PARAMSN), t); for (let i = a; i < a + c && i < 16; i++) Jt.setHashAddr(u, i), Jt.thashF(r, t, r, t, s, u); } /** * Expands seed into WOTS private key */ static expand_seed(r, t) { for (let n = 0; n < Ze.WOTSLEN; n++) { const o = Ye.toBytes(n, 32); Jt.prf(r, n * 32, o, t); } } /** * Converts message to base w (convenience overload) */ static base_w(r, t) { return this.base_w_(r, t, 0, t.length); } /** * Converts message to base w */ static base_w_(r, t, n = 0, o = t.length) { let a = 0, c = 0, s = 0, u = 0; for (let i = 0; i < o; i++) u === 0 && (s = r[a++], u += 8), u -= 4, t[c++ + n] = s >> u & 15; return t; } /** * Computes WOTS checksum */ static wotsChecksum(r, t) { let n = 0; for (let a = 0; a < 64; a++) n += 15 - r[a]; n <<= 4; const o = new Uint8Array(2); return o[0] = n >> 8 & 255, o[1] = n & 255, this.base_w_(o, r, t, r.length - t); } /** * Computes chain lengths */ static chain_lengths(r, t) { const n = this.base_w_(r, t, 0, 64); return this.wotsChecksum(n, 64); } /** * Generates WOTS public key */ static wots_pkgen(r, t, n, o, a) { this.expand_seed(r, t); const c = rr.wrap(a); c.order(Cn.LITTLE_ENDIAN); for (let s = 0; s < Ze.WOTSLEN; s++) Jt.setChainAddr(c, s), this.gen_chain(r, s * 32, r, s * 32, 0, 15, n.subarray(o), c); } /** * Signs a message using WOTS */ static wots_sign(r, t, n, o, a, c) { const s = new Array(Ze.WOTSLEN); this.chain_lengths(t, s), this.expand_seed(r, n); const u = rr.wrap(c); u.order(Cn.LITTLE_ENDIAN); for (let i = 0; i < Ze.WOTSLEN; i++) Jt.setChainAddr(u, i), this.gen_chain(r, i * 32, r, i * 32, 0, s[i], o.subarray(a), u); } /** * Verifies a WOTS signature */ static wots_pk_from_sig(r, t, n, o) { const a = new Uint8Array(Ze.WOTSSIGBYTES), c = new Array(Ze.WOTSLEN), s = new Uint8Array(o), u = rr.wrap(s); u.order(Cn.LITTLE_ENDIAN), this.chain_lengths(t, c); for (let i = 0; i < Ze.WOTSLEN; i++) Jt.setChainAddr(u, i), this.gen_chain(a, i * 32, r, i * 32, c[i], 15 - c[i], n, u); return a; } /** * Generates a WOTS address using the componentsGenerator. * Note:: use you own componentsGenerator that fills in deterministic bytes if you want to generate a specific address */ static generateAddress(r, t, n) { if (!n) throw new Error("Invalid componentsGenerator"); if (t.length !== 32) throw new Error("Invalid secret length"); if (r !== null && r.length !== 12) throw new Error("Invalid tag"); const o = new Uint8Array(2144), a = n(t); Ze.wots_pkgen(o, a.private_seed, a.public_seed, 0, a.addr_seed); const c = new Uint8Array(2208); c.set(o, 0), c.set(a.public_seed, 2144), c.set(a.addr_seed, 2176); const s = r ? Na.tag(c, r) : c; for (let u = 0; u < 10; u++) if (!this.isValid(a.private_seed, s, hn)) throw new Error("Invalid WOTS"); return s; } /** * Validates WOTS components */ static isValidWithComponents(r, t, n, o, a) { if (r.length !== 32) throw new Error("Invalid secret length"); if (t.length !== 2144) throw new Error("Invalid pk length"); if (n.length !== 32) throw new Error("Invalid pubSeed length"); if (o.length !== 32) throw new Error("Invalid rnd2 length"); const c = new Uint8Array(32); a(c); const s = new Uint8Array(2144); this.wots_sign(s, c, r, n, 0, o); const u = this.wots_pk_from_sig(s, c, n, o); return Ye.compareBytes(u, t); } /** * Splits a WOTS address into its components */ static splitAddress(r, t, n, o, a) { if (r.length !== 2208) throw new Error("Invalid address length"); if (t.length !== 2144) throw new Error("Invalid pk length"); if (n.length !== 32) throw new Error("Invalid pubSeed length"); if (o.length !== 32) throw new Error("Invalid rnd2 length"); if (a !== null && a.length !== 12) throw new Error("Invalid tag length"); t.set(r.subarray(0, 2144)), n.set(r.subarray(2144, 2176)), o.set(r.subarray(2176, 2208)), a !== null && a.set(o.subarray(20, 32)); } /** * Validates a WOTS address using a Random generator */ static isValid(r, t, n = hn) { const o = new Uint8Array(2144), a = new Uint8Array(32), c = new Uint8Array(32); return this.splitAddress(t, o, a, c, null), this.isValidWithComponents(r, o, a, c, hn); } /** * Generates a random WOTS address using the randomGenerator * Note:: use you own randomGenerator that fills in deterministic bytes if you want to generate a specific address */ static generateRandomAddress(r, t, n = hn) { if (t.length !== 32) throw new Error("Invalid secret length"); if (r !== null && r.length !== 12) throw new Error("Invalid tag"); const o = new Uint8Array(2208), a = new Uint8Array(32); n(o), a.set(o.subarray(2176, 2208)), this.wots_pkgen(o, t, o, 2144, a), o.set(a, 2176); const c = r ? Na.tag(o, r) : o; for (let s = 0; s < 10; s++) if (!this.isValid(t, c, n)) throw new Error("Invalid WOTS"); return c; } }; at.WOTSW = 16, at.WOTSLOGW = 4, at.PARAMSN = 32, at.WOTSLEN1 = 64, at.WOTSLEN2 = 3, at.WOTSLEN = 67, at.WOTSSIGBYTES = 2144, at.TXSIGLEN = 2144; let ae = at; function hn(e) { for (let r = 0; r < e.length; r++) e[r] = Math.floor(Math.random() * 256); } const Ce = 40, ke = 20, w0 = 2144, _0 = 8; class Lt { constructor() { this.address = new Uint8Array(Ce), this.amount = BigInt(0); } bytes() { const r = new Uint8Array(Ce + _0); return r.set(this.address), r.set(this.getAmountBytes(), Ce), r; } getTag() { return this.address.slice(0, ke); } setTag(r) { this.address.set(r.slice(0, ke), 0); } getAddrHash() { return this.address.slice(ke, Ce); } getAddress() { return this.address.slice(0, Ce); } setAddrHash(r) { this.address.set(r.slice(0, ke), ke); } setAmountBytes(r) { this.amount = BigInt( new DataView(r.buffer).getBigUint64(0, !0) ); } getAmount() { return this.amount; } getAmountBytes() { const r = new ArrayBuffer(_0); return new DataView(r).setBigUint64(0, this.amount, !0), new Uint8Array(r); } static wotsAddressFromBytes(r) { const t = new Lt(); if (r.length === w0) { const n = this.addrFromWots(r); n && (t.setTag(n.slice(0, ke)), t.setAddrHash(n.slice(ke, Ce))); } else r.length === Ce ? (t.setTag(r.slice(0, ke)), t.setAddrHash(r.slice(ke, Ce))) : r.length === Ce + _0 && (t.setTag(r.slice(0, ke)), t.setAddrHash(r.slice(ke, Ce)), t.setAmountBytes(r.slice(Ce))); return t; } static wotsAddressFromHex(r) { const t = Buffer.from(r, "hex"); return t.length !== Ce ? new Lt() : this.wotsAddressFromBytes(t); } static addrFromImplicit(r) { const t = new Uint8Array(Ce); return t.set(r.slice(0, ke), 0), t.set(r.slice(0, Ce - ke), ke), t; } static addrHashGenerate(r) { const t = je.hashWith("sha3-512", r); return je.hashWith("ripemd160", t); } static addrFromWots(r) { if (r.length !== w0) return null; const t = this.addrHashGenerate(r.slice(0, w0)); return this.addrFromImplicit(t); } } const cu = [ 0, 4129, 8258, 12387, 16516, 20645, 24774, 28903, 33032, 37161, 41290, 45419, 49548, 53677, 57806, 61935, 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302, 37689, 33560, 45947, 41818, 54205, 50076, 62463, 58334, 9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637, 42346, 46411, 34088, 38153, 58862, 62927, 50604, 54669, 13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100, 46939, 42874, 38681, 34616, 63455, 59390, 55197, 51132, 18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371, 51660, 55789, 59790, 63919, 35144, 39273, 43274, 47403, 23285, 19156, 31415, 27286, 6769, 2640, 14899, 10770, 56317, 52188, 64447, 60318, 39801, 35672, 47931, 43802, 27814, 31879, 19684, 23749, 11298, 15363, 3168, 7233, 60846, 64911, 52716, 56781, 44330, 48395, 36200, 40265, 32407, 28342, 24277, 20212, 15891, 11826, 7761, 3696, 65439, 61374, 57309, 53244, 48923, 44858, 40793, 36728, 37256, 33193, 45514, 41451, 53516, 49453, 61774, 57711, 4224, 161, 12482, 8419, 20484, 16421, 28742, 24679, 33721, 37784, 41979, 46042, 49981, 54044, 58239, 62302, 689, 4752, 8947, 13010, 16949, 21012, 25207, 29270, 46570, 42443, 38312, 34185, 62830, 58703, 54572, 50445, 13538, 9411, 5280, 1153, 29798, 25671, 21540, 17413, 42971, 47098, 34713, 38840, 59231, 63358, 50973, 55100, 9939, 14066, 1681, 5808, 26199, 30326, 17941, 22068, 55628, 51565, 63758, 59695, 39368, 35305, 47498, 43435, 22596, 18533, 30726, 26663, 6336, 2273, 14466, 10403, 52093, 56156, 60223, 64286, 35833, 39896, 43963, 48026, 19061, 23124, 27191, 31254, 2801, 6864, 10931, 14994, 64814, 60687, 56684, 52557, 48554, 44427, 40424, 36297, 31782, 27655, 23652, 19525, 15522, 11395, 7392, 3265, 61215, 65342, 53085, 57212, 44955, 49082, 36825, 40952, 28183, 32310, 20053, 24180, 11923, 16050, 3793, 7920 ]; function lu(e, r, t) { if (r + t > e.length) throw new Error("Offset + length exceeds array bounds"); let n = 0; for (let o = r; o < r + t; o++) { const a = e[o] & 255, c = (n >>> 8 ^ a) & 255; n = (n << 8 ^ cu[c]) & 65535; } return n; } function uu(e) { if (e.length >= 255) throw new TypeError("Alphabet too long"); const r = new Uint8Array(256); for (let i = 0; i < r.length; i++) r[i] = 255; for (let i = 0; i < e.length; i++) { const l = e.charAt(i), h = l.charCodeAt(0); if (r[h] !== 255) throw new TypeError(l + " is ambiguous"); r[h] = i; } const t = e.length, n = e.charAt(0), o = Math.log(t) / Math.log(256), a = Math.log(256) / Math.log(t); function c(i) { if (i instanceof Uint8Array || (ArrayBuffer.isView(i) ? i = new Uint8Array(i.buffer, i.byteOffset, i.byteLength) : Array.isArray(i) && (i = Uint8Array.from(i))), !(i instanceof Uint8Array)) throw new TypeError("Expected Uint8Array"); if (i.length === 0) return ""; let l = 0, h = 0, d = 0; const f = i.length; for (; d !== f && i[d] === 0; ) d++, l++; const x = (f - d) * a + 1 >>> 0, w = new Uint8Array(x); for (; d !== f; ) { let p = i[d], g = 0; for (let y = x - 1; (p !== 0 || g < h) && y !== -1; y--, g++) p += 256 * w[y] >>> 0, w[y] = p % t >>> 0, p = p / t >>> 0; if (p !== 0) throw new Error("Non-zero carry"); h = g, d++; } let v = x - h; for (; v !== x && w[v] === 0; ) v++; let _ = n.repeat(l); for (; v < x; ++v) _ += e.charAt(w[v]); return _; } function s(i) { if (typeof i != "string") throw new TypeError("Expected String"); if (i.length === 0) return new Uint8Array(); let l = 0, h = 0, d = 0; for (; i[l] === n; ) h++, l++; const f = (i.length - l) * o + 1 >>> 0, x = new Uint8Array(f); for (; i[l]; ) { let p = r[i.charCodeAt(l)]; if (p === 255) return; let g = 0; for (let y = f - 1; (p !== 0 || g < d) && y !== -1; y--, g++) p += t * x[y] >>> 0, x[y] = p % 256 >>> 0, p = p / 256 >>> 0; if (p !== 0) throw new Error("Non-zero carry"); d = g, l++; } let w = f - d; for (; w !== f && x[w] === 0; ) w++; const v = new Uint8Array(h + (f - w)); let _ = h; for (; w !== f; ) v[_++] = x[w++]; return v; } function u(i) { const l = s(i); if (l) return l; throw new Error("Non-base" + t + " character"); } return { encode: c, decodeUnsafe: s, decode: u }; } var fu = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; const du = uu(fu); function hu(e) { if (!e) return null; if (e.length !== 20) throw new Error("Invalid address tag length"); const r = lu(e, 0, 20), t = new Uint8Array(22); t.set(e); const n = [r & 255, r >> 8 & 255]; return t.set(n, e.length), du.encode(t); } let Ut = class Ms { /** * Creates a new WOTS wallet */ constructor({ name: r = null, wots: t = null, addrTag: n = null, secret: o = null }) { var a; if (o && o.length !== 32) throw new Error("Invalid secret length"); if (n && n.length !== 20) throw new Error("Invalid address tag"); this.name = r, this.wots = t ? new Uint8Array(t) : null, this.addrTag = n ? new Uint8Array(n) : null, this.secret = o ? new Uint8Array(o) : null, this.wotsAddrHex = this.wots ? Ye.bytesToHex(this.wots) : null, this.addrTagHex = this.addrTag ? Ye.bytesToHex(this.addrTag) : null, this.mochimoAddr = this.wots ? Lt.wotsAddressFromBytes(this.wots.slice(0, 2144)) : null, (a = this.mochimoAddr) == null || a.setTag(this.addrTag); } getName() { return this.name; } /** * Get the full wots address (2208 bytes) * @returns */ getWots() { return this.wots ? new Uint8Array(this.wots) : null; } /** * Get the hex string of the full wots address */ getWotsHex() { return this.wotsAddrHex; } /** * Get the wots public key (2144 bytes) */ getWotsPk() { return this.wots ? new Uint8Array(this.wots.slice(0, ae.WOTSSIGBYTES)) : null; } /** * Get the public seed used when generating the wots address */ getWotsPubSeed() { return this.wots ? this.wots.subarray(ae.WOTSSIGBYTES, ae.WOTSSIGBYTES + 32) : null; } /** * Get the wots+ address scheme used when generating the address */ getWotsAdrs() { return this.wots ? this.wots.subarray(ae.WOTSSIGBYTES + 32, ae.WOTSSIGBYTES + 64) : null; } /** * Get the wots+ tag used when generating the address */ getWotsTag() { return this.wots ? this.wots.subarray(ae.WOTSSIGBYTES + 64 - 12, ae.WOTSSIGBYTES + 64) : null; } /** * Get the 40 byte mochimo address [20 bytes tag + 20 bytes address] */ getAddress() { return this.mochimoAddr ? this.mochimoAddr.bytes().slice(0, 40) : null; } /** * Get the address tag (20 bytes) */ getAddrTag() { return this.addrTag ? new Uint8Array(this.addrTag) : null; } getAddrTagHex() { return this.addrTagHex; } getAddrTagBase58() { return this.addrTag ? hu(this.getAddrTag()) : null; } /** * Get the address hash of mochimo address (20 bytes) */ getAddrHash() { return this.mochimoAddr ? this.mochimoAddr.getAddrHash() : null; } getSecret() { return this.secret ? new Uint8Array(this.secret) : null; } hasSecret() { return this.secret !== null; } /** * Sign data using the secret key */ sign(r) { const t = this.secret, n = this.wots; if (!t || !n) throw new Error("Cannot sign without secret key or address"); if (t.length !== 32) throw new Error("Invalid sourceSeed length, expected 32, got " + t.length); if (n.length !== 2208) throw new Error("Invalid sourceWots length, expected 2208, got " + n.length); n.subarray(0, ae.WOTSSIGBYTES); const o = n.subarray(ae.WOTSSIGBYTES, ae.WOTSSIGBYTES + 32), a = n.subarray(ae.WOTSSIGBYTES + 32, ae.WOTSSIGBYTES + 64), c = new Uint8Array(ae.WOTSSIGBYTES); return ae.wots_sign(c, r, t, o, 0, a), c; } /** * Verifies whether a signature is valid for a given message */ verify(r, t) { if (!this.wots) throw new Error("Cannot verify without public key (address)"); const n = this.wots, o = n.subarray(0, ae.WOTSSIGBYTES), a = n.subarray(ae.WOTSSIGBYTES, ae.WOTSSIGBYTES + 32), c = n.subarray(ae.WOTSSIGBYTES + 32, ae.WOTSSIGBYTES + 64), s = ae.wots_pk_from_sig(t, r, a, c); return Ye.areEqual(s, o); } /** * Address components generator used for generating address components for pk generation * @param wotsSeed * @returns */ static componentsGenerator(r) { const t = Buffer.from(r).toString("ascii"), n = je.hash(Buffer.from(t + "seed", "ascii")), o = je.hash(Buffer.from(t + "publ", "ascii")), a = je.hash(Buffer.from(t + "addr", "ascii")); return { private_seed: n, public_seed: o, addr_seed: a }; } clear() { this.secret && Ye.clear(this.secret), this.wots && Ye.clear(this.wots), this.addrTag && Ye.clear(this.addrTag), this.addrTagHex && (this.addrTagHex = null), this.wotsAddrHex && (this.wotsAddrHex = null), this.mochimoAddr && (this.mochimoAddr = null); } toString() { let r = "Empty address"; return this.wotsAddrHex ? r = `${this.wotsAddrHex.substring(0, 32)}...${this.wotsAddrHex.substring(this.wotsAddrHex.length - 24)}` : this.addrTagHex && (r = `tag-${this.addrTagHex}`), r; } /** * Creates a wallet instance */ static create(r, t, n, o) { if (t.length !== 32) throw new Error("Invalid secret length"); let a = t, c = null; const s = Buffer.from("420000000e00000001000000", "hex"); if (o ? c = ae.generateRandomAddress(s, t, o) : ({ private_seed: a } = this.componentsGenerator(t), c = ae.generateAddress(s, t, this.componentsGenerator)), c.length !== 2208) throw new Error("Invalid sourcePK length"); let u = n; if (u || (u = Lt.wotsAddressFromBytes(c.slice(0, 2144)).getTag()), u.length !== 20) throw new Error("Invalid tag"); return new Ms({ name: r, wots: c, addrTag: u, secret: a }); } toJSON() { return { name: this.name, wots: this.wots, addrTag: this.addrTag, secret: this.secret, addrTagHex: this.addrTagHex, wotsAddrHex: this.wotsAddrHex }; } }; function Dr(e) { if (!Number.isSafeInteger(e) || e < 0) throw new Error("positive integer expected, got " + e); } function xu(e) { return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array"; } function Yn(e, ...r) { if (!xu(e)) throw new Error("Uint8Array expected"); if (r.length > 0 && !r.includes(e.length)) throw new Error("Uint8Array expected of length " + r + ", got length=" + e.length); } function Ws(e) { if (typeof e != "function" || typeof e.create != "function") throw new Error("Hash should be wrapped by utils.wrapConstructor"); Dr(e.outputLen), Dr(e.blockLen); } function Tn(e, r = !0) { if (e.destroyed) throw new Error("Hash instance has been destroyed"); if (r && e.finished) throw new Error("Hash#digest() has already been called"); } function pu(e, r) { Yn(e); const t = r.outputLen; if (e.length < t) throw new Error("digestInto() expects output buffer of length at least " + t); } /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ const kn = (e) => new DataView(e.buffer, e.byteOffset, e.byteLength), qe = (e, r) => e << 32 - r | e >>> r, gu = async () => { }; async function yu(e, r, t) { let n = Date.now(); for (let o = 0; o < e; o++) { t(o); const a = Date.now() - n; a >= 0 && a < r || (await gu(), n += a); } } function vu(e) { if (typeof e != "string") throw new Error("utf8ToBytes expected string, got " + typeof e); return new Uint8Array(new TextEncoder().encode(e)); } function Or(e) { return typeof e == "string" && (e = vu(e)), Yn(e), e; } class js { // Safe version that clones internal state clone() { return this._cloneInto(); } } function wu(e, r) { if (r !== void 0 && {}.toString.call(r) !== "[object Object]") throw new Error("Options should be object or undefined"); return Object.assign(e, r); } function $s(e) { const r = (n) => e().update(Or(n)).digest(), t = e(); return r.outputLen = t.outputLen, r.blockLen = t.blockLen, r.create = () => e(), r; } class Ks extends js { constructor(r, t) { super(), this.finished = !1, this.destroyed = !1, Ws(r); const n = Or(t); if (this.iHash = r.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 o = this.blockLen, a = new Uint8Array(o); a.set(n.length > o ? r.create().update(n).digest() : n); for (let c = 0; c < a.length; c++) a[c] ^= 54; this.iHash.update(a), this.oHash = r.create(); for (let c = 0; c < a.length; c++) a[c] ^= 106; this.oHash.update(a), a.fill(0); } update(r) { return Tn(this), this.iHash.update(r), this; } digestInto(r) { Tn(this), Yn(r, this.outputLen), this.finished = !0, this.iHash.digestInto(r), this.oHash.update(r), this.oHash.digestInto(r), this.destroy(); } digest() { const r = new Uint8Array(this.oHash.outputLen); return this.digestInto(r), r; } _cloneInto(r) { r || (r = Object.create(Object.getPrototypeOf(this), {})); const { oHash: t, iHash: n, finished: o, destroyed: a, blockLen: c, outputLen: s } = this; return r = r, r.finished = o, r.destroyed = a, r.blockLen = c, r.outputLen = s, r.oHash = t._cloneInto(r.oHash), r.iHash = n._cloneInto(r.iHash), r; } destroy() { this.destroyed = !0, this.oHash.destroy(), this.iHash.destroy(); } } const Vs = (e, r, t) => new Ks(e, r).update(t).digest(); Vs.create = (e, r) => new Ks(e, r); function _u(e, r, t, n) { Ws(e); const o = wu({ dkLen: 32, asyncTick: 10 }, n), { c: a, dkLen: c, asyncTick: s } = o; if (Dr(a), Dr(c), Dr(s), a < 1) throw new Error("PBKDF2: iterations (c) should be >= 1"); const u = Or(r), i = Or(t), l = new Uint8Array(c), h = Vs.create(e, u), d = h._cloneInto().update(i); return { c: a, dkLen: c, asyncTick: s, DK: l, PRF: h, PRFSalt: d }; } function bu(e, r, t, n, o) { return e.destroy(), r.destroy(), n && n.destroy(), o.fill(0), t; } async function mu(e, r, t, n) { const { c: o, dkLen: a, asyncTick: c, DK: s, PRF: u, PRFSalt: i } = _u(e, r, t, n); let l; const h = new Uint8Array(4), d = kn(h), f = new Uint8Array(u.outputLen); for (let x = 1, w = 0; w < a; x++, w += u.outputLen) { const v = s.subarray(w, w + u.outputLen); d.setInt32(0, x, !1), (l = i._cloneInto(l)).update(h).digestInto(f), v.set(f.subarray(0, v.length)), await yu(o - 1, c, () => { u._cloneInto(l).update(f).digestInto(f); for (let _ = 0; _ < v.length; _++) v[_] ^= f[_]; }); } return bu(u, i, s, l, f); } function Eu(e, r, t, n) { if (typeof e.setBigUint64 == "function") return e.setBigUint64(r, t, n); const o = BigInt(32), a = BigInt(4294967295), c = Number(t >> o & a), s = Number(t & a), u = n ? 4 : 0, i = n ? 0 : 4; e.setUint32(r + u, c, n), e.setUint32(r + i, s, n); } const Au = (e, r, t) => e & r ^ ~e & t, Bu = (e, r, t) => e & r ^ e & t ^ r & t; class qs extends js { constructor(r, t, n, o) { super(), this.blockLen = r, this.outputLen = t, this.padOffset = n, this.isLE = o, this.finished = !1, this.length = 0, this.pos = 0, this.destroyed = !1, this.buffer = new Uint8Array(r), this.view = kn(this.buffer); } update(r) { Tn(this); const { view: t, buffer: n, blockLen: o } = this; r = Or(r); const a = r.length; for (let c = 0; c < a; ) { const s = Math.min(o - this.pos, a - c); if (s === o) { const u = kn(r); for (; o <= a - c; c += o) this.process(u, c); continue; } n.set(r.subarray(c, c + s), this.pos), this.pos += s, c += s, this.pos === o && (this.process(t, 0), this.pos = 0); } return this.length += r.length, this.roundClean(), this; } digestInto(r) { Tn(this), pu(r, this), this.finished = !0; const { buffer: t, view: n, blockLen: o, isLE: a } = this; let { pos: c } = this; t[c++] = 128, this.buffer.subarray(c).fill(0), this.padOffset > o - c && (this.process(n, 0), c = 0); for (let h = c; h < o; h++) t[h] = 0; Eu(n, o - 8, BigInt(this.length * 8), a), this.process(n, 0); const s = kn(r), u = this.outputLen; if (u % 4) throw new Error("_sha2: outputLen should be aligned to 32bit"); const i = u / 4, l = this.get(); if (i > l.length) throw new Error("_sha2: outputLen bigger than state"); for (let h = 0; h < i; h++) s.setUint32(4 * h, l[h], a); } digest() { const { buffer: r, outputLen: t } = this; this.digestInto(r); const n = r.slice(0, t); return this.destroy(), n; } _cloneInto(r) { r || (r = new this.constructor()), r.set(...this.get()); const { blockLen: t, buffer: n, length: o, finished: a, destroyed: c, pos: s } = this; return r.length = o, r.pos = s, r.finished = a, r.destroyed = c, o % t && r.buffer.set(n), r; } } const Cu = /* @__PURE__ */ new Uint32Array([ 1116352408, 1899447441, 3049323471, 3921009573, 961987163, 1508970993, 2453635748, 2870763221, 3624381080, 310598401, 607225278, 1426881987, 1925078388, 2162078206, 2614888103,