-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAccountRecoveryFacet.t.sol
354 lines (261 loc) · 15 KB
/
AccountRecoveryFacet.t.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.21;
import "forge-std/Test.sol";
// base contracts
import {BarzFactory} from "../../contracts/BarzFactory.sol";
import {Barz} from "../../contracts/Barz.sol";
// facets
import {AccountFacet} from "../../contracts/facets/AccountFacet.sol";
import {Secp256k1VerificationFacet} from "../../contracts/facets/verification/secp256k1/Secp256k1VerificationFacet.sol";
import {GuardianFacet} from "../../contracts/facets/GuardianFacet.sol";
import {AccountRecoveryFacet} from "../../contracts/facets/AccountRecoveryFacet.sol";
import {LockFacet} from "../../contracts/facets/LockFacet.sol";
import {RecoveryConfig} from "../../contracts/libraries/LibFacetStorage.sol";
import {LibVerification} from "../../contracts/libraries/LibVerification.sol";
// interfaces
import {IDiamondCut} from "../../contracts/facets/base/interfaces/IDiamondCut.sol";
import {UserOperation} from "../../contracts/aa-4337/interfaces/UserOperation.sol";
// constants & utils
import {Setup} from "./utils/Setup.sol";
import {Constants} from "./utils/Constants.sol";
import {AccountRecoveryFacetTestBase} from "./base/AccountRecoveryFacetTestBase.sol";
contract AccountRecoveryFacetTest is Test, Setup, AccountRecoveryFacetTestBase {
address[] public wallets;
address public operator;
address public user1;
address public guardian1;
address public guardian2;
uint256 public guardian1PrivateKey = 1;
uint256 public guardian2PrivateKey = 2;
bytes constant recoveryPublicKey = user2PublicKey;
BarzFactory public barzFactory;
Barz public barz;
function setUp() public {
skip(1706504435); // 1706504435 = Date and time (GMT): Monday, 29 January 2024 05:00:35
uint256[] memory signers = new uint256[](2);
signers[0] = user1PrivateKey;
signers[1] = user2PrivateKey;
wallets = _setUpSigners(2, signers, 50 ether);
deployer = wallets[0];
operator = wallets[1];
guardian1 = vm.addr(guardian1PrivateKey);
guardian2 = vm.addr(guardian2PrivateKey);
barzFactory = _setUpBarzFactory();
// k1Facet & r1Facet are deployed during the setup of BarzFactory
barz = barzFactory.createAccount(address(k1Facet), user1PublicKey, walletCreationSalt);
vm.deal(address(barz), 10 ether);
_addGuardianFacet();
_addAccountRecoveryFacet();
_addLockFacet();
}
function _addGuardianFacet() internal {
cutFacet(address(guardianFacet), IDiamondCut.FacetCutAction.Add, Constants.guardianFacetSelectors(), address(barz), user1PrivateKey);
}
function _addAccountRecoveryFacet() internal {
cutFacet(address(accountRecoveryFacet), IDiamondCut.FacetCutAction.Add, Constants.accountRecoveryFacetSelectors(), address(barz), user1PrivateKey);
}
function _addLockFacet() internal {
cutFacet(address(lockFacet), IDiamondCut.FacetCutAction.Add, Constants.lockFacetSelectors(), address(barz), user1PrivateKey);
}
function _initiateRecovery() internal {
uint64 expectedExecutionTime = uint64(block.timestamp + Constants.defaultRecoveryPeriod);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
vm.expectEmit(true, true, false, true);
emit RecoveryExecuted(recoveryPublicKey, expectedExecutionTime);
vm.prank(guardian2);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
}
function _addGuardian(address _guardian) internal {
UserOperation[] memory userOp = new UserOperation[](1);
uint256[] memory signingKey = new uint256[](1);
bytes memory addGuardianData = abi.encodeWithSignature("addGuardian(address)", _guardian);
bytes memory callData = encodeExecuteCall(address(barz), 0, addGuardianData);
userOp[0] = this.prepareUserOp(address(barz), nonce[address(barz)]++, callData);
signingKey[0] = user1PrivateKey;
userOp = signUserOperation(userOp, signingKey);
entryPoint.handleOps(userOp, payable(barz));
skip(GuardianFacet(address(barz)).getAdditionSecurityPeriod() + 1);
GuardianFacet(address(barz)).confirmGuardianAddition(_guardian);
address[] memory expectedGuardian = new address[](1);
expectedGuardian[0] = _guardian;
}
function test_approveAccountRecovery() public {
_addGuardian(guardian1);
uint64 expectedExpiry = uint64(block.timestamp + Constants.defaultApprovalValidationPeriod);
vm.expectEmit(true, true, false, true, address(barz));
emit RecoveryApproved(recoveryPublicKey, guardian1, expectedExpiry);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
}
function test_revokeAccountRecoveryApproval() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
uint64 expectedExpiry = uint64(block.timestamp + Constants.defaultApprovalValidationPeriod);
vm.expectEmit(true, true, false, true, address(barz));
emit RecoveryApproved(recoveryPublicKey, guardian1, expectedExpiry);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
vm.expectEmit(true, true, false, true, address(barz));
emit RecoveryApprovalRevoked(recoveryPublicKey, guardian1);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).revokeAccountRecoveryApproval(recoveryPublicKey);
}
function test_executeRecovery() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
address[] memory guardians = new address[](2);
guardians[0] = guardian1;
guardians[1] = guardian2;
bytes[] memory recoveryApprovalSignatures = new bytes[](2);
bytes32 recoveryPublicKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(recoveryPublicKey, "ExecuteRecovery");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(guardian1PrivateKey, recoveryPublicKeyHash);
recoveryApprovalSignatures[0] = abi.encodePacked(r, s, v);
(v, r, s) = vm.sign(guardian2PrivateKey, recoveryPublicKeyHash);
recoveryApprovalSignatures[1] = abi.encodePacked(r, s, v);
vm.expectEmit(true, true, false, true);
emit RecoveryExecuted(recoveryPublicKey, uint64(block.timestamp + Constants.defaultRecoveryPeriod));
AccountRecoveryFacet(address(barz)).executeRecovery(recoveryPublicKey, guardians, recoveryApprovalSignatures);
// Barz should be locked if recovery is executed
assertEq(LockFacet(address(barz)).isLocked(), true);
}
function test_finalizeRecovery() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
// checking the state of owner before recovery
assertEq(bytesToAddress(Secp256k1VerificationFacet(address(barz)).owner()), publicKeyToAddress(user1PublicKey));
// approve on-chain & off-chain
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
address[] memory guardians = new address[](1);
guardians[0] = guardian2;
bytes[] memory recoveryApprovalSignatures = new bytes[](1);
bytes32 recoveryPublicKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(recoveryPublicKey, "ExecuteRecovery");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(guardian2PrivateKey, recoveryPublicKeyHash);
recoveryApprovalSignatures[0] = abi.encodePacked(r, s, v);
vm.expectEmit(true, true, false, true);
emit RecoveryExecuted(recoveryPublicKey, uint64(block.timestamp + Constants.defaultRecoveryPeriod));
AccountRecoveryFacet(address(barz)).executeRecovery(recoveryPublicKey, guardians, recoveryApprovalSignatures);
skip(Constants.defaultRecoveryPeriod + 1);
vm.expectEmit(true, false, false, true);
emit RecoveryFinalized(recoveryPublicKey);
AccountRecoveryFacet(address(barz)).finalizeRecovery();
// checking if the owner of Barz has been updated
assertEq(bytesToAddress(Secp256k1VerificationFacet(address(barz)).owner()), publicKeyToAddress(recoveryPublicKey));
}
function test_approveCancelRecovery() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
_initiateRecovery();
uint64 expectedExecutionTime = uint64(block.timestamp + Constants.defaultRecoveryPeriod);
RecoveryConfig memory recoveryConfig = AccountRecoveryFacet(address(barz)).getPendingRecovery();
assertEq(recoveryConfig.recoveryPublicKey, recoveryPublicKey);
assertEq(recoveryConfig.executeAfter, expectedExecutionTime);
vm.expectEmit(true, true, false, true);
emit RecoveryCancellationApproved(recoveryPublicKey, guardian1);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveCancelRecovery(recoveryPublicKey);
vm.expectEmit(true, false, false, true);
emit RecoveryCanceled(recoveryPublicKey);
vm.prank(guardian2);
AccountRecoveryFacet(address(barz)).approveCancelRecovery(recoveryPublicKey);
// check if the final state has been initialized back to 0
RecoveryConfig memory updatedRecoveryConfig = AccountRecoveryFacet(address(barz)).getPendingRecovery();
assertEq(updatedRecoveryConfig.recoveryPublicKey, new bytes(0));
assertEq(updatedRecoveryConfig.executeAfter, uint64(0));
}
function test_hardStopRecovery() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
_initiateRecovery();
bytes32 hardstopPublicKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(
"0",
"HardstopRecovery"
);
bytes32 domainSeparator = keccak256(abi.encode(LibVerification.DOMAIN_SEPARATOR_TYPEHASH, block.chainid, address(barz)));
bytes32 encodedMessageHash = keccak256(abi.encode(LibVerification.BARZ_MSG_TYPEHASH, keccak256(abi.encode(hardstopPublicKeyHash))));
bytes32 msgHash = keccak256(abi.encodePacked("\x19\x01", domainSeparator, encodedMessageHash));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(user1PrivateKey, msgHash);
vm.expectEmit(true, false, false, true);
emit RecoveryHardstopped();
AccountRecoveryFacet(address(barz)).hardstopRecovery(abi.encodePacked(r, s, v));
assertEq(LockFacet(address(barz)).isLocked(), false);
}
function test_cancelRecovery() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
_initiateRecovery();
address[] memory guardians = new address[](2);
guardians[0] = guardian1;
guardians[1] = guardian2;
bytes[] memory recoveryApprovalSignatures = new bytes[](2);
bytes32 recoveryPublicKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(recoveryPublicKey, "CancelRecovery");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(guardian1PrivateKey, recoveryPublicKeyHash);
recoveryApprovalSignatures[0] = abi.encodePacked(r, s, v);
(v, r, s) = vm.sign(guardian2PrivateKey, recoveryPublicKeyHash);
recoveryApprovalSignatures[1] = abi.encodePacked(r, s, v);
vm.expectEmit(true, true, false, true);
emit RecoveryCanceled(recoveryPublicKey);
AccountRecoveryFacet(address(barz)).cancelRecovery(recoveryPublicKey, guardians, recoveryApprovalSignatures);
// Barz should be unlocked if recovery is canceled
assertEq(LockFacet(address(barz)).isLocked(), false);
}
function test_validateNewOwner() public {
// to be successful with valid public key
AccountRecoveryFacet(address(barz)).validateNewOwner(recoveryPublicKey);
AccountRecoveryFacet(address(barz)).validateNewOwner(abi.encodePacked(publicKeyToAddress(recoveryPublicKey)));
// to revert - invalid format for Secp256k1VerificationFacet
vm.expectRevert(AccountRecoveryFacet__InvalidRecoveryPublicKey.selector);
AccountRecoveryFacet(address(barz)).validateNewOwner(abi.encodePacked("0x1234567890"));
}
function test_getApprovalRecoveryKeyHash() public {
// sample testing with execute recovery hash
bytes32 actualKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(recoveryPublicKey, "ExecuteRecovery");
uint256 recoveryFacetNonce = 0;
bytes32 expectedKeyHash = keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
keccak256(
abi.encode(
recoveryPublicKey,
"ExecuteRecovery",
address(barz),
getChainId(),
recoveryFacetNonce
)
)
)
);
assertEq(actualKeyHash, expectedKeyHash);
}
function test_getRecoveryApprovalCountWithTimeValidity() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
bytes32 recoveryKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(recoveryPublicKey, "ExecuteRecovery");
assertEq(AccountRecoveryFacet(address(barz)).getRecoveryApprovalCountWithTimeValidity(recoveryKeyHash), 0);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
assertEq(AccountRecoveryFacet(address(barz)).getRecoveryApprovalCountWithTimeValidity(recoveryKeyHash), 1);
vm.prank(guardian2);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
assertEq(AccountRecoveryFacet(address(barz)).getRecoveryApprovalCountWithTimeValidity(recoveryKeyHash), 2);
skip(block.timestamp + Constants.defaultApprovalValidationPeriod);
assertEq(AccountRecoveryFacet(address(barz)).getRecoveryApprovalCountWithTimeValidity(recoveryKeyHash), 0);
}
function test_isRecoveryApproved() public {
_addGuardian(guardian1);
_addGuardian(guardian2);
bytes32 recoveryKeyHash = AccountRecoveryFacet(address(barz)).getApprovalRecoveryKeyHash(recoveryPublicKey, "ExecuteRecovery");
assertEq(AccountRecoveryFacet(address(barz)).isRecoveryApproved(recoveryKeyHash, guardian1), false);
assertEq(AccountRecoveryFacet(address(barz)).isRecoveryApproved(recoveryKeyHash, guardian2), false);
vm.prank(guardian1);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
assertEq(AccountRecoveryFacet(address(barz)).isRecoveryApproved(recoveryKeyHash, guardian1), true);
vm.prank(guardian2);
AccountRecoveryFacet(address(barz)).approveAccountRecovery(recoveryPublicKey);
assertEq(AccountRecoveryFacet(address(barz)).isRecoveryApproved(recoveryKeyHash, guardian2), true);
skip(block.timestamp + Constants.defaultApprovalValidationPeriod);
assertEq(AccountRecoveryFacet(address(barz)).isRecoveryApproved(recoveryKeyHash, guardian1), false);
assertEq(AccountRecoveryFacet(address(barz)).isRecoveryApproved(recoveryKeyHash, guardian2), false);
}
}