Skip to content
Verified contractExactApache-2.0

BizGuard

Address0xd0d1a7a4914a113ef6aa388f8334a7fac28b2cd4
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 1,000,000 runs
EVM version
prague
Via IR
yes
License
Apache-2.0
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,546,678
Creation transaction
0x726ff35dd3…af363f3929

src/infra/BizGuard.sol

// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.30;

import {MultiStepOwnable} from "../security/MultiStepOwnable.sol";

/**
 * @title BizGuard - A guard contract for BizPasskeySession.
 * @dev BizGuard is an ownable smart contract with 4337 flag to enable/disable ERC-4337 of BizPasskeySession.
 * @author Trust Wallet
 * @author David Kim - @PowerStream3604
 */
contract BizGuard is MultiStepOwnable {
    event FlagSet(bool flag);

    uint256 public constant OWNERSHIP_TRANSFER_BUFFER = 259200; // 3 days

    mapping(address => uint256) public accountNonce;
    bool erc4337Flag;

    /**
     * @notice constructor
     * @dev Sets the initial owner of the BizGuard.
     * @param owner address.
     */
    constructor(address owner) MultiStepOwnable(owner, OWNERSHIP_TRANSFER_BUFFER) {}

    /**
     * @notice Increment nonce of the msg.sender.
     * @dev Nonce can only increment and not decrement.
     * @return nonce uint256 nonce of the msg.sender.
     */
    function incrementNonce() external returns (uint256 nonce) {
        nonce = accountNonce[msg.sender];
        unchecked {
            ++accountNonce[msg.sender];
        }
    }

    /**
     * @notice Return if the 4337 flag is enabled.
     *
     * @return isEnabled bool flag whether 4337 is enabled.
     */
    function is4337Enabled() external view returns (bool isEnabled) {
        isEnabled = erc4337Flag;
    }

    /**
     * @notice Set function to set 4337 Flag.
     * @dev This function can only be called by the owner.
     *
     * @param flag bool value of the flag.
     */
    function set4337Flag(bool flag) external onlyOwner {
        erc4337Flag = flag;
        emit FlagSet(flag);
    }
}

src/security/MultiStepOwnable.sol

// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.30;

/**
 * @title MultiStepOwnable - A secure smart contract ownership management framework.
 * @dev Most important concepts:
 *      - MultiStepOwnable has 3 separate steps for ownership transfer.
 *          - initiateOwnershipTransfer(): Initiating the ownership transfer process.
 *          - confirmOwnershipTransfer(): Confirming the ownership transfer.
 *          - acceptOwnership(): Accepting the ownership transfer, by the new owner.
 *      - There is an enforced timewindow between initiateOwnershipTransfer() and confirmOwnershipTransfer().
 *        This timewindow is set during the deployment(constructor) by default.
 *        During the timewindow, the framework expects the owner to validate the preconfirmed owner address on-chain.
 *        By reviewing the new address on-chain, it reduces the risk of blind signing and makes it possible to revert from operational mistakes/fraud.
 *      - Even after the ownership transfer is confirmed with confirmOwnershipTransfer(), it can be canceled anytime before the transfer is accepted, by the original owner.
 *      - MultiStepOwnable is defined in ERC-8023 standard.
 *      - onlyOwner() modifier can be used to gatekeep functions same as Ownable & Ownable2Step.
 * @author Trust Wallet
 * @author David Kim - @PowerStream3604
 */
abstract contract MultiStepOwnable {
    struct OwnerStorage {
        address owner;
        address preConfirmedOwner;
        address pendingOwner;
        uint256 initiatedTimestamp;
    }

    error NotOwner();
    error ZeroAddress();
    error PendingOwnerMisaligned();
    error PreConfirmedOwnerMisaligned();
    error UnauthorizedPendingOwner(address);
    error OverMaxBuffer();
    error InsecureTimeBuffer();

    event OwnershipTransferStarted(address prevPreConfirmedOwner, address newPreConfirmedOwner);
    event OwnershipTransferDoubleChecked(address prevPendingOwner, address pendingOwner);
    event OwnershipTransferred(address prevOwner, address newOwner);
    event PendingOwnershipTransferCancelled();

    // keccak256(abi.encode(uint256(keccak256("trustwallet.MultiStepOwnable.storage.v0.1.0")) - 1)) & ~bytes32(uint256(0xff));
    bytes32 private constant STORAGE_SLOT = 0xe055784b1cf39d2612b71f02867678e67484d21d65d5b57a5f69a250dd961a00;

    uint256 private immutable FIXED_OWNERSHIP_TRANSFER_BUFFER;
    uint256 private constant MAX_BUFFER = 1296000; // 15 days

    /// @dev Requires the caller(msg.sender) to be owner.
    modifier onlyOwner() {
        if (owner() != msg.sender) {
            revert NotOwner();
        }
        _;
    }

    /**
     * @notice Sets the initial owner and the timeBuffer for ownership transfer.
     *
     * @param initialOwner Address of the initial owner.
     * @param timeBuffer Uint256 ownership transfer time buffer.
     */
    constructor(address initialOwner, uint256 timeBuffer) {
        if (timeBuffer > MAX_BUFFER) {
            revert OverMaxBuffer();
        }
        _zeroAddressCheck(initialOwner);

        FIXED_OWNERSHIP_TRANSFER_BUFFER = timeBuffer;
        _getStorage().owner = initialOwner;
        emit OwnershipTransferred(address(0), initialOwner);
    }

    /**
     * @notice Private function to set preConfirmedOwner.
     * @dev Sets preconfirmed owner.
     *      Sets initiated timestamp to current block.timestamp.
     *      Sets pending owner to address zero.
     *
     * @param _preConfirmedOwner Address of new preConfirmedOwner.
     */
    function _setPreConfirmedOwner(address _preConfirmedOwner) private {
        _zeroAddressCheck(_preConfirmedOwner);

        OwnerStorage storage ownerStorage = _getStorage();
        address prevPreConfirmedOwner = ownerStorage.preConfirmedOwner;
        ownerStorage.initiatedTimestamp = block.timestamp;
        ownerStorage.preConfirmedOwner = _preConfirmedOwner;
        ownerStorage.pendingOwner = address(0);
        emit OwnershipTransferStarted(prevPreConfirmedOwner, _preConfirmedOwner);
    }

    /**
     * @notice Private function to set pendingOwner.
     * @dev Sets pending owner.
     *      Resets initiated timestamp to zero.
     *      Resets preconfirmed owner to zero.
     *
     * @param _pendingOwner Address of new pendingOwner.
     */
    function _setPendingOwner(address _pendingOwner) private {
        _zeroAddressCheck(_pendingOwner);

        OwnerStorage storage ownerStorage = _getStorage();
        if (_pendingOwner != ownerStorage.preConfirmedOwner) {
            revert PreConfirmedOwnerMisaligned();
        }
        uint256 initiatedTimestamp = ownerStorage.initiatedTimestamp;
        if (initiatedTimestamp == 0 || block.timestamp < initiatedTimestamp + FIXED_OWNERSHIP_TRANSFER_BUFFER) {
            revert InsecureTimeBuffer();
        }

        address prevPendingOwner = ownerStorage.pendingOwner;
        ownerStorage.initiatedTimestamp = 0;
        ownerStorage.preConfirmedOwner = address(0);
        ownerStorage.pendingOwner = _pendingOwner;
        emit OwnershipTransferDoubleChecked(prevPendingOwner, _pendingOwner);
    }

    /**
     * @notice Private function to set owner.
     * @dev Sets owner.
     *      Resets pending owner to zero.
     *
     * @param _owner Address of new owner.
     */
    function _setOwner(address _owner) private {
        _zeroAddressCheck(_owner);

        OwnerStorage storage ownerStorage = _getStorage();
        if (_owner != ownerStorage.pendingOwner) {
            revert PendingOwnerMisaligned();
        }

        address prevOwner = ownerStorage.owner;
        ownerStorage.pendingOwner = address(0);
        ownerStorage.owner = _owner;
        emit OwnershipTransferred(prevOwner, _owner);
    }

    /**
     * @notice Private function to cancel pending ownership transfer.
     * @dev Resets pending owner to zero.
     */
    function _cancelPendingOwnershipTransfer() private {
        OwnerStorage storage ownerStorage = _getStorage();

        ownerStorage.pendingOwner = address(0);
        emit PendingOwnershipTransferCancelled();
    }

    /**
     * @notice Returns the address of preconfirmed owner.
     * @return Address of preconfirmed owner.
     */
    function preConfirmedOwner() public view returns (address) {
        return _getStorage().preConfirmedOwner;
    }

    /**
     * @notice Returns the address of pending owner.
     * @return Address of pending owner.
     */
    function pendingOwner() public view returns (address) {
        return _getStorage().pendingOwner;
    }

    /**
     * @notice Returns the address of owner.
     * @return Address of owner.
     */
    function owner() public view returns (address) {
        return _getStorage().owner;
    }

    /**
     * @notice Returns the storage of MultiStepOwnable.
     * @return ownerStorage OwnerStorage of MultiStepOwnable.
     */
    function _getStorage() internal pure returns (OwnerStorage storage ownerStorage) {
        bytes32 slot = STORAGE_SLOT;
        assembly {
            ownerStorage.slot := slot
        }
    }

    /**
     * @notice Initiates the ownership transfer. 1st step in ownership transfer.
     * @dev Internally calls _setPreConfirmedOwner().
     *
     * @param newOwner Address of new owner, for ownership transfer initiation.
     */
    function initiateOwnershipTransfer(address newOwner) external onlyOwner {
        _setPreConfirmedOwner(newOwner);
    }

    /**
     * @notice Confirms the ownership transfer. Will revert if ownershipTransferBuffer didn't pass after initiation.
     * @dev Internally calls _setPendingOwner().
     *
     * @param newOwner Address of new owner, for ownership transfer confirmation.
     */
    function confirmOwnershipTransfer(address newOwner) external onlyOwner {
        _setPendingOwner(newOwner);
    }

    /**
     * @notice Cancel pending ownership transfer.
     * @dev Internally calls _cancelPendingOwnershipTransfer().
     */
    function cancelPendingOwnershipTransfer() external onlyOwner {
        _cancelPendingOwnershipTransfer();
    }

    /**
     * @notice Accept ownership. Called by the new(confirmed) owner.
     * @dev msg.sender should be pendingOwner.
     */
    function acceptOwnership() external {
        if (pendingOwner() != msg.sender) {
            revert UnauthorizedPendingOwner(msg.sender);
        }
        _setOwner(msg.sender);
    }

    /**
     * @notice Returns ownership transfer buffer. Enforced between initiation and confirmation.
     */
    function getOwnershipTransferBuffer() external view returns (uint256) {
        return FIXED_OWNERSHIP_TRANSFER_BUFFER;
    }

    /**
     * @notice Performs zero address check.
     *
     * @param addr Address to be check if zero.
     */
    function _zeroAddressCheck(address addr) private pure {
        if (addr == address(0)) {
            revert ZeroAddress();
        }
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 1000000,
    "enabled": true
  },
  "evmVersion": "prague",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@aa/contracts/=lib/account-abstraction/contracts/",
    "account-abstraction/=lib/account-abstraction/contracts/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.