Changeset View
Changeset View
Standalone View
Standalone View
src/base58.cpp
Show First 20 Lines • Show All 256 Lines • ▼ Show 20 Lines | if (data.size() == 20 + script_prefix.size() && | ||||
std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) { | std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) { | ||||
memcpy(hash.begin(), &data[script_prefix.size()], 20); | memcpy(hash.begin(), &data[script_prefix.size()], 20); | ||||
return CScriptID(hash); | return CScriptID(hash); | ||||
} | } | ||||
return CNoDestination(); | return CNoDestination(); | ||||
} | } | ||||
} // namespace | } // namespace | ||||
void CBitcoinSecret::SetKey(const CKey &vchSecret) { | CKey DecodeSecret(const std::string &str) { | ||||
assert(vchSecret.IsValid()); | CKey key; | ||||
SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), | std::vector<unsigned char> data; | ||||
vchSecret.size()); | if (DecodeBase58Check(str, data)) { | ||||
if (vchSecret.IsCompressed()) vchData.push_back(1); | 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; | |||||
} | |||||
std::string EncodeSecret(const CKey &key) { | |||||
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); | |||||
CKey CBitcoinSecret::GetKey() { | memory_cleanse(data.data(), data.size()); | ||||
CKey ret; | |||||
assert(vchData.size() >= 32); | |||||
ret.Set(vchData.begin(), vchData.begin() + 32, | |||||
vchData.size() > 32 && vchData[32] == 1); | |||||
return ret; | 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 EncodeLegacyAddr(const CTxDestination &dest, | std::string EncodeLegacyAddr(const CTxDestination &dest, | ||||
const CChainParams ¶ms) { | const CChainParams ¶ms) { | ||||
return boost::apply_visitor(DestinationEncoder(params), dest); | return boost::apply_visitor(DestinationEncoder(params), dest); | ||||
} | } | ||||
CTxDestination DecodeLegacyAddr(const std::string &str, | CTxDestination DecodeLegacyAddr(const std::string &str, | ||||
const CChainParams ¶ms) { | const CChainParams ¶ms) { | ||||
return DecodeLegacyDestination(str, params); | return DecodeLegacyDestination(str, params); | ||||
} | } |