UNPKG

matrix-js-sdk

Version:
132 lines (122 loc) 4.25 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; /* 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-wasm"; import { Device, DeviceVerification } from "../models/device.js"; /** * Convert a {@link RustSdkCryptoJs.Device} to a {@link Device} * @param device - Rust Sdk device * @param userId - owner of the device * * @internal */ export function rustDeviceToJsDevice(device, userId) { // Copy rust device keys to Device.keys var keys = new Map(); for (var _ref3 of device.keys.entries()) { var _ref2 = _slicedToArray(_ref3, 2); var keyId = _ref2[0]; var key = _ref2[1]; keys.set(keyId.toString(), key.toBase64()); } // Compute verified from device state var verified = DeviceVerification.Unverified; if (device.isBlacklisted()) { verified = DeviceVerification.Blocked; } else if (device.isVerified()) { verified = DeviceVerification.Verified; } // Convert rust signatures to Device.signatures var signatures = new Map(); var mayBeSignatureMap = device.signatures.get(userId); if (mayBeSignatureMap) { var convertedSignatures = new Map(); // Convert maybeSignatures map to a Map<string, string> for (var _ref6 of mayBeSignatureMap.entries()) { var _ref5 = _slicedToArray(_ref6, 2); var _key = _ref5[0]; var value = _ref5[1]; if (value.isValid() && value.signature) { convertedSignatures.set(_key, value.signature.toBase64()); } } signatures.set(userId.toString(), convertedSignatures); } // Convert rust algorithms to algorithms var rustAlgorithms = device.algorithms; // Use set to ensure that algorithms are not duplicated var algorithms = new Set(); rustAlgorithms.forEach(algorithm => { switch (algorithm) { case RustSdkCryptoJs.EncryptionAlgorithm.MegolmV1AesSha2: algorithms.add("m.megolm.v1.aes-sha2"); break; case RustSdkCryptoJs.EncryptionAlgorithm.OlmV1Curve25519AesSha2: default: algorithms.add("m.olm.v1.curve25519-aes-sha2"); break; } }); return new Device({ deviceId: device.deviceId.toString(), userId: userId.toString(), keys, algorithms: Array.from(algorithms), verified, signatures, displayName: device.displayName, dehydrated: device.isDehydrated }); } /** * Convert {@link DeviceKeys} from `/keys/query` request to a `Map<string, Device>` * @param deviceKeys - Device keys object to convert * * @internal */ export function deviceKeysToDeviceMap(deviceKeys) { return new Map(Object.entries(deviceKeys).map(_ref7 => { var _ref8 = _slicedToArray(_ref7, 2), deviceId = _ref8[0], device = _ref8[1]; return [deviceId, downloadDeviceToJsDevice(device)]; })); } // Device from `/keys/query` request /** * Convert `/keys/query` {@link QueryDevice} device to {@link Device} * @param device - Device from `/keys/query` request * * @internal */ export function downloadDeviceToJsDevice(device) { var _device$unsigned; var keys = new Map(Object.entries(device.keys)); var displayName = (_device$unsigned = device.unsigned) === null || _device$unsigned === void 0 ? void 0 : _device$unsigned.device_display_name; var signatures = new Map(); if (device.signatures) { for (var userId in device.signatures) { signatures.set(userId, new Map(Object.entries(device.signatures[userId]))); } } return new Device({ deviceId: device.device_id, userId: device.user_id, keys, algorithms: device.algorithms, verified: DeviceVerification.Unverified, signatures, displayName }); } //# sourceMappingURL=device-converter.js.map