aboutsummaryrefslogtreecommitdiff
path: root/src/base58.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2013-06-22 06:56:14 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2013-06-22 06:56:14 -0700
commit01b45731b75a19ac194b4206b20716bfedcb2357 (patch)
tree90bd76e552d0ac7b44382885e212d9705fe24d96 /src/base58.h
parent905738a15aa68c39e32bc5b9bbde53a9610dc818 (diff)
parent0e4b31755534fac4ea6c20a60f719e3694252220 (diff)
Merge pull request #2632 from mikehearn/chainparams
Refactor chain-specific tweaks into a CChainParams class and introduce a regtest 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)