28 lines
1.0 KiB
TypeScript
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);
|
||
|
|
},
|
||
|
|
};
|