forked from Consensys/abi-decoder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
206 lines (177 loc) · 5.12 KB
/
index.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
const { sha3, BN } = require("web3-utils");
const abiCoder = require("web3-eth-abi");
const state = {
savedABIs: [],
methodIDs: {},
};
function _getABIs() {
return state.savedABIs;
}
function _addABI(abiArray) {
if (Array.isArray(abiArray)) {
// Iterate new abi to generate method id"s
abiArray.map(function(abi) {
if (abi.name) {
let signature;
if (abi.type === "event") {
signature = abiCoder.encodeFunctionSignature(abi);
} else {
signature = abiCoder.encodeEventSignature(abi);
}
if (abi.type === "event") {
state.methodIDs[signature.slice(2)] = abi;
} else {
state.methodIDs[signature.slice(2, 10)] = abi;
}
}
});
state.savedABIs = state.savedABIs.concat(abiArray);
} else {
throw new Error("Expected ABI array, got " + typeof abiArray);
}
}
function _removeABI(abiArray) {
if (Array.isArray(abiArray)) {
// Iterate new abi to generate method id"s
abiArray.map(function(abi) {
if (abi.name) {
let signature;
if (abi.type === "event") {
signature = abiCoder.encodeFunctionSignature(abi);
} else {
signature = abiCoder.encodeEventSignature(abi);
}
if (abi.type === "event") {
if (state.methodIDs[signature.slice(2)]) {
delete state.methodIDs[signature.slice(2)];
}
} else {
if (state.methodIDs[signature.slice(2, 10)]) {
delete state.methodIDs[signature.slice(2, 10)];
}
}
}
});
} else {
throw new Error("Expected ABI array, got " + typeof abiArray);
}
}
function _getMethodIDs() {
return state.methodIDs;
}
function _isArrayKey(key) {
return /^\d+$/.test(key) || key === "__length__";
}
function _isArray(obj) {
if (!Array.isArray(obj)) {
return false;
}
return Object.keys(obj).every(value => {
return _isArrayKey(value);
});
}
function _cleanObject(obj) {
if (_isArray(obj)) {
// Return an array with all of the elements recursively cleaned.
const cleanedArray = [];
for (const elt of obj) {
cleanedArray.push(_cleanObject(elt));
}
return cleanedArray;
} else if (typeof obj === "object" && obj !== null) {
// Remove all of the array-style keys, and clean the values for the non array-style keys.
const cleanedObject = {};
for (const [key, value] of Object.entries(obj)) {
// Only keep keys that are not array keys.
if (!_isArrayKey(key)) {
cleanedObject[key] = _cleanObject(value);
}
}
return cleanedObject;
} else {
// If the object is not an array or an object, we assume it's a primative and return it.
return obj;
}
}
function _decodeMethod(data) {
const methodID = data.slice(2, 10);
const abiItem = state.methodIDs[methodID];
if (abiItem) {
return {
name: abiItem.name,
params: _cleanObject(abiCoder.decodeParameters(abiItem.inputs, data.slice(10)))
};
}
}
function _decodeLogs(logs) {
return logs.filter(log => log.topics.length > 0).map((logItem) => {
const methodID = logItem.topics[0].slice(2);
const method = state.methodIDs[methodID];
if (method) {
const logData = logItem.data;
let decodedParams = [];
let dataIndex = 0;
let topicsIndex = 1;
let dataTypes = [];
method.inputs.map(function(input) {
if (!input.indexed) {
dataTypes.push(input.type);
}
});
const decodedData = abiCoder.decodeParameters(
dataTypes,
logData.slice(2)
);
// Loop topic and data to get the params
method.inputs.map(function(param) {
let decodedP = {
name: param.name,
type: param.type,
};
if (param.indexed) {
decodedP.value = logItem.topics[topicsIndex];
topicsIndex++;
} else {
decodedP.value = decodedData[dataIndex];
dataIndex++;
}
if (param.type === "address") {
decodedP.value = decodedP.value.toLowerCase();
// 42 because len(0x) + 40
if (decodedP.value.length > 42) {
let toRemove = decodedP.value.length - 42;
let temp = decodedP.value.split("");
temp.splice(2, toRemove);
decodedP.value = temp.join("");
}
}
if (
param.type === "uint256" ||
param.type === "uint8" ||
param.type === "int"
) {
// ensure to remove leading 0x for hex numbers
if (typeof decodedP.value === "string" && decodedP.value.startsWith("0x")) {
decodedP.value = new BN(decodedP.value.slice(2), 16).toString(10);
} else {
decodedP.value = new BN(decodedP.value).toString(10);
}
}
decodedParams.push(decodedP);
});
return {
name: method.name,
events: decodedParams,
address: logItem.address,
};
}
});
}
module.exports = {
getABIs: _getABIs,
addABI: _addABI,
getMethodIDs: _getMethodIDs,
decodeMethod: _decodeMethod,
decodeLogs: _decodeLogs,
removeABI: _removeABI,
};