Skip to content

Commit

Permalink
Cleanup: remove unneeded copies
Browse files Browse the repository at this point in the history
CBLSWrapper::ToByteVector does not return a CDataStream
  • Loading branch information
random-zebra authored and furszy committed Jan 28, 2022
1 parent cd2ebeb commit 3d2a71a
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/bls/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ static std::string EncodeBLS(const CChainParams& params,
CChainParams::Bech32Type type)
{
if (!key.IsValid()) return "";
auto vec{key.ToByteVector()};
// ConvertBits requires unsigned char, but CDataStream uses char
std::vector<unsigned char> ss(vec.begin(), vec.end());
std::vector<unsigned char> vec{key.ToByteVector()};
std::vector<unsigned char> data;
// See calculation comment below
data.reserve((ss.size() * 8 + 4) / 5);
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, ss.begin(), ss.end());
data.reserve((vec.size() * 8 + 4) / 5);
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, vec.begin(), vec.end());
auto res = bech32::Encode(params.Bech32HRP(type), data);
memory_cleanse(ss.data(), ss.size());
memory_cleanse(vec.data(), vec.size());
memory_cleanse(data.data(), data.size());
return res;
}
Expand Down

0 comments on commit 3d2a71a

Please sign in to comment.