-
Notifications
You must be signed in to change notification settings - Fork 37
/
ValidationLocatorLib.sol
373 lines (320 loc) · 14.8 KB
/
ValidationLocatorLib.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// This file is part of Modular Account.
//
// Copyright 2024 Alchemy Insights, Inc.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
// Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see
// <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.26;
import {DIRECT_CALL_VALIDATION_ENTITY_ID} from "@erc6900/reference-implementation/helpers/Constants.sol";
import {ModuleEntity, ModuleEntityLib} from "@erc6900/reference-implementation/libraries/ModuleEntityLib.sol";
import {
ValidationConfig,
ValidationConfigLib
} from "@erc6900/reference-implementation/libraries/ValidationConfigLib.sol";
import {ValidationStorage} from "../account/AccountStorage.sol";
// A type representing a validation lookup key and flags for validation options.
// The validation lookup key is a tagged union between a direct call validation address and a validation entity ID.
type ValidationLocator is uint168;
// Layout:
// Unused
// 0x0000000000000000000000__________________________________________
// Either the direct call validation's address, or the entity ID for non-direct-call validation.
// 0x______________________AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA__
// Validation options
// 0x______________________________________________________________BB
// ValidationOptions layout:
// 0b00000___ // Unused
// 0b_____A__ // is direct call validation (union tag)
// 0b______B_ // has deferred action
// 0b_______C // is global validation
// A type representing only the validation lookup key, with validation options masked out except for the
// direct call validation flag.
type ValidationLookupKey is uint168;
using ValidationLocatorLib for ValidationLocator global;
using ValidationLocatorLib for ValidationLookupKey global;
library ValidationLocatorLib {
using ValidationConfigLib for ValidationConfig;
uint8 internal constant _VALIDATION_TYPE_GLOBAL = 1;
uint8 internal constant _HAS_DEFERRED_ACTION = 2;
uint8 internal constant _IS_DIRECT_CALL_VALIDATION = 4;
function moduleEntity(ValidationLookupKey _lookupKey, ValidationStorage storage validationStorage)
internal
view
returns (ModuleEntity result)
{
if (_lookupKey.isDirectCallValidation()) {
result = ModuleEntityLib.pack(_lookupKey.directCallAddress(), DIRECT_CALL_VALIDATION_ENTITY_ID);
} else {
result = ModuleEntityLib.pack(validationStorage.module, _lookupKey.entityId());
}
}
// User op nonce, 4337 mandated layout:
// 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA________________ // Parallel Nonce Key
// 0x________________________________________________BBBBBBBBBBBBBBBB // Sequential Nonce Key
// User op nonce, Alchemy MA usage:
// With non-direct call validation
// 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA__________________________ // Parallel Nonce Key
// 0x______________________________________BBBBBBBB__________________ // Validation Entity ID
// 0x______________________________________________CC________________ // Options byte
// 0x________________________________________________BBBBBBBBBBBBBBBB // Sequential Nonce Key
// With direct call validation
// 0xAAAAAA__________________________________________________________ // Parallel Nonce Key
// 0x______BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB__________________ // Caller address of direct-call
// validation
// 0x______________________________________________CC________________ // Options byte
// 0x________________________________________________BBBBBBBBBBBBBBBB // Sequential Nonce Key
function loadFromNonce(uint256 nonce) internal pure returns (ValidationLocator result) {
assembly ("memory-safe") {
nonce := shr(64, nonce)
let validationType := and(nonce, _IS_DIRECT_CALL_VALIDATION)
switch validationType
case 0 {
// If not using direct call validation, the validation locator contains a 4-byte entity ID
// Mask it to the lower 5 bytes
result := and(nonce, 0xFFFFFFFFFF)
}
default {
// If using direct call validation, the validation locator contains a 20-byte address
// Mask it to the lower 21 bytes
result := and(nonce, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
}
}
}
// executeRuntimeValidation authorization layout, and isValidSignature signature layout
// [1-byte options][4-byte validation id OR 20-byte address of direct call validation][remainder]
// With non-direct call validation
// 0xAA______________ // Validation Type
// 0x__BBBBBBBB______ // Validation Entity ID
// 0x__________CCC... // Remainder
// With direct call validation
// 0xAA______________________________________________ // Validation Type
// 0x__BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB______ // Caller address of direct-call validation
// 0x__________________________________________CCC... // Remainder
function loadFromSignature(bytes calldata signature)
internal
pure
returns (ValidationLocator result, bytes calldata remainder)
{
assembly ("memory-safe") {
// Regular validation requires at least 5 bytes. Direct call validation requires at least 21 bytes,
// checked later.
if lt(signature.length, 5) { revert(0, 0) }
result := calldataload(signature.offset)
let validationOptions := shr(248, result)
switch and(validationOptions, _IS_DIRECT_CALL_VALIDATION)
case 0 {
// If not using direct call validation, the validation locator contains a 32-byte entity ID
// Result contains:
// 0xAA______________________________________________________________ // Validation Type
// 0x__BBBBBBBB______________________________________________________ // Validation Entity ID
// 0x__________CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC // Remainder bits and/or
// zeros
// We need to clear the upper byte by shifting left 1 bytes (8 bits), then shift right 28 bytes
// (224 bits), leaving only the entity ID.
result := shr(224, shl(8, result))
// Next, we need to set the validation type, which is 0 in this branch
result := or(shl(8, result), validationOptions)
// Advance the remainder by 5 bytes
remainder.offset := add(signature.offset, 5)
remainder.length := sub(signature.length, 5)
}
default {
// Direct call validation requires at least 21 bytes
if lt(signature.length, 21) { revert(0, 0) }
// If using direct call validation, the validation locator contains a 20-byte address
// Result contains:
// 0xAA______________________________________________________________ // Validation Type
// 0x__BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB______________________ // Caller address of
// direct-call validation
// 0x__________________________________________CCCCCCCCCCCCCCCCCCCCCC // Remainder bits and/or
// zeros
// So we need to clear the upper byte by shifting left 1 bytes (8 bits), then shift right 12
// bytes (96 bits) to get the address.
result := shr(96, shl(8, result))
// Next, we need to set the validation type
result := or(shl(8, result), validationOptions)
// Advance the remainder by 21 bytes
remainder.offset := add(signature.offset, 21)
remainder.length := sub(signature.length, 21)
}
}
}
// Only safe to call if the lookup has been asserted to be a direct call validation.
function directCallAddress(ValidationLookupKey _lookupKey) internal pure returns (address result) {
assembly ("memory-safe") {
result := and(shr(8, _lookupKey), 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
}
}
// Only safe to call if the lookup has been asserted to be a non-direct call validation.
function entityId(ValidationLookupKey _lookupKey) internal pure returns (uint32 result) {
assembly ("memory-safe") {
result := and(shr(8, _lookupKey), 0xFFFFFFFF)
}
}
function isGlobal(ValidationLocator locator) internal pure returns (bool) {
return (ValidationLocator.unwrap(locator) & _VALIDATION_TYPE_GLOBAL) != 0;
}
function hasDeferredAction(ValidationLocator locator) internal pure returns (bool) {
return (ValidationLocator.unwrap(locator) & _HAS_DEFERRED_ACTION) != 0;
}
function isDirectCallValidation(ValidationLookupKey _lookupKey) internal pure returns (bool) {
return (ValidationLookupKey.unwrap(_lookupKey) & _IS_DIRECT_CALL_VALIDATION) != 0;
}
function configToLookupKey(ValidationConfig validationConfig)
internal
pure
returns (ValidationLookupKey result)
{
if (validationConfig.entityId() == DIRECT_CALL_VALIDATION_ENTITY_ID) {
result = ValidationLookupKey.wrap(
uint168(uint160(validationConfig.module())) << 8 | _IS_DIRECT_CALL_VALIDATION
);
} else {
result = ValidationLookupKey.wrap(uint168(uint160(validationConfig.entityId())) << 8);
}
}
function moduleEntityToLookupKey(ModuleEntity _moduleEntity)
internal
pure
returns (ValidationLookupKey result)
{
(address module, uint32 _entityId) = ModuleEntityLib.unpack(_moduleEntity);
if (_entityId == DIRECT_CALL_VALIDATION_ENTITY_ID) {
result = ValidationLookupKey.wrap(uint168(uint160(module)) << 8 | _IS_DIRECT_CALL_VALIDATION);
} else {
result = ValidationLookupKey.wrap(uint168(uint160(_entityId)) << 8);
}
}
function directCallLookupKey(address directCallValidation)
internal
pure
returns (ValidationLookupKey result)
{
result = ValidationLookupKey.wrap(uint168(uint160(directCallValidation)) << 8 | _IS_DIRECT_CALL_VALIDATION);
}
function lookupKey(ValidationLocator locator) internal pure returns (ValidationLookupKey result) {
assembly ("memory-safe") {
result := and(locator, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF04)
}
}
// Packing functions. These should not be used in the account, but in scripts and tests.
function pack(uint32 _entityId, bool _isGlobal, bool _hasDeferredAction)
internal
pure
returns (ValidationLocator)
{
uint168 result = uint168(_entityId) << 8;
if (_isGlobal) {
result |= _VALIDATION_TYPE_GLOBAL;
}
if (_hasDeferredAction) {
result |= _HAS_DEFERRED_ACTION;
}
return ValidationLocator.wrap(result);
}
function packDirectCall(address directCallValidation, bool _isGlobal, bool _hasDeferredAction)
internal
pure
returns (ValidationLocator)
{
uint168 result = uint168(uint160(directCallValidation)) << 8 | _IS_DIRECT_CALL_VALIDATION;
if (_isGlobal) {
result |= _VALIDATION_TYPE_GLOBAL;
}
if (_hasDeferredAction) {
result |= _HAS_DEFERRED_ACTION;
}
return ValidationLocator.wrap(result);
}
function packNonce(uint32 validationEntityId, bool _isGlobal, bool _hasDeferredAction)
internal
pure
returns (uint256 result)
{
result = uint256(validationEntityId) << 8;
if (_isGlobal) {
result |= _VALIDATION_TYPE_GLOBAL;
}
if (_hasDeferredAction) {
result |= _HAS_DEFERRED_ACTION;
}
// Finally, shift left to make space for the sequential nonce key
result <<= 64;
}
function packNonceDirectCall(address directCallValidation, bool _isGlobal, bool _hasDeferredAction)
internal
pure
returns (uint256 result)
{
result = uint256(uint160(directCallValidation)) << 8 | _IS_DIRECT_CALL_VALIDATION;
if (_isGlobal) {
result |= _VALIDATION_TYPE_GLOBAL;
}
if (_hasDeferredAction) {
result |= _HAS_DEFERRED_ACTION;
}
// Finally, shift left to make space for the sequential nonce key
result <<= 64;
}
function packSignature(uint32 validationEntityId, bool _isGlobal, bytes memory signature)
internal
pure
returns (bytes memory result)
{
uint8 options = 0;
if (_isGlobal) {
options |= _VALIDATION_TYPE_GLOBAL;
}
return bytes.concat(abi.encodePacked(options, uint32(validationEntityId)), signature);
}
function packSignatureDirectCall(
address directCallValidation,
bool _isGlobal,
bool _hasDeferredAction,
bytes memory signature
) internal pure returns (bytes memory result) {
uint8 options = _IS_DIRECT_CALL_VALIDATION;
if (_isGlobal) {
options |= _VALIDATION_TYPE_GLOBAL;
}
if (_hasDeferredAction) {
options |= _HAS_DEFERRED_ACTION;
}
return bytes.concat(abi.encodePacked(options, uint160(directCallValidation)), signature);
}
// Converts a module entity to a locator.
function packFromModuleEntity(ModuleEntity _moduleEntity, bool _isGlobal, bool _hasDeferredAction)
internal
pure
returns (ValidationLocator)
{
uint168 result;
(address module, uint32 _entityId) = ModuleEntityLib.unpack(_moduleEntity);
if (_entityId == DIRECT_CALL_VALIDATION_ENTITY_ID) {
result = uint168(uint160(module)) << 8 | _IS_DIRECT_CALL_VALIDATION;
} else {
result = uint168(_entityId) << 8;
}
if (_isGlobal) {
result |= _VALIDATION_TYPE_GLOBAL;
}
if (_hasDeferredAction) {
result |= _HAS_DEFERRED_ACTION;
}
return ValidationLocator.wrap(result);
}
// Operators
function eq(ValidationLookupKey a, ValidationLookupKey b) internal pure returns (bool) {
return ValidationLookupKey.unwrap(a) == ValidationLookupKey.unwrap(b);
}
}