ledger-core
BitcoinLikeAccount.hpp
1 /*
2  *
3  * BitcoinLikeAccount
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 28/04/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_BITCOINLIKEACCOUNT_HPP
32 #define LEDGER_CORE_BITCOINLIKEACCOUNT_HPP
33 
34 #include "BitcoinLikeWallet.hpp"
35 #include <api/BitcoinLikeAccount.hpp>
36 #include "explorers/BitcoinLikeBlockchainExplorer.hpp"
37 #include <wallet/bitcoin/keychains/BitcoinLikeKeychain.hpp>
38 #include <soci.h>
39 #include <preferences/Preferences.hpp>
40 #include <wallet/common/AbstractAccount.hpp>
41 #include <api/Amount.hpp>
42 #include <api/AmountCallback.hpp>
43 #include <api/OperationListCallback.hpp>
44 #include <api/BitcoinLikeOutput.hpp>
45 #include <api/BitcoinLikePickingStrategy.hpp>
46 #include <api/BitcoinLikeTransactionRequest.hpp>
47 #include <api/BitcoinLikePreparedTransaction.hpp>
48 #include <api/BigIntListCallback.hpp>
49 #include <wallet/bitcoin/types.h>
50 #include <wallet/bitcoin/transaction_builders/BitcoinLikeUtxoPicker.h>
51 
52 #include <wallet/bitcoin/synchronizers/BitcoinLikeAccountSynchronizer.hpp>
53 
54 namespace ledger {
55  namespace core {
56  class Operation;
57  class BitcoinLikeUtxoPicker;
59  public:
60  static const int FLAG_NEW_TRANSACTION = 0x01;
61  static const int FLAG_TRANSACTION_UPDATED = 0x01 << 1;
62  static const int FLAG_TRANSACTION_IGNORED = 0x00;
63  static const int FLAG_TRANSACTION_ON_PREVIOUSLY_EMPTY_ADDRESS = 0x01 << 2;
64  static const int FLAG_TRANSACTION_ON_USED_ADDRESS = 0x01 << 3;
65  static const int FLAG_TRANSACTION_CREATED_SENDING_OPERATION = 0x01 << 4;
66  static const int FLAG_TRANSACTION_CREATED_RECEPTION_OPERATION = 0x01 << 5;
67 
68  BitcoinLikeAccount(const std::shared_ptr<AbstractWallet>& wallet,
69  int32_t index,
70  const std::shared_ptr<BitcoinLikeBlockchainExplorer>& explorer,
71  const std::shared_ptr<BitcoinLikeBlockchainObserver>& observer,
72  const std::shared_ptr<BitcoinLikeAccountSynchronizer>& synchronizer,
73  const std::shared_ptr<BitcoinLikeKeychain>& keychain
74  );
75 
76  std::shared_ptr<api::BitcoinLikeAccount> asBitcoinLikeAccount() override;
77 
83  int putTransaction(soci::session& sql, const BitcoinLikeBlockchainExplorerTransaction& transaction);
89  bool putBlock(soci::session& sql, const BitcoinLikeBlockchainExplorer::Block& block);
90 
91  std::shared_ptr<BitcoinLikeKeychain> getKeychain() const;
92 
93  void startBlockchainObservation() override;
94  void stopBlockchainObservation() override;
95  bool isObservingBlockchain() override;
96 
97  /***
98  * REVIEW
99  */
100 
101  bool isSynchronizing() override;
102 
103  FuturePtr<ledger::core::Amount> getBalance() override;
104 
105  Future<std::vector<std::shared_ptr<api::Amount>>> getBalanceHistory(const std::string & start,
106  const std::string & end,
107  api::TimePeriod precision) override;
108 
109  FuturePtr<BitcoinLikeBlockchainExplorerTransaction> getTransaction(const std::string& hash);
110 
111  std::shared_ptr<api::EventBus> synchronize() override;
112 
113  void broadcastRawTransaction(const std::vector<uint8_t> &transaction,
114  const std::shared_ptr<api::StringCallback> &callback) override;
115 
116  void broadcastTransaction(const std::shared_ptr<api::BitcoinLikeTransaction> &transaction,
117  const std::shared_ptr<api::StringCallback> &callback) override;
118 
119  std::shared_ptr<api::BitcoinLikeTransactionBuilder> buildTransaction(std::experimental::optional<bool> partial) override;
120 
121  std::shared_ptr<api::OperationQuery> queryOperations() override;
122 
123  void getUTXO(int32_t from, int32_t to,
124  const std::shared_ptr<api::BitcoinLikeOutputListCallback> &callback) override;
126  Future<std::vector<std::shared_ptr<api::BitcoinLikeOutput>>> getUTXO(int32_t from, int32_t to);
127  void getUTXOCount(const std::shared_ptr<api::I32Callback> &callback) override;
128  Future<int32_t> getUTXOCount();
129 
130  Future<AddressList> getFreshPublicAddresses() override;
131 
132  Future<std::string> broadcastTransaction(const std::vector<uint8_t>& transaction);
133 
134  std::string getRestoreKey() override;
135 
136  const std::shared_ptr<BitcoinLikeBlockchainExplorer>& getExplorer() const;
137 
138  Future<api::ErrorCode> eraseDataSince(const std::chrono::system_clock::time_point & date) override ;
139 
140  void getFees(const std::shared_ptr<api::BigIntListCallback> & callback) override ;
141  protected:
142  bool checkIfWalletIsEmpty();
143 
144  private:
145  std::shared_ptr<BitcoinLikeAccount> getSelf();
146  inline void inflateOperation(Operation& out,
148  inline void computeOperationTrust(Operation& operation,
150 
151  private:
152  std::shared_ptr<BitcoinLikeKeychain> _keychain;
153  std::shared_ptr<BitcoinLikeBlockchainExplorer> _explorer;
154  std::shared_ptr<BitcoinLikeAccountSynchronizer> _synchronizer;
155  std::shared_ptr<BitcoinLikeBlockchainObserver> _observer;
156  std::shared_ptr<BitcoinLikeUtxoPicker> _picker;
157  std::shared_ptr<api::EventBus> _currentSyncEventBus;
158  std::mutex _synchronizationLock;
159  uint64_t _currentBlockHeight;
160  };
161  }
162 }
163 
164 
165 #endif //LEDGER_CORE_BITCOINLIKEACCOUNT_HPP
Definition: BitcoinLikeAccount.hpp:58
Definition: BitcoinLikeAccount.hpp:29
std::shared_ptr< api::BitcoinLikeAccount > asBitcoinLikeAccount() override
Definition: BitcoinLikeAccount.cpp:670
Definition: BitcoinLikeBlockchainExplorer.hpp:78
Definition: AbstractAccount.hpp:55
std::shared_ptr< api::EventBus > synchronize() override
Definition: BitcoinLikeAccount.cpp:273
bool isObservingBlockchain() override
Definition: BitcoinLikeAccount.cpp:512
Definition: Deffered.hpp:49
void startBlockchainObservation() override
Definition: BitcoinLikeAccount.cpp:504
void stopBlockchainObservation() override
Definition: BitcoinLikeAccount.cpp:508
int putTransaction(soci::session &sql, const BitcoinLikeBlockchainExplorerTransaction &transaction)
Definition: BitcoinLikeAccount.cpp:110
Definition: Operation.h:53
std::shared_ptr< api::OperationQuery > queryOperations() override
Definition: BitcoinLikeAccount.cpp:367
Definition: Account.cpp:8
bool isSynchronizing() override
Definition: BitcoinLikeAccount.cpp:268
std::string getRestoreKey() override
Definition: BitcoinLikeAccount.cpp:674
bool putBlock(soci::session &sql, const BitcoinLikeBlockchainExplorer::Block &block)
Definition: BitcoinLikeAccount.cpp:342
Definition: Block.h:40
Definition: optional.hpp:177