Skip to content

Commit 3296a3b

Browse files
committed
Generalize ConvertBits
1 parent b225010 commit 3296a3b

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/key_io.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class DestinationEncoder : public boost::static_visitor<std::string>
4343
std::string operator()(const WitnessV0KeyHash& id) const
4444
{
4545
std::vector<unsigned char> data = {0};
46-
ConvertBits<8, 5, true>(data, id.begin(), id.end());
46+
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
4747
return bech32::Encode(m_params.Bech32HRP(), data);
4848
}
4949

5050
std::string operator()(const WitnessV0ScriptHash& id) const
5151
{
5252
std::vector<unsigned char> data = {0};
53-
ConvertBits<8, 5, true>(data, id.begin(), id.end());
53+
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
5454
return bech32::Encode(m_params.Bech32HRP(), data);
5555
}
5656

@@ -60,7 +60,7 @@ class DestinationEncoder : public boost::static_visitor<std::string>
6060
return {};
6161
}
6262
std::vector<unsigned char> data = {(unsigned char)id.version};
63-
ConvertBits<8, 5, true>(data, id.program, id.program + id.length);
63+
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
6464
return bech32::Encode(m_params.Bech32HRP(), data);
6565
}
6666

@@ -94,7 +94,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
9494
// Bech32 decoding
9595
int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16)
9696
// The rest of the symbols are converted witness program bytes.
97-
if (ConvertBits<5, 8, false>(data, bech.second.begin() + 1, bech.second.end())) {
97+
if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) {
9898
if (version == 0) {
9999
{
100100
WitnessV0KeyHash keyid;

src/utilstrencodings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
151151

152152
/** Convert from one power-of-2 number base to another. */
153153
template<int frombits, int tobits, bool pad, typename O, typename I>
154-
bool ConvertBits(O& out, I it, I end) {
154+
bool ConvertBits(const O& outfn, I it, I end) {
155155
size_t acc = 0;
156156
size_t bits = 0;
157157
constexpr size_t maxv = (1 << tobits) - 1;
@@ -161,12 +161,12 @@ bool ConvertBits(O& out, I it, I end) {
161161
bits += frombits;
162162
while (bits >= tobits) {
163163
bits -= tobits;
164-
out.push_back((acc >> bits) & maxv);
164+
outfn((acc >> bits) & maxv);
165165
}
166166
++it;
167167
}
168168
if (pad) {
169-
if (bits) out.push_back((acc << (tobits - bits)) & maxv);
169+
if (bits) outfn((acc << (tobits - bits)) & maxv);
170170
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
171171
return false;
172172
}

0 commit comments

Comments
 (0)