Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: move getKeysByAccount to account utils #88

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
};