-
Notifications
You must be signed in to change notification settings - Fork 11
/
TransactionHelper.sol
414 lines (371 loc) · 19.2 KB
/
TransactionHelper.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "../openzeppelin/token/ERC20/IERC20.sol";
import "../openzeppelin/token/ERC20/utils/SafeERC20.sol";
import "../interfaces/IPaymasterFlow.sol";
import "../interfaces/IContractDeployer.sol";
import {BASE_TOKEN_SYSTEM_CONTRACT, BOOTLOADER_FORMAL_ADDRESS} from "../Constants.sol";
import "./RLPEncoder.sol";
import "./EfficientCall.sol";
/// @dev The type id of zkSync's EIP-712-signed transaction.
uint8 constant EIP_712_TX_TYPE = 0x71;
/// @dev The type id of legacy transactions.
uint8 constant LEGACY_TX_TYPE = 0x0;
/// @dev The type id of legacy transactions.
uint8 constant EIP_2930_TX_TYPE = 0x01;
/// @dev The type id of EIP1559 transactions.
uint8 constant EIP_1559_TX_TYPE = 0x02;
/// @notice Structure used to represent a zkSync transaction.
struct Transaction {
// The type of the transaction.
uint256 txType;
// The caller.
uint256 from;
// The callee.
uint256 to;
// The gasLimit to pass with the transaction.
// It has the same meaning as Ethereum's gasLimit.
uint256 gasLimit;
// The maximum amount of gas the user is willing to pay for a byte of pubdata.
uint256 gasPerPubdataByteLimit;
// The maximum fee per gas that the user is willing to pay.
// It is akin to EIP1559's maxFeePerGas.
uint256 maxFeePerGas;
// The maximum priority fee per gas that the user is willing to pay.
// It is akin to EIP1559's maxPriorityFeePerGas.
uint256 maxPriorityFeePerGas;
// The transaction's paymaster. If there is no paymaster, it is equal to 0.
uint256 paymaster;
// The nonce of the transaction.
uint256 nonce;
// The value to pass with the transaction.
uint256 value;
// In the future, we might want to add some
// new fields to the struct. The `txData` struct
// is to be passed to account and any changes to its structure
// would mean a breaking change to these accounts. In order to prevent this,
// we should keep some fields as "reserved".
// It is also recommended that their length is fixed, since
// it would allow easier proof integration (in case we will need
// some special circuit for preprocessing transactions).
uint256[4] reserved;
// The transaction's calldata.
bytes data;
// The signature of the transaction.
bytes signature;
// The properly formatted hashes of bytecodes that must be published on L1
// with the inclusion of this transaction. Note, that a bytecode has been published
// before, the user won't pay fees for its republishing.
bytes32[] factoryDeps;
// The input to the paymaster.
bytes paymasterInput;
// Reserved dynamic type for the future use-case. Using it should be avoided,
// But it is still here, just in case we want to enable some additional functionality.
bytes reservedDynamic;
}
/**
* @author Matter Labs
* @custom:security-contact security@matterlabs.dev
* @notice Library is used to help custom accounts to work with common methods for the Transaction type.
*/
library TransactionHelper {
using SafeERC20 for IERC20;
/// @notice The EIP-712 typehash for the contract's domain
bytes32 constant EIP712_DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId)");
bytes32 constant EIP712_TRANSACTION_TYPE_HASH =
keccak256(
"Transaction(uint256 txType,uint256 from,uint256 to,uint256 gasLimit,uint256 gasPerPubdataByteLimit,uint256 maxFeePerGas,uint256 maxPriorityFeePerGas,uint256 paymaster,uint256 nonce,uint256 value,bytes data,bytes32[] factoryDeps,bytes paymasterInput)"
);
/// @notice Whether the token is Ethereum.
/// @param _addr The address of the token
/// @return `true` or `false` based on whether the token is Ether.
/// @dev This method assumes that address is Ether either if the address is 0 (for convenience)
/// or if the address is the address of the L2BaseToken system contract.
function isEthToken(uint256 _addr) internal pure returns (bool) {
return _addr == uint256(uint160(address(BASE_TOKEN_SYSTEM_CONTRACT))) || _addr == 0;
}
/// @notice Calculate the suggested signed hash of the transaction,
/// i.e. the hash that is signed by EOAs and is recommended to be signed by other accounts.
function encodeHash(Transaction calldata _transaction) internal view returns (bytes32 resultHash) {
if (_transaction.txType == LEGACY_TX_TYPE) {
resultHash = _encodeHashLegacyTransaction(_transaction);
} else if (_transaction.txType == EIP_712_TX_TYPE) {
resultHash = _encodeHashEIP712Transaction(_transaction);
} else if (_transaction.txType == EIP_1559_TX_TYPE) {
resultHash = _encodeHashEIP1559Transaction(_transaction);
} else if (_transaction.txType == EIP_2930_TX_TYPE) {
resultHash = _encodeHashEIP2930Transaction(_transaction);
} else {
// Currently no other transaction types are supported.
// Any new transaction types will be processed in a similar manner.
revert("Encoding unsupported tx");
}
}
/// @notice Encode hash of the zkSync native transaction type.
/// @return keccak256 hash of the EIP-712 encoded representation of transaction
function _encodeHashEIP712Transaction(Transaction calldata _transaction) private view returns (bytes32) {
bytes32 structHash = keccak256(
abi.encode(
EIP712_TRANSACTION_TYPE_HASH,
_transaction.txType,
_transaction.from,
_transaction.to,
_transaction.gasLimit,
_transaction.gasPerPubdataByteLimit,
_transaction.maxFeePerGas,
_transaction.maxPriorityFeePerGas,
_transaction.paymaster,
_transaction.nonce,
_transaction.value,
EfficientCall.keccak(_transaction.data),
keccak256(abi.encodePacked(_transaction.factoryDeps)),
EfficientCall.keccak(_transaction.paymasterInput)
)
);
bytes32 domainSeparator = keccak256(
abi.encode(EIP712_DOMAIN_TYPEHASH, keccak256("zkSync"), keccak256("2"), block.chainid)
);
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
}
/// @notice Encode hash of the legacy transaction type.
/// @return keccak256 of the serialized RLP encoded representation of transaction
function _encodeHashLegacyTransaction(Transaction calldata _transaction) private view returns (bytes32) {
// Hash of legacy transactions are encoded as one of the:
// - RLP(nonce, gasPrice, gasLimit, to, value, data, chainId, 0, 0)
// - RLP(nonce, gasPrice, gasLimit, to, value, data)
//
// In this RLP encoding, only the first one above list appears, so we encode each element
// inside list and then concatenate the length of all elements with them.
bytes memory encodedNonce = RLPEncoder.encodeUint256(_transaction.nonce);
// Encode `gasPrice` and `gasLimit` together to prevent "stack too deep error".
bytes memory encodedGasParam;
{
bytes memory encodedGasPrice = RLPEncoder.encodeUint256(_transaction.maxFeePerGas);
bytes memory encodedGasLimit = RLPEncoder.encodeUint256(_transaction.gasLimit);
encodedGasParam = bytes.concat(encodedGasPrice, encodedGasLimit);
}
bytes memory encodedTo = RLPEncoder.encodeAddress(address(uint160(_transaction.to)));
bytes memory encodedValue = RLPEncoder.encodeUint256(_transaction.value);
// Encode only the length of the transaction data, and not the data itself,
// so as not to copy to memory a potentially huge transaction data twice.
bytes memory encodedDataLength;
{
// Safe cast, because the length of the transaction data can't be so large.
uint64 txDataLen = uint64(_transaction.data.length);
if (txDataLen != 1) {
// If the length is not equal to one, then only using the length can it be encoded definitely.
encodedDataLength = RLPEncoder.encodeNonSingleBytesLen(txDataLen);
} else if (_transaction.data[0] >= 0x80) {
// If input is a byte in [0x80, 0xff] range, RLP encoding will concatenates 0x81 with the byte.
encodedDataLength = hex"81";
}
// Otherwise the length is not encoded at all.
}
// Encode `chainId` according to EIP-155, but only if the `chainId` is specified in the transaction.
bytes memory encodedChainId;
if (_transaction.reserved[0] != 0) {
encodedChainId = bytes.concat(RLPEncoder.encodeUint256(block.chainid), hex"80_80");
}
bytes memory encodedListLength;
unchecked {
uint256 listLength = encodedNonce.length +
encodedGasParam.length +
encodedTo.length +
encodedValue.length +
encodedDataLength.length +
_transaction.data.length +
encodedChainId.length;
// Safe cast, because the length of the list can't be so large.
encodedListLength = RLPEncoder.encodeListLen(uint64(listLength));
}
return
keccak256(
bytes.concat(
encodedListLength,
encodedNonce,
encodedGasParam,
encodedTo,
encodedValue,
encodedDataLength,
_transaction.data,
encodedChainId
)
);
}
/// @notice Encode hash of the EIP2930 transaction type.
/// @return keccak256 of the serialized RLP encoded representation of transaction
function _encodeHashEIP2930Transaction(Transaction calldata _transaction) private view returns (bytes32) {
// Hash of EIP2930 transactions is encoded the following way:
// H(0x01 || RLP(chain_id, nonce, gas_price, gas_limit, destination, amount, data, access_list))
//
// Note, that on zkSync access lists are not supported and should always be empty.
// Encode all fixed-length params to avoid "stack too deep error"
bytes memory encodedFixedLengthParams;
{
bytes memory encodedChainId = RLPEncoder.encodeUint256(block.chainid);
bytes memory encodedNonce = RLPEncoder.encodeUint256(_transaction.nonce);
bytes memory encodedGasPrice = RLPEncoder.encodeUint256(_transaction.maxFeePerGas);
bytes memory encodedGasLimit = RLPEncoder.encodeUint256(_transaction.gasLimit);
bytes memory encodedTo = RLPEncoder.encodeAddress(address(uint160(_transaction.to)));
bytes memory encodedValue = RLPEncoder.encodeUint256(_transaction.value);
encodedFixedLengthParams = bytes.concat(
encodedChainId,
encodedNonce,
encodedGasPrice,
encodedGasLimit,
encodedTo,
encodedValue
);
}
// Encode only the length of the transaction data, and not the data itself,
// so as not to copy to memory a potentially huge transaction data twice.
bytes memory encodedDataLength;
{
// Safe cast, because the length of the transaction data can't be so large.
uint64 txDataLen = uint64(_transaction.data.length);
if (txDataLen != 1) {
// If the length is not equal to one, then only using the length can it be encoded definitely.
encodedDataLength = RLPEncoder.encodeNonSingleBytesLen(txDataLen);
} else if (_transaction.data[0] >= 0x80) {
// If input is a byte in [0x80, 0xff] range, RLP encoding will concatenates 0x81 with the byte.
encodedDataLength = hex"81";
}
// Otherwise the length is not encoded at all.
}
// On zkSync, access lists are always zero length (at least for now).
bytes memory encodedAccessListLength = RLPEncoder.encodeListLen(0);
bytes memory encodedListLength;
unchecked {
uint256 listLength = encodedFixedLengthParams.length +
encodedDataLength.length +
_transaction.data.length +
encodedAccessListLength.length;
// Safe cast, because the length of the list can't be so large.
encodedListLength = RLPEncoder.encodeListLen(uint64(listLength));
}
return
keccak256(
bytes.concat(
"\x01",
encodedListLength,
encodedFixedLengthParams,
encodedDataLength,
_transaction.data,
encodedAccessListLength
)
);
}
/// @notice Encode hash of the EIP1559 transaction type.
/// @return keccak256 of the serialized RLP encoded representation of transaction
function _encodeHashEIP1559Transaction(Transaction calldata _transaction) private view returns (bytes32) {
// Hash of EIP1559 transactions is encoded the following way:
// H(0x02 || RLP(chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, destination, amount, data, access_list))
//
// Note, that on zkSync access lists are not supported and should always be empty.
// Encode all fixed-length params to avoid "stack too deep error"
bytes memory encodedFixedLengthParams;
{
bytes memory encodedChainId = RLPEncoder.encodeUint256(block.chainid);
bytes memory encodedNonce = RLPEncoder.encodeUint256(_transaction.nonce);
bytes memory encodedMaxPriorityFeePerGas = RLPEncoder.encodeUint256(_transaction.maxPriorityFeePerGas);
bytes memory encodedMaxFeePerGas = RLPEncoder.encodeUint256(_transaction.maxFeePerGas);
bytes memory encodedGasLimit = RLPEncoder.encodeUint256(_transaction.gasLimit);
bytes memory encodedTo = RLPEncoder.encodeAddress(address(uint160(_transaction.to)));
bytes memory encodedValue = RLPEncoder.encodeUint256(_transaction.value);
encodedFixedLengthParams = bytes.concat(
encodedChainId,
encodedNonce,
encodedMaxPriorityFeePerGas,
encodedMaxFeePerGas,
encodedGasLimit,
encodedTo,
encodedValue
);
}
// Encode only the length of the transaction data, and not the data itself,
// so as not to copy to memory a potentially huge transaction data twice.
bytes memory encodedDataLength;
{
// Safe cast, because the length of the transaction data can't be so large.
uint64 txDataLen = uint64(_transaction.data.length);
if (txDataLen != 1) {
// If the length is not equal to one, then only using the length can it be encoded definitely.
encodedDataLength = RLPEncoder.encodeNonSingleBytesLen(txDataLen);
} else if (_transaction.data[0] >= 0x80) {
// If input is a byte in [0x80, 0xff] range, RLP encoding will concatenates 0x81 with the byte.
encodedDataLength = hex"81";
}
// Otherwise the length is not encoded at all.
}
// On zkSync, access lists are always zero length (at least for now).
bytes memory encodedAccessListLength = RLPEncoder.encodeListLen(0);
bytes memory encodedListLength;
unchecked {
uint256 listLength = encodedFixedLengthParams.length +
encodedDataLength.length +
_transaction.data.length +
encodedAccessListLength.length;
// Safe cast, because the length of the list can't be so large.
encodedListLength = RLPEncoder.encodeListLen(uint64(listLength));
}
return
keccak256(
bytes.concat(
"\x02",
encodedListLength,
encodedFixedLengthParams,
encodedDataLength,
_transaction.data,
encodedAccessListLength
)
);
}
/// @notice Processes the common paymaster flows, e.g. setting proper allowance
/// for tokens, etc. For more information on the expected behavior, check out
/// the "Paymaster flows" section in the documentation.
function processPaymasterInput(Transaction calldata _transaction) internal {
require(_transaction.paymasterInput.length >= 4, "The standard paymaster input must be at least 4 bytes long");
bytes4 paymasterInputSelector = bytes4(_transaction.paymasterInput[0:4]);
if (paymasterInputSelector == IPaymasterFlow.approvalBased.selector) {
require(
_transaction.paymasterInput.length >= 68,
"The approvalBased paymaster input must be at least 68 bytes long"
);
// While the actual data consists of address, uint256 and bytes data,
// the data is needed only for the paymaster, so we ignore it here for the sake of optimization
(address token, uint256 minAllowance) = abi.decode(_transaction.paymasterInput[4:68], (address, uint256));
address paymaster = address(uint160(_transaction.paymaster));
uint256 currentAllowance = IERC20(token).allowance(address(this), paymaster);
if (currentAllowance < minAllowance) {
// Some tokens, e.g. USDT require that the allowance is firsty set to zero
// and only then updated to the new value.
IERC20(token).safeApprove(paymaster, 0);
IERC20(token).safeApprove(paymaster, minAllowance);
}
} else if (paymasterInputSelector == IPaymasterFlow.general.selector) {
// Do nothing. general(bytes) paymaster flow means that the paymaster must interpret these bytes on his own.
} else {
revert("Unsupported paymaster flow");
}
}
/// @notice Pays the required fee for the transaction to the bootloader.
/// @dev Currently it pays the maximum amount "_transaction.maxFeePerGas * _transaction.gasLimit",
/// it will change in the future.
function payToTheBootloader(Transaction calldata _transaction) internal returns (bool success) {
address bootloaderAddr = BOOTLOADER_FORMAL_ADDRESS;
uint256 amount = _transaction.maxFeePerGas * _transaction.gasLimit;
assembly {
success := call(gas(), bootloaderAddr, amount, 0, 0, 0, 0)
}
}
// Returns the balance required to process the transaction.
function totalRequiredBalance(Transaction calldata _transaction) internal pure returns (uint256 requiredBalance) {
if (address(uint160(_transaction.paymaster)) != address(0)) {
// Paymaster pays for the fee
requiredBalance = _transaction.value;
} else {
// The user should have enough balance for both the fee and the value of the transaction
requiredBalance = _transaction.maxFeePerGas * _transaction.gasLimit + _transaction.value;
}
}
}