-
-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a mechanism for using the rust-crypto-sdk #2969
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
60f642f
Implement rust-crypto
richvdh c0adbf3
Add dependency on matrix-sdk-crypto-js
richvdh dbb07f6
improve tsdoc
richvdh c33afe2
Update src/client.ts
richvdh 2893717
add an @experimental tag
richvdh d4a54b2
Undo `yarn.lock` mangling
richvdh df8d44b
Use a constant for the indexeddb prefix
richvdh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import "fake-indexeddb/auto"; | ||
import { IDBFactory } from "fake-indexeddb"; | ||
|
||
import { createClient } from "../../src"; | ||
|
||
afterEach(() => { | ||
// reset fake-indexeddb after each test, to make sure we don't leak connections | ||
// cf https://github.com/dumbmatter/fakeIndexedDB#wipingresetting-the-indexeddb-for-a-fresh-state | ||
// eslint-disable-next-line no-global-assign | ||
indexedDB = new IDBFactory(); | ||
}); | ||
|
||
describe("MatrixClient.initRustCrypto", () => { | ||
it("should raise if userId or deviceId is unknown", async () => { | ||
const unknownUserClient = createClient({ | ||
baseUrl: "http://test.server", | ||
deviceId: "aliceDevice", | ||
}); | ||
await expect(() => unknownUserClient.initRustCrypto()).rejects.toThrow("unknown userId"); | ||
|
||
const unknownDeviceClient = createClient({ | ||
baseUrl: "http://test.server", | ||
userId: "@alice:test", | ||
}); | ||
await expect(() => unknownDeviceClient.initRustCrypto()).rejects.toThrow("unknown deviceId"); | ||
}); | ||
|
||
it("should create the indexed dbs", async () => { | ||
const matrixClient = createClient({ | ||
baseUrl: "http://test.server", | ||
userId: "@alice:localhost", | ||
deviceId: "aliceDevice", | ||
}); | ||
|
||
// No databases. | ||
expect(await indexedDB.databases()).toHaveLength(0); | ||
|
||
await matrixClient.initRustCrypto(); | ||
|
||
// should have two dbs now | ||
const databaseNames = (await indexedDB.databases()).map((db) => db.name); | ||
expect(databaseNames).toEqual( | ||
expect.arrayContaining(["matrix-js-sdk::matrix-sdk-crypto", "matrix-js-sdk::matrix-sdk-crypto-meta"]), | ||
); | ||
}); | ||
|
||
it("should ignore a second call", async () => { | ||
const matrixClient = createClient({ | ||
baseUrl: "http://test.server", | ||
userId: "@alice:localhost", | ||
deviceId: "aliceDevice", | ||
}); | ||
|
||
await matrixClient.initRustCrypto(); | ||
await matrixClient.initRustCrypto(); | ||
}); | ||
}); | ||
|
||
describe("MatrixClient.clearStores", () => { | ||
it("should clear the indexeddbs", async () => { | ||
const matrixClient = createClient({ | ||
baseUrl: "http://test.server", | ||
userId: "@alice:localhost", | ||
deviceId: "aliceDevice", | ||
}); | ||
|
||
await matrixClient.initRustCrypto(); | ||
expect(await indexedDB.databases()).toHaveLength(2); | ||
await matrixClient.stopClient(); | ||
|
||
await matrixClient.clearStores(); | ||
expect(await indexedDB.databases()).toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** The prefix used on indexeddbs created by rust-crypto */ | ||
export const RUST_SDK_STORE_PREFIX = "matrix-js-sdk"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js"; | ||
|
||
import { RustCrypto } from "./rust-crypto"; | ||
import { logger } from "../logger"; | ||
import { CryptoBackend } from "../common-crypto/CryptoBackend"; | ||
import { RUST_SDK_STORE_PREFIX } from "./constants"; | ||
|
||
export async function initRustCrypto(userId: string, deviceId: string): Promise<CryptoBackend> { | ||
// initialise the rust matrix-sdk-crypto-js, if it hasn't already been done | ||
await RustSdkCryptoJs.initAsync(); | ||
|
||
// enable tracing in the rust-sdk | ||
new RustSdkCryptoJs.Tracing(RustSdkCryptoJs.LoggerLevel.Debug).turnOn(); | ||
|
||
const u = new RustSdkCryptoJs.UserId(userId); | ||
const d = new RustSdkCryptoJs.DeviceId(deviceId); | ||
logger.info("Init OlmMachine"); | ||
|
||
// TODO: use the pickle key for the passphrase | ||
const olmMachine = await RustSdkCryptoJs.OlmMachine.initialize(u, d, RUST_SDK_STORE_PREFIX, "test pass"); | ||
const rustCrypto = new RustCrypto(olmMachine, userId, deviceId); | ||
|
||
logger.info("Completed rust crypto-sdk setup"); | ||
return rustCrypto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js"; | ||
|
||
import { IEventDecryptionResult } from "../@types/crypto"; | ||
import { MatrixEvent } from "../models/event"; | ||
import { CryptoBackend } from "../common-crypto/CryptoBackend"; | ||
|
||
// import { logger } from "../logger"; | ||
|
||
/** | ||
* An implementation of {@link CryptoBackend} using the Rust matrix-sdk-crypto. | ||
*/ | ||
export class RustCrypto implements CryptoBackend { | ||
public globalBlacklistUnverifiedDevices = false; | ||
public globalErrorOnUnknownDevices = false; | ||
|
||
/** whether stop() has been called */ | ||
private stopped = false; | ||
|
||
public constructor(private readonly olmMachine: RustSdkCryptoJs.OlmMachine, _userId: string, _deviceId: string) {} | ||
|
||
public stop(): void { | ||
// stop() may be called multiple times, but attempting to close() the OlmMachine twice | ||
// will cause an error. | ||
if (this.stopped) { | ||
return; | ||
} | ||
this.stopped = true; | ||
|
||
// make sure we close() the OlmMachine; doing so means that all the Rust objects will be | ||
// cleaned up; in particular, the indexeddb connections will be closed, which means they | ||
// can then be deleted. | ||
this.olmMachine.close(); | ||
} | ||
|
||
public async decryptEvent(event: MatrixEvent): Promise<IEventDecryptionResult> { | ||
await this.olmMachine.decryptRoomEvent("event", new RustSdkCryptoJs.RoomId("room")); | ||
throw new Error("not implemented"); | ||
} | ||
|
||
public async userHasCrossSigningKeys(): Promise<boolean> { | ||
// TODO | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or whatever jsdoc wants to say this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively, apparently
@internal
will exclude it from public types, despite being public itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's not exported explicitly, and it's only dynamically referenced, so it doesn't actually appear in the generated documentation.