-
Notifications
You must be signed in to change notification settings - Fork 174
/
utility.cpp
183 lines (156 loc) · 4.82 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
175
176
177
178
179
180
181
182
183
/**
* Copyright (c) 2011-2014 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin-explorer.
*
* libbitcoin-explorer is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License with
* additional permissions to the one published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. For more information see LICENSE.
*
* 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 <iomanip>
#include <iostream>
#include <random>
#include <cstdint>
#include <string>
#include <vector>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/date_time/posix_time/posix_time.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/bitcoin.hpp>
#include <bitcoin/explorer/define.hpp>
using namespace boost::posix_time;
namespace libbitcoin {
namespace explorer {
bool is_base2(const std::string& text)
{
for (const auto& character : text)
{
if (character != '0' && character != '1')
return false;
}
return true;
}
// The key may be invalid, caller may test for null secret.
ec_secret new_key(data_slice seed)
{
// The testnet value is not relevant to the secret.
constexpr bool testnet = false;
// This is retained in order to preserve test cases and docs.
// Using HD key generation because we don't have helper for EC.
const hd_private_key hd_key(seed, testnet);
if (!hd_key.valid())
{
std::cerr << "You just won the lottery!" << std::endl;
return ec_secret();
}
return hd_key.private_key();
}
// Not testable due to lack of random engine injection.
data_chunk new_seed(size_t bitlength)
{
size_t fill_seed_size = bitlength / byte_bits;
data_chunk seed(fill_seed_size);
random_fill(seed);
return seed;
}
// Not testable due to lack of clock injection.
ptime now()
{
using namespace boost::posix_time;
ptime local_time_in_seconds(second_clock::local_time());
return local_time_in_seconds;
}
std::vector<std::string> numbers_to_strings(const index_list& indexes)
{
std::vector<std::string> stringlist;
for (const auto& index: indexes)
stringlist.push_back(std::to_string(index));
return stringlist;
}
// Not testable due to lack of random engine injection.
void random_fill(data_chunk& chunk)
{
std::random_device random;
std::default_random_engine engine(random());
for (uint8_t& byte: chunk)
byte = engine() % std::numeric_limits<uint8_t>::max();
}
std::string read_stream(std::istream& stream)
{
BC_SET_BINARY_FILE_MODE(true);
std::istreambuf_iterator<char> first(stream), last;
std::string result(first, last);
BC_SET_BINARY_FILE_MODE(false);
return result;
}
script_type script_to_raw_data_script(const script_type& script)
{
return raw_data_script(save_script(script));
}
// Not unit testable (sleep).
void sleep_ms(uint32_t milliseconds)
{
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
bool starts_with(const std::string& value, const std::string& prefix)
{
try
{
return boost::istarts_with(value, prefix);
}
catch (boost::bad_lexical_cast)
{
return false;
}
}
void trim_left(std::string& value, const std::string& chars)
{
boost::trim_left_if(value, boost::is_any_of(chars));
}
bool unwrap(wrapped_data& data, data_slice wrapped)
{
return bc::unwrap(data.version, data.payload, data.checksum, wrapped);
}
data_chunk wrap(const wrapped_data& data)
{
return bc::wrap(data.version, data.payload);
}
// 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