Skip to content

Commit

Permalink
Review of device.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Apr 21, 2023
1 parent d8a78c7 commit 2b57d0e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
17 changes: 8 additions & 9 deletions spec/unit/device-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { DeviceInfo, DeviceVerification } from "../../src/crypto/deviceinfo";
import { DeviceKeys } from "../../src";
import { DeviceInfo } from "../../src/crypto/deviceinfo";
import { DeviceKeys, DeviceVerification } from "../../src";
import { downloadDeviceToJsDevice } from "../../src/rust-crypto/device-converter";
import { deviceInfoToDevice } from "../../src/crypto/device-converter";

Expand All @@ -31,8 +31,9 @@ describe("device-converter", () => {
const algorithms = ["algo1", "algo2"];
const verified = DeviceVerification.Verified;
const signatures = { [userId]: { [deviceId]: "sign1" } };
const displayName = "display name";
const unsigned = {
device_display_name: "display name",
device_display_name: displayName,
};

describe("deviceInfoToDevice", () => {
Expand All @@ -45,14 +46,13 @@ describe("device-converter", () => {
expect(device.verified).toBe(verified);
expect(device.getIdentityKey()).toBe(keys[`curve25519:${deviceId}`]);
expect(device.getFingerprint()).toBe(keys[`ed25519:${deviceId}`]);
expect(device.getDisplayName()).toBe(unsigned.device_display_name);
expect(device.displayName).toBe(displayName);
});

it("should add empty signatures and unsigned map", () => {
it("should add empty signatures", () => {
const deviceInfo = DeviceInfo.fromStorage({ keys, algorithms, verified }, deviceId);
const device = deviceInfoToDevice(deviceInfo, userId);

expect(device.unsigned.size).toBe(0);
expect(device.signatures.size).toBe(0);
});
});
Expand All @@ -74,10 +74,10 @@ describe("device-converter", () => {
expect(device.verified).toBe(DeviceVerification.Unverified);
expect(device.getIdentityKey()).toBe(keys[`curve25519:${deviceId}`]);
expect(device.getFingerprint()).toBe(keys[`ed25519:${deviceId}`]);
expect(device.getDisplayName()).toBe(unsigned.device_display_name);
expect(device.displayName).toBe(displayName);
});

it("should add empty signatures and unsigned map", () => {
it("should add empty signatures", () => {
const queryDevice: DeviceKeys[keyof DeviceKeys] = {
keys,
algorithms,
Expand All @@ -86,7 +86,6 @@ describe("device-converter", () => {
};
const device = downloadDeviceToJsDevice(queryDevice);

expect(device.unsigned.size).toBe(0);
expect(device.signatures.size).toBe(0);
});
});
Expand Down
7 changes: 1 addition & 6 deletions src/crypto/deviceinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import { ISignatures } from "../@types/signed";
import { DeviceVerification } from "../models/device";

export interface IDevice {
keys: Record<string, string>;
Expand All @@ -25,12 +26,6 @@ export interface IDevice {
signatures?: ISignatures;
}

export enum DeviceVerification {
Blocked = -1,
Unverified = 0,
Verified = 1,
}

/**
* Information about a user's device
*/
Expand Down
1 change: 1 addition & 0 deletions src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export * from "./models/poll";
export * from "./models/room-member";
export * from "./models/room-state";
export * from "./models/user";
export * from "./models/device";
export * from "./scheduler";
export * from "./filter";
export * from "./timeline-window";
Expand Down
33 changes: 17 additions & 16 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { DeviceVerification } from "../crypto/deviceinfo";
/** State of the verification of the device.
* Beware that the enum values are numbers instead of plain strings
*/
export enum DeviceVerification {
Blocked = -1,
Unverified = 0,
Verified = 1,
}

// user-Id → device-Id → IDevice
/** A map from user ID to device ID to Device */
export type DeviceMap = Map<string, Map<string, Device>>;

type DeviceParameters = Pick<Device, "deviceId" | "userId" | "algorithms" | "keys"> & Partial<Device>;

/**
* Information on a user's device, as returned by {@link CryptoApi.getUserDeviceInfo}.
*/
export class Device {
/** id of the device */
public readonly deviceId: string;

/** id of the device user */
/** id of the user that owns the device */
public readonly userId: string;

/** list of algorithms supported by this device */
Expand All @@ -37,20 +47,20 @@ export class Device {
/** whether the device has been verified/blocked by the user */
public readonly verified: DeviceVerification;

/** additional data from the homeserver */
public readonly unsigned: Map<string, string>;

/** a map `<userId, map<algorithm:device_id, signature>>` */
public readonly signatures: Map<string, Map<string, string>>;

/** display name of the device */
public readonly displayName?: string;

public constructor(opts: DeviceParameters) {
this.deviceId = opts.deviceId;
this.userId = opts.userId;
this.algorithms = opts.algorithms;
this.keys = opts.keys;
this.verified = opts.verified || DeviceVerification.Unverified;
this.unsigned = opts.unsigned || new Map();
this.signatures = opts.signatures || new Map();
this.displayName = opts.displayName;
}

/**
Expand All @@ -70,13 +80,4 @@ export class Device {
public getIdentityKey(): string | undefined {
return this.keys.get(`curve25519:${this.deviceId}`);
}

/**
* Get the configured display name for this device, if any
* In `unsigned.device_display_name`
* @returns the device display name
*/
public getDisplayName(): string | undefined {
return this.unsigned.get("device_display_name");
}
}
16 changes: 4 additions & 12 deletions src/rust-crypto/device-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ limitations under the License.

import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js";

import { DeviceVerification } from "../crypto/deviceinfo";
import { Device } from "../models/device";
import { Device, DeviceVerification } from "../models/device";
import { DeviceKeys } from "../client";

/**
Expand Down Expand Up @@ -71,21 +70,14 @@ export function rustDeviceToJsDevice(device: RustSdkCryptoJs.Device, userId: Rus
}
});

// Add device display name to unsigned field
const unsigned = new Map<string, string>();
const { displayName } = device;
if (displayName) {
unsigned.set("device_display_name", displayName);
}

return new Device({
deviceId: device.deviceId.toString(),
userId: userId.toString(),
keys,
algorithms: Array.from(algorithms),
verified,
signatures,
unsigned,
displayName: device.displayName,
});
}

Expand All @@ -108,7 +100,7 @@ type QueryDevice = DeviceKeys[keyof DeviceKeys];
*/
export function downloadDeviceToJsDevice(device: QueryDevice): Device {
const keys = new Map(Object.entries(device.keys));
const unsigned = new Map(Object.entries(device.unsigned || {}));
const displayName = device.unsigned?.device_display_name;

const signatures = new Map<string, Map<string, string>>();
if (device.signatures) {
Expand All @@ -124,6 +116,6 @@ export function downloadDeviceToJsDevice(device: QueryDevice): Device {
algorithms: device.algorithms,
verified: DeviceVerification.Unverified,
signatures,
unsigned,
displayName,
});
}

0 comments on commit 2b57d0e

Please sign in to comment.