Browse Source

Generalize version bytes

0.10
Pieter Wuille 12 years ago
parent
commit
8388289eb6
  1. 46
      src/base58.h
  2. 21
      src/chainparams.cpp
  3. 4
      src/chainparams.h

46
src/base58.h

@ -177,8 +177,8 @@ inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>
class CBase58Data class CBase58Data
{ {
protected: protected:
// the version byte // the version byte(s)
unsigned char nVersion; std::vector<unsigned char> vchVersion;
// the actually encoded data // the actually encoded data
typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar; typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> > vector_uchar;
@ -186,38 +186,38 @@ protected:
CBase58Data() CBase58Data()
{ {
nVersion = 0; vchVersion.clear();
vchData.clear(); vchData.clear();
} }
void SetData(int nVersionIn, const void* pdata, size_t nSize) void SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize)
{ {
nVersion = nVersionIn; vchVersion = vchVersionIn;
vchData.resize(nSize); vchData.resize(nSize);
if (!vchData.empty()) if (!vchData.empty())
memcpy(&vchData[0], pdata, nSize); memcpy(&vchData[0], pdata, nSize);
} }
void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) void SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend)
{ {
SetData(nVersionIn, (void*)pbegin, pend - pbegin); SetData(vchVersionIn, (void*)pbegin, pend - pbegin);
} }
public: public:
bool SetString(const char* psz) bool SetString(const char* psz, unsigned int nVersionBytes = 1)
{ {
std::vector<unsigned char> vchTemp; std::vector<unsigned char> vchTemp;
DecodeBase58Check(psz, vchTemp); DecodeBase58Check(psz, vchTemp);
if (vchTemp.empty()) if (vchTemp.size() < nVersionBytes)
{ {
vchData.clear(); vchData.clear();
nVersion = 0; vchVersion.clear();
return false; return false;
} }
nVersion = vchTemp[0]; vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
vchData.resize(vchTemp.size() - 1); vchData.resize(vchTemp.size() - nVersionBytes);
if (!vchData.empty()) if (!vchData.empty())
memcpy(&vchData[0], &vchTemp[1], vchData.size()); memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size());
OPENSSL_cleanse(&vchTemp[0], vchData.size()); OPENSSL_cleanse(&vchTemp[0], vchData.size());
return true; return true;
} }
@ -229,15 +229,15 @@ public:
std::string ToString() const std::string ToString() const
{ {
std::vector<unsigned char> vch(1, nVersion); std::vector<unsigned char> vch = vchVersion;
vch.insert(vch.end(), vchData.begin(), vchData.end()); vch.insert(vch.end(), vchData.begin(), vchData.end());
return EncodeBase58Check(vch); return EncodeBase58Check(vch);
} }
int CompareTo(const CBase58Data& b58) const int CompareTo(const CBase58Data& b58) const
{ {
if (nVersion < b58.nVersion) return -1; if (vchVersion < b58.vchVersion) return -1;
if (nVersion > b58.nVersion) return 1; if (vchVersion > b58.vchVersion) return 1;
if (vchData < b58.vchData) return -1; if (vchData < b58.vchData) return -1;
if (vchData > b58.vchData) return 1; if (vchData > b58.vchData) return 1;
return 0; return 0;
@ -289,8 +289,8 @@ public:
bool IsValid() const bool IsValid() const
{ {
bool fCorrectSize = vchData.size() == 20; bool fCorrectSize = vchData.size() == 20;
bool fKnownVersion = nVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS) || bool fKnownVersion = vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS) ||
nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS); vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
return fCorrectSize && fKnownVersion; return fCorrectSize && fKnownVersion;
} }
@ -318,16 +318,16 @@ public:
return CNoDestination(); return CNoDestination();
uint160 id; uint160 id;
memcpy(&id, &vchData[0], 20); memcpy(&id, &vchData[0], 20);
if (nVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)) if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
return CKeyID(id); return CKeyID(id);
else if (nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS)) else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
return CScriptID(id); return CScriptID(id);
else else
return CNoDestination(); return CNoDestination();
} }
bool GetKeyID(CKeyID &keyID) const { bool GetKeyID(CKeyID &keyID) const {
if (!IsValid() || nVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS)) if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
return false; return false;
uint160 id; uint160 id;
memcpy(&id, &vchData[0], 20); memcpy(&id, &vchData[0], 20);
@ -336,7 +336,7 @@ public:
} }
bool IsScript() const { bool IsScript() const {
return IsValid() && nVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS); return IsValid() && vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
} }
}; };
@ -366,7 +366,7 @@ public:
bool IsValid() const bool IsValid() const
{ {
bool fExpectedFormat = 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); bool fCorrectVersion = vchVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
return fExpectedFormat && fCorrectVersion; return fExpectedFormat && fCorrectVersion;
} }

21
src/chainparams.cpp

@ -9,6 +9,10 @@
#include "main.h" #include "main.h"
#include "util.h" #include "util.h"
#include <boost/assign/list_of.hpp>
using namespace boost::assign;
// //
// Main network // Main network
// //
@ -140,9 +144,9 @@ public:
vSeeds.push_back(CDNSSeedData("dashjr.org", "dnsseed.bitcoin.dashjr.org")); vSeeds.push_back(CDNSSeedData("dashjr.org", "dnsseed.bitcoin.dashjr.org"));
vSeeds.push_back(CDNSSeedData("xf2.org", "bitseed.xf2.org")); vSeeds.push_back(CDNSSeedData("xf2.org", "bitseed.xf2.org"));
base58Prefixes[PUBKEY_ADDRESS] = 0; base58Prefixes[PUBKEY_ADDRESS] = list_of(0);
base58Prefixes[SCRIPT_ADDRESS] = 5; base58Prefixes[SCRIPT_ADDRESS] = list_of(5);
base58Prefixes[SECRET_KEY] = 128; base58Prefixes[SECRET_KEY] = list_of(128);
// Convert the pnSeeds array into usable address objects. // Convert the pnSeeds array into usable address objects.
for (unsigned int i = 0; i < ARRAYLEN(pnSeed); i++) for (unsigned int i = 0; i < ARRAYLEN(pnSeed); i++)
@ -202,10 +206,9 @@ public:
vSeeds.push_back(CDNSSeedData("bitcoin.petertodd.org", "testnet-seed.bitcoin.petertodd.org")); vSeeds.push_back(CDNSSeedData("bitcoin.petertodd.org", "testnet-seed.bitcoin.petertodd.org"));
vSeeds.push_back(CDNSSeedData("bluematt.me", "testnet-seed.bluematt.me")); vSeeds.push_back(CDNSSeedData("bluematt.me", "testnet-seed.bluematt.me"));
base58Prefixes[PUBKEY_ADDRESS] = 111; base58Prefixes[PUBKEY_ADDRESS] = list_of(111);
base58Prefixes[SCRIPT_ADDRESS] = 196; base58Prefixes[SCRIPT_ADDRESS] = list_of(196);
base58Prefixes[SECRET_KEY] = 239; base58Prefixes[SECRET_KEY] = list_of(239);
} }
virtual Network NetworkID() const { return CChainParams::TESTNET; } virtual Network NetworkID() const { return CChainParams::TESTNET; }
}; };
@ -233,10 +236,6 @@ public:
assert(hashGenesisBlock == uint256("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206")); assert(hashGenesisBlock == uint256("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
vSeeds.clear(); // Regtest mode doesn't have any DNS seeds. vSeeds.clear(); // Regtest mode doesn't have any DNS seeds.
base58Prefixes[PUBKEY_ADDRESS] = 0;
base58Prefixes[SCRIPT_ADDRESS] = 5;
base58Prefixes[SECRET_KEY] = 128;
} }
virtual bool RequireRPCPassword() const { return false; } virtual bool RequireRPCPassword() const { return false; }

4
src/chainparams.h

@ -60,7 +60,7 @@ public:
const string& DataDir() const { return strDataDir; } const string& DataDir() const { return strDataDir; }
virtual Network NetworkID() const = 0; virtual Network NetworkID() const = 0;
const vector<CDNSSeedData>& DNSSeeds() const { return vSeeds; } const vector<CDNSSeedData>& DNSSeeds() const { return vSeeds; }
int Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } const std::vector<unsigned char> &Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
virtual const vector<CAddress>& FixedSeeds() const = 0; virtual const vector<CAddress>& FixedSeeds() const = 0;
int RPCPort() const { return nRPCPort; } int RPCPort() const { return nRPCPort; }
protected: protected:
@ -76,7 +76,7 @@ protected:
int nSubsidyHalvingInterval; int nSubsidyHalvingInterval;
string strDataDir; string strDataDir;
vector<CDNSSeedData> vSeeds; vector<CDNSSeedData> vSeeds;
int base58Prefixes[MAX_BASE58_TYPES]; std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
}; };
/** /**

Loading…
Cancel
Save