文件 1 的 9:Address.sol
pragma solidity ^0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 9:ChainIdValidators.sol
pragma solidity ^0.8.0;
import "./access/Ownable.sol";
import "./utils/ECDSA.sol";
contract ChainIdValidators is Ownable {
using ECDSA for bytes32;
uint256[] public chainIds;
function addChainId(uint256 _chainId) external onlyOwner {
(bool found,) = indexOfChainId(_chainId);
require(!found, 'ChainId already added');
chainIds.push(_chainId);
}
function removeChainId(uint256 _chainId) external onlyOwner {
(bool found, uint256 index) = indexOfChainId(_chainId);
require(found, 'ChainId not found');
if (chainIds.length > 1) {
chainIds[index] = chainIds[chainIds.length - 1];
}
chainIds.pop();
}
function getListChainIds() public view returns (uint256[] memory) {
return chainIds;
}
function indexOfChainId(uint256 _chainId) public view returns (bool found, uint256 index) {
for (uint256 i = 0; i < chainIds.length; i++) {
if (chainIds[i] == _chainId) {
return (true, i);
}
}
return (false, 0);
}
}
文件 3 的 9:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
this;
return msg.data;
}
}
文件 4 的 9:ECDSA.sol
pragma solidity ^0.8.0;
library ECDSA {
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
bytes32 r;
bytes32 s;
uint8 v;
if (signature.length == 65) {
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
} else if (signature.length == 64) {
assembly {
let vs := mload(add(signature, 0x40))
r := mload(add(signature, 0x20))
s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
v := add(shr(255, vs), 27)
}
} else {
revert("ECDSA: invalid signature length");
}
return recover(hash, v, r, s);
}
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
require(uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, "ECDSA: invalid signature 's' value");
require(v == 27 || v == 28, "ECDSA: invalid signature 'v' value");
address signer = ecrecover(hash, v, r, s);
require(signer != address(0), "ECDSA: invalid signature");
return signer;
}
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
}
}
文件 5 的 9:ETHTimeLocker.sol
pragma solidity ^0.8.0;
import "./interfaces/IERC20.sol";
import "./utils/token/SafeERC20.sol";
import "./utils/ECDSA.sol";
import "./Validators.sol";
contract TimeLocker is Validators {
using ECDSA for bytes32;
using SafeERC20 for IERC20;
event Locked(address indexed _from, uint256 indexed _toChainId, uint256 indexed _lockId, uint256 _amount);
event Unlocked(address indexed _from, uint256 indexed _fromChainId, uint256 indexed _burnId, uint256 _amount);
IERC20 public erc20Time;
bool private initialized;
uint256 public lastLockId;
mapping(uint256 => mapping(uint256 => bool)) public burnIdsUsed;
function init(address _erc20Time) external onlyOwner {
require(!initialized, "Initialized");
erc20Time = IERC20(_erc20Time);
initialized = true;
}
function onTokenTransfer(address _sender, uint256 _amount, bytes memory _data) external {
require(address(erc20Time) == _msgSender(), "Sender address does not match expected");
require(_amount > 0, "The amount of the lock must not be zero");
(uint256 _toChainId) = abi.decode(_data, (uint256));
(bool found,) = indexOfChainId(_toChainId);
require(found, "ChainId not allowed");
lastLockId ++;
emit Locked(_sender, _toChainId, lastLockId, _amount);
}
function lock(uint256 _toChainId, uint256 _amount) external {
require(_amount > 0, "The amount of the lock must not be zero");
(bool found,) = indexOfChainId(_toChainId);
require(found, "ChainId not allowed");
require(erc20Time.allowance(_msgSender(), address(this)) >= _amount, "Not enough allowance");
erc20Time.safeTransferFrom(_msgSender(), address(this), _amount);
lastLockId ++;
emit Locked(_msgSender(), _toChainId, lastLockId, _amount);
}
function unlock(uint256 _fromChainId, uint256 _burnId, uint256 _amount, bytes[] memory _signatures) external {
require(!burnIdsUsed[_fromChainId][_burnId], "Burn id already used");
bytes32 messageHash = keccak256(abi.encodePacked(_msgSender(), _fromChainId, block.chainid, _burnId, _amount));
require(checkSignatures(messageHash, _signatures), "Incorrect signature(s)");
burnIdsUsed[_fromChainId][_burnId] = true;
erc20Time.safeTransfer(_msgSender(), _amount);
emit Unlocked(_msgSender(), _fromChainId, _burnId, _amount);
}
}
文件 6 的 9:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 7 的 9:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
address private _pendingOwner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view returns (address) {
return _owner;
}
function pendingOwner() public view returns (address) {
return _pendingOwner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
modifier onlyPendingOwner() {
require(pendingOwner() == _msgSender(), "Ownable: caller is not the pending owner");
_;
}
function transferOwnership(address newOwner) external onlyOwner {
_pendingOwner = newOwner;
}
function claimOwnership() external onlyPendingOwner {
_owner = _pendingOwner;
_pendingOwner = address(0);
emit OwnershipTransferred(_owner, _pendingOwner);
}
}
文件 8 的 9:SafeERC20.sol
pragma solidity ^0.8.0;
import "../../interfaces/IERC20.sol";
import "../Address.sol";
library SafeERC20 {
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 9 的 9:Validators.sol
pragma solidity ^0.8.0;
import "./utils/ECDSA.sol";
import "./ChainIdValidators.sol";
contract Validators is ChainIdValidators {
using ECDSA for bytes32;
address[] public bridgeValidators;
function addBridgeValidator(address _validator) external onlyOwner {
(bool found,) = indexOfBridgeValidator(_validator);
require(!found, 'Validator already added');
bridgeValidators.push(_validator);
}
function removeBridgeValidator(address _validator) external onlyOwner {
(bool found, uint index) = indexOfBridgeValidator(_validator);
require(found, 'Validator not found');
if (bridgeValidators.length > 1) {
bridgeValidators[index] = bridgeValidators[bridgeValidators.length - 1];
}
bridgeValidators.pop();
}
function getListBridgeValidators() public view returns (address[] memory) {
return bridgeValidators;
}
function indexOfBridgeValidator(address _validator) public view returns (bool found, uint index) {
for (uint i = 0; i < bridgeValidators.length; i++) {
if (bridgeValidators[i] == _validator) {
return (true, i);
}
}
return (false, 0);
}
function checkSignatures(bytes32 _messageHash, bytes[] memory _signatures) public view returns (bool) {
require(bridgeValidators.length > 0, 'Validators not added');
require(_signatures.length == bridgeValidators.length, 'The number of signatures does not match the number of validators');
bool[] memory markedValidators = new bool[](bridgeValidators.length);
for (uint i = 0; i < _signatures.length; i++) {
address extractedAddress = _messageHash.toEthSignedMessageHash().recover(_signatures[i]);
(bool found, uint index) = indexOfBridgeValidator(extractedAddress);
if (found && !markedValidators[index]) {
markedValidators[index] = true;
} else {
return false;
}
}
return true;
}
}
{
"compilationTarget": {
"contracts/ETHTimeLocker.sol": "TimeLocker"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"uint256","name":"_toChainId","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"_lockId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"Locked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"uint256","name":"_fromChainId","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"_burnId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"Unlocked","type":"event"},{"inputs":[{"internalType":"address","name":"_validator","type":"address"}],"name":"addBridgeValidator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_chainId","type":"uint256"}],"name":"addChainId","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"bridgeValidators","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"burnIdsUsed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"chainIds","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_messageHash","type":"bytes32"},{"internalType":"bytes[]","name":"_signatures","type":"bytes[]"}],"name":"checkSignatures","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"claimOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"erc20Time","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getListBridgeValidators","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getListChainIds","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_validator","type":"address"}],"name":"indexOfBridgeValidator","outputs":[{"internalType":"bool","name":"found","type":"bool"},{"internalType":"uint256","name":"index","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_chainId","type":"uint256"}],"name":"indexOfChainId","outputs":[{"internalType":"bool","name":"found","type":"bool"},{"internalType":"uint256","name":"index","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_erc20Time","type":"address"}],"name":"init","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"lastLockId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_toChainId","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"lock","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_sender","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"onTokenTransfer","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_validator","type":"address"}],"name":"removeBridgeValidator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_chainId","type":"uint256"}],"name":"removeChainId","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_fromChainId","type":"uint256"},{"internalType":"uint256","name":"_burnId","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"bytes[]","name":"_signatures","type":"bytes[]"}],"name":"unlock","outputs":[],"stateMutability":"nonpayable","type":"function"}]