Description:
Smart contract deployed on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
interface IERC20 {
function transferFrom(address from, address to, uint256 value) external returns (bool);
function transfer(address to, uint256 value) external returns (bool);
}
contract ERC20Bridge {
IERC20 public immutable token;
address public validator;
mapping(bytes32 => bool) public processed;
event Deposited(address indexed l2Receiver, uint256 amount);
event Withdrawn(address indexed user, uint256 amount, bytes32 indexed nonce);
constructor(address tokenAddress, address _validator) {
require(tokenAddress != address(0), "Invalid token");
require(_validator != address(0), "Invalid validator");
token = IERC20(tokenAddress);
validator = _validator;
}
function deposit(uint256 amount) external {
require(amount > 0, "Invalid amount");
require(
token.transferFrom(msg.sender, address(this), amount),
"Transfer failed"
);
emit Deposited(msg.sender, amount);
}
function withdraw(uint256 amount, bytes32 nonce, bytes memory signature) external {
require(!processed[nonce], "Already processed");
bytes32 messageHash = getMessageHash(msg.sender, amount, nonce);
require(recoverSigner(messageHash, signature) == validator, "Invalid signature");
processed[nonce] = true;
require(token.transfer(msg.sender, amount), "Transfer failed");
emit Withdrawn(msg.sender, amount, nonce);
}
function getMessageHash(address user, uint256 amount, bytes32 nonce) public pure returns (bytes32) {
return keccak256(abi.encodePacked(user, amount, nonce));
}
function recoverSigner(bytes32 messageHash, bytes memory signature) public pure returns (address) {
bytes32 ethSigned = keccak256(abi.encodePacked(
"\x19Ethereum Signed Message:
32", messageHash
));
(bytes32 r, bytes32 s, uint8 v) = splitSignature(signature);
return ecrecover(ethSigned, v, r, s);
}
function splitSignature(bytes memory sig) internal pure returns (bytes32 r, bytes32 s, uint8 v) {
require(sig.length == 65, "Invalid signature length");
assembly {
r := mload(add(sig, 32))
s := mload(add(sig, 64))
v := byte(0, mload(add(sig, 96)))
}
}
}
Submitted on: 2025-09-17 17:22:43
Comments
Log in to comment.
No comments yet.