From cabf9fb86e0aad485b4427c090d41a0f8172b681 Mon Sep 17 00:00:00 2001 From: Vitor Date: Tue, 3 Dec 2024 10:21:07 -0300 Subject: [PATCH] fix: add multi-chain support for sign user operation --- .../account-adapters/to-universal-account.ts | 7 ++++++- apps/popup/src/lib/account-abstraction/utils.ts | 4 +++- packages/universal-data/src/chains.ts | 7 ++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/apps/popup/src/lib/account-abstraction/account-adapters/to-universal-account.ts b/apps/popup/src/lib/account-abstraction/account-adapters/to-universal-account.ts index 129c2b40..3c0fb859 100644 --- a/apps/popup/src/lib/account-abstraction/account-adapters/to-universal-account.ts +++ b/apps/popup/src/lib/account-abstraction/account-adapters/to-universal-account.ts @@ -1,5 +1,5 @@ import type { Address, TypedData } from 'abitype'; -import { universalDeployments } from 'universal-data'; +import { isValidChain, universalDeployments } from 'universal-data'; import { type Assign, BaseError, @@ -244,11 +244,16 @@ export async function toUniversalAccount( const { chainId, ...userOperation } = parameters; const address = await this.getAddress(); + if (!isValidChain(chainId)) { + throw new BaseError('Invalid chainId'); + } + // @ts-ignore const packedUserOperationHash = getUserOperationHash(userOperation); const packedUserOpTypedHash = getPackedUserOperationTypedDataHash({ address, + chainId, packedUserOperationHash, }); diff --git a/apps/popup/src/lib/account-abstraction/utils.ts b/apps/popup/src/lib/account-abstraction/utils.ts index b941c56e..5ac2a636 100644 --- a/apps/popup/src/lib/account-abstraction/utils.ts +++ b/apps/popup/src/lib/account-abstraction/utils.ts @@ -191,13 +191,15 @@ export function getUserOperationHash(params: UserOperationParams): Hex { export function getPackedUserOperationTypedDataHash({ address, + chainId, packedUserOperationHash, }: { address: Address; + chainId: number; packedUserOperationHash: Hex; }): Hex { const parts: Hex[] = ['0x1901']; - const domainSeparator = buildDomainSeparator({ address, chainId: 84532 }); + const domainSeparator = buildDomainSeparator({ address, chainId }); parts.push(domainSeparator); parts.push(packedUserOperationHash); diff --git a/packages/universal-data/src/chains.ts b/packages/universal-data/src/chains.ts index 504bd8ba..c35164de 100644 --- a/packages/universal-data/src/chains.ts +++ b/packages/universal-data/src/chains.ts @@ -34,7 +34,12 @@ export function isTestnetChain(chainId: number): chainId is TestnetChain['id'] { return testnetChainIds.includes(chainId as TestnetChain['id']); } -export function isValidChain(chainId: number): chainId is ValidChain['id'] { +export function isValidChain( + chainId: number | undefined, +): chainId is ValidChain['id'] { + if (chainId === undefined) { + return false; + } return isProductionChain(chainId) || isTestnetChain(chainId); }