UNPKG

libnexa-ts

Version:

A pure and powerful Nexa SDK library.

1,586 lines 220 kB
import { isString as c, isNumber as z, isUndefined as y, isNil as _, isObject as T, isInteger as ie, isArray as Ft, inRange as ot, isNull as Ne, isEmpty as $, isDate as Fe } from "lodash-es"; import * as se from "base64-js"; import Ue from "bn.js"; import He from "elliptic"; import ne from "js-big-decimal"; import ue from "bs58"; class R { /** * Determines whether a string contains only hexadecimal values * * @param value * @returns true if the string is the hexa representation of a number */ static isHexa(t) { return c(t) && t.length % 2 === 0 && /^[0-9a-fA-F]+$/.test(t); } /** * Test if an argument is a valid JSON object. If it is, returns a truthy * value (the json object decoded), so no double JSON.parse call is necessary * * @param arg * @return false if the argument is not a JSON string. */ static isValidJSON(t) { if (!c(t)) return !1; try { return JSON.parse(t); } catch { return !1; } } /** * Checks that a value is a natural number. * * @param value * @return true if a positive integer or zero. */ static isNaturalNumber(t) { return typeof t == "number" && isFinite(t) && Math.floor(t) === t && t >= 0; } /** * Checks that a value is a natural number. * * @param value * @return true if a positive integer or zero. */ static isNaturalBigInt(t) { return typeof t == "bigint" && t >= 0n; } } class a { static validateState(t, e) { if (!t) throw new Error(`Invalid State: ${e}`); } static validateArgument(t, e, r = "") { if (!t) throw new Error(`Invalid Argument: ${e}. ${r}`); } static validateArgumentType(t, e, r) { if (r = r || "(unknown name)", c(e)) { if (typeof t !== e) throw new TypeError(`Invalid Argument for ${r}, expected ${e} but got ${typeof t}`); } else if (!(t instanceof e)) throw new TypeError(`Invalid Argument for ${r}, expected ${e} but got ${typeof t}`); } } class u { /** * Tests for both node's Buffer and Uint8Array * * @param arg * @return Returns true if the given argument is an instance of a Uint8Array. */ static isBuffer(t) { return t instanceof Uint8Array; } /** * Tests for both node's Buffer and Uint8Array * * @param arg * @return Returns true if the given argument is an instance of a hash160 or hash256 buffer. */ static isHashBuffer(t) { return this.isBuffer(t) && (t.length === 20 || t.length === 32); } /** * Reverse a Uint8Array * @param param * @return new reversed Uint8Array */ static reverse(t) { return Uint8Array.from(t).reverse(); } /** * Convert a Uint8Array to a UTF-8 string. * * @param buffer - Uint8Array containing UTF-8 encoded bytes * @returns Decoded string */ static bufferToUtf8(t) { return new TextDecoder("utf-8").decode(t); } /** * Convert a UTF-8 string to a Uint8Array. * * @param str - UTF-8 string * @returns Encoded Uint8Array */ static utf8ToBuffer(t) { return new TextEncoder().encode(t); } /** * Convert a Uint8Array to a Base64 string. * * @param buffer - Uint8Array containing Base64 encoded bytes * @returns Decoded string */ static bufferToBase64(t) { return se.fromByteArray(t); } /** * Convert a Base64 string to a Uint8Array. * * @param str - Base64 string * @returns Encoded Uint8Array */ static base64ToBuffer(t) { return se.toByteArray(t); } /** * Transforms a buffer into a string with a number in hexa representation * * Similar for <tt>buffer.toString('hex')</tt> * * @param buffer * @return string */ static bufferToHex(t) { return a.validateArgumentType(t, Uint8Array, "buffer"), Array.from(t).map((e) => e.toString(16).padStart(2, "0")).join(""); } /** * Convert a hexadecimal string into a Uint8Array. * * @param hex - Hex string (must have even length) * @returns Uint8Array representing the bytes */ static hexToBuffer(t) { if (!R.isHexa(t)) return new Uint8Array(); const e = t.length / 2, r = new Uint8Array(e); for (let s = 0; s < e; s++) r[s] = parseInt(t.slice(s * 2, s * 2 + 2), 16); return r; } /** * Concatenate multiple Uint8Arrays into a single Uint8Array. * * Mimics Node.js Buffer.concat(list, totalLength?): * - list: Array of Uint8Arrays * - totalLength: Optional precomputed total length * * @param list - Array of Uint8Arrays to concatenate * @param totalLength - Optional total length to preallocate * @returns New Uint8Array containing all bytes from input arrays * * @example * const a = new Uint8Array([1,2]); * const b = new Uint8Array([3,4]); * const result = Uint8ArrayUtils.concat([a,b]); * console.log(result); // Uint8Array(4) [1,2,3,4] */ static concat(t, e) { const r = e ?? t.reduce((h, f) => h + f.length, 0), s = new Uint8Array(r); let n = 0; for (const h of t) s.set(h, n), n += h.length; return s; } /** * Compares two Uint8Arrays for byte-wise equality. * * This function checks whether the two arrays have the same length * and the same content. * * @param a - The first Uint8Array to compare. * @param b - The second Uint8Array to compare. * @returns `true` if the arrays have the same length and contents, `false` otherwise. */ static equals(t, e) { if (t === e) return !0; if (t.byteLength !== e.byteLength) return !1; for (let r = 0; r < t.byteLength; r++) if (t[r] !== e[r]) return !1; return !0; } /** * Transforms a number from 0 to 255 into a Uint8Array of size 1 with that value * * @param integer * @return Uint8Array */ static integerAsSingleByteBuffer(t) { return a.validateArgumentType(t, "number", "integer"), new Uint8Array([t & 255]); } /** * Transforms the first byte of an array into a number ranging from -128 to 127 * * @param buffer * @return number */ static integerFromSingleByteBuffer(t) { return a.validateArgumentType(t, Uint8Array, "buffer"), t[0]; } /** * Transform a 4-byte integer into a Uint8Array of length 4. * * @param integer * @return Uint8Array */ static integerAsBuffer(t) { a.validateArgumentType(t, "number", "integer"); const e = new Uint8Array(4); return e[0] = t >> 24 & 255, e[1] = t >> 16 & 255, e[2] = t >> 8 & 255, e[3] = t & 255, e; } /** * Transform the first 4 values of a Uint8Array into a number, in little endian encoding * * @param buffer * @return integer */ static integerFromBuffer(t) { return a.validateArgumentType(t, Uint8Array, "buffer"), t[0] << 24 | t[1] << 16 | t[2] << 8 | t[3]; } /** * @return secure random bytes */ static getRandomBuffer(t) { const e = new Uint8Array(t); return crypto.getRandomValues(e), e; } } class vt { name; alias; prefix; pubkeyhash; privatekey; scripthash; xpubkey; xprivkey; networkMagic; port; dnsSeeds; constructor(t) { this.name = t.name, this.alias = t.alias, this.prefix = t.prefix, this.pubkeyhash = t.pubkeyhash, this.privatekey = t.privatekey, this.scripthash = t.scripthash, this.xpubkey = t.xpubkey, this.xprivkey = t.xprivkey, this.networkMagic = u.integerAsBuffer(t.networkMagic), this.port = t.port, this.dnsSeeds = t.dnsSeeds; } toString() { return this.name; } } const jt = new vt({ name: "mainnet", alias: "livenet", prefix: "nexa", pubkeyhash: 25, privatekey: 35, scripthash: 68, xpubkey: 1114203936, xprivkey: 1114401651, networkMagic: 1915163169, port: 7228, dnsSeeds: [ // from https://gitlab.com/nexa/nexa/-/blob/dev/src/chainparams.cpp#L592 "seed.nextchain.cash", "seeder.nexa.org", "nexa-seeder.bitcoinunlimited.info" ] }), ae = new vt({ name: "testnet", alias: "testnet", prefix: "nexatest", pubkeyhash: 111, privatekey: 239, scripthash: 196, xpubkey: 70617039, xprivkey: 70615956, networkMagic: 1915163170, port: 7230, dnsSeeds: [ "nexa-testnet-seeder.bitcoinunlimited.info", "testnetseeder.nexa.org" ] }); class Rt { static _instance = new Rt(); networks = [jt, ae]; _defaultNetwork = jt; get mainnet() { return jt; } get testnet() { return ae; } get defaultNetwork() { return this._defaultNetwork; } set defaultNetwork(t) { this._defaultNetwork = t; } /** * @returns the singleton instance of NetworkManager */ static getInstance() { return this._instance; } get(t, e) { if (t instanceof vt) { if (this.networks.includes(t)) return t; if (this.networks.map((r) => r.name).includes(t.name)) return this.networks.find((r) => r.name == t.name); } return e ? this.networks.find((r) => e == "networkMagic" ? u.integerFromBuffer(r[e]) == t : r[e] == t) : this.networks.find((r) => Object.keys(r).some((s) => { let n = s; return n == "networkMagic" ? u.integerFromBuffer(r[n]) == t : r[n] == t; })); } create(t) { return new vt(t); } add(t) { t instanceof vt || (t = new vt(t)), this.networks.push(t); } remove(t) { if (!(typeof t != "object" && (t = this.get(t), !t))) for (let e = 0; e < this.networks.length; e++) (this.networks[e] === t || JSON.stringify(this.networks[e]) == JSON.stringify(t)) && this.networks.splice(e, 1); } } const B = Rt.getInstance(); class l extends Ue { static Zero = new l(0); static One = new l(1); static Minus1 = new l(-1); static fromNumber(t) { return a.validateArgument(z(t), "num"), new l(t); } static fromBigInt(t) { return a.validateArgument(typeof t == "bigint", "num"), new l(t.toString()); } static fromString(t, e) { return a.validateArgument(c(t), "str"), new l(t, e); } static fromBuffer(t, e) { return a.validateArgument(u.isBuffer(t), "buf"), e?.endian === "little" && (t = u.reverse(t)), new l(u.bufferToHex(t), 16); } /** * Create a BN from a "ScriptNum": * This is analogous to the constructor for CScriptNum in nexad. Many ops in * nexad's script interpreter use CScriptNum, which is not really a proper * bignum. Instead, an error is thrown if trying to input a number bigger than * 4 bytes. We copy that behavior here. A third argument, `size`, is provided to * extend the hard limit of 4 bytes, as some usages require more than 4 bytes. */ static fromScriptNumBuffer(t, e, r) { let s = r || 4; if (a.validateArgument(t.length <= s, "script number overflow"), e && t.length > 0 && (t[t.length - 1] & 127) === 0 && (t.length <= 1 || (t[t.length - 2] & 128) === 0)) throw new Error("non-minimally encoded script number"); return l.fromSM(t, { endian: "little" }); } // Override arithmetic methods to ensure they return BNExtended instances add(t) { const e = super.add(t).toString(); return new l(e); } sub(t) { const e = super.sub(t).toString(); return new l(e); } mul(t) { const e = super.mul(t).toString(); return new l(e); } mod(t) { const e = super.mod(t).toString(); return new l(e); } umod(t) { const e = super.umod(t).toString(); return new l(e); } invm(t) { const e = super.invm(t).toString(); return new l(e); } neg() { const t = super.neg().toString(); return new l(t); } toNumber() { return parseInt(this.toString(10), 10); } toBigInt() { return BigInt(this.toString(10)); } toByteArray(t, e) { if (c(t)) return Uint8Array.from(super.toArray(t, e)); let r = this.toString(16, 2), s = u.hexToBuffer(r); if (t?.size) { let n = r.length / 2; n > t.size ? s = l.trim(s, n) : n < t.size && (s = l.pad(s, n, t.size)); } return t?.endian === "little" && (s = u.reverse(s)), s; } /** * The corollary to the above, with the notable exception that we do not throw * an error if the output is larger than four bytes. (Which can happen if * performing a numerical operation that results in an overflow to more than 4 * bytes). */ toScriptNumBuffer() { return this.toSM({ endian: "little" }); } toScriptBigNumBuffer() { return this.toSM({ endian: "little", bignum: !0 }); } getSize() { return (this.toString(2).replace("-", "").length + 1) / 8; } safeAdd(t, e) { const r = this.add(t); return this.checkOperationForOverflow(t, r, e), r; } safeSub(t, e) { const r = this.sub(t); return this.checkOperationForOverflow(t, r, e), r; } safeMul(t, e) { const r = this.mul(t); return this.checkOperationForOverflow(t, r, e), r; } checkOperationForOverflow(t, e, r) { if (this.getSize() > r || t.getSize() > r || e.getSize() > 8) throw new Error("overflow"); } toSMBigEndian() { let t; return this.cmp(l.Zero) === -1 ? (t = this.neg().toByteArray(), t[0] & 128 ? t = u.concat([Uint8Array.from([128]), t]) : t[0] = t[0] | 128) : (t = this.toByteArray(), t[0] & 128 && (t = u.concat([Uint8Array.from([0]), t]))), t.length === 1 && t[0] === 0 && (t = Uint8Array.from([])), t; } toBigNumSMBigEndian() { let t; return this.cmp(l.Zero) === -1 ? (t = this.neg().toByteArray(), t = u.concat([Uint8Array.from([128]), t])) : (t = this.toByteArray(), t = u.concat([Uint8Array.from([0]), t])), t; } toSM(t) { let e = t?.bignum ? this.toBigNumSMBigEndian() : this.toSMBigEndian(); return t?.endian === "little" && (e = u.reverse(e)), e; } /** * Instantiate a BigNumber from a "signed magnitude buffer" * (a buffer where the most significant bit represents the sign (0 = positive, -1 = negative)) */ static fromSM(t, e) { if (t.length === 0) return this.fromBuffer(Uint8Array.from([0])); e?.endian === "little" && (t = u.reverse(t)); let r; return t[0] & 128 ? (t[0] = t[0] & 127, r = this.fromBuffer(t), r.neg().copy(r)) : r = this.fromBuffer(t), r; } static trim(t, e) { return t.subarray(e - t.length, e); } static pad(t, e, r) { let s = new Uint8Array(r); for (let n = 0; n < t.length; n++) s[s.length - 1 - n] = t[t.length - 1 - n]; for (let n = 0; n < r - e; n++) s[n] = 0; return s; } } const Ce = He.ec; class S { static ec = new Ce("secp256k1").curve; ecPoint; static _g = new S(this.ec.g); constructor(t, e = !1) { this.ecPoint = t, e || this.validate(); } /** * Will return the X coordinate of the Point * * @returns A BN instance of the X coordinate */ getX() { return new l(this.ecPoint.getX().toArray()); } /** * Will return the Y coordinate of the Point * * @returns A BN instance of the Y coordinate */ getY() { return new l(this.ecPoint.getY().toArray()); } add(t) { return new S(this.ecPoint.add(t.ecPoint), !0); } mul(t) { let e = this.ecPoint.mul(t); return new S(e, !0); } mulAdd(t, e, r) { let s = this.ecPoint.mulAdd(t, e.ecPoint, r); return new S(s, !0); } eq(t) { return this.ecPoint.eq(t.ecPoint); } /** * Will determine if the point is valid * * @see {@link https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf} * @throws A validation error if exists * @returns An instance of the same Point */ validate() { if (this.ecPoint.isInfinity()) throw new Error("Point cannot be equal to Infinity"); let t; try { t = S.ec.pointFromX(this.getX(), this.getY().isOdd()); } catch { throw new Error("Point does not lie on the curve"); } if (t.y.cmp(this.ecPoint.y) !== 0) throw new Error("Invalid y value for curve."); if (!this.ecPoint.mul(S.getN()).isInfinity()) throw new Error("Point times N must be infinity"); return this; } hasSquare() { return !this.ecPoint.isInfinity() && S.isSquare(this.getY()); } static isSquare(t) { let e = new l("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", "hex"); return new l(t).toRed(l.red(e)).redPow(e.sub(l.One).div(new l(2))).fromRed().eq(new l(1)); } /** * Instantiate a valid secp256k1 Point from the X and Y coordinates. * * @param x - The X coordinate * @param y - The Y coordinate * @see {@link https://github.com/indutny/elliptic} * @throws A validation error if exists */ static ecPoint(t, e, r) { return new S(this.ec.point(t, e, r)); } /** * * Instantiate a valid secp256k1 Point from only the X coordinate * * @param odd - If the Y coordinate is odd * @param x - The X coordinate * @throws A validation error if exists */ static ecPointFromX(t, e) { let r; try { r = this.ec.pointFromX(e, t); } catch { throw new Error("Invalid X"); } return new S(r); } /** * * Will return a secp256k1 ECDSA base point. * * @see {@link https://en.bitcoin.it/wiki/Secp256k1} * @returns An instance of the base point. */ static getG() { return this._g; } /** * * Will return the max of range of valid private keys as governed by the secp256k1 ECDSA standard. * * @see {@link https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys} * @returns A BN instance of the number of points on the curve */ static getN() { return new l(this.ec.n.toArray()); } static pointToCompressed(t) { let e = t.getX().toByteArray({ size: 32 }), r = t.getY().toByteArray({ size: 32 }), s = r[r.length - 1] % 2, n = Uint8Array.from(s ? [3] : [2]); return u.concat([n, e]); } } function Le(i) { return i instanceof Uint8Array || ArrayBuffer.isView(i) && i.constructor.name === "Uint8Array"; } function he(i, t = "") { if (!Number.isSafeInteger(i) || i < 0) { const e = t && `"${t}" `; throw new Error(`${e}expected integer >= 0, got ${i}`); } } function Ut(i, t, e = "") { const r = Le(i), s = i?.length, n = t !== void 0; if (!r || n && s !== t) { const h = e && `"${e}" `, f = n ? ` of length ${t}` : "", o = r ? `length=${s}` : `type=${typeof i}`; throw new Error(h + "expected Uint8Array" + f + ", got " + o); } return i; } function Re(i) { if (typeof i != "function" || typeof i.create != "function") throw new Error("Hash must wrapped by utils.createHasher"); he(i.outputLen), he(i.blockLen); } function Ht(i, t = !0) { if (i.destroyed) throw new Error("Hash instance has been destroyed"); if (t && i.finished) throw new Error("Hash#digest() has already been called"); } function De(i, t) { Ut(i, void 0, "digestInto() output"); const e = t.outputLen; if (i.length < e) throw new Error('"digestInto() output" expected to be of length >=' + e); } function Q(...i) { for (let t = 0; t < i.length; t++) i[t].fill(0); } function Wt(i) { return new DataView(i.buffer, i.byteOffset, i.byteLength); } function J(i, t) { return i << 32 - t | i >>> t; } function yt(i, t) { return i << t | i >>> 32 - t >>> 0; } function Dt(i, t = {}) { const e = (s, n) => i(n).update(s).digest(), r = i(void 0); return e.outputLen = r.outputLen, e.blockLen = r.blockLen, e.create = (s) => i(s), Object.assign(e, t), Object.freeze(e); } const Pe = (i) => ({ oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, i]) }); function we(i, t, e) { return i & t ^ ~i & e; } function Se(i, t, e) { return i & t ^ i & e ^ t & e; } class Kt { blockLen; outputLen; padOffset; isLE; // For partial updates less than block size buffer; view; finished = !1; length = 0; pos = 0; destroyed = !1; constructor(t, e, r, s) { this.blockLen = t, this.outputLen = e, this.padOffset = r, this.isLE = s, this.buffer = new Uint8Array(t), this.view = Wt(this.buffer); } update(t) { Ht(this), Ut(t); const { view: e, buffer: r, blockLen: s } = this, n = t.length; for (let h = 0; h < n; ) { const f = Math.min(s - this.pos, n - h); if (f === s) { const o = Wt(t); for (; s <= n - h; h += s) this.process(o, h); continue; } r.set(t.subarray(h, h + f), this.pos), this.pos += f, h += f, this.pos === s && (this.process(e, 0), this.pos = 0); } return this.length += t.length, this.roundClean(), this; } digestInto(t) { Ht(this), De(t, this), this.finished = !0; const { buffer: e, view: r, blockLen: s, isLE: n } = this; let { pos: h } = this; e[h++] = 128, Q(this.buffer.subarray(h)), this.padOffset > s - h && (this.process(r, 0), h = 0); for (let p = h; p < s; p++) e[p] = 0; r.setBigUint64(s - 8, BigInt(this.length * 8), n), this.process(r, 0); const f = Wt(t), o = this.outputLen; if (o % 4) throw new Error("_sha2: outputLen must be aligned to 32bit"); const b = o / 4, P = this.get(); if (b > P.length) throw new Error("_sha2: outputLen bigger than state"); for (let p = 0; p < b; p++) f.setUint32(4 * p, P[p], n); } digest() { const { buffer: t, outputLen: e } = this; this.digestInto(t); const r = t.slice(0, e); return this.destroy(), r; } _cloneInto(t) { t ||= new this.constructor(), t.set(...this.get()); const { blockLen: e, buffer: r, length: s, finished: n, destroyed: h, pos: f } = this; return t.destroyed = h, t.finished = n, t.length = s, t.pos = f, s % e && t.buffer.set(r), t; } clone() { return this._cloneInto(); } } const nt = /* @__PURE__ */ Uint32Array.from([ 1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225 ]), V = /* @__PURE__ */ Uint32Array.from([ 1779033703, 4089235720, 3144134277, 2227873595, 1013904242, 4271175723, 2773480762, 1595750129, 1359893119, 2917565137, 2600822924, 725511199, 528734635, 4215389547, 1541459225, 327033209 ]), _t = /* @__PURE__ */ BigInt(2 ** 32 - 1), fe = /* @__PURE__ */ BigInt(32); function Ke(i, t = !1) { return t ? { h: Number(i & _t), l: Number(i >> fe & _t) } : { h: Number(i >> fe & _t) | 0, l: Number(i & _t) | 0 }; } function Ve(i, t = !1) { const e = i.length; let r = new Uint32Array(e), s = new Uint32Array(e); for (let n = 0; n < e; n++) { const { h, l: f } = Ke(i[n], t); [r[n], s[n]] = [h, f]; } return [r, s]; } const oe = (i, t, e) => i >>> e, le = (i, t, e) => i << 32 - e | t >>> e, At = (i, t, e) => i >>> e | t << 32 - e, Bt = (i, t, e) => i << 32 - e | t >>> e, Ot = (i, t, e) => i << 64 - e | t >>> e - 32, kt = (i, t, e) => i >>> e - 32 | t << 64 - e; function et(i, t, e, r) { const s = (t >>> 0) + (r >>> 0); return { h: i + e + (s / 2 ** 32 | 0) | 0, l: s | 0 }; } const Me = (i, t, e) => (i >>> 0) + (t >>> 0) + (e >>> 0), ze = (i, t, e, r) => t + e + r + (i / 2 ** 32 | 0) | 0, Ge = (i, t, e, r) => (i >>> 0) + (t >>> 0) + (e >>> 0) + (r >>> 0), Ye = (i, t, e, r, s) => t + e + r + s + (i / 2 ** 32 | 0) | 0, Xe = (i, t, e, r, s) => (i >>> 0) + (t >>> 0) + (e >>> 0) + (r >>> 0) + (s >>> 0), je = (i, t, e, r, s, n) => t + e + r + s + n + (i / 2 ** 32 | 0) | 0, We = /* @__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 ]), ut = /* @__PURE__ */ new Uint32Array(64); class $e extends Kt { constructor(t) { super(64, t, 8, !1); } get() { const { A: t, B: e, C: r, D: s, E: n, F: h, G: f, H: o } = this; return [t, e, r, s, n, h, f, o]; } // prettier-ignore set(t, e, r, s, n, h, f, o) { this.A = t | 0, this.B = e | 0, this.C = r | 0, this.D = s | 0, this.E = n | 0, this.F = h | 0, this.G = f | 0, this.H = o | 0; } process(t, e) { for (let p = 0; p < 16; p++, e += 4) ut[p] = t.getUint32(e, !1); for (let p = 16; p < 64; p++) { const v = ut[p - 15], A = ut[p - 2], F = J(v, 7) ^ J(v, 18) ^ v >>> 3, k = J(A, 17) ^ J(A, 19) ^ A >>> 10; ut[p] = k + ut[p - 7] + F + ut[p - 16] | 0; } let { A: r, B: s, C: n, D: h, E: f, F: o, G: b, H: P } = this; for (let p = 0; p < 64; p++) { const v = J(f, 6) ^ J(f, 11) ^ J(f, 25), A = P + v + we(f, o, b) + We[p] + ut[p] | 0, k = (J(r, 2) ^ J(r, 13) ^ J(r, 22)) + Se(r, s, n) | 0; P = b, b = o, o = f, f = h + A | 0, h = n, n = s, s = r, r = A + k | 0; } r = r + this.A | 0, s = s + this.B | 0, n = n + this.C | 0, h = h + this.D | 0, f = f + this.E | 0, o = o + this.F | 0, b = b + this.G | 0, P = P + this.H | 0, this.set(r, s, n, h, f, o, b, P); } roundClean() { Q(ut); } destroy() { this.set(0, 0, 0, 0, 0, 0, 0, 0), Q(this.buffer); } } class qe extends $e { // We cannot use array here since array allows indexing by variable // which means optimizer/compiler cannot use registers. A = nt[0] | 0; B = nt[1] | 0; C = nt[2] | 0; D = nt[3] | 0; E = nt[4] | 0; F = nt[5] | 0; G = nt[6] | 0; H = nt[7] | 0; constructor() { super(32); } } const Ae = Ve([ "0x428a2f98d728ae22", "0x7137449123ef65cd", "0xb5c0fbcfec4d3b2f", "0xe9b5dba58189dbbc", "0x3956c25bf348b538", "0x59f111f1b605d019", "0x923f82a4af194f9b", "0xab1c5ed5da6d8118", "0xd807aa98a3030242", "0x12835b0145706fbe", "0x243185be4ee4b28c", "0x550c7dc3d5ffb4e2", "0x72be5d74f27b896f", "0x80deb1fe3b1696b1", "0x9bdc06a725c71235", "0xc19bf174cf692694", "0xe49b69c19ef14ad2", "0xefbe4786384f25e3", "0x0fc19dc68b8cd5b5", "0x240ca1cc77ac9c65", "0x2de92c6f592b0275", "0x4a7484aa6ea6e483", "0x5cb0a9dcbd41fbd4", "0x76f988da831153b5", "0x983e5152ee66dfab", "0xa831c66d2db43210", "0xb00327c898fb213f", "0xbf597fc7beef0ee4", "0xc6e00bf33da88fc2", "0xd5a79147930aa725", "0x06ca6351e003826f", "0x142929670a0e6e70", "0x27b70a8546d22ffc", "0x2e1b21385c26c926", "0x4d2c6dfc5ac42aed", "0x53380d139d95b3df", "0x650a73548baf63de", "0x766a0abb3c77b2a8", "0x81c2c92e47edaee6", "0x92722c851482353b", "0xa2bfe8a14cf10364", "0xa81a664bbc423001", "0xc24b8b70d0f89791", "0xc76c51a30654be30", "0xd192e819d6ef5218", "0xd69906245565a910", "0xf40e35855771202a", "0x106aa07032bbd1b8", "0x19a4c116b8d2d0c8", "0x1e376c085141ab53", "0x2748774cdf8eeb99", "0x34b0bcb5e19b48a8", "0x391c0cb3c5c95a63", "0x4ed8aa4ae3418acb", "0x5b9cca4f7763e373", "0x682e6ff3d6b2b8a3", "0x748f82ee5defb2fc", "0x78a5636f43172f60", "0x84c87814a1f0ab72", "0x8cc702081a6439ec", "0x90befffa23631e28", "0xa4506cebde82bde9", "0xbef9a3f7b2c67915", "0xc67178f2e372532b", "0xca273eceea26619c", "0xd186b8c721c0c207", "0xeada7dd6cde0eb1e", "0xf57d4f7fee6ed178", "0x06f067aa72176fba", "0x0a637dc5a2c898a6", "0x113f9804bef90dae", "0x1b710b35131c471b", "0x28db77f523047d84", "0x32caab7b40c72493", "0x3c9ebe0a15c9bebc", "0x431d67c49c100d4c", "0x4cc5d4becb3e42b6", "0x597f299cfc657e2a", "0x5fcb6fab3ad6faec", "0x6c44198c4a475817" ].map((i) => BigInt(i))), Je = Ae[0], Qe = Ae[1], at = /* @__PURE__ */ new Uint32Array(80), ht = /* @__PURE__ */ new Uint32Array(80); class Ze extends Kt { constructor(t) { super(128, t, 16, !1); } // prettier-ignore get() { const { Ah: t, Al: e, Bh: r, Bl: s, Ch: n, Cl: h, Dh: f, Dl: o, Eh: b, El: P, Fh: p, Fl: v, Gh: A, Gl: F, Hh: k, Hl: X } = this; return [t, e, r, s, n, h, f, o, b, P, p, v, A, F, k, X]; } // prettier-ignore set(t, e, r, s, n, h, f, o, b, P, p, v, A, F, k, X) { this.Ah = t | 0, this.Al = e | 0, this.Bh = r | 0, this.Bl = s | 0, this.Ch = n | 0, this.Cl = h | 0, this.Dh = f | 0, this.Dl = o | 0, this.Eh = b | 0, this.El = P | 0, this.Fh = p | 0, this.Fl = v | 0, this.Gh = A | 0, this.Gl = F | 0, this.Hh = k | 0, this.Hl = X | 0; } process(t, e) { for (let O = 0; O < 16; O++, e += 4) at[O] = t.getUint32(e), ht[O] = t.getUint32(e += 4); for (let O = 16; O < 80; O++) { const q = at[O - 15] | 0, H = ht[O - 15] | 0, bt = At(q, H, 1) ^ At(q, H, 8) ^ oe(q, H, 7), Gt = Bt(q, H, 1) ^ Bt(q, H, 8) ^ le(q, H, 7), Z = at[O - 2] | 0, tt = ht[O - 2] | 0, It = At(Z, tt, 19) ^ Ot(Z, tt, 61) ^ oe(Z, tt, 6), Yt = Bt(Z, tt, 19) ^ kt(Z, tt, 61) ^ le(Z, tt, 6), xt = Ge(Gt, Yt, ht[O - 7], ht[O - 16]), Xt = Ye(xt, bt, It, at[O - 7], at[O - 16]); at[O] = Xt | 0, ht[O] = xt | 0; } let { Ah: r, Al: s, Bh: n, Bl: h, Ch: f, Cl: o, Dh: b, Dl: P, Eh: p, El: v, Fh: A, Fl: F, Gh: k, Gl: X, Hh: st, Hl: gt } = this; for (let O = 0; O < 80; O++) { const q = At(p, v, 14) ^ At(p, v, 18) ^ Ot(p, v, 41), H = Bt(p, v, 14) ^ Bt(p, v, 18) ^ kt(p, v, 41), bt = p & A ^ ~p & k, Gt = v & F ^ ~v & X, Z = Xe(gt, H, Gt, Qe[O], ht[O]), tt = je(Z, st, q, bt, Je[O], at[O]), It = Z | 0, Yt = At(r, s, 28) ^ Ot(r, s, 34) ^ Ot(r, s, 39), xt = Bt(r, s, 28) ^ kt(r, s, 34) ^ kt(r, s, 39), Xt = r & n ^ r & f ^ n & f, ke = s & h ^ s & o ^ h & o; st = k | 0, gt = X | 0, k = A | 0, X = F | 0, A = p | 0, F = v | 0, { h: p, l: v } = et(b | 0, P | 0, tt | 0, It | 0), b = f | 0, P = o | 0, f = n | 0, o = h | 0, n = r | 0, h = s | 0; const re = Me(It, xt, ke); r = ze(re, tt, Yt, Xt), s = re | 0; } ({ h: r, l: s } = et(this.Ah | 0, this.Al | 0, r | 0, s | 0)), { h: n, l: h } = et(this.Bh | 0, this.Bl | 0, n | 0, h | 0), { h: f, l: o } = et(this.Ch | 0, this.Cl | 0, f | 0, o | 0), { h: b, l: P } = et(this.Dh | 0, this.Dl | 0, b | 0, P | 0), { h: p, l: v } = et(this.Eh | 0, this.El | 0, p | 0, v | 0), { h: A, l: F } = et(this.Fh | 0, this.Fl | 0, A | 0, F | 0), { h: k, l: X } = et(this.Gh | 0, this.Gl | 0, k | 0, X | 0), { h: st, l: gt } = et(this.Hh | 0, this.Hl | 0, st | 0, gt | 0), this.set(r, s, n, h, f, o, b, P, p, v, A, F, k, X, st, gt); } roundClean() { Q(at, ht); } destroy() { Q(this.buffer), this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } } class tr extends Ze { Ah = V[0] | 0; Al = V[1] | 0; Bh = V[2] | 0; Bl = V[3] | 0; Ch = V[4] | 0; Cl = V[5] | 0; Dh = V[6] | 0; Dl = V[7] | 0; Eh = V[8] | 0; El = V[9] | 0; Fh = V[10] | 0; Fl = V[11] | 0; Gh = V[12] | 0; Gl = V[13] | 0; Hh = V[14] | 0; Hl = V[15] | 0; constructor() { super(64); } } const ce = /* @__PURE__ */ Dt( () => new qe(), /* @__PURE__ */ Pe(1) ), me = /* @__PURE__ */ Dt( () => new tr(), /* @__PURE__ */ Pe(3) ), Tt = /* @__PURE__ */ Uint32Array.from([ 1732584193, 4023233417, 2562383102, 271733878, 3285377520 ]), ft = /* @__PURE__ */ new Uint32Array(80); class er extends Kt { A = Tt[0] | 0; B = Tt[1] | 0; C = Tt[2] | 0; D = Tt[3] | 0; E = Tt[4] | 0; constructor() { super(64, 20, 8, !1); } get() { const { A: t, B: e, C: r, D: s, E: n } = this; return [t, e, r, s, n]; } set(t, e, r, s, n) { this.A = t | 0, this.B = e | 0, this.C = r | 0, this.D = s | 0, this.E = n | 0; } process(t, e) { for (let o = 0; o < 16; o++, e += 4) ft[o] = t.getUint32(e, !1); for (let o = 16; o < 80; o++) ft[o] = yt(ft[o - 3] ^ ft[o - 8] ^ ft[o - 14] ^ ft[o - 16], 1); let { A: r, B: s, C: n, D: h, E: f } = this; for (let o = 0; o < 80; o++) { let b, P; o < 20 ? (b = we(s, n, h), P = 1518500249) : o < 40 ? (b = s ^ n ^ h, P = 1859775393) : o < 60 ? (b = Se(s, n, h), P = 2400959708) : (b = s ^ n ^ h, P = 3395469782); const p = yt(r, 5) + b + f + P + ft[o] | 0; f = h, h = n, n = yt(s, 30), s = r, r = p; } r = r + this.A | 0, s = s + this.B | 0, n = n + this.C | 0, h = h + this.D | 0, f = f + this.E | 0, this.set(r, s, n, h, f); } roundClean() { Q(ft); } destroy() { this.set(0, 0, 0, 0, 0), Q(this.buffer); } } const rr = /* @__PURE__ */ Dt(() => new er()), ir = /* @__PURE__ */ Uint8Array.from([ 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8 ]), Be = Uint8Array.from(new Array(16).fill(0).map((i, t) => t)), sr = Be.map((i) => (9 * i + 5) % 16), ve = /* @__PURE__ */ (() => { const e = [[Be], [sr]]; for (let r = 0; r < 4; r++) for (let s of e) s.push(s[r].map((n) => ir[n])); return e; })(), Te = ve[0], Ee = ve[1], Ie = /* @__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((i) => Uint8Array.from(i)), nr = /* @__PURE__ */ Te.map((i, t) => i.map((e) => Ie[t][e])), ur = /* @__PURE__ */ Ee.map((i, t) => i.map((e) => Ie[t][e])), ar = /* @__PURE__ */ Uint32Array.from([ 0, 1518500249, 1859775393, 2400959708, 2840853838 ]), hr = /* @__PURE__ */ Uint32Array.from([ 1352829926, 1548603684, 1836072691, 2053994217, 0 ]); function de(i, t, e, r) { return i === 0 ? t ^ e ^ r : i === 1 ? t & e | ~t & r : i === 2 ? (t | ~e) ^ r : i === 3 ? t & r | e & ~r : t ^ (e | ~r); } const Nt = /* @__PURE__ */ new Uint32Array(16); class fr extends Kt { h0 = 1732584193; h1 = -271733879; h2 = -1732584194; h3 = 271733878; h4 = -1009589776; constructor() { super(64, 20, 8, !0); } get() { const { h0: t, h1: e, h2: r, h3: s, h4: n } = this; return [t, e, r, s, n]; } set(t, e, r, s, n) { this.h0 = t | 0, this.h1 = e | 0, this.h2 = r | 0, this.h3 = s | 0, this.h4 = n | 0; } process(t, e) { for (let A = 0; A < 16; A++, e += 4) Nt[A] = t.getUint32(e, !0); let r = this.h0 | 0, s = r, n = this.h1 | 0, h = n, f = this.h2 | 0, o = f, b = this.h3 | 0, P = b, p = this.h4 | 0, v = p; for (let A = 0; A < 5; A++) { const F = 4 - A, k = ar[A], X = hr[A], st = Te[A], gt = Ee[A], O = nr[A], q = ur[A]; for (let H = 0; H < 16; H++) { const bt = yt(r + de(A, n, f, b) + Nt[st[H]] + k, O[H]) + p | 0; r = p, p = b, b = yt(f, 10) | 0, f = n, n = bt; } for (let H = 0; H < 16; H++) { const bt = yt(s + de(F, h, o, P) + Nt[gt[H]] + X, q[H]) + v | 0; s = v, v = P, P = yt(o, 10) | 0, o = h, h = bt; } } this.set(this.h1 + f + P | 0, this.h2 + b + v | 0, this.h3 + p + s | 0, this.h4 + r + h | 0, this.h0 + n + o | 0); } roundClean() { Q(Nt); } destroy() { this.destroyed = !0, Q(this.buffer), this.set(0, 0, 0, 0, 0); } } const or = /* @__PURE__ */ Dt(() => new fr()); class xe { oHash; iHash; blockLen; outputLen; finished = !1; destroyed = !1; constructor(t, e) { if (Re(t), Ut(e, 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, s = new Uint8Array(r); s.set(e.length > r ? t.create().update(e).digest() : e); for (let n = 0; n < s.length; n++) s[n] ^= 54; this.iHash.update(s), this.oHash = t.create(); for (let n = 0; n < s.length; n++) s[n] ^= 106; this.oHash.update(s), Q(s); } update(t) { return Ht(this), this.iHash.update(t), this; } digestInto(t) { Ht(this), Ut(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: e, iHash: r, finished: s, destroyed: n, blockLen: h, outputLen: f } = this; return t = t, t.finished = s, t.destroyed = n, t.blockLen = h, t.outputLen = f, t.oHash = e._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 Jt = (i, t, e) => new xe(i, t).update(e).digest(); Jt.create = (i, t) => new xe(i, t); class w { static sha1(t) { return a.validateArgument(u.isBuffer(t), "buf", "Must be Buffer"), rr(t); } static sha256(t) { return a.validateArgument(u.isBuffer(t), "buf", "Must be Buffer"), ce(t); } static sha512(t) { return a.validateArgument(u.isBuffer(t), "buf", "Must be Buffer"), me(t); } static ripemd160(t) { return a.validateArgument(u.isBuffer(t), "buf", "Must be Buffer"), or(t); } static sha256sha256(t) { return a.validateArgument(u.isBuffer(t), "buf", "Must be Buffer"), this.sha256(this.sha256(t)); } static sha256ripemd160(t) { return a.validateArgument(u.isBuffer(t), "buf", "Must be Buffer"), this.ripemd160(this.sha256(t)); } static sha256hmac(t, e) { return Jt(ce, e, t); } static sha512hmac(t, e) { return Jt(me, e, t); } } class G { r; s; i; compressed; constructor(t) { this.r = t.r, this.s = t.s, this.i = t.i, this.compressed = t.compressed; } toBuffer(t = !0) { if (t) return u.concat([this.r.toByteArray({ size: 32 }), this.s.toByteArray({ size: 32 })]); let e = this.r.toByteArray(), r = this.s.toByteArray(), s = !!(e[0] & 128), n = !!(r[0] & 128), h = s ? u.concat([Uint8Array.from([0]), e]) : e, f = n ? u.concat([Uint8Array.from([0]), r]) : r, o = h.length, b = f.length, P = 2 + o + 2 + b; return u.concat([Uint8Array.from([48, P, 2, o]), h, Uint8Array.from([2, b]), f]); } toTxFormat(t) { let e = this.toBuffer(); return u.isBuffer(t) ? u.concat([e, t]) : e; } toString(t = !0) { return u.bufferToHex(this.toBuffer(t)); } /** * Schnorr signatures are 64 bytes: r [len] 32 || s [len] 32. * * There can be a few more bytes that is the sighashtype. It needs to be trimmed before calling this. */ static fromBuffer(t, e) { if (t.length === 64) { let s = this.parseSchnorrEncodedSig(t); return new G(s); } let r = G.parseDER(t, e); return new G({ r: r.r, s: r.s }); } /** * The format used in a tx. * schnorr is 64 bytes, the rest are sighashtype bytes * * @param buf */ static fromTxFormat(t) { let e = t.subarray(0, 64); return G.fromBuffer(e); } /** * This assumes the str is a raw signature and does not have sighashtype. * Use {@link Signature.fromTxString} when decoding a tx * * @param str the signature hex string * @see fromTxString */ static fromString(t) { let e = u.hexToBuffer(t); return G.fromBuffer(e); } /** * This assumes the str might have sighashtype bytes and will trim it if needed. * Use this when decoding a tx signature string * * @param str the tx signature hex string */ static fromTxString(t) { return G.fromTxFormat(u.hexToBuffer(t)); } static parseSchnorrEncodedSig(t) { let e = t.subarray(0, 32), r = t.subarray(32, 64); return { r: l.fromBuffer(e), s: l.fromBuffer(r) }; } /** * For ECDSA. In order to mimic the non-strict DER encoding of OpenSSL, set strict = false. */ static parseDER(t, e) { a.validateArgument(u.isBuffer(t), "DER formatted signature should be a buffer"), y(e) && (e = !0); let r = t[0]; a.validateArgument(r === 48, "Header byte should be 0x30"); let s = t[1], n = t.subarray(2).length; a.validateArgument(!e || s === n, "Length byte should length of what follows"), s = s < n ? s : n; let h = t[2]; a.validateArgument(h === 2, "Integer byte for r should be 0x02"); let f = t[3], o = t.subarray(4, 4 + f), b = l.fromBuffer(o), P = t[4] === 0; a.validateArgument(f === o.length, "Length of r incorrect"); let p = t[4 + f + 0]; a.validateArgument(p === 2, "Integer byte for s should be 0x02"); let v = t[4 + f + 1], A = t.subarray(4 + f + 2, 4 + f + 2 + v), F = l.fromBuffer(A), k = t[4 + f + 2 + 2] === 0; a.validateArgument(v === A.length, "Length of s incorrect"); let X = 4 + f + 2 + v; return a.validateArgument(s === X - 2, "Length of signature incorrect"), { header: r, length: s, rheader: h, rlength: f, rneg: P, rbuf: o, r: b, sheader: p, slength: v, sneg: k, sbuf: A, s: F }; } /** * ECDSA format. used for sign messages */ toCompact(t, e) { t = typeof t == "number" ? t : this.i, e = typeof e == "boolean" ? e : this.compressed, a.validateArgument(t === 0 || t === 1 || t === 2 || t === 3, "i must be equal to 0, 1, 2, or 3"); let r = t + 27 + 4; e === !1 && (r = r - 4); let s = Uint8Array.from([r]), n = this.r.toByteArray({ size: 32 }), h = this.s.toByteArray({ size: 32 }); return u.concat([s, n, h]); } static fromCompact(t) { a.validateArgument(u.isBuffer(t), "Argument is expected to be a Buffer"); let e = !0, r = t.subarray(0, 1)[0] - 27 - 4; r < 0 && (e = !1, r = r + 4); let s = t.subarray(1, 33), n = t.subarray(33, 65); return a.validateArgument(r === 0 || r === 1 || r === 2 || r === 3, "i must be 0, 1, 2, or 3"), a.validateArgument(s.length === 32, "r must be 32 bytes"), a.validateArgument(n.length === 32, "s must be 32 bytes"), new G({ r: l.fromBuffer(s), s: l.fromBuffer(n), i: r, compressed: e }); } } class _e { hashbuf; endian; privkey; pubkey; sig; verified; constructor(t) { t && this.set(t); } set(t) { return this.hashbuf = t.hashbuf || this.hashbuf, this.endian = t.endian || this.endian, this.privkey = t.privkey || this.privkey, this.pubkey = t.pubkey || (this.privkey ? this.privkey.publicKey : this.pubkey), this.sig = t.sig || this.sig, this.verified = t.verified || this.verified, this; } sign() { let t = this.hashbuf, e = this.privkey, r = e.bn; a.validateState(!_(t) && !_(e) && !_(r), "invalid parameters"), a.validateState(u.isBuffer(t) && t.length === 32, "hashbuf must be a 32 byte buffer"); let s = l.fromBuffer(t, this.endian ? { endian: this.endian } : void 0), n = this._findSignature(r, s); return n.compressed = this.pubkey.compressed, this.sig = new G(n), this; } verify() { return this.verified = !this.sigError(), this; } toPublicKey() { return this.privkey.toPublicKey(); } privkey2pubkey() { this.pubkey = this.privkey.toPublicKey(); } } class I { point; compressed; network; /** * @param data - The pubkey data */ constructor(t) { if (a.validateArgument(!_(t), "First argument is required, please include public key data."), t instanceof I) return t; if (I._isPublicKeyData(t)) t.point.validate(), this.point = t.point, this.compressed = y(t.compressed) || t.compressed, this.network = t.network || B.defaultNetwork; else throw new TypeError("First argument is an unrecognized data format."); } toObject = this.toJSON; toDER = this.toBuffer; /** * @returns A plain object of the PublicKey */ toJSON() { return { x: this.point.getX().toString("hex", 2), y: this.point.getY().toString("hex", 2), compressed: this.compressed, network: this.network.toString() }; } /** * Will output the PublicKey to a DER Uint8Array * * @returns A DER encoded buffer */ toBuffer() { let t = this.point.getX(), e = this.point.getY(), r = t.toByteArray({ size: 32 }), s = e.toByteArray({ size: 32 }), n; return this.compressed ? (s[s.length - 1] % 2 ? n = Uint8Array.from([3]) : n = Uint8Array.from([2]), u.concat([n, r])) : (n = Uint8Array.from([4]), u.concat([n, r, s])); } /** * Will output the PublicKey to a DER encoded hex string * * @returns A DER hex encoded string */ toString() { return u.bufferToHex(this.toBuffer()); } /** * Will return a string formatted for the console * * @returns Public key string inspection */ inspect() { return "<PublicKey: " + this.toString() + (this.compressed ? "" : ", uncompressed") + ">"; } /** * Instantiate a PublicKey from various formats * * @param data The encoded data in various formats * @param compressed If the public key is compressed * @param network The key network * @returns New PublicKey instance */ static from(t, e, r) { if (t instanceof I) return t; if (t instanceof S) return this.fromPoint(t, e, r); if (this._isPublicKeyDto(t)) return this.fromObject(t); if (this._isPublicKeyData(t)) return new I(t); if (this._isPrivateKeyData(t)) return this.fromPrivateKey(t); if (u.isBuffer(t)) return this.fromBuffer(t, !0, r); if (R.isHexa(t)) return this.fromString(t, r); throw new TypeError("First argument is an unrecognized data format."); } static fromDER = this.fromBuffer; static fromObject = this.fromJSON; /** * Instantiate a PublicKey from a Uint8Array * * @param buf - A DER hex buffer * @param strict - if set to false, will loosen some conditions * @param network - the network of the key * @returns A new valid instance of PublicKey */ static fromBuffer(t, e, r) { a.validateArgument(u.isBuffer(t), "Must be a hex buffer of DER encoded public key"); let s = I._transformDER(t, e); return new I({ point: s.point, compressed: s.compressed, network: r }); } /** * Instantiate a PublicKey from a Point * * @param point - A Point instance * @param compressed - whether to store this public key as compressed format * @param network - the network of the key * @returns A new valid instance of PublicKey */ static fromPoint(t, e, r) { return a.validateArgument(t instanceof S, "First argument must be an instance of Point."), new I({ point: t, compressed: e, network: r }); } /** * Instantiate a PublicKey from a DER hex encoded string * * @param str - A DER hex string * @param network - the network of the key * @returns A new valid instance of PublicKey */ static fromString(t, e) { let r = u.hexToBuffer(t), s = I._transformDER(r); return new I({ point: s.point, compressed: s.compressed, network: e }); } /** * Instantiate a PublicKey from PrivateKey data * * @param data - Object contains data of PrivateKey * @returns A new valid instance of PublicKey */ static fromPrivateKey(t) { a.validateArgument(this._isPrivateKeyData(t), "data", "Must be data of PrivateKey"); let e = S.getG().mul(t.bn); return new I({ point: e, compressed: t.compressed, network: t.network }); } static fromJSON(t) { let e = I._transformObject(t); return new I(e); } /** * Check if there would be any errors when initializing a PublicKey * * @param data - The encoded data in various formats * @returns An error if exists */ static getValidationError(t) { try { this.from(t); } catch (e) { return e; } } /** * Check if the parameters are valid * * @param data - The encoded data in various formats * @returns true If the public key would be valid */ static isValid(t) { return !I.getValidationError(t); } static _isPublicKeyData(t) { return T(t) && "point" in t && t.point instanceof S; } static _isPublicKeyDto(t) { return T(t) && "x" in t && "y" in t; } static _isPrivateKeyData(t) { return T(t) && "bn" in t && "network" in t; } /** * Internal function to transform DER into a public key point * * @param buf - An hex encoded buffer * @param strict - if set to false, will loosen some conditions * @returns An object with keys: point and compressed */ static _transformDER(t, e) { if (a.validateArgument(u.isBuffer(t), "Must be a hex buffer of DER encoded public key"), e = y(e) ? !0 : e, t[0] === 4 || !e && (t[0] === 6 || t[0] === 7)) { let r = t.subarray(1, 33), s = t.subarray(33, 65); if (r.length !== 32 || s.length !== 32 || t.length !== 65) throw new TypeError("Length of x and y must be 32 bytes"); let n = new l(r), h = new l(s); return { point: S.ecPoint(n, h), compressed: !1 }; } else if (t[0] === 3) { let r = t.subarray(1), s = new l(r); return { point: S.ecPointFromX(!0, s), compressed: !0 }; } else if (t[0] === 2) { let r = t.subarray(1), s = new l(r); return { point: S.ecPointFromX(!1, s), compressed: !0 }; } else throw new TypeError("Invalid DER format public key"); } /** * Internal function to transform a JSON into a public key point */ static _transformObject(t) { a.validateArgument(c(t.x), "x", "must be a hex string"), a.validateArgument(c(t.y), "y", "must be a hex string"); let e = new l(t.x, "hex"), r = new l(t.y, "hex"); return { point: S.ecPoint(e, r), compressed: t.compressed, network: B.get(t.network) }; } } class ct extends _e { k; set(t) { return this.k = t.k || this.k, super.set(t); } sigError() { if (!u.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) return "hashbuf must be a 32 byte buffer"; let t = this.sig.r, e = this.sig.s; if (!(t.gt(l.Zero) && t.lt(S.getN())) || !(e.gt(l.Zero) && e.lt(S.getN()))) return "r and s not in range"; let r = l.fromBuffer(this.hashbuf, this.endian ? { endian: this.endian } : void 0), s = S.getN(), n = e.invm(s), h = n.mul(r).umod(s), f = n.mul(t).umod(s), o = S.getG().mulAdd(new l(h), this.pubkey.point, new l(f)); return o.ecPoint.isInfinity() ? "p is infinity" : o.getX().umod(s).cmp(t) !== 0 ? "Inval