From 339c6436a9ada3eb72d615ba044bf7a1fed6c9c8 Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Thu, 29 Jun 2023 06:34:45 -0400 Subject: [PATCH] Add typesafe pick function for filtering unwanted parameters in storage --- apps/extension/src/utils/index.ts | 36 +++++++++++------------------ packages/utils/src/helpers/index.ts | 9 ++++++++ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/apps/extension/src/utils/index.ts b/apps/extension/src/utils/index.ts index aab9d97a8c..87b4060409 100644 --- a/apps/extension/src/utils/index.ts +++ b/apps/extension/src/utils/index.ts @@ -3,6 +3,7 @@ import { v5 as uuid } from "uuid"; import { DerivedAccount } from "@anoma/types"; import { AccountStore } from "background/keyring"; +import { pick } from "@anoma/utils"; /** * Query the current extension tab and close it @@ -15,33 +16,22 @@ export const closeCurrentTab = async (): Promise => { }; /** - * Return all unencrypted values from account stores + * Return all unencrypted values from key store */ export const getAccountValuesFromStore = ( accounts: AccountStore[] ): DerivedAccount[] => { - return accounts.map( - ({ - address, - alias, - chainId, - path, - parentId, - id, - owner, - type, - publicKey, - }) => ({ - address, - alias, - chainId, - id, - owner, - parentId, - path, - type, - publicKey, - }) + return accounts.map((account) => + pick( + account, + "alias", + "chainId", + "id", + "parentId", + "path", + "type", + "publicKey" + ) ); }; diff --git a/packages/utils/src/helpers/index.ts b/packages/utils/src/helpers/index.ts index ad0598f976..e2a614ccb2 100644 --- a/packages/utils/src/helpers/index.ts +++ b/packages/utils/src/helpers/index.ts @@ -261,3 +261,12 @@ export const makeBip44Path = ( return typeof index === "number" ? `${basePath}/${index}` : basePath; }; +/** + * Pick object parameters + */ +export function pick(obj: T, ...keys: K[]): Pick { + return keys.reduce((acc, val) => { + return (acc[val] = obj[val]), acc; + }, {} as Pick); +} +