ledger-core
networks.hpp
1 /*
2  *
3  * networks
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 27/01/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_NETWORKS_HPP
32 #define LEDGER_CORE_NETWORKS_HPP
33 
34 #ifndef LIBCORE_EXPORT
35  #if defined(_MSC_VER)
36  #include <libcore_export.h>
37  #else
38  #define LIBCORE_EXPORT
39  #endif
40 #endif
41 
42 #include "../../api/BitcoinLikeNetworkParameters.hpp"
43 
44 namespace ledger {
45  namespace core {
46 
47  namespace networks {
48 
49  enum sigHashType : uint8_t {
50  SIGHASH_ALL = 0x01,
51  SIGHASH_NONE = 0x02,
52  SIGHASH_SINGLE = 0x03,
53  SIGHASH_FORKID = 0x40,
54  SIGHASH_ANYONECANPAY = 0x80
55  };
56 
57  // Since version for networks are starting at 1
58  static const int HIGHTEST_PARAMETERS_VERSION = 2;
59 
60  extern LIBCORE_EXPORT const api::BitcoinLikeNetworkParameters getNetworkParameters(const std::string &networkName, int version = HIGHTEST_PARAMETERS_VERSION);
61 
62  // Since we are not (supposed) to have too many versions, we migrate from the beginning ...
63  // TODO: could be optimized by saving last HIGHTEST_PARAMETERS_VERSION and start from it
64  // or better add a field version to BitcoinLikeNetworkParameters
65  template <int migration>
66  void migrateParameters(api::BitcoinLikeNetworkParameters &params) {
67  // Migration not implemented
68  return;
69  }
70  void migrateParameters(api::BitcoinLikeNetworkParameters &params, int migration);
71 
72  static bool migrateParameters(api::BitcoinLikeNetworkParameters &params,
73  int currentVersion,
74  int targetVersion) {
75  if (targetVersion < 2 || currentVersion < 1) {
76  return false;
77  }
78  auto previous = migrateParameters(params, currentVersion, targetVersion - 1);
79  if (currentVersion < targetVersion) {
80  migrateParameters(params, targetVersion);
81  return true;
82  }
83  return previous;
84  };
85 
86  extern LIBCORE_EXPORT const std::vector<api::BitcoinLikeNetworkParameters> ALL;
87 
88  //BIP115 (ex: Zencash)
90  std::string blockHash;
91  std::vector<uint8_t> blockHeight;
92  };
93  extern LIBCORE_EXPORT const BIP115Parameters BIP115_PARAMETERS;
94 
95  //ZIP: Zcash improvements/updates (ex: Zcash overwinter, Sapling ...)
96  struct ZIPParameters {
97  uint32_t version;
98  std::vector<uint8_t> overwinterFlag;
99  std::vector<uint8_t> versionGroupId;
100  uint64_t blockHeight; //block height at which ZIP will be effective
101  };
102  extern LIBCORE_EXPORT const ZIPParameters ZIP143_PARAMETERS;
103  extern LIBCORE_EXPORT const ZIPParameters ZIP_SAPLING_PARAMETERS;
104  template<class Archive>
105  void serialize(Archive & archive,
107  {
108  archive(
109  p.Identifier,
110  p.P2PKHVersion,
111  p.P2SHVersion,
112  p.XPUBVersion,
113  p.FeePolicy,
114  p.DustAmount,
115  p.MessagePrefix,
117  p.TimestampDelay,
118  p.SigHash,
120  );
121  }
122 
123  }
124  }
125 }
126 
127 
128 #endif //LEDGER_CORE_NETWORKS_HPP
std::vector< uint8_t > P2SHVersion
Definition: BitcoinLikeNetworkParameters.hpp:23
std::string MessagePrefix
Definition: BitcoinLikeNetworkParameters.hpp:31
Definition: networks.hpp:89
std::vector< uint8_t > SigHash
Definition: BitcoinLikeNetworkParameters.hpp:37
std::vector< uint8_t > XPUBVersion
Definition: BitcoinLikeNetworkParameters.hpp:25
std::vector< std::string > AdditionalBIPs
Definition: BitcoinLikeNetworkParameters.hpp:39
std::string Identifier
Definition: BitcoinLikeNetworkParameters.hpp:19
int64_t DustAmount
Definition: BitcoinLikeNetworkParameters.hpp:29
std::vector< uint8_t > P2PKHVersion
Definition: BitcoinLikeNetworkParameters.hpp:21
Definition: Account.cpp:8
BitcoinLikeFeePolicy FeePolicy
Definition: BitcoinLikeNetworkParameters.hpp:27
Definition: networks.hpp:96
bool UsesTimestampedTransaction
Definition: BitcoinLikeNetworkParameters.hpp:33
int64_t TimestampDelay
Definition: BitcoinLikeNetworkParameters.hpp:35
Definition: BitcoinLikeNetworkParameters.hpp:17