-
Notifications
You must be signed in to change notification settings - Fork 5k
/
AbiCoder.js
271 lines (230 loc) · 7.24 KB
/
AbiCoder.js
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
/*
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/>.
*/
/**
* @file index.js
* @author Marek Kotewicz <[email protected]>
* @author Fabian Vogelsteller <[email protected]>
* @date 2018
*/
import isArray from 'lodash/isArray';
import isObject from 'lodash/isObject';
// TODO: Implement it by our self this can't be a dependency because of the importance of it.
export default class AbiCoder {
/**
* @param {Utils} utils
* @param {EthersAbiCoder} ethersAbiCoder
*
* @constructor
*/
constructor(utils, ethersAbiCoder) {
this.utils = utils;
this.ethersAbiCoder = ethersAbiCoder;
}
/**
* Encodes the function name to its ABI representation, which are the first 4 bytes of the keccak256 of the function name including types.
*
* @method encodeFunctionSignature
*
* @param {String|Object} functionName
*
* @returns {String} encoded function name
*/
encodeFunctionSignature(functionName) {
if (isObject(functionName)) {
functionName = this.utils.jsonInterfaceMethodToString(functionName);
}
return this.utils.keccak256(functionName).slice(0, 10);
}
/**
* Encodes the function name to its ABI representation, which are the first 4 bytes of the keccak256 of the function name including types.
*
* @method encodeEventSignature
*
* @param {String|Object} functionName
*
* @returns {String} encoded function name
*/
encodeEventSignature(functionName) {
if (isObject(functionName)) {
functionName = this.utils.jsonInterfaceMethodToString(functionName);
}
return this.utils.keccak256(functionName);
}
/**
* Should be used to encode plain param
*
* @method encodeParameter
*
* @param {String} type
* @param {Object} param
*
* @returns {String} encoded plain param
*/
encodeParameter(type, param) {
return this.encodeParameters([type], [param]);
}
/**
* Should be used to encode list of params
*
* @method encodeParameters
*
* @param {Array} types
* @param {Array} params
*
* @returns {String} encoded list of params
*/
encodeParameters(types, params) {
return this.ethersAbiCoder.encode(types, params);
}
/**
* Encodes a function call from its json interface and parameters.
*
* @method encodeFunctionCall
*
* @param {Object} jsonInterface
* @param {Array} params
*
* @returns {String} The encoded ABI for this function call
*/
encodeFunctionCall(jsonInterface, params) {
return (
this.encodeFunctionSignature(jsonInterface) +
this.encodeParameters(jsonInterface.inputs, params).replace('0x', '')
);
}
/**
* Should be used to decode bytes to plain param
*
* @method decodeParameter
*
* @param {String} type
* @param {String} bytes
*
* @returns {Object} plain param
*/
decodeParameter(type, bytes) {
return this.decodeParameters([type], bytes)[0];
}
/**
* Should be used to decode list of params
*
* @method decodeParameter
*
* @param {Array<String|Object>|Object} outputs
* @param {String} bytes
*
* @returns {Object} Object with named and indexed properties of the returnValues
*/
decodeParameters(outputs, bytes) {
if (isArray(outputs) && outputs.length === 0) {
throw new Error('Empty outputs array given!');
}
if (!bytes || bytes === '0x' || bytes === '0X') {
throw new Error(`Invalid bytes string given: ${bytes}`);
}
const result = this.ethersAbiCoder.decode(outputs, bytes);
let returnValues = {};
let decodedValue;
if (isArray(result)) {
if (outputs.length > 1) {
outputs.forEach((output, i) => {
decodedValue = result[i];
if (decodedValue === '0x') {
decodedValue = null;
}
returnValues[i] = decodedValue;
if (isObject(output) && output.name) {
returnValues[output.name] = decodedValue;
}
});
return returnValues;
}
return result;
}
if (isObject(outputs[0]) && outputs[0].name) {
returnValues[outputs[0].name] = result;
}
returnValues[0] = result;
return returnValues;
}
/**
* Decodes events non- and indexed parameters.
*
* @method decodeLog
*
* @param {Array} inputs
* @param {String} data
* @param {Array} topics
*
* @returns {Object} Object with named and indexed properties of the returnValues
*/
decodeLog(inputs, data = '', topics) {
const returnValues = {};
let topicCount = 0;
let value;
let nonIndexedInputKeys = [];
let nonIndexedInputItems = [];
if (!isArray(topics)) {
topics = [topics];
}
inputs.forEach((input, i) => {
if (input.indexed) {
if (input.type === 'string') {
return;
}
value = topics[topicCount];
if (this.isStaticType(input.type)) {
value = this.decodeParameter(input.type, topics[topicCount]);
}
returnValues[i] = value;
returnValues[input.name] = value;
topicCount++;
return;
}
nonIndexedInputKeys.push(i);
nonIndexedInputItems.push(input);
});
if (data) {
let values = this.decodeParameters(nonIndexedInputItems, data);
let decodedValue;
nonIndexedInputKeys.forEach((itemKey, index) => {
decodedValue = values[index];
returnValues[itemKey] = decodedValue;
returnValues[nonIndexedInputItems[index].name] = decodedValue;
});
}
return returnValues;
}
/**
* Checks if a given type string is a static solidity type
*
* @method isStaticType
*
* @param {String} type
*
* @returns {Boolean}
*/
isStaticType(type) {
if (type === 'bytes') {
return false;
}
if (type === 'string') {
return false;
}
if (type.indexOf('[') && type.slice(type.indexOf('[')).length === 2) {
return false;
}
return true;
}
}