Skip to content

Commit

Permalink
Simplifying contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
eloi010 committed Jun 20, 2023
1 parent 4d294f1 commit f0c31b9
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 79 deletions.
2 changes: 1 addition & 1 deletion contracts/core/managed/ManagedOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract ManagedOpenfortAccount is BaseOpenfortAccount {
/*
* @notice Initialize the smart contract wallet.
*/
function initialize(address _defaultAdmin, bytes calldata) public initializer {
function initialize(address _defaultAdmin) public initializer {
if (_defaultAdmin == address(0)) {
revert ZeroAddressNotAllowed();
}
Expand Down
17 changes: 7 additions & 10 deletions contracts/core/managed/ManagedOpenfortFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ contract ManagedOpenfortFactory is IBaseOpenfortFactory {
/*
* @notice Deploy a new account for _admin with a nonce.
*/
function createAccountWithNonce(address _admin, bytes calldata _data, uint256 nonce)
external
returns (address account)
{
bytes32 salt = keccak256(abi.encode(msg.sender, nonce));
account = getAddressWithNonce(_admin, nonce);
function createAccountWithNonce(address _admin, bytes calldata _nonce) external returns (address account) {
bytes32 salt = keccak256(abi.encode(_admin, _nonce));
account = getAddressWithNonce(_admin, _nonce);

if (account.code.length > 0) {
return account;
Expand All @@ -46,22 +43,22 @@ contract ManagedOpenfortFactory is IBaseOpenfortFactory {
account = address(
new OpenfortBeaconProxy{salt: salt}(
openfortBeacon,
abi.encodeCall(ManagedOpenfortAccount.initialize, (_admin, _data))
abi.encodeCall(ManagedOpenfortAccount.initialize, (_admin))
)
);
}

/*
* @notice Return the address of an account that would be deployed with the given admin signer and nonce.
*/
function getAddressWithNonce(address _admin, uint256 nonce) public view returns (address) {
bytes32 salt = keccak256(abi.encode(msg.sender, nonce));
function getAddressWithNonce(address _admin, bytes calldata _nonce) public view returns (address) {
bytes32 salt = keccak256(abi.encode(_admin, _nonce));
return Create2.computeAddress(
salt,
keccak256(
abi.encodePacked(
type(OpenfortBeaconProxy).creationCode,
abi.encode(openfortBeacon, abi.encodeCall(ManagedOpenfortAccount.initialize, (_admin, "")))
abi.encode(openfortBeacon, abi.encodeCall(ManagedOpenfortAccount.initialize, (_admin)))
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/managed/OpenfortBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import {UpgradeableBeacon} from "@openzeppelin/contracts/proxy/beacon/Upgradeabl
* - UpgradeableBeacon
*/
contract OpenfortBeacon is UpgradeableBeacon {
constructor(address implementation_) UpgradeableBeacon(implementation_) {}
constructor(address _implementation) UpgradeableBeacon(_implementation) {}
}
2 changes: 1 addition & 1 deletion contracts/core/static/StaticOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract StaticOpenfortAccount is BaseOpenfortAccount {
/*
* @notice Initialize the smart contract wallet.
*/
function initialize(address _defaultAdmin, address _entrypoint, bytes calldata) public initializer {
function initialize(address _defaultAdmin, address _entrypoint) public initializer {
if (_defaultAdmin == address(0) || _entrypoint == address(0)) {
revert ZeroAddressNotAllowed();
}
Expand Down
19 changes: 7 additions & 12 deletions contracts/core/static/StaticOpenfortFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ contract StaticOpenfortFactory is IBaseOpenfortFactory {
/*
* @notice Deploy a new account for _admin and a given nonce.
*/
function createAccountWithNonce(address _admin, bytes calldata _data, uint256 nonce)
external
returns (address account)
{
function createAccountWithNonce(address _admin, bytes calldata _nonce) external returns (address account) {
address impl = accountImplementation;
bytes32 salt = keccak256(abi.encode(msg.sender, nonce));
bytes32 salt = keccak256(abi.encode(_admin, _nonce));
account = Clones.predictDeterministicAddress(impl, salt);

if (account.code.length > 0) {
Expand All @@ -47,23 +44,21 @@ contract StaticOpenfortFactory is IBaseOpenfortFactory {

emit AccountCreated(account, _admin);
account = Clones.cloneDeterministic(impl, salt);
_initializeAccount(account, _admin, entrypointContract, _data);
_initializeAccount(account, _admin, entrypointContract);
}

/*
* @notice Return the address of an account that would be deployed with the given admin signer and nonce.
*/
function getAddressWithNonce(address _admin, uint256 nonce) public view returns (address) {
bytes32 salt = keccak256(abi.encode(msg.sender, nonce));
function getAddressWithNonce(address _admin, bytes calldata _nonce) public view returns (address) {
bytes32 salt = keccak256(abi.encode(_admin, _nonce));
return Clones.predictDeterministicAddress(accountImplementation, salt);
}

/*
* @dev Called in `createAccount`. Initializes the account contract created in `createAccount`.
*/
function _initializeAccount(address _account, address _admin, address _entrypointContract, bytes calldata _data)
internal
{
StaticOpenfortAccount(payable(_account)).initialize(_admin, _entrypointContract, _data);
function _initializeAccount(address _account, address _admin, address _entrypointContract) internal {
StaticOpenfortAccount(payable(_account)).initialize(_admin, _entrypointContract);
}
}
2 changes: 1 addition & 1 deletion contracts/core/upgradeable/UpgradeableOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract UpgradeableOpenfortAccount is BaseOpenfortAccount, UUPSUpgradeable {
/*
* @notice Initialize the smart contract wallet.
*/
function initialize(address _defaultAdmin, address _entrypoint, bytes calldata) public initializer {
function initialize(address _defaultAdmin, address _entrypoint) public initializer {
if (_defaultAdmin == address(0) || _entrypoint == address(0)) {
revert ZeroAddressNotAllowed();
}
Expand Down
17 changes: 7 additions & 10 deletions contracts/core/upgradeable/UpgradeableOpenfortFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ contract UpgradeableOpenfortFactory is IBaseOpenfortFactory {
/*
* @notice Deploy a new account for _admin with a nonce.
*/
function createAccountWithNonce(address _admin, bytes calldata _data, uint256 nonce)
external
returns (address account)
{
bytes32 salt = keccak256(abi.encode(msg.sender, nonce));
account = getAddressWithNonce(_admin, nonce);
function createAccountWithNonce(address _admin, bytes calldata _nonce) external returns (address account) {
bytes32 salt = keccak256(abi.encode(_admin, _nonce));
account = getAddressWithNonce(_admin, _nonce);

if (account.code.length > 0) {
return account;
Expand All @@ -46,24 +43,24 @@ contract UpgradeableOpenfortFactory is IBaseOpenfortFactory {
account = address(
new OpenfortUpgradeableProxy{salt: salt}(
accountImplementation,
abi.encodeCall(UpgradeableOpenfortAccount.initialize, (_admin, entrypointContract, _data))
abi.encodeCall(UpgradeableOpenfortAccount.initialize, (_admin, entrypointContract))
)
);
}

/*
* @notice Return the address of an account that would be deployed with the given admin signer and nonce.
*/
function getAddressWithNonce(address _admin, uint256 nonce) public view returns (address) {
bytes32 salt = keccak256(abi.encode(msg.sender, nonce));
function getAddressWithNonce(address _admin, bytes calldata _nonce) public view returns (address) {
bytes32 salt = keccak256(abi.encode(_admin, _nonce));
return Create2.computeAddress(
salt,
keccak256(
abi.encodePacked(
type(OpenfortUpgradeableProxy).creationCode,
abi.encode(
accountImplementation,
abi.encodeCall(UpgradeableOpenfortAccount.initialize, (_admin, entrypointContract, ""))
abi.encodeCall(UpgradeableOpenfortAccount.initialize, (_admin, entrypointContract))
)
)
)
Expand Down
14 changes: 3 additions & 11 deletions contracts/interfaces/IBaseOpenfortFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ interface IBaseOpenfortFactory {
error ZeroAddressNotAllowed();

/// @notice Deploys a new Account for admin.
//function createAccount(address _admin, bytes calldata _data) external returns (address account);

/// @notice Deploys a new Account for admin.
function createAccountWithNonce(address _admin, bytes calldata _data, uint256 nonce)
external
returns (address account);
function createAccountWithNonce(address _admin, bytes calldata _nonce) external returns (address account);

/// @notice Returns the address of the Account implementation.
function accountImplementation() external view returns (address);

/// @notice Returns the address of an Account that would be deployed with the given admin signer.
//function getAddress(address adminSigner) external view returns (address);

/// @notice Returns the address of an Account that would be deployed with the given admin signer and nonce.
function getAddressWithNonce(address adminSigner, uint256 nonce) external view returns (address);
/// @notice Returns the address of an Account that would be deployed with the given admin and nonce.
function getAddressWithNonce(address _admin, bytes calldata _nonce) external view returns (address);
}
2 changes: 1 addition & 1 deletion contracts/mock/MockedV2ManagedOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract MockedV2ManagedOpenfortAccount is BaseOpenfortAccount {
/*
* @notice Initialize the smart contract wallet.
*/
function initialize(address _defaultAdmin, bytes calldata) public initializer {
function initialize(address _defaultAdmin) public initializer {
if (_defaultAdmin == address(0)) {
revert ZeroAddressNotAllowed();
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/mock/MockedV2UpgradeableOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract MockedV2UpgradeableOpenfortAccount is BaseOpenfortAccount, UUPSUpgradea
* @notice Initialize the smart contract wallet.
*/

function initialize(address _defaultAdmin, address _entrypoint, bytes calldata) public initializer {
function initialize(address _defaultAdmin, address _entrypoint) public initializer {
if (_defaultAdmin == address(0) || _entrypoint == address(0)) {
revert ZeroAddressNotAllowed();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfort/openfort-contracts",
"version": "0.2.0",
"version": "0.3.0",
"description": "Official Contracts of the Openfort Project",
"scripts": {
"build": "npm run clean-all && npm run compile",
Expand Down
2 changes: 1 addition & 1 deletion script/UserOpTestCounter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ contract UserOpTestCounter is Script {
testCounter.count();
assert(testCounter.counters(deployAddress) == 1);

address account = staticOpenfortFactory.createAccountWithNonce(deployAddress, "", 1);
address account = staticOpenfortFactory.createAccountWithNonce(deployAddress, "1");

// Count using userOp
UserOperation[] memory userOp = _setupUserOpExecute(
Expand Down
2 changes: 1 addition & 1 deletion script/deployBatching.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ contract DeployBatching is Script {

// Created with:
// $forge create StaticOpenfortAccount --constructor-args $ENTRY_POINT_ADDRESS 0x6E767F52d49b0abD686003727b8bc0684011819B --mnemonic $MNEMONIC --rpc-url $POLYGON_MUMBAI_RPC --verify
address account = staticOpenfortFactory.createAccountWithNonce(deployAddress, "", 1);
address account = staticOpenfortFactory.createAccountWithNonce(deployAddress, "1");

uint256 count = 3;
address[] memory targets = new address[](count);
Expand Down
6 changes: 3 additions & 3 deletions script/deployManagedAccounts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract ManagedOpenfortDeploy is Script {
// address account1 = managedOpenfortFactory.accountImplementation();

// The first call should create a new account, while the second will just return the corresponding account address
address account2 = managedOpenfortFactory.createAccountWithNonce(deployAddress, "", 1);
address account2 = managedOpenfortFactory.createAccountWithNonce(deployAddress, "1");
console.log(
"Factory at address %s has created an account at address %s", address(managedOpenfortFactory), account2
);
Expand All @@ -37,12 +37,12 @@ contract ManagedOpenfortDeploy is Script {
// (mockedOpenfortAccount);

// assert(account1 != account2);
// address account3 = managedOpenfortFactory.createAccountWithNonce(deployAddress, "", 3);
// address account3 = managedOpenfortFactory.createAccountWithNonce(deployAddress, 3);
// console.log(
// "Factory at address %s has created an account at address %s", address(managedOpenfortFactory), account3
// );
// assert(account2 != account3);
// address account4 = managedOpenfortFactory.createAccountWithNonce(deployAddress, "", 4);
// address account4 = managedOpenfortFactory.createAccountWithNonce(deployAddress, 4);
// console.log(
// "Factory at address %s has created an account at address %s", address(managedOpenfortFactory), account4
// );
Expand Down
6 changes: 3 additions & 3 deletions script/deployStaticAccounts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ contract StaticOpenfortDeploy is Script {
// address account1 = staticOpenfortFactory.accountImplementation();

// The first call should create a new account, while the second will just return the corresponding account address
address account2 = staticOpenfortFactory.createAccountWithNonce(deployAddress, "", 1);
address account2 = staticOpenfortFactory.createAccountWithNonce(deployAddress, "1");
console.log(
"Factory at address %s has created an account at address %s", address(staticOpenfortFactory), account2
);
// assert(account1 != account2);
// address account3 = staticOpenfortFactory.createAccountWithNonce(deployAddress, "", 3);
// address account3 = staticOpenfortFactory.createAccountWithNonce(deployAddress, 3);
// console.log(
// "Factory at address %s has created an account at address %s", address(staticOpenfortFactory), account3
// );
// assert(account2 != account3);
// address account4 = staticOpenfortFactory.createAccountWithNonce(deployAddress, "", 4);
// address account4 = staticOpenfortFactory.createAccountWithNonce(deployAddress, 4);
// console.log(
// "Factory at address %s has created an account at address %s", address(staticOpenfortFactory), account4
// );
Expand Down
6 changes: 3 additions & 3 deletions script/deployUpgradeableAccounts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ contract UpgradeableOpenfortDeploy is Script {
address account1 = upgradeableOpenfortFactory.accountImplementation();

// The first call should create a new account, while the second will just return the corresponding account address
address account2 = upgradeableOpenfortFactory.createAccountWithNonce(deployAddress, "", 1);
address account2 = upgradeableOpenfortFactory.createAccountWithNonce(deployAddress, "1");
console.log(
"Factory at address %s has created an account at address %s", address(upgradeableOpenfortFactory), account2
);

assert(account1 != account2);
// address account3 = upgradeableOpenfortFactory.createAccountWithNonce(deployAddress, "", 3);
// address account3 = upgradeableOpenfortFactory.createAccountWithNonce(deployAddress, 3);
// console.log(
// "Factory at address %s has created an account at address %s", address(upgradeableOpenfortFactory), account3
// );
// assert(account2 != account3);
// address account4 = upgradeableOpenfortFactory.createAccountWithNonce(deployAddress, "", 4);
// address account4 = upgradeableOpenfortFactory.createAccountWithNonce(deployAddress, 4);
// console.log(
// "Factory at address %s has created an account at address %s", address(upgradeableOpenfortFactory), account4
// );
Expand Down
12 changes: 6 additions & 6 deletions test/foundry/core/managed/ManagedOpenfortAccountTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ contract ManagedOpenfortAccountTest is Test {
// deploy account factory
managedOpenfortFactory = new ManagedOpenfortFactory(address(openfortBeacon));
// Create an static account wallet and get its address
account = managedOpenfortFactory.createAccountWithNonce(accountAdmin, "", 1);
account = managedOpenfortFactory.createAccountWithNonce(accountAdmin, "1");
// deploy a new TestCounter
testCounter = new TestCounter();
// deploy a new TestToken (ERC20)
Expand All @@ -162,19 +162,19 @@ contract ManagedOpenfortAccountTest is Test {
function testCreateAccountWithNonceViaFactory() public {
// Get the counterfactual address
vm.prank(factoryAdmin);
address account2 = managedOpenfortFactory.getAddressWithNonce(accountAdmin, 2);
address account2 = managedOpenfortFactory.getAddressWithNonce(accountAdmin, "2");

// Expect that we will see an event containing the account and admin
vm.expectEmit(true, true, false, true);
emit AccountCreated(account2, accountAdmin);

// Deploy a static account to the counterfactual address
vm.prank(factoryAdmin);
managedOpenfortFactory.createAccountWithNonce(accountAdmin, "", 2);
managedOpenfortFactory.createAccountWithNonce(accountAdmin, "2");

// Make sure the counterfactual address has not been altered
vm.prank(factoryAdmin);
assertEq(account2, managedOpenfortFactory.getAddressWithNonce(accountAdmin, 2));
assertEq(account2, managedOpenfortFactory.getAddressWithNonce(accountAdmin, "2"));
}

/*
Expand Down Expand Up @@ -1032,7 +1032,7 @@ contract ManagedOpenfortAccountTest is Test {
function testUpgradeTo() public {
// Create a managed account wallet using the old implementation and get its address
vm.prank(factoryAdmin);
address payable accountOld = payable(managedOpenfortFactory.createAccountWithNonce(accountAdmin, "", 2));
address payable accountOld = payable(managedOpenfortFactory.createAccountWithNonce(accountAdmin, "2"));
ManagedOpenfortAccount managedAccount = ManagedOpenfortAccount(accountOld);
assertEq(managedAccount.version(), 1);

Expand All @@ -1053,7 +1053,7 @@ contract ManagedOpenfortAccountTest is Test {

// Same for new accounts. From now on, they have the new version.
vm.prank(factoryAdmin);
address payable account3 = payable(managedOpenfortFactory.createAccountWithNonce(accountAdmin, "", 3));
address payable account3 = payable(managedOpenfortFactory.createAccountWithNonce(accountAdmin, "3"));
ManagedOpenfortAccount managedAccount3 = ManagedOpenfortAccount(account3);
managedAccount3.version();
}
Expand Down
Loading

0 comments on commit f0c31b9

Please sign in to comment.