Skip to content

Commit

Permalink
util: make EncodeBase64 consume Spans
Browse files Browse the repository at this point in the history
Adapted from btc@e2aa1a585a83971639572cd2c84565ec360deac9
  • Loading branch information
furszy committed Jul 6, 2021
1 parent c36754f commit a1f3aed
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/messagesigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool CHashSigner::VerifyHash(const uint256& hash, const CKeyID& keyID, const std
if(pubkeyFromSig.GetID() != keyID) {
strErrorRet = strprintf("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s",
EncodeDestination(keyID), EncodeDestination(pubkeyFromSig.GetID()),
hash.ToString(), EncodeBase64(&vchSig[0], vchSig.size()));
hash.ToString(), EncodeBase64(vchSig));
return false;
}

Expand Down Expand Up @@ -131,6 +131,6 @@ bool CSignedMessage::CheckSignature(const CKeyID& keyID) const

std::string CSignedMessage::GetSignatureBase64() const
{
return EncodeBase64(&vchSig[0], vchSig.size());
return EncodeBase64(vchSig);
}

2 changes: 1 addition & 1 deletion src/qt/pivx/settings/settingssignmessagewidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void SettingsSignMessageWidgets::onSignMessageButtonSMClicked()
ui->statusLabel_SM->setStyleSheet("QLabel { color: green; }");
ui->statusLabel_SM->setText(QString("<nobr>") + tr("Message signed.") + QString("</nobr>"));

ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(&vchSig[0], vchSig.size())));
ui->signatureOut_SM->setText(QString::fromStdString(EncodeBase64(vchSig)));
}

void SettingsSignMessageWidgets::onVerifyMessage()
Expand Down
8 changes: 4 additions & 4 deletions src/utilstrencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ std::vector<unsigned char> ParseHex(const std::string& str)
return ParseHex(str.c_str());
}

std::string EncodeBase64(const unsigned char* pch, size_t len)
std::string EncodeBase64(Span<const unsigned char> input)
{
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

std::string str;
str.reserve(((len + 2) / 3) * 4);
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, pch, pch + len);
str.reserve(((input.size() + 2) / 3) * 4);
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
while (str.size() % 4) str += '=';
return str;
}

std::string EncodeBase64(const std::string& str)
{
return EncodeBase64((const unsigned char*)str.c_str(), str.size());
return EncodeBase64(MakeUCharSpan(str));
}

std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
Expand Down
3 changes: 2 additions & 1 deletion src/utilstrencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define BITCOIN_UTILSTRENCODINGS_H

#include "support/allocators/secure.h"
#include "span.h"
#include <algorithm>
#include <stdint.h>
#include <string>
Expand Down Expand Up @@ -54,7 +55,7 @@ signed char HexDigit(char c);
bool IsHex(const std::string& str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
std::string DecodeBase64(const std::string& str);
std::string EncodeBase64(const unsigned char* pch, size_t len);
std::string EncodeBase64(Span<const unsigned char> input);
std::string EncodeBase64(const std::string& str);
std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
std::string DecodeBase32(const std::string& str);
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@ UniValue signmessage(const JSONRPCRequest& request)
if (!key.SignCompact(ss.GetHash(), vchSig))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");

return EncodeBase64(&vchSig[0], vchSig.size());
return EncodeBase64(vchSig);
}

UniValue getreceivedbyaddress(const JSONRPCRequest& request)
Expand Down

0 comments on commit a1f3aed

Please sign in to comment.