-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Element-R: wire up device lists (#3272)
* Add `getUserDeviceInfo` to `CryptoBackend` and old crypto impl * Add `getUserDeviceInfo` WIP impl to `rust-crypto` * Add tests for `downloadUncached` * WIP test * Fix typo and use `downloadDeviceToJsDevice` * Add `getUserDeviceInfo` to `client.ts` * Use new `Device` class instead of `IDevice` * Add tests for `device-convertor` * Add method description for `isInRustUserIds` in `rust-crypto.ts` * Misc * Fix typo * Fix `rustDeviceToJsDevice` * Fix comments and new one * Review of `device.ts` * Remove `getUserDeviceInfo` from `client.ts` * Review of `getUserDeviceInfo` in `rust-crypto.ts` * Fix typo in `index.ts` * Review `device-converter.ts` * Add documentation to `getUserDeviceInfo` in `crypto-api.ts` * Last changes in comments
- Loading branch information
1 parent
63dea59
commit fbb1c4b
Showing
11 changed files
with
697 additions
and
8 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { DeviceInfo } from "../../../src/crypto/deviceinfo"; | ||
import { DeviceVerification } from "../../../src"; | ||
import { deviceInfoToDevice } from "../../../src/crypto/device-converter"; | ||
|
||
describe("device-converter", () => { | ||
const userId = "@alice:example.com"; | ||
const deviceId = "xcvf"; | ||
|
||
// All parameters for DeviceInfo initialization | ||
const keys = { | ||
[`ed25519:${deviceId}`]: "key1", | ||
[`curve25519:${deviceId}`]: "key2", | ||
}; | ||
const algorithms = ["algo1", "algo2"]; | ||
const verified = DeviceVerification.Verified; | ||
const signatures = { [userId]: { [deviceId]: "sign1" } }; | ||
const displayName = "display name"; | ||
const unsigned = { | ||
device_display_name: displayName, | ||
}; | ||
|
||
describe("deviceInfoToDevice", () => { | ||
it("should convert a DeviceInfo to a Device", () => { | ||
const deviceInfo = DeviceInfo.fromStorage({ keys, algorithms, verified, signatures, unsigned }, deviceId); | ||
const device = deviceInfoToDevice(deviceInfo, userId); | ||
|
||
expect(device.deviceId).toBe(deviceId); | ||
expect(device.userId).toBe(userId); | ||
expect(device.verified).toBe(verified); | ||
expect(device.getIdentityKey()).toBe(keys[`curve25519:${deviceId}`]); | ||
expect(device.getFingerprint()).toBe(keys[`ed25519:${deviceId}`]); | ||
expect(device.displayName).toBe(displayName); | ||
}); | ||
|
||
it("should add empty signatures", () => { | ||
const deviceInfo = DeviceInfo.fromStorage({ keys, algorithms, verified }, deviceId); | ||
const device = deviceInfoToDevice(deviceInfo, userId); | ||
|
||
expect(device.signatures.size).toBe(0); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { DeviceKeys, DeviceVerification } from "../../../src"; | ||
import { downloadDeviceToJsDevice } from "../../../src/rust-crypto/device-converter"; | ||
|
||
describe("device-converter", () => { | ||
const userId = "@alice:example.com"; | ||
const deviceId = "xcvf"; | ||
|
||
// All parameters for QueryDevice initialization | ||
const keys = { | ||
[`ed25519:${deviceId}`]: "key1", | ||
[`curve25519:${deviceId}`]: "key2", | ||
}; | ||
const algorithms = ["algo1", "algo2"]; | ||
const signatures = { [userId]: { [deviceId]: "sign1" } }; | ||
const displayName = "display name"; | ||
const unsigned = { | ||
device_display_name: displayName, | ||
}; | ||
|
||
describe("downloadDeviceToJsDevice", () => { | ||
it("should convert a QueryDevice to a Device", () => { | ||
const queryDevice: DeviceKeys[keyof DeviceKeys] = { | ||
keys, | ||
algorithms, | ||
device_id: deviceId, | ||
user_id: userId, | ||
signatures, | ||
unsigned, | ||
}; | ||
const device = downloadDeviceToJsDevice(queryDevice); | ||
|
||
expect(device.deviceId).toBe(deviceId); | ||
expect(device.userId).toBe(userId); | ||
expect(device.verified).toBe(DeviceVerification.Unverified); | ||
expect(device.getIdentityKey()).toBe(keys[`curve25519:${deviceId}`]); | ||
expect(device.getFingerprint()).toBe(keys[`ed25519:${deviceId}`]); | ||
expect(device.displayName).toBe(displayName); | ||
}); | ||
|
||
it("should add empty signatures", () => { | ||
const queryDevice: DeviceKeys[keyof DeviceKeys] = { | ||
keys, | ||
algorithms, | ||
device_id: deviceId, | ||
user_id: userId, | ||
}; | ||
const device = downloadDeviceToJsDevice(queryDevice); | ||
|
||
expect(device.signatures.size).toBe(0); | ||
}); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { Device } from "../models/device"; | ||
import { DeviceInfo } from "./deviceinfo"; | ||
|
||
/** | ||
* Convert a {@link DeviceInfo} to a {@link Device}. | ||
* @param deviceInfo - deviceInfo to convert | ||
* @param userId - id of the user that owns the device. | ||
*/ | ||
export function deviceInfoToDevice(deviceInfo: DeviceInfo, userId: string): Device { | ||
const keys = new Map<string, string>(Object.entries(deviceInfo.keys)); | ||
const displayName = deviceInfo.getDisplayName() || undefined; | ||
|
||
const signatures = new Map<string, Map<string, string>>(); | ||
if (deviceInfo.signatures) { | ||
for (const userId in deviceInfo.signatures) { | ||
signatures.set(userId, new Map(Object.entries(deviceInfo.signatures[userId]))); | ||
} | ||
} | ||
|
||
return new Device({ | ||
deviceId: deviceInfo.deviceId, | ||
userId: userId, | ||
keys, | ||
algorithms: deviceInfo.algorithms, | ||
verified: deviceInfo.verified, | ||
signatures, | ||
displayName, | ||
}); | ||
} |
Oops, something went wrong.