-
-
Notifications
You must be signed in to change notification settings - Fork 604
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Description: | ||
// | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||
userName: '********', | ||
password: '********', | ||
rawUrl: 'matrix.org', | ||
|
||
roomId: '********', | ||
msg: 'Hello World!' | ||
}; | ||
|
||
function log(msg) | ||
{ | ||
console.log(`${sLogPrefix}${msg}`); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); |
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" | ||
} | ||
} |
There was a problem hiding this comment.
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)