-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathAccountTestBase.sol
More file actions
284 lines (226 loc) · 11.4 KB
/
Copy pathAccountTestBase.sol
File metadata and controls
284 lines (226 loc) · 11.4 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
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import {EntryPoint} from "@eth-infinitism/account-abstraction/core/EntryPoint.sol";
import {PackedUserOperation} from "@eth-infinitism/account-abstraction/interfaces/PackedUserOperation.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {ReferenceModularAccount} from "../../src/account/ReferenceModularAccount.sol";
import {SemiModularAccount} from "../../src/account/SemiModularAccount.sol";
import {ModuleEntity, ModuleEntityLib} from "../../src/helpers/ModuleEntityLib.sol";
import {Call, IModularAccount} from "../../src/interfaces/IModularAccount.sol";
import {SingleSignerValidationModule} from "../../src/modules/validation/SingleSignerValidationModule.sol";
import {OptimizedTest} from "./OptimizedTest.sol";
import {TEST_DEFAULT_VALIDATION_ENTITY_ID as EXT_CONST_TEST_DEFAULT_VALIDATION_ENTITY_ID} from
"./TestConstants.sol";
import {SingleSignerFactoryFixture} from "../mocks/SingleSignerFactoryFixture.sol";
/// @dev This contract handles common boilerplate setup for tests using ReferenceModularAccount with
/// SingleSignerValidationModule.
abstract contract AccountTestBase is OptimizedTest {
using ModuleEntityLib for ModuleEntity;
using MessageHashUtils for bytes32;
EntryPoint public entryPoint;
address payable public beneficiary;
SingleSignerValidationModule public singleSignerValidationModule;
SingleSignerFactoryFixture public factory;
address public owner1;
uint256 public owner1Key;
ReferenceModularAccount public account1;
ModuleEntity internal _signerValidation;
uint8 public constant SELECTOR_ASSOCIATED_VALIDATION = 0;
uint8 public constant GLOBAL_VALIDATION = 1;
// Re-declare the constant to prevent derived test contracts from having to import it
uint32 public constant TEST_DEFAULT_VALIDATION_ENTITY_ID = EXT_CONST_TEST_DEFAULT_VALIDATION_ENTITY_ID;
uint256 public constant CALL_GAS_LIMIT = 100_000;
uint256 public constant VERIFICATION_GAS_LIMIT = 1_200_000;
struct PreValidationHookData {
uint8 index;
bytes validationData;
}
constructor() {
entryPoint = new EntryPoint();
(owner1, owner1Key) = makeAddrAndKey("owner1");
beneficiary = payable(makeAddr("beneficiary"));
address deployedSingleSignerValidation = address(_deploySingleSignerValidationModule());
// We etch the single signer validation to the max address, such that it coincides with the fallback
// validation module entity for semi modular account tests.
singleSignerValidationModule = SingleSignerValidationModule(address(type(uint160).max));
vm.etch(address(singleSignerValidationModule), deployedSingleSignerValidation.code);
factory = new SingleSignerFactoryFixture(entryPoint, singleSignerValidationModule);
account1 = factory.createAccount(owner1, 0);
vm.deal(address(account1), 100 ether);
_signerValidation =
ModuleEntityLib.pack(address(singleSignerValidationModule), TEST_DEFAULT_VALIDATION_ENTITY_ID);
}
function _runExecUserOp(address target, bytes memory callData) internal {
_runUserOp(abi.encodeCall(IModularAccount.execute, (target, 0, callData)));
}
function _runExecUserOp(address target, bytes memory callData, bytes memory revertReason) internal {
_runUserOp(abi.encodeCall(IModularAccount.execute, (target, 0, callData)), revertReason);
}
function _runExecBatchUserOp(Call[] memory calls) internal {
_runUserOp(abi.encodeCall(IModularAccount.executeBatch, (calls)));
}
function _runExecBatchUserOp(Call[] memory calls, bytes memory revertReason) internal {
_runUserOp(abi.encodeCall(IModularAccount.executeBatch, (calls)), revertReason);
}
function _runUserOp(bytes memory callData) internal {
// Run user op without expecting a revert
_runUserOp(callData, hex"");
}
function _runUserOp(bytes memory callData, bytes memory expectedRevertData) internal {
uint256 nonce = entryPoint.getNonce(address(account1), 0);
PackedUserOperation memory userOp = PackedUserOperation({
sender: address(account1),
nonce: nonce,
initCode: hex"",
callData: callData,
accountGasLimits: _encodeGas(VERIFICATION_GAS_LIMIT, CALL_GAS_LIMIT),
preVerificationGas: 0,
gasFees: _encodeGas(1, 1),
paymasterAndData: hex"",
signature: hex""
});
bytes32 userOpHash = entryPoint.getUserOpHash(userOp);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(owner1Key, userOpHash.toEthSignedMessageHash());
userOp.signature = _encodeSignature(_signerValidation, GLOBAL_VALIDATION, abi.encodePacked(r, s, v));
PackedUserOperation[] memory userOps = new PackedUserOperation[](1);
userOps[0] = userOp;
if (expectedRevertData.length > 0) {
vm.expectRevert(expectedRevertData);
}
entryPoint.handleOps(userOps, beneficiary);
}
function _runtimeExec(address target, bytes memory callData) internal {
_runtimeCall(abi.encodeCall(IModularAccount.execute, (target, 0, callData)));
}
function _runtimeExec(address target, bytes memory callData, bytes memory expectedRevertData) internal {
_runtimeCall(abi.encodeCall(IModularAccount.execute, (target, 0, callData)), expectedRevertData);
}
function _runtimeExecExpFail(address target, bytes memory callData, bytes memory expectedRevertData)
internal
{
_runtimeCallExpFail(abi.encodeCall(IModularAccount.execute, (target, 0, callData)), expectedRevertData);
}
function _runtimeExecBatch(Call[] memory calls) internal {
_runtimeCall(abi.encodeCall(IModularAccount.executeBatch, (calls)));
}
function _runtimeExecBatch(Call[] memory calls, bytes memory expectedRevertData) internal {
_runtimeCall(abi.encodeCall(IModularAccount.executeBatch, (calls)), expectedRevertData);
}
function _runtimeExecBatchExpFail(Call[] memory calls, bytes memory expectedRevertData) internal {
_runtimeCallExpFail(abi.encodeCall(IModularAccount.executeBatch, (calls)), expectedRevertData);
}
function _runtimeCall(bytes memory callData) internal {
_runtimeCall(callData, "");
}
function _runtimeCall(bytes memory callData, bytes memory expectedRevertData) internal {
if (expectedRevertData.length > 0) {
vm.expectRevert(expectedRevertData);
}
vm.prank(owner1);
account1.executeWithAuthorization(callData, _encodeSignature(_signerValidation, GLOBAL_VALIDATION, ""));
}
// Always expects a revert, even if the revert data is zero-length.
function _runtimeCallExpFail(bytes memory callData, bytes memory expectedRevertData) internal {
vm.expectRevert(expectedRevertData);
vm.prank(owner1);
account1.executeWithAuthorization(callData, _encodeSignature(_signerValidation, GLOBAL_VALIDATION, ""));
}
function _transferOwnershipToTest() internal {
// Transfer ownership to test contract for easier invocation.
vm.prank(owner1);
if (vm.envOr("SMA_TEST", false)) {
account1.executeWithAuthorization(
abi.encodeCall(SemiModularAccount(payable(account1)).updateFallbackSigner, (address(this))),
_encodeSignature(_signerValidation, GLOBAL_VALIDATION, "")
);
return;
}
account1.executeWithAuthorization(
abi.encodeCall(
account1.execute,
(
address(singleSignerValidationModule),
0,
abi.encodeCall(
SingleSignerValidationModule.transferSigner,
(TEST_DEFAULT_VALIDATION_ENTITY_ID, address(this))
)
)
),
_encodeSignature(_signerValidation, GLOBAL_VALIDATION, "")
);
}
// helper function to compress 2 gas values into a single bytes32
function _encodeGas(uint256 g1, uint256 g2) internal pure returns (bytes32) {
return bytes32(uint256((g1 << 128) + uint128(g2)));
}
// helper function to encode a 1271 signature, according to the per-hook and per-validation data format.
function _encode1271Signature(
ModuleEntity validationFunction,
PreValidationHookData[] memory preValidationHookData,
bytes memory validationData
) internal pure returns (bytes memory) {
bytes memory sig = abi.encodePacked(validationFunction);
sig = abi.encodePacked(sig, _packPreHookDatas(preValidationHookData));
sig = abi.encodePacked(sig, _packValidationResWithIndex(255, validationData));
return sig;
}
// helper function to encode a signature, according to the per-hook and per-validation data format.
function _encodeSignature(
ModuleEntity validationFunction,
uint8 globalOrNot,
PreValidationHookData[] memory preValidationHookData,
bytes memory validationData
) internal pure returns (bytes memory) {
bytes memory sig = abi.encodePacked(validationFunction, globalOrNot);
sig = abi.encodePacked(sig, _packPreHookDatas(preValidationHookData));
sig = abi.encodePacked(sig, _packValidationResWithIndex(255, validationData));
return sig;
}
// overload for the case where there are no pre validation hooks
function _encodeSignature(ModuleEntity validationFunction, uint8 globalOrNot, bytes memory validationData)
internal
pure
returns (bytes memory)
{
PreValidationHookData[] memory emptyPreValidationHookData = new PreValidationHookData[](0);
return _encodeSignature(validationFunction, globalOrNot, emptyPreValidationHookData, validationData);
}
// overload for the case where there are no pre validation hooks
function _encode1271Signature(ModuleEntity validationFunction, bytes memory validationData)
internal
pure
returns (bytes memory)
{
PreValidationHookData[] memory emptyPreValidationHookData = new PreValidationHookData[](0);
return _encode1271Signature(validationFunction, emptyPreValidationHookData, validationData);
}
// helper function to pack pre validation hook datas, according to the sparse calldata segment spec.
function _packPreHookDatas(PreValidationHookData[] memory preValidationHookData)
internal
pure
returns (bytes memory)
{
bytes memory res = "";
for (uint256 i = 0; i < preValidationHookData.length; ++i) {
res = abi.encodePacked(
res,
_packValidationResWithIndex(
preValidationHookData[i].index, preValidationHookData[i].validationData
)
);
}
return res;
}
// helper function to pack validation data with an index, according to the sparse calldata segment spec.
function _packValidationResWithIndex(uint8 index, bytes memory validationData)
internal
pure
returns (bytes memory)
{
return abi.encodePacked(uint32(validationData.length + 1), index, validationData);
}
function _buildDirectCallDisallowedError(bytes4 selector) internal pure returns (bytes memory) {
return abi.encodeWithSelector(ReferenceModularAccount.ValidationFunctionMissing.selector, selector);
}
}