-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashLib.sol
More file actions
180 lines (168 loc) · 7.32 KB
/
Copy pathHashLib.sol
File metadata and controls
180 lines (168 loc) · 7.32 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
import { EfficientHashLib } from "solady/utils/EfficientHashLib.sol";
interface IERC5267 {
function eip712Domain()
external
view
returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
);
}
// keccak256("SuperTx(MeeUserOp[] meeUserOps)MeeUserOp(bytes32 userOpHash,uint256 lowerBoundTimestamp,uint256
// upperBoundTimestamp)");
bytes32 constant SUPER_TX_MEE_USER_OP_ARRAY_TYPEHASH =
0x18920ab59b79e66eb8250f08215198bc72e5a4b3822706ea145ae8f0cbb22526;
// keccak256("EIP712Domain(string name)");
bytes32 constant _DOMAIN_TYPEHASH = 0xb2178a58fb1eefb359ecfdd57bb19c0bdd0f4e6eed8547f46600e500ed111af3;
uint256 constant STATIC_HEAD_LENGTH = 0x80; // introduced to re-use it in the contracts that use this library
library HashLib {
error UnexpectedSuperTxEntry(bytes32 occurredItemHash, bytes32 expectedItemHash);
using EfficientHashLib for *;
function parsePackedSigDataHead(bytes calldata packedSignatureData)
internal
pure
returns (bytes32 outerTypeHash, uint256 itemIndex, bytes32[] calldata itemHashes, bytes calldata signature)
{
/*
* packedSignatureData layout :
* ======== static head part : 0x80 (128) bytes========
* 32 bytes : outerTypeHash
* 32 bytes : itemIndex
* 32 bytes : itemHashes offset
* 32 bytes : signature offset
* ======== static tail for fusion modes =====
* ....
* ======== dynamic tail ==========
* 32 bytes : itemHashes length
* ..... : itemHashes content
* 32 bytes : signature length
* ..... : signature content
*/
assembly {
outerTypeHash := calldataload(packedSignatureData.offset)
itemIndex := calldataload(add(packedSignatureData.offset, 0x20))
let u := calldataload(add(packedSignatureData.offset, 0x40)) // local offset of the array of hashes
let s := add(packedSignatureData.offset, u) // global offset of the array of hashes
itemHashes.offset := add(s, 0x20) // account for 32 bytes length
itemHashes.length := calldataload(s) // get the length
u := calldataload(add(packedSignatureData.offset, sub(STATIC_HEAD_LENGTH, 0x20))) // load local offset of
// the
// signature
s := add(packedSignatureData.offset, u) // global offset of the signature
signature.offset := add(s, 0x20) // account for 32 bytes length
signature.length := calldataload(s) // get the length
}
}
function compareAndGetFinalHashForAccount(
address account,
bytes32 outerTypeHash,
bytes32 currentItemHash,
uint256 itemIndex,
bytes32[] calldata itemHashes
)
internal
view
returns (bytes32 finalHash)
{
// Compare
if (currentItemHash != itemHashes[itemIndex]) {
revert UnexpectedSuperTxEntry(currentItemHash, itemHashes[itemIndex]);
} else {
// SuperTx is a dynamic struct { EntryType1 entryA, EntryType2 entryB, ... EntryTypeN entryX }
// It's typehash is provided from the sdk, and the items are considered to be already
// hashed as properly encoded structs as per eip-712 and provided as bytes32 hashes.
// hashStruct(s : 𝕊) = keccak256(typeHash ‖ encodeData(s))
// The encoding of a struct instance is enc(value₁) ‖ enc(value₂) ‖ … ‖ enc(valueₙ)
// Since all our values are bytes32, we need to just concat all of them
// There is one case which deserves a dedicated flow in terms of optimizations -
// it is when all the superTxn entries are MeeUserOps structs. Then it makes sense
// to treat them as an array of MeeUserOps structs and use the dedicated typehash.
bytes32 structHash;
if (outerTypeHash == SUPER_TX_MEE_USER_OP_ARRAY_TYPEHASH) {
// if SuperTx is an array of MeeUserOps structs, then encoded data is a
// "keccak256 hash of the concatenated encodeData of their contents" as per eip-712
uint256 length = itemHashes.length;
bytes32[] memory a = EfficientHashLib.malloc(length);
for (uint256 i; i < length; ++i) {
a.set(i, itemHashes[i]);
}
bytes32 encodedData = a.hash();
structHash = EfficientHashLib.hash(outerTypeHash, encodedData);
} else {
// if SuperTx is a struct, then encoded data is just the concat of all the itemHashes
/// forge-lint:disable-next-line(asm-keccak256)
structHash = keccak256(abi.encodePacked(outerTypeHash, itemHashes));
}
finalHash = hashTypedDataForAccount(account, structHash);
}
}
function compareAndGetFinalHash(
bytes32 outerTypeHash,
bytes32 currentItemHash,
uint256 itemIndex,
bytes32[] calldata itemHashes
)
internal
view
returns (bytes32 finalHash)
{
finalHash = compareAndGetFinalHashForAccount(msg.sender, outerTypeHash, currentItemHash, itemIndex, itemHashes);
}
function rehashWithAccountAndChainId(
bytes32 dataHash,
address account,
uint256 chainId
)
internal
pure
returns (bytes32 res)
{
//res = keccak256(abi.encodePacked(dataHash, account, chainId));
assembly {
let ptr := mload(0x40)
mstore(ptr, dataHash)
mstore(add(ptr, 0x20), shl(96, account))
mstore(add(ptr, 0x34), chainId)
res := keccak256(ptr, 0x54)
}
}
/// @notice Hashes typed data according to eip-712
/// Uses account's domain separator
/// @param account the smart account, who's domain separator will be used
/// @param structHash the typed data struct hash
function hashTypedDataForAccount(address account, bytes32 structHash) internal view returns (bytes32 digest) {
(
/*bytes1 fields*/
,
string memory name,
/*string memory version*/,
/*uint256 chainId*/,
/*address verifyingContract*/,
/*bytes32 salt*/
,
/*uint256[] memory extensions*/
) = IERC5267(account).eip712Domain();
/// @solidity memory-safe-assembly
assembly {
//Rebuild domain separator out of 712 domain
let m := mload(0x40) // Load the free memory pointer.
mstore(m, _DOMAIN_TYPEHASH)
mstore(add(m, 0x20), keccak256(add(name, 0x20), mload(name))) // Name hash.
digest := keccak256(m, 0x40) //domain separator
// Hash typed data
mstore(0x00, 0x1901000000000000) // Store "\x19\x01".
mstore(0x1a, digest) // Store the domain separator.
mstore(0x3a, structHash) // Store the struct hash.
digest := keccak256(0x18, 0x42)
// Restore the part of the free memory slot that was overwritten.
mstore(0x3a, 0)
}
}
}