Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor signatureValidator #295

Merged
merged 6 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contracts/AMMWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import "./utils/StrategyBase.sol";
import "./utils/AMMLibEIP712.sol";
import "./utils/BaseLibEIP712.sol";
import "./utils/LibConstant.sol";
import "./utils/SignatureValidator.sol";
import { validateSignature } from "./utils/SignatureValidator.sol";

contract AMMWrapper is IAMMWrapper, StrategyBase, ReentrancyGuard, BaseLibEIP712, SignatureValidator {
contract AMMWrapper is IAMMWrapper, StrategyBase, ReentrancyGuard, BaseLibEIP712 {
using SafeMath for uint16;
using SafeMath for uint256;
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -229,7 +229,7 @@ contract AMMWrapper is IAMMWrapper, StrategyBase, ReentrancyGuard, BaseLibEIP712
// Verify user signature
transactionHash = AMMLibEIP712._getOrderHash(_order);
bytes32 EIP712SignDigest = getEIP712Hash(transactionHash);
require(isValidSignature(_order.userAddr, EIP712SignDigest, bytes(""), _sig), "AMMWrapper: invalid user signature");
require(validateSignature(_order.userAddr, EIP712SignDigest, _sig), "AMMWrapper: invalid user signature");
// Set transaction as seen, PermanentStorage would throw error if transaction already seen.
permStorage.setAMMTransactionSeen(transactionHash);
}
Expand Down
12 changes: 6 additions & 6 deletions contracts/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import "./utils/LibUniswapV2.sol";
import "./utils/LibUniswapV3.sol";
import "./utils/LibOrderStorage.sol";
import "./utils/LimitOrderLibEIP712.sol";
import "./utils/SignatureValidator.sol";
import { validateSignature } from "./utils/SignatureValidator.sol";

/// @title LimitOrder Contract
/// @author imToken Labs
contract LimitOrder is ILimitOrder, StrategyBase, BaseLibEIP712, SignatureValidator, ReentrancyGuard {
contract LimitOrder is ILimitOrder, StrategyBase, BaseLibEIP712, ReentrancyGuard {
using SafeMath for uint256;
using SafeERC20 for IERC20;

Expand Down Expand Up @@ -173,7 +173,7 @@ contract LimitOrder is ILimitOrder, StrategyBase, BaseLibEIP712, SignatureValida
require(_fill.recipient != address(0), "LimitOrder: recipient can not be zero address");

bytes32 fillHash = getEIP712Hash(LimitOrderLibEIP712._getFillStructHash(_fill));
require(isValidSignature(_fill.taker, fillHash, bytes(""), _fillTakerSig), "LimitOrder: Fill is not signed by taker");
require(validateSignature(_fill.taker, fillHash, _fillTakerSig), "LimitOrder: Fill is not signed by taker");

// Set fill seen to avoid replay attack.
// PermanentStorage would throw error if fill is already seen.
Expand All @@ -199,7 +199,7 @@ contract LimitOrder is ILimitOrder, StrategyBase, BaseLibEIP712, SignatureValida
})
)
);
require(isValidSignature(coordinator, allowFillHash, bytes(""), _crdParams.sig), "LimitOrder: AllowFill is not signed by coordinator");
require(validateSignature(coordinator, allowFillHash, _crdParams.sig), "LimitOrder: AllowFill is not signed by coordinator");

// Set allow fill seen to avoid replay attack
// PermanentStorage would throw error if allow fill is already seen.
Expand Down Expand Up @@ -445,7 +445,7 @@ contract LimitOrder is ILimitOrder, StrategyBase, BaseLibEIP712, SignatureValida
cancelledOrder.takerTokenAmount = 0;

bytes32 cancelledOrderHash = getEIP712Hash(LimitOrderLibEIP712._getOrderStructHash(cancelledOrder));
require(isValidSignature(_order.maker, cancelledOrderHash, bytes(""), _cancelOrderMakerSig), "LimitOrder: Cancel request is not signed by maker");
require(validateSignature(_order.maker, cancelledOrderHash, _cancelOrderMakerSig), "LimitOrder: Cancel request is not signed by maker");
}

// Set cancelled state to storage
Expand All @@ -464,7 +464,7 @@ contract LimitOrder is ILimitOrder, StrategyBase, BaseLibEIP712, SignatureValida
bool isCancelled = LibOrderStorage.getStorage().orderHashToCancelled[_orderHash];
require(!isCancelled, "LimitOrder: Order is cancelled");

require(isValidSignature(_order.maker, _orderHash, bytes(""), _orderMakerSig), "LimitOrder: Order is not signed by maker");
require(validateSignature(_order.maker, _orderHash, _orderMakerSig), "LimitOrder: Order is not signed by maker");
}

function _validateOrderTaker(LimitOrderLibEIP712.Order memory _order, address _taker) internal pure {
Expand Down
8 changes: 4 additions & 4 deletions contracts/RFQ.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import "./interfaces/IRFQ.sol";
import "./utils/StrategyBase.sol";
import "./utils/RFQLibEIP712.sol";
import "./utils/BaseLibEIP712.sol";
import "./utils/SignatureValidator.sol";
import { validateSignature } from "./utils/SignatureValidator.sol";
import "./utils/LibConstant.sol";

/// @title RFQ Contract
/// @author imToken Labs
contract RFQ is IRFQ, StrategyBase, ReentrancyGuard, SignatureValidator, BaseLibEIP712 {
contract RFQ is IRFQ, StrategyBase, ReentrancyGuard, BaseLibEIP712 {
using SafeMath for uint256;
using SafeERC20 for IERC20;
using Address for address;
Expand Down Expand Up @@ -83,9 +83,9 @@ contract RFQ is IRFQ, StrategyBase, ReentrancyGuard, SignatureValidator, BaseLib

// Validate signatures
vars.orderHash = RFQLibEIP712._getOrderHash(_order);
require(isValidSignature(_order.makerAddr, getEIP712Hash(vars.orderHash), bytes(""), _mmSignature), "RFQ: invalid MM signature");
require(validateSignature(_order.makerAddr, getEIP712Hash(vars.orderHash), _mmSignature), "RFQ: invalid MM signature");
vars.transactionHash = RFQLibEIP712._getTransactionHash(_order);
require(isValidSignature(_order.takerAddr, getEIP712Hash(vars.transactionHash), bytes(""), _userSignature), "RFQ: invalid user signature");
require(validateSignature(_order.takerAddr, getEIP712Hash(vars.transactionHash), _userSignature), "RFQ: invalid user signature");

// Set transaction as seen, PermanentStorage would throw error if transaction already seen.
permStorage.setRFQTransactionSeen(vars.transactionHash);
Expand Down
8 changes: 4 additions & 4 deletions contracts/RFQv2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { Asset } from "./utils/Asset.sol";
import { Offer } from "./utils/Offer.sol";
import { RFQOrder, getRFQOrderHash } from "./utils/RFQOrder.sol";
import { LibConstant } from "./utils/LibConstant.sol";
import { SignatureValidator } from "./utils/SignatureValidator.sol";
import { validateSignature } from "./utils/SignatureValidator.sol";
import { StrategyBase } from "./utils/StrategyBase.sol";
import { IRFQv2 } from "./interfaces/IRFQv2.sol";

/// @title RFQv2 Contract
/// @author imToken Labs
contract RFQv2 is IRFQv2, StrategyBase, TokenCollector, SignatureValidator, BaseLibEIP712 {
contract RFQv2 is IRFQv2, StrategyBase, TokenCollector, BaseLibEIP712 {
using SafeMath for uint256;
using Asset for address;

Expand Down Expand Up @@ -72,11 +72,11 @@ contract RFQv2 is IRFQv2, StrategyBase, TokenCollector, SignatureValidator, Base
permStorage.setRFQOfferFilled(offerHash);

// check maker signature
require(isValidSignature(_offer.maker, getEIP712Hash(offerHash), bytes(""), makerSignature), "invalid signature");
require(validateSignature(_offer.maker, getEIP712Hash(offerHash), makerSignature), "invalid signature");

// check taker signature if needed
if (_offer.taker != msg.sender) {
require(isValidSignature(_offer.taker, getEIP712Hash(rfqOrderHash), bytes(""), takerSignature), "invalid signature");
require(validateSignature(_offer.taker, getEIP712Hash(rfqOrderHash), takerSignature), "invalid signature");
}

// transfer takerToken to maker
Expand Down
21 changes: 0 additions & 21 deletions contracts/interfaces/IERC1271Wallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,5 @@
pragma solidity >=0.7.0;

interface IERC1271Wallet {
/**
* @notice Verifies whether the provided signature is valid with respect to the provided data
* @dev MUST return the correct magic value if the signature provided is valid for the provided data
* > The bytes4 magic value to return when signature is valid is 0x20c13b0b : bytes4(keccak256("isValidSignature(bytes,bytes)")
* > This function MAY modify Ethereum's state
* @param _data Arbitrary length data signed on the behalf of address(this)
* @param _signature Signature byte array associated with _data
* @return magicValue Magic value 0x20c13b0b if the signature is valid and 0x0 otherwise
*
*/
function isValidSignature(bytes calldata _data, bytes calldata _signature) external view returns (bytes4 magicValue);

/**
* @notice Verifies whether the provided signature is valid with respect to the provided hash
* @dev MUST return the correct magic value if the signature provided is valid for the provided hash
* > The bytes4 magic value to return when signature is valid is 0x20c13b0b : bytes4(keccak256("isValidSignature(bytes,bytes)")
* > This function MAY modify Ethereum's state
* @param _hash keccak256 hash that was signed
* @param _signature Signature byte array associated with _data
* @return magicValue Magic value 0x20c13b0b if the signature is valid and 0x0 otherwise
*/
function isValidSignature(bytes32 _hash, bytes calldata _signature) external view returns (bytes4 magicValue);
}
Loading