Skip to content

Commit

Permalink
feat: add isEVMAccount helper (#297)
Browse files Browse the repository at this point in the history
* feat: add isEvmAccount helper

* fix: 4337 methods

* fix: create InternalAccountType

* fix: naming of isEvmAccount

* revert: Signing methods for 4337 except SignTransaction

* fix: update logic to pass in account type rather than account object

* refactor: move to utils in eth folder

* chore: update lint

* revert: removal of sign to another PR

* fix: update jsdoc
  • Loading branch information
montelaidev committed May 2, 2024
1 parent e004d46 commit 0f028b0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
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

0 comments on commit 0f028b0

Please sign in to comment.