Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

feat: add isEVMAccount helper #297

Merged
merged 10 commits into from
May 2, 2024
1 change: 1 addition & 0 deletions src/eth/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './erc4337';
export * from './types';
export * from './utils';
17 changes: 17 additions & 0 deletions src/eth/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { EthAccountType } from '.';
import { BtcAccountType } from '../btc';
import { isEvmAccountType } from './utils';

describe('isEvmAccountType', () => {
it.each([
[EthAccountType.Eoa, true],
[EthAccountType.Erc4337, true],
[BtcAccountType.P2wpkh, false],
[{}, false],
[null, false],
['bitcoin', false],
])('%s should return %s', (account, result) => {
// @ts-expect-error for error cases
expect(isEvmAccountType(account)).toBe(result);
});
});
11 changes: 11 additions & 0 deletions src/eth/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { InternalAccountType } from '../internal';
import { EthAccountType } from './types';

/**
* Checks if the given type is an EVM account type.
* @param type - The type to check.
* @returns Returns true if the type is an EVM account type, false otherwise.
*/
export function isEvmAccountType(type: InternalAccountType): boolean {
return type === EthAccountType.Eoa || type === EthAccountType.Erc4337;
}
9 changes: 8 additions & 1 deletion src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from '../eth/types';
import { exactOptional, object } from '../superstruct';

export type InternalAccountType = EthAccountType | BtcAccountType;

export const InternalAccountMetadataStruct = object({
metadata: object({
name: string(),
Expand Down Expand Up @@ -75,7 +77,12 @@ export const InternalAccountStructs: Record<
[`${BtcAccountType.P2wpkh}`]: InternalBtcP2wpkhAccountStruct,
};

export const InternalAccountStruct = define(
export type InternalAccountTypes =
| InternalEthEoaAccount
| InternalEthErc4337Account
| InternalBtcP2wpkhAccount;

export const InternalAccountStruct = define<InternalAccountTypes>(
'InternalAccount',
(value: unknown) => {
const account = mask(value, BaseKeyringAccountStruct);
Expand Down
Loading