ledger-core
BitcoinLikeKeychain.hpp
1 /*
2  *
3  * BitcoinLikeKeychain
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 17/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_BITCOINLIKEKEYCHAIN_HPP
32 #define LEDGER_CORE_BITCOINLIKEKEYCHAIN_HPP
33 
34 #include "../../../bitcoin/BitcoinLikeExtendedPublicKey.hpp"
35 #include <string>
36 #include <vector>
37 #include <utils/DerivationScheme.hpp>
38 #include "../../../utils/Option.hpp"
39 #include "../../../preferences/Preferences.hpp"
40 #include "../../../api/Configuration.hpp"
41 #include "../../../api/DynamicObject.hpp"
42 #include <api/Currency.hpp>
43 #include <api/AccountCreationInfo.hpp>
44 #include <api/ExtendedKeyAccountCreationInfo.hpp>
45 
46 #include <bitcoin/BitcoinLikeAddress.hpp>
47 
48 namespace ledger {
49  namespace core {
50 
52  public:
53  enum KeyPurpose {
54  RECEIVE, CHANGE
55  };
56 
57  public:
58  using Address = std::shared_ptr<BitcoinLikeAddress>;
59 
61  const std::shared_ptr<api::DynamicObject>& configuration,
62  const api::Currency& params,
63  int account,
64  const std::shared_ptr<Preferences>& preferences);
65 
66  virtual bool markAsUsed(const std::vector<std::string>& addresses);
67  virtual bool markAsUsed(const std::string& address);
68  virtual bool markPathAsUsed(const DerivationPath& path) = 0;
69 
70  virtual std::vector<Address> getAllObservableAddresses(uint32_t from, uint32_t to) = 0;
71  virtual std::vector<Address> getAllObservableAddresses(KeyPurpose purpose, uint32_t from, uint32_t to) = 0;
72 
73  virtual Address getFreshAddress(KeyPurpose purpose) = 0;
74  virtual std::vector<Address> getFreshAddresses(KeyPurpose purpose, size_t n) = 0;
75 
76  virtual Option<KeyPurpose> getAddressPurpose(const std::string& address) const = 0;
77  virtual Option<std::string> getAddressDerivationPath(const std::string& address) const = 0;
78  virtual bool isEmpty() const = 0;
79 
80  int getAccountIndex() const;
81  const api::BitcoinLikeNetworkParameters& getNetworkParameters() const;
82  const api::Currency& getCurrency() const;
83 
84  virtual Option<std::vector<uint8_t>> getPublicKey(const std::string& address) const = 0;
85 
86 
87  std::shared_ptr<api::DynamicObject> getConfiguration() const;
88  const DerivationScheme& getDerivationScheme() const;
89  const DerivationScheme& getFullDerivationScheme() const;
90  std::string getKeychainEngine() const;
91  bool isSegwit() const;
92  bool isNativeSegwit() const;
93 
94  virtual std::string getRestoreKey() const = 0;
95  virtual int32_t getObservableRangeSize() const = 0;
96  virtual bool contains(const std::string& address) const = 0;
97  virtual int32_t getOutputSizeAsSignedTxInput() const = 0;
98 
99  static bool isSegwit(const std::string &keychainEngine);
100  static bool isNativeSegwit(const std::string &keychainEngine);
101  protected:
102  std::shared_ptr<Preferences> getPreferences() const;
103  DerivationScheme& getDerivationScheme();
104 
105  private:
106  const api::Currency _currency;
107  DerivationScheme _scheme;
108  DerivationScheme _fullScheme;
109  int _account;
110  std::shared_ptr<Preferences> _preferences;
111  std::shared_ptr<api::DynamicObject> _configuration;
112  };
113  }
114 }
115 
116 #endif //LEDGER_CORE_BITCOINLIKEKEYCHAIN_HPP
Definition: BitcoinLikeKeychain.hpp:51
Definition: Option.hpp:49
Definition: DerivationScheme.hpp:55
Definition: DerivationPath.hpp:40
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: BitcoinLikeNetworkParameters.hpp:17