-
-
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.
Add
getUserDeviceInfo
WIP impl to rust-crypto
- Loading branch information
1 parent
58285ab
commit 91e1502
Showing
3 changed files
with
127 additions
and
5 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,74 @@ | ||
/* | ||
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 * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js"; | ||
|
||
import { DeviceVerification, IDevice } from "../crypto/deviceinfo"; | ||
import { DeviceKeys } from "../client"; | ||
|
||
/** | ||
* Convert a {@link RustSdkCryptoJs.Device} to a {@link IDevice} | ||
* @param device - Rust Sdk device | ||
*/ | ||
export function rustDeviceToJsDevice(device: RustSdkCryptoJs.Device): IDevice { | ||
const keys: Record<string, string> = Object.create(null); | ||
for (const [keyId, key] of device.keys.entries()) { | ||
keys[keyId.toString()] = key.toBase64(); | ||
} | ||
|
||
let verified: DeviceVerification = DeviceVerification.Unverified; | ||
if (device.isBlacklisted()) { | ||
verified = DeviceVerification.Blocked; | ||
} else if (device.isVerified()) { | ||
verified = DeviceVerification.Verified; | ||
} | ||
|
||
return { | ||
algorithms: [], // TODO | ||
keys: keys, | ||
known: false, // TODO | ||
signatures: undefined, // TODO | ||
verified, | ||
}; | ||
} | ||
|
||
/** | ||
* Convert {@link DeviceKeys} from `/keys/query` request to a `Map<string, IDevice>` | ||
* @param deviceKeys - Device keys object to convert | ||
*/ | ||
export function deviceKeysToIDeviceMap(deviceKeys: DeviceKeys): Map<string, IDevice> { | ||
return new Map( | ||
Object.entries(deviceKeys).map(([deviceId, device]) => [deviceId, downloadDeviceToJsDevice(device)]), | ||
); | ||
} | ||
|
||
// Device from `/keys/query` request | ||
type QueryDevice = DeviceKeys[keyof DeviceKeys]; | ||
|
||
/** | ||
* Convert `/keys/query` {@link QueryDevice} device to {@link IDevice} | ||
* @param device - Device from `/keys/query` request | ||
*/ | ||
export function downloadDeviceToJsDevice(device: QueryDevice): IDevice { | ||
return { | ||
algorithms: device.algorithms, | ||
keys: device.keys, | ||
known: false, | ||
signatures: device.signatures, | ||
verified: DeviceVerification.Unverified, | ||
unsigned: device.unsigned, | ||
}; | ||
} |
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