matrix-org / matrix-org/matrix-js-sdk
How to verify a client writen in node js using verification phrase?
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.2k
- Forks
- 704
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 40
Description
I'm developing a matrix client with node & express.js. I need to do the following:
1. Connect to Matrix.
2. Enable e2e encryption.
3. Verify the logged in device (Current session).
4. Read messages of a specific room.
I did the following:
1. Connect to the home server and create client.
2. Login to the home server by username and password.
3. Re-create the client with new access token and device id.
4. Get a decoded code from the security phrase(Recovery key).
5. Now I need to verify the client with the recovery code instead of confirming emojis or scan a QR code (because I can't in my node client).
```
const express = require("express");
const {
createClient,
MatrixEvent,
MatrixEventEvent,
} = require("matrix-js-sdk");
const { logger } = require("matrix-js-sdk/lib/logger");
require("isomorphic-fetch");
const Olm = require("@matrix-org/olm");
const { levels } = require("loglevel");
const { log } = require("console");
const { SyncState } = require("matrix-js-sdk/lib/sync");
global.Olm = Olm;
//define
const homeserverUrl = "xxxxxxxxxxx";
let accessToken = "xxxxxxxxxxx";
const username = "xxxxxxxxxxx";
const password = "xxxxxxxxxxx";
const recoveryKey =
"xxxxxxxxxxx";
//init the node express server
const app = express();
const port = 3000;
//create the client
let client = createClient({
baseUrl: homeserverUrl,
});
//start the server
app.listen(port, () => {
console.log(`backend client is running on port ${port}`);
});
client
.login("m.login.password", {
user: username,
password: password,
})
.then(async (response) => {
accessToken = response.access_token;
deviceId = response.device_id;
await client.setDeviceDetails(deviceId, {
display_name: `Test Device ${Math.floor(Math.random() * 101)}`,
});
client = createClient({
baseUrl: homeserverUrl,
userId: username,
deviceId: deviceId,
accessToken: accessToken,
});
await client.initCrypto();
fetchDecodedKey().then(async (keyInfo) => {
const decodedKey = client.keyBackupKeyFromRecoveryKey(recoveryKey);
const correct = await client.checkSecretStorageKey(
decodedKey,
keyInfo[1]
);
if (correct) {
client.startClient();
}
});
})
.catch((err) => {
console.error("Failed to log in:", err);
});
client.once("sync", (state, prevState, data) => {
const rooms = client.getRooms();
log(rooms, "!frFFTrSpBpfnvFBsgP:homeserver.com");
const _room = client.getRoom("!frFFTrSpBpfnvFBsgP:homeserver.com");
log(_room);
client
.setRoomEncryption(_room.roomId, {
decryption_key: decodedKey,
})
.then(async () => {
log("done");
})
.catch((error) => {
console.error("Failed to enable encryption:", error);
});
});
client.on("Room.timeline", (event, room, toStartOfTimeline) => {
console.log("Received event:", event);
});
//exit server
process.on("SIGINT", () => {
console.log("Server is shutting down...");
client
.logout()
.then(() => {
console.log("Client logged out successfully.");
process.exit();
})
.catch((error) => {
console.error("Failed to log out client:", error);
process.exit();
});
});
//secret fetcher.
const fetchDecodedKey = async () => {
const key = await client.getDefaultSecretStorageKeyId();
const secret = await client.crypto?.getSecretStorageKey(key);
return secret;
};
```
Help me to verify the newly created device, please.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the matrix-js-sdk entry points shown in the issue: initCrypto(), keyBackupKeyFromRecoveryKey(), checkSecretStorageKey(), and startClient(). Review the SDK's device-verification and secret-storage behavior to determine whether a recovery key can verify the newly created device; done means identifying a supported flow or documenting the missing capability.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- express, javascript, node.js, typescript
- Domain
- authentication, backend, security
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100