mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-04 07:42:24 -08:00
refactor: convert secret storage keys to typescript
This commit is contained in:
3 files changed
+52
-45
No files matched your search
Vendored
-8
@@ -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'
|
||||
>;
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
Reference in new issue
Block a user