From 2eb3e24f386d1839e866632cc24e3d66423a2f9f Mon Sep 17 00:00:00 2001 From: livingrockrises <90545960+livingrockrises@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:55:42 +0400 Subject: [PATCH] respond to PR comments --- contracts/libs/AddressUtils.sol | 30 +++++++++++++++++++ .../sponsorship/SponsorshipPaymaster.sol | 21 +++---------- 2 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 contracts/libs/AddressUtils.sol diff --git a/contracts/libs/AddressUtils.sol b/contracts/libs/AddressUtils.sol new file mode 100644 index 0000000..43cb832 --- /dev/null +++ b/contracts/libs/AddressUtils.sol @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + + +/** + * Utility library of inline functions on addresses + */ +library AddressUtils { + + /** + * Returns whether the target address is a contract + * @dev This function will return false if invoked during the constructor of a contract, + * as the code is not actually created until after the constructor finishes. + * @param _addr address to check + * @return whether the target address is a contract + */ + function isContract(address _addr) internal view returns (bool) { + uint256 size; + // XXX Currently there is no better way to check if there is a contract in an address + // than to check the size of the code at that address. + // See https://ethereum.stackexchange.com/a/14016/36603 + // for more details about how this works. + // TODO Check this again before the Serenity release, because all addresses will be + // contracts then. + // solium-disable-next-line security/no-inline-assembly + assembly { size := extcodesize(_addr) } + return size > 0; + } + +} \ No newline at end of file diff --git a/contracts/sponsorship/SponsorshipPaymaster.sol b/contracts/sponsorship/SponsorshipPaymaster.sol index 68a87a9..89b2c00 100644 --- a/contracts/sponsorship/SponsorshipPaymaster.sol +++ b/contracts/sponsorship/SponsorshipPaymaster.sol @@ -9,6 +9,7 @@ import {UserOperation, UserOperationLib} from "@account-abstraction/contracts/in import "../BasePaymaster.sol"; import {SponsorshipPaymasterErrors} from "../common/Errors.sol"; import {MathLib} from "../libs/MathLib.sol"; +import {AddressUtils} from "../libs/AddressUtils.sol"; import {ISponsorshipPaymaster} from "../interfaces/paymasters/ISponsorshipPaymaster.sol"; /** @@ -29,6 +30,7 @@ contract SponsorshipPaymaster is ISponsorshipPaymaster { using ECDSA for bytes32; + using AddressUtils for address; using UserOperationLib for UserOperation; uint32 private constant PRICE_DENOMINATOR = 1e6; @@ -72,7 +74,7 @@ contract SponsorshipPaymaster is * @param paymasterId dapp identifier for which deposit is being made */ function depositFor(address paymasterId) external payable nonReentrant { - if(isContract(paymasterId)) revert PaymasterIdCannotBeContract(); + if(paymasterId.isContract()) revert PaymasterIdCannotBeContract(); if (paymasterId == address(0)) revert PaymasterIdCannotBeZero(); if (msg.value == 0) revert DepositCanNotBeZero(); paymasterIdBalances[paymasterId] += msg.value; @@ -109,7 +111,7 @@ contract SponsorshipPaymaster is function setFeeCollector( address _newFeeCollector ) external payable onlyOwner { - if(isContract(_newFeeCollector)) revert FeeCollectorCannotBeContract(); + if(_newFeeCollector.isContract()) revert FeeCollectorCannotBeContract(); if (_newFeeCollector == address(0)) revert FeeCollectorCannotBeZero(); address oldFeeCollector = feeCollector; assembly { @@ -360,19 +362,4 @@ contract SponsorshipPaymaster is maxPriorityFeePerGas + block.basefee ); } - - /** - * @notice Will return true if provided address is a contract - * @param account Address to verify if contract or not - * @dev This contract will return false if called within the constructor of - * a contract's deployment, as the code is not yet stored on-chain. - */ - function isContract(address account) internal view returns (bool) { - uint256 csize; - // solhint-disable-next-line no-inline-assembly - assembly { - csize := extcodesize(account) - } - return csize != 0; - } }