Skip to content

Commit

Permalink
Pass HttpApi into RustCrypto
Browse files Browse the repository at this point in the history
... so that we can make outbound requests.
  • Loading branch information
richvdh committed Jan 4, 2023
1 parent 6168ced commit 0ebea8b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion spec/unit/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { IDBFactory } from "fake-indexeddb";

import { RustCrypto } from "../../src/rust-crypto/rust-crypto";
import { initRustCrypto } from "../../src/rust-crypto";
import { IHttpOpts, MatrixHttpApi } from "../../src";

afterEach(() => {
// reset fake-indexeddb after each test, to make sure we don't leak connections
Expand All @@ -34,7 +35,8 @@ describe("RustCrypto", () => {
let rustCrypto: RustCrypto;

beforeEach(async () => {
rustCrypto = (await initRustCrypto(TEST_USER, TEST_DEVICE_ID)) as RustCrypto;
const mockHttpApi = {} as MatrixHttpApi<IHttpOpts>;
rustCrypto = (await initRustCrypto(mockHttpApi, TEST_USER, TEST_DEVICE_ID)) as RustCrypto;
});

describe(".exportRoomKeys", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// importing rust-crypto will download the webassembly, so we delay it until we know it will be
// needed.
const RustCrypto = await import("./rust-crypto");
this.cryptoBackend = await RustCrypto.initRustCrypto(userId, deviceId);
this.cryptoBackend = await RustCrypto.initRustCrypto(this.http, userId, deviceId);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/rust-crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import { RustCrypto } from "./rust-crypto";
import { logger } from "../logger";
import { CryptoBackend } from "../common-crypto/CryptoBackend";
import { RUST_SDK_STORE_PREFIX } from "./constants";
import { IHttpOpts, MatrixHttpApi } from "../http-api";

export async function initRustCrypto(userId: string, deviceId: string): Promise<CryptoBackend> {
export async function initRustCrypto(
http: MatrixHttpApi<IHttpOpts>,
userId: string,
deviceId: string,
): Promise<CryptoBackend> {
// initialise the rust matrix-sdk-crypto-js, if it hasn't already been done
await RustSdkCryptoJs.initAsync();

Expand All @@ -34,7 +39,7 @@ export async function initRustCrypto(userId: string, deviceId: string): Promise<

// 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);
const rustCrypto = new RustCrypto(olmMachine, http, userId, deviceId);

logger.info("Completed rust crypto-sdk setup");
return rustCrypto;
Expand Down
8 changes: 7 additions & 1 deletion src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js";
import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto";
import { MatrixEvent } from "../models/event";
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
import { IHttpOpts, MatrixHttpApi } from "../http-api";

// import { logger } from "../logger";

Expand All @@ -32,7 +33,12 @@ export class RustCrypto implements CryptoBackend {
/** whether stop() has been called */
private stopped = false;

public constructor(private readonly olmMachine: RustSdkCryptoJs.OlmMachine, _userId: string, _deviceId: string) {}
public constructor(
private readonly olmMachine: RustSdkCryptoJs.OlmMachine,
private readonly http: MatrixHttpApi<IHttpOpts>,
_userId: string,
_deviceId: string,
) {}

public stop(): void {
// stop() may be called multiple times, but attempting to close() the OlmMachine twice
Expand Down

0 comments on commit 0ebea8b

Please sign in to comment.