Skip to content

Commit

Permalink
use custom errors and IER1271.isValidSignature.selector
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng committed Nov 21, 2023
1 parent 74ad1a2 commit 30479c6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
19 changes: 13 additions & 6 deletions contracts/base/ERC721Permit.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

import {ChainId} from "../libraries/ChainId.sol";
import {IERC1271} from "../interfaces/external/IERC1271.sol";
import {IERC721Permit} from "../interfaces/IERC721Permit.sol";

error PermitExpired();
error InvalidSignature();
error ApprovalToOwner();
error Unauthorized();

/// @title ERC721 with permit
/// @notice Nonfungible tokens that support an approve via signature, i.e. permit
abstract contract ERC721Permit is ERC721, IERC721Permit {
Expand Down Expand Up @@ -51,7 +56,7 @@ abstract contract ERC721Permit is ERC721, IERC721Permit {
payable
override
{
require(block.timestamp <= deadline, "Permit expired");
if (block.timestamp > deadline) revert PermitExpired();

bytes32 digest = keccak256(
abi.encodePacked(
Expand All @@ -61,14 +66,16 @@ abstract contract ERC721Permit is ERC721, IERC721Permit {
)
);
address owner = ownerOf(tokenId);
require(spender != owner, "ERC721Permit: approval to current owner");
if (spender == owner) revert ApprovalToOwner();

if (owner.code.length > 0) {
require(IERC1271(owner).isValidSignature(digest, abi.encodePacked(r, s, v)) == 0x1626ba7e, "Unauthorized");
if (IERC1271(owner).isValidSignature(digest, abi.encodePacked(r, s, v)) != IERC1271.isValidSignature.selector) {
revert Unauthorized();
}
} else {
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress != address(0), "Invalid signature");
require(recoveredAddress == owner, "Unauthorized");
if (recoveredAddress == address(0)) revert InvalidSignature();
if (recoveredAddress != owner) revert Unauthorized();
}

_approve(spender, tokenId);
Expand Down
4 changes: 3 additions & 1 deletion contracts/base/PeripheryPayments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {IPeripheryPayments} from "../interfaces/IPeripheryPayments.sol";

import "../libraries/TransferHelper.sol";

error InsufficientToken();

abstract contract PeripheryPayments is IPeripheryPayments {
/// @inheritdoc IPeripheryPayments
function sweepToken(address token, uint256 amountMinimum, address recipient) public payable override {
uint256 balanceToken = IERC20(token).balanceOf(address(this));
require(balanceToken >= amountMinimum, "Insufficient token");
if (balanceToken < amountMinimum) revert InsufficientToken();

if (balanceToken > 0) {
TransferHelper.safeTransfer(IERC20Minimal(token), recipient, balanceToken);
Expand Down
4 changes: 3 additions & 1 deletion contracts/base/PeripheryValidation.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

error TransactionTooOld();

abstract contract PeripheryValidation {
modifier checkDeadline(uint256 deadline) {
require(block.timestamp <= deadline, "Transaction too old");
if (block.timestamp > deadline) revert TransactionTooOld();
_;
}
}
10 changes: 7 additions & 3 deletions contracts/libraries/TransferHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ pragma solidity ^0.8.15;

import {IERC20Minimal} from "@uniswap/v4-core/contracts/interfaces/external/IERC20Minimal.sol";

error TransferFailed();
error STF();
error STE();

/// @title TransferHelper
/// @notice Contains helper methods for interacting with ERC20 tokens that do not consistently return true/false
/// @dev implementation from https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol#L63
Expand Down Expand Up @@ -37,7 +41,7 @@ library TransferHelper {
)
}

require(success, "TRANSFER_FAILED");
if (!success) revert TransferFailed();
}

/// @notice Transfers tokens from from to a recipient
Expand All @@ -49,7 +53,7 @@ library TransferHelper {
function safeTransferFrom(IERC20Minimal token, address from, address to, uint256 value) internal {
(bool success, bytes memory data) =
address(token).call(abi.encodeWithSelector(IERC20Minimal.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), "STF");
if (!success || !(data.length == 0 || abi.decode(data, (bool)))) revert STF();
}

/// @notice Transfers ETH to the recipient address
Expand All @@ -58,6 +62,6 @@ library TransferHelper {
/// @param value The value to be transferred
function safeTransferETH(address to, uint256 value) internal {
(bool success,) = to.call{value: value}(new bytes(0));
require(success, "STE");
if (!success) revert STE();
}
}

0 comments on commit 30479c6

Please sign in to comment.