Skip to content

help with end to end encryption #4768

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

Closed
mcollina opened this issue Mar 28, 2025 · 2 comments
Closed

help with end to end encryption #4768

mcollina opened this issue Mar 28, 2025 · 2 comments

Comments

@mcollina
Copy link

I'm trying to create a small Matrix client in Node.js and boot E2EE.

This is where I got:

import * as sdk from "matrix-js-sdk";
const myUserId = process.env.MATRIX_USER;
const myAccessToken = process.env.MATRIX_ACCESS_TOKEN;
const client = sdk.createClient({
  baseUrl: "https://matrix.org",
  accessToken: myAccessToken,
  userId: myUserId,
  deviceId: "mydevice",
  cryptoCallbacks: {
    getSecretStorageKey: async (keys) => {
      return [Object.keys(keys.keys)[0], Buffer.from('foobar')];
    },
  },
});

await client.initRustCrypto({ useIndexedDB: false });

client.on(sdk.ClientEvent.Room, async (room) => {
  client.sendMessage(room.roomId, {
    msgtype: "m.text",
    body: "Hello, I'm a bot!",
  });
})


await client.startClient({ initialSyncLimit: 10 });

await client.getCrypto().bootstrapSecretStorage({
    // This function will be called if a new secret storage key (aka recovery key) is needed.
    // You should prompt the user to save the key somewhere, because they will need it to unlock secret storage in future.
    createSecretStorageKey: async (...args) => {
      return {
        keyInfo: {},
        privateKey: Buffer.from('foobar'),
      };
    },
});

await client.getCrypto().bootstrapCrossSigning({
  setupNewCrossSigning: true,
  authUploadDeviceSignatures: async (makeRequest) => {
    console.log('authUploadDeviceSignatures')
    return makeRequest({
      "type": "m.login.password",
      "user": myUserId,
      "password": process.env.MATRIX_PASSWORD,
    });
  }
});

Unfortunately this crashes with an error:

M_UNKNOWN: MatrixError: [400] One time key signed_curve25519:AAAAAAAAAA4 already exists. Old key: {"key":"XX+koCbzdmvfXl5C5Q0","signatures":{"@XXX:matrix.org":{"ed25519:AAA":"AAA"}}}; new key: {'key': 'BB', 'signatures': {'@VV:matrix.org': {'ed25519:mydevice': 'BBB'}}} (https://matrix.org/_matrix/client/v3/keys/upload)
    at parseErrorResponse (file:///Users/matteo/repos/mcbot/node_modules/matrix-js-sdk/lib/http-api/utils.js:78:12)
    at file:///Users/matteo/repos/mcbot/node_modules/matrix-js-sdk/lib/http-api/fetch.js:332:15
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/matteo/repos/mcbot/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17)
    at _next (/Users/matteo/repos/mcbot/node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  httpStatus: 400,
  httpHeaders: Headers {},
  url: 'https://matrix.org/_matrix/client/v3/keys/upload',
  event: undefined,
  errcode: 'M_UNKNOWN',
  data: {
    errcode: 'M_UNKNOWN',
    error: `One time key XXX already exists. Old key: {"key":"XX+koCbzdmvfXl5C5Q0","signatures":{"@XXX:matrix.org":{"ed25519:AAA":"AAA"}}}; new key: {'key': 'BB', 'signatures': {'@VV:matrix.org': {'ed25519:mydevice': 'BBB'}}}`
  }
}

Node.js v22.14.0

What am I missing?

I see there were a few asks for a complete example for E2EE (#4535 and others). That'd be really helpful.

@richvdh
Copy link
Member

richvdh commented Mar 28, 2025

This is not a support forum, but what you are missing is persistent storage for the crypto database, by setting useIndexedDB: false.

See README and #4769

@richvdh richvdh closed this as completed Mar 28, 2025
@mcollina
Copy link
Author

mcollina commented Mar 28, 2025

@richvdh as I wrote at the top, I'm trying to use this with Node.js, and as you pointed out in #4769, there is no support for IndexedDB right now in Node.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants