Skip to content

Commit

Permalink
Merge pull request #1127 from AmbireTech/fix/old-accounts-backward-co…
Browse files Browse the repository at this point in the history
…mpatibility

Fix: disable 4337 for all accounts that aren't on our latest factory
  • Loading branch information
borislav-itskov authored Nov 27, 2024
2 parents a87afe3 + 41023e8 commit df6d104
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/controllers/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,11 @@ export class MainController extends EventEmitter {
const network = this.networks.networks.find((net) => net.id === accOp.networkId)
if (!network) return undefined // shouldn't happen

const account = this.accounts.accounts.find((x) => x.addr === accOp.accountAddr)
if (!account) return undefined // shouldn't happen

const is4337 = isErc4337Broadcast(
account,
network,
this.accounts.accountStates[accOp.accountAddr][accOp.networkId]
)
Expand Down Expand Up @@ -1868,6 +1872,7 @@ export class MainController extends EventEmitter {
feeTokens,
{
is4337Broadcast: isErc4337Broadcast(
account,
network,
this.accounts.accountStates[localAccountOp.accountAddr][localAccountOp.networkId]
)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/signAccountOp/signAccountOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ export class SignAccountOpController extends EventEmitter {
// in that case, we don't do user operations
isERC4337:
this.paidBy === this.accountOp.accountAddr &&
isErc4337Broadcast(this.#network, accountState),
isErc4337Broadcast(this.account, this.#network, accountState),
isGasTank: this.feeTokenResult.flags.onGasTank,
inToken: this.feeTokenResult.address,
feeTokenNetworkId: this.feeTokenResult.networkId,
Expand Down
16 changes: 13 additions & 3 deletions src/libs/userOperation/userOperation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbiCoder, concat, hexlify, Interface, keccak256, toBeHex } from 'ethers'
import { AbiCoder, concat, getAddress, hexlify, Interface, keccak256, toBeHex } from 'ethers'
import { Network } from 'interfaces/network'

import AmbireAccount from '../../../contracts/compiled/AmbireAccount.json'
Expand Down Expand Up @@ -162,15 +162,25 @@ export function shouldUsePaymaster(network: Network): boolean {
return !!network.erc4337.hasPaymaster
}

export function isErc4337Broadcast(network: Network, accountState: AccountOnchainState): boolean {
export function isErc4337Broadcast(
acc: Account,
network: Network,
accountState: AccountOnchainState
): boolean {
// we can broadcast a 4337 if:
// - the account is not deployed (we do deployAndExecute in the factoryData)
// - the entry point is enabled (standard ops)
// - we have a paymaster (through the edge case)
const canWeBroadcast4337 =
accountState.isErc4337Enabled || shouldUsePaymaster(network) || !accountState.isDeployed

return network.erc4337.enabled && canWeBroadcast4337 && accountState.isV2
return (
network.erc4337.enabled &&
canWeBroadcast4337 &&
accountState.isV2 &&
!!acc.creation &&
getAddress(acc.creation.factoryAddr) === AMBIRE_ACCOUNT_FACTORY
)
}

// for special cases where we broadcast a 4337 operation with an EOA,
Expand Down

0 comments on commit df6d104

Please sign in to comment.