Skip to content

Instantly share code, notes, and snippets.

@ara4n
Created October 20, 2021 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ara4n/b3fadd728ffe6df48fb135f8be09fec9 to your computer and use it in GitHub Desktop.
Save ara4n/b3fadd728ffe6df48fb135f8be09fec9 to your computer and use it in GitHub Desktop.
Test jig for sending encrypted to-device messages
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