Skip to content

Commit e511612

Browse files
authored
Call disable initializer in smart wallet factory contract constructors (#566)
* Add _disableInitializers() * test revert: initialize implementation contract
1 parent d6bba75 commit e511612

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

contracts/prebuilts/account/dynamic/DynamicAccountFactory.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ contract DynamicAccountFactory is Initializable, BaseAccountFactory, ContractMet
3434
address(new DynamicAccount(IEntryPoint(ENTRYPOINT_ADDRESS), _defaultExtensions)),
3535
ENTRYPOINT_ADDRESS
3636
)
37-
{}
37+
{
38+
_disableInitializers();
39+
}
3840

3941
/// @notice Initializes the factory contract.
4042
function initialize(address _defaultAdmin, string memory _contractURI) external initializer {

contracts/prebuilts/account/managed/ManagedAccountFactory.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ contract ManagedAccountFactory is
3636
constructor(IEntryPoint _entrypoint, Extension[] memory _defaultExtensions)
3737
BaseRouter(_defaultExtensions)
3838
BaseAccountFactory(address(new ManagedAccount(_entrypoint)), address(_entrypoint))
39-
{}
39+
{
40+
_disableInitializers();
41+
}
4042

4143
/// @notice Initializes the factory contract.
4244
function initialize(address _defaultAdmin, string memory _contractURI) external initializer {

contracts/prebuilts/account/non-upgradeable/AccountFactory.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ contract AccountFactory is Initializable, BaseAccountFactory, ContractMetadata,
3131
Constructor
3232
//////////////////////////////////////////////////////////////*/
3333

34-
constructor(IEntryPoint _entrypoint) BaseAccountFactory(address(new Account(_entrypoint)), address(_entrypoint)) {}
34+
constructor(IEntryPoint _entrypoint) BaseAccountFactory(address(new Account(_entrypoint)), address(_entrypoint)) {
35+
_disableInitializers();
36+
}
3537

3638
/// @notice Initializes the factory contract.
3739
function initialize(address _defaultAdmin, string memory _contractURI) external initializer {

src/test/smart-wallet/Account.t.sol

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ contract SimpleAccountTest is BaseTest {
5555

5656
bytes32 private uidCache = bytes32("random uid");
5757

58+
address internal factoryImpl;
59+
5860
event AccountCreated(address indexed account, address indexed accountAdmin);
5961

6062
function _prepareSignature(IAccountPermissions.SignerPermissionRequest memory _req)
@@ -251,7 +253,7 @@ contract SimpleAccountTest is BaseTest {
251253
// Setup contracts
252254
entrypoint = new EntryPoint();
253255
// deploy account factory
254-
address factoryImpl = address(new AccountFactory(IEntryPoint(payable(address(entrypoint)))));
256+
factoryImpl = address(new AccountFactory(IEntryPoint(payable(address(entrypoint)))));
255257
accountFactory = AccountFactory(
256258
address(
257259
payable(
@@ -276,6 +278,11 @@ contract SimpleAccountTest is BaseTest {
276278
assertEq(accountFactory.hasRole(0x00, deployer), true);
277279
}
278280

281+
function test_revert_initializeImplementation() public {
282+
vm.expectRevert("Initializable: contract is already initialized");
283+
AccountFactory(factoryImpl).initialize(deployer, "https://example.com");
284+
}
285+
279286
/*///////////////////////////////////////////////////////////////
280287
Test: creating an account
281288
//////////////////////////////////////////////////////////////*/

src/test/smart-wallet/DynamicAccount.t.sol

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ contract DynamicAccountTest is BaseTest {
7171

7272
bytes32 private uidCache = bytes32("random uid");
7373

74+
address internal factoryImpl;
75+
7476
event AccountCreated(address indexed account, address indexed accountAdmin);
7577

7678
function _prepareSignature(IAccountPermissions.SignerPermissionRequest memory _req)
@@ -320,7 +322,7 @@ contract DynamicAccountTest is BaseTest {
320322
extensions[0] = defaultExtension;
321323

322324
// deploy account factory
323-
address factoryImpl = address(new DynamicAccountFactory(extensions));
325+
factoryImpl = address(new DynamicAccountFactory(extensions));
324326
accountFactory = DynamicAccountFactory(
325327
address(
326328
payable(
@@ -339,6 +341,11 @@ contract DynamicAccountTest is BaseTest {
339341
Test: creating an account
340342
//////////////////////////////////////////////////////////////*/
341343

344+
function test_revert_initializeImplementation() public {
345+
vm.expectRevert("Initializable: contract is already initialized");
346+
DynamicAccountFactory(factoryImpl).initialize(deployer, "https://example.com");
347+
}
348+
342349
/// @dev benchmark test for deployment gas cost
343350
function test_deploy_dynamicAccount() public {
344351
// Setting up default extension.
@@ -385,12 +392,12 @@ contract DynamicAccountTest is BaseTest {
385392
extensions[0] = defaultExtension;
386393

387394
// deploy account factory
388-
address factoryImpl = address(new DynamicAccountFactory(extensions));
395+
address factoryImplementation = address(new DynamicAccountFactory(extensions));
389396
DynamicAccountFactory factory = DynamicAccountFactory(
390397
address(
391398
payable(
392399
new TWProxy(
393-
factoryImpl,
400+
factoryImplementation,
394401
abi.encodeWithSignature("initialize(address,string)", deployer, "https://example.com")
395402
)
396403
)

src/test/smart-wallet/ManagedAccount.t.sol

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ contract ManagedAccountTest is BaseTest {
7272

7373
bytes32 private uidCache = bytes32("random uid");
7474

75+
address internal factoryImpl;
76+
7577
event AccountCreated(address indexed account, address indexed accountAdmin);
7678

7779
function _prepareSignature(IAccountPermissions.SignerPermissionRequest memory _req)
@@ -321,7 +323,7 @@ contract ManagedAccountTest is BaseTest {
321323

322324
// deploy account factory
323325
vm.prank(factoryDeployer);
324-
address factoryImpl = address(new ManagedAccountFactory(IEntryPoint(payable(address(entrypoint))), extensions));
326+
factoryImpl = address(new ManagedAccountFactory(IEntryPoint(payable(address(entrypoint))), extensions));
325327
accountFactory = ManagedAccountFactory(
326328
payable(
327329
address(
@@ -336,6 +338,11 @@ contract ManagedAccountTest is BaseTest {
336338
numberContract = new Number();
337339
}
338340

341+
function test_revert_initializeImplementation() public {
342+
vm.expectRevert("Initializable: contract is already initialized");
343+
ManagedAccountFactory(payable(factoryImpl)).initialize(deployer, "https://example.com");
344+
}
345+
339346
/// @dev benchmark test for deployment gas cost
340347
function test_deploy_managedAccount() public {
341348
// Setting up default extension.
@@ -383,7 +390,7 @@ contract ManagedAccountTest is BaseTest {
383390

384391
// deploy account factory
385392
vm.prank(factoryDeployer);
386-
address factoryImpl = address(new ManagedAccountFactory(IEntryPoint(payable(address(entrypoint))), extensions));
393+
factoryImpl = address(new ManagedAccountFactory(IEntryPoint(payable(address(entrypoint))), extensions));
387394
ManagedAccountFactory factory = ManagedAccountFactory(
388395
payable(
389396
address(

0 commit comments

Comments
 (0)