Skip to content

Commit

Permalink
check nonce before auth. fix rlp
Browse files Browse the repository at this point in the history
  • Loading branch information
drortirosh committed Dec 11, 2024
1 parent 9a12e26 commit f46bd6e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
13 changes: 4 additions & 9 deletions packages/bundler/src/modules/BundleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ChainConfig, Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { EOACode7702Transaction } from '@ethereumjs/tx'
import { AuthorizationList, EOACode7702TxData } from '@ethereumjs/tx/src/types'
import { PrefixedHexString } from '@ethereumjs/util'
import { toRlpHex } from '@account-abstraction/utils/dist/src/interfaces/EIP7702Authorization'

const debug = Debug('aa.exec.cron')

Expand Down Expand Up @@ -199,23 +200,17 @@ export class BundleManager implements IBundleManager {
chainId: 1337
}
const common = new Common({ chain, eips: [2718, 2929, 2930, 7702] })

const authorizationList: AuthorizationList = eip7702Tuples.map(it => {
const res = {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion,@typescript-eslint/no-base-to-string
chainId: `0x${parseInt(it.chainId.toString()).toString(16)}` as PrefixedHexString,
address: it.address as PrefixedHexString,
nonce: it.nonce as PrefixedHexString,
yParity: it.yParity as PrefixedHexString,
nonce: toRlpHex(it.nonce as PrefixedHexString),
yParity: toRlpHex(it.yParity as PrefixedHexString),
r: it.r as PrefixedHexString,
s: it.s as PrefixedHexString
}
if (res.yParity === '0x0') {
// o, for fuck's sake!
res.yParity = '0x'
}
if (res.nonce === '0x0') {
throw new Error('ethereumjs/tx does not handle zero nonce!')
}
return res
})
const txData: EOACode7702TxData = {
Expand Down
24 changes: 15 additions & 9 deletions packages/utils/src/interfaces/EIP7702Authorization.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import { BigNumberish } from 'ethers'
import RLP from 'rlp'
import { bytesToHex, ecrecover, hexToBigInt, hexToBytes, pubToAddress } from '@ethereumjs/util'
import { bytesToHex, ecrecover, hexToBigInt, hexToBytes, PrefixedHexString, pubToAddress } from '@ethereumjs/util'
import { AddressZero } from '../ERC4337Utils'
import { keccak256 } from '@ethersproject/keccak256'
import { hexlify } from 'ethers/lib/utils'

export interface EIP7702Authorization {
chainId: BigNumberish,
address: string,
nonce: BigNumberish,
yParity: BigNumberish,
r: BigNumberish,
chainId: BigNumberish
address: string
nonce: BigNumberish
yParity: BigNumberish
r: BigNumberish
s: BigNumberish
}

export function toRlpHex (s: any): PrefixedHexString {
// remove leading zeros (also, 0x0 returned as 0x)
return s.toString().replace(/0x0*/, '0x') as PrefixedHexString
}

export function getEip7702AuthorizationSigner (authorization: EIP7702Authorization): string {
const rlpEncode = [
5,
...RLP.encode(
[
authorization.chainId.toString(),
authorization.address.toString(),
authorization.nonce.toString()
toRlpHex(authorization.chainId),
toRlpHex(authorization.address),
toRlpHex(authorization.nonce)
]
)
]
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/interfaces/OperationRIP7560.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { OperationBase } from './OperationBase'
import { BigNumberish, BytesLike } from 'ethers'
import { EIP7702Authorization } from './EIP7702Authorization'

export interface OperationRIP7560 extends OperationBase {
chainId: BigNumberish
Expand Down
14 changes: 10 additions & 4 deletions packages/validation-manager/src/ValidationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,21 @@ export class ValidationManager implements IValidationManager {
// TODO: why don't we have 'provider' as a member in here?
const provider = this.entryPoint.provider as JsonRpcProvider
for (const authorization of authorizations) {
const sender = getEip7702AuthorizationSigner(authorization)
const currentDelegateeCode = await provider.getCode(sender)
const authSigner = getEip7702AuthorizationSigner(authorization)
const nonce = await provider.getTransactionCount(authSigner)
const authNonce: any = authorization.nonce
if (nonce !== BigNumber.from(authNonce.replace(/0x$/, '0x0')).toNumber()) {
continue
}
const currentDelegateeCode = await provider.getCode(authSigner)
const newDelegateeCode = await provider.getCode(authorization.address)
// TODO should be: hexConcat(['0xef0100', authorization.address])
const noCurrentDelegation = currentDelegateeCode.length <= 2
// TODO: do not send such authorizations to 'handleOps' as it is a waste of gas
const changeDelegation = newDelegateeCode !== currentDelegateeCode
if (noCurrentDelegation || changeDelegation) {
console.log('Adding state override:', { address: sender, code: newDelegateeCode.slice(0, 20) })
stateOverride[sender] = {
console.log('Adding state override:', { address: authSigner, code: newDelegateeCode.slice(0, 20) })
stateOverride[authSigner] = {
code: newDelegateeCode
}
}
Expand Down

0 comments on commit f46bd6e

Please sign in to comment.