text: // Copyright (c) 2010 Satoshi Nakamoto
  // Copyright (c) 2009-2012 The Bitcoin developers
  // Distributed under the MIT/X11 software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include "assert.h"
  
  #include "chainparams.h"
  #include "main.h"
  #include "util.h"
  
  #include <boost/assign/list_of.hpp>
  
  using namespace boost::assign;
  
  struct SeedSpec6 {
      uint8_t addr[16];
      uint16_t port;
  };
  
  #include "chainparamsseeds.h"
  
  //
  // Main network
  //
  
  // Convert the pnSeeds6 array into usable address objects.
  static void convertSeed6(std::vector<CAddress> &vSeedsOut, const SeedSpec6 *data, unsigned int count)
  {
      // It'll only connect to one or two seed nodes because once it connects,
      // it'll get a pile of addresses with newer timestamps.
      // Seed nodes are given a random 'last seen time' of between one and two
      // weeks ago.
      const int64_t nOneWeek = 7*24*60*60;
      for (unsigned int i = 0; i < count; i++)
      {
          struct in6_addr ip;
          memcpy(&ip, data[i].addr, sizeof(ip));
          CAddress addr(CService(ip, data[i].port));
          addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
          vSeedsOut.push_back(addr);
      }
  }
  
  class CMainParams : public CChainParams {
  public:
      CMainParams() {
          // The message start string is designed to be unlikely to occur in normal data.
          // The characters are rarely used upper ASCII, not valid as UTF-8, and produce
          // a large 4-byte int at any alignment.
          pchMessageStart[0] = 0xfa;
          pchMessageStart[1] = 0x0e;
          pchMessageStart[2] = 0x33;
          pchMessageStart[3] = 0x66;
          vAlertPubKey = ParseHex("04d56163829f3121b70e9b6ce3881eb8bc6d99fc0a04850033aa8078b6d7e3ce769bb5434ec9b0ebb4e18678f2faf4348cc0b4619ac976b067f5114b911b7c4701");
          nDefaultPort = 6666;
          nRPCPort = 3333;
          bnProofOfWorkLimit = CBigNum(~uint256(0) >> 20);
          bnProofOfStakeLimit = CBigNum(~uint256(0) >> 20);
  
          const char* pszTimestamp = "Prayers are to men as dolls are to children";
          std::vector<CTxIn> vin;
          vin.resize(1);
          vin[0].scriptSig = CScript() << 0 << CBigNum(42) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
          std::vector<CTxOut> vout;
          vout.resize(1);
          vout[0].SetEmpty();
          CTransaction txNew(1, 1444260302, vin, vout, 0);
          genesis.vtx.push_back(txNew);
          genesis.hashPrevBlock = 0;
          genesis.hashMerkleRoot = genesis.BuildMerkleTree();
          genesis.nVersion = 1;
          genesis.nTime    = 1444260302;
          genesis.nBits    = bnProofOfWorkLimit.GetCompact();
          genesis.nNonce   = 1509270;
  
          hashGenesisBlock = genesis.GetHash();
          assert(hashGenesisBlock == uint256("0x00000b7d18c65c1714f810c8d2b807869d31949416b29b319f08fac216b9d5e4"));
          assert(genesis.hashMerkleRoot == uint256("0xd33d0d869a7a6d3683605012e84ed0bca572eb72a71cc637e9a03f54ed018d32"));
  
          base58Prefixes[PUBKEY_ADDRESS] = list_of(30);
          base58Prefixes[SCRIPT_ADDRESS] = list_of(85);
          base58Prefixes[SECRET_KEY] =     list_of(214);
          base58Prefixes[EXT_PUBLIC_KEY] = list_of(0x04)(0x88)(0xB2)(0x1E);
          base58Prefixes[EXT_SECRET_KEY] = list_of(0x04)(0x88)(0xAD)(0xE4);
  
          convertSeed6(vFixedSeeds, pnSeed6_main, ARRAYLEN(pnSeed6_main));
  
          nTargetSpacing = 60;
          nTargetTimespan = 120 * nTargetSpacing;
          nLastPoWBlock = 10000;
          nStartPoSBlock = 666;
      }
  
      virtual const CBlock& GenesisBlock() const { return genesis; }
      virtual Network NetworkID() const { return CChainParams::MAIN; }
  
      virtual const vector<CAddress>& FixedSeeds() const {
          return vFixedSeeds;
      }
  protected:
      CBlock genesis;
      vector<CAddress> vFixedSeeds;
  };
  static CMainParams mainParams;
  
  
  //
  // Testnet
  //
  
  class CTestNetParams : public CMainParams {
  public:
      CTestNetParams() {
          // The message start string is designed to be unlikely to occur in normal data.
          // The characters are rarely used upper ASCII, not valid as UTF-8, and produce
          // a large 4-byte int at any alignment.
          pchMessageStart[0] = 0x5a;
          pchMessageStart[1] = 0x32;
          pchMessageStart[2] = 0x67;
          pchMessageStart[3] = 0xac;
          bnProofOfWorkLimit = CBigNum(~uint256(0) >> 16);
          bnProofOfStakeLimit = CBigNum(~uint256(0) >> 16);
  
          vAlertPubKey = ParseHex("045b116774fd26b208e0be485e267557545d4b67c9c0825087f84ac0c95f3681f563623f0bdf4599c4af17c4eb4ab320ef97ed09dd96534d5825b4898306a6ddb9");
          nDefaultPort = 16666;
          nRPCPort = 13333;
          strDataDir = "testnet";
  
          // Modify the testnet genesis block so the timestamp is valid for a later start.
          genesis.nBits  = bnProofOfWorkLimit.GetCompact();
          genesis.nNonce = 695804;
  
          hashGenesisBlock = genesis.GetHash();
          assert(hashGenesisBlock == uint256("0x0000e23bd82daaf1815ef2af47ca8dd758a9bd50745b6ed661f06a336427f6d6"));
  
          vFixedSeeds.clear();
          vSeeds.clear();
  
          base58Prefixes[PUBKEY_ADDRESS] = list_of(118);
          base58Prefixes[SCRIPT_ADDRESS] = list_of(196);
          base58Prefixes[SECRET_KEY]     = list_of(246);
          base58Prefixes[EXT_PUBLIC_KEY] = list_of(0x04)(0x35)(0x87)(0xCF);
          base58Prefixes[EXT_SECRET_KEY] = list_of(0x04)(0x35)(0x83)(0x94);
  
          convertSeed6(vFixedSeeds, pnSeed6_test, ARRAYLEN(pnSeed6_test));
  
          nTargetSpacing = 30;
          nLastPoWBlock = 0x7fffffff;
          nStartPoSBlock = 666;
      }
      virtual Network NetworkID() const { return CChainParams::TESTNET; }
  };
  static CTestNetParams testNetParams;
  
  
  //
  // Regression test
  //
  class CRegTestParams : public CTestNetParams {
  public:
      CRegTestParams() {
          pchMessageStart[0] = 0x0a;
          pchMessageStart[1] = 0xbc;
          pchMessageStart[2] = 0x10;
          pchMessageStart[3] = 0xfe;
          bnProofOfWorkLimit = CBigNum(~uint256(0) >> 1);
          genesis.nTime = 1444260302;
          genesis.nBits  = bnProofOfWorkLimit.GetCompact();
          genesis.nNonce = 44;
  
          hashGenesisBlock = genesis.GetHash();
          nDefaultPort = 26666;
          strDataDir = "regtest";
  
          assert(hashGenesisBlock == uint256("0x4f0402d1e7e86e241f30b136229e3eacff445e28da9d506d083f587850c5d29f"));
  
          vSeeds.clear();  // Regtest mode doesn't have any DNS seeds.
      }
  
      virtual bool RequireRPCPassword() const { return false; }
      virtual Network NetworkID() const { return CChainParams::REGTEST; }
  };
  static CRegTestParams regTestParams;
  
  static CChainParams *pCurrentParams = &mainParams;
  
  const CChainParams &Params() {
      return *pCurrentParams;
  }
  
  void SelectParams(CChainParams::Network network) {
      switch (network) {
          case CChainParams::MAIN:
              pCurrentParams = &mainParams;
              break;
          case CChainParams::TESTNET:
              pCurrentParams = &testNetParams;
              break;
          case CChainParams::REGTEST:
              pCurrentParams = &regTestParams;
              break;
          default:
              assert(false && "Unimplemented network");
              return;
      }
  }
  
  bool SelectParamsFromCommandLine() {
      bool fRegTest = GetBoolArg("-regtest", false);
      bool fTestNet = GetBoolArg("-testnet", false);
  
      if (fTestNet && fRegTest) {
          return false;
      }
  
      if (fRegTest) {
          SelectParams(CChainParams::REGTEST);
      } else if (fTestNet) {
          SelectParams(CChainParams::TESTNET);
      } else {
          SelectParams(CChainParams::MAIN);
      }
      return true;
  }
  