ledger-core
CommonBitcoinLikeKeychains.hpp
1 /*
2  *
3  * CommonBitcoinLikeKeychain
4  * ledger-core
5  *
6  * Created by El Khalil Bellakrid on 30/04/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 #ifndef LEDGER_CORE_COMMONBITCOINLIKEKEYCHAINS_H
32 #define LEDGER_CORE_COMMONBITCOINLIKEKEYCHAINS_H
33 
34 #include "BitcoinLikeKeychain.hpp"
35 #include <set>
36 #include "../../../collections/DynamicObject.hpp"
37 #include <bitcoin/BitcoinLikeAddress.hpp>
38 
39 namespace ledger {
40  namespace core {
41 
43  uint32_t maxConsecutiveChangeIndex;
44  uint32_t maxConsecutiveReceiveIndex;
45  std::set<uint32_t> nonConsecutiveChangeIndexes;
46  std::set<uint32_t> nonConsecutiveReceiveIndexes;
47  bool empty;
48 
49  template <class Archive>
50  void serialize(Archive& archive, std::uint32_t const version) {
51  if (version == 0) {
52  archive(
53  maxConsecutiveChangeIndex,
54  maxConsecutiveReceiveIndex,
55  nonConsecutiveChangeIndexes,
56  nonConsecutiveReceiveIndexes,
57  empty
58  );
59  }
60  }
61  };
62 
64  public:
65  CommonBitcoinLikeKeychains(const std::shared_ptr<api::DynamicObject> &configuration,
66  const api::Currency &params, int account,
67  const std::shared_ptr<api::BitcoinLikeExtendedPublicKey> &xpub,
68  const std::shared_ptr<Preferences> &preferences);
69 
70  bool markPathAsUsed(const DerivationPath &path) override;
71 
72  BitcoinLikeKeychain::Address getFreshAddress(KeyPurpose purpose) override;
73  std::vector<BitcoinLikeKeychain::Address> getAllObservableAddresses(uint32_t from, uint32_t to) override;
74  std::vector<BitcoinLikeKeychain::Address> getFreshAddresses(KeyPurpose purpose, size_t n) override;
75  Option<KeyPurpose> getAddressPurpose(const std::string &address) const override;
76  Option<std::string> getAddressDerivationPath(const std::string &address) const override;
77  std::vector<BitcoinLikeKeychain::Address> getAllObservableAddresses(KeyPurpose purpose, uint32_t from, uint32_t to) override;
78  bool isEmpty() const override;
79  std::shared_ptr<api::BitcoinLikeExtendedPublicKey> getExtendedPublicKey() const;
80  std::string getRestoreKey() const override;
81 
82  bool contains(const std::string &address) const override;
83 
84  int32_t getObservableRangeSize() const override;
85 
86  Option<std::vector<uint8_t>> getPublicKey(const std::string &address) const override;
87 
88  protected:
89  std::shared_ptr<api::BitcoinLikeExtendedPublicKey> _internalNodeXpub;
90  std::shared_ptr<api::BitcoinLikeExtendedPublicKey> _publicNodeXpub;
91  uint32_t _observableRange;
92  std::string _keychainEngine;
93 
94  private:
95  BitcoinLikeKeychain::Address derive(KeyPurpose purpose, off_t index);
96  void saveState();
98  std::shared_ptr<api::BitcoinLikeExtendedPublicKey> _xpub;
99  };
100  }
101 }
102 
103 #endif //LEDGER_CORE_COMMONBITCOINLIKEKEYCHAINS_H
Definition: BitcoinLikeKeychain.hpp:51
Definition: Option.hpp:49
Definition: CommonBitcoinLikeKeychains.hpp:42
Definition: CommonBitcoinLikeKeychains.hpp:63
Definition: DerivationPath.hpp:40
Definition: Currency.hpp:23
Definition: Account.cpp:8