Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-cats-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed `getUserOperationHash` calculation for EIP-7702 UserOperations.
26 changes: 17 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/account-abstraction/utils/userOperation/getInitCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ export function getInitCode(
UserOperation,
'authorization' | 'factory' | 'factoryData'
>,
options?: { forHash?: boolean },
) {
const { authorization, factory, factoryData } = userOperation
if (
factory === '0x7702' ||
factory === '0x7702000000000000000000000000000000000000'
) {
// For hash calculation: substitute delegate address
// For packing (ABI call): keep raw factory
if (options?.forHash && authorization) {
const delegation = authorization.address
return concat([delegation, factoryData ?? '0x'])
}
if (!authorization) return '0x7702000000000000000000000000000000000000'
const delegation = authorization.address
return concat([delegation, factoryData ?? '0x'])
return concat([factory, factoryData ?? '0x'])
}
if (!factory) return '0x'
return concat([factory, factoryData ?? '0x'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ export function getUserOperationHash<
if (entryPointVersion === '0.6') {
const factory = userOperation.initCode?.slice(0, 42) as Hex
const factoryData = userOperation.initCode?.slice(42) as Hex | undefined
const initCode = getInitCode({
authorization,
factory,
factoryData,
})
const initCode = getInitCode(
{
authorization,
factory,
factoryData,
},
{ forHash: true },
)
return encodeAbiParameters(
[
{ type: 'address' },
Expand Down Expand Up @@ -88,7 +91,9 @@ export function getUserOperationHash<
}

if (entryPointVersion === '0.7') {
const packedUserOp = toPackedUserOperation(userOperation)
const packedUserOp = toPackedUserOperation(userOperation, {
forHash: true,
})
return encodeAbiParameters(
[
{ type: 'address' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getUserOperationTypedData(
): GetUserOperationTypedDataReturnType {
const { chainId, entryPointAddress, userOperation } = parameters

const packedUserOp = toPackedUserOperation(userOperation)
const packedUserOp = toPackedUserOperation(userOperation, { forHash: true })

return {
types,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getInitCode } from './getInitCode.js'

export function toPackedUserOperation(
userOperation: UserOperation,
options?: { forHash?: boolean },
): PackedUserOperation {
const {
callGasLimit,
Expand All @@ -29,7 +30,8 @@ export function toPackedUserOperation(
pad(numberToHex(verificationGasLimit || 0n), { size: 16 }),
pad(numberToHex(callGasLimit || 0n), { size: 16 }),
])
const initCode = getInitCode(userOperation)
const initCode = getInitCode(userOperation, options)

const gasFees = concat([
pad(numberToHex(maxPriorityFeePerGas || 0n), { size: 16 }),
pad(numberToHex(maxFeePerGas || 0n), { size: 16 }),
Expand Down