-
Notifications
You must be signed in to change notification settings - Fork 175
/
utility.cpp
174 lines (149 loc) · 4.97 KB
/
utility.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
/**
* Copyright (c) 2011-2017 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/explorer/utility.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
#include <cstdint>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
#include <vector>
#include <boost/date_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <bitcoin/client.hpp>
#include <bitcoin/explorer/command.hpp>
#include <bitcoin/explorer/define.hpp>
using namespace bc::client;
using boost::filesystem::path;
namespace libbitcoin {
namespace explorer {
connection_type get_connection(const command& command)
{
const auto public_key = command.get_server_server_public_key_setting();
const auto private_key = command.get_server_client_private_key_setting();
const auto timeout = command.get_server_connect_timeout_seconds_setting();
connection_type connection;
connection.retries = command.get_server_connect_retries_setting();
connection.timeout_seconds = timeout;
connection.server = command.get_server_url_setting();
connection.socks = command.get_server_socks_proxy_setting();
connection.server_public_key = public_key;
connection.client_private_key = private_key;
return connection;
}
// The key may be invalid, caller may test for null secret.
ec_secret new_key(const data_chunk& seed)
{
const wallet::hd_private key(seed);
return key.secret();
}
// Not testable due to lack of random engine injection.
data_chunk new_seed(size_t bit_length)
{
size_t fill_seed_size = bit_length / byte_bits;
data_chunk seed(fill_seed_size);
pseudo_random_fill(seed);
return seed;
}
string_list numbers_to_strings(const chain::point::indexes& indexes)
{
string_list list;
for (const auto index: indexes)
list.push_back(std::to_string(index));
return list;
}
// TODO: switch to binary for raw (primitive) reads in windows.
std::string read_stream(std::istream& stream)
{
std::istreambuf_iterator<char> first(stream), last;
std::string result(first, last);
return result;
}
name_value_pairs split_pairs(const std::vector<std::string> tokens,
const std::string delimiter)
{
name_value_pairs list;
for (const auto& token: tokens)
{
const auto words = split(token, delimiter);
const auto& left = words[0];
std::string right;
if (words.size() > 1u)
right = words[1];
list.push_back(std::make_pair(left, right));
}
return list;
}
bool starts_with(const std::string& value, const std::string& prefix)
{
return bc::starts_with(value.begin(), value.end(), prefix);
}
// This verifies the checksum.
bool unwrap(wallet::wrapped_data& data, const data_slice& wrapped)
{
if (!verify_checksum(wrapped))
return false;
data.version = wrapped.data()[0];
const auto payload_begin = std::begin(wrapped) + 1;
const auto checksum_begin = std::end(wrapped) - checksum_size;
data.payload.resize(checksum_begin - payload_begin);
std::copy(payload_begin, checksum_begin, data.payload.begin());
data.checksum = from_little_endian_unsafe<uint32_t>(checksum_begin);
return true;
}
// This recalculates the checksum, ignoring what is in data.checksum.
data_chunk wrap(const wallet::wrapped_data& data)
{
data_chunk bytes{ data.version };
extend_data(bytes, data.payload);
append_checksum(bytes);
return bytes;
}
// We aren't yet using a reader, although it is possible using ptree.
std::ostream& write_stream(std::ostream& output, const pt::ptree& tree,
encoding_engine engine)
{
switch (engine)
{
case encoding_engine::json:
pt::write_json(output, tree);
break;
case encoding_engine::xml:
pt::write_xml(output, tree);
// property tree XML serialization doesn't terminate the string.
output << std::endl;
break;
default:
pt::write_info(output, tree);
break;
}
return output;
}
} // namespace explorer
} // namespace libbitcoin