File: olm-device-export-import.js

package info (click to toggle)
node-matrix-js-sdk 9.3.0%2B~cs9.9.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,904 kB
  • sloc: javascript: 39,573; sh: 275; makefile: 31; python: 11
file content (122 lines) | stat: -rw-r--r-- 4,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
if (!Olm) {
    console.error(
        "global.Olm does not seem to be present."
        + " Did you forget to add olm in the lib/ directory?"
    );
}

const BASE_URL = 'http://localhost:8008';
const ROOM_CRYPTO_CONFIG = { algorithm: 'm.megolm.v1.aes-sha2' };
const PASSWORD = 'password';

// useful to create new usernames
window.randomHex = () => Math.floor(Math.random() * (10**6)).toString(16);

window.newMatrixClient = async function (username) {
    const registrationClient = matrixcs.createClient(BASE_URL);

    const userRegisterResult = await registrationClient.register(
      username,
      PASSWORD,
      null,
      { type: 'm.login.dummy' }
    );
  
    const matrixClient = matrixcs.createClient({
      baseUrl: BASE_URL,
      userId: userRegisterResult.user_id,
      accessToken: userRegisterResult.access_token,
      deviceId: userRegisterResult.device_id,
      sessionStore: new matrixcs.WebStorageSessionStore(window.localStorage),
      cryptoStore: new matrixcs.MemoryCryptoStore(),
    });

    extendMatrixClient(matrixClient);

    await matrixClient.initCrypto();
    await matrixClient.startClient();
    return matrixClient;
}

window.importMatrixClient = async function (exportedDevice, accessToken) {
    const matrixClient = matrixcs.createClient({
      baseUrl: BASE_URL,
      deviceToImport: exportedDevice,
      accessToken,
      sessionStore: new matrixcs.WebStorageSessionStore(window.localStorage),
      cryptoStore: new matrixcs.MemoryCryptoStore(),
    });

    extendMatrixClient(matrixClient);

    await matrixClient.initCrypto();
    await matrixClient.startClient();
    return matrixClient;
}

function extendMatrixClient(matrixClient) {
    // automatic join
    matrixClient.on('RoomMember.membership', async (event, member) => {
        if (member.membership === 'invite' && member.userId === matrixClient.getUserId()) {
            await matrixClient.joinRoom(member.roomId);
            // setting up of room encryption seems to be triggered automatically
            // but if we don't wait for it the first messages we send are unencrypted
            await matrixClient.setRoomEncryption(member.roomId, { algorithm: 'm.megolm.v1.aes-sha2' })
        }
    });

    matrixClient.onDecryptedMessage = message => {
        console.log('Got encrypted message: ', message);
    }

    matrixClient.on('Event.decrypted', (event) => {
        if (event.getType() === 'm.room.message'){
            matrixClient.onDecryptedMessage(event.getContent().body);
        } else {
            console.log('decrypted an event of type', event.getType());
            console.log(event);
        }
    });
  
    matrixClient.createEncryptedRoom = async function(usersToInvite) {
        const {
          room_id: roomId,
        } = await this.createRoom({
          visibility: 'private',
          invite: usersToInvite,
        });

        // matrixClient.setRoomEncryption() only updates local state
        // but does not send anything to the server
        // (see https://github.com/matrix-org/matrix-js-sdk/issues/905)
        // so we do it ourselves with 'sendStateEvent'
        await this.sendStateEvent(
          roomId, 'm.room.encryption', ROOM_CRYPTO_CONFIG,
        );
        await this.setRoomEncryption(
          roomId, ROOM_CRYPTO_CONFIG,
        );

        // Marking all devices as verified
        let room = this.getRoom(roomId);
        let members = (await room.getEncryptionTargetMembers()).map(x => x["userId"])
        let memberkeys = await this.downloadKeys(members);
        for (const userId in memberkeys) {
          for (const deviceId in memberkeys[userId]) {
            await this.setDeviceVerified(userId, deviceId);
          }
        }

        return roomId;
    }

    matrixClient.sendTextMessage = async function(message, roomId) {
        return matrixClient.sendMessage(
            roomId,
            {
                body: message,
                msgtype: 'm.text',
            }
        )
    }
}