UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

137 lines (136 loc) • 5.01 kB
import { m as asSafeIntegerInRange } from "./number-coercion-D6u-4YD8.mjs"; import { n as deriveCanonicalEd25519PrivateKeyRaw, r as deriveCanonicalEd25519PublicKeyRaw } from "./ed25519-signature-CUaH7IDK.mjs"; import crypto from "node:crypto"; //#region src/infra/device-identity-material.ts const PRIMARY_DEVICE_IDENTITY_KEY = "primary"; var DeviceIdentityStorageError = class extends Error { constructor(e, t) { super(e, t); this.name = "DeviceIdentityStorageError"; } }; function normalizeIdentityKey(e) { const t = e ?? PRIMARY_DEVICE_IDENTITY_KEY; if (t.length === 0 || t !== t.trim()) throw new DeviceIdentityStorageError("Device identity key must be a non-empty string without surrounding whitespace."); if (t.length > 128) throw new DeviceIdentityStorageError("Device identity key exceeds 128 characters."); return t; } function invalidStoredIdentityError(e, t) { return new DeviceIdentityStorageError(`SQLite contains an invalid persisted device identity "${e}". Run "openclaw doctor --fix" before starting the gateway or connecting this client.`, t === void 0 ? void 0 : { cause: t }); } function fingerprintPublicKey(e) { const t = deriveCanonicalEd25519PublicKeyRaw(e); return crypto.createHash("sha256").update(t).digest("hex"); } /** Generate canonical Ed25519 material before entering a synchronous write transaction. */ function generateStoredDeviceIdentity(e = Date.now()) { const { publicKey: t, privateKey: n } = crypto.generateKeyPairSync("ed25519"); const r = t.export({ type: "spki", format: "pem" }); const i = n.export({ type: "pkcs8", format: "pem" }); return { deviceId: fingerprintPublicKey(r), publicKeyPem: r, privateKeyPem: i, createdAtMs: e }; } function keyPairMatches(e, t) { try { deriveCanonicalEd25519PublicKeyRaw(e); deriveCanonicalEd25519PrivateKeyRaw(t); return crypto.createPublicKey(t).export({ type: "spki", format: "pem" }) === e; } catch { return false; } } function parseCreatedAtMs(e) { return asSafeIntegerInRange(e, { min: 0 }) ?? null; } /** Validate persisted key material and return the canonical runtime shape. */ function validateStoredDeviceIdentity(e, t = PRIMARY_DEVICE_IDENTITY_KEY) { try { if (!e.deviceId || !/^[a-f0-9]{64}$/.test(e.deviceId) || !e.publicKeyPem || !e.privateKeyPem || parseCreatedAtMs(e.createdAtMs) === null || !keyPairMatches(e.publicKeyPem, e.privateKeyPem)) throw invalidStoredIdentityError(t); if (fingerprintPublicKey(e.publicKeyPem) !== e.deviceId) throw invalidStoredIdentityError(t); return { deviceId: e.deviceId, publicKeyPem: e.publicKeyPem, privateKeyPem: e.privateKeyPem }; } catch (e) { if (e instanceof DeviceIdentityStorageError) throw e; throw invalidStoredIdentityError(t, e); } } function storedDeviceIdentityFromRow(e, t) { if (e.identity_key !== t || typeof e.device_id !== "string" || typeof e.public_key_pem !== "string" || typeof e.private_key_pem !== "string" || parseCreatedAtMs(e.created_at_ms) === null || parseCreatedAtMs(e.updated_at_ms) === null) throw invalidStoredIdentityError(t); const n = { deviceId: e.device_id, publicKeyPem: e.public_key_pem, privateKeyPem: e.private_key_pem, createdAtMs: e.created_at_ms }; validateStoredDeviceIdentity(n, t); return n; } function salvageStoredIdentityRow(e, t, n) { if (e.identity_key !== t || typeof e.public_key_pem !== "string" || typeof e.private_key_pem !== "string") return null; try { const r = crypto.createPublicKey(e.public_key_pem); const i = crypto.createPrivateKey(e.private_key_pem); if (r.asymmetricKeyType !== "ed25519" || i.asymmetricKeyType !== "ed25519") return null; const a = r.export({ type: "spki", format: "pem" }); const o = i.export({ type: "pkcs8", format: "pem" }); if (crypto.createPublicKey(o).export({ type: "spki", format: "pem" }) !== a) return null; const s = parseCreatedAtMs(e.created_at_ms) ?? parseCreatedAtMs(e.updated_at_ms) ?? n; const c = { deviceId: fingerprintPublicKey(a), publicKeyPem: a, privateKeyPem: o, createdAtMs: s }; validateStoredDeviceIdentity(c, t); return c; } catch { return null; } } function storedDeviceIdentityToRow(e, t, n = t.createdAtMs) { return { identity_key: e, device_id: t.deviceId, public_key_pem: t.publicKeyPem, private_key_pem: t.privateKeyPem, created_at_ms: t.createdAtMs, updated_at_ms: n }; } function identityFromCredentialResult(e) { if (e.kind !== "identity") throw new Error("Client credential owner returned an unexpected identity result."); const t = e.identity; return t ? { deviceId: t.device_id, publicKeyPem: t.public_key_pem, privateKeyPem: t.private_key_pem, createdAtMs: t.created_at_ms } : null; } //#endregion export { salvageStoredIdentityRow as a, validateStoredDeviceIdentity as c, normalizeIdentityKey as i, generateStoredDeviceIdentity as n, storedDeviceIdentityFromRow as o, identityFromCredentialResult as r, storedDeviceIdentityToRow as s, DeviceIdentityStorageError as t };