forked from erc6900/reference-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationResHelpers.sol
50 lines (42 loc) · 1.94 KB
/
ValidationResHelpers.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;
// solhint-disable-next-line private-vars-leading-underscore
function _coalescePreValidation(uint256 validationRes1, uint256 validationRes2)
pure
returns (uint256 resValidationData)
{
uint48 validUntil1 = uint48(validationRes1 >> 160);
if (validUntil1 == 0) {
validUntil1 = type(uint48).max;
}
uint48 validUntil2 = uint48(validationRes2 >> 160);
if (validUntil2 == 0) {
validUntil2 = type(uint48).max;
}
resValidationData = ((validUntil1 > validUntil2) ? uint256(validUntil2) << 160 : uint256(validUntil1) << 160);
uint48 validAfter1 = uint48(validationRes1 >> 208);
uint48 validAfter2 = uint48(validationRes2 >> 208);
resValidationData |= ((validAfter1 < validAfter2) ? uint256(validAfter2) << 208 : uint256(validAfter1) << 208);
// Once we know that the authorizer field is 0 or 1, we can safely bubble up SIG_FAIL with bitwise OR
resValidationData |= uint160(validationRes1) | uint160(validationRes2);
}
// solhint-disable-next-line private-vars-leading-underscore
function _coalesceValidation(uint256 preValidationData, uint256 validationRes)
pure
returns (uint256 resValidationData)
{
uint48 validUntil1 = uint48(preValidationData >> 160);
if (validUntil1 == 0) {
validUntil1 = type(uint48).max;
}
uint48 validUntil2 = uint48(validationRes >> 160);
if (validUntil2 == 0) {
validUntil2 = type(uint48).max;
}
resValidationData = ((validUntil1 > validUntil2) ? uint256(validUntil2) << 160 : uint256(validUntil1) << 160);
uint48 validAfter1 = uint48(preValidationData >> 208);
uint48 validAfter2 = uint48(validationRes >> 208);
resValidationData |= ((validAfter1 < validAfter2) ? uint256(validAfter2) << 208 : uint256(validAfter1) << 208);
// If prevalidation failed, bubble up failure
resValidationData |= uint160(preValidationData) == 1 ? 1 : uint160(validationRes);
}