Skip to content

Commit

Permalink
Add typesafe pick function for filtering unwanted parameters in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jun 29, 2023
1 parent f411702 commit 339c643
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
36 changes: 13 additions & 23 deletions apps/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,33 +16,22 @@ export const closeCurrentTab = async (): Promise<void> => {
};

/**
* 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<DerivedAccount, keyof DerivedAccount>(
account,
"alias",
"chainId",
"id",
"parentId",
"path",
"type",
"publicKey"
)
);
};

Expand Down
9 changes: 9 additions & 0 deletions packages/utils/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,12 @@ export const makeBip44Path = (
return typeof index === "number" ? `${basePath}/${index}` : basePath;
};

/**
* Pick object parameters
*/
export function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
return keys.reduce((acc, val) => {
return (acc[val] = obj[val]), acc;
}, {} as Pick<T, K>);
}

0 comments on commit 339c643

Please sign in to comment.