-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethabi.cpp
229 lines (174 loc) · 5.33 KB
/
ethabi.cpp
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
#include <array>
#include <vector>
#ifdef _DEBUG
#include <iostream>
#endif //_DEBUG
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <ethabi.hpp>
using namespace std;
namespace eth_interface
{
string
ethabi(string const &args)
{
string result;
array<char, IPC_BUFFER_LENGTH> pipe_buffer;
#ifdef _DEBUG
cout << "ethabi(): Requested '" << args << "'" << endl;
#endif //_DEBUG
FILE *ethabi_pipe = popen(("ethabi " + args).c_str(), "r");
if (ethabi_pipe == NULL)
{
// Failed to open pipe to ethabi -- is the binary installed and in $PATH?
throw ResourceRequestFailedException(
"ethabi(): Failed to popen() pipe to ethabi binary. "
"Is the binary installed and in the $PATH environment variable?");
}
while (fgets(pipe_buffer.data(), IPC_BUFFER_LENGTH, ethabi_pipe) != NULL)
{
result += pipe_buffer.data();
}
if (pclose(ethabi_pipe) != 0)
{
throw ResourceRequestFailedException(
"ethabi(): ethabi binary exited with a failure status!\n"
"Args: "
+ args + "\n"
"Output: "
+ result + "\n");
}
#ifdef _DEBUG
cout << "ethabi(): Call successful!" << endl;
cout << "ethabi(): Returning: " << boost::trim_copy(result) << endl;
#endif //_DEBUG
return boost::trim_copy(result);
;
}
unordered_map<string, string>
ethabi_decode_log(
string const &abiFile,
string const &eventName,
vector<string> &topics,
string const &data)
{
string query, responce;
vector<string> lines;
unordered_map<string, string> parsedLog;
string topic_query;
for (vector<string>::iterator iter = topics.begin(); iter != topics.end(); ++iter)
{
topic_query += "-l " + *iter + " ";
}
query = "decode log " + abiFile + " " + eventName + " " + topic_query + data;
responce = ethabi(query);
boost::split(lines, responce, boost::is_any_of("\n"));
for (vector<string>::iterator iter = lines.begin(); iter != lines.end(); ++iter)
{
if (boost::trim_copy(*iter).compare("") == 0)
{
continue;
}
string key = (*iter).substr(0, (*iter).find_first_of(" "));
string value = (*iter).substr((*iter).find_first_of(" ") + 1);
parsedLog[key] = value;
}
return parsedLog;
}
string
ethabi_decode_result(string const &abiFile, string const &eventName, string const &data)
{
string query, responce;
query = "decode function " + abiFile + " " + eventName + " " + data; //TODO: Check data does not have "0x"...
responce = ethabi(query);
#ifdef _DEBUG
cout << "ethabi_decode_result() responce:" << responce << endl;
#endif //_DEBUG
if (responce.find_first_of(" ") == string::npos)
{
return "";
}
return responce.substr(responce.find_first_of(" ") + 1);
}
vector<string>
ethabi_decode_results(string const &abiFile, string const &eventName, string const &data)
{
vector<string> array;
string query, responce;
query = "decode function " + abiFile + " " + eventName + " " + data; //TODO: Check data does not have "0x"...
responce = ethabi(query);
#ifdef _DEBUG
cout << "ethabi_decode_results() responce:" << responce << endl;
#endif //_DEBUG
boost::split(array, responce, boost::is_any_of("\n"));
for (vector<string>::iterator it = array.begin(); it != array.end(); ++it)
{
if (responce.find_first_of(" ") == string::npos)
{
*it = "";
}
else
{
*it = (*it).substr((*it).find_first_of(" ") + 1);
}
}
return array;
}
vector<string>
ethabi_decode_string_array(string const &abiFile, string const &eventName, string const &data)
{
vector<string> array;
string responce;
responce = boost::trim_copy(ethabi_decode_result(abiFile, eventName, data));
responce = responce.substr(1, responce.length() - 2);
boost::split(array, responce, boost::is_any_of(","));
return array;
}
vector<uint32_t>
ethabi_decode_uint32_array(string const &abiFile, string const &eventName, string const &data)
{
vector<string> arrayStr;
vector<uint32_t> arrayUInt32;
string responce;
responce = boost::trim_copy(ethabi_decode_result(abiFile, eventName, data));
responce = responce.substr(1, responce.length() - 2);
boost::split(arrayStr, responce, boost::is_any_of(","));
for (vector<string>::iterator it = arrayStr.begin(); it != arrayStr.end(); ++it)
{
arrayUInt32.push_back(strtoul(((*it).c_str()), nullptr, 16));
}
return arrayUInt32;
}
vector<uint8_t>
ethabi_decode_uint8_array(string const &abiFile, string const &eventName, string const &data)
{
vector<string> arrayStr;
vector<uint8_t> arrayUInt8;
string responce;
responce = boost::trim_copy(ethabi_decode_result(abiFile, eventName, data));
responce = responce.substr(1, responce.length() - 2);
boost::split(arrayStr, responce, boost::is_any_of(","));
for (vector<string>::iterator it = arrayStr.begin(); it != arrayStr.end(); ++it)
{
arrayUInt8.push_back(strtoul(((*it).c_str()), nullptr, 16));
}
return arrayUInt8;
}
vector<uint8_t>
ethabi_decode_3d_uint8_array(string const &abiFile, string const &eventName, string const &data)
{
vector<string> arrayStr;
vector<uint8_t> arrayUInt8;
string responce;
responce = boost::trim_copy(ethabi_decode_result(abiFile, eventName, data));
boost::replace_all(responce, "[", "");
boost::replace_all(responce, "]", "");
boost::split(arrayStr, responce, boost::is_any_of(","));
for (vector<string>::iterator it = arrayStr.begin(); it != arrayStr.end(); ++it)
{
arrayUInt8.push_back(strtoul(((*it).c_str()), nullptr, 16));
}
return arrayUInt8;
}
} //namespace