refactor: convert secret storage keys to typescript

This commit is contained in:
hazre committed 2026-04-14 23:43:03 +02:00
1 parent 9998034c5c
commit 0bc8dae940
3 files changed
+52 -45

No files matched your search

-8
View File
@@ -1,8 +0,0 @@
import type { CryptoCallbacks } from '$types/matrix-sdk';
export function storePrivateKey(keyId: string, privateKey: Uint8Array): void;
export function clearSecretStorageKeys(): void;
export const cryptoCallbacks: Pick<
CryptoCallbacks,
'getSecretStorageKey' | 'cacheSecretStorageKey'
>;
-37
View File
@@ -1,37 +0,0 @@
const secretStorageKeys = new Map();
export function storePrivateKey(keyId, privateKey) {
if (privateKey instanceof Uint8Array === false) {
throw new Error('Unable to store, privateKey is invalid.');
}
secretStorageKeys.set(keyId, privateKey);
}
function hasPrivateKey(keyId) {
return secretStorageKeys.get(keyId) instanceof Uint8Array;
}
function getPrivateKey(keyId) {
return secretStorageKeys.get(keyId);
}
export function clearSecretStorageKeys() {
secretStorageKeys.clear();
}
async function getSecretStorageKey({ keys }) {
const keyIds = Object.keys(keys);
const keyId = keyIds.find(hasPrivateKey);
if (!keyId) return undefined;
const privateKey = getPrivateKey(keyId);
return [keyId, privateKey];
}
function cacheSecretStorageKey(keyId, keyInfo, privateKey) {
secretStorageKeys.set(keyId, privateKey);
}
export const cryptoCallbacks = {
getSecretStorageKey,
cacheSecretStorageKey,
};
+52
View File
@@ -0,0 +1,52 @@
import type { CryptoCallbacks } from '$types/matrix-sdk';
const secretStorageKeys = new Map<string, Uint8Array>();
export function storePrivateKey(keyId: string, privateKey: Uint8Array): void {
if (privateKey instanceof Uint8Array === false) {
throw new Error('Unable to store, privateKey is invalid.');
}
secretStorageKeys.set(keyId, privateKey);
}
function hasPrivateKey(keyId: string): boolean {
return secretStorageKeys.get(keyId) instanceof Uint8Array;
}
function getPrivateKey(keyId: string): Uint8Array | undefined {
return secretStorageKeys.get(keyId);
}
export function clearSecretStorageKeys(): void {
secretStorageKeys.clear();
}
const getSecretStorageKey: NonNullable<CryptoCallbacks['getSecretStorageKey']> = async (
{ keys },
name
) => {
void name;
const keyIds = Object.keys(keys);
const keyId = keyIds.find(hasPrivateKey);
if (!keyId) return null;
const privateKey = getPrivateKey(keyId);
if (!privateKey) return null;
return [keyId, privateKey];
};
const cacheSecretStorageKey: NonNullable<CryptoCallbacks['cacheSecretStorageKey']> = (
keyId,
keyInfo,
privateKey
) => {
void keyInfo;
secretStorageKeys.set(keyId, privateKey);
};
export const cryptoCallbacks: Pick<
CryptoCallbacks,
'getSecretStorageKey' | 'cacheSecretStorageKey'
> = {
getSecretStorageKey,
cacheSecretStorageKey,
};