util: make EncodeBase64 consume Spans

This commit is contained in:
Sebastian Falbesoner 2020-08-07 20:25:42 +02:00
parent 2bc207190e
commit e2aa1a585a
4 changed files with 11 additions and 11 deletions

View File

@ -1300,7 +1300,7 @@ UniValue combinepsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << merged_psbt; ssTx << merged_psbt;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue finalizepsbt(const JSONRPCRequest& request) UniValue finalizepsbt(const JSONRPCRequest& request)
@ -1435,7 +1435,7 @@ UniValue createpsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx; ssTx << psbtx;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue converttopsbt(const JSONRPCRequest& request) UniValue converttopsbt(const JSONRPCRequest& request)
@ -1502,7 +1502,7 @@ UniValue converttopsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx; ssTx << psbtx;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue utxoupdatepsbt(const JSONRPCRequest& request) UniValue utxoupdatepsbt(const JSONRPCRequest& request)
@ -1590,7 +1590,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx; ssTx << psbtx;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue joinpsbts(const JSONRPCRequest& request) UniValue joinpsbts(const JSONRPCRequest& request)
@ -1683,7 +1683,7 @@ UniValue joinpsbts(const JSONRPCRequest& request)
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << shuffled_psbt; ssTx << shuffled_psbt;
return EncodeBase64((unsigned char*)ssTx.data(), ssTx.size()); return EncodeBase64(MakeUCharSpan(ssTx));
} }
UniValue analyzepsbt(const JSONRPCRequest& request) UniValue analyzepsbt(const JSONRPCRequest& request)

View File

@ -64,7 +64,7 @@ bool MessageSign(
return false; return false;
} }
signature = EncodeBase64(signature_bytes.data(), signature_bytes.size()); signature = EncodeBase64(signature_bytes);
return true; return true;
} }

View File

@ -126,20 +126,20 @@ void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
hostOut = in; hostOut = in;
} }
std::string EncodeBase64(const unsigned char* pch, size_t len) std::string EncodeBase64(Span<const unsigned char> input)
{ {
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string str; std::string str;
str.reserve(((len + 2) / 3) * 4); str.reserve(((input.size() + 2) / 3) * 4);
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, pch, pch + len); ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
while (str.size() % 4) str += '='; while (str.size() % 4) str += '=';
return str; return str;
} }
std::string EncodeBase64(const std::string& str) std::string EncodeBase64(const std::string& str)
{ {
return EncodeBase64((const unsigned char*)str.data(), str.size()); return EncodeBase64(MakeUCharSpan(str));
} }
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid) std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)

View File

@ -48,7 +48,7 @@ bool IsHex(const std::string& str);
bool IsHexNumber(const std::string& str); bool IsHexNumber(const std::string& str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr); std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr); std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
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::string EncodeBase64(const std::string& str);
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr); std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr); std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);