This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Anderson/add new forwarder contract (#440)
* add minimal forwarder * rename test forwarder contract * add tests for the new forwarder * add forwarder test * rename test * fix comments * fix comments * remove responseForwarder
- Loading branch information
1 parent
2daf257
commit e2f5cda
Showing
7 changed files
with
989 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity 0.6.12; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "@openzeppelin/contracts/cryptography/ECDSA.sol"; | ||
import "@openzeppelin/contracts/drafts/EIP712.sol"; | ||
|
||
/** | ||
@notice This contract refers to Openzeppelin's MinimalForwarder contract. | ||
*/ | ||
contract Forwarder is EIP712 { | ||
using ECDSA for bytes32; | ||
|
||
struct ForwardRequest { | ||
address from; | ||
address to; | ||
uint256 value; | ||
uint256 gas; | ||
uint256 nonce; | ||
bytes data; | ||
} | ||
|
||
bytes32 private constant _TYPEHASH = | ||
keccak256("ForwardRequest(address from,address to,uint256 value,uint256 gas,uint256 nonce,bytes data)"); | ||
|
||
mapping(address => uint256) private _nonces; | ||
|
||
constructor() EIP712("Forwarder", "0.0.1") public {} | ||
|
||
function getNonce(address from) public view returns (uint256) { | ||
return _nonces[from]; | ||
} | ||
|
||
function verify(ForwardRequest calldata req, bytes calldata signature) public view returns (bool) { | ||
address signer = _hashTypedDataV4( | ||
keccak256(abi.encode(_TYPEHASH, req.from, req.to, req.value, req.gas, req.nonce, keccak256(req.data))) | ||
).recover(signature); | ||
return _nonces[req.from] == req.nonce && signer == req.from; | ||
} | ||
|
||
function execute(ForwardRequest calldata req, bytes calldata signature) | ||
public | ||
payable | ||
returns (bool, bytes memory) | ||
{ | ||
require(verify(req, signature), "MinimalForwarder: signature does not match request"); | ||
_nonces[req.from] = req.nonce + 1; | ||
|
||
(bool success, bytes memory returndata) = req.to.call{gas: req.gas, value: req.value}( | ||
abi.encodePacked(req.data, req.from) | ||
); | ||
|
||
assert(gasleft() > req.gas / 63); | ||
|
||
return (success, returndata); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.