Skip to content

Commit

Permalink
Huge refactor under construction
Browse files Browse the repository at this point in the history
  • Loading branch information
eloi010 committed Nov 20, 2023
1 parent 129f161 commit a1e24d7
Show file tree
Hide file tree
Showing 19 changed files with 2,513 additions and 2,544 deletions.
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions contracts/core/base/BaseOpenfortFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity =0.8.19;

import {IBaseOpenfortFactory} from "../../interfaces/IBaseOpenfortFactory.sol";

/**
* @title BaseOpenfortFactory (Non-upgradeable)
* @notice Contract to create an on-chain factory to deploy new OpenfortAccounts.
* It inherits from:
* - IBaseOpenfortFactory
*/
abstract contract BaseOpenfortFactory is IBaseOpenfortFactory {
address public entrypointContract;
address public accountImplementation;
uint256 public recoveryPeriod;
uint256 public securityPeriod;
uint256 public securityWindow;
uint256 public lockPeriod;
address public openfortGuardian;

error InsecurePeriod();

constructor(
address _entrypoint,
address _accountImplementation,
uint256 _recoveryPeriod,
uint256 _securityPeriod,
uint256 _securityWindow,
uint256 _lockPeriod,
address _openfortGuardian
) {
if (_entrypoint == address(0) || _accountImplementation == address(0) || _openfortGuardian == address(0)) {
revert ZeroAddressNotAllowed();
}
if (_lockPeriod < _recoveryPeriod || _recoveryPeriod < _securityPeriod + _securityWindow) {
revert InsecurePeriod();
}
entrypointContract = _entrypoint;
accountImplementation = _accountImplementation;
recoveryPeriod = _recoveryPeriod;
securityPeriod = _securityPeriod;
securityWindow = _securityWindow;
lockPeriod = _lockPeriod;
openfortGuardian = _openfortGuardian;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import {
Ownable2StepUpgradeable,
OwnableUpgradeable
} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";

import {BaseOpenfortAccount, IEntryPoint, SafeCastUpgradeable, ECDSAUpgradeable} from "../BaseOpenfortAccount.sol";
import {BaseOpenfortAccount, IEntryPoint, ECDSAUpgradeable} from "../base/BaseOpenfortAccount.sol";

/**
* @title RecoverableOpenfortAccount
* @notice Openfort account with session keys, guardians and pausability following the ERC-4337 standard.
* It inherits from:
* - BaseOpenfortAccount
* - UUPSUpgradeable
* - Ownable2StepUpgradeable
*/
contract RecoverableOpenfortAccount is BaseOpenfortAccount, Ownable2StepUpgradeable, UUPSUpgradeable {
abstract contract BaseRecoverableAccount is BaseOpenfortAccount, Ownable2StepUpgradeable {
using ECDSAUpgradeable for bytes32;

address internal entrypointContract;
Expand Down Expand Up @@ -122,8 +120,6 @@ contract RecoverableOpenfortAccount is BaseOpenfortAccount, Ownable2StepUpgradea
emit GuardianAdded(_openfortGuardian);
}

function _authorizeUpgrade(address) internal override onlyOwner {}

/**
* Return the current EntryPoint
*/
Expand All @@ -135,15 +131,6 @@ contract RecoverableOpenfortAccount is BaseOpenfortAccount, Ownable2StepUpgradea
return OwnableUpgradeable.owner();
}

/**
* Update the EntryPoint address
*/
function updateEntryPoint(address _newEntrypoint) external onlyOwner {
if (_newEntrypoint == address(0)) revert ZeroAddressNotAllowed();
emit EntryPointUpdated(entrypointContract, _newEntrypoint);
entrypointContract = _newEntrypoint;
}

/**
* Locking functionalities *
*/
Expand Down Expand Up @@ -238,7 +225,7 @@ contract RecoverableOpenfortAccount is BaseOpenfortAccount, Ownable2StepUpgradea

/**
* @notice Lets the owner propose a guardian to its Openfort account.
* The first guardian is added immediately (see constructor). All following proposals must be confirmed
* The first guardians are added when the account is created. All following proposals must be confirmed
* by calling the confirmGuardianProposal() method. Only the owner can add guardians.
* Guardians must either be an EOA or a contract with an owner() (ERC-173).
* @param _guardian The guardian to propose.
Expand Down
3 changes: 1 addition & 2 deletions contracts/core/eip6551/EIP6551OpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC6551Account} from "erc6551/src/interfaces/IERC6551Account.sol";
import {IERC6551Executable} from "erc6551/src/interfaces/IERC6551Executable.sol";
import {ERC6551AccountLib} from "erc6551/src/lib/ERC6551AccountLib.sol";

import {BaseOpenfortAccount, IEntryPoint, ECDSAUpgradeable} from "../BaseOpenfortAccount.sol";
import {BaseOpenfortAccount, IEntryPoint, ECDSAUpgradeable} from "../base/BaseOpenfortAccount.sol";

/**
* @title EIP6551OpenfortAccount (Non-upgradeable)
Expand Down
20 changes: 4 additions & 16 deletions contracts/core/managed/ManagedOpenfortAccount.sol
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity =0.8.19;

// Base account contract to inherit from and EntryPoint interface
import {BaseRecoverableAccount, IEntryPoint} from "../base/BaseRecoverableAccount.sol";
import {
Ownable2StepUpgradeable,
OwnableUpgradeable
} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {BaseOpenfortAccount, IEntryPoint} from "../BaseOpenfortAccount.sol";

/**
* @title ManagedOpenfortAccount (Upgradeable via Beacon)
* @notice Smart contract wallet managed via Beacon with session keys following the ERC-4337 standard.
* It inherits from:
* - BaseOpenfortAccount
* - BaseRecoverableAccount
*/
contract ManagedOpenfortAccount is BaseOpenfortAccount, Ownable2StepUpgradeable {
contract ManagedOpenfortAccount is BaseRecoverableAccount {
address private constant ENTRYPOINTCONTRACT = 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789;

/*
* @notice Initialize the smart contract wallet.
*/
function initialize(address _defaultAdmin) public initializer {
if (_defaultAdmin == address(0)) {
revert ZeroAddressNotAllowed();
}
_transferOwnership(_defaultAdmin);
__EIP712_init("Openfort", "0.5");
}

function owner() public view virtual override(BaseOpenfortAccount, OwnableUpgradeable) returns (address) {
function owner() public view virtual override returns (address) {
return OwnableUpgradeable.owner();
}

Expand Down
69 changes: 46 additions & 23 deletions contracts/core/managed/ManagedOpenfortFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,46 @@ import {UpgradeableBeacon} from "@openzeppelin/contracts/proxy/beacon/Upgradeabl

// Smart wallet implementation to use
import {ManagedOpenfortAccount} from "./ManagedOpenfortAccount.sol";
import {OpenfortBeaconProxy} from "./OpenfortBeaconProxy.sol";
import {OpenfortManagedProxy} from "./OpenfortManagedProxy.sol";

// Interfaces
import {IBaseOpenfortFactory} from "../../interfaces/IBaseOpenfortFactory.sol";
import {BaseOpenfortFactory} from "../base/BaseOpenfortFactory.sol";
import {IEntryPoint} from "account-abstraction/interfaces/IEntryPoint.sol";

/**
* @title ManagedOpenfortFactory (Non-upgradeable)
* @notice Contract to create an on-chain factory to deploy new ManagedOpenfortAccounts.
* It uses OpenZeppelin's Create2 and OpenfortBeaconProxy libraries.
* It uses OpenZeppelin's Create2 and OpenfortManagedProxy libraries.
* It inherits from:
* - IBaseOpenfortFactory
* - UpgradeableBeacon to also work as the beacon
*/
contract ManagedOpenfortFactory is IBaseOpenfortFactory, UpgradeableBeacon {
address internal entrypointContract;

constructor(address _owner, address _entrypoint, address _accountImplementation)
contract ManagedOpenfortFactory is BaseOpenfortFactory, UpgradeableBeacon {
constructor(
address _owner,
address _entrypoint,
address _accountImplementation,
uint256 _recoveryPeriod,
uint256 _securityPeriod,
uint256 _securityWindow,
uint256 _lockPeriod,
address _openfortGuardian
)
BaseOpenfortFactory(
_entrypoint,
_accountImplementation,
_recoveryPeriod,
_securityPeriod,
_securityWindow,
_lockPeriod,
_openfortGuardian
)
UpgradeableBeacon(_accountImplementation)
{
if (_owner == address(0) || _entrypoint == address(0) || _accountImplementation == address(0)) {
if (_owner == address(0)) {
revert ZeroAddressNotAllowed();
}
_transferOwnership(_owner);
entrypointContract = _entrypoint;
}

/*
Expand All @@ -43,11 +58,10 @@ contract ManagedOpenfortFactory is IBaseOpenfortFactory, UpgradeableBeacon {
if (account.code.length != 0) return account;

emit AccountCreated(account, _admin);
account = address(
new OpenfortBeaconProxy{salt: salt}(
address(this),
abi.encodeCall(ManagedOpenfortAccount.initialize, (_admin))
)

account = address(new OpenfortManagedProxy{salt: salt}(address(this), ""));
ManagedOpenfortAccount(payable(account)).initialize(
_admin, entrypointContract, recoveryPeriod, securityPeriod, securityWindow, lockPeriod, openfortGuardian
);
}

Expand All @@ -60,23 +74,32 @@ contract ManagedOpenfortFactory is IBaseOpenfortFactory, UpgradeableBeacon {
salt,
keccak256(
abi.encodePacked(
type(OpenfortBeaconProxy).creationCode,
abi.encode(address(this), abi.encodeCall(ManagedOpenfortAccount.initialize, (_admin)))
type(OpenfortManagedProxy).creationCode,
abi.encode(address(this), "")
)
)
);
}

function accountImplementation() external view override returns (address) {
return implementation();
}


/**
* Add stake for this factory.
* This method can also carry eth value to add to the current stake.
* @param unstakeDelaySec - the unstake delay for this factory. Can only be increased.
* @dev {See BaseOpenfortFactory}
*/
function addStake(uint32 unstakeDelaySec) external payable onlyOwner {
IEntryPoint(entrypointContract).addStake{value: msg.value}(unstakeDelaySec);
}

/**
* @dev {See BaseOpenfortFactory}
*/
function unlockStake() external onlyOwner {
IEntryPoint(entrypointContract).unlockStake();
}

/**
* @dev {See BaseOpenfortFactory}
*/
function withdrawStake(address payable withdrawAddress) external onlyOwner {
IEntryPoint(entrypointContract).withdrawStake(withdrawAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pragma solidity =0.8.19;
import {BeaconProxy} from "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol";

/**
* @title OpenfortBeaconProxy (Non-upgradeable)
* @title OpenfortManagedProxy (Non-upgradeable)
* @notice Contract to create the beacon. It determines the implementation contract.
* It inherits from:
* - BeaconProxy
*/
contract OpenfortBeaconProxy is BeaconProxy {
contract OpenfortManagedProxy is BeaconProxy {
constructor(address beacon, bytes memory data) BeaconProxy(beacon, data) {}

function implementation() external view returns (address) {
Expand Down
18 changes: 0 additions & 18 deletions contracts/core/recoverable/OpenfortRecoverableProxy.sol

This file was deleted.

83 changes: 0 additions & 83 deletions contracts/core/recoverable/RecoverableOpenfortFactory.sol

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/core/upgradeable/OpenfortUpgradeableProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ contract OpenfortUpgradeableProxy is ERC1967Proxy {
constructor(address _logic, bytes memory _data) ERC1967Proxy(_logic, _data) {}

function implementation() external view returns (address) {
return _getImplementation();
return _implementation();
}
}
Loading

0 comments on commit a1e24d7

Please sign in to comment.