Skip to content

Commit

Permalink
refactor: move getKeysByAccount to account utils
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed Sep 17, 2024
1 parent 58cbf4d commit 2e23129
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/components/KeyManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ import {
KeyPairType,
SafeKeyWithType,
} from '../types/wallet';
import { getKeysByAccount } from '../utils/account';
import { BUTTON_ICON_SIZE } from '../utils/constants';
import {
AnySpawnArguments,
getTemplateNameByKey,
MultiSigSpawnArguments,
SingleSigSpawnArguments,
VaultSpawnArguments,
} from '../utils/templates';
import { safeKeyForAccount } from '../utils/wallet';
Expand Down Expand Up @@ -171,29 +171,6 @@ function KeyManager({ isOpen, onClose }: KeyManagerProps): JSX.Element {
'text/plain'
);

const getKeysByAccount = (acc: AccountWithAddress) => {
switch (acc.templateAddress) {
case StdPublicKeys.SingleSig: {
const pk = (acc.spawnArguments as SingleSigSpawnArguments).PublicKey;
return (wallet?.keychain ?? []).filter((k) => k.publicKey === pk);
}
case StdPublicKeys.MultiSig:
case StdPublicKeys.Vesting: {
const pks = (acc.spawnArguments as MultiSigSpawnArguments).PublicKeys;
return (wallet?.keychain ?? []).filter((k) =>
pks.includes(k.publicKey)
);
}
case StdPublicKeys.Vault: {
const pk = (acc.spawnArguments as VaultSpawnArguments).Owner;
return (wallet?.keychain ?? []).filter((k) => k.publicKey === pk);
}
default: {
throw new Error('Unknown account type');
}
}
};

const withoutInitialUnlockAmount = (args: AnySpawnArguments) => {
if (Object.hasOwn(args, 'InitialUnlockAmount')) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -355,7 +332,7 @@ function KeyManager({ isOpen, onClose }: KeyManagerProps): JSX.Element {
</Flex>
<Box flex={1}>
{accounts.map((acc, idx) => {
const keys = getKeysByAccount(acc);
const keys = getKeysByAccount(acc, wallet?.keychain ?? []);
return (
<Box
key={safeKeyForAccount(acc)}
Expand Down
23 changes: 23 additions & 0 deletions src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,26 @@ export const getVaultUnlockedAmount = (
available: zeroOrMore(available),
};
};
export const getKeysByAccount = (
acc: AccountWithAddress,
keys: SafeKeyWithType[]
) => {
switch (acc.templateAddress) {
case StdPublicKeys.SingleSig: {
const pk = (acc.spawnArguments as SingleSigSpawnArguments).PublicKey;
return keys.filter((k) => k.publicKey === pk);
}
case StdPublicKeys.MultiSig:
case StdPublicKeys.Vesting: {
const pks = (acc.spawnArguments as MultiSigSpawnArguments).PublicKeys;
return keys.filter((k) => pks.includes(k.publicKey));
}
case StdPublicKeys.Vault: {
const pk = (acc.spawnArguments as VaultSpawnArguments).Owner;
return keys.filter((k) => k.publicKey === pk);
}
default: {
throw new Error('Unknown account type');
}
}
};

0 comments on commit 2e23129

Please sign in to comment.