ledger-core
AbstractWallet.hpp
1 /*
2  *
3  * AbstractWallet
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 27/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_ABSTRACTWALLET_HPP
32 #define LEDGER_CORE_ABSTRACTWALLET_HPP
33 
34 #include "api/Wallet.hpp"
35 #include <api/Currency.hpp>
36 #include <api/Account.hpp>
37 #include <preferences/Preferences.hpp>
38 #include <async/DedicatedContext.hpp>
39 #include <events/EventPublisher.hpp>
40 #include <debug/logger.hpp>
41 #include <api/WalletType.hpp>
42 #include <database/DatabaseSessionPool.hpp>
43 #include <collections/DynamicObject.hpp>
44 #include <utils/DerivationScheme.hpp>
45 #include <api/AccountCreationInfo.hpp>
46 #include <api/ExtendedKeyAccountCreationInfo.hpp>
47 #include <api/AccountCreationInfoCallback.hpp>
48 #include <api/ExtendedKeyAccountCreationInfoCallback.hpp>
49 #include <api/AccountCallback.hpp>
50 #include <api/Block.hpp>
51 #include <api/BlockCallback.hpp>
52 #include <api/DynamicObject.hpp>
53 #include <utils/TTLCache.h>
54 #include <wallet/common/Amount.h>
55 
56 namespace ledger {
57  namespace core {
58 
59  class WalletPool;
60  class AbstractAccount;
61  class AbstractWallet : public virtual api::Wallet, public DedicatedContext, public virtual std::enable_shared_from_this<AbstractWallet> {
62  public:
63  AbstractWallet(const std::string& walletName,
64  const api::Currency& currency,
65  const std::shared_ptr<WalletPool>& pool,
66  const std::shared_ptr<DynamicObject>& configuration,
67  const DerivationScheme& derivationScheme
68  );
69  std::shared_ptr<api::EventBus> getEventBus() override;
70  std::shared_ptr<api::Preferences> getPreferences() override;
71  bool isInstanceOfBitcoinLikeWallet() override;
72  bool isInstanceOfEthereumLikeWallet() override;
73  bool isInstanceOfRippleLikeWallet() override;
74  std::shared_ptr<api::Logger> getLogger() override;
75  api::WalletType getWalletType() override;
76  std::shared_ptr<api::Preferences> getAccountPreferences(int32_t index) override;
77  std::shared_ptr<WalletPool> getPool() const;
78  std::shared_ptr<api::BitcoinLikeWallet> asBitcoinLikeWallet() override;
79 
80  api::Currency getCurrency() override;
81  const api::Currency& getCurrency() const;
82  std::string getName() override;
83 
84  void getNextAccountIndex(const std::shared_ptr<api::I32Callback> &callback) override;
85  Future<int32_t> getNextAccountIndex();
86  Future<int32_t> getAccountCount();
87  void getAccountCount(const std::shared_ptr<api::I32Callback> &callback) override;
88 
89  void getLastBlock(const std::shared_ptr<api::BlockCallback> &callback) override;
90  Future<api::Block> getLastBlock();
91 
92  template <typename T>
93  std::shared_ptr<T> asInstanceOf() {
94  auto type = getWalletType();
95  if (type == T::type) {
96  return std::dynamic_pointer_cast<T>(shared_from_this());
97  }
98  throw make_exception(api::ErrorCode::BAD_CAST, "Wallet of type {} cannot be cast to {}", api::to_string(type), api::to_string(T::type));
99  };
100 
101  void getAccount(int32_t index, const std::shared_ptr<api::AccountCallback> &callback) override;
102  FuturePtr<api::Account> getAccount(int32_t index);
103  void
104  getAccounts(int32_t offset, int32_t count, const std::shared_ptr<api::AccountListCallback> &callback) override;
105  Future<std::vector<std::shared_ptr<api::Account>>> getAccounts(int32_t offset, int32_t count);
106 
107  void getNextAccountCreationInfo(const std::shared_ptr<api::AccountCreationInfoCallback> &callback) override;
108  void getNextExtendedKeyAccountCreationInfo(
109  const std::shared_ptr<api::ExtendedKeyAccountCreationInfoCallback> &callback) override;
110 
111  void getAccountCreationInfo(int32_t accountIndex,
112  const std::shared_ptr<api::AccountCreationInfoCallback> &callback) override;
113 
114  void getExtendedKeyAccountCreationInfo(int32_t accountIndex,
115  const std::shared_ptr<api::ExtendedKeyAccountCreationInfoCallback> &callback) override;
116 
117  void newAccountWithInfo(const api::AccountCreationInfo &accountCreationInfo,
118  const std::shared_ptr<api::AccountCallback> &callback) override;
119 
120  void
121  newAccountWithExtendedKeyInfo(const api::ExtendedKeyAccountCreationInfo &extendedKeyAccountCreationInfo,
122  const std::shared_ptr<api::AccountCallback> &callback) override;
123 
124  void eraseDataSince(const std::chrono::system_clock::time_point & date, const std::shared_ptr<api::ErrorCodeCallback> & callback) override ;
125  Future<api::ErrorCode> eraseDataSince(const std::chrono::system_clock::time_point & date);
126 
127  std::shared_ptr<api::DynamicObject> getConfiguration() override;
128 
129  Option<Amount> getBalanceFromCache(size_t accountIndex);
130  void updateBalanceCache(size_t accountIndex, Amount balance);
131 
132  virtual FuturePtr<api::Account> newAccountWithInfo(const api::AccountCreationInfo& info) = 0;
133  virtual FuturePtr<api::Account> newAccountWithExtendedKeyInfo(const api::ExtendedKeyAccountCreationInfo& info) = 0;
134  virtual Future<api::ExtendedKeyAccountCreationInfo> getExtendedKeyAccountCreationInfo(int32_t accountIndex) = 0;
135  virtual Future<api::AccountCreationInfo> getAccountCreationInfo(int32_t accountIndex) = 0;
136  virtual Future<api::AccountCreationInfo> getNextAccountCreationInfo();
137  virtual Future<api::ExtendedKeyAccountCreationInfo> getNextExtendedKeyAccountCreationInfo();
138  public:
139  virtual std::shared_ptr<Preferences> getAccountExternalPreferences(int32_t index);
140  virtual std::shared_ptr<Preferences> getAccountInternalPreferences(int32_t index);
141  virtual std::shared_ptr<Preferences> getInternalPreferences() const;
142  virtual std::shared_ptr<Preferences> getExternalPreferences() const;
143  virtual std::shared_ptr<EventPublisher> getEventPublisher() const;
144  virtual std::shared_ptr<spdlog::logger> logger() const;
145  virtual std::shared_ptr<DatabaseSessionPool> getDatabase() const;
146  virtual std::shared_ptr<api::ExecutionContext> getMainExecutionContext() const;
147  virtual std::string getWalletUid() const;
148  virtual std::shared_ptr<DynamicObject> getConfig() const;
149  virtual const DerivationScheme& getDerivationScheme() const;
150 
151  protected:
152  virtual std::shared_ptr<AbstractAccount> createAccountInstance(soci::session& sql, const std::string& accountUid) = 0;
153  void addAccountInstanceToInstanceCache(const std::shared_ptr<AbstractAccount>& account);
154 
155  private:
156  std::string _name;
157  std::string _uid;
158  std::shared_ptr<spdlog::logger> _logger;
159  std::shared_ptr<api::Logger> _loggerApi;
160  std::shared_ptr<EventPublisher> _publisher;
161  std::shared_ptr<Preferences> _externalPreferences;
162  std::shared_ptr<Preferences> _internalPreferences;
163  std::shared_ptr<DatabaseSessionPool> _database;
164  api::Currency _currency;
165  std::shared_ptr<api::ExecutionContext> _mainExecutionContext;
166  std::shared_ptr<DynamicObject> _configuration;
167  DerivationScheme _scheme;
168  std::weak_ptr<WalletPool> _pool;
169  std::unordered_map<int32_t, std::shared_ptr<AbstractAccount>> _accounts;
170  TTLCache<std::string, Amount> _balanceCache;
171  };
172  }
173 }
174 
175 
176 #endif //LEDGER_CORE_ABSTRACTWALLET_HPP
api::WalletType getWalletType() override
Definition: AbstractWallet.cpp:116
bool isInstanceOfRippleLikeWallet() override
Definition: AbstractWallet.cpp:92
api::Currency getCurrency() override
Definition: AbstractWallet.cpp:136
Definition: AbstractWallet.hpp:61
std::shared_ptr< api::Preferences > getPreferences() override
Definition: AbstractWallet.cpp:80
std::shared_ptr< api::EventBus > getEventBus() override
Definition: AbstractWallet.cpp:76
bool isInstanceOfEthereumLikeWallet() override
Definition: AbstractWallet.cpp:88
Definition: Option.hpp:49
std::shared_ptr< api::Logger > getLogger() override
Definition: AbstractWallet.cpp:112
std::shared_ptr< api::DynamicObject > getConfiguration() override
Definition: AbstractWallet.cpp:355
Definition: Deffered.hpp:49
Definition: ExtendedKeyAccountCreationInfo.hpp:16
Definition: DerivationScheme.hpp:55
std::shared_ptr< api::BitcoinLikeWallet > asBitcoinLikeWallet() override
Definition: AbstractWallet.cpp:144
std::shared_ptr< api::Preferences > getAccountPreferences(int32_t index) override
Definition: AbstractWallet.cpp:128
Definition: Wallet.hpp:39
Definition: Currency.hpp:23
Definition: Account.cpp:8
WalletType
Definition: WalletType.hpp:20
bool isInstanceOfBitcoinLikeWallet() override
Definition: AbstractWallet.cpp:84
std::string getName() override
Definition: AbstractWallet.cpp:140
Definition: DedicatedContext.hpp:39
Definition: Amount.h:41
Definition: AccountCreationInfo.hpp:16
Definition: TTLCache.h:45
Definition: logger.hpp:44