-
Notifications
You must be signed in to change notification settings - Fork 13
/
SmartWalletFactory.sol
313 lines (283 loc) · 13.7 KB
/
SmartWalletFactory.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// SPDX-License-Identifier:MIT
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/cryptography/ECDSA.sol";
import "../interfaces/ISmartWalletFactory.sol";
import "../utils/RSKAddrValidator.sol";
/* solhint-disable no-inline-assembly */
/* solhint-disable avoid-low-level-calls */
/**
====================================================================================================================================================
Documentation of the Proxy Code being deployed
====================================================================================================================================================
A simple proxy that delegates every call to an address.
This ProxyA is the one instantiated per smart wallet, and it will receive the Forwarder as MC. So every call
made to this proxy will end up in MC.
MC is controlled by the same developer who created the factory, and .
For the transaction execution (execute() call), MC will do the signature verification and payment. Then it will
execute the request
====================================================================================================================================================
PROXY A
====================================================================================================================================================
Constructor
====================================================================================================================================================
PC | OPCODE| Mnemonic | Stack [top, bottom] | Comments
----------------------------------------------------------------------------------------------------------------------------------------------------
0 | 60 2D | PUSH1 2D | [45] | Size of runtime code
2 | 3D | RETURNDATASIZE | [0, 45] | Before any external call, returdatasize = 0 (cheaper than PUSH1 00)
3 | 81 | DUP2 | [45, 0, 45] |
4 | 60 09 | PUSH1 09 | [9, 45, 0, 45] | Size of constructor code
6 | 3D | RETURNDATASIZE | [0, 9, 45, 0, 45] |
7 | 39 | CODECOPY | [0, 45] | Mem[0:44] = address(this).code[9:53]
8 | F3 | RETURN | [] | return Mem[0:44]
====================================================================================================================================================
Runtime Code
====================================================================================================================================================
PC | OPCODE| Mnemonic | Stack [top, bottom] | Comments
----------------------------------------------------------------------------------------------------------------------------------
0 | 36 | CALLDATASIZE | [msg.data.size] |
1 | 3D | RETURNDATASIZE | [0, msg.data.size] |
2 | 3D | RETURNDATASIZE | [0, 0, msg.data.size] |
3 | 37 | CALLDATACOPY | [] | Mem[0:msg.data.size-1] = msg.data[0:msg.data.size-1]
4 | 3D | RETURNDATASIZE | [0] |
5 | 3D | RETURNDATASIZE | [0, 0] |
6 | 3D | RETURNDATASIZE | [0, 0, 0] |
7 | 3D | RETURNDATASIZE | [0, 0, 0, 0] |
8 | 36 | CALLDATASIZE | [msg.data.size, 0, 0, 0, 0] |
9 | 3D | RETURNDATASIZE | [0, msg.data.size, 0, 0, 0, 0] |
10 | 73 MC | PUSH20 MC | [mcAddr,0, msg.data.size, 0, 0, 0, 0] | mcAddr = address of master Copy, injected by factory
31 | 5A | GAS | [rGas, mcAddr,0, msg.data.size, 0, 0, 0, 0] | rGas = remaining gas
32 | F4 | DELEGATECALL | [isSuccess, 0, 0] | isSuccess, Mem[0:0] = address(mcAddr).delegateCall.gas(rGas)(Mem[0:msg.data.size-1])
33 | 3D | RETURNDATASIZE | [rds, isSuccess, 0, 0] | rds = size of what the logic called returned
34 | 92 | SWAP3 | [0, isSuccess, 0, rds] |
35 | 3D | RETURNDATASIZE | [rds, 0, isSuccess, 0, rds] |
36 | 90 | SWAP1 | [0, rds, isSuccess, 0, rds] |
37 | 80 | DUP1 | [0, 0, rds, isSuccess, 0, rds] |
38 | 3E | RETURNDATACOPY | [isSuccess, 0, rds] | Mem[0:rds-1] = RETURNDATA[0:rds-1]
39 | 60 2B | PUSH1 2B | [43, isSuccess, 0, rds] |
41 | 57 | JUMPI | [0, rds] | if(isSuccess) then jump to PC=43
42 | FD | REVERT | [] | revert(Mem[0, rds-1])
43 | 5B | JUMPDEST | [0, rds] |
44 | F3 | RETURN | [] | return(Mem[0, rds-1])
*/
/** Factory of Proxies to the SmartWallet (Forwarder)
*/
contract SmartWalletFactory is ISmartWalletFactory {
using ECDSA for bytes32;
bytes11 private constant _RUNTIME_START = hex"363D3D373D3D3D3D363D73";
bytes14 private constant _RUNTIME_END = hex"5AF43D923D90803E602B57FD5BF3";
address public masterCopy; // this is the ForwarderProxy contract that will be proxied
bytes32 public constant DATA_VERSION_HASH = keccak256("2");
bytes32 public domainSeparator =
keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
), //hex"8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f",
keccak256("RSK Enveloping Transaction"), //DOMAIN_NAME hex"d41b7f69f4d7734774d21b5548d74704ad02f9f1545db63927a1d58479c576c8"
DATA_VERSION_HASH,
_getChainID(),
address(this)
)
);
// Nonces of senders, used to prevent replay attacks
mapping(address => uint256) private _nonces;
/**
* @param forwarderTemplate It implements all the payment and execution needs,
* it pays for the deployment during initialization, and it pays for the transaction
* execution on each execute() call.
*/
constructor(address forwarderTemplate) public {
masterCopy = forwarderTemplate;
}
function runtimeCodeHash() external view override returns (bytes32) {
return
keccak256(
abi.encodePacked(_RUNTIME_START, masterCopy, _RUNTIME_END)
);
}
function nonce(address from) public view override returns (uint256) {
return _nonces[from];
}
function createUserSmartWallet(
address owner,
address recoverer,
uint256 index,
bytes calldata sig
) external override {
bytes32 _hash = keccak256(
abi.encodePacked(address(this), owner, recoverer, index)
);
(sig);
bytes32 message = keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash)
);
require(
RSKAddrValidator.safeEquals(message.recover(sig), owner),
"Invalid signature"
);
//a6b63eb8 => initialize(address owner,address tokenAddr,address tokenRecipient,uint256 tokenAmount,uint256 tokenGas)
bytes memory initData = abi.encodeWithSelector(
hex"a6b63eb8",
owner,
address(0), // This "gas-funded" call does not pay with tokens
address(0),
0,
0 //No token transfer
);
_deploy(
getCreationBytecode(),
keccak256(
abi.encodePacked(owner, recoverer, index) // salt
),
initData
);
}
function relayedUserSmartWalletCreation(
IForwarder.DeployRequest memory req,
bytes32 suffixData,
address feesReceiver,
bytes calldata sig
) external override {
require(msg.sender == req.relayHub, "Invalid caller");
_verifySig(req, suffixData, sig);
// solhint-disable-next-line not-rely-on-time
require(
req.validUntilTime == 0 || req.validUntilTime > block.timestamp,
"SW: request expired"
);
_nonces[req.from]++;
//a6b63eb8 => initialize(address owner,address tokenAddr,address tokenRecipient,uint256 tokenAmount,uint256 tokenGas)
//a9059cbb = transfer(address _to, uint256 _value) public returns (bool success)
/* solhint-disable avoid-tx-origin */
_deploy(
getCreationBytecode(),
keccak256(
abi.encodePacked(req.from, req.recoverer, req.index) // salt
),
abi.encodeWithSelector(
hex"a6b63eb8",
req.from,
req.tokenContract,
feesReceiver,
req.tokenAmount,
req.tokenGas
)
);
}
/**
* Calculates the Smart Wallet address for an owner EOA
* @param owner - EOA of the owner of the smart wallet
* @param recoverer - Address of that can be used by some contracts to give specific roles to the caller (e.g, a recoverer)
* @param index - Allows to create many addresses for the same owner|recoverer
*/
function getSmartWalletAddress(
address owner,
address recoverer,
uint256 index
) external view override returns (address) {
return
address(
uint160(
uint256(
keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
keccak256(
abi.encodePacked(owner, recoverer, index)
), // salt
keccak256(getCreationBytecode())
)
)
)
)
);
}
function _deploy(
bytes memory code,
bytes32 salt,
bytes memory initdata
) internal returns (address addr) {
//Deployment of the Smart Wallet
/* solhint-disable-next-line no-inline-assembly */
assembly {
addr := create2(0, add(code, 0x20), mload(code), salt)
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
//Since the init code determines the address of the smart wallet, any initialization
//required is done via the runtime code, to avoid the parameters impacting on the resulting address
/* solhint-disable-next-line avoid-low-level-calls */
(bool success, ) = addr.call(initdata);
/* solhint-disable-next-line reason-string */
require(success, "Unable to initialize SW");
//No info is returned, an event is emitted to inform the new deployment
emit Deployed(addr, uint256(salt));
}
// Returns the proxy code to that is deployed on every Smart Wallet creation
function getCreationBytecode() public view override returns (bytes memory) {
//The code to install: constructor, runtime start, master copy, runtime end
return
abi.encodePacked(
hex"602D3D8160093D39F3",
_RUNTIME_START,
masterCopy,
_RUNTIME_END
);
}
function _getEncoded(
IForwarder.DeployRequest memory req,
bytes32 suffixData
) internal pure returns (bytes memory) {
return
abi.encodePacked(
keccak256(
"RelayRequest(address relayHub,address from,address to,address tokenContract,address recoverer,uint256 value,uint256 nonce,uint256 tokenAmount,uint256 tokenGas,uint256 validUntilTime,uint256 index,bytes data,RelayData relayData)RelayData(uint256 gasPrice,address feesReceiver,address callForwarder,address callVerifier)"
),
abi.encode(
req.relayHub,
req.from,
req.to,
req.tokenContract,
req.recoverer,
req.value,
req.nonce,
req.tokenAmount,
req.tokenGas,
req.validUntilTime,
req.index,
keccak256(req.data)
),
suffixData
);
}
function _getChainID() internal pure returns (uint256 id) {
/* solhint-disable no-inline-assembly */
assembly {
id := chainid()
}
}
function _verifySig(
IForwarder.DeployRequest memory req,
bytes32 suffixData,
bytes memory sig
) internal view {
//Verify nonce
require(_nonces[req.from] == req.nonce, "nonce mismatch");
require(
RSKAddrValidator.safeEquals(
keccak256(
abi.encodePacked(
"\x19\x01",
domainSeparator,
keccak256(_getEncoded(req, suffixData))
)
).recover(sig),
req.from
),
"Signature mismatch"
);
}
}