forked from stellar/stellar-core
/
dumpxdr.cpp
424 lines (389 loc) · 11.2 KB
/
dumpxdr.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "main/dumpxdr.h"
#include "crypto/Hex.h"
#include "crypto/SecretKey.h"
#include "transactions/SignatureUtils.h"
#include "util/Decoder.h"
#include "util/Fs.h"
#include "util/XDROperators.h"
#include "util/XDRStream.h"
#include "util/format.h"
#include "util/types.h"
#include <cereal/archives/json.hpp>
#include <cereal/cereal.hpp>
#include <iostream>
#include <regex>
#include <xdrpp/printer.h>
#if !defined(USE_TERMIOS) && !MSVC
#define HAVE_TERMIOS 1
#endif
#if HAVE_TERMIOS
extern "C" {
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
}
#endif // HAVE_TERMIOS
#if MSVC
#include <io.h>
#define isatty _isatty
#endif // MSVC
using namespace std::placeholders;
template <uint32_t N>
void
cereal_override(cereal::JSONOutputArchive& ar, const xdr::opaque_array<N>& s,
const char* field)
{
xdr::archive(ar, stellar::binToHex(stellar::ByteSlice(s.data(), s.size())),
field);
}
// We still need one explicit composite-container override because cereal
// appears to process arrays-of-arrays internally, without calling back through
// an NVP adaptor.
template <uint32_t N>
void
cereal_override(cereal::JSONOutputArchive& ar,
const xdr::xarray<stellar::Hash, N>& s, const char* field)
{
std::vector<std::string> tmp;
for (auto const& h : s)
{
tmp.emplace_back(
stellar::binToHex(stellar::ByteSlice(h.data(), h.size())));
}
xdr::archive(ar, tmp, field);
}
template <uint32_t N>
void
cereal_override(cereal::JSONOutputArchive& ar, const xdr::opaque_vec<N>& s,
const char* field)
{
xdr::archive(ar, stellar::binToHex(stellar::ByteSlice(s.data(), s.size())),
field);
}
void
cereal_override(cereal::JSONOutputArchive& ar, const stellar::PublicKey& s,
const char* field)
{
xdr::archive(ar, stellar::KeyUtils::toStrKey<stellar::PublicKey>(s), field);
}
void
cereal_override(cereal::JSONOutputArchive& ar, const stellar::Asset& s,
const char* field)
{
xdr::archive(ar, stellar::assetToString(s), field);
}
template <typename T>
void
cereal_override(cereal::JSONOutputArchive& ar, const xdr::pointer<T>& t,
const char* field)
{
// We tolerate a little information-loss here collapsing *T into T for
// the non-null case, and use JSON 'null' for the null case. This reads
// much better than the thing JSONOutputArchive does with PtrWrapper.
if (t)
{
xdr::archive(ar, *t, field);
}
else
{
ar.setNextName(field);
ar.writeName();
ar.saveValue(nullptr);
}
}
// This has to be included _after_ the cereal_override overloads above,
// otherwise some interplay of name lookup and visibility during the
// enable_if call in the cereal adaptor fails to find them.
#include <xdrpp/cereal.h>
namespace stellar
{
std::string
xdr_printer(const PublicKey& pk)
{
return KeyUtils::toStrKey<PublicKey>(pk);
}
template <typename T>
void
dumpstream(XDRInputFileStream& in, bool json)
{
T tmp;
if (json)
{
cereal::JSONOutputArchive archive(std::cout);
archive.makeArray();
while (in && in.readOne(tmp))
{
archive(tmp);
}
}
else
{
while (in && in.readOne(tmp))
{
std::cout << xdr::xdr_to_string(tmp) << std::endl;
}
}
}
void
dumpXdrStream(std::string const& filename, bool json)
{
std::regex rx(
".*(ledger|bucket|transactions|results|scp)-[[:xdigit:]]+\\.xdr");
std::smatch sm;
if (std::regex_match(filename, sm, rx))
{
XDRInputFileStream in;
in.open(filename);
if (sm[1] == "ledger")
{
dumpstream<LedgerHeaderHistoryEntry>(in, json);
}
else if (sm[1] == "bucket")
{
dumpstream<BucketEntry>(in, json);
}
else if (sm[1] == "transactions")
{
dumpstream<TransactionHistoryEntry>(in, json);
}
else if (sm[1] == "results")
{
dumpstream<TransactionHistoryResultEntry>(in, json);
}
else
{
assert(sm[1] == "scp");
dumpstream<SCPHistoryEntry>(in, json);
}
}
else
{
throw std::runtime_error("unrecognized XDR filename");
}
}
#define throw_perror(msg) \
do \
{ \
throw std::runtime_error(std::string(msg) + ": " + \
xdr_strerror(errno)); \
} while (0)
static xdr::opaque_vec<>
readFile(const std::string& filename, bool base64 = false)
{
using namespace std;
ostringstream input;
if (filename == "-" || filename.empty())
input << cin.rdbuf();
else
{
ifstream file(filename.c_str());
if (!file)
throw_perror(filename);
input << file.rdbuf();
}
string ret;
if (base64)
decoder::decode_b64(input.str(), ret);
else
ret = input.str();
return {ret.begin(), ret.end()};
}
template <typename T>
void
printOneXdr(xdr::opaque_vec<> const& o, std::string const& desc)
{
T tmp;
xdr::xdr_from_opaque(o, tmp);
std::cout << xdr::xdr_to_string(tmp, desc.c_str()) << std::endl;
}
void
printXdr(std::string const& filename, std::string const& filetype, bool base64)
{
// need to use this pattern as there is no good way to get a human readable
// type name from a type
#define PRINTONEXDR(T) std::bind(printOneXdr<T>, _1, #T)
auto dumpMap =
std::map<std::string, std::function<void(xdr::opaque_vec<> const&)>>{
{"ledgerheader", PRINTONEXDR(LedgerHeader)},
{"meta", PRINTONEXDR(TransactionMeta)},
{"result", PRINTONEXDR(TransactionResult)},
{"resultpair", PRINTONEXDR(TransactionResultPair)},
{"tx", PRINTONEXDR(TransactionEnvelope)},
{"txfee", PRINTONEXDR(LedgerEntryChanges)}};
#undef PRINTONEXDR
try
{
auto d = readFile(filename, base64);
if (filetype == "auto")
{
bool processed = false;
for (auto const& it : dumpMap)
{
try
{
it.second(d);
processed = true;
break;
}
catch (xdr::xdr_runtime_error)
{
}
}
if (!processed)
{
throw std::invalid_argument("Could not detect type");
}
}
else
{
auto it = dumpMap.find(filetype);
if (it != dumpMap.end())
{
it->second(d);
}
else
{
throw std::invalid_argument(
fmt::format("unknown filetype {}", filetype));
}
}
}
catch (const std::exception& e)
{
std::cerr << "Could not decode with type '" << filetype
<< "' : " << e.what() << std::endl;
}
}
#if HAVE_TERMIOS
static int
set_echo_flag(int fd, bool flag)
{
struct termios tios;
if (tcgetattr(fd, &tios))
return -1;
int old = !!(tios.c_lflag & ECHO);
if (flag)
tios.c_lflag |= ECHO;
else
tios.c_lflag &= ~ECHO;
return tcsetattr(fd, TCSAFLUSH, &tios) == 0 ? old : -1;
}
#endif
#if __GNUC__ >= 4 && __GLIBC__ >= 2
// Realistically, if a write fails in one of these utility functions,
// it should kill us with SIGPIPE. Hence, we don't need to check the
// return value of write. However, GCC and glibc are now extremely
// aggressive with warn_unused_result, to the point that a cast to
// void won't do anything. To work around the problem, we define an
// equivalent function without the warn_unused_result attribute.
constexpr ssize_t (&mywrite)(int, const void*, size_t) = ::write;
#else // not (gcc 4+ and glibc)
#define mywrite write
#endif // not (gcc 4+ and glibc)
std::string
readSecret(const std::string& prompt, bool force_tty)
{
std::string ret;
if (!isatty(0) && !force_tty)
{
std::getline(std::cin, ret);
return ret;
}
#if !HAVE_TERMIOS
// Sorry, Windows. Might need to use something like this:
// https://msdn.microsoft.com/en-us/library/ms683167(VS.85).aspx
// http://stackoverflow.com/questions/1413445/read-a-password-from-stdcin/1455007#1455007
throw std::invalid_argument("reading secrets from terminal not supported");
#else
struct cleanup
{
std::function<void()> action_;
~cleanup()
{
if (action_)
action_();
}
};
int fd = open("/dev/tty", O_RDWR);
if (fd == -1)
throw_perror("/dev/tty");
cleanup clean_fd;
clean_fd.action_ = [fd] { close(fd); };
cleanup clean_echo;
int oldecho = set_echo_flag(fd, false);
if (oldecho == -1)
throw_perror("cannot disable terminal echo");
else if (oldecho)
clean_echo.action_ = [fd] { set_echo_flag(fd, true); };
mywrite(fd, prompt.c_str(), prompt.size());
char buf[256];
ssize_t n = read(fd, buf, sizeof(buf));
if (n < 0)
throw_perror("read secret key");
mywrite(fd, "\n", 1);
char* p = static_cast<char*>(std::memchr(buf, '\n', sizeof(buf)));
if (!p)
throw std::runtime_error("line too long");
ret.assign(buf, p - buf);
memset(buf, 0, sizeof(buf));
return ret;
#endif
}
void
signtxn(std::string const& filename, std::string netId, bool base64)
{
using namespace std;
try
{
if (netId.empty())
netId = getenv("STELLAR_NETWORK_ID");
if (netId.empty())
throw std::runtime_error("missing --netid argument or "
"STELLAR_NETWORK_ID environment variable");
const bool txn_stdin = filename == "-" || filename.empty();
if (!base64 && isatty(1))
throw std::runtime_error(
"Refusing to write binary transaction to terminal");
TransactionEnvelope txenv;
xdr::xdr_from_opaque(readFile(filename, base64), txenv);
if (txenv.signatures.size() == txenv.signatures.max_size())
throw std::runtime_error(
"Evelope already contains maximum number of signatures");
SecretKey sk(SecretKey::fromStrKeySeed(
readSecret("Secret key seed: ", txn_stdin)));
TransactionSignaturePayload payload;
payload.networkId = sha256(netId);
payload.taggedTransaction.type(ENVELOPE_TYPE_TX);
payload.taggedTransaction.tx() = txenv.tx;
txenv.signatures.emplace_back(
SignatureUtils::getHint(sk.getPublicKey().ed25519()),
sk.sign(sha256(xdr::xdr_to_opaque(payload))));
auto out = xdr::xdr_to_opaque(txenv);
if (base64)
cout << decoder::encode_b64(out) << std::endl;
else
cout.write(reinterpret_cast<char*>(out.data()), out.size());
}
catch (const std::exception& e)
{
cerr << e.what() << endl;
}
}
void
priv2pub()
{
using namespace std;
try
{
SecretKey sk(
SecretKey::fromStrKeySeed(readSecret("Secret key seed: ", false)));
cout << sk.getStrKeyPublic() << endl;
}
catch (const std::exception& e)
{
cerr << e.what() << endl;
}
}
}