-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
utils.ts
150 lines (138 loc) · 5.63 KB
/
utils.ts
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
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { HexString } from 'web3-types';
import { bytesToHex } from 'web3-utils';
import { setLengthLeft, toUint8Array } from '../common/utils.js';
import type { AccessList, AccessListUint8Array, AccessListItem } from './types.js';
import { isAccessList } from './types.js';
import type { Common } from '../common/common.js';
export const checkMaxInitCodeSize = (common: Common, length: number) => {
const maxInitCodeSize = common.param('vm', 'maxInitCodeSize');
if (maxInitCodeSize && BigInt(length) > maxInitCodeSize) {
throw new Error(
`the initcode size of this transaction is too large: it is ${length} while the max is ${common.param(
'vm',
'maxInitCodeSize',
)}`,
);
}
};
export const getAccessListData = (accessList: AccessListUint8Array | AccessList) => {
let AccessListJSON;
let uint8arrayAccessList;
if (isAccessList(accessList)) {
AccessListJSON = accessList;
const newAccessList: AccessListUint8Array = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < accessList.length; i += 1) {
const item: AccessListItem = accessList[i];
const addressBytes = toUint8Array(item.address);
const storageItems: Uint8Array[] = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let index = 0; index < item.storageKeys.length; index += 1) {
storageItems.push(toUint8Array(item.storageKeys[index]));
}
newAccessList.push([addressBytes, storageItems]);
}
uint8arrayAccessList = newAccessList;
} else {
uint8arrayAccessList = accessList ?? [];
// build the JSON
const json: AccessList = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < uint8arrayAccessList.length; i += 1) {
const data = uint8arrayAccessList[i];
const address = bytesToHex(data[0]);
const storageKeys: string[] = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let item = 0; item < data[1].length; item += 1) {
storageKeys.push(bytesToHex(data[1][item]));
}
const jsonItem: AccessListItem = {
address,
storageKeys,
};
json.push(jsonItem);
}
AccessListJSON = json;
}
return {
AccessListJSON,
accessList: uint8arrayAccessList,
};
};
export const verifyAccessList = (accessList: AccessListUint8Array) => {
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let key = 0; key < accessList.length; key += 1) {
const accessListItem = accessList[key];
const address = accessListItem[0];
const storageSlots = accessListItem[1];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/consistent-type-assertions
if ((<any>accessListItem)[2] !== undefined) {
throw new Error(
'Access list item cannot have 3 elements. It can only have an address, and an array of storage slots.',
);
}
if (address.length !== 20) {
throw new Error('Invalid EIP-2930 transaction: address length should be 20 bytes');
}
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let storageSlot = 0; storageSlot < storageSlots.length; storageSlot += 1) {
if (storageSlots[storageSlot].length !== 32) {
throw new Error(
'Invalid EIP-2930 transaction: storage slot length should be 32 bytes',
);
}
}
}
};
export const getAccessListJSON = (
accessList: AccessListUint8Array,
): {
address: HexString;
storageKeys: HexString[];
}[] => {
const accessListJSON: { address: HexString; storageKeys: HexString[] }[] = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let index = 0; index < accessList.length; index += 1) {
const item: any = accessList[index];
const JSONItem: { address: HexString; storageKeys: HexString[] } = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/consistent-type-assertions
address: bytesToHex(setLengthLeft(<Uint8Array>item[0], 20)),
storageKeys: [],
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/prefer-optional-chain
const storageSlots: Uint8Array[] = item && item[1];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let slot = 0; slot < storageSlots.length; slot += 1) {
const storageSlot = storageSlots[slot];
JSONItem.storageKeys.push(bytesToHex(setLengthLeft(storageSlot, 32)));
}
accessListJSON.push(JSONItem);
}
return accessListJSON;
};
export const getDataFeeEIP2930 = (accessList: AccessListUint8Array, common: Common): number => {
const accessListStorageKeyCost = common.param('gasPrices', 'accessListStorageKeyCost');
const accessListAddressCost = common.param('gasPrices', 'accessListAddressCost');
let slots = 0;
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let index = 0; index < accessList.length; index += 1) {
const item = accessList[index];
const storageSlots = item[1];
slots += storageSlots.length;
}
const addresses = accessList.length;
return addresses * Number(accessListAddressCost) + slots * Number(accessListStorageKeyCost);
};