openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.16 kB
JavaScript
import{t as pruneMapToMaxSize}from"./map-size-DLQqorvK.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{n as deriveCanonicalEd25519PrivateKeyRaw}from"./ed25519-signature-BXghQ01p.mjs";import{n as generateStoredDeviceIdentity,r as identityFromCredentialResult,s as storedDeviceIdentityToRow}from"./device-identity-material-CkS6RKfU.mjs";import{n as runClientCredentialOperationAsync}from"./client-credential-async-Cjr3lnxO.mjs";import{n as resolveDeviceIdentityStore,t as resolveDeviceIdentityEnvironment}from"./device-identity-store-options-CreQnSDU.mjs";import{hkdfSync}from"node:crypto";
//!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}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}}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}))}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}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}export{loadOrCreateProcessDeviceIdentityAsync as i,loadDeviceIdentityIfPresentAsync as n,loadOrCreateDeviceIdentityAsync as r,loadChatRequestIdentityKeyAsync as t};