181 lines
7.2 KiB
Solidity
181 lines
7.2 KiB
Solidity
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
pragma solidity ^0.8.3;
|
|
|
|
/**
|
|
* @title Utils
|
|
* @notice Common utility methods used by modules.
|
|
*/
|
|
library Utils {
|
|
|
|
// ERC20, ERC721 & ERC1155 transfers & approvals
|
|
bytes4 private constant ERC20_TRANSFER = bytes4(keccak256("transfer(address,uint256)"));
|
|
bytes4 private constant ERC20_APPROVE = bytes4(keccak256("approve(address,uint256)"));
|
|
bytes4 private constant ERC721_SET_APPROVAL_FOR_ALL = bytes4(keccak256("setApprovalForAll(address,bool)"));
|
|
bytes4 private constant ERC721_TRANSFER_FROM = bytes4(keccak256("transferFrom(address,address,uint256)"));
|
|
bytes4 private constant ERC721_SAFE_TRANSFER_FROM = bytes4(keccak256("safeTransferFrom(address,address,uint256)"));
|
|
bytes4 private constant ERC721_SAFE_TRANSFER_FROM_BYTES = bytes4(keccak256("safeTransferFrom(address,address,uint256,bytes)"));
|
|
bytes4 private constant ERC1155_SAFE_TRANSFER_FROM = bytes4(keccak256("safeTransferFrom(address,address,uint256,uint256,bytes)"));
|
|
|
|
bytes4 private constant OWNER_SIG = 0x8da5cb5b;
|
|
/**
|
|
* @notice Helper method to recover the signer at a given position from a list of concatenated signatures.
|
|
* @param _signedHash The signed hash
|
|
* @param _signatures The concatenated signatures.
|
|
* @param _index The index of the signature to recover.
|
|
*/
|
|
function recoverSigner(bytes32 _signedHash, bytes memory _signatures, uint _index) internal pure returns (address) {
|
|
uint8 v;
|
|
bytes32 r;
|
|
bytes32 s;
|
|
// we jump 32 (0x20) as the first slot of bytes contains the length
|
|
// we jump 65 (0x41) per signature
|
|
// for v we load 32 bytes ending with v (the first 31 come from s) then apply a mask
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
r := mload(add(_signatures, add(0x20,mul(0x41,_index))))
|
|
s := mload(add(_signatures, add(0x40,mul(0x41,_index))))
|
|
v := and(mload(add(_signatures, add(0x41,mul(0x41,_index)))), 0xff)
|
|
}
|
|
require(v == 27 || v == 28, "Utils: bad v value in signature");
|
|
|
|
address recoveredAddress = ecrecover(_signedHash, v, r, s);
|
|
require(recoveredAddress != address(0), "Utils: ecrecover returned 0");
|
|
return recoveredAddress;
|
|
}
|
|
|
|
/**
|
|
* @notice Helper method to recover the spender from a contract call.
|
|
* The method returns the contract unless the call is to a standard method of a ERC20/ERC721/ERC1155 token
|
|
* in which case the spender is recovered from the data.
|
|
* @param _to The target contract.
|
|
* @param _data The data payload.
|
|
*/
|
|
function recoverSpender(address _to, bytes memory _data) internal pure returns (address spender) {
|
|
if(_data.length >= 68) {
|
|
bytes4 methodId;
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
methodId := mload(add(_data, 0x20))
|
|
}
|
|
if(
|
|
methodId == ERC20_TRANSFER ||
|
|
methodId == ERC20_APPROVE ||
|
|
methodId == ERC721_SET_APPROVAL_FOR_ALL)
|
|
{
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
spender := mload(add(_data, 0x24))
|
|
}
|
|
return spender;
|
|
}
|
|
if(
|
|
methodId == ERC721_TRANSFER_FROM ||
|
|
methodId == ERC721_SAFE_TRANSFER_FROM ||
|
|
methodId == ERC721_SAFE_TRANSFER_FROM_BYTES ||
|
|
methodId == ERC1155_SAFE_TRANSFER_FROM)
|
|
{
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
spender := mload(add(_data, 0x44))
|
|
}
|
|
return spender;
|
|
}
|
|
}
|
|
|
|
spender = _to;
|
|
}
|
|
|
|
/**
|
|
* @notice Helper method to parse data and extract the method signature.
|
|
*/
|
|
function functionPrefix(bytes memory _data) internal pure returns (bytes4 prefix) {
|
|
require(_data.length >= 4, "Utils: Invalid functionPrefix");
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
prefix := mload(add(_data, 0x20))
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notice Checks if an address is a contract.
|
|
* @param _addr The address.
|
|
*/
|
|
function isContract(address _addr) internal view returns (bool) {
|
|
uint32 size;
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
size := extcodesize(_addr)
|
|
}
|
|
return (size > 0);
|
|
}
|
|
|
|
/**
|
|
* @notice Checks if an address is a guardian or an account authorised to sign on behalf of a smart-contract guardian
|
|
* given a list of guardians.
|
|
* @param _guardians the list of guardians
|
|
* @param _guardian the address to test
|
|
* @return true and the list of guardians minus the found guardian upon success, false and the original list of guardians if not found.
|
|
*/
|
|
function isGuardianOrGuardianSigner(address[] memory _guardians, address _guardian) internal view returns (bool, address[] memory) {
|
|
if (_guardians.length == 0 || _guardian == address(0)) {
|
|
return (false, _guardians);
|
|
}
|
|
bool isFound = false;
|
|
address[] memory updatedGuardians = new address[](_guardians.length - 1);
|
|
uint256 index = 0;
|
|
for (uint256 i = 0; i < _guardians.length; i++) {
|
|
if (!isFound) {
|
|
// check if _guardian is an account guardian
|
|
if (_guardian == _guardians[i]) {
|
|
isFound = true;
|
|
continue;
|
|
}
|
|
// check if _guardian is the owner of a smart contract guardian
|
|
if (isContract(_guardians[i]) && isGuardianOwner(_guardians[i], _guardian)) {
|
|
isFound = true;
|
|
continue;
|
|
}
|
|
}
|
|
if (index < updatedGuardians.length) {
|
|
updatedGuardians[index] = _guardians[i];
|
|
index++;
|
|
}
|
|
}
|
|
return isFound ? (true, updatedGuardians) : (false, _guardians);
|
|
}
|
|
|
|
/**
|
|
* @notice Checks if an address is the owner of a guardian contract.
|
|
* The method does not revert if the call to the owner() method consumes more then 25000 gas.
|
|
* @param _guardian The guardian contract
|
|
* @param _owner The owner to verify.
|
|
*/
|
|
function isGuardianOwner(address _guardian, address _owner) internal view returns (bool) {
|
|
address owner = address(0);
|
|
|
|
// solhint-disable-next-line no-inline-assembly
|
|
assembly {
|
|
let ptr := mload(0x40)
|
|
mstore(ptr,OWNER_SIG)
|
|
let result := staticcall(25000, _guardian, ptr, 0x20, ptr, 0x20)
|
|
if eq(result, 1) {
|
|
owner := mload(ptr)
|
|
}
|
|
}
|
|
return owner == _owner;
|
|
}
|
|
|
|
/**
|
|
* @notice Returns ceil(a / b).
|
|
*/
|
|
function ceil(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
uint256 c = a / b;
|
|
if (a % b == 0) {
|
|
return c;
|
|
} else {
|
|
return c + 1;
|
|
}
|
|
}
|
|
}
|