diff --git a/src/client/secretStorageKeys.d.ts b/src/client/secretStorageKeys.d.ts deleted file mode 100644 index 9b14490f6..000000000 --- a/src/client/secretStorageKeys.d.ts +++ /dev/null @@ -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' ->; diff --git a/src/client/secretStorageKeys.js b/src/client/secretStorageKeys.js deleted file mode 100644 index 78c0cb4cb..000000000 --- a/src/client/secretStorageKeys.js +++ /dev/null @@ -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, -}; diff --git a/src/client/secretStorageKeys.ts b/src/client/secretStorageKeys.ts new file mode 100644 index 000000000..cfa45a5d4 --- /dev/null +++ b/src/client/secretStorageKeys.ts @@ -0,0 +1,52 @@ +import type { CryptoCallbacks } from '$types/matrix-sdk'; + +const secretStorageKeys = new Map(); + +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 = 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 = ( + keyId, + keyInfo, + privateKey +) => { + void keyInfo; + secretStorageKeys.set(keyId, privateKey); +}; + +export const cryptoCallbacks: Pick< + CryptoCallbacks, + 'getSecretStorageKey' | 'cacheSecretStorageKey' +> = { + getSecretStorageKey, + cacheSecretStorageKey, +};