Skip to content

Commit 364c795

Browse files
authored
Use account factory to generate the init code of wallet (eth-infinitism#250)
1 parent abff2ac commit 364c795

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

src/AASigner.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import { BigNumber, Bytes, ethers, Event, Signer } from 'ethers'
2-
import { zeroAddress } from 'ethereumjs-util'
3-
import { BaseProvider, Provider, TransactionRequest } from '@ethersproject/providers'
1+
import { TransactionResponse } from '@ethersproject/abstract-provider'
2+
import { TransactionReceipt } from '@ethersproject/abstract-provider/src.ts/index'
3+
import { BytesLike, hexValue } from '@ethersproject/bytes'
44
import { Deferrable, resolveProperties } from '@ethersproject/properties'
5+
import { BaseProvider, Provider, TransactionRequest } from '@ethersproject/providers'
6+
import { BigNumber, Bytes, ethers, Event, Signer } from 'ethers'
7+
import { clearInterval } from 'timers'
8+
import { getAccountAddress, getAccountInitCode } from '../test/testutils'
9+
import { fillAndSign, getUserOpHash } from '../test/UserOp'
10+
import { UserOperation } from '../test/UserOperation'
511
import {
612
EntryPoint,
713
EntryPoint__factory,
8-
ERC1967Proxy__factory,
914
SimpleAccount,
15+
SimpleAccountFactory,
16+
SimpleAccountFactory__factory,
1017
SimpleAccount__factory
1118
} from '../typechain'
12-
import { BytesLike, hexValue } from '@ethersproject/bytes'
13-
import { TransactionResponse } from '@ethersproject/abstract-provider'
14-
import { fillAndSign, getUserOpHash } from '../test/UserOp'
15-
import { UserOperation } from '../test/UserOperation'
16-
import { TransactionReceipt } from '@ethersproject/abstract-provider/src.ts/index'
17-
import { clearInterval } from 'timers'
18-
import { Create2Factory } from './Create2Factory'
19-
import { getCreate2Address, hexConcat, Interface, keccak256 } from 'ethers/lib/utils'
20-
import { HashZero } from '../test/testutils'
2119

2220
export type SendUserOp = (userOp: UserOperation) => Promise<TransactionResponse | undefined>
2321

@@ -202,6 +200,7 @@ export class AASigner extends Signer {
202200

203201
private _isPhantom = true
204202
public entryPoint: EntryPoint
203+
public accountFactory: SimpleAccountFactory
205204

206205
private _chainId: Promise<number> | undefined
207206

@@ -212,9 +211,10 @@ export class AASigner extends Signer {
212211
* @param sendUserOp function to actually send the UserOp to the entryPoint.
213212
* @param index - index of this account for this signer.
214213
*/
215-
constructor (readonly signer: Signer, readonly entryPointAddress: string, readonly sendUserOp: SendUserOp, readonly index = 0, readonly provider = signer.provider) {
214+
constructor (readonly signer: Signer, readonly entryPointAddress: string, readonly sendUserOp: SendUserOp, readonly accountFactoryAddress: string, readonly index = 0, readonly provider = signer.provider) {
216215
super()
217216
this.entryPoint = EntryPoint__factory.connect(entryPointAddress, signer)
217+
this.accountFactory = SimpleAccountFactory__factory.connect(accountFactoryAddress, signer)
218218
}
219219

220220
// connect to a specific pre-deployed address
@@ -234,18 +234,6 @@ export class AASigner extends Signer {
234234
throw new Error('connect not implemented')
235235
}
236236

237-
async _deploymentAddress (): Promise<string> {
238-
return getCreate2Address(Create2Factory.contractAddress, HashZero, keccak256(await this._deploymentTransaction()))
239-
}
240-
241-
// TODO TODO: THERE IS UTILS.getAccountInitCode - why not use that?
242-
async _deploymentTransaction (): Promise<BytesLike> {
243-
const implementationAddress = zeroAddress() // TODO: pass implementation in here
244-
const ownerAddress = await this.signer.getAddress()
245-
const initializeCall = new Interface(SimpleAccount__factory.abi).encodeFunctionData('initialize', [ownerAddress])
246-
return new ERC1967Proxy__factory(this.signer).getDeployTransaction(implementationAddress, initializeCall).data!
247-
}
248-
249237
async getAddress (): Promise<string> {
250238
await this.syncAccount()
251239
return this._account!.address
@@ -353,7 +341,7 @@ export class AASigner extends Signer {
353341

354342
async syncAccount (): Promise<void> {
355343
if (this._account == null) {
356-
const address = await this._deploymentAddress()
344+
const address = await getAccountAddress(await this.signer.getAddress(), this.accountFactory)
357345
this._account = SimpleAccount__factory.connect(address, this.signer)
358346
}
359347

@@ -380,12 +368,7 @@ export class AASigner extends Signer {
380368

381369
let initCode: BytesLike | undefined
382370
if (this._isPhantom) {
383-
const initCallData = new Create2Factory(this.provider!).getDeployTransactionCallData(hexValue(await this._deploymentTransaction()), HashZero)
384-
385-
initCode = hexConcat([
386-
Create2Factory.contractAddress,
387-
initCallData
388-
])
371+
initCode = getAccountInitCode(await this.signer.getAddress(), this.accountFactory)
389372
}
390373
const execFromEntryPoint = await this._account!.populateTransaction.execute(tx.to!, tx.value ?? 0, tx.data!)
391374

src/runop.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import { TransactionReceipt } from '@ethersproject/abstract-provider/src.ts/inde
2424
await hre.run('etherscan-verify')
2525
}
2626
}
27-
const [entryPointAddress, testCounterAddress] = await Promise.all([
27+
const [entryPointAddress, testCounterAddress, accountFactoryAddress] = await Promise.all([
2828
hre.deployments.get('EntryPoint').then(d => d.address),
29-
hre.deployments.get('TestCounter').then(d => d.address)
29+
hre.deployments.get('TestCounter').then(d => d.address),
30+
hre.deployments.get('SimpleAccountFactory').then(d => d.address)
3031
])
3132

3233
console.log('entryPointAddress:', entryPointAddress, 'testCounterAddress:', testCounterAddress)
@@ -51,7 +52,7 @@ import { TransactionReceipt } from '@ethersproject/abstract-provider/src.ts/inde
5152
// index is unique for an account (so same owner can have multiple accounts, with different index
5253
const index = parseInt(process.env.AA_INDEX ?? '0')
5354
console.log('using account index (AA_INDEX)', index)
54-
const aasigner = new AASigner(ethersSigner, entryPointAddress, sendUserOp, index)
55+
const aasigner = new AASigner(ethersSigner, entryPointAddress, sendUserOp, accountFactoryAddress, index)
5556
// connect to pre-deployed account
5657
// await aasigner.connectAccountAddress(accountAddress)
5758
const myAddress = await aasigner.getAddress()

0 commit comments

Comments
 (0)