noble-curves-extended
Version:
This project extends @noble/curves to allow randomBytes to be specified externally
1,539 lines • 102 kB
JavaScript
import { numberToBytesLE as Qr, bytesToNumberLE as Qe, ensureBytes as je } from "@noble/curves/abstract/utils";
import { randomBytes as jr, concatBytes as en } from "@noble/hashes/utils";
import { mod as I, Field as Z, getMinHashLength as tn, mapHashToField as rn, pow2 as v, isNegativeLE as nn } from "@noble/curves/abstract/modular";
import { sha512 as ct, sha384 as sn } from "@noble/hashes/sha512";
import { twistedEdwards as on } from "@noble/curves/abstract/edwards";
import { hmac as fn } from "@noble/hashes/hmac";
import { weierstrass as cn } from "@noble/curves/abstract/weierstrass";
import { sha256 as at } from "@noble/hashes/sha256";
import { compareUint8Arrays as oe, isUint8Array as fe, ensureUint8Array as ce, encodeBase64Url as D, decodeBase64Url as $, decodeHex as an } from "u8a-utils";
const ln = /* @__PURE__ */ BigInt(0), Ie = (i) => typeof i == "bigint" && ln <= i;
function un(i, e, t) {
return Ie(i) && Ie(e) && Ie(t) && e <= i && i < t;
}
function et(i, e, t, r) {
if (!un(e, t, r))
throw new Error(
"expected valid " + i + ": " + t + " <= n < " + r + ", got " + e
);
}
function hn(i, e, t = {}) {
if (!i || typeof i != "object")
throw new Error("expected valid options object");
function r(n, s, o) {
const f = i[n];
if (o && f === void 0) return;
const c = typeof f;
if (c !== s || f === null)
throw new Error(
`param "${n}" is invalid: expected ${s}, got ${c}`
);
}
Object.entries(e).forEach(([n, s]) => r(n, s, !1)), Object.entries(t).forEach(([n, s]) => r(n, s, !0));
}
const re = BigInt(0), W = BigInt(1), he = BigInt(2);
function dn(i) {
return hn(i, {
adjustScalarBytes: "function",
powPminus2: "function"
}), Object.freeze({ ...i });
}
function gn(i) {
const e = dn(i), { P: t, type: r, adjustScalarBytes: n, powPminus2: s, randomBytes: o } = e, f = r === "x25519";
if (!f && r !== "x448") throw new Error("invalid type");
const c = o || jr, u = f ? 255 : 448, h = f ? 32 : 56, a = BigInt(f ? 9 : 5), d = BigInt(f ? 121665 : 39081), B = f ? he ** BigInt(254) : he ** BigInt(447), p = f ? BigInt(8) * he ** BigInt(251) - W : BigInt(4) * he ** BigInt(445) - W, F = B + p + W, b = (k) => I(k, t), j = ae(a);
function ae(k) {
return Qr(b(k), h);
}
function me(k) {
const E = je("u coordinate", k, h);
return f && (E[31] &= 127), b(Qe(E));
}
function Ee(k) {
return Qe(
n(je("scalar", k, h))
);
}
function ee(k, E) {
const U = Xr(me(E), Ee(k));
if (U === re) throw new Error("invalid private or public key received");
return ae(U);
}
function te(k) {
return ee(k, j);
}
function le(k, E, U) {
const ue = b(k * (E - U));
return E = b(E - ue), U = b(U + ue), { x_2: E, x_3: U };
}
function Xr(k, E) {
et("u", k, re, t), et("scalar", E, B, F);
const U = E, ue = k;
let T = W, H = re, M = k, O = W, K = re;
for (let xe = BigInt(u - 1); xe >= re; xe--) {
const Ke = U >> xe & W;
K ^= Ke, { x_2: T, x_3: M } = le(K, T, M), { x_2: H, x_3: O } = le(K, H, O), K = Ke;
const pe = T + H, Se = b(pe * pe), Ae = T - H, We = b(Ae * Ae), qe = Se - We, zr = M + O, Yr = M - O, Xe = b(Yr * pe), Ze = b(zr * Ae), ze = Xe + Ze, Ye = Xe - Ze;
M = b(ze * ze), O = b(ue * b(Ye * Ye)), T = b(Se * We), H = b(qe * (Se + b(d * qe)));
}
({ x_2: T, x_3: M } = le(K, T, M)), { x_2: H, x_3: O } = le(K, H, O);
const Zr = s(H);
return b(T * Zr);
}
return {
CURVE: e,
scalarMult: ee,
scalarMultBase: te,
getSharedSecret: (k, E) => ee(k, E),
getPublicKey: (k) => te(k),
utils: { randomPrivateKey: () => c(h) },
GuBytes: j.slice()
};
}
const tt = Z(
BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
), Ci = (i) => ({
utils: {
randomPrivateKey: () => {
const t = tn(tt.ORDER);
return rn(i(t), tt.ORDER);
}
}
}), wn = BigInt(1), rt = BigInt(2), Bn = BigInt(5), vn = BigInt(8), z = {
p: BigInt(
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
),
n: BigInt(
"0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"
),
h: vn,
a: BigInt(
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"
),
d: BigInt(
"0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"
),
Gx: BigInt(
"0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"
),
Gy: BigInt(
"0x6666666666666666666666666666666666666666666666666666666666666658"
)
};
function lt(i) {
const e = BigInt(10), t = BigInt(20), r = BigInt(40), n = BigInt(80), s = z.p, f = i * i % s * i % s, c = v(f, rt, s) * f % s, u = v(c, wn, s) * i % s, h = v(u, Bn, s) * u % s, a = v(h, e, s) * h % s, d = v(a, t, s) * a % s, B = v(d, r, s) * d % s, p = v(B, n, s) * B % s, F = v(p, n, s) * B % s, b = v(F, e, s) * h % s;
return { pow_p_5_8: v(b, rt, s) * i % s, b2: f };
}
function ut(i) {
return i[0] &= 248, i[31] &= 127, i[31] |= 64, i;
}
const nt = /* @__PURE__ */ BigInt(
"19681161376707505956807079304988542015446066515923890162744021073123829784752"
);
function yn(i, e) {
const t = z.p, r = I(e * e * e, t), n = I(r * r * e, t), s = lt(i * n).pow_p_5_8;
let o = I(i * r * s, t);
const f = I(e * o * o, t), c = o, u = I(o * nt, t), h = f === i, a = f === I(-i, t), d = f === I(-i * nt, t);
return h && (o = c), (a || d) && (o = u), nn(o, t) && (o = I(-o, t)), { isValid: h || a, value: o };
}
const bn = Z(z.p, void 0, !0), kn = {
...z,
Fp: bn,
hash: ct,
adjustScalarBytes: ut,
// dom2
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
// Constant-time, u/√v
uvRatio: yn
}, mn = (i) => on({
...kn,
randomBytes: i
}), ht = (i) => {
switch (i.CURVE.p) {
case z.p:
return "Ed25519";
default:
throw new Error("Unknown curve");
}
}, Ri = ["Ed25519"], we = (i) => {
switch (i.GuBytes.length) {
case 32:
return "X25519";
default:
throw new Error("Unknown curve");
}
}, En = BigInt(3), xn = (i) => /* @__PURE__ */ (() => {
const e = z.p;
return gn({
P: e,
type: "x25519",
powPminus2: (t) => {
const { pow_p_5_8: r, b2: n } = lt(t);
return I(v(r, En, e) * n, e);
},
adjustScalarBytes: ut,
randomBytes: i
});
})(), Ti = ["X25519"], pn = (i) => (e, ...t) => fn(i, e, en(...t));
function Be(i, e, t) {
return cn({
...i,
hash: e,
randomBytes: t,
hmac: pn(e)
});
}
const dt = (i, e) => {
if (e.length == i.CURVE.nByteLength * 2)
return i.Signature.fromCompact(e);
if (e.length == i.CURVE.nByteLength * 2 + 1) {
const t = e[e.length - 1], r = e.slice(0, -1);
return i.Signature.fromCompact(r).addRecoveryBit(t);
}
throw new Error("Invalid raw signature");
}, ve = {
p: BigInt(
"0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff"
),
n: BigInt(
"0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"
),
h: BigInt(1),
a: BigInt(
"0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc"
),
b: BigInt(
"0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"
),
Gx: BigInt(
"0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"
),
Gy: BigInt(
"0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"
)
}, Sn = Z(ve.p), gt = (i) => Be(
{ ...ve, Fp: Sn, lowS: !1 },
at,
i
), ye = {
p: BigInt(
"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"
),
n: BigInt(
"0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973"
),
h: BigInt(1),
a: BigInt(
"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc"
),
b: BigInt(
"0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef"
),
Gx: BigInt(
"0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7"
),
Gy: BigInt(
"0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f"
)
}, An = Z(ye.p), wt = (i) => Be(
{ ...ye, Fp: An, lowS: !1 },
sn,
i
), be = {
p: BigInt(
"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
),
n: BigInt(
"0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409"
),
h: BigInt(1),
a: BigInt(
"0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc"
),
b: BigInt(
"0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00"
),
Gx: BigInt(
"0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66"
),
Gy: BigInt(
"0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650"
)
}, In = Z(be.p), Bt = (i) => Be(
{
...be,
Fp: In,
lowS: !1,
allowedPrivateKeyLengths: [130, 131, 132]
},
ct,
i
), q = {
p: BigInt(
"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"
),
n: BigInt(
"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"
),
h: BigInt(1),
a: BigInt(0),
b: BigInt(7),
Gx: BigInt(
"0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
),
Gy: BigInt(
"0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
)
}, Vn = BigInt(1), Ne = BigInt(2), it = (i, e) => (i + e / Ne) / e;
function Nn(i) {
const e = q.p, t = BigInt(3), r = BigInt(6), n = BigInt(11), s = BigInt(22), o = BigInt(23), f = BigInt(44), c = BigInt(88), u = i * i * i % e, h = u * u * i % e, a = v(h, t, e) * h % e, d = v(a, t, e) * h % e, B = v(d, Ne, e) * u % e, p = v(B, n, e) * B % e, F = v(p, s, e) * p % e, b = v(F, f, e) * F % e, j = v(b, c, e) * b % e, ae = v(j, f, e) * F % e, me = v(ae, t, e) * h % e, Ee = v(me, o, e) * p % e, ee = v(Ee, r, e) * u % e, te = v(ee, Ne, e);
if (!Ue.eql(Ue.sqr(te), i)) throw new Error("Cannot find square root");
return te;
}
const Ue = Z(q.p, void 0, void 0, { sqrt: Nn }), vt = (i) => Be(
{
...q,
Fp: Ue,
lowS: !0,
// Allow only low-S signatures by default in sign() and verify()
endo: {
// Endomorphism, see above
beta: BigInt(
"0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"
),
splitScalar: (e) => {
const t = q.n, r = BigInt("0x3086d221a7d46bcde86c90e49284eb15"), n = -Vn * BigInt("0xe4437ed6010e88286f547fa90abfe4c3"), s = BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), o = r, f = BigInt("0x100000000000000000000000000000000"), c = it(o * e, t), u = it(-n * e, t);
let h = I(e - c * r - u * s, t), a = I(-c * n - u * o, t);
const d = h > f, B = a > f;
if (d && (h = t - h), B && (a = t - a), h > f || a > f)
throw new Error("splitScalar: Endomorphism failed, k=" + e);
return { k1neg: d, k1: h, k2neg: B, k2: a };
}
}
},
at,
i
), He = (i) => {
switch (i.CURVE.p) {
case ve.p:
return "P-256";
case ye.p:
return "P-384";
case be.p:
return "P-521";
case q.p:
return "secp256k1";
default:
throw new Error("Unknown curve");
}
}, Le = (i) => {
switch (i.CURVE.p) {
case ve.p:
return "ES256";
case ye.p:
return "ES384";
case be.p:
return "ES512";
case q.p:
return "ES256K";
default:
throw new Error("Unknown curve");
}
}, Un = (i) => {
if (!i || i.recovery === void 0 || i.recovery === null)
throw new Error("Invalid signature");
const e = i.toCompactRawBytes(), t = new Uint8Array(e.length + 1);
return t.set(e), t[t.length - 1] = i.recovery, t;
}, Hi = [
"P-256",
"P-384",
"P-521",
"secp256k1"
], Li = [
"ES256",
"ES384",
"ES512",
"ES256K"
], w = (i) => i instanceof Error ? i.message : String(i), Cn = (i) => {
try {
return i.utils.randomPrivateKey();
} catch (e) {
throw console.log(w(e)), new Error("Failed to generate random private key");
}
}, Rn = (i, e, t = !0) => {
if (!t)
throw new Error("Uncompressed public key is not supported");
try {
const r = i.CURVE.nByteLength;
if (e.length === r * 2) {
const n = e.slice(0, r), s = e.slice(r), o = i.getPublicKey(n);
if (oe(o, s))
return o;
throw new Error("Embedded public key is invalid");
}
return i.getPublicKey(e);
} catch (r) {
throw console.log(w(r)), new Error("Failed to get public key");
}
}, Tn = (i, { message: e, privateKey: t, recovered: r = !1 }) => {
if (r)
throw new Error("Recovered signature is not supported");
try {
const n = fe(e) ? ce(e) : e, s = i.CURVE.nByteLength, o = t.length === s * 2 ? t.slice(0, s) : t;
return i.sign(n, o);
} catch (n) {
throw console.log(w(n)), new Error("Failed to sign message");
}
}, Hn = (i, { signature: e, message: t, publicKey: r }) => {
try {
const n = fe(t) ? ce(t) : t;
return i.verify(e, n, r);
} catch (n) {
return console.log(w(n)), !1;
}
}, yt = (i, e) => {
try {
const t = i.CURVE.nByteLength;
if (e.length !== t)
throw new Error("Invalid public key length");
return {
kty: "OKP",
crv: "Ed25519",
alg: "EdDSA",
x: D(e)
};
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert public key to JWK");
}
}, Ln = (i, e) => {
try {
const t = i.getPublicKey(e);
return {
...yt(i, t),
d: D(e)
};
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert private key to JWK");
}
}, _n = (i, e) => {
try {
return bt(i, e);
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert JWK to raw public key");
}
}, bt = (i, e) => {
if (e.kty === void 0 || e.kty === null)
throw new Error("Missing required parameter for kty");
if (e.kty !== "OKP")
throw new Error(`Invalid key type: ${e.kty}, expected OKP`);
if (e.crv === void 0 || e.crv === null)
throw new Error("Missing required parameter for crv");
const t = ht(i);
if (e.crv !== t)
throw new Error(
`Invalid curve: ${e.crv}, expected ${t}`
);
if (e.x === void 0 || e.x === null)
throw new Error("Missing required parameter for x");
if (typeof e.x != "string")
throw new Error("Invalid parameter type for x");
if (e.alg !== void 0 && e.alg !== null && e.alg !== "EdDSA")
throw new Error(`Invalid algorithm: ${e.alg}, expected EdDSA`);
let r;
try {
r = $(e.x);
} catch {
throw new Error("Malformed encoding for x");
}
if (r.length !== i.CURVE.nByteLength)
throw new Error(
`Invalid the length of the key data for x: ${r.length}, expected ${i.CURVE.nByteLength}`
);
return r;
}, Fn = (i, e) => {
try {
return Mn(i, e);
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert JWK to raw private key");
}
}, Mn = (i, e) => {
const t = bt(i, e);
if (e.d === void 0 || e.d === null)
throw new Error("Missing required parameter for d");
if (typeof e.d != "string")
throw new Error("Invalid parameter type for d");
let r;
try {
r = $(e.d);
} catch {
throw new Error("Malformed encoding for d");
}
if (r.length !== i.CURVE.nByteLength)
throw new Error(
`Invalid the length of the key data for d: ${r.length}, expected ${i.CURVE.nByteLength}`
);
if (!oe(i.getPublicKey(r), t))
throw new Error(
"The public key derived from the private key does not match the public key in the JWK"
);
return r;
};
class On {
/**
* Creates a new Edwards instance.
*
* @param {CurveFn} curve - The curve implementation to use
* @param {RandomBytes} randomBytes - Function to generate random bytes
*/
constructor(e, t) {
this.randomPrivateKey = () => Cn(this.curve), this.getPublicKey = (r, n = !0) => Rn(this.curve, r, n), this.sign = ({
message: r,
privateKey: n,
recovered: s = !1
}) => Tn(this.curve, { message: r, privateKey: n, recovered: s }), this.verify = ({ signature: r, message: n, publicKey: s }) => Hn(this.curve, { signature: r, message: n, publicKey: s }), this.recoverPublicKey = (r) => {
throw new Error("Public key recovery is not supported");
}, this.toJwkPrivateKey = (r) => Ln(this.curve, r), this.toJwkPublicKey = (r) => yt(this.curve, r), this.toRawPrivateKey = (r) => Fn(this.curve, r), this.toRawPublicKey = (r) => _n(this.curve, r), this.curve = e, this.randomBytes = t, this.curveName = ht(e), this.signatureAlgorithmName = "EdDSA";
}
/**
* Gets the underlying curve implementation.
* This method allows access to the raw CurveFn implementation when needed.
*
* @returns {T} The underlying curve implementation
* @template T The type of the curve implementation
*/
getCurve() {
return this.curve;
}
}
const Dn = (i) => {
try {
return i.utils.randomPrivateKey();
} catch (e) {
throw console.log(w(e)), new Error("Failed to generate random private key");
}
}, $n = (i, e, t = !0) => {
try {
return i.getPublicKey(e, t);
} catch (r) {
throw console.log(w(r)), new Error("Failed to get public key");
}
}, Pn = (i, { message: e, privateKey: t, recovered: r = !1 }) => {
try {
const n = fe(e) ? ce(e) : e, s = i.sign(n, t, { prehash: !0 });
return r ? Un(s) : s.toCompactRawBytes();
} catch (n) {
throw console.log(w(n)), new Error("Failed to sign message");
}
}, Jn = (i, { signature: e, message: t, publicKey: r }) => {
try {
const n = fe(t) ? ce(t) : t, s = dt(i, e);
return i.verify(s, n, r, { prehash: !0 });
} catch (n) {
return console.log(w(n)), !1;
}
}, Gn = (i, { privateKey: e, publicKey: t }) => {
try {
const r = i.getSharedSecret(e, t).slice(1);
if (r.some((n) => n !== 0))
return r;
throw new Error("Shared secret is zero");
} catch (r) {
throw console.log(w(r)), new Error("Failed to compute shared secret");
}
}, kt = (i, e) => {
try {
const r = i.ProjectivePoint.fromHex(e).toRawBytes(!1), n = (r.length - 1) / 2, s = r.slice(1, 1 + n), o = r.slice(1 + n);
return {
kty: "EC",
crv: He(i),
alg: Le(i),
x: D(s),
y: D(o)
};
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert public key to JWK");
}
}, Kn = (i, e) => {
try {
const t = i.getPublicKey(e);
return {
...kt(i, t),
d: D(e)
};
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert private key to JWK");
}
}, Wn = (i, e) => {
try {
return mt(i, e);
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert JWK to raw public key");
}
}, mt = (i, e) => {
if (e.kty === void 0 || e.kty === null)
throw new Error("Missing required parameter for kty");
if (e.kty !== "EC")
throw new Error(`Invalid key type: ${e.kty}, expected EC`);
if (e.crv === void 0 || e.crv === null)
throw new Error("Missing required parameter for crv");
const t = He(i);
if (e.crv !== t)
throw new Error(
`Invalid curve: ${e.crv}, expected ${t}`
);
if (e.x === void 0 || e.x === null)
throw new Error("Missing required parameter for x");
if (typeof e.x != "string")
throw new Error("Invalid parameter type for x");
if (e.y === void 0 || e.y === null)
throw new Error("Missing required parameter for y");
if (typeof e.y != "string")
throw new Error("Invalid parameter type for y");
const r = Le(i);
if (e.alg !== void 0 && e.alg !== null && e.alg !== r)
throw new Error(
`Invalid algorithm: ${e.alg}, expected ${r}`
);
let n;
try {
n = $(e.x);
} catch {
throw new Error("Malformed encoding for x");
}
if (n.length !== i.CURVE.nByteLength)
throw new Error(
`Invalid the length of the key data for x: ${n.length}, expected ${i.CURVE.nByteLength}`
);
let s;
try {
s = $(e.y);
} catch {
throw new Error("Malformed encoding for y");
}
if (s.length !== i.CURVE.nByteLength)
throw new Error(
`Invalid the length of the key data for y: ${s.length}, expected ${i.CURVE.nByteLength}`
);
return new Uint8Array([4, ...n, ...s]);
}, qn = (i, e) => {
try {
return Xn(i, e);
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert JWK to raw private key");
}
}, Xn = (i, e) => {
const t = mt(i, e);
if (e.d === void 0 || e.d === null)
throw new Error("Missing required parameter for d");
if (typeof e.d != "string")
throw new Error("Invalid parameter type for d");
let r;
try {
r = $(e.d);
} catch {
throw new Error("Malformed encoding for d");
}
if (r.length !== i.CURVE.nByteLength)
throw new Error(
`Invalid the length of the key data for d: ${r.length}, expected ${i.CURVE.nByteLength}`
);
if (!oe(i.getPublicKey(r, !1), t))
throw new Error(
"The public key derived from the private key does not match the public key in the JWK"
);
return r;
}, Zn = (i, { signature: e, message: t, compressed: r = !0 }) => {
try {
const n = fe(t) ? ce(t) : t, s = i.CURVE.hash(n);
return dt(i, e).recoverPublicKey(s).toRawBytes(r);
} catch (n) {
throw console.log(w(n)), new Error("Failed to recover public key");
}
};
class L {
/**
* Creates a new Weierstrass instance.
*
* @param {CurveFn} curve - The curve implementation to use
* @param {RandomBytes} randomBytes - Function to generate random bytes
*/
constructor(e, t) {
this.randomPrivateKey = () => Dn(this.curve), this.getPublicKey = (r, n = !0) => $n(this.curve, r, n), this.sign = ({
message: r,
privateKey: n,
recovered: s = !1
}) => Pn(this.curve, { message: r, privateKey: n, recovered: s }), this.verify = ({ signature: r, message: n, publicKey: s }) => Jn(this.curve, { signature: r, message: n, publicKey: s }), this.recoverPublicKey = ({
signature: r,
message: n,
compressed: s = !0
}) => Zn(this.curve, {
signature: r,
message: n,
compressed: s
}), this.getSharedSecret = ({
privateKey: r,
publicKey: n
}) => Gn(this.curve, { privateKey: r, publicKey: n }), this.toJwkPrivateKey = (r) => Kn(this.curve, r), this.toJwkPublicKey = (r) => kt(this.curve, r), this.toRawPrivateKey = (r) => qn(this.curve, r), this.toRawPublicKey = (r) => Wn(this.curve, r), this.curve = e, this.randomBytes = t, this.curveName = He(e), this.signatureAlgorithmName = Le(e);
}
/**
* Gets the underlying curve implementation.
* This method allows access to the raw CurveFn implementation when needed.
*
* @returns {CurveFn} The underlying curve implementation
*/
getCurve() {
return this.curve;
}
}
const zn = (i) => {
try {
return i.utils.randomPrivateKey();
} catch (e) {
throw console.log(w(e)), new Error("Failed to generate random private key");
}
}, Yn = (i, e, t = !0) => {
if (!t)
throw new Error("Uncompressed public key is not supported");
try {
return i.getPublicKey(e);
} catch (r) {
throw console.log(w(r)), new Error("Failed to get public key");
}
}, Et = (i, e) => {
try {
if (e.length !== i.GuBytes.length)
throw new Error("Invalid public key length");
return {
kty: "OKP",
crv: we(i),
x: D(e)
};
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert public key to JWK");
}
}, Qn = (i, e) => {
try {
const t = i.getPublicKey(e);
return {
...Et(i, t),
d: D(e)
};
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert private key to JWK");
}
}, jn = (i, e) => {
try {
return xt(i, e);
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert JWK to raw public key");
}
}, xt = (i, e) => {
if (e.kty === void 0 || e.kty === null)
throw new Error("Missing required parameter for kty");
if (e.kty !== "OKP")
throw new Error(`Invalid key type: ${e.kty}, expected OKP`);
if (e.crv === void 0 || e.crv === null)
throw new Error("Missing required parameter for crv");
const t = we(i);
if (e.crv !== t)
throw new Error(
`Invalid curve: ${e.crv}, expected ${t}`
);
if (e.x === void 0 || e.x === null)
throw new Error("Missing required parameter for x");
if (typeof e.x != "string")
throw new Error("Invalid parameter type for x");
let r;
try {
r = $(e.x);
} catch {
throw new Error("Malformed encoding for x");
}
if (r.length !== i.GuBytes.length)
throw new Error(
`Invalid the length of the key data for x: ${r.length}, expected ${i.GuBytes.length}`
);
return r;
}, ei = (i, e) => {
try {
return ti(i, e);
} catch (t) {
throw console.log(w(t)), new Error("Failed to convert JWK to raw private key");
}
}, ti = (i, e) => {
const t = xt(i, e);
if (e.d === void 0 || e.d === null)
throw new Error("Missing required parameter for d");
if (typeof e.d != "string")
throw new Error("Invalid parameter type for d");
let r;
try {
r = $(e.d);
} catch {
throw new Error("Malformed encoding for d");
}
if (r.length !== i.GuBytes.length)
throw new Error(
`Invalid the length of the key data for d: ${r.length}, expected ${i.GuBytes.length}`
);
if (!oe(i.getPublicKey(r), t))
throw new Error(
"The public key derived from the private key does not match the public key in the JWK"
);
return r;
}, ri = [
// 0 (order 1)
"0000000000000000000000000000000000000000000000000000000000000000",
// 1 (order 1)
"0100000000000000000000000000000000000000000000000000000000000000",
// 325606250916557431795983626356110631294008115727848805560023387167927233504 (order 8)
"e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800",
// 39382357235489614581723060781553021112529911719440698176882885853963445705823 (order 8)
"5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157",
// p-1 (order 2)
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec",
// p (order 4)
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed",
// p+1 (order 1)
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee"
].map((i) => an(i)), ni = (i) => ri.some(
(e) => oe(i, e)
), ii = (i, { privateKey: e, publicKey: t }) => {
try {
if (we(i) === "X25519" && ni(t))
throw new Error("Public key is a small order point");
const r = i.getSharedSecret(e, t);
if (r.some((n) => n !== 0))
return r;
throw new Error("Shared secret is zero");
} catch (r) {
throw console.log(w(r)), new Error("Failed to compute shared secret");
}
};
class si {
/**
* Creates a new Montgomery instance.
*
* @param {CurveFn} curve - The curve implementation to use
* @param {RandomBytes} randomBytes - Function to generate random bytes
*/
constructor(e, t) {
this.curve = e, this.randomBytes = t, this.curveName = we(e);
}
/**
* Gets the underlying curve implementation.
* This method allows access to the raw CurveFn implementation when needed.
*
* @returns {CurveFn} The underlying curve implementation
*/
getCurve() {
return this.curve;
}
/**
* Generates a random private key for the Montgomery curve.
*
* @returns {Uint8Array} The random private key
*/
randomPrivateKey() {
return zn(this.curve);
}
/**
* Derives a public key from a private key.
*
* @param {Uint8Array} privateKey - The private key to derive from
* @param {boolean} [compressed=true] - Whether to return a compressed public key
* @returns {Uint8Array} The derived public key
* @throws {Error} If the private key is invalid or uncompressed keys are requested
*/
getPublicKey(e, t = !0) {
return Yn(this.curve, e, t);
}
/**
* Computes a shared secret using ECDH.
*
* @param {GetSharedSecretParams} params - The parameters for shared secret computation
* @param {Uint8Array} params.privateKey - The private key
* @param {Uint8Array} params.publicKey - The public key
* @returns {Uint8Array} The computed shared secret
* @throws {Error} If either the private key or public key is invalid
*/
getSharedSecret({
privateKey: e,
publicKey: t
}) {
return ii(this.curve, { privateKey: e, publicKey: t });
}
/**
* Converts a private key to JWK format.
*
* @param {Uint8Array} privateKey - The private key to convert
* @returns {JwkPrivateKey} The private key in JWK format
* @throws {Error} If the private key is invalid
*/
toJwkPrivateKey(e) {
return Qn(this.curve, e);
}
/**
* Converts a public key to JWK format.
*
* @param {Uint8Array} publicKey - The public key to convert
* @returns {JwkPublicKey} The public key in JWK format
* @throws {Error} If the public key is invalid
*/
toJwkPublicKey(e) {
return Et(this.curve, e);
}
/**
* Converts a JWK private key to raw format.
*
* @param {JwkPrivateKey} jwkPrivateKey - The JWK private key to convert
* @returns {Uint8Array} The private key in raw format
* @throws {Error} If the JWK is invalid
*/
toRawPrivateKey(e) {
return ei(this.curve, e);
}
/**
* Converts a JWK public key to raw format.
*
* @param {JwkPublicKey} jwkPublicKey - The JWK public key to convert
* @returns {Uint8Array} The public key in raw format
* @throws {Error} If the JWK is invalid
*/
toRawPublicKey(e) {
return jn(this.curve, e);
}
}
const _i = (i, e) => {
switch (i) {
case "P-256":
return new L(gt(e), e);
case "P-384":
return new L(wt(e), e);
case "P-521":
return new L(Bt(e), e);
case "secp256k1":
return new L(vt(e), e);
case "X25519":
return new si(xn(e), e);
default:
throw new Error(`Unsupported signature curve: ${i}`);
}
}, Fi = (i, e) => {
switch (i) {
case "P-256":
return new L(gt(e), e);
case "P-384":
return new L(wt(e), e);
case "P-521":
return new L(Bt(e), e);
case "secp256k1":
return new L(vt(e), e);
case "Ed25519":
return new On(mn(e), e);
default:
throw new Error(`Unsupported signature curve: ${i}`);
}
};
/*!
* MIT License
*
* Copyright (c) 2017-2024 Peculiar Ventures, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
const oi = "[object ArrayBuffer]";
class g {
static isArrayBuffer(e) {
return Object.prototype.toString.call(e) === oi;
}
static toArrayBuffer(e) {
return this.isArrayBuffer(e) ? e : e.byteLength === e.buffer.byteLength || e.byteOffset === 0 && e.byteLength === e.buffer.byteLength ? e.buffer : this.toUint8Array(e.buffer).slice(e.byteOffset, e.byteOffset + e.byteLength).buffer;
}
static toUint8Array(e) {
return this.toView(e, Uint8Array);
}
static toView(e, t) {
if (e.constructor === t)
return e;
if (this.isArrayBuffer(e))
return new t(e);
if (this.isArrayBufferView(e))
return new t(e.buffer, e.byteOffset, e.byteLength);
throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
}
static isBufferSource(e) {
return this.isArrayBufferView(e) || this.isArrayBuffer(e);
}
static isArrayBufferView(e) {
return ArrayBuffer.isView(e) || e && this.isArrayBuffer(e.buffer);
}
static isEqual(e, t) {
const r = g.toUint8Array(e), n = g.toUint8Array(t);
if (r.length !== n.byteLength)
return !1;
for (let s = 0; s < r.length; s++)
if (r[s] !== n[s])
return !1;
return !0;
}
static concat(...e) {
let t;
Array.isArray(e[0]) && !(e[1] instanceof Function) || Array.isArray(e[0]) && e[1] instanceof Function ? t = e[0] : e[e.length - 1] instanceof Function ? t = e.slice(0, e.length - 1) : t = e;
let r = 0;
for (const o of t)
r += o.byteLength;
const n = new Uint8Array(r);
let s = 0;
for (const o of t) {
const f = this.toUint8Array(o);
n.set(f, s), s += f.length;
}
return e[e.length - 1] instanceof Function ? this.toView(n, e[e.length - 1]) : n.buffer;
}
}
const Ve = "string", fi = /^[0-9a-f\s]+$/i, ci = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, ai = /^[a-zA-Z0-9-_]+$/;
class st {
static fromString(e) {
const t = unescape(encodeURIComponent(e)), r = new Uint8Array(t.length);
for (let n = 0; n < t.length; n++)
r[n] = t.charCodeAt(n);
return r.buffer;
}
static toString(e) {
const t = g.toUint8Array(e);
let r = "";
for (let s = 0; s < t.length; s++)
r += String.fromCharCode(t[s]);
return decodeURIComponent(escape(r));
}
}
class N {
static toString(e, t = !1) {
const r = g.toArrayBuffer(e), n = new DataView(r);
let s = "";
for (let o = 0; o < r.byteLength; o += 2) {
const f = n.getUint16(o, t);
s += String.fromCharCode(f);
}
return s;
}
static fromString(e, t = !1) {
const r = new ArrayBuffer(e.length * 2), n = new DataView(r);
for (let s = 0; s < e.length; s++)
n.setUint16(s * 2, e.charCodeAt(s), t);
return r;
}
}
class y {
static isHex(e) {
return typeof e === Ve && fi.test(e);
}
static isBase64(e) {
return typeof e === Ve && ci.test(e);
}
static isBase64Url(e) {
return typeof e === Ve && ai.test(e);
}
static ToString(e, t = "utf8") {
const r = g.toUint8Array(e);
switch (t.toLowerCase()) {
case "utf8":
return this.ToUtf8String(r);
case "binary":
return this.ToBinary(r);
case "hex":
return this.ToHex(r);
case "base64":
return this.ToBase64(r);
case "base64url":
return this.ToBase64Url(r);
case "utf16le":
return N.toString(r, !0);
case "utf16":
case "utf16be":
return N.toString(r);
default:
throw new Error(`Unknown type of encoding '${t}'`);
}
}
static FromString(e, t = "utf8") {
if (!e)
return new ArrayBuffer(0);
switch (t.toLowerCase()) {
case "utf8":
return this.FromUtf8String(e);
case "binary":
return this.FromBinary(e);
case "hex":
return this.FromHex(e);
case "base64":
return this.FromBase64(e);
case "base64url":
return this.FromBase64Url(e);
case "utf16le":
return N.fromString(e, !0);
case "utf16":
case "utf16be":
return N.fromString(e);
default:
throw new Error(`Unknown type of encoding '${t}'`);
}
}
static ToBase64(e) {
const t = g.toUint8Array(e);
if (typeof btoa < "u") {
const r = this.ToString(t, "binary");
return btoa(r);
} else
return Buffer.from(t).toString("base64");
}
static FromBase64(e) {
const t = this.formatString(e);
if (!t)
return new ArrayBuffer(0);
if (!y.isBase64(t))
throw new TypeError("Argument 'base64Text' is not Base64 encoded");
return typeof atob < "u" ? this.FromBinary(atob(t)) : new Uint8Array(Buffer.from(t, "base64")).buffer;
}
static FromBase64Url(e) {
const t = this.formatString(e);
if (!t)
return new ArrayBuffer(0);
if (!y.isBase64Url(t))
throw new TypeError("Argument 'base64url' is not Base64Url encoded");
return this.FromBase64(this.Base64Padding(t.replace(/\-/g, "+").replace(/\_/g, "/")));
}
static ToBase64Url(e) {
return this.ToBase64(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
}
static FromUtf8String(e, t = y.DEFAULT_UTF8_ENCODING) {
switch (t) {
case "ascii":
return this.FromBinary(e);
case "utf8":
return st.fromString(e);
case "utf16":
case "utf16be":
return N.fromString(e);
case "utf16le":
case "usc2":
return N.fromString(e, !0);
default:
throw new Error(`Unknown type of encoding '${t}'`);
}
}
static ToUtf8String(e, t = y.DEFAULT_UTF8_ENCODING) {
switch (t) {
case "ascii":
return this.ToBinary(e);
case "utf8":
return st.toString(e);
case "utf16":
case "utf16be":
return N.toString(e);
case "utf16le":
case "usc2":
return N.toString(e, !0);
default:
throw new Error(`Unknown type of encoding '${t}'`);
}
}
static FromBinary(e) {
const t = e.length, r = new Uint8Array(t);
for (let n = 0; n < t; n++)
r[n] = e.charCodeAt(n);
return r.buffer;
}
static ToBinary(e) {
const t = g.toUint8Array(e);
let r = "";
for (let n = 0; n < t.length; n++)
r += String.fromCharCode(t[n]);
return r;
}
static ToHex(e) {
const t = g.toUint8Array(e);
let r = "";
const n = t.length;
for (let s = 0; s < n; s++) {
const o = t[s];
o < 16 && (r += "0"), r += o.toString(16);
}
return r;
}
static FromHex(e) {
let t = this.formatString(e);
if (!t)
return new ArrayBuffer(0);
if (!y.isHex(t))
throw new TypeError("Argument 'hexString' is not HEX encoded");
t.length % 2 && (t = `0${t}`);
const r = new Uint8Array(t.length / 2);
for (let n = 0; n < t.length; n = n + 2) {
const s = t.slice(n, n + 2);
r[n / 2] = parseInt(s, 16);
}
return r.buffer;
}
static ToUtf16String(e, t = !1) {
return N.toString(e, t);
}
static FromUtf16String(e, t = !1) {
return N.fromString(e, t);
}
static Base64Padding(e) {
const t = 4 - e.length % 4;
if (t < 4)
for (let r = 0; r < t; r++)
e += "=";
return e;
}
static formatString(e) {
return (e == null ? void 0 : e.replace(/[\n\r\t ]/g, "")) || "";
}
}
y.DEFAULT_UTF8_ENCODING = "utf8";
/*!
Copyright (c) Peculiar Ventures, LLC
*/
function X(i, e) {
let t = 0;
if (i.length === 1)
return i[0];
for (let r = i.length - 1; r >= 0; r--)
t += i[i.length - 1 - r] * Math.pow(2, e * r);
return t;
}
function P(i, e, t = -1) {
const r = t;
let n = i, s = 0, o = Math.pow(2, e);
for (let f = 1; f < 8; f++) {
if (i < o) {
let c;
if (r < 0)
c = new ArrayBuffer(f), s = f;
else {
if (r < f)
return new ArrayBuffer(0);
c = new ArrayBuffer(r), s = r;
}
const u = new Uint8Array(c);
for (let h = f - 1; h >= 0; h--) {
const a = Math.pow(2, h * e);
u[s - h - 1] = Math.floor(n / a), n -= u[s - h - 1] * a;
}
return c;
}
o *= Math.pow(2, e);
}
return new ArrayBuffer(0);
}
function Ce(...i) {
let e = 0, t = 0;
for (const s of i)
e += s.length;
const r = new ArrayBuffer(e), n = new Uint8Array(r);
for (const s of i)
n.set(s, t), t += s.length;
return n;
}
function pt() {
const i = new Uint8Array(this.valueHex);
if (this.valueHex.byteLength >= 2) {
const f = i[0] === 255 && i[1] & 128, c = i[0] === 0 && (i[1] & 128) === 0;
(f || c) && this.warnings.push("Needlessly long format");
}
const e = new ArrayBuffer(this.valueHex.byteLength), t = new Uint8Array(e);
for (let f = 0; f < this.valueHex.byteLength; f++)
t[f] = 0;
t[0] = i[0] & 128;
const r = X(t, 8), n = new ArrayBuffer(this.valueHex.byteLength), s = new Uint8Array(n);
for (let f = 0; f < this.valueHex.byteLength; f++)
s[f] = i[f];
return s[0] &= 127, X(s, 8) - r;
}
function li(i) {
const e = i < 0 ? i * -1 : i;
let t = 128;
for (let r = 1; r < 8; r++) {
if (e <= t) {
if (i < 0) {
const o = t - e, f = P(o, 8, r), c = new Uint8Array(f);
return c[0] |= 128, f;
}
let n = P(e, 8, r), s = new Uint8Array(n);
if (s[0] & 128) {
const o = n.slice(0), f = new Uint8Array(o);
n = new ArrayBuffer(n.byteLength + 1), s = new Uint8Array(n);
for (let c = 0; c < o.byteLength; c++)
s[c + 1] = f[c];
s[0] = 0;
}
return n;
}
t *= Math.pow(2, 8);
}
return new ArrayBuffer(0);
}
function ui(i, e) {
if (i.byteLength !== e.byteLength)
return !1;
const t = new Uint8Array(i), r = new Uint8Array(e);
for (let n = 0; n < t.length; n++)
if (t[n] !== r[n])
return !1;
return !0;
}
function S(i, e) {
const t = i.toString(10);
if (e < t.length)
return "";
const r = e - t.length, n = new Array(r);
for (let o = 0; o < r; o++)
n[o] = "0";
return n.join("").concat(t);
}
/*!
* Copyright (c) 2014, GMO GlobalSign
* Copyright (c) 2015-2022, Peculiar Ventures
* All rights reserved.
*
* Author 2014-2019, Yury Strozhevsky
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
function de() {
if (typeof BigInt > "u")
throw new Error("BigInt is not defined. Your environment doesn't implement BigInt.");
}
function _e(i) {
let e = 0, t = 0;
for (let n = 0; n < i.length; n++) {
const s = i[n];
e += s.byteLength;
}
const r = new Uint8Array(e);
for (let n = 0; n < i.length; n++) {
const s = i[n];
r.set(new Uint8Array(s), t), t += s.byteLength;
}
return r.buffer;
}
function C(i, e, t, r) {
return e instanceof Uint8Array ? e.byteLength ? t < 0 ? (i.error = "Wrong parameter: inputOffset less than zero", !1) : r < 0 ? (i.error = "Wrong parameter: inputLength less than zero", !1) : e.byteLength - t - r < 0 ? (i.error = "End of input reached before message was fully decoded (inconsistent offset and length values)", !1) : !0 : (i.error = "Wrong parameter: inputBuffer has zero length", !1) : (i.error = "Wrong parameter: inputBuffer must be 'Uint8Array'", !1);
}
class Fe {
constructor() {
this.items = [];
}
write(e) {
this.items.push(e);
}
final() {
return _e(this.items);
}
}
const ne = [new Uint8Array([1])], ot = "0123456789", Y = "", V = new ArrayBuffer(0), Me = new Uint8Array(0), se = "EndOfContent", St = "OCTET STRING", At = "BIT STRING";
function R(i) {
var e;
return e = class extends i {
get valueHex() {
return this.valueHexView.slice().buffer;
}
set valueHex(r) {
this.valueHexView = new Uint8Array(r);
}
constructor(...r) {
var n;
super(...r);
const s = r[0] || {};
this.isHexOnly = (n = s.isHexOnly) !== null && n !== void 0 ? n : !1, this.valueHexView = s.valueHex ? g.toUint8Array(s.valueHex) : Me;
}
fromBER(r, n, s) {
const o = r instanceof ArrayBuffer ? new Uint8Array(r) : r;
if (!C(this, o, n, s))
return -1;
const f = n + s;
return this.valueHexView = o.subarray(n, f), this.valueHexView.length ? (this.blockLength = s, f) : (this.warnings.push("Zero buffer length"), n);
}
toBER(r = !1) {
return this.isHexOnly ? r ? new ArrayBuffer(this.valueHexView.byteLength) : this.valueHexView.byteLength === this.valueHexView.buffer.byteLength ? this.valueHexView.buffer : this.valueHexView.slice().buffer : (this.error = "Flag 'isHexOnly' is not set, abort", V);
}
toJSON() {
return {
...super.toJSON(),
isHexOnly: this.isHexOnly,
valueHex: y.ToHex(this.valueHexView)
};
}
}, e.NAME = "hexBlock", e;
}
class J {
static blockName() {
return this.NAME;
}
get valueBeforeDecode() {
return this.valueBeforeDecodeView.slice().buffer;
}
set valueBeforeDecode(e) {
this.valueBeforeDecodeView = new Uint8Array(e);
}
constructor({ blockLength: e = 0, error: t = Y, warnings: r = [], valueBeforeDecode: n = Me } = {}) {
this.blockLength = e, this.error = t, this.warnings = r, this.valueBeforeDecodeView = g.toUint8Array(n);
}
toJSON() {
return {
blockName: this.constructor.NAME,
blockLength: this.blockLength,
error: this.error,
warnings: this.warnings,
valueBeforeDecode: y.ToHex(this.valueBeforeDecodeView)
};
}
}
J.NAME = "baseBlock";
class x extends J {
fromBER(e, t, r) {
throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
}
toBER(e, t) {
throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
}
}
x.NAME = "valueBlock";
class It extends R(J) {
constructor({ idBlock: e = {} } = {}) {
var t, r, n, s;
super(), e ? (this.isHexOnly = (t = e.isHexOnly) !== null && t !== void 0 ? t : !1, this.valueHexView = e.valueHex ? g.toUint8Array(e.valueHex) : Me, this.tagClass = (r = e.tagClass) !== null && r !== void 0 ? r : -1, this.tagNumber = (n = e.tagNumber) !== null && n !== void 0 ? n : -1, this.isConstructed = (s = e.isConstructed) !== null && s !== void 0 ? s : !1) : (this.tagClass = -1, this.tagNumber = -1, this.isConstructed = !1);
}
toBER(e = !1) {
let t = 0;
switch (this.tagClass) {
case 1:
t |= 0;
break;
case 2:
t |= 64;
break;
case 3:
t |= 128;
break;
case 4:
t |= 192;
break;
default:
return this.error = "Unknown tag class", V;
}
if (this.isConstructed && (t |= 32), this.tagNumber < 31 && !this.isHexOnly) {
const n = new Uint8Array(1);
if (!e) {
let s = this.tagNumber;
s &= 31, t |= s, n[0] = t;
}
return n.buffer;
}
if (!this.isHexOnly) {
const n = P(this.tagNumber, 7), s = new Uint8Array(n), o = n.byteLength, f = new Uint8Array(o + 1);
if (f[0] = t | 31, !e) {
for (let c = 0; c < o - 1; c++)
f[c + 1] = s[c] | 128;
f[o] = s[o - 1];
}
return f.buffer;
}
const r = new Uint8Array(this.valueHexView.byteLength + 1);
if (r[0] = t | 31, !e) {
const n = this.valueHexView;
for (let s = 0; s < n.length - 1; s++)
r[s + 1] = n[s] | 128;
r[this.valueHexView.byteLength] = n[n.length - 1];
}
return r.buffer;
}
fromBER(e, t, r) {
const n = g.toUint8Array(e);
if (!C(this, n, t, r))
return -1;
const s = n.subarray(t, t + r);
if (s.length === 0)
return this.error = "Zero buffer length", -1;
switch (s[0] & 192) {
case 0:
this.tagClass = 1;
break;
case 64:
this.tagClass = 2;
break;
case 128:
this.tagClass = 3;
break;
case 192:
this.tagClass = 4;
break;
default:
return this.error = "Unknown tag class", -1;
}
this.isConstructed = (s[0] & 32) === 32, this.isHexOnly = !1;
const f = s[0] & 31;
if (f !== 31)
this.tagNumber = f, this.blockLength = 1;
else {
let c = 1, u = this.valueHexView = new Uint8Array(255), h = 255;
for (; s[c] & 128; ) {
if (u[c - 1] = s[c] & 127, c++, c >= s.length)
return this.error = "End of input reached before message was fully decoded", -1;
if (c === h) {
h += 255;
const d = new Uint8Array(h);
for (let B = 0; B < u.length; B++)
d[B] = u[B];
u = this.valueHexView = new Uint8Array(h);
}
}
this.blockLength = c + 1, u[c - 1] = s[c] & 127;
const a = new Uint8Array(c);
for (let d = 0; d < c; d++)
a[d] = u[d];
u = this.valueHexView = new Uint8Array(c), u.set