Skip to content

Commit

Permalink
check EIP1271 signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaryash90 committed Apr 2, 2024
1 parent a9e6477 commit 1edeae5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/extension/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract contract Ownable is IOwnable {
}

/// @dev Lets a contract admin set a new owner for the contract. The new owner must be a contract admin.
function _setupOwner(address _newOwner) internal {
function _setupOwner(address _newOwner) internal virtual {
address _prevOwner = _owner;
_owner = _newOwner;

Expand Down
39 changes: 39 additions & 0 deletions contracts/prebuilts/unaudited/airdrop/Airdrop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import "../../../eip/interface/IERC20.sol";
import "../../../eip/interface/IERC721.sol";
import "../../../eip/interface/IERC1155.sol";

interface IEIP1271 {
function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4);
}

contract Airdrop is EIP712, Initializable, Ownable {
using ECDSA for bytes32;

Expand All @@ -40,6 +44,9 @@ contract Airdrop is EIP712, Initializable, Ownable {
/// @dev Mapping from request UID => whether the request is processed.
mapping(bytes32 => bool) private processed;

/// @dev Flag to indicate if signature verification should be EIP-1271 (based on whether owner is a contract)
bool private check1271;

struct AirdropContentERC20 {
address recipient;
uint256 amount;
Expand Down Expand Up @@ -98,6 +105,8 @@ contract Airdrop is EIP712, Initializable, Ownable {
"AirdropRequestERC1155(bytes32 uid,address tokenAddress,uint256 expirationTimestamp,AirdropContentERC1155[] contents)AirdropContentERC1155(address recipient,uint256 tokenId,uint256 amount)"
);

bytes4 private constant EIP1271_MAGIC_VALUE = 0x1626ba7e;

address private constant NATIVE_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

/*///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -406,6 +415,12 @@ contract Airdrop is EIP712, Initializable, Ownable {
tokenMerkleRoot[_token] = _tokenMerkleRoot;
}

function _setupOwner(address _newOwner) internal override {
super._setupOwner(_newOwner);

check1271 = _isContract(_newOwner) ? true : false;
}

/*///////////////////////////////////////////////////////////////
Miscellaneous
//////////////////////////////////////////////////////////////*/
Expand All @@ -426,6 +441,15 @@ contract Airdrop is EIP712, Initializable, Ownable {
return false;
}

function _isContract(address account) internal view returns (bool) {
if (account == address(0)) return false;
bytes32 codehash;
assembly {
codehash := extcodehash(account)
}
return (codehash != 0x0 && codehash != 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470);
}

Check warning

Code scanning / Slither

Assembly usage Warning


function _canSetOwner() internal view virtual override returns (bool) {
return msg.sender == owner();
}
Expand Down Expand Up @@ -485,6 +509,11 @@ contract Airdrop is EIP712, Initializable, Ownable {
);

bytes32 digest = _hashTypedData(structHash);

if (check1271) {
return IEIP1271(owner()).isValidSignature(digest, signature) == EIP1271_MAGIC_VALUE;
}

address recovered = digest.recover(signature);
return recovered == owner();
}
Expand All @@ -499,6 +528,11 @@ contract Airdrop is EIP712, Initializable, Ownable {
);

bytes32 digest = _hashTypedData(structHash);

if (check1271) {
return IEIP1271(owner()).isValidSignature(digest, signature) == EIP1271_MAGIC_VALUE;
}

address recovered = digest.recover(signature);
return recovered == owner();
}
Expand All @@ -513,6 +547,11 @@ contract Airdrop is EIP712, Initializable, Ownable {
);

bytes32 digest = _hashTypedData(structHash);

if (check1271) {
return IEIP1271(owner()).isValidSignature(digest, signature) == EIP1271_MAGIC_VALUE;
}

address recovered = digest.recover(signature);
return recovered == owner();
}
Expand Down

0 comments on commit 1edeae5

Please sign in to comment.