Skip to content

Commit

Permalink
respond to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed Nov 29, 2023
1 parent a13d668 commit 2eb3e24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
30 changes: 30 additions & 0 deletions contracts/libs/AddressUtils.sol
Original file line number Diff line number Diff line change
@@ -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;
}

}
21 changes: 4 additions & 17 deletions contracts/sponsorship/SponsorshipPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2eb3e24

Please sign in to comment.