forked from Consensys/abi-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (157 loc) · 4.65 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
const SolidityCoder = require("web3/lib/solidity/coder.js");
const Web3 = require('web3');
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){
const signature = new Web3().sha3(abi.name + "(" + abi.inputs.map(function(input) {return input.type;}).join(",") + ")");
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){
const signature = new Web3().sha3(abi.name + "(" + abi.inputs.map(function(input) {return input.type;}).join(",") + ")");
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 _decodeMethod(data) {
const methodID = data.slice(2, 10);
const abiItem = state.methodIDs[methodID];
if (abiItem) {
const params = abiItem.inputs.map(function (item) { return item.type; });
let decoded = SolidityCoder.decodeParams(params, data.slice(10));
return {
name: abiItem.name,
params: decoded.map(function (param, index) {
let parsedParam = param;
const isUint = abiItem.inputs[index].type.indexOf("uint") == 0;
const isInt = abiItem.inputs[index].type.indexOf("int") == 0;
if (isUint || isInt) {
const isArray = Array.isArray(param);
if (isArray) {
parsedParam = param.map(val => new Web3().toBigNumber(val).toString());
} else {
parsedParam = new Web3().toBigNumber(param).toString();
}
}
return {
name: abiItem.inputs[index].name,
value: parsedParam,
type: abiItem.inputs[index].type
};
})
}
}
}
function padZeros (address) {
var formatted = address;
if (address.indexOf('0x') != -1) {
formatted = address.slice(2);
}
if (formatted.length < 40) {
while (formatted.length < 40) formatted = "0" + formatted;
}
return "0x" + formatted;
};
function _decodeLogs(logs, keepBlockNumber, keepTxHash) {
return logs.map(function(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 = SolidityCoder.decodeParams(dataTypes, logData.slice(2));
// Loop topic and data to get the params
method.inputs.map(function (param) {
var 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 = padZeros(new Web3().toBigNumber(decodedP.value).toString(16));
}
else if(param.type == "uint256" || param.type == "uint8" || param.type == "int" ){
decodedP.value = new Web3().toBigNumber(decodedP.value).toString(10);
}
decodedParams.push(decodedP);
});
var eventObj = {
name: method.name,
events: decodedParams,
address: logItem.address
};
if (keepBlockNumber) {
eventObj['blockNumber'] = logItem.blockNumber;
}
if (keepTxHash) {
eventObj['transactionHash'] = logItem.transactionHash;
}
return eventObj;
}
});
}
module.exports = {
getABIs: _getABIs,
addABI: _addABI,
getMethodIDs: _getMethodIDs,
decodeMethod: _decodeMethod,
decodeLogs: _decodeLogs,
removeABI: _removeABI
};