-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathERC2771Recipient.sol
63 lines (55 loc) · 2.29 KB
/
ERC2771Recipient.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
// solhint-disable no-inline-assembly
pragma solidity >=0.6.9;
import "./interfaces/IERC2771Recipient.sol";
/**
* @title The ERC-2771 Recipient Base Abstract Class - Implementation
*
* @notice Note that this contract was called `BaseRelayRecipient` in the previous revision of the GSN.
*
* @notice A base contract to be inherited by any contract that want to receive relayed transactions.
*
* @notice A subclass must use `_msgSender()` instead of `msg.sender`.
*/
abstract contract ERC2771Recipient is IERC2771Recipient {
/*
* Forwarder singleton we accept calls from
*/
address private _trustedForwarder;
/**
* :warning: **Warning** :warning: The Forwarder can have a full control over your Recipient. Only trust verified Forwarder.
* @notice Method is not a required method to allow Recipients to trust multiple Forwarders. Not recommended yet.
* @return forwarder The address of the Forwarder contract that is being used.
*/
function getTrustedForwarder() public virtual view returns (address forwarder){
return _trustedForwarder;
}
function _setTrustedForwarder(address _forwarder) internal {
_trustedForwarder = _forwarder;
}
/// @inheritdoc IERC2771Recipient
function isTrustedForwarder(address forwarder) public virtual override view returns(bool) {
return forwarder == _trustedForwarder;
}
/// @inheritdoc IERC2771Recipient
function _msgSender() internal override virtual view returns (address ret) {
if (msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
// At this point we know that the sender is a trusted forwarder,
// so we trust that the last bytes of msg.data are the verified sender address.
// extract sender address from the end of msg.data
assembly {
ret := shr(96,calldataload(sub(calldatasize(),20)))
}
} else {
ret = msg.sender;
}
}
/// @inheritdoc IERC2771Recipient
function _msgData() internal override virtual view returns (bytes calldata ret) {
if (msg.data.length >= 20 && isTrustedForwarder(msg.sender)) {
return msg.data[0:msg.data.length-20];
} else {
return msg.data;
}
}
}