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

Added option of only using Account Address in some methods #113

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
"dependencies": {
"dotenv": "^16.4.5",
"solady": "^0.0.235"
}
},
"packageManager": "[email protected]+sha512.1f79bc245a66eb0b07c5d4d83131240774642caaa86ef7d0434ab47c0d16f66b04e21e0c086eb61e62c77efc4d7f7ec071afad3796af64892fae66509173893a"
}
1 change: 1 addition & 0 deletions src/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export {
encodeValidatorNonce,
} from './api'
export { SafeHookType } from './safe/types'
export { isAccount } from './utils'

export type { Account, AccountType, Execution, InitialModules } from './types'
24 changes: 24 additions & 0 deletions src/account/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Account, AccountType } from './types';
import { isAddress } from 'viem';

export function isAccount(obj: unknown): obj is Account {
const account = obj as Account;
return (
typeof obj === 'object' &&
obj !== null &&
isAddress(account.address) &&
(account.initCode === undefined ||
account.initCode !== undefined &&
typeof account.initCode === 'string' &&
account.initCode.startsWith('0x')) &&
typeof account.type === 'string' &&
isAccountType(account.type) &&
Array.isArray(account.deployedOnChains) &&
account.deployedOnChains.every(chainId => typeof chainId === 'number')
);
}

function isAccountType(value: unknown): value is AccountType {
const validTypes: AccountType[] = ['erc7579-implementation', 'kernel', 'safe', 'nexus'];
return typeof value === 'string' && validTypes.includes(value as AccountType);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
encodeModuleUninstallationData,
encodeValidatorNonce,
SafeHookType,
isAccount,
} from './account'
export type { Account, AccountType, Execution, InitialModules } from './account'

Expand Down
10 changes: 5 additions & 5 deletions src/module/smart-sessions/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
SmartSessionModeType,
} from './types'
import { LibZip } from 'solady'
import { Account, AccountType, Execution } from '../../account'
import { Account, AccountType, Execution, isAccount } from '../../account'

export const getPermissionId = ({ session }: { session: Session }): Hex => {
return keccak256(
Expand Down Expand Up @@ -68,14 +68,14 @@ export const getSessionNonce = async ({
permissionId,
}: {
client: PublicClient
account: Account
account: Account | Address
permissionId: Hex
}) => {
return (await client.readContract({
address: SMART_SESSIONS_ADDRESS,
abi,
functionName: 'getNonce',
args: [permissionId, account.address],
args: [permissionId, isAccount(account) ? account.address : account],
})) as bigint
}

Expand Down Expand Up @@ -175,14 +175,14 @@ export const isSessionEnabled = async ({
permissionId,
}: {
client: PublicClient
account: Account
account: Account | Address
permissionId: Hex
}) => {
return (await client.readContract({
address: SMART_SESSIONS_ADDRESS,
abi,
functionName: 'isPermissionEnabled',
args: [permissionId, account.address],
args: [permissionId, isAccount(account) ? account.address : account],
})) as boolean
}

Expand Down
72 changes: 72 additions & 0 deletions test/unit/account/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { isAccount } from '../../../src/account/utils';
import { Account } from '../../../src/account/types';
import { Address, getAddress } from 'viem';

describe('isAccount', () => {

const testAddress: Address = getAddress('0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC');

test('should return true for a valid Account object', () => {
const validAccount: Account = {
address: testAddress,
type: 'kernel',
deployedOnChains: [1, 2, 3]
};
expect(isAccount(validAccount)).toBe(true);
});

test('should return false for an object with an invalid address', () => {
const invalidAccount = {
address: '1234567890abcdef',
type: 'kernel',
deployedOnChains: [1, 2, 3]
};
expect(isAccount(invalidAccount)).toBe(false);
});

test('should return false for an object with an invalid type', () => {
const invalidAccount = {
address: testAddress,
type: 'invalid-type',
deployedOnChains: [1, 2, 3]
};
expect(isAccount(invalidAccount)).toBe(false);
});

test('should return false for an object with an invalid deployedOnChains', () => {
const invalidAccount = {
address: testAddress,
type: 'kernel',
deployedOnChains: ['1', '2', '3']
};
expect(isAccount(invalidAccount)).toBe(false);
});

test('should return false for a non-object input', () => {
expect(isAccount(null)).toBe(false);
expect(isAccount(undefined)).toBe(false);
expect(isAccount(123)).toBe(false);
expect(isAccount('string')).toBe(false);
});

test('should return true for a valid Account object with initCode', () => {
const validAccount: Account = {
address: testAddress,
type: 'kernel',
deployedOnChains: [1, 2, 3],
initCode: '0xabcdef'
};
expect(isAccount(validAccount)).toBe(true);
});

test('should return false for an object with an invalid initCode', () => {
const invalidAccount = {
address: testAddress,
type: 'kernel',
deployedOnChains: [1, 2, 3],
initCode: 'abcdef'
};
expect(isAccount(invalidAccount)).toBe(false);
});
});

Loading