aboutsummaryrefslogtreecommitdiff
path: root/src/base58.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/base58.h')
-rw-r--r--src/base58.h107
1 files changed, 23 insertions, 84 deletions
diff --git a/src/base58.h b/src/base58.h
index efe3a95ebd..630d6fe9aa 100644
--- a/src/base58.h
+++ b/src/base58.h
@@ -18,6 +18,7 @@
#include <string>
#include <vector>
+#include "chainparams.h"
#include "bignum.h"
#include "key.h"
#include "script.h"
@@ -270,21 +271,13 @@ public:
class CBitcoinAddress : public CBase58Data
{
public:
- enum
- {
- PUBKEY_ADDRESS = 0,
- SCRIPT_ADDRESS = 5,
- PUBKEY_ADDRESS_TEST = 111,
- SCRIPT_ADDRESS_TEST = 196,
- };
-
bool Set(const CKeyID &id) {
- SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20);
+ SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
return true;
}
bool Set(const CScriptID &id) {
- SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20);
+ SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20);
return true;
}
@@ -295,32 +288,10 @@ public:
bool IsValid() const
{
- unsigned int nExpectedSize = 20;
- bool fExpectTestNet = false;
- switch(nVersion)
- {
- case PUBKEY_ADDRESS:
- nExpectedSize = 20; // Hash of public key
- fExpectTestNet = false;
- break;
- case SCRIPT_ADDRESS:
- nExpectedSize = 20; // Hash of CScript
- fExpectTestNet = false;
- break;
-
- case PUBKEY_ADDRESS_TEST:
- nExpectedSize = 20;
- fExpectTestNet = true;
- break;
- case SCRIPT_ADDRESS_TEST:
- nExpectedSize = 20;
- fExpectTestNet = true;
- break;
-
- default:
- return false;
- }
- return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
+ bool fCorrectSize = vchData.size() == 20;
+ bool fKnownVersion = nVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS) ||
+ nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
+ return fCorrectSize && fKnownVersion;
}
CBitcoinAddress()
@@ -345,48 +316,27 @@ public:
CTxDestination Get() const {
if (!IsValid())
return CNoDestination();
- switch (nVersion) {
- case PUBKEY_ADDRESS:
- case PUBKEY_ADDRESS_TEST: {
- uint160 id;
- memcpy(&id, &vchData[0], 20);
+ uint160 id;
+ memcpy(&id, &vchData[0], 20);
+ if (nVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
return CKeyID(id);
- }
- case SCRIPT_ADDRESS:
- case SCRIPT_ADDRESS_TEST: {
- uint160 id;
- memcpy(&id, &vchData[0], 20);
+ else if (nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
return CScriptID(id);
- }
- }
- return CNoDestination();
+ else
+ return CNoDestination();
}
bool GetKeyID(CKeyID &keyID) const {
- if (!IsValid())
+ if (!IsValid() || nVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
return false;
- switch (nVersion) {
- case PUBKEY_ADDRESS:
- case PUBKEY_ADDRESS_TEST: {
- uint160 id;
- memcpy(&id, &vchData[0], 20);
- keyID = CKeyID(id);
- return true;
- }
- default: return false;
- }
+ uint160 id;
+ memcpy(&id, &vchData[0], 20);
+ keyID = CKeyID(id);
+ return true;
}
bool IsScript() const {
- if (!IsValid())
- return false;
- switch (nVersion) {
- case SCRIPT_ADDRESS:
- case SCRIPT_ADDRESS_TEST: {
- return true;
- }
- default: return false;
- }
+ return IsValid() && nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
}
};
@@ -401,7 +351,7 @@ public:
void SetKey(const CKey& vchSecret)
{
assert(vchSecret.IsValid());
- SetData(fTestNet ? 239 : 128, vchSecret.begin(), vchSecret.size());
+ SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), vchSecret.size());
if (vchSecret.IsCompressed())
vchData.push_back(1);
}
@@ -415,20 +365,9 @@ public:
bool IsValid() const
{
- bool fExpectTestNet = false;
- switch(nVersion)
- {
- case 128:
- break;
-
- case 239:
- fExpectTestNet = true;
- break;
-
- default:
- return false;
- }
- return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1));
+ bool fExpectedFormat = vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1);
+ bool fCorrectVersion = nVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
+ return fExpectedFormat && fCorrectVersion;
}
bool SetString(const char* pszSecret)