Skip to content
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

E2EE text messaging example. #3754

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions examples/e2ee/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Description:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget license header and copyright attribution (to yourself)

//
// Example showing how to send an end-to-end-encrypted (E2EE) message via
// Matrix.

// Requirements:
//
// - A user must exist (enter credentials below).
// - A room must exist, where the existing user is a joined member (enter room
// ID, below).
//
// Tested with:
//
// - NodeJS v18.17.1
// - matrix-js-sdk v28.2.0
// - olm v3.1.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably wants to be a block comment?


import * as sdk from 'matrix-js-sdk';
import olm from 'olm';

const sLogPrefix = '*** MAIN ***: ',
sC = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does sC mean?

userName: '********',
password: '********',
rawUrl: 'matrix.org',

roomId: '********',
msg: 'Hello World!'
};

function log(msg)
{
console.log(`${sLogPrefix}${msg}`);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole log wrapper and prefix feels weird and distracting for an example, especially since the prefix is hardcoded.


let client = null;

globalThis.Olm = olm;

client = sdk.createClient({ baseUrl: `https://${sC.rawUrl}` });

const pwLoginRes = await client.loginWithPassword(sC.userName, sC.password);

client = sdk.createClient(
{
baseUrl: `https://${sC.rawUrl}`,
cryptoStore: new sdk.MemoryCryptoStore(),
deviceId: pwLoginRes.device_id,
accessToken: pwLoginRes.access_token,
userId: pwLoginRes.user_id
});

client.on(
'sync',
async (state/*, prevState, res*/) =>
{
log(`State \"${state}\" is reached.`);

if(state === 'PREPARED')
{
try
{
await client.setRoomEncryption(
sC.roomId,
{
algorithm: 'm.megolm.v1.aes-sha2' // Seems to be OK..
});
}
catch(roomEncErr)
{
log(`Error: \"${roomEncErr.message}\"!`);
process.exit(1);
}

// Marking all devices as verified:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but... why?

//
{
const room = client.getRoom(sC.roomId);
const encTargetMembRes =
await room.getEncryptionTargetMembers();
const userIds = encTargetMembRes.map(
roomMemb => roomMemb.userId);

const devInfoRes = await client.downloadKeys(userIds, false);
//
// Better use the function below, but it fails with
// NodeJS v18.17.1, matrix-js-sdk v28.2.0 and olm v3.1.5:
//
// const devInfoRes = await client.getUserDeviceInfo(
// userIds, true);

for(const [userId, devInfos] of devInfoRes)
{
for(const devId of devInfos.keys())
{
try
{
await client.setDeviceVerified(userId, devId, true);
}
catch(devVerifyErr)
{
log(`Error: \"${devVerifyErr.message}\"!`);
process.exit(1);
}
}
}
}

try
{
await client.sendTextMessage(sC.roomId, sC.msg);
}
catch(sendErr)
{
log(`Error: \"${sendErr.message}\"!`);
process.exit(1);
}

await client.stopClient();
await client.logout();
process.exit(0);
}
});

await client.initCrypto();
await client.startClient();
6 changes: 6 additions & 0 deletions examples/e2ee/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"matrix-js-sdk": "^28.2.0",
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.5.tgz"
}
}
Loading