Skip to content

Commit 8367277

Browse files
authored
Allow customizing the IndexedDB database prefix used by Rust crypto. (#4878)
* Allow customizing the IndexedDB database prefix used by Rust crypto. Related to #3974 Signed-off-by: Patrick Cloke <[email protected]> * Rename argument --------- Signed-off-by: Patrick Cloke <[email protected]>
1 parent 4efb273 commit 8367277

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

spec/integ/crypto/rust-crypto.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ describe("MatrixClient.initRustCrypto", () => {
6767
expect(databaseNames).toEqual(expect.arrayContaining(["matrix-js-sdk::matrix-sdk-crypto"]));
6868
});
6969

70+
it("should create the indexed db with a custom prefix", async () => {
71+
const matrixClient = createClient({
72+
baseUrl: "http://test.server",
73+
userId: "@alice:localhost",
74+
deviceId: "aliceDevice",
75+
});
76+
77+
// No databases.
78+
expect(await indexedDB.databases()).toHaveLength(0);
79+
80+
await matrixClient.initRustCrypto({ cryptoDatabasePrefix: "my-prefix" });
81+
82+
// should have an indexed db now
83+
const databaseNames = (await indexedDB.databases()).map((db) => db.name);
84+
expect(databaseNames).toEqual(expect.arrayContaining(["my-prefix::matrix-sdk-crypto"]));
85+
});
86+
7087
it("should create the meta db if given a storageKey", async () => {
7188
const matrixClient = createClient({
7289
baseUrl: "http://test.server",
@@ -475,4 +492,22 @@ describe("MatrixClient.clearStores", () => {
475492
await matrixClient.clearStores();
476493
// No error thrown in clearStores
477494
});
495+
496+
it("should clear the indexeddbs with a custom prefix", async () => {
497+
const matrixClient = createClient({
498+
baseUrl: "http://test.server",
499+
userId: "@alice:localhost",
500+
deviceId: "aliceDevice",
501+
});
502+
503+
// No databases.
504+
expect(await indexedDB.databases()).toHaveLength(0);
505+
506+
await matrixClient.initRustCrypto({ cryptoDatabasePrefix: "my-prefix" });
507+
expect(await indexedDB.databases()).toHaveLength(1);
508+
await matrixClient.stopClient();
509+
510+
await matrixClient.clearStores({ cryptoDatabasePrefix: "my-prefix" });
511+
expect(await indexedDB.databases()).toHaveLength(0);
512+
});
478513
});

src/client.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,9 +1527,14 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
15271527
/**
15281528
* Clear any data out of the persistent stores used by the client.
15291529
*
1530+
* @param args.cryptoDatabasePrefix - The database name to use for indexeddb, defaults to 'matrix-js-sdk'.
15301531
* @returns Promise which resolves when the stores have been cleared.
15311532
*/
1532-
public clearStores(): Promise<void> {
1533+
public clearStores(
1534+
args: {
1535+
cryptoDatabasePrefix?: string;
1536+
} = {},
1537+
): Promise<void> {
15331538
if (this.clientRunning) {
15341539
throw new Error("Cannot clear stores while client is running");
15351540
}
@@ -1552,8 +1557,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
15521557
return;
15531558
}
15541559
for (const dbname of [
1555-
`${RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto`,
1556-
`${RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto-meta`,
1560+
`${args.cryptoDatabasePrefix ?? RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto`,
1561+
`${args.cryptoDatabasePrefix ?? RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto-meta`,
15571562
]) {
15581563
const prom = new Promise((resolve, reject) => {
15591564
this.logger.info(`Removing IndexedDB instance ${dbname}`);
@@ -1901,6 +1906,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
19011906
* ensuring that only one `MatrixClient` issue is instantiated at a time.
19021907
*
19031908
* @param args.useIndexedDB - True to use an indexeddb store, false to use an in-memory store. Defaults to 'true'.
1909+
* @param args.cryptoDatabasePrefix - The database name to use for indexeddb, defaults to 'matrix-js-sdk'.
1910+
* Unused if useIndexedDB is 'false'.
19041911
* @param args.storageKey - A key with which to encrypt the indexeddb store. If provided, it must be exactly
19051912
* 32 bytes of data, and must be the same each time the client is initialised for a given device.
19061913
* If both this and `storagePassword` are unspecified, the store will be unencrypted.
@@ -1914,6 +1921,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
19141921
public async initRustCrypto(
19151922
args: {
19161923
useIndexedDB?: boolean;
1924+
cryptoDatabasePrefix?: string;
19171925
storageKey?: Uint8Array;
19181926
storagePassword?: string;
19191927
} = {},
@@ -1950,7 +1958,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
19501958
deviceId: deviceId,
19511959
secretStorage: this.secretStorage,
19521960
cryptoCallbacks: this.cryptoCallbacks,
1953-
storePrefix: args.useIndexedDB === false ? null : RUST_SDK_STORE_PREFIX,
1961+
storePrefix: args.useIndexedDB === false ? null : (args.cryptoDatabasePrefix ?? RUST_SDK_STORE_PREFIX),
19541962
storeKey: args.storageKey,
19551963
storePassphrase: args.storagePassword,
19561964

0 commit comments

Comments
 (0)