aboutsummaryrefslogtreecommitdiff
path: root/src/base58.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-09-19 16:49:52 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2018-02-19 18:55:20 -0800
commit32e69fa0df8fc1cfc8ac4f8381bc54b8f33e1c38 (patch)
treecd00b30d48911729cef8bd436797177bd8ebeb33 /src/base58.cpp
parentffc6e48b2983189dc0ce7de0a038e5329bc07b1b (diff)
Replace CBitcoinSecret with {Encode,Decode}Secret
Diffstat (limited to 'src/base58.cpp')
-rw-r--r--src/base58.cpp48
1 files changed, 22 insertions, 26 deletions
diff --git a/src/base58.cpp b/src/base58.cpp
index 499afbe382..826fe630bc 100644
--- a/src/base58.cpp
+++ b/src/base58.cpp
@@ -323,39 +323,35 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
}
} // namespace
-void CBitcoinSecret::SetKey(const CKey& vchSecret)
+CKey DecodeSecret(const std::string& str)
{
- assert(vchSecret.IsValid());
- SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), vchSecret.size());
- if (vchSecret.IsCompressed())
- vchData.push_back(1);
+ CKey key;
+ std::vector<unsigned char> data;
+ if (DecodeBase58Check(str, data)) {
+ const std::vector<unsigned char>& privkey_prefix = Params().Base58Prefix(CChainParams::SECRET_KEY);
+ if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
+ std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
+ bool compressed = data.size() == 33 + privkey_prefix.size();
+ key.Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
+ }
+ }
+ memory_cleanse(data.data(), data.size());
+ return key;
}
-CKey CBitcoinSecret::GetKey()
+std::string EncodeSecret(const CKey& key)
{
- CKey ret;
- assert(vchData.size() >= 32);
- ret.Set(vchData.begin(), vchData.begin() + 32, vchData.size() > 32 && vchData[32] == 1);
+ assert(key.IsValid());
+ std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
+ data.insert(data.end(), key.begin(), key.end());
+ if (key.IsCompressed()) {
+ data.push_back(1);
+ }
+ std::string ret = EncodeBase58Check(data);
+ memory_cleanse(data.data(), data.size());
return ret;
}
-bool CBitcoinSecret::IsValid() const
-{
- bool fExpectedFormat = vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1);
- bool fCorrectVersion = vchVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
- return fExpectedFormat && fCorrectVersion;
-}
-
-bool CBitcoinSecret::SetString(const char* pszSecret)
-{
- return CBase58Data::SetString(pszSecret) && IsValid();
-}
-
-bool CBitcoinSecret::SetString(const std::string& strSecret)
-{
- return SetString(strSecret.c_str());
-}
-
std::string EncodeDestination(const CTxDestination& dest)
{
return boost::apply_visitor(DestinationEncoder(Params()), dest);