Created
October 20, 2021 01:22
-
-
Save ara4n/b3fadd728ffe6df48fb135f8be09fec9 to your computer and use it in GitHub Desktop.
Test jig for sending encrypted to-device messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import olm from "olm"; | |
import sdk from "matrix-js-sdk"; | |
import { LocalStorage } from "node-localstorage"; | |
global.Olm = olm; | |
const myUserId = "@matthewtest94:matrix.org"; | |
const myAccessToken = "secret"; | |
const matrixClient = sdk.createClient({ | |
baseUrl: "http://matrix.org", | |
accessToken: myAccessToken, | |
userId: myUserId, | |
deviceId: "JKGRJUVTBL", | |
sessionStore: new sdk.WebStorageSessionStore(new LocalStorage("./scratch")), | |
cryptoStore: new sdk.MemoryCryptoStore(), | |
}); | |
await matrixClient.initCrypto(); | |
await matrixClient.startClient(); | |
const targetUserId = "@matthewtest9:matrix.org"; | |
await matrixClient.crypto.downloadKeys([targetUserId]); | |
const devices = await matrixClient.crypto.getStoredDevicesForUser(targetUserId); | |
const userDeviceMap = []; | |
for (const device of devices) { | |
userDeviceMap.push({ userId: targetUserId, deviceInfo: device }); | |
} | |
await matrixClient.crypto.encryptAndSendToDevices( | |
userDeviceMap, | |
{ | |
"type": "org.matrix.test", | |
"contents": { | |
"body": "i am a fish", | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment