Files
cinny/src/client/secretStorageKeys.ts
T
Lotus CIandClaude Opus 5.5 b944456ab5
CI / Build & Quality Checks (push) Skipped
CI / Secret scan (gitleaks) (push) Skipped
CI / Playwright smoke (e2e) (push) Skipped
CI / Build & Quality Checks (pull_request) Successful in 3m32s
CI / Trigger Desktop Build (pull_request) Skipped
CI / Docker image build & smoke test (pull_request) Skipped
CI / Secret scan (gitleaks) (pull_request) Successful in 16s
CI / Playwright smoke (e2e) (pull_request) Successful in 24m32s
chore(types): typed secret storage key cache; drop the cryptoCallbacks cast (#210)
secretStorageKeys was untyped JS, passed to createClient behind `as any`,
which hid that getSecretStorageKey returned undefined where the SDK type
says null. Now TypeScript against the SDK's CryptoCallbacks, returns null,
and storePrivateKey keeps its own copy of the key. Unit test added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017PCb59uY3UKqDqt7B6hzd5
2026-10-25 19:38:10 -04:00

28 lines
1.0 KiB
TypeScript

import type { CryptoCallbacks } from 'matrix-js-sdk/lib/crypto-api';
const secretStorageKeys = new Map<string, Uint8Array<ArrayBuffer>>();
export function storePrivateKey(keyId: string, privateKey: Uint8Array) {
if (privateKey instanceof Uint8Array === false) {
throw new Error('Unable to store, privateKey is invalid.');
}
// Our own copy, on a plain ArrayBuffer (what the SDK's callback type wants).
secretStorageKeys.set(keyId, new Uint8Array(privateKey));
}
export function clearSecretStorageKeys() {
secretStorageKeys.clear();
}
export const cryptoCallbacks: CryptoCallbacks = {
async getSecretStorageKey({ keys }) {
const keyId = Object.keys(keys).find((id) => secretStorageKeys.get(id) instanceof Uint8Array);
const privateKey = keyId ? secretStorageKeys.get(keyId) : undefined;
// The SDK expects null (not undefined) when no key is cached.
return keyId && privateKey ? [keyId, privateKey] : null;
},
cacheSecretStorageKey(keyId, keyInfo, privateKey) {
secretStorageKeys.set(keyId, privateKey);
},
};