web-eth-abi 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +59 -0
  3. package/lib/commonjs/api/errors_api.d.ts +5 -0
  4. package/lib/commonjs/api/errors_api.js +44 -0
  5. package/lib/commonjs/api/errors_api.js.map +1 -0
  6. package/lib/commonjs/api/events_api.d.ts +51 -0
  7. package/lib/commonjs/api/events_api.js +90 -0
  8. package/lib/commonjs/api/events_api.js.map +1 -0
  9. package/lib/commonjs/api/functions_api.d.ts +92 -0
  10. package/lib/commonjs/api/functions_api.js +139 -0
  11. package/lib/commonjs/api/functions_api.js.map +1 -0
  12. package/lib/commonjs/api/logs_api.d.ts +49 -0
  13. package/lib/commonjs/api/logs_api.js +107 -0
  14. package/lib/commonjs/api/logs_api.js.map +1 -0
  15. package/lib/commonjs/api/parameters_api.d.ts +238 -0
  16. package/lib/commonjs/api/parameters_api.js +280 -0
  17. package/lib/commonjs/api/parameters_api.js.map +1 -0
  18. package/lib/commonjs/coders/base/address.d.ts +4 -0
  19. package/lib/commonjs/coders/base/address.js +75 -0
  20. package/lib/commonjs/coders/base/address.js.map +1 -0
  21. package/lib/commonjs/coders/base/array.d.ts +4 -0
  22. package/lib/commonjs/coders/base/array.js +106 -0
  23. package/lib/commonjs/coders/base/array.js.map +1 -0
  24. package/lib/commonjs/coders/base/bool.d.ts +4 -0
  25. package/lib/commonjs/coders/base/bool.js +56 -0
  26. package/lib/commonjs/coders/base/bool.js.map +1 -0
  27. package/lib/commonjs/coders/base/bytes.d.ts +4 -0
  28. package/lib/commonjs/coders/base/bytes.js +101 -0
  29. package/lib/commonjs/coders/base/bytes.js.map +1 -0
  30. package/lib/commonjs/coders/base/index.d.ts +11 -0
  31. package/lib/commonjs/coders/base/index.js +109 -0
  32. package/lib/commonjs/coders/base/index.js.map +1 -0
  33. package/lib/commonjs/coders/base/number.d.ts +4 -0
  34. package/lib/commonjs/coders/base/number.js +116 -0
  35. package/lib/commonjs/coders/base/number.js.map +1 -0
  36. package/lib/commonjs/coders/base/numbersLimits.d.ts +4 -0
  37. package/lib/commonjs/coders/base/numbersLimits.js +40 -0
  38. package/lib/commonjs/coders/base/numbersLimits.js.map +1 -0
  39. package/lib/commonjs/coders/base/string.d.ts +4 -0
  40. package/lib/commonjs/coders/base/string.js +40 -0
  41. package/lib/commonjs/coders/base/string.js.map +1 -0
  42. package/lib/commonjs/coders/base/tuple.d.ts +7 -0
  43. package/lib/commonjs/coders/base/tuple.js +123 -0
  44. package/lib/commonjs/coders/base/tuple.js.map +1 -0
  45. package/lib/commonjs/coders/base/utils.d.ts +2 -0
  46. package/lib/commonjs/coders/base/utils.js +50 -0
  47. package/lib/commonjs/coders/base/utils.js.map +1 -0
  48. package/lib/commonjs/coders/decode.d.ts +5 -0
  49. package/lib/commonjs/coders/decode.js +29 -0
  50. package/lib/commonjs/coders/decode.js.map +1 -0
  51. package/lib/commonjs/coders/encode.d.ts +38 -0
  52. package/lib/commonjs/coders/encode.js +113 -0
  53. package/lib/commonjs/coders/encode.js.map +1 -0
  54. package/lib/commonjs/coders/types.d.ts +16 -0
  55. package/lib/commonjs/coders/types.js +19 -0
  56. package/lib/commonjs/coders/types.js.map +1 -0
  57. package/lib/commonjs/coders/utils.d.ts +23 -0
  58. package/lib/commonjs/coders/utils.js +115 -0
  59. package/lib/commonjs/coders/utils.js.map +1 -0
  60. package/lib/commonjs/decode_contract_error_data.d.ts +3 -0
  61. package/lib/commonjs/decode_contract_error_data.js +73 -0
  62. package/lib/commonjs/decode_contract_error_data.js.map +1 -0
  63. package/lib/commonjs/eip_712.d.ts +50 -0
  64. package/lib/commonjs/eip_712.js +144 -0
  65. package/lib/commonjs/eip_712.js.map +1 -0
  66. package/lib/commonjs/index.d.ts +8 -0
  67. package/lib/commonjs/index.js +43 -0
  68. package/lib/commonjs/index.js.map +1 -0
  69. package/lib/commonjs/package.json +1 -0
  70. package/lib/commonjs/utils.d.ts +43 -0
  71. package/lib/commonjs/utils.js +216 -0
  72. package/lib/commonjs/utils.js.map +1 -0
  73. package/lib/esm/api/errors_api.js +40 -0
  74. package/lib/esm/api/errors_api.js.map +1 -0
  75. package/lib/esm/api/events_api.js +86 -0
  76. package/lib/esm/api/events_api.js.map +1 -0
  77. package/lib/esm/api/functions_api.js +134 -0
  78. package/lib/esm/api/functions_api.js.map +1 -0
  79. package/lib/esm/api/logs_api.js +103 -0
  80. package/lib/esm/api/logs_api.js.map +1 -0
  81. package/lib/esm/api/parameters_api.js +271 -0
  82. package/lib/esm/api/parameters_api.js.map +1 -0
  83. package/lib/esm/coders/base/address.js +70 -0
  84. package/lib/esm/coders/base/address.js.map +1 -0
  85. package/lib/esm/coders/base/array.js +101 -0
  86. package/lib/esm/coders/base/array.js.map +1 -0
  87. package/lib/esm/coders/base/bool.js +51 -0
  88. package/lib/esm/coders/base/bool.js.map +1 -0
  89. package/lib/esm/coders/base/bytes.js +96 -0
  90. package/lib/esm/coders/base/bytes.js.map +1 -0
  91. package/lib/esm/coders/base/index.js +90 -0
  92. package/lib/esm/coders/base/index.js.map +1 -0
  93. package/lib/esm/coders/base/number.js +111 -0
  94. package/lib/esm/coders/base/number.js.map +1 -0
  95. package/lib/esm/coders/base/numbersLimits.js +37 -0
  96. package/lib/esm/coders/base/numbersLimits.js.map +1 -0
  97. package/lib/esm/coders/base/string.js +35 -0
  98. package/lib/esm/coders/base/string.js.map +1 -0
  99. package/lib/esm/coders/base/tuple.js +118 -0
  100. package/lib/esm/coders/base/tuple.js.map +1 -0
  101. package/lib/esm/coders/base/utils.js +46 -0
  102. package/lib/esm/coders/base/utils.js.map +1 -0
  103. package/lib/esm/coders/decode.js +25 -0
  104. package/lib/esm/coders/decode.js.map +1 -0
  105. package/lib/esm/coders/encode.js +108 -0
  106. package/lib/esm/coders/encode.js.map +1 -0
  107. package/lib/esm/coders/types.js +18 -0
  108. package/lib/esm/coders/types.js.map +1 -0
  109. package/lib/esm/coders/utils.js +105 -0
  110. package/lib/esm/coders/utils.js.map +1 -0
  111. package/lib/esm/decode_contract_error_data.js +69 -0
  112. package/lib/esm/decode_contract_error_data.js.map +1 -0
  113. package/lib/esm/eip_712.js +140 -0
  114. package/lib/esm/eip_712.js.map +1 -0
  115. package/lib/esm/index.js +25 -0
  116. package/lib/esm/index.js.map +1 -0
  117. package/lib/esm/package.json +1 -0
  118. package/lib/esm/utils.js +199 -0
  119. package/lib/esm/utils.js.map +1 -0
  120. package/lib/types/api/errors_api.d.ts +6 -0
  121. package/lib/types/api/errors_api.d.ts.map +1 -0
  122. package/lib/types/api/events_api.d.ts +52 -0
  123. package/lib/types/api/events_api.d.ts.map +1 -0
  124. package/lib/types/api/functions_api.d.ts +93 -0
  125. package/lib/types/api/functions_api.d.ts.map +1 -0
  126. package/lib/types/api/logs_api.d.ts +50 -0
  127. package/lib/types/api/logs_api.d.ts.map +1 -0
  128. package/lib/types/api/parameters_api.d.ts +239 -0
  129. package/lib/types/api/parameters_api.d.ts.map +1 -0
  130. package/lib/types/coders/base/address.d.ts +5 -0
  131. package/lib/types/coders/base/address.d.ts.map +1 -0
  132. package/lib/types/coders/base/array.d.ts +5 -0
  133. package/lib/types/coders/base/array.d.ts.map +1 -0
  134. package/lib/types/coders/base/bool.d.ts +5 -0
  135. package/lib/types/coders/base/bool.d.ts.map +1 -0
  136. package/lib/types/coders/base/bytes.d.ts +5 -0
  137. package/lib/types/coders/base/bytes.d.ts.map +1 -0
  138. package/lib/types/coders/base/index.d.ts +12 -0
  139. package/lib/types/coders/base/index.d.ts.map +1 -0
  140. package/lib/types/coders/base/number.d.ts +5 -0
  141. package/lib/types/coders/base/number.d.ts.map +1 -0
  142. package/lib/types/coders/base/numbersLimits.d.ts +5 -0
  143. package/lib/types/coders/base/numbersLimits.d.ts.map +1 -0
  144. package/lib/types/coders/base/string.d.ts +5 -0
  145. package/lib/types/coders/base/string.d.ts.map +1 -0
  146. package/lib/types/coders/base/tuple.d.ts +8 -0
  147. package/lib/types/coders/base/tuple.d.ts.map +1 -0
  148. package/lib/types/coders/base/utils.d.ts +3 -0
  149. package/lib/types/coders/base/utils.d.ts.map +1 -0
  150. package/lib/types/coders/decode.d.ts +6 -0
  151. package/lib/types/coders/decode.d.ts.map +1 -0
  152. package/lib/types/coders/encode.d.ts +39 -0
  153. package/lib/types/coders/encode.d.ts.map +1 -0
  154. package/lib/types/coders/types.d.ts +17 -0
  155. package/lib/types/coders/types.d.ts.map +1 -0
  156. package/lib/types/coders/utils.d.ts +24 -0
  157. package/lib/types/coders/utils.d.ts.map +1 -0
  158. package/lib/types/decode_contract_error_data.d.ts +4 -0
  159. package/lib/types/decode_contract_error_data.d.ts.map +1 -0
  160. package/lib/types/eip_712.d.ts +51 -0
  161. package/lib/types/eip_712.d.ts.map +1 -0
  162. package/lib/types/index.d.ts +9 -0
  163. package/lib/types/index.d.ts.map +1 -0
  164. package/lib/types/utils.d.ts +44 -0
  165. package/lib/types/utils.d.ts.map +1 -0
  166. package/package.json +56 -0
  167. package/src/api/errors_api.ts +45 -0
  168. package/src/api/events_api.ts +91 -0
  169. package/src/api/functions_api.ts +145 -0
  170. package/src/api/logs_api.ts +131 -0
  171. package/src/api/parameters_api.ts +287 -0
  172. package/src/coders/base/address.ts +75 -0
  173. package/src/coders/base/array.ts +120 -0
  174. package/src/coders/base/bool.ts +54 -0
  175. package/src/coders/base/bytes.ts +106 -0
  176. package/src/coders/base/index.ts +95 -0
  177. package/src/coders/base/number.ts +116 -0
  178. package/src/coders/base/numbersLimits.ts +39 -0
  179. package/src/coders/base/string.ts +38 -0
  180. package/src/coders/base/tuple.ts +130 -0
  181. package/src/coders/base/utils.ts +51 -0
  182. package/src/coders/decode.ts +32 -0
  183. package/src/coders/encode.ts +114 -0
  184. package/src/coders/types.ts +39 -0
  185. package/src/coders/utils.ts +128 -0
  186. package/src/decode_contract_error_data.ts +80 -0
  187. package/src/eip_712.ts +252 -0
  188. package/src/index.ts +25 -0
  189. package/src/utils.ts +266 -0
  190. package/wimho03d.cjs +1 -0
@@ -0,0 +1,238 @@
1
+ import { AbiInput, HexString } from 'web3-types';
2
+ export { encodeParameters, inferTypesAndEncodeParameters } from '../coders/encode.js';
3
+ /**
4
+ * Encodes a parameter based on its type to its ABI representation.
5
+ * @param abi - The type of the parameter. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
6
+ * @param param - The actual parameter to encode.
7
+ * @returns - The ABI encoded parameter
8
+ * @example
9
+ * ```ts
10
+ * const res = web3.eth.abi.encodeParameter("uint256", "2345675643");
11
+ * console.log(res);
12
+ * 0x000000000000000000000000000000000000000000000000000000008bd02b7b
13
+ *
14
+ * const res = web3.eth.abi.encodeParameter("uint", "2345675643");
15
+ *
16
+ * console.log(res);
17
+ * >0x000000000000000000000000000000000000000000000000000000008bd02b7b
18
+ *
19
+ * const res = web3.eth.abi.encodeParameter("bytes32", "0xdf3234");
20
+ *
21
+ * console.log(res);
22
+ * >0xdf32340000000000000000000000000000000000000000000000000000000000
23
+ *
24
+ * const res = web3.eth.abi.encodeParameter("bytes", "0xdf3234");
25
+ *
26
+ * console.log(res);
27
+ * > 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000
28
+ *
29
+ * const res = web3.eth.abi.encodeParameter("bytes32[]", ["0xdf3234", "0xfdfd"]);
30
+ *
31
+ * console.log(res);
32
+ * > 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000
33
+ *
34
+ * const res = web3.eth.abi.encodeParameter(
35
+ * {
36
+ * ParentStruct: {
37
+ * propertyOne: "uint256",
38
+ * propertyTwo: "uint256",
39
+ * childStruct: {
40
+ * propertyOne: "uint256",
41
+ * propertyTwo: "uint256",
42
+ * },
43
+ * },
44
+ * },
45
+ * {
46
+ * propertyOne: 42,
47
+ * propertyTwo: 56,
48
+ * childStruct: {
49
+ * propertyOne: 45,
50
+ * propertyTwo: 78,
51
+ * },
52
+ * }
53
+ * );
54
+ *
55
+ * console.log(res);
56
+ * > 0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e
57
+ * ```
58
+ */
59
+ export declare const encodeParameter: (abi: AbiInput, param: unknown) => string;
60
+ /**
61
+ * Should be used to decode list of params
62
+ */
63
+ export declare const decodeParametersWith: (abis: AbiInput[] | ReadonlyArray<AbiInput>, bytes: HexString, loose: boolean) => {
64
+ [key: string]: unknown;
65
+ __length__: number;
66
+ };
67
+ /**
68
+ * Should be used to decode list of params
69
+ */
70
+ /**
71
+ * Decodes ABI encoded parameters to its JavaScript types.
72
+ * @param abi - An array of {@link AbiInput}. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
73
+ * @param bytes - The ABI byte code to decode
74
+ * @returns - The result object containing the decoded parameters.
75
+ * @example
76
+ * ```ts
77
+ * let res = web3.eth.abi.decodeParameters(
78
+ * ["string", "uint256"],
79
+ * "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
80
+ * );
81
+ * console.log(res);
82
+ * > { '0': 'Hello!%!', '1': 234n, __length__: 2 }
83
+ *
84
+ * let res = web3.eth.abi.decodeParameters(
85
+ * [
86
+ * {
87
+ * type: "string",
88
+ * name: "myString",
89
+ * },
90
+ * {
91
+ * type: "uint256",
92
+ * name: "myNumber",
93
+ * },
94
+ * ],
95
+ * "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
96
+ * );
97
+ * console.log(res);
98
+ * > {
99
+ * '0': 'Hello!%!',
100
+ * '1': 234n,
101
+ * __length__: 2,
102
+ * myString: 'Hello!%!',
103
+ * myNumber: 234n
104
+ * }
105
+ *
106
+ * const res = web3.eth.abi.decodeParameters(
107
+ * [
108
+ * "uint8[]",
109
+ * {
110
+ * ParentStruct: {
111
+ * propertyOne: "uint256",
112
+ * propertyTwo: "uint256",
113
+ * childStruct: {
114
+ * propertyOne: "uint256",
115
+ * propertyTwo: "uint256",
116
+ * },
117
+ * },
118
+ * },
119
+ * ],
120
+ * "0x00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000018"
121
+ * );
122
+ * console.log(res);
123
+ * >
124
+ * '0': [ 42n, 24n ],
125
+ * '1': {
126
+ * '0': 42n,
127
+ * '1': 56n,
128
+ * '2': {
129
+ * '0': 45n,
130
+ * '1': 78n,
131
+ * __length__: 2,
132
+ * propertyOne: 45n,
133
+ * propertyTwo: 78n
134
+ * },
135
+ * __length__: 3,
136
+ * propertyOne: 42n,
137
+ * propertyTwo: 56n,
138
+ * childStruct: {
139
+ * '0': 45n,
140
+ * '1': 78n,
141
+ * __length__: 2,
142
+ * propertyOne: 45n,
143
+ * propertyTwo: 78n
144
+ * }
145
+ * },
146
+ * __length__: 2,
147
+ * ParentStruct: {
148
+ * '0': 42n,
149
+ * '1': 56n,
150
+ * '2': {
151
+ * '0': 45n,
152
+ * '1': 78n,
153
+ * __length__: 2,
154
+ * propertyOne: 45n,
155
+ * propertyTwo: 78n
156
+ * },
157
+ * __length__: 3,
158
+ * propertyOne: 42n,
159
+ * propertyTwo: 56n,
160
+ * childStruct: {
161
+ * '0': 45n,
162
+ * '1': 78n,
163
+ * __length__: 2,
164
+ * propertyOne: 45n,
165
+ * propertyTwo: 78n
166
+ * }
167
+ * }
168
+ *}
169
+ * ```
170
+ */
171
+ export declare const decodeParameters: (abi: AbiInput[] | ReadonlyArray<AbiInput>, bytes: HexString) => {
172
+ [key: string]: unknown;
173
+ __length__: number;
174
+ };
175
+ /**
176
+ * Should be used to decode bytes to plain param
177
+ */
178
+ /**
179
+ * Decodes an ABI encoded parameter to its JavaScript type.
180
+ * @param abi - The type of the parameter. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
181
+ * @param bytes - The ABI byte code to decode
182
+ * @returns - The decoded parameter
183
+ * @example
184
+ * ```ts
185
+ * const res = web3.eth.abi.decodeParameter(
186
+ * "uint256",
187
+ * "0x0000000000000000000000000000000000000000000000000000000000000010"
188
+ * );
189
+ * console.log(res);
190
+ * > 16n
191
+ *
192
+ * const res = web3.eth.abi.decodeParameter(
193
+ * "string",
194
+ * "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
195
+ * );
196
+ *
197
+ * console.log(res);
198
+ * > Hello!%!
199
+ *
200
+ * const res = web3.eth.abi.decodeParameter(
201
+ * {
202
+ * ParentStruct: {
203
+ * propertyOne: "uint256",
204
+ * propertyTwo: "uint256",
205
+ * childStruct: {
206
+ * propertyOne: "uint256",
207
+ * propertyTwo: "uint256",
208
+ * },
209
+ * },
210
+ * },
211
+ * "0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e"
212
+ * );
213
+ *
214
+ * console.log(res);
215
+ * {
216
+ * '0': 42n,
217
+ * '1': 56n,
218
+ * '2': {
219
+ * '0': 45n,
220
+ * '1': 78n,
221
+ * __length__: 2,
222
+ * propertyOne: 45n,
223
+ * propertyTwo: 78n
224
+ * },
225
+ * __length__: 3,
226
+ * propertyOne: 42n,
227
+ * propertyTwo: 56n,
228
+ * childStruct: {
229
+ * '0': 45n,
230
+ * '1': 78n,
231
+ * __length__: 2,
232
+ * propertyOne: 45n,
233
+ * propertyTwo: 78n
234
+ * }
235
+ *}
236
+ * ```
237
+ */
238
+ export declare const decodeParameter: (abi: AbiInput, bytes: HexString) => unknown;
@@ -0,0 +1,280 @@
1
+ "use strict";
2
+ /*
3
+ This file is part of web3.js.
4
+
5
+ web3.js is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ web3.js is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.decodeParameter = exports.decodeParameters = exports.decodeParametersWith = exports.encodeParameter = exports.inferTypesAndEncodeParameters = exports.encodeParameters = void 0;
20
+ /**
21
+ *
22
+ * @module ABI
23
+ */
24
+ const web3_errors_1 = require("web3-errors");
25
+ const decode_js_1 = require("../coders/decode.js");
26
+ const encode_js_1 = require("../coders/encode.js");
27
+ var encode_js_2 = require("../coders/encode.js");
28
+ Object.defineProperty(exports, "encodeParameters", { enumerable: true, get: function () { return encode_js_2.encodeParameters; } });
29
+ Object.defineProperty(exports, "inferTypesAndEncodeParameters", { enumerable: true, get: function () { return encode_js_2.inferTypesAndEncodeParameters; } });
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
+ const encodeParameter = (abi, param) => (0, encode_js_1.encodeParameters)([abi], [param]);
87
+ exports.encodeParameter = encodeParameter;
88
+ /**
89
+ * Should be used to decode list of params
90
+ */
91
+ const decodeParametersWith = (abis, bytes, loose) => {
92
+ try {
93
+ if (abis.length > 0 && (!bytes || bytes === '0x' || bytes === '0X')) {
94
+ throw new web3_errors_1.AbiError("Returned values aren't valid, did it run Out of Gas? " +
95
+ 'You might also see this error if you are not using the ' +
96
+ 'correct ABI for the contract you are retrieving data from, ' +
97
+ 'requesting data from a block number that does not exist, ' +
98
+ 'or querying a node which is not fully synced.');
99
+ }
100
+ return (0, decode_js_1.decodeParameters)(abis, `0x${bytes.replace(/0x/i, '')}`, loose);
101
+ }
102
+ catch (err) {
103
+ throw new web3_errors_1.AbiError(`Parameter decoding error: ${err.message}`, {
104
+ internalErr: err,
105
+ });
106
+ }
107
+ };
108
+ exports.decodeParametersWith = decodeParametersWith;
109
+ /**
110
+ * Should be used to decode list of params
111
+ */
112
+ /**
113
+ * Decodes ABI encoded parameters to its JavaScript types.
114
+ * @param abi - An array of {@link AbiInput}. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
115
+ * @param bytes - The ABI byte code to decode
116
+ * @returns - The result object containing the decoded parameters.
117
+ * @example
118
+ * ```ts
119
+ * let res = web3.eth.abi.decodeParameters(
120
+ * ["string", "uint256"],
121
+ * "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
122
+ * );
123
+ * console.log(res);
124
+ * > { '0': 'Hello!%!', '1': 234n, __length__: 2 }
125
+ *
126
+ * let res = web3.eth.abi.decodeParameters(
127
+ * [
128
+ * {
129
+ * type: "string",
130
+ * name: "myString",
131
+ * },
132
+ * {
133
+ * type: "uint256",
134
+ * name: "myNumber",
135
+ * },
136
+ * ],
137
+ * "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
138
+ * );
139
+ * console.log(res);
140
+ * > {
141
+ * '0': 'Hello!%!',
142
+ * '1': 234n,
143
+ * __length__: 2,
144
+ * myString: 'Hello!%!',
145
+ * myNumber: 234n
146
+ * }
147
+ *
148
+ * const res = web3.eth.abi.decodeParameters(
149
+ * [
150
+ * "uint8[]",
151
+ * {
152
+ * ParentStruct: {
153
+ * propertyOne: "uint256",
154
+ * propertyTwo: "uint256",
155
+ * childStruct: {
156
+ * propertyOne: "uint256",
157
+ * propertyTwo: "uint256",
158
+ * },
159
+ * },
160
+ * },
161
+ * ],
162
+ * "0x00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000018"
163
+ * );
164
+ * console.log(res);
165
+ * >
166
+ * '0': [ 42n, 24n ],
167
+ * '1': {
168
+ * '0': 42n,
169
+ * '1': 56n,
170
+ * '2': {
171
+ * '0': 45n,
172
+ * '1': 78n,
173
+ * __length__: 2,
174
+ * propertyOne: 45n,
175
+ * propertyTwo: 78n
176
+ * },
177
+ * __length__: 3,
178
+ * propertyOne: 42n,
179
+ * propertyTwo: 56n,
180
+ * childStruct: {
181
+ * '0': 45n,
182
+ * '1': 78n,
183
+ * __length__: 2,
184
+ * propertyOne: 45n,
185
+ * propertyTwo: 78n
186
+ * }
187
+ * },
188
+ * __length__: 2,
189
+ * ParentStruct: {
190
+ * '0': 42n,
191
+ * '1': 56n,
192
+ * '2': {
193
+ * '0': 45n,
194
+ * '1': 78n,
195
+ * __length__: 2,
196
+ * propertyOne: 45n,
197
+ * propertyTwo: 78n
198
+ * },
199
+ * __length__: 3,
200
+ * propertyOne: 42n,
201
+ * propertyTwo: 56n,
202
+ * childStruct: {
203
+ * '0': 45n,
204
+ * '1': 78n,
205
+ * __length__: 2,
206
+ * propertyOne: 45n,
207
+ * propertyTwo: 78n
208
+ * }
209
+ * }
210
+ *}
211
+ * ```
212
+ */
213
+ const decodeParameters = (abi, bytes) => (0, exports.decodeParametersWith)(abi, bytes, false);
214
+ exports.decodeParameters = decodeParameters;
215
+ /**
216
+ * Should be used to decode bytes to plain param
217
+ */
218
+ /**
219
+ * Decodes an ABI encoded parameter to its JavaScript type.
220
+ * @param abi - The type of the parameter. See the [Solidity documentation](https://docs.soliditylang.org/en/develop/types.html) for a list of types.
221
+ * @param bytes - The ABI byte code to decode
222
+ * @returns - The decoded parameter
223
+ * @example
224
+ * ```ts
225
+ * const res = web3.eth.abi.decodeParameter(
226
+ * "uint256",
227
+ * "0x0000000000000000000000000000000000000000000000000000000000000010"
228
+ * );
229
+ * console.log(res);
230
+ * > 16n
231
+ *
232
+ * const res = web3.eth.abi.decodeParameter(
233
+ * "string",
234
+ * "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000"
235
+ * );
236
+ *
237
+ * console.log(res);
238
+ * > Hello!%!
239
+ *
240
+ * const res = web3.eth.abi.decodeParameter(
241
+ * {
242
+ * ParentStruct: {
243
+ * propertyOne: "uint256",
244
+ * propertyTwo: "uint256",
245
+ * childStruct: {
246
+ * propertyOne: "uint256",
247
+ * propertyTwo: "uint256",
248
+ * },
249
+ * },
250
+ * },
251
+ * "0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e"
252
+ * );
253
+ *
254
+ * console.log(res);
255
+ * {
256
+ * '0': 42n,
257
+ * '1': 56n,
258
+ * '2': {
259
+ * '0': 45n,
260
+ * '1': 78n,
261
+ * __length__: 2,
262
+ * propertyOne: 45n,
263
+ * propertyTwo: 78n
264
+ * },
265
+ * __length__: 3,
266
+ * propertyOne: 42n,
267
+ * propertyTwo: 56n,
268
+ * childStruct: {
269
+ * '0': 45n,
270
+ * '1': 78n,
271
+ * __length__: 2,
272
+ * propertyOne: 45n,
273
+ * propertyTwo: 78n
274
+ * }
275
+ *}
276
+ * ```
277
+ */
278
+ const decodeParameter = (abi, bytes) => (0, exports.decodeParameters)([abi], bytes)['0'];
279
+ exports.decodeParameter = decodeParameter;
280
+ //# sourceMappingURL=parameters_api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parameters_api.js","sourceRoot":"","sources":["../../../src/api/parameters_api.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;EAeE;;;AAEF;;;GAGG;AAEH,6CAAuC;AAEvC,mDAAmF;AACnF,mDAAuD;AAEvD,iDAAsF;AAA7E,6GAAA,gBAAgB,OAAA;AAAE,0HAAA,6BAA6B,OAAA;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACI,MAAM,eAAe,GAAG,CAAC,GAAa,EAAE,KAAc,EAAU,EAAE,CACxE,IAAA,4BAAgB,EAAC,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AADrB,QAAA,eAAe,mBACM;AAClC;;GAEG;AACI,MAAM,oBAAoB,GAAG,CACnC,IAA0C,EAC1C,KAAgB,EAChB,KAAc,EACmC,EAAE;IACnD,IAAI;QACH,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,CAAC,EAAE;YACpE,MAAM,IAAI,sBAAQ,CACjB,uDAAuD;gBACtD,yDAAyD;gBACzD,6DAA6D;gBAC7D,2DAA2D;gBAC3D,+CAA+C,CAChD,CAAC;SACF;QACD,OAAO,IAAA,4BAAwB,EAAC,IAAI,EAAE,KAAK,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;KAC9E;IAAC,OAAO,GAAG,EAAE;QACb,MAAM,IAAI,sBAAQ,CAAC,6BAA8B,GAAa,CAAC,OAAO,EAAE,EAAE;YACzE,WAAW,EAAE,GAAG;SAChB,CAAC,CAAC;KACH;AACF,CAAC,CAAC;AArBW,QAAA,oBAAoB,wBAqB/B;AAEF;;GAEG;AACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoGG;AACI,MAAM,gBAAgB,GAAG,CAC/B,GAAyC,EACzC,KAAgB,EACiC,EAAE,CAAC,IAAA,4BAAoB,EAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAHhF,QAAA,gBAAgB,oBAGgE;AAE7F;;GAEG;AACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACI,MAAM,eAAe,GAAG,CAAC,GAAa,EAAE,KAAgB,EAAW,EAAE,CAC3E,IAAA,wBAAgB,EAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC;AADxB,QAAA,eAAe,mBACS"}
@@ -0,0 +1,4 @@
1
+ import { AbiParameter } from 'web3-types';
2
+ import { DecoderResult, EncoderResult } from '../types.js';
3
+ export declare function encodeAddress(param: AbiParameter, input: unknown): EncoderResult;
4
+ export declare function decodeAddress(_param: AbiParameter, bytes: Uint8Array): DecoderResult<string>;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeAddress = exports.encodeAddress = void 0;
4
+ /*
5
+ This file is part of web3.js.
6
+
7
+ web3.js is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU Lesser General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ web3.js is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU Lesser General Public License for more details.
16
+
17
+ You should have received a copy of the GNU Lesser General Public License
18
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+ const web3_errors_1 = require("web3-errors");
21
+ const web3_utils_1 = require("web3-utils");
22
+ const web3_validator_1 = require("web3-validator");
23
+ const utils_js_1 = require("../utils.js");
24
+ const ADDRESS_BYTES_COUNT = 20;
25
+ const ADDRESS_OFFSET = utils_js_1.WORD_SIZE - ADDRESS_BYTES_COUNT;
26
+ function encodeAddress(param, input) {
27
+ if (typeof input !== 'string') {
28
+ throw new web3_errors_1.AbiError('address type expects string as input type', {
29
+ value: input,
30
+ name: param.name,
31
+ type: param.type,
32
+ });
33
+ }
34
+ let address = input.toLowerCase();
35
+ if (!address.startsWith('0x')) {
36
+ address = `0x${address}`;
37
+ }
38
+ if (!(0, web3_validator_1.isAddress)(address)) {
39
+ throw new web3_errors_1.AbiError('provided input is not valid address', {
40
+ value: input,
41
+ name: param.name,
42
+ type: param.type,
43
+ });
44
+ }
45
+ // for better performance, we could convert hex to destination bytes directly (encoded var)
46
+ const addressBytes = web3_validator_1.utils.hexToUint8Array(address);
47
+ // expand address to WORD_SIZE
48
+ const encoded = (0, utils_js_1.alloc)(utils_js_1.WORD_SIZE);
49
+ encoded.set(addressBytes, ADDRESS_OFFSET);
50
+ return {
51
+ dynamic: false,
52
+ encoded,
53
+ };
54
+ }
55
+ exports.encodeAddress = encodeAddress;
56
+ function decodeAddress(_param, bytes) {
57
+ const addressBytes = bytes.subarray(ADDRESS_OFFSET, utils_js_1.WORD_SIZE);
58
+ if (addressBytes.length !== ADDRESS_BYTES_COUNT) {
59
+ throw new web3_errors_1.AbiError('Invalid decoding input, not enough bytes to decode address', { bytes });
60
+ }
61
+ const result = web3_validator_1.utils.uint8ArrayToHexString(addressBytes);
62
+ // should we check is decoded value is valid address?
63
+ // if(!isAddress(result)) {
64
+ // throw new AbiError("encoded data is not valid address", {
65
+ // address: result,
66
+ // });
67
+ // }
68
+ return {
69
+ result: (0, web3_utils_1.toChecksumAddress)(result),
70
+ encoded: bytes.subarray(utils_js_1.WORD_SIZE),
71
+ consumed: utils_js_1.WORD_SIZE,
72
+ };
73
+ }
74
+ exports.decodeAddress = decodeAddress;
75
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.js","sourceRoot":"","sources":["../../../../src/coders/base/address.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;EAeE;AACF,6CAAuC;AAEvC,2CAA+C;AAC/C,mDAAkD;AAElD,0CAA+C;AAE/C,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAC/B,MAAM,cAAc,GAAG,oBAAS,GAAG,mBAAmB,CAAC;AAEvD,SAAgB,aAAa,CAAC,KAAmB,EAAE,KAAc;IAChE,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QAC9B,MAAM,IAAI,sBAAQ,CAAC,2CAA2C,EAAE;YAC/D,KAAK,EAAE,KAAK;YACZ,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,IAAI,EAAE,KAAK,CAAC,IAAI;SAChB,CAAC,CAAC;KACH;IACD,IAAI,OAAO,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;IAClC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAC9B,OAAO,GAAG,KAAK,OAAO,EAAE,CAAC;KACzB;IACD,IAAI,CAAC,IAAA,0BAAS,EAAC,OAAO,CAAC,EAAE;QACxB,MAAM,IAAI,sBAAQ,CAAC,qCAAqC,EAAE;YACzD,KAAK,EAAE,KAAK;YACZ,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,IAAI,EAAE,KAAK,CAAC,IAAI;SAChB,CAAC,CAAC;KACH;IACD,2FAA2F;IAC3F,MAAM,YAAY,GAAG,sBAAK,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IACpD,8BAA8B;IAC9B,MAAM,OAAO,GAAG,IAAA,gBAAK,EAAC,oBAAS,CAAC,CAAC;IACjC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;IAC1C,OAAO;QACN,OAAO,EAAE,KAAK;QACd,OAAO;KACP,CAAC;AACH,CAAC;AA5BD,sCA4BC;AAED,SAAgB,aAAa,CAAC,MAAoB,EAAE,KAAiB;IACpE,MAAM,YAAY,GAAG,KAAK,CAAC,QAAQ,CAAC,cAAc,EAAE,oBAAS,CAAC,CAAC;IAC/D,IAAI,YAAY,CAAC,MAAM,KAAK,mBAAmB,EAAE;QAChD,MAAM,IAAI,sBAAQ,CAAC,4DAA4D,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;KAC5F;IACD,MAAM,MAAM,GAAG,sBAAK,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC;IAEzD,qDAAqD;IACrD,2BAA2B;IAC3B,gEAAgE;IAChE,2BAA2B;IAC3B,UAAU;IACV,IAAI;IACJ,OAAO;QACN,MAAM,EAAE,IAAA,8BAAiB,EAAC,MAAM,CAAC;QACjC,OAAO,EAAE,KAAK,CAAC,QAAQ,CAAC,oBAAS,CAAC;QAClC,QAAQ,EAAE,oBAAS;KACnB,CAAC;AACH,CAAC;AAlBD,sCAkBC"}
@@ -0,0 +1,4 @@
1
+ import { AbiParameter } from 'web3-types';
2
+ import { DecoderResult, EncoderResult } from '../types.js';
3
+ export declare function encodeArray(param: AbiParameter, values: unknown): EncoderResult;
4
+ export declare function decodeArray(param: AbiParameter, bytes: Uint8Array): DecoderResult<unknown[]>;