forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRelayedCall.sol
More file actions
141 lines (129 loc) · 6.6 KB
/
RelayedCall.sol
File metadata and controls
141 lines (129 loc) · 6.6 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/RelayedCall.sol)
pragma solidity ^0.8.20;
/**
* @dev Library for performing external calls through dynamically deployed relay contracts that hide the original
* caller's address from the target contract. This pattern is used in ERC-4337's EntryPoint for account factory
* calls and ERC-6942 for safe factory interactions.
*
* When privileged contracts need to make arbitrary external calls based on user input, calling the target directly
* can be risky because the target sees the privileged contract as `msg.sender` and could exploit this trust
* relationship. This library solves this by deploying minimal relay contracts that act as intermediaries, ensuring
* the target only sees the unprivileged relay address as `msg.sender`.
*
* For example, instead of `target.call(data)` where the target sees this contract as `msg.sender`, use
* {relayCall} where the target sees a relay address as `msg.sender`.
*/
library RelayedCall {
/// @dev Relays a call to the target contract through a dynamically deployed relay contract.
function relayCall(address target, bytes memory data) internal returns (bool success, bytes memory retData) {
return relayCall(target, 0, data, bytes32(0));
}
/// @dev Same as {relayCall-address-bytes} but with a value.
function relayCall(
address target,
uint256 value,
bytes memory data
) internal returns (bool success, bytes memory retData) {
return relayCall(target, value, data, bytes32(0));
}
/// @dev Same as {relayCall-address-bytes} but with a salt.
function relayCall(
address target,
bytes memory data,
bytes32 salt
) internal returns (bool success, bytes memory retData) {
return relayCall(target, 0, data, salt);
}
/// @dev Same as {relayCall-address-bytes} but with a salt and a value.
function relayCall(
address target,
uint256 value,
bytes memory data,
bytes32 salt
) internal returns (bool success, bytes memory retData) {
return getRelayer(salt).call{value: value}(abi.encodePacked(target, data));
}
/// @dev Same as {getRelayer} but with a `bytes32(0)` default salt.
function getRelayer() internal returns (address) {
return getRelayer(bytes32(0));
}
/// @dev Returns the relayer address for a given salt.
function getRelayer(bytes32 salt) internal returns (address relayer) {
// [Relayer details]
//
// deployment prefix: 60475f8160095f39f3
// deployed bytecode: 73<addr>331460133611166022575f5ffd5b6014360360145f375f5f601436035f345f3560601c5af13d5f5f3e5f3d91604557fd5bf3
//
// offset | bytecode | opcode | stack
// -------|-------------|----------------|--------
// 0x0000 | 73<factory> | push20 <addr> | <factory>
// 0x0015 | 33 | address | <caller> <factory>
// 0x0016 | 14 | eq | access
// 0x0017 | 6013 | push1 0x13 | 0x13 access
// 0x0019 | 36 | calldatasize | cds 0x13 access
// 0x001a | 11 | gt | (cds>0x13) access
// 0x001b | 16 | and | (cds>0x13 && access)
// 0x001c | 6022 | push1 0x22 | 0x22 (cds>0x13 && access)
// 0x001e | 57 | jumpi |
// 0x001f | 5f | push0 | 0
// 0x0020 | 5f | push0 | 0 0
// 0x0021 | fd | revert |
// 0x0022 | 5b | jumpdest |
// 0x0023 | 6014 | push1 0x14 | 0x14
// 0x0025 | 36 | calldatasize | cds 0x14
// 0x0026 | 03 | sub | (cds-0x14)
// 0x0027 | 6014 | push1 0x14 | 0x14 (cds-0x14)
// 0x0029 | 5f | push0 | 0 0x14 (cds-0x14)
// 0x002a | 37 | calldatacopy |
// 0x002b | 5f | push0 | 0
// 0x002c | 5f | push0 | 0 0
// 0x002d | 6014 | push1 0x14 | 0x14 0 0
// 0x002f | 36 | calldatasize | cds 0x14 0 0
// 0x0030 | 03 | sub | (cds-0x14) 0 0
// 0x0031 | 5f | push0 | 0 (cds-0x14) 0 0
// 0x0032 | 34 | callvalue | value 0 (cds-0x14) 0 0
// 0x0033 | 5f | push0 | 0 value 0 (cds-0x14) 0 0
// 0x0034 | 35 | calldataload | cd[0] value 0 (cds-0x14) 0 0
// 0x0035 | 6060 | push1 0x60 | 0x60 cd[0] value 0 (cds-0x14) 0 0
// 0x0037 | 1c | shr | target value 0 (cds-0x14) 0 0
// 0x0038 | 5a | gas | gas target value 0 (cds-0x14) 0 0
// 0x0039 | f1 | call | suc
// 0x003a | 3d | returndatasize | rds suc
// 0x003b | 5f | push0 | 0 rds suc
// 0x003c | 5f | push0 | 0 0 rds suc
// 0x003d | 3e | returndatacopy | suc
// 0x003e | 5f | push0 | 0 suc
// 0x003f | 3d | returndatasize | rds 0 suc
// 0x0040 | 91 | swap2 | suc 0 rds
// 0x0041 | 6045 | push1 0x45 | 0x45 suc 0 rds
// 0x0043 | 57 | jumpi | 0 rds
// 0x0044 | fd | revert |
// 0x0045 | 5b | jumpdest | 0 rds
// 0x0046 | f3 | return |
assembly ("memory-safe") {
let fmp := mload(0x40)
// build initcode at FMP
mstore(add(fmp, 0x46), 0x60145f375f5f601436035f345f3560601c5af13d5f5f3e5f3d91604557fd5bf3)
mstore(add(fmp, 0x26), 0x331460133611166022575f5ffd5b60143603)
mstore(add(fmp, 0x14), address())
mstore(add(fmp, 0x00), 0x60475f8160095f39f373)
let initcodehash := keccak256(add(fmp, 0x16), 0x50)
// compute create2 address
mstore(0x40, initcodehash)
mstore(0x20, salt)
mstore(0x00, address())
mstore8(0x0b, 0xff)
relayer := and(keccak256(0x0b, 0x55), shr(96, not(0)))
// is relayer not yet deployed, deploy it
if iszero(extcodesize(relayer)) {
if iszero(create2(0, add(fmp, 0x16), 0x50, salt)) {
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
}
// cleanup fmp space used as scratch
mstore(0x40, fmp)
}
}
}