openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
90 lines (89 loc) • 3.87 kB
JavaScript
import { t as pruneMapToMaxSize } from "./map-size-BBr20tbc.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { n as deriveCanonicalEd25519PrivateKeyRaw } from "./ed25519-signature-CUaH7IDK.mjs";
import { n as generateStoredDeviceIdentity, r as identityFromCredentialResult, s as storedDeviceIdentityToRow } from "./device-identity-material-BGzHHiUD.mjs";
import { n as runClientCredentialOperationAsync } from "./client-credential-async-SU4iNwpp.mjs";
import { n as resolveDeviceIdentityStore, t as resolveDeviceIdentityEnvironment } from "./device-identity-store-options-Bz70uEBm.mjs";
import { hkdfSync } from "node:crypto";
//#region src/infra/device-identity-async.ts
const log = createSubsystemLogger("gateway/chat");
let warnedUnavailableChatIdentity = false;
const identities = /* @__PURE__ */ new Map();
const chatRequestKeys = /* @__PURE__ */ new WeakMap();
function readProcessDeviceIdentity(e) {
return identities.get(e);
}
function getChatRequestIdentityKey(e) {
const t = chatRequestKeys.get(e);
if (t) return t;
const n = Buffer.from(hkdfSync("sha256", deriveCanonicalEd25519PrivateKeyRaw(e.privateKeyPem), "openclaw.device-identity", "openclaw.chat.request.v1", 32));
chatRequestKeys.set(e, n);
return n;
}
function cacheProcessDeviceIdentity(e, t) {
const n = identities.get(e);
if (n) return n;
pruneMapToMaxSize(identities, 31);
identities.set(e, t);
return t;
}
/** Resolve private signing material without opening the selected Gateway database. */
async function loadOrCreateDeviceIdentityAsync(e = {}) {
const { identityKey: t } = resolveDeviceIdentityStore(e);
const n = resolveDeviceIdentityEnvironment(e);
const r = identityFromCredentialResult(await runClientCredentialOperationAsync(n, {
kind: "identity.create",
identity: storedDeviceIdentityToRow(t, generateStoredDeviceIdentity())
}));
if (!r) throw new Error(`Client credential owner did not persist identity "${t}".`);
return {
deviceId: r.deviceId,
publicKeyPem: r.publicKeyPem,
privateKeyPem: r.privateKeyPem
};
}
/** Keep the existing process-lifetime identity cache without opening SQLite on a warm hit. */
async function loadOrCreateProcessDeviceIdentityAsync(e = {}) {
const { databasePath: t, identityKey: n } = resolveDeviceIdentityStore(e);
const r = `${t}\0${n}`;
const i = readProcessDeviceIdentity(r);
if (i) return i;
return cacheProcessDeviceIdentity(r, await loadOrCreateDeviceIdentityAsync({
...e,
path: t,
identityKey: n
}));
}
/** Chat admission consumes an existing identity without creating or replacing one. */
async function loadChatRequestIdentityKeyAsync(e = {}) {
const { databasePath: t, identityKey: n } = resolveDeviceIdentityStore(e);
const r = `${t}\0${n}`;
try {
const i = readProcessDeviceIdentity(r) ?? await loadDeviceIdentityIfPresentAsync({
...e,
path: t,
identityKey: n
});
if (i) return getChatRequestIdentityKey(cacheProcessDeviceIdentity(r, i));
} catch {}
if (!warnedUnavailableChatIdentity) {
warnedUnavailableChatIdentity = true;
log.warn(`Gateway device identity "${n}" is missing or unreadable; chat.send will continue without a keyed request fingerprint.`);
}
return null;
}
/** Existing-only reads never create a local store or a signing identity. */
async function loadDeviceIdentityIfPresentAsync(e = {}) {
const { identityKey: t } = resolveDeviceIdentityStore(e);
const n = identityFromCredentialResult(await runClientCredentialOperationAsync(resolveDeviceIdentityEnvironment(e), {
kind: "identity.read",
identity_key: t
}));
return n ? {
deviceId: n.deviceId,
publicKeyPem: n.publicKeyPem,
privateKeyPem: n.privateKeyPem
} : null;
}
//#endregion
export { loadOrCreateProcessDeviceIdentityAsync as i, loadDeviceIdentityIfPresentAsync as n, loadOrCreateDeviceIdentityAsync as r, loadChatRequestIdentityKeyAsync as t };