-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathEip7702TransactionLib.sol
More file actions
238 lines (209 loc) · 9.02 KB
/
Eip7702TransactionLib.sol
File metadata and controls
238 lines (209 loc) · 9.02 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
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.13 <0.9.0;
import {VmRlp} from "../StdVm.sol";
import {TxRlp} from "./TxRlp.sol";
import {AccessListItem} from "./AccessListTypes.sol";
/// @notice An EIP-7702 authorization tuple.
struct Eip7702Authorization {
uint256 chainId;
address codeAddress;
uint64 nonce;
uint8 yParity;
bytes32 r;
bytes32 s;
}
/// @notice An EIP-7702 (type 4) Ethereum transaction.
struct Eip7702Transaction {
uint64 chainId;
uint64 nonce;
uint256 maxPriorityFeePerGas;
uint256 maxFeePerGas;
uint64 gasLimit;
address to;
uint256 value;
bytes data;
AccessListItem[] accessList;
Eip7702Authorization[] authorizationList;
}
/// @title Builder and RLP encoder for EIP-7702 transactions (type 0x04).
library Eip7702TransactionLib {
using Eip7702TransactionLib for Eip7702Transaction;
/// @notice EIP-7702 transaction type prefix.
uint8 internal constant TX_TYPE = 0x04;
/// @notice EIP-7702 authorization magic for signing.
uint8 internal constant AUTH_MAGIC = 0x05;
/// @notice Creates a new EIP-7702 transaction with default values.
function create() internal view returns (Eip7702Transaction memory tx_) {
tx_.chainId = uint64(block.chainid);
tx_.gasLimit = 21000;
}
/// @notice Sets the chain ID.
function withChainId(Eip7702Transaction memory self, uint64 chainId)
internal
pure
returns (Eip7702Transaction memory)
{
self.chainId = chainId;
return self;
}
/// @notice Sets the nonce.
function withNonce(Eip7702Transaction memory self, uint64 nonce) internal pure returns (Eip7702Transaction memory) {
self.nonce = nonce;
return self;
}
/// @notice Sets the max priority fee per gas.
function withMaxPriorityFeePerGas(Eip7702Transaction memory self, uint256 fee)
internal
pure
returns (Eip7702Transaction memory)
{
self.maxPriorityFeePerGas = fee;
return self;
}
/// @notice Sets the max fee per gas.
function withMaxFeePerGas(Eip7702Transaction memory self, uint256 fee)
internal
pure
returns (Eip7702Transaction memory)
{
self.maxFeePerGas = fee;
return self;
}
/// @notice Sets the gas limit.
function withGasLimit(Eip7702Transaction memory self, uint64 gasLimit)
internal
pure
returns (Eip7702Transaction memory)
{
self.gasLimit = gasLimit;
return self;
}
/// @notice Sets the recipient address.
function withTo(Eip7702Transaction memory self, address to) internal pure returns (Eip7702Transaction memory) {
self.to = to;
return self;
}
/// @notice Sets the value to send.
function withValue(Eip7702Transaction memory self, uint256 value)
internal
pure
returns (Eip7702Transaction memory)
{
self.value = value;
return self;
}
/// @notice Sets the calldata.
function withData(Eip7702Transaction memory self, bytes memory data)
internal
pure
returns (Eip7702Transaction memory)
{
self.data = data;
return self;
}
/// @notice Sets the access list.
function withAccessList(Eip7702Transaction memory self, AccessListItem[] memory accessList)
internal
pure
returns (Eip7702Transaction memory)
{
self.accessList = accessList;
return self;
}
/// @notice Sets the authorization list.
function withAuthorizationList(Eip7702Transaction memory self, Eip7702Authorization[] memory authorizationList)
internal
pure
returns (Eip7702Transaction memory)
{
self.authorizationList = authorizationList;
return self;
}
/// @notice RLP encodes the unsigned transaction with type prefix 0x04.
/// @dev Format: 0x04 || RLP([chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, authorizationList])
function encode(Eip7702Transaction memory self, VmRlp) internal pure returns (bytes memory) {
bytes[] memory fields = new bytes[](10);
fields[0] = TxRlp.encodeString(TxRlp.encodeUint(self.chainId));
fields[1] = TxRlp.encodeString(TxRlp.encodeUint(self.nonce));
fields[2] = TxRlp.encodeString(TxRlp.encodeUint(self.maxPriorityFeePerGas));
fields[3] = TxRlp.encodeString(TxRlp.encodeUint(self.maxFeePerGas));
fields[4] = TxRlp.encodeString(TxRlp.encodeUint(self.gasLimit));
fields[5] = TxRlp.encodeString(self.to == address(0) ? TxRlp.encodeNone() : TxRlp.encodeAddress(self.to));
fields[6] = TxRlp.encodeString(TxRlp.encodeUint(self.value));
fields[7] = TxRlp.encodeString(self.data);
fields[8] = _encodeAccessList(self.accessList);
fields[9] = _encodeAuthorizationList(self.authorizationList);
bytes memory rlpPayload = TxRlp.encodeRawList(fields);
return abi.encodePacked(TX_TYPE, rlpPayload);
}
/// @notice RLP encodes the signed transaction with type prefix 0x04.
/// @dev Format: 0x04 || RLP([chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, authorizationList, yParity, r, s])
function encodeWithSignature(Eip7702Transaction memory self, VmRlp, uint8 v, bytes32 r, bytes32 s)
internal
pure
returns (bytes memory)
{
bytes[] memory fields = new bytes[](13);
fields[0] = TxRlp.encodeString(TxRlp.encodeUint(self.chainId));
fields[1] = TxRlp.encodeString(TxRlp.encodeUint(self.nonce));
fields[2] = TxRlp.encodeString(TxRlp.encodeUint(self.maxPriorityFeePerGas));
fields[3] = TxRlp.encodeString(TxRlp.encodeUint(self.maxFeePerGas));
fields[4] = TxRlp.encodeString(TxRlp.encodeUint(self.gasLimit));
fields[5] = TxRlp.encodeString(self.to == address(0) ? TxRlp.encodeNone() : TxRlp.encodeAddress(self.to));
fields[6] = TxRlp.encodeString(TxRlp.encodeUint(self.value));
fields[7] = TxRlp.encodeString(self.data);
fields[8] = _encodeAccessList(self.accessList);
fields[9] = _encodeAuthorizationList(self.authorizationList);
uint8 yParity = v >= 27 ? v - 27 : v;
fields[10] = TxRlp.encodeString(TxRlp.encodeUint(yParity));
fields[11] = TxRlp.encodeString(TxRlp.encodeBytes32(r));
fields[12] = TxRlp.encodeString(TxRlp.encodeBytes32(s));
bytes memory rlpPayload = TxRlp.encodeRawList(fields);
return abi.encodePacked(TX_TYPE, rlpPayload);
}
/// @notice Computes the authorization hash for signing.
/// @dev Hash: keccak256(0x05 || RLP([chainId, codeAddress, nonce]))
function computeAuthorizationHash(uint256 chainId, address codeAddress, uint64 authNonce)
internal
pure
returns (bytes32)
{
bytes[] memory fields = new bytes[](3);
fields[0] = TxRlp.encodeString(TxRlp.encodeUint(chainId));
fields[1] = TxRlp.encodeString(TxRlp.encodeAddress(codeAddress));
fields[2] = TxRlp.encodeString(TxRlp.encodeUint(authNonce));
bytes memory rlpPayload = TxRlp.encodeRawList(fields);
return keccak256(abi.encodePacked(AUTH_MAGIC, rlpPayload));
}
/// @notice Encodes the access list as an RLP list.
function _encodeAccessList(AccessListItem[] memory list) private pure returns (bytes memory) {
bytes[] memory encodedItems = new bytes[](list.length);
for (uint256 i = 0; i < list.length; i++) {
bytes[] memory keys = new bytes[](list[i].storageKeys.length);
for (uint256 j = 0; j < list[i].storageKeys.length; j++) {
keys[j] = TxRlp.encodeString(TxRlp.encodeBytes32Full(list[i].storageKeys[j]));
}
bytes memory keysList = TxRlp.encodeRawList(keys);
bytes[] memory itemFields = new bytes[](2);
itemFields[0] = TxRlp.encodeString(TxRlp.encodeAddress(list[i].target));
itemFields[1] = keysList;
encodedItems[i] = TxRlp.encodeRawList(itemFields);
}
return TxRlp.encodeRawList(encodedItems);
}
/// @notice Encodes the authorization list as an RLP list.
function _encodeAuthorizationList(Eip7702Authorization[] memory list) private pure returns (bytes memory) {
bytes[] memory encodedAuths = new bytes[](list.length);
for (uint256 i = 0; i < list.length; i++) {
bytes[] memory authFields = new bytes[](6);
authFields[0] = TxRlp.encodeString(TxRlp.encodeUint(list[i].chainId));
authFields[1] = TxRlp.encodeString(TxRlp.encodeAddress(list[i].codeAddress));
authFields[2] = TxRlp.encodeString(TxRlp.encodeUint(list[i].nonce));
authFields[3] = TxRlp.encodeString(TxRlp.encodeUint(list[i].yParity));
authFields[4] = TxRlp.encodeString(TxRlp.encodeBytes32(list[i].r));
authFields[5] = TxRlp.encodeString(TxRlp.encodeBytes32(list[i].s));
encodedAuths[i] = TxRlp.encodeRawList(authFields);
}
return TxRlp.encodeRawList(encodedAuths);
}
}