From 5370cc463d5553f5ad0b68464cf462cc7819f2f9 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 1 Sep 2022 13:24:11 -0400 Subject: [PATCH] Simplify AccountKeys json parsing --- libs/common/src/models/domain/account.ts | 40 ++++++++++-------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/libs/common/src/models/domain/account.ts b/libs/common/src/models/domain/account.ts index 3016111d04..e1e8a33f75 100644 --- a/libs/common/src/models/domain/account.ts +++ b/libs/common/src/models/domain/account.ts @@ -40,8 +40,8 @@ export class EncryptionPair { static fromJSON( obj: Jsonify, Jsonify>>, - decryptedFromJson?: (obj: Jsonify) => TDecrypted, - encryptedFromJson?: (obj: Jsonify) => TEncrypted + decryptedFromJson?: (decObj: Jsonify) => TDecrypted, + encryptedFromJson?: (encObj: Jsonify) => TEncrypted ) { const pair = new EncryptionPair(); if (obj?.encrypted) { @@ -137,32 +137,26 @@ export class AccountKeys { SymmetricCryptoKey.fromJSON ), }, - { - organizationKeys: EncryptionPair.fromJSON(obj?.organizationKeys, (obj: any) => { - const map = new Map(); - for (const orgId in obj) { - map.set(orgId, SymmetricCryptoKey.fromJSON(obj[orgId])); - } - return map; - }), - }, - { - providerKeys: EncryptionPair.fromJSON(obj?.providerKeys, (obj: any) => { - const map = new Map(); - for (const providerId in obj) { - map.set(providerId, SymmetricCryptoKey.fromJSON(obj[providerId])); - } - return map; - }), - }, - { - privateKey: EncryptionPair.fromJSON(obj?.privateKey), - }, + { organizationKeys: AccountKeys.initMapEncryptionPairsFromJSON(obj?.organizationKeys) }, + { providerKeys: AccountKeys.initMapEncryptionPairsFromJSON(obj?.providerKeys) }, + { privateKey: EncryptionPair.fromJSON(obj?.privateKey) }, { publicKey: Utils.fromByteStringToArray(obj?.publicKeySerialized)?.buffer, } ); } + + // These `any` types are a result of Jsonify> === {} + // Issue raised https://github.com/sindresorhus/type-fest/issues/457 + static initMapEncryptionPairsFromJSON(obj: any) { + return EncryptionPair.fromJSON(obj, (decObj: any) => { + const map = new Map(); + for (const id in decObj) { + map.set(id, SymmetricCryptoKey.fromJSON(decObj[id])); + } + return map; + }); + } } export class AccountProfile {