matrix-org / matrix-org/matrix-js-sdk

Calling `initRustCrypto` Causes TypeError

Open
#5,169 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-Element-R O-Frequent S-Minor T-Defect
Dominant language
TypeScript
Stars
2.2k
Forks
704
Avg merge
1d 20h
Merged PRs (30d)
40

Description

I'm writing an HTML page and want to enable e2ee. I've read and re-read the docs and some other implementations and I cannot figure out what I'm doing wrong here.

I've defined the following function to set up a matrix client, and initialize the rust crypto module. I haven't included either the `store` or `cryptoStore` parameters, as I don't have any legacy data to migrate.
```ts
export const initClient = async (session: Session): Promise => {
console.log("Initializing matrix", session);

const client = createClient({
baseUrl: session.baseUrl,
accessToken: session.accessToken,
userId: session.userId,
deviceId: session.deviceId,
timelineSupport: true,
useAuthorizationHeader: true,
verificationMethods: ["m.sas.v1"],
});

await client.initRustCrypto(); // Throws type error, despite the lack of legacy data

client.setMaxListeners(50);

return client;
};
```

When `initRustCrypto` is called, it throws the following TypeError:
```
TypeError: Cannot read properties of undefined (reading 'getMigrationState')
at _IndexedDBCryptoStore.getMigrationState
```

It appears to be being thrown here, which is SUPER odd.
```ts
// node_modules/matrix-js-sdk/src/rust-crypto/libolm_migration.ts
getMigrationState() {
return this.backend.getMigrationState();
}
```

Final thing that's happening is this **only happens on a page reload**, after successfully signing in and caching the session information. I've verified that I'm passing in the correct userId/deviceId/accessToken on the second sign in attempt.

Even if I pass a legacy crypto store it still hits the error!

```ts
const cryptoStore = new IndexedDBCryptoStore(window.indexedDB, "crypto-store");

const client = createClient({
baseUrl: session.baseUrl,
accessToken: session.accessToken,
userId: session.userId,
deviceId: session.deviceId,
timelineSupport: true,
cryptoStore: cryptoStore,
useAuthorizationHeader: true,
verificationMethods: ["m.sas.v1"],
});

await client.initRustCrypto(); // Still throws
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with client.initRustCrypto and libolm_migration.ts, especially IndexedDBCryptoStore.getMigrationState, then reproduce the reported flow across a page reload with and without cryptoStore. Confirm the failure and trace which initialization state is missing; done means the reload path completes without the TypeError and has regression coverage for both configurations.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.