openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
97 lines (96 loc) • 3.8 kB
JavaScript
import crypto from "node:crypto";
//#region src/infra/ed25519-signature.ts
const ED25519_RAW_KEY_LENGTH = 32;
const ED25519_SPKI_PREFIX = Buffer.from("302a300506032b6570032100", "hex");
const ED25519_PKCS8_PRIVATE_PREFIX = Buffer.from("302e020100300506032b657004220420", "hex");
const MAX_BASE64URL_DECODE_INPUT_LENGTH = 4096;
/** Decode the existing permissive base64url wire shape. */
function base64UrlDecode(e) {
if (e.length > MAX_BASE64URL_DECODE_INPUT_LENGTH) throw new Error("base64url input exceeds the maximum allowed length");
const t = e.replaceAll("-", "+").replaceAll("_", "/");
const n = t + "=".repeat((4 - t.length % 4) % 4);
return Buffer.from(n, "base64");
}
function pemEncode(e, t) {
return `-----BEGIN ${e}-----\n${t.toString("base64").match(/.{1,64}/g)?.join("\n") ?? ""}\n-----END ${e}-----\n`;
}
function decodeCanonicalPem(e, t) {
const n = `-----BEGIN ${e}-----\n`;
const r = `\n-----END ${e}-----\n`;
if (!t.startsWith(n) || !t.endsWith(r)) throw new Error(`${e} must use canonical PEM framing`);
const i = t.slice(n.length, -r.length);
if (!i || !/^[A-Za-z0-9+/=\n]+$/.test(i)) throw new Error(`${e} contains invalid PEM body bytes`);
const a = Buffer.from(i.replaceAll("\n", ""), "base64");
if (pemEncode(e, a) !== t) throw new Error(`${e} must use canonical PEM base64 encoding`);
return a;
}
function assertEd25519KeyType(e, t) {
if (e.asymmetricKeyType !== "ed25519") throw new Error(`${t} must be an Ed25519 key`);
}
function deriveRawKeyFromDer(e, t, n) {
const r = n.length + ED25519_RAW_KEY_LENGTH;
if (e.length !== r || !e.subarray(0, n.length).equals(n)) throw new Error(`${t} has a noncanonical Ed25519 encoding`);
return e.subarray(n.length);
}
function deriveCanonicalEd25519PublicKeyRaw(e) {
const t = decodeCanonicalPem("PUBLIC KEY", e);
assertEd25519KeyType(crypto.createPublicKey({
key: t,
type: "spki",
format: "der"
}), "public key");
return deriveRawKeyFromDer(t, "public key", ED25519_SPKI_PREFIX);
}
function deriveCanonicalEd25519PrivateKeyRaw(e) {
const t = decodeCanonicalPem("PRIVATE KEY", e);
assertEd25519KeyType(crypto.createPrivateKey({
key: t,
type: "pkcs8",
format: "der"
}), "private key");
return deriveRawKeyFromDer(t, "private key", ED25519_PKCS8_PRIVATE_PREFIX);
}
/** Parse any Node-compatible Ed25519 PEM and return its canonical raw public key. */
function deriveEd25519PublicKeyRaw(e) {
const t = crypto.createPublicKey(e);
assertEd25519KeyType(t, "public key");
return deriveRawKeyFromDer(t.export({
type: "spki",
format: "der"
}), "public key", ED25519_SPKI_PREFIX);
}
function publicKeyRawBase64UrlFromEd25519Pem(e) {
return deriveEd25519PublicKeyRaw(e).toString("base64url");
}
function normalizeEd25519PublicKeyBase64Url(e) {
try {
const t = e.includes("BEGIN") ? deriveEd25519PublicKeyRaw(e) : base64UrlDecode(e);
if (t.length === 0) return null;
return t.toString("base64url");
} catch {
return null;
}
}
function signEd25519Payload(e, t) {
const n = crypto.createPrivateKey(e);
return crypto.sign(null, Buffer.from(t, "utf8"), n).toString("base64url");
}
function createEd25519PublicKey(e) {
if (e.includes("BEGIN")) return crypto.createPublicKey(e);
return crypto.createPublicKey({
key: Buffer.concat([ED25519_SPKI_PREFIX, base64UrlDecode(e)]),
type: "spki",
format: "der"
});
}
function verifyEd25519SignatureBytes(e) {
try {
const t = createEd25519PublicKey(e.publicKey);
const n = base64UrlDecode(e.signatureBase64Url);
return crypto.verify(null, e.payload, t, n);
} catch {
return false;
}
}
//#endregion
export { signEd25519Payload as a, publicKeyRawBase64UrlFromEd25519Pem as i, deriveCanonicalEd25519PublicKeyRaw as n, verifyEd25519SignatureBytes as o, normalizeEd25519PublicKeyBase64Url as r, deriveCanonicalEd25519PrivateKeyRaw as t };