web-eth-abi 4.3.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +14 -0
- package/README.md +59 -0
- package/lib/commonjs/api/errors_api.d.ts +5 -0
- package/lib/commonjs/api/errors_api.js +44 -0
- package/lib/commonjs/api/errors_api.js.map +1 -0
- package/lib/commonjs/api/events_api.d.ts +51 -0
- package/lib/commonjs/api/events_api.js +90 -0
- package/lib/commonjs/api/events_api.js.map +1 -0
- package/lib/commonjs/api/functions_api.d.ts +92 -0
- package/lib/commonjs/api/functions_api.js +139 -0
- package/lib/commonjs/api/functions_api.js.map +1 -0
- package/lib/commonjs/api/logs_api.d.ts +49 -0
- package/lib/commonjs/api/logs_api.js +107 -0
- package/lib/commonjs/api/logs_api.js.map +1 -0
- package/lib/commonjs/api/parameters_api.d.ts +238 -0
- package/lib/commonjs/api/parameters_api.js +280 -0
- package/lib/commonjs/api/parameters_api.js.map +1 -0
- package/lib/commonjs/coders/base/address.d.ts +4 -0
- package/lib/commonjs/coders/base/address.js +75 -0
- package/lib/commonjs/coders/base/address.js.map +1 -0
- package/lib/commonjs/coders/base/array.d.ts +4 -0
- package/lib/commonjs/coders/base/array.js +106 -0
- package/lib/commonjs/coders/base/array.js.map +1 -0
- package/lib/commonjs/coders/base/bool.d.ts +4 -0
- package/lib/commonjs/coders/base/bool.js +56 -0
- package/lib/commonjs/coders/base/bool.js.map +1 -0
- package/lib/commonjs/coders/base/bytes.d.ts +4 -0
- package/lib/commonjs/coders/base/bytes.js +101 -0
- package/lib/commonjs/coders/base/bytes.js.map +1 -0
- package/lib/commonjs/coders/base/index.d.ts +11 -0
- package/lib/commonjs/coders/base/index.js +109 -0
- package/lib/commonjs/coders/base/index.js.map +1 -0
- package/lib/commonjs/coders/base/number.d.ts +4 -0
- package/lib/commonjs/coders/base/number.js +116 -0
- package/lib/commonjs/coders/base/number.js.map +1 -0
- package/lib/commonjs/coders/base/numbersLimits.d.ts +4 -0
- package/lib/commonjs/coders/base/numbersLimits.js +40 -0
- package/lib/commonjs/coders/base/numbersLimits.js.map +1 -0
- package/lib/commonjs/coders/base/string.d.ts +4 -0
- package/lib/commonjs/coders/base/string.js +40 -0
- package/lib/commonjs/coders/base/string.js.map +1 -0
- package/lib/commonjs/coders/base/tuple.d.ts +7 -0
- package/lib/commonjs/coders/base/tuple.js +123 -0
- package/lib/commonjs/coders/base/tuple.js.map +1 -0
- package/lib/commonjs/coders/base/utils.d.ts +2 -0
- package/lib/commonjs/coders/base/utils.js +50 -0
- package/lib/commonjs/coders/base/utils.js.map +1 -0
- package/lib/commonjs/coders/decode.d.ts +5 -0
- package/lib/commonjs/coders/decode.js +29 -0
- package/lib/commonjs/coders/decode.js.map +1 -0
- package/lib/commonjs/coders/encode.d.ts +38 -0
- package/lib/commonjs/coders/encode.js +113 -0
- package/lib/commonjs/coders/encode.js.map +1 -0
- package/lib/commonjs/coders/types.d.ts +16 -0
- package/lib/commonjs/coders/types.js +19 -0
- package/lib/commonjs/coders/types.js.map +1 -0
- package/lib/commonjs/coders/utils.d.ts +23 -0
- package/lib/commonjs/coders/utils.js +115 -0
- package/lib/commonjs/coders/utils.js.map +1 -0
- package/lib/commonjs/decode_contract_error_data.d.ts +3 -0
- package/lib/commonjs/decode_contract_error_data.js +73 -0
- package/lib/commonjs/decode_contract_error_data.js.map +1 -0
- package/lib/commonjs/eip_712.d.ts +50 -0
- package/lib/commonjs/eip_712.js +144 -0
- package/lib/commonjs/eip_712.js.map +1 -0
- package/lib/commonjs/index.d.ts +8 -0
- package/lib/commonjs/index.js +43 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/utils.d.ts +43 -0
- package/lib/commonjs/utils.js +216 -0
- package/lib/commonjs/utils.js.map +1 -0
- package/lib/esm/api/errors_api.js +40 -0
- package/lib/esm/api/errors_api.js.map +1 -0
- package/lib/esm/api/events_api.js +86 -0
- package/lib/esm/api/events_api.js.map +1 -0
- package/lib/esm/api/functions_api.js +134 -0
- package/lib/esm/api/functions_api.js.map +1 -0
- package/lib/esm/api/logs_api.js +103 -0
- package/lib/esm/api/logs_api.js.map +1 -0
- package/lib/esm/api/parameters_api.js +271 -0
- package/lib/esm/api/parameters_api.js.map +1 -0
- package/lib/esm/coders/base/address.js +70 -0
- package/lib/esm/coders/base/address.js.map +1 -0
- package/lib/esm/coders/base/array.js +101 -0
- package/lib/esm/coders/base/array.js.map +1 -0
- package/lib/esm/coders/base/bool.js +51 -0
- package/lib/esm/coders/base/bool.js.map +1 -0
- package/lib/esm/coders/base/bytes.js +96 -0
- package/lib/esm/coders/base/bytes.js.map +1 -0
- package/lib/esm/coders/base/index.js +90 -0
- package/lib/esm/coders/base/index.js.map +1 -0
- package/lib/esm/coders/base/number.js +111 -0
- package/lib/esm/coders/base/number.js.map +1 -0
- package/lib/esm/coders/base/numbersLimits.js +37 -0
- package/lib/esm/coders/base/numbersLimits.js.map +1 -0
- package/lib/esm/coders/base/string.js +35 -0
- package/lib/esm/coders/base/string.js.map +1 -0
- package/lib/esm/coders/base/tuple.js +118 -0
- package/lib/esm/coders/base/tuple.js.map +1 -0
- package/lib/esm/coders/base/utils.js +46 -0
- package/lib/esm/coders/base/utils.js.map +1 -0
- package/lib/esm/coders/decode.js +25 -0
- package/lib/esm/coders/decode.js.map +1 -0
- package/lib/esm/coders/encode.js +108 -0
- package/lib/esm/coders/encode.js.map +1 -0
- package/lib/esm/coders/types.js +18 -0
- package/lib/esm/coders/types.js.map +1 -0
- package/lib/esm/coders/utils.js +105 -0
- package/lib/esm/coders/utils.js.map +1 -0
- package/lib/esm/decode_contract_error_data.js +69 -0
- package/lib/esm/decode_contract_error_data.js.map +1 -0
- package/lib/esm/eip_712.js +140 -0
- package/lib/esm/eip_712.js.map +1 -0
- package/lib/esm/index.js +25 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/utils.js +199 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/types/api/errors_api.d.ts +6 -0
- package/lib/types/api/errors_api.d.ts.map +1 -0
- package/lib/types/api/events_api.d.ts +52 -0
- package/lib/types/api/events_api.d.ts.map +1 -0
- package/lib/types/api/functions_api.d.ts +93 -0
- package/lib/types/api/functions_api.d.ts.map +1 -0
- package/lib/types/api/logs_api.d.ts +50 -0
- package/lib/types/api/logs_api.d.ts.map +1 -0
- package/lib/types/api/parameters_api.d.ts +239 -0
- package/lib/types/api/parameters_api.d.ts.map +1 -0
- package/lib/types/coders/base/address.d.ts +5 -0
- package/lib/types/coders/base/address.d.ts.map +1 -0
- package/lib/types/coders/base/array.d.ts +5 -0
- package/lib/types/coders/base/array.d.ts.map +1 -0
- package/lib/types/coders/base/bool.d.ts +5 -0
- package/lib/types/coders/base/bool.d.ts.map +1 -0
- package/lib/types/coders/base/bytes.d.ts +5 -0
- package/lib/types/coders/base/bytes.d.ts.map +1 -0
- package/lib/types/coders/base/index.d.ts +12 -0
- package/lib/types/coders/base/index.d.ts.map +1 -0
- package/lib/types/coders/base/number.d.ts +5 -0
- package/lib/types/coders/base/number.d.ts.map +1 -0
- package/lib/types/coders/base/numbersLimits.d.ts +5 -0
- package/lib/types/coders/base/numbersLimits.d.ts.map +1 -0
- package/lib/types/coders/base/string.d.ts +5 -0
- package/lib/types/coders/base/string.d.ts.map +1 -0
- package/lib/types/coders/base/tuple.d.ts +8 -0
- package/lib/types/coders/base/tuple.d.ts.map +1 -0
- package/lib/types/coders/base/utils.d.ts +3 -0
- package/lib/types/coders/base/utils.d.ts.map +1 -0
- package/lib/types/coders/decode.d.ts +6 -0
- package/lib/types/coders/decode.d.ts.map +1 -0
- package/lib/types/coders/encode.d.ts +39 -0
- package/lib/types/coders/encode.d.ts.map +1 -0
- package/lib/types/coders/types.d.ts +17 -0
- package/lib/types/coders/types.d.ts.map +1 -0
- package/lib/types/coders/utils.d.ts +24 -0
- package/lib/types/coders/utils.d.ts.map +1 -0
- package/lib/types/decode_contract_error_data.d.ts +4 -0
- package/lib/types/decode_contract_error_data.d.ts.map +1 -0
- package/lib/types/eip_712.d.ts +51 -0
- package/lib/types/eip_712.d.ts.map +1 -0
- package/lib/types/index.d.ts +9 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/utils.d.ts +44 -0
- package/lib/types/utils.d.ts.map +1 -0
- package/package.json +56 -0
- package/src/api/errors_api.ts +45 -0
- package/src/api/events_api.ts +91 -0
- package/src/api/functions_api.ts +145 -0
- package/src/api/logs_api.ts +131 -0
- package/src/api/parameters_api.ts +287 -0
- package/src/coders/base/address.ts +75 -0
- package/src/coders/base/array.ts +120 -0
- package/src/coders/base/bool.ts +54 -0
- package/src/coders/base/bytes.ts +106 -0
- package/src/coders/base/index.ts +95 -0
- package/src/coders/base/number.ts +116 -0
- package/src/coders/base/numbersLimits.ts +39 -0
- package/src/coders/base/string.ts +38 -0
- package/src/coders/base/tuple.ts +130 -0
- package/src/coders/base/utils.ts +51 -0
- package/src/coders/decode.ts +32 -0
- package/src/coders/encode.ts +114 -0
- package/src/coders/types.ts +39 -0
- package/src/coders/utils.ts +128 -0
- package/src/decode_contract_error_data.ts +80 -0
- package/src/eip_712.ts +252 -0
- package/src/index.ts +25 -0
- package/src/utils.ts +266 -0
- package/wimho03d.cjs +1 -0
@@ -0,0 +1,287 @@
|
|
1
|
+
/*
|
2
|
+
This file is part of web3.js.
|
3
|
+
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU Lesser General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
|
18
|
+
/**
|
19
|
+
*
|
20
|
+
* @module ABI
|
21
|
+
*/
|
22
|
+
|
23
|
+
import { AbiError } from 'web3-errors';
|
24
|
+
import { AbiInput, HexString } from 'web3-types';
|
25
|
+
import { decodeParameters as decodeParametersInternal } from '../coders/decode.js';
|
26
|
+
import { encodeParameters } from '../coders/encode.js';
|
27
|
+
|
28
|
+
export { encodeParameters, inferTypesAndEncodeParameters } from '../coders/encode.js';
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Encodes a parameter based on its type to its ABI representation.
|
32
|
+
* @param abi - The type of the parameter. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
|
33
|
+
* @param param - The actual parameter to encode.
|
34
|
+
* @returns - The ABI encoded parameter
|
35
|
+
* @example
|
36
|
+
* ```ts
|
37
|
+
* const res = web3.eth.abi.encodeParameter("uint256", "2345675643");
|
38
|
+
* console.log(res);
|
39
|
+
* 0x000000000000000000000000000000000000000000000000000000008bd02b7b
|
40
|
+
*
|
41
|
+
* const res = web3.eth.abi.encodeParameter("uint", "2345675643");
|
42
|
+
*
|
43
|
+
* console.log(res);
|
44
|
+
* >0x000000000000000000000000000000000000000000000000000000008bd02b7b
|
45
|
+
*
|
46
|
+
* const res = web3.eth.abi.encodeParameter("bytes32", "0xdf3234");
|
47
|
+
*
|
48
|
+
* console.log(res);
|
49
|
+
* >0xdf32340000000000000000000000000000000000000000000000000000000000
|
50
|
+
*
|
51
|
+
* const res = web3.eth.abi.encodeParameter("bytes", "0xdf3234");
|
52
|
+
*
|
53
|
+
* console.log(res);
|
54
|
+
* > 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000
|
55
|
+
*
|
56
|
+
* const res = web3.eth.abi.encodeParameter("bytes32[]", ["0xdf3234", "0xfdfd"]);
|
57
|
+
*
|
58
|
+
* console.log(res);
|
59
|
+
* > 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000
|
60
|
+
*
|
61
|
+
* const res = web3.eth.abi.encodeParameter(
|
62
|
+
* {
|
63
|
+
* ParentStruct: {
|
64
|
+
* propertyOne: "uint256",
|
65
|
+
* propertyTwo: "uint256",
|
66
|
+
* childStruct: {
|
67
|
+
* propertyOne: "uint256",
|
68
|
+
* propertyTwo: "uint256",
|
69
|
+
* },
|
70
|
+
* },
|
71
|
+
* },
|
72
|
+
* {
|
73
|
+
* propertyOne: 42,
|
74
|
+
* propertyTwo: 56,
|
75
|
+
* childStruct: {
|
76
|
+
* propertyOne: 45,
|
77
|
+
* propertyTwo: 78,
|
78
|
+
* },
|
79
|
+
* }
|
80
|
+
* );
|
81
|
+
*
|
82
|
+
* console.log(res);
|
83
|
+
* > 0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e
|
84
|
+
* ```
|
85
|
+
*/
|
86
|
+
export const encodeParameter = (abi: AbiInput, param: unknown): string =>
|
87
|
+
encodeParameters([abi], [param]);
|
88
|
+
/**
|
89
|
+
* Should be used to decode list of params
|
90
|
+
*/
|
91
|
+
export const decodeParametersWith = (
|
92
|
+
abis: AbiInput[] | ReadonlyArray<AbiInput>,
|
93
|
+
bytes: HexString,
|
94
|
+
loose: boolean,
|
95
|
+
): { [key: string]: unknown; __length__: number } => {
|
96
|
+
try {
|
97
|
+
if (abis.length > 0 && (!bytes || bytes === '0x' || bytes === '0X')) {
|
98
|
+
throw new AbiError(
|
99
|
+
"Returned values aren't valid, did it run Out of Gas? " +
|
100
|
+
'You might also see this error if you are not using the ' +
|
101
|
+
'correct ABI for the contract you are retrieving data from, ' +
|
102
|
+
'requesting data from a block number that does not exist, ' +
|
103
|
+
'or querying a node which is not fully synced.',
|
104
|
+
);
|
105
|
+
}
|
106
|
+
return decodeParametersInternal(abis, `0x${bytes.replace(/0x/i, '')}`, loose);
|
107
|
+
} catch (err) {
|
108
|
+
throw new AbiError(`Parameter decoding error: ${(err as Error).message}`, {
|
109
|
+
internalErr: err,
|
110
|
+
});
|
111
|
+
}
|
112
|
+
};
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Should be used to decode list of params
|
116
|
+
*/
|
117
|
+
/**
|
118
|
+
* Decodes ABI encoded parameters to its JavaScript types.
|
119
|
+
* @param abi - An array of {@link AbiInput}. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
|
120
|
+
* @param bytes - The ABI byte code to decode
|
121
|
+
* @returns - The result object containing the decoded parameters.
|
122
|
+
* @example
|
123
|
+
* ```ts
|
124
|
+
* let res = web3.eth.abi.decodeParameters(
|
125
|
+
* ["string", "uint256"],
|
126
|
+
* "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
|
127
|
+
* );
|
128
|
+
* console.log(res);
|
129
|
+
* > { '0': 'Hello!%!', '1': 234n, __length__: 2 }
|
130
|
+
*
|
131
|
+
* let res = web3.eth.abi.decodeParameters(
|
132
|
+
* [
|
133
|
+
* {
|
134
|
+
* type: "string",
|
135
|
+
* name: "myString",
|
136
|
+
* },
|
137
|
+
* {
|
138
|
+
* type: "uint256",
|
139
|
+
* name: "myNumber",
|
140
|
+
* },
|
141
|
+
* ],
|
142
|
+
* "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
|
143
|
+
* );
|
144
|
+
* console.log(res);
|
145
|
+
* > {
|
146
|
+
* '0': 'Hello!%!',
|
147
|
+
* '1': 234n,
|
148
|
+
* __length__: 2,
|
149
|
+
* myString: 'Hello!%!',
|
150
|
+
* myNumber: 234n
|
151
|
+
* }
|
152
|
+
*
|
153
|
+
* const res = web3.eth.abi.decodeParameters(
|
154
|
+
* [
|
155
|
+
* "uint8[]",
|
156
|
+
* {
|
157
|
+
* ParentStruct: {
|
158
|
+
* propertyOne: "uint256",
|
159
|
+
* propertyTwo: "uint256",
|
160
|
+
* childStruct: {
|
161
|
+
* propertyOne: "uint256",
|
162
|
+
* propertyTwo: "uint256",
|
163
|
+
* },
|
164
|
+
* },
|
165
|
+
* },
|
166
|
+
* ],
|
167
|
+
* "0x00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000018"
|
168
|
+
* );
|
169
|
+
* console.log(res);
|
170
|
+
* >
|
171
|
+
* '0': [ 42n, 24n ],
|
172
|
+
* '1': {
|
173
|
+
* '0': 42n,
|
174
|
+
* '1': 56n,
|
175
|
+
* '2': {
|
176
|
+
* '0': 45n,
|
177
|
+
* '1': 78n,
|
178
|
+
* __length__: 2,
|
179
|
+
* propertyOne: 45n,
|
180
|
+
* propertyTwo: 78n
|
181
|
+
* },
|
182
|
+
* __length__: 3,
|
183
|
+
* propertyOne: 42n,
|
184
|
+
* propertyTwo: 56n,
|
185
|
+
* childStruct: {
|
186
|
+
* '0': 45n,
|
187
|
+
* '1': 78n,
|
188
|
+
* __length__: 2,
|
189
|
+
* propertyOne: 45n,
|
190
|
+
* propertyTwo: 78n
|
191
|
+
* }
|
192
|
+
* },
|
193
|
+
* __length__: 2,
|
194
|
+
* ParentStruct: {
|
195
|
+
* '0': 42n,
|
196
|
+
* '1': 56n,
|
197
|
+
* '2': {
|
198
|
+
* '0': 45n,
|
199
|
+
* '1': 78n,
|
200
|
+
* __length__: 2,
|
201
|
+
* propertyOne: 45n,
|
202
|
+
* propertyTwo: 78n
|
203
|
+
* },
|
204
|
+
* __length__: 3,
|
205
|
+
* propertyOne: 42n,
|
206
|
+
* propertyTwo: 56n,
|
207
|
+
* childStruct: {
|
208
|
+
* '0': 45n,
|
209
|
+
* '1': 78n,
|
210
|
+
* __length__: 2,
|
211
|
+
* propertyOne: 45n,
|
212
|
+
* propertyTwo: 78n
|
213
|
+
* }
|
214
|
+
* }
|
215
|
+
*}
|
216
|
+
* ```
|
217
|
+
*/
|
218
|
+
export const decodeParameters = (
|
219
|
+
abi: AbiInput[] | ReadonlyArray<AbiInput>,
|
220
|
+
bytes: HexString,
|
221
|
+
): { [key: string]: unknown; __length__: number } => decodeParametersWith(abi, bytes, false);
|
222
|
+
|
223
|
+
/**
|
224
|
+
* Should be used to decode bytes to plain param
|
225
|
+
*/
|
226
|
+
/**
|
227
|
+
* Decodes an ABI encoded parameter to its JavaScript type.
|
228
|
+
* @param abi - The type of the parameter. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
|
229
|
+
* @param bytes - The ABI byte code to decode
|
230
|
+
* @returns - The decoded parameter
|
231
|
+
* @example
|
232
|
+
* ```ts
|
233
|
+
* const res = web3.eth.abi.decodeParameter(
|
234
|
+
* "uint256",
|
235
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000010"
|
236
|
+
* );
|
237
|
+
* console.log(res);
|
238
|
+
* > 16n
|
239
|
+
*
|
240
|
+
* const res = web3.eth.abi.decodeParameter(
|
241
|
+
* "string",
|
242
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
|
243
|
+
* );
|
244
|
+
*
|
245
|
+
* console.log(res);
|
246
|
+
* > Hello!%!
|
247
|
+
*
|
248
|
+
* const res = web3.eth.abi.decodeParameter(
|
249
|
+
* {
|
250
|
+
* ParentStruct: {
|
251
|
+
* propertyOne: "uint256",
|
252
|
+
* propertyTwo: "uint256",
|
253
|
+
* childStruct: {
|
254
|
+
* propertyOne: "uint256",
|
255
|
+
* propertyTwo: "uint256",
|
256
|
+
* },
|
257
|
+
* },
|
258
|
+
* },
|
259
|
+
* "0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e"
|
260
|
+
* );
|
261
|
+
*
|
262
|
+
* console.log(res);
|
263
|
+
* {
|
264
|
+
* '0': 42n,
|
265
|
+
* '1': 56n,
|
266
|
+
* '2': {
|
267
|
+
* '0': 45n,
|
268
|
+
* '1': 78n,
|
269
|
+
* __length__: 2,
|
270
|
+
* propertyOne: 45n,
|
271
|
+
* propertyTwo: 78n
|
272
|
+
* },
|
273
|
+
* __length__: 3,
|
274
|
+
* propertyOne: 42n,
|
275
|
+
* propertyTwo: 56n,
|
276
|
+
* childStruct: {
|
277
|
+
* '0': 45n,
|
278
|
+
* '1': 78n,
|
279
|
+
* __length__: 2,
|
280
|
+
* propertyOne: 45n,
|
281
|
+
* propertyTwo: 78n
|
282
|
+
* }
|
283
|
+
*}
|
284
|
+
* ```
|
285
|
+
*/
|
286
|
+
export const decodeParameter = (abi: AbiInput, bytes: HexString): unknown =>
|
287
|
+
decodeParameters([abi], bytes)['0'];
|
@@ -0,0 +1,75 @@
|
|
1
|
+
/*
|
2
|
+
This file is part of web3.js.
|
3
|
+
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU Lesser General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
import { AbiError } from 'web3-errors';
|
18
|
+
import { AbiParameter } from 'web3-types';
|
19
|
+
import { toChecksumAddress } from 'web3-utils';
|
20
|
+
import { isAddress, utils } from 'web3-validator';
|
21
|
+
import { DecoderResult, EncoderResult } from '../types.js';
|
22
|
+
import { alloc, WORD_SIZE } from '../utils.js';
|
23
|
+
|
24
|
+
const ADDRESS_BYTES_COUNT = 20;
|
25
|
+
const ADDRESS_OFFSET = WORD_SIZE - ADDRESS_BYTES_COUNT;
|
26
|
+
|
27
|
+
export function encodeAddress(param: AbiParameter, input: unknown): EncoderResult {
|
28
|
+
if (typeof input !== 'string') {
|
29
|
+
throw new AbiError('address type expects string as input type', {
|
30
|
+
value: input,
|
31
|
+
name: param.name,
|
32
|
+
type: param.type,
|
33
|
+
});
|
34
|
+
}
|
35
|
+
let address = input.toLowerCase();
|
36
|
+
if (!address.startsWith('0x')) {
|
37
|
+
address = `0x${address}`;
|
38
|
+
}
|
39
|
+
if (!isAddress(address)) {
|
40
|
+
throw new AbiError('provided input is not valid address', {
|
41
|
+
value: input,
|
42
|
+
name: param.name,
|
43
|
+
type: param.type,
|
44
|
+
});
|
45
|
+
}
|
46
|
+
// for better performance, we could convert hex to destination bytes directly (encoded var)
|
47
|
+
const addressBytes = utils.hexToUint8Array(address);
|
48
|
+
// expand address to WORD_SIZE
|
49
|
+
const encoded = alloc(WORD_SIZE);
|
50
|
+
encoded.set(addressBytes, ADDRESS_OFFSET);
|
51
|
+
return {
|
52
|
+
dynamic: false,
|
53
|
+
encoded,
|
54
|
+
};
|
55
|
+
}
|
56
|
+
|
57
|
+
export function decodeAddress(_param: AbiParameter, bytes: Uint8Array): DecoderResult<string> {
|
58
|
+
const addressBytes = bytes.subarray(ADDRESS_OFFSET, WORD_SIZE);
|
59
|
+
if (addressBytes.length !== ADDRESS_BYTES_COUNT) {
|
60
|
+
throw new AbiError('Invalid decoding input, not enough bytes to decode address', { bytes });
|
61
|
+
}
|
62
|
+
const result = utils.uint8ArrayToHexString(addressBytes);
|
63
|
+
|
64
|
+
// should we check is decoded value is valid address?
|
65
|
+
// if(!isAddress(result)) {
|
66
|
+
// throw new AbiError("encoded data is not valid address", {
|
67
|
+
// address: result,
|
68
|
+
// });
|
69
|
+
// }
|
70
|
+
return {
|
71
|
+
result: toChecksumAddress(result),
|
72
|
+
encoded: bytes.subarray(WORD_SIZE),
|
73
|
+
consumed: WORD_SIZE,
|
74
|
+
};
|
75
|
+
}
|
@@ -0,0 +1,120 @@
|
|
1
|
+
/*
|
2
|
+
This file is part of web3.js.
|
3
|
+
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU Lesser General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
import { AbiError } from 'web3-errors';
|
18
|
+
import { AbiParameter } from 'web3-types';
|
19
|
+
import { uint8ArrayConcat } from 'web3-utils';
|
20
|
+
// eslint-disable-next-line import/no-cycle
|
21
|
+
import { decodeParamFromAbiParameter, encodeNumber, encodeParamFromAbiParameter } from './index.js';
|
22
|
+
import { DecoderResult, EncoderResult } from '../types.js';
|
23
|
+
import { extractArrayType, isDynamic, WORD_SIZE } from '../utils.js';
|
24
|
+
import { decodeNumber } from './number.js';
|
25
|
+
import { encodeDynamicParams } from './utils.js';
|
26
|
+
|
27
|
+
export function encodeArray(param: AbiParameter, values: unknown): EncoderResult {
|
28
|
+
if (!Array.isArray(values)) {
|
29
|
+
throw new AbiError('Expected value to be array', { abi: param, values });
|
30
|
+
}
|
31
|
+
const { size, param: arrayItemParam } = extractArrayType(param);
|
32
|
+
const encodedParams = values.map(v => encodeParamFromAbiParameter(arrayItemParam, v));
|
33
|
+
const dynamic = size === -1;
|
34
|
+
const dynamicItems = encodedParams.length > 0 && encodedParams[0].dynamic;
|
35
|
+
if (!dynamic && values.length !== size) {
|
36
|
+
throw new AbiError("Given arguments count doesn't match array length", {
|
37
|
+
arrayLength: size,
|
38
|
+
argumentsLength: values.length,
|
39
|
+
});
|
40
|
+
}
|
41
|
+
if (dynamic || dynamicItems) {
|
42
|
+
const encodingResult = encodeDynamicParams(encodedParams);
|
43
|
+
if (dynamic) {
|
44
|
+
const encodedLength = encodeNumber(
|
45
|
+
{ type: 'uint256', name: '' },
|
46
|
+
encodedParams.length,
|
47
|
+
).encoded;
|
48
|
+
return {
|
49
|
+
dynamic: true,
|
50
|
+
encoded:
|
51
|
+
encodedParams.length > 0
|
52
|
+
? uint8ArrayConcat(encodedLength, encodingResult)
|
53
|
+
: encodedLength,
|
54
|
+
};
|
55
|
+
}
|
56
|
+
return {
|
57
|
+
dynamic: true,
|
58
|
+
encoded: encodingResult,
|
59
|
+
};
|
60
|
+
}
|
61
|
+
|
62
|
+
return {
|
63
|
+
dynamic: false,
|
64
|
+
encoded: uint8ArrayConcat(...encodedParams.map(p => p.encoded)),
|
65
|
+
};
|
66
|
+
}
|
67
|
+
|
68
|
+
export function decodeArray(param: AbiParameter, bytes: Uint8Array): DecoderResult<unknown[]> {
|
69
|
+
// eslint-disable-next-line prefer-const
|
70
|
+
let { size, param: arrayItemParam } = extractArrayType(param);
|
71
|
+
const dynamic = size === -1;
|
72
|
+
|
73
|
+
let consumed = 0;
|
74
|
+
const result: unknown[] = [];
|
75
|
+
let remaining = bytes;
|
76
|
+
// dynamic array, we need to decode length
|
77
|
+
if (dynamic) {
|
78
|
+
const lengthResult = decodeNumber({ type: 'uint32', name: '' }, bytes);
|
79
|
+
size = Number(lengthResult.result);
|
80
|
+
consumed = lengthResult.consumed;
|
81
|
+
remaining = lengthResult.encoded;
|
82
|
+
}
|
83
|
+
const hasDynamicChild = isDynamic(arrayItemParam);
|
84
|
+
if (hasDynamicChild) {
|
85
|
+
// known length but dynamic child, each child is actually head element with encoded offset
|
86
|
+
for (let i = 0; i < size; i += 1) {
|
87
|
+
const offsetResult = decodeNumber(
|
88
|
+
{ type: 'uint32', name: '' },
|
89
|
+
remaining.subarray(i * WORD_SIZE),
|
90
|
+
);
|
91
|
+
consumed += offsetResult.consumed;
|
92
|
+
const decodedChildResult = decodeParamFromAbiParameter(
|
93
|
+
arrayItemParam,
|
94
|
+
remaining.subarray(Number(offsetResult.result)),
|
95
|
+
);
|
96
|
+
consumed += decodedChildResult.consumed;
|
97
|
+
result.push(decodedChildResult.result);
|
98
|
+
}
|
99
|
+
return {
|
100
|
+
result,
|
101
|
+
encoded: remaining.subarray(consumed),
|
102
|
+
consumed,
|
103
|
+
};
|
104
|
+
}
|
105
|
+
|
106
|
+
for (let i = 0; i < size; i += 1) {
|
107
|
+
// decode static params
|
108
|
+
const decodedChildResult = decodeParamFromAbiParameter(
|
109
|
+
arrayItemParam,
|
110
|
+
bytes.subarray(consumed),
|
111
|
+
);
|
112
|
+
consumed += decodedChildResult.consumed;
|
113
|
+
result.push(decodedChildResult.result);
|
114
|
+
}
|
115
|
+
return {
|
116
|
+
result,
|
117
|
+
encoded: bytes.subarray(consumed),
|
118
|
+
consumed,
|
119
|
+
};
|
120
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
/*
|
2
|
+
This file is part of web3.js.
|
3
|
+
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU Lesser General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
import { AbiError, InvalidBooleanError } from 'web3-errors';
|
18
|
+
import { AbiParameter } from 'web3-types';
|
19
|
+
import { toBool } from 'web3-utils';
|
20
|
+
import { DecoderResult, EncoderResult } from '../types.js';
|
21
|
+
import { WORD_SIZE } from '../utils.js';
|
22
|
+
import { decodeNumber, encodeNumber } from './number.js';
|
23
|
+
|
24
|
+
export function encodeBoolean(param: AbiParameter, input: unknown): EncoderResult {
|
25
|
+
let value;
|
26
|
+
try {
|
27
|
+
value = toBool(input);
|
28
|
+
} catch (e) {
|
29
|
+
if (e instanceof InvalidBooleanError) {
|
30
|
+
throw new AbiError('provided input is not valid boolean value', {
|
31
|
+
type: param.type,
|
32
|
+
value: input,
|
33
|
+
name: param.name,
|
34
|
+
});
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
return encodeNumber({ type: 'uint8', name: '' }, Number(value));
|
39
|
+
}
|
40
|
+
|
41
|
+
export function decodeBool(_param: AbiParameter, bytes: Uint8Array): DecoderResult<boolean> {
|
42
|
+
const numberResult = decodeNumber({ type: 'uint8', name: '' }, bytes);
|
43
|
+
if (numberResult.result > 1 || numberResult.result < 0) {
|
44
|
+
throw new AbiError('Invalid boolean value encoded', {
|
45
|
+
boolBytes: bytes.subarray(0, WORD_SIZE),
|
46
|
+
numberResult,
|
47
|
+
});
|
48
|
+
}
|
49
|
+
return {
|
50
|
+
result: numberResult.result === BigInt(1),
|
51
|
+
encoded: numberResult.encoded,
|
52
|
+
consumed: WORD_SIZE,
|
53
|
+
};
|
54
|
+
}
|
@@ -0,0 +1,106 @@
|
|
1
|
+
/*
|
2
|
+
This file is part of web3.js.
|
3
|
+
|
4
|
+
web3.js is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU Lesser General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
web3.js is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU Lesser General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU Lesser General Public License
|
15
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
import { AbiError } from 'web3-errors';
|
18
|
+
import { AbiParameter, Bytes } from 'web3-types';
|
19
|
+
import { bytesToHex, bytesToUint8Array } from 'web3-utils';
|
20
|
+
import { isBytes, ValidInputTypes } from 'web3-validator';
|
21
|
+
import { DecoderResult, EncoderResult } from '../types.js';
|
22
|
+
import { alloc, WORD_SIZE } from '../utils.js';
|
23
|
+
import { decodeNumber, encodeNumber } from './number.js';
|
24
|
+
|
25
|
+
const MAX_STATIC_BYTES_COUNT = 32;
|
26
|
+
|
27
|
+
export function encodeBytes(param: AbiParameter, input: unknown): EncoderResult {
|
28
|
+
// hack for odd length hex strings
|
29
|
+
if (typeof input === 'string' && input.length % 2 !== 0) {
|
30
|
+
// eslint-disable-next-line no-param-reassign
|
31
|
+
input += '0';
|
32
|
+
}
|
33
|
+
if (!isBytes(input as ValidInputTypes)) {
|
34
|
+
throw new AbiError('provided input is not valid bytes value', {
|
35
|
+
type: param.type,
|
36
|
+
value: input,
|
37
|
+
name: param.name,
|
38
|
+
});
|
39
|
+
}
|
40
|
+
const bytes = bytesToUint8Array(input as Bytes);
|
41
|
+
const [, size] = param.type.split('bytes');
|
42
|
+
// fixed size
|
43
|
+
if (size) {
|
44
|
+
if (Number(size) > MAX_STATIC_BYTES_COUNT || Number(size) < 1) {
|
45
|
+
throw new AbiError(
|
46
|
+
'invalid bytes type. Static byte type can have between 1 and 32 bytes',
|
47
|
+
{
|
48
|
+
type: param.type,
|
49
|
+
},
|
50
|
+
);
|
51
|
+
}
|
52
|
+
if (Number(size) < bytes.length) {
|
53
|
+
throw new AbiError('provided input size is different than type size', {
|
54
|
+
type: param.type,
|
55
|
+
value: input,
|
56
|
+
name: param.name,
|
57
|
+
});
|
58
|
+
}
|
59
|
+
const encoded = alloc(WORD_SIZE);
|
60
|
+
encoded.set(bytes);
|
61
|
+
return {
|
62
|
+
dynamic: false,
|
63
|
+
encoded,
|
64
|
+
};
|
65
|
+
}
|
66
|
+
|
67
|
+
const partsLength = Math.ceil(bytes.length / WORD_SIZE);
|
68
|
+
// one word for length of data + WORD for each part of actual data
|
69
|
+
const encoded = alloc(WORD_SIZE + partsLength * WORD_SIZE);
|
70
|
+
|
71
|
+
encoded.set(encodeNumber({ type: 'uint32', name: '' }, bytes.length).encoded);
|
72
|
+
encoded.set(bytes, WORD_SIZE);
|
73
|
+
return {
|
74
|
+
dynamic: true,
|
75
|
+
encoded,
|
76
|
+
};
|
77
|
+
}
|
78
|
+
|
79
|
+
export function decodeBytes(param: AbiParameter, bytes: Uint8Array): DecoderResult<string> {
|
80
|
+
const [, sizeString] = param.type.split('bytes');
|
81
|
+
let size = Number(sizeString);
|
82
|
+
let remainingBytes = bytes;
|
83
|
+
let partsCount = 1;
|
84
|
+
let consumed = 0;
|
85
|
+
if (!size) {
|
86
|
+
// dynamic bytes
|
87
|
+
const result = decodeNumber({ type: 'uint32', name: '' }, remainingBytes);
|
88
|
+
size = Number(result.result);
|
89
|
+
consumed += result.consumed;
|
90
|
+
remainingBytes = result.encoded;
|
91
|
+
partsCount = Math.ceil(size / WORD_SIZE);
|
92
|
+
}
|
93
|
+
if (size > bytes.length) {
|
94
|
+
throw new AbiError('there is not enough data to decode', {
|
95
|
+
type: param.type,
|
96
|
+
encoded: bytes,
|
97
|
+
size,
|
98
|
+
});
|
99
|
+
}
|
100
|
+
|
101
|
+
return {
|
102
|
+
result: bytesToHex(remainingBytes.subarray(0, size)),
|
103
|
+
encoded: remainingBytes.subarray(partsCount * WORD_SIZE),
|
104
|
+
consumed: consumed + partsCount * WORD_SIZE,
|
105
|
+
};
|
106
|
+
}
|