ledger-core
keychain_test_helper.h
1 /*
2  *
3  * keychain_test_helper
4  * ledger-core
5  *
6  * Created by El Khalil Bellakrid on 16/05/2018.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2018 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 
32 #ifndef LEDGER_CORE_KEYCHAIN_TEST_HELPER_H
33 #define LEDGER_CORE_KEYCHAIN_TEST_HELPER_H
34 
35 #include <src/wallet/currencies.hpp>
36 #include <api/EthereumLikeNetworkParameters.hpp>
37 #include <api/Currency.hpp>
38 #include "../BaseFixture.h"
39 
40 
42 
46  std::string xpub;
47  std::string derivationPath;
48 
49  KeychainTestData() = default;
50 
53  std::string xpub_,
54  std::string derivationPath_):
55  btc_parameters(std::move(parameters_)),
56  currency(std::move(currency_)),
57  xpub(std::move(xpub_)),
58  derivationPath(std::move(derivationPath_))
59  {
60  currency.bitcoinLikeNetworkParameters = btc_parameters;
61  }
62 
65  std::string xpub_,
66  std::string derivationPath_):
67  eth_parameters(std::move(parameters_)),
68  currency(std::move(currency_)),
69  xpub(std::move(xpub_)),
70  derivationPath(std::move(derivationPath_))
71  {
72  currency.ethereumLikeNetworkParameters = eth_parameters;
73  }
74 
75  KeychainTestData(const KeychainTestData &data) {
76  this->btc_parameters = data.btc_parameters;
77  this->eth_parameters = data.eth_parameters;
78  this->currency = data.currency;
79  this->xpub = data.xpub;
80  this->derivationPath = data.derivationPath;
81  }
82 
83  KeychainTestData& operator=(const KeychainTestData &data) {
84  this->btc_parameters = data.btc_parameters;
85  this->eth_parameters = data.eth_parameters;
86  this->currency = data.currency;
87  this->xpub = data.xpub;
88  this->derivationPath = data.derivationPath;
89  return *this;
90  }
91 };
92 
93 extern KeychainTestData BTC_DATA;
94 extern KeychainTestData BTC_TESTNET_DATA;
95 extern KeychainTestData BCH_DATA;
96 extern KeychainTestData BTG_DATA;
97 extern KeychainTestData ZCASH_DATA;
98 extern KeychainTestData ZENCASH_DATA;
99 extern KeychainTestData LTC_DATA_LEGACY;
100 extern KeychainTestData LTC_DATA_SEGWIT;
101 extern KeychainTestData PEERCOIN_DATA;
102 extern KeychainTestData DIGIBYTE_DATA;
103 extern KeychainTestData HCASH_DATA;
104 extern KeychainTestData QTUM_DATA;
105 extern KeychainTestData XST_DATA;
106 extern KeychainTestData VTC_DATA_LEGACY;
107 extern KeychainTestData VTC_DATA_SEGWIT;
108 extern KeychainTestData VIA_DATA_LEGACY;
109 extern KeychainTestData VIA_DATA_SEGWIT;
110 extern KeychainTestData DASH_DATA;
111 extern KeychainTestData DOGE_DATA;
112 extern KeychainTestData STRATIS_DATA;
113 extern KeychainTestData KOMODO_DATA;
114 extern KeychainTestData POSWALLET_DATA;
115 extern KeychainTestData PIVX_DATA;
116 extern KeychainTestData CLUBCOIN_DATA;
117 extern KeychainTestData DECRED_DATA;
118 extern KeychainTestData ETHEREUM_DATA;
119 
120 template <class Keychain>
121 class KeychainFixture : public BaseFixture {
122 public:
123  void testKeychain(const KeychainTestData &data, std::function<void (Keychain&)> f) {
124  auto backend = std::make_shared<ledger::core::PreferencesBackend>(
125  "/preferences/tests.db",
126  dispatcher->getMainExecutionContext(),
127  resolver
128  );
129  auto configuration = std::make_shared<DynamicObject>();
130  dispatcher->getMainExecutionContext()->execute(ledger::qt::make_runnable([=]() {
131  Keychain keychain(
132  configuration,
133  data.currency,
134  0,
135  ledger::core::BitcoinLikeExtendedPublicKey::fromBase58(data.currency,
136  data.xpub,
137  optional<std::string>(data.derivationPath),
138  configuration),
139  backend->getPreferences("keychain")
140  );
141  f(keychain);
142  dispatcher->stop();
143  }));
144  dispatcher->waitUntilStopped();
145  };
146 };
147 
148 #endif //LEDGER_CORE_KEYCHAIN_TEST_HELPER_H
Definition: keychain_test_helper.h:41
Definition: Currency.hpp:23
Definition: BaseFixture.h:76
Definition: keychain_test_helper.h:121
Definition: EthereumLikeNetworkParameters.hpp:15
std::experimental::optional< BitcoinLikeNetworkParameters > bitcoinLikeNetworkParameters
Definition: Currency.hpp:41
Definition: optional.hpp:177
Definition: BitcoinLikeNetworkParameters.hpp:17
std::experimental::optional< EthereumLikeNetworkParameters > ethereumLikeNetworkParameters
Definition: Currency.hpp:43