-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathERC4337Account.spec
More file actions
264 lines (190 loc) · 9.92 KB
/
ERC4337Account.spec
File metadata and controls
264 lines (190 loc) · 9.92 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
using EntryPointMock as EntryPoint;
use builtin rule sanity;
methods {
function SignatureCheckerLib.isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal returns (bool)
with (env e) => isValidSignatureNowCVL(e.block.timestamp, signer, hash, signature);
function SignatureCheckerLib.isValidSignatureNowCalldata(address signer, bytes32 hash, bytes calldata signature) internal returns (bool)
with (env e) => isValidSignatureNowCVL(e.block.timestamp, signer, hash, signature);
function WebAuthn.verify(bytes memory, bool, WebAuthn.WebAuthnAuth memory, uint256, uint256) internal returns (bool) => NONDET;
function EntryPoint.balanceOf(address account) external returns (uint256) envfree;
function nextOwnerIndex() external returns (uint256) envfree;
function ownerAtIndex(uint256) external returns (bytes) envfree;
function isOwnerAddress(address) external returns (bool) envfree;
function isOwnerBytes(bytes) external returns (bool) envfree;
function compareBytes(bytes, bytes) external returns (bool) envfree;
function entryPoint() external returns (address) envfree;
}
persistent ghost signatureValidTime(bytes32,uint256) returns bool;
persistent ghost validSignature(address,bytes32,bytes32) returns bool;
definition initialized() returns bool = nextOwnerIndex() > 0;
function isValidSignatureNowCVL(uint256 time, address signer, bytes32 hash, bytes signature) returns bool {
bytes32 hashSignature = keccak256(signature);
return signatureValidTime(hashSignature, time) && validSignature(signer, hash, hashSignature);
}
// STATUS - verified
// After initialize has been called, nextOwnerIndex > 0
rule afterInitialize(env e) {
bytes[] owners;
require owners.length > 0; // if owners array is empty, nothing happens
initialize(e, owners);
assert nextOwnerIndex() > 0, "Remember, with great power comes great responsibility.";
}
// STATUS - verified
// Initialize can't be called twice.
rule cantInitTwice(env e1, env e2, env e3, method f) filtered {
f -> f.selector != sig:initialize(bytes[]).selector // calling initialize() in f will lead to unreachability
} {
bytes[] owners1;
bytes[] owners2;
require owners1.length > 0; // if owners array is empty, nothing happens
initialize(e1, owners1);
calldataarg args;
f(e3, args); // checking if something can make initialize callable twice
initialize@withrevert(e2, owners2);
bool isReverted = lastReverted;
assert isReverted, "Remember, with great power comes great responsibility.";
}
// STATUS - verified
// After initialisation, if ownerAtIndex[i] changes, msg.sender must be an owner
rule onlyOwnerCanChangeOwnerAtIndex(env e, method f) filtered {
f -> !f.isView // view functions aren't state-changing, so no reason to check them
&& f.selector != sig:initialize(bytes[]).selector // we are in a state after initialization, so initialize() fails vacuity anyway
} {
uint256 i;
bytes ownerAtIndexBefore = ownerAtIndex(i);
bool ownerBefore = e.msg.sender == currentContract || isOwnerAddress(e.msg.sender); // owner can remove themselves
require initialized();
calldataarg args;
f(e, args);
bytes ownerAtIndexAfter = ownerAtIndex(i);
assert !compareBytes(ownerAtIndexBefore, ownerAtIndexAfter)
=> ownerBefore;
}
// STATUS - verified
// After initialisation, If isOwner[i] changes, msg.sender must be an owner
rule onlyOwnerCanChangeIsOwnerBytes(env e, method f) filtered {
f -> !f.isView // view functions aren't state-changing, so no reason to check them
&& f.selector != sig:initialize(bytes[]).selector // we are in a state after initialization, so initialize() fails vacuity anyway
} {
bytes account;
bool isOwnerBytesBefore = isOwnerBytes(account);
bool ownerBefore = e.msg.sender == currentContract || isOwnerAddress(e.msg.sender); // owner can remove themselves
require initialized();
calldataarg args;
f(e, args);
bool isOwnerBytesAfter = isOwnerBytes(account);
assert isOwnerBytesBefore != isOwnerBytesAfter => ownerBefore;
}
// STATUS - verified
// Only owner or self can call addOwnerAddress, addOwnerPublicKey,
// removeOwnerAtIndex, upgradeToAndCall(harnessed)
rule OnlyOwnerOrSelf(env e, method f) filtered {
// There is no difference between filtering functions and using them as a lhs of implication
f -> f.selector == sig:addOwnerAddress(address).selector
|| f.selector == sig:addOwnerPublicKey(bytes32, bytes32).selector
|| f.selector == sig:removeOwnerAtIndex(uint256, bytes).selector
|| f.selector == sig:removeLastOwner(uint256, bytes).selector
|| f.selector == sig:upgradeToAndCall(address, bytes).selector
} {
bool ownerBefore = e.msg.sender == currentContract || isOwnerAddress(e.msg.sender); // owner can remove themselves
calldataarg args;
f@withrevert(e, args);
bool isReverted = lastReverted;
assert !isReverted => ownerBefore;
}
// STATUS - verified
// Only EntryPoint, owner, or self can call execute, executeBatch
rule OnlyOwnerSelfOrEntryPoint(env e, method f) filtered {
// There is no difference between filtering functions and using them as a lhs of implication
f -> f.selector == sig:execute(address, uint256, bytes).selector
|| f.selector == sig:executeBatch(CoinbaseSmartWallet.Call[]).selector
} {
bool ownerBefore = e.msg.sender == currentContract
|| isOwnerAddress(e.msg.sender)
|| e.msg.sender == entryPoint();
calldataarg args;
f@withrevert(e, args);
bool isReverted = lastReverted;
assert !isReverted => ownerBefore;
}
// STATUS - verified
// Only EntryPoint can call executeWithoutChainIdValidation, validateUserOp
rule OnlyEntryPoint(env e, method f) filtered {
// There is no difference between filtering functions and using them as a lhs of implication
f -> f.selector == sig:validateUserOp(EntryPointMock.UserOperation, bytes32, uint256).selector
|| f.selector == sig:executeWithoutChainIdValidation(bytes[]).selector
} {
bool ownerBefore = e.msg.sender == entryPoint();
calldataarg args;
f@withrevert(e, args);
bool isReverted = lastReverted; // added revert case so advanced sanity check doesn't fail
assert !isReverted => ownerBefore;
}
// STATUS - verified
// There is no owner at index greater or equal to nextOwnerIndex
invariant noMoreThanNextOwnerIndex(uint256 i)
i >= nextOwnerIndex() && nextOwnerIndex() < max_uint256 => ownerAtIndex(i).length == 0;
// STATUS - verified
// nextOwnerIndex should grow monotonically
rule newUserIndexMonotonicGrowth(env e, method f){
uint256 _index = nextOwnerIndex();
calldataarg args;
f(e, args);
uint256 index_ = nextOwnerIndex();
assert index_ >= _index,"next owner index cannot decrease";
}
// STATUS - verified
// unless it's the initialize function, index should grow by 1 only
rule newUserIndexGrowsBy1(env e, method f)
filtered { f -> f.selector != sig:initialize(bytes[]).selector }{
uint256 _index = nextOwnerIndex();
calldataarg args;
f(e, args);
uint256 index_ = nextOwnerIndex();
assert index_ == _index || _index + 1 == to_mathint(index_),"index can increase by 1 only";
}
// STATUS - verified
// Owner with length == 0 can’t be an owner
invariant emptyInNotAnOwner(bytes empty)
empty.length == 0 => !isOwnerBytes(empty);
// STATUS - verified
// Eth balance of account should decrease by at least missing account funds
rule ethBalanceDecreaseByMissingAccountFunds(env e){
uint256 _ethBalance = nativeBalances[currentContract];
EntryPointMock.UserOperation userOp;
bytes32 userOpHash;
uint256 missingAccountFunds;
require e.msg.value == 0;
require _ethBalance >= missingAccountFunds;
require missingAccountFunds + nativeBalances[e.msg.sender] <= max_uint256;
require EntryPoint == entryPoint();
require EntryPoint.balanceOf(currentContract) + missingAccountFunds <= max_uint112;
validateUserOp(e, userOp, userOpHash, missingAccountFunds);
uint256 ethBalance_ = nativeBalances[currentContract];
assert ethBalance_ + missingAccountFunds <= to_mathint(_ethBalance),"eth balance of account should go down by at least missingAccountFunds";
}
// STATUS - verified
// When we add an owner and index isn't max_uint256 => only the latest index was changed and length is increased by 1
rule addNewOwnerCheck(env e, method f) filtered {
f -> f.selector == sig:addOwnerAddress(address).selector
|| f.selector == sig:addOwnerPublicKey(bytes32, bytes32).selector
} {
uint256 index; uint256 anotherIndex;
bytes ownerAtIndexBefore = ownerAtIndex(index);
bytes ownerAtIndexAnotherBefore = ownerAtIndex(anotherIndex);
uint256 nextOwnerIndexBefore = nextOwnerIndex();
require index != anotherIndex; // make sure indexes are different to check that only the latest one was changed
require anotherIndex < nextOwnerIndex(); // make sure anotherIndex exists, so it should be unchanged
require isOwnerBytes(ownerAtIndex(anotherIndex)); // set a correlation between ownerAtIndex and isOwnerBytes
require index == nextOwnerIndex(); // checking "only the latest index was changed"
requireInvariant noMoreThanNextOwnerIndex(index); // making non-existing indexes empty.
requireInvariant noMoreThanNextOwnerIndex(anotherIndex); // making non-existing indexes empty
calldataarg args;
f(e, args);
bytes ownerAtIndexAfter = ownerAtIndex(index);
bytes ownerAtIndexAnotherAfter = ownerAtIndex(anotherIndex);
uint256 nextOwnerIndexAfter = nextOwnerIndex();
assert to_mathint(nextOwnerIndexBefore) == nextOwnerIndexAfter - 1;
assert !compareBytes(ownerAtIndexBefore, ownerAtIndexAfter);
assert compareBytes(ownerAtIndexAnotherBefore, ownerAtIndexAnotherAfter);
}