You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
Rename ssss cache functions to be more general
This commit is contained in:
@@ -23,15 +23,10 @@ import {MatrixEvent} from "../../../src/models/event";
|
|||||||
import * as algorithms from "../../../src/crypto/algorithms";
|
import * as algorithms from "../../../src/crypto/algorithms";
|
||||||
import {WebStorageSessionStore} from "../../../src/store/session/webstorage";
|
import {WebStorageSessionStore} from "../../../src/store/session/webstorage";
|
||||||
import {MemoryCryptoStore} from "../../../src/crypto/store/memory-crypto-store";
|
import {MemoryCryptoStore} from "../../../src/crypto/store/memory-crypto-store";
|
||||||
import {
|
|
||||||
IndexedDBCryptoStore,
|
|
||||||
} from '../../../src/crypto/store/indexeddb-crypto-store';
|
|
||||||
import {MockStorageApi} from "../../MockStorageApi";
|
import {MockStorageApi} from "../../MockStorageApi";
|
||||||
import * as testUtils from "../../test-utils";
|
import * as testUtils from "../../test-utils";
|
||||||
import {OlmDevice} from "../../../src/crypto/OlmDevice";
|
import {OlmDevice} from "../../../src/crypto/OlmDevice";
|
||||||
import {Crypto} from "../../../src/crypto";
|
import {Crypto} from "../../../src/crypto";
|
||||||
import 'fake-indexeddb/auto';
|
|
||||||
import 'jest-localstorage-mock';
|
|
||||||
|
|
||||||
const Olm = global.Olm;
|
const Olm = global.Olm;
|
||||||
|
|
||||||
@@ -84,13 +79,6 @@ const BACKUP_INFO = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const testKey = new Uint8Array([
|
|
||||||
0xda, 0x5a, 0x27, 0x60, 0xe3, 0x3a, 0xc5, 0x82,
|
|
||||||
0x9d, 0x12, 0xc3, 0xbe, 0xe8, 0xaa, 0xc2, 0xef,
|
|
||||||
0xae, 0xb1, 0x05, 0xc1, 0xe7, 0x62, 0x78, 0xa6,
|
|
||||||
0xd7, 0x1f, 0xf8, 0x2c, 0x51, 0x85, 0xf0, 0x1d,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const keys = {};
|
const keys = {};
|
||||||
|
|
||||||
function getCrossSigningKey(type) {
|
function getCrossSigningKey(type) {
|
||||||
@@ -555,45 +543,3 @@ describe("MegolmBackup", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.each([
|
|
||||||
["IndexedDBCryptoStore",
|
|
||||||
() => new IndexedDBCryptoStore(global.indexedDB, "tests")],
|
|
||||||
["LocalStorageCryptoStore",
|
|
||||||
() => new IndexedDBCryptoStore(undefined, "tests")],
|
|
||||||
["MemoryCryptoStore", () => {
|
|
||||||
const store = new IndexedDBCryptoStore(undefined, "tests");
|
|
||||||
store._backend = new MemoryCryptoStore();
|
|
||||||
store._backendPromise = Promise.resolve(store._backend);
|
|
||||||
return store;
|
|
||||||
}],
|
|
||||||
])("Crypto store backup key cache functions [%s]", function(name, dbFactory) {
|
|
||||||
let store;
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
store = dbFactory();
|
|
||||||
await store.startup();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Stores and retrieves a backup key", async () => {
|
|
||||||
expect(store._backend).not.toBeNull();
|
|
||||||
|
|
||||||
await store.doTxn(
|
|
||||||
'readwrite',
|
|
||||||
[IndexedDBCryptoStore.STORE_ACCOUNT],
|
|
||||||
(txn) => {
|
|
||||||
store.storeBackupKey(txn, "m.megolm_backup.v1", testKey);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
const result = await new Promise((resolve) => {
|
|
||||||
store.doTxn(
|
|
||||||
'readonly',
|
|
||||||
[IndexedDBCryptoStore.STORE_ACCOUNT],
|
|
||||||
(txn) => {
|
|
||||||
store.getBackupKey(txn, resolve, "m.megolm_backup.v1");
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
expect(result).toEqual(testKey);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
@@ -585,7 +585,7 @@ export function createCryptoStoreCacheCallbacks(store) {
|
|||||||
'readonly',
|
'readonly',
|
||||||
[IndexedDBCryptoStore.STORE_ACCOUNT],
|
[IndexedDBCryptoStore.STORE_ACCOUNT],
|
||||||
(txn) => {
|
(txn) => {
|
||||||
store.getCrossSigningPrivateKey(txn, resolve, type);
|
store.getSecretStorePrivateKey(txn, resolve, type);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -595,7 +595,7 @@ export function createCryptoStoreCacheCallbacks(store) {
|
|||||||
'readwrite',
|
'readwrite',
|
||||||
[IndexedDBCryptoStore.STORE_ACCOUNT],
|
[IndexedDBCryptoStore.STORE_ACCOUNT],
|
||||||
(txn) => {
|
(txn) => {
|
||||||
store.storeCrossSigningPrivateKey(txn, type, key);
|
store.storeSecretStorePrivateKey(txn, type, key);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@@ -341,7 +341,7 @@ export class Backend {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getCrossSigningPrivateKey(txn, func, type) {
|
getSecretStorePrivateKey(txn, func, type) {
|
||||||
const objectStore = txn.objectStore("account");
|
const objectStore = txn.objectStore("account");
|
||||||
const getReq = objectStore.get(`ssss_cache:${type}`);
|
const getReq = objectStore.get(`ssss_cache:${type}`);
|
||||||
getReq.onsuccess = function() {
|
getReq.onsuccess = function() {
|
||||||
@@ -358,28 +358,11 @@ export class Backend {
|
|||||||
objectStore.put(keys, "crossSigningKeys");
|
objectStore.put(keys, "crossSigningKeys");
|
||||||
}
|
}
|
||||||
|
|
||||||
storeCrossSigningPrivateKey(txn, type, key) {
|
storeSecretStorePrivateKey(txn, type, key) {
|
||||||
const objectStore = txn.objectStore("account");
|
const objectStore = txn.objectStore("account");
|
||||||
objectStore.put(key, `ssss_cache:${type}`);
|
objectStore.put(key, `ssss_cache:${type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
getBackupKey(txn, func, type) {
|
|
||||||
const objectStore = txn.objectStore("account");
|
|
||||||
const getReq = objectStore.get(`backup_key_cache:${type}`);
|
|
||||||
getReq.onsuccess = function() {
|
|
||||||
try {
|
|
||||||
func(getReq.result || null);
|
|
||||||
} catch (e) {
|
|
||||||
abortWithException(txn, e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
storeBackupKey(txn, type, key) {
|
|
||||||
const objectStore = txn.objectStore("account");
|
|
||||||
objectStore.put(key, `backup_key_cache:${type}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Olm Sessions
|
// Olm Sessions
|
||||||
|
|
||||||
countEndToEndSessions(txn, func) {
|
countEndToEndSessions(txn, func) {
|
||||||
|
@@ -313,8 +313,8 @@ export class IndexedDBCryptoStore {
|
|||||||
* @param {function(string)} func Called with the private key
|
* @param {function(string)} func Called with the private key
|
||||||
* @param {string} type A key type
|
* @param {string} type A key type
|
||||||
*/
|
*/
|
||||||
getCrossSigningPrivateKey(txn, func, type) {
|
getSecretStorePrivateKey(txn, func, type) {
|
||||||
this._backend.getCrossSigningPrivateKey(txn, func, type);
|
this._backend.getSecretStorePrivateKey(txn, func, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -334,28 +334,8 @@ export class IndexedDBCryptoStore {
|
|||||||
* @param {string} type The type of cross-signing private key to store
|
* @param {string} type The type of cross-signing private key to store
|
||||||
* @param {string} key keys object as getCrossSigningKeys()
|
* @param {string} key keys object as getCrossSigningKeys()
|
||||||
*/
|
*/
|
||||||
storeCrossSigningPrivateKey(txn, type, key) {
|
storeSecretStorePrivateKey(txn, type, key) {
|
||||||
this._backend.storeCrossSigningPrivateKey(txn, type, key);
|
this._backend.storeSecretStorePrivateKey(txn, type, key);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve session backup keys from the store
|
|
||||||
* @param {*} txn An active transaction. See doTxn().
|
|
||||||
* @param {function(Uint8Array)} func Called with the private key
|
|
||||||
* @param {string} type A key type
|
|
||||||
*/
|
|
||||||
getBackupKey(txn, func, type) {
|
|
||||||
this._backend.getBackupKey(txn, func, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {*} txn
|
|
||||||
* @param {string} type A key type
|
|
||||||
* @param {Uint8Array} key A private key
|
|
||||||
*/
|
|
||||||
storeBackupKey(txn, type, key) {
|
|
||||||
this._backend.storeBackupKey(txn, type, key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Olm sessions
|
// Olm sessions
|
||||||
|
@@ -367,7 +367,7 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore {
|
|||||||
func(keys);
|
func(keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCrossSigningPrivateKey(txn, func, type) {
|
getSecretStorePrivateKey(txn, func, type) {
|
||||||
const key = getJsonItem(this.store, E2E_PREFIX + `ssss_cache.${type}`);
|
const key = getJsonItem(this.store, E2E_PREFIX + `ssss_cache.${type}`);
|
||||||
func(key ? Uint8Array.from(key) : key);
|
func(key ? Uint8Array.from(key) : key);
|
||||||
}
|
}
|
||||||
@@ -378,23 +378,12 @@ export class LocalStorageCryptoStore extends MemoryCryptoStore {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
storeCrossSigningPrivateKey(txn, type, key) {
|
storeSecretStorePrivateKey(txn, type, key) {
|
||||||
setJsonItem(
|
setJsonItem(
|
||||||
this.store, E2E_PREFIX + `ssss_cache.${type}`, Array.from(key),
|
this.store, E2E_PREFIX + `ssss_cache.${type}`, Array.from(key),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getBackupKey(txn, func, type) {
|
|
||||||
const key = getJsonItem(this.store, `backup_key_cache.${type}`);
|
|
||||||
func(key ? Uint8Array.from(key) : key);
|
|
||||||
}
|
|
||||||
|
|
||||||
storeBackupKey(txn, type, key) {
|
|
||||||
setJsonItem(
|
|
||||||
this.store, `backup_key_cache.${type}`, Array.from(key),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
doTxn(mode, stores, func) {
|
doTxn(mode, stores, func) {
|
||||||
return Promise.resolve(func(null));
|
return Promise.resolve(func(null));
|
||||||
}
|
}
|
||||||
|
@@ -257,7 +257,7 @@ export class MemoryCryptoStore {
|
|||||||
func(this._crossSigningKeys);
|
func(this._crossSigningKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCrossSigningPrivateKey(txn, func, type) {
|
getSecretStorePrivateKey(txn, func, type) {
|
||||||
const result = this._privateKeys[type];
|
const result = this._privateKeys[type];
|
||||||
return func(result || null);
|
return func(result || null);
|
||||||
}
|
}
|
||||||
@@ -266,19 +266,10 @@ export class MemoryCryptoStore {
|
|||||||
this._crossSigningKeys = keys;
|
this._crossSigningKeys = keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
storeCrossSigningPrivateKey(txn, type, key) {
|
storeSecretStorePrivateKey(txn, type, key) {
|
||||||
this._privateKeys[type] = key;
|
this._privateKeys[type] = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
getBackupKey(txn, func, type) {
|
|
||||||
const result = this._backupKeys[type];
|
|
||||||
return func(result || null);
|
|
||||||
}
|
|
||||||
|
|
||||||
storeBackupKey(txn, type, key) {
|
|
||||||
this._backupKeys[type] = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Olm Sessions
|
// Olm Sessions
|
||||||
|
|
||||||
countEndToEndSessions(txn, func) {
|
countEndToEndSessions(txn, func) {
|
||||||
|
Reference in New Issue
Block a user