aboutsummaryrefslogtreecommitdiff
path: root/src/base58.h
diff options
context:
space:
mode:
authorMike Hearn <hearn@google.com>2013-05-07 15:16:25 +0200
committerMike Hearn <hearn@google.com>2013-06-19 16:28:52 +0200
commit0e4b31755534fac4ea6c20a60f719e3694252220 (patch)
tree80f576f67c855485e5d82007b98a45536bbf2f9a /src/base58.h
parent70e7fba06da36218688a4cae4a5d12332c714247 (diff)
downloadbitcoin-0e4b31755534fac4ea6c20a60f719e3694252220.tar.xz
Introduce a CChainParameters singleton class and regtest mode.
The new class is accessed via the Params() method and holds most things that vary between main, test and regtest networks. The regtest mode has two purposes, one is to run the bitcoind/bitcoinj comparison tool which compares two separate implementations of the Bitcoin protocol looking for divergence. The other is that when run, you get a local node which can mine a single block instantly, which is highly convenient for testing apps during development as there's no need to wait 10 minutes for a block on the testnet.
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)