From 41c60fd2a9e446a38aee55a326c06b9a1ccf73ff Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Wed, 16 Nov 2022 19:11:28 -0500 Subject: [PATCH] Libs account serialization improvements --- libs/common/src/misc/utils.ts | 18 +++++++++++++++++- libs/common/src/models/domain/account.ts | 6 +++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/libs/common/src/misc/utils.ts b/libs/common/src/misc/utils.ts index c62d64d2f7d..0066ac8796c 100644 --- a/libs/common/src/misc/utils.ts +++ b/libs/common/src/misc/utils.ts @@ -1,5 +1,6 @@ /* eslint-disable no-useless-escape */ import { getHostname, parse } from "tldts"; +import { Merge } from "type-fest"; import { CryptoService } from "../abstractions/crypto.service"; import { EncryptService } from "../abstractions/encrypt.service"; @@ -55,6 +56,10 @@ export class Utils { } static fromB64ToArray(str: string): Uint8Array { + if (str == null) { + return null; + } + if (Utils.isNode) { return new Uint8Array(Buffer.from(str, "base64")); } else { @@ -108,6 +113,9 @@ export class Utils { } static fromBufferToB64(buffer: ArrayBuffer): string { + if (buffer == null) { + return null; + } if (Utils.isNode) { return Buffer.from(buffer).toString("base64"); } else { @@ -429,7 +437,7 @@ export class Utils { * @param map * @returns */ - static mapToRecord(map: Map): Record { + static mapToRecord(map: Map): Record { return map == null ? null : Object.fromEntries(map); } @@ -457,6 +465,14 @@ export class Utils { } } + /** Applies Object.assign, but converts the type nicely using Type-Fest Merge */ + static merge( + destination: Destination, + source: Source + ): Merge { + return Object.assign(destination, source) as unknown as Merge; + } + private static isMobile(win: Window) { let mobile = false; ((a) => { diff --git a/libs/common/src/models/domain/account.ts b/libs/common/src/models/domain/account.ts index 75a1eebfebb..9fe36001d03 100644 --- a/libs/common/src/models/domain/account.ts +++ b/libs/common/src/models/domain/account.ts @@ -1,4 +1,4 @@ -import { Except, Jsonify } from "type-fest"; +import { Jsonify } from "type-fest"; import { AuthenticationStatus } from "../../enums/authenticationStatus"; import { KdfType } from "../../enums/kdfType"; @@ -40,7 +40,7 @@ export class EncryptionPair { } static fromJSON( - obj: Jsonify, Jsonify>>, + obj: { encrypted?: Jsonify; decrypted?: string | Jsonify }, decryptedFromJson?: (decObj: Jsonify | string) => TDecrypted, encryptedFromJson?: (encObj: Jsonify) => TEncrypted ) { @@ -123,7 +123,7 @@ export class AccountKeys { apiKeyClientSecret?: string; toJSON() { - return Object.assign(this as Except, { + return Utils.merge(this, { publicKey: Utils.fromBufferToByteString(this.publicKey), }); }