Electroneum
wallet.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #ifndef WALLET_IMPL_H
33 #define WALLET_IMPL_H
34 
35 #include "wallet/wallet2_api.h"
36 #include "wallet/wallet2.h"
37 
38 #include <string>
39 #include <boost/thread/mutex.hpp>
40 #include <boost/thread/thread.hpp>
41 #include <boost/thread/condition_variable.hpp>
42 
43 
44 namespace Electroneum {
45 class TransactionHistoryImpl;
46 class PendingTransactionImpl;
47 class UnsignedTransactionImpl;
48 class AddressBookImpl;
49 struct Wallet2CallbackImpl;
50 
51 class WalletImpl : public Wallet
52 {
53 public:
54  WalletImpl(bool testnet = false);
55  ~WalletImpl();
56  bool create(const std::string &path, const std::string &password,
57  const std::string &language);
58  bool createWatchOnly(const std::string &path, const std::string &password,
59  const std::string &language) const;
60  bool open(const std::string &path, const std::string &password);
61  bool recover(const std::string &path, const std::string &seed);
62  bool recoverFromKeys(const std::string &path,
63  const std::string &language,
64  const std::string &address_string,
65  const std::string &viewkey_string,
66  const std::string &spendkey_string = "");
67  bool close();
68  std::string seed() const;
69  std::string getSeedLanguage() const;
70  void setSeedLanguage(const std::string &arg);
71  // void setListener(Listener *) {}
72  int status() const;
73  std::string errorString() const;
74  bool setPassword(const std::string &password);
75  std::string address() const;
76  std::string integratedAddress(const std::string &payment_id) const;
77  std::string path() const;
78  std::string secretViewKey() const;
79  std::string publicViewKey() const;
80  std::string secretSpendKey() const;
81  std::string publicSpendKey() const;
82  bool store(const std::string &path);
83  std::string filename() const;
84  std::string keysFilename() const;
85  bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "");
86  bool connectToDaemon();
88  void setTrustedDaemon(bool arg);
89  bool trustedDaemon() const;
90  uint64_t balance() const;
91  uint64_t unlockedBalance() const;
92  uint64_t blockChainHeight() const;
93  uint64_t approximateBlockChainHeight() const;
94  uint64_t daemonBlockChainHeight() const;
95  uint64_t daemonBlockChainTargetHeight() const;
96  bool synchronized() const;
97  bool refresh();
98  void refreshAsync();
99  void setAutoRefreshInterval(int millis);
100  int autoRefreshInterval() const;
101  void setRefreshFromBlockHeight(uint64_t refresh_from_block_height);
103  void setRecoveringFromSeed(bool recoveringFromSeed);
104  bool watchOnly() const;
105  bool rescanSpent();
106  bool testnet() const {return m_wallet->testnet();}
107  void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const;
108  bool useForkRules(uint8_t version, int64_t early_blocks) const;
109 
110  PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id,
111  optional<uint64_t> amount, uint32_t mixin_count,
114  bool submitTransaction(const std::string &fileName);
115  virtual UnsignedTransaction * loadUnsignedTx(const std::string &unsigned_filename);
116  bool exportKeyImages(const std::string &filename);
117  bool importKeyImages(const std::string &filename);
118 
119  virtual void disposeTransaction(PendingTransaction * t);
120  virtual TransactionHistory * history() const;
121  virtual AddressBook * addressBook() const;
122  virtual void setListener(WalletListener * l);
123  virtual bool setUserNote(const std::string &txid, const std::string &note);
124  virtual std::string getUserNote(const std::string &txid) const;
125  virtual std::string getTxKey(const std::string &txid) const;
126  virtual std::string signMessage(const std::string &message);
127  virtual bool verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const;
128  virtual void startRefresh();
129  virtual void pauseRefresh();
130  virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error);
131  virtual std::string getDefaultDataDir() const;
132 
133 private:
134  void clearStatus() const;
135  void refreshThreadFunc();
136  void doRefresh();
137  bool daemonSynced() const;
138  void stopRefresh();
139  bool isNewWallet() const;
140  bool doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit);
141 
142 private:
144  friend class UnsignedTransactionImpl;
146  friend struct Wallet2CallbackImpl;
147  friend class AddressBookImpl;
148 
150  mutable std::atomic<int> m_status;
151  mutable std::string m_errorString;
152  std::string m_password;
158 
159  // multi-threaded refresh stuff
160  std::atomic<bool> m_refreshEnabled;
161  std::atomic<bool> m_refreshThreadDone;
162  std::atomic<int> m_refreshIntervalMillis;
163  // synchronizing refresh loop;
164  boost::mutex m_refreshMutex;
165 
166  // synchronizing sync and async refresh
167  boost::mutex m_refreshMutex2;
168  boost::condition_variable m_refreshCV;
169  boost::thread m_refreshThread;
170  // flag indicating wallet is recovering from seed
171  // so it shouldn't be considered as new and pull blocks (slow-refresh)
172  // instead of pulling hashes (fast-refresh)
173  std::atomic<bool> m_recoveringFromSeed;
174  std::atomic<bool> m_synchronized;
175  std::atomic<bool> m_rebuildWalletCache;
176  // cache connection status to avoid unnecessary RPC calls
177  mutable std::atomic<bool> m_is_connected;
178  boost::optional<epee::net_utils::http::login> m_daemon_login{};
179 };
180 
181 
182 } // namespace
183 
184 namespace Bitelectroneum = Electroneum;
185 
186 #endif
187 
WalletImpl(bool testnet=false)
Definition: wallet.cpp:273
virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector< std::string > &unknown_parameters, std::string &error)
Definition: wallet.cpp:1399
virtual void setListener(WalletListener *l)
Definition: wallet.cpp:1164
void setSeedLanguage(const std::string &arg)
Definition: wallet.cpp:582
Definition: wallet.h:51
bool recoverFromKeys(const std::string &path, const std::string &language, const std::string &address_string, const std::string &viewkey_string, const std::string &spendkey_string="")
Definition: wallet.cpp:394
uint64_t unlockedBalance() const
Definition: wallet.cpp:700
bool testnet() const
Definition: wallet2.h:416
std::string getSeedLanguage() const
Definition: wallet.cpp:577
std::string publicViewKey() const
publicViewKey - returns public view key
Definition: wallet.cpp:634
virtual void disposeTransaction(PendingTransaction *t)
disposeTransaction - destroys transaction object
Definition: wallet.cpp:1149
Wallet2CallbackImpl * m_wallet2Callback
Definition: wallet.h:156
boost::mutex m_refreshMutex
Definition: wallet.h:164
bool rescanSpent()
Definition: wallet.cpp:1409
boost::thread m_refreshThread
Definition: wallet.h:169
Definition: wallet2.h:97
uint64_t approximateBlockChainHeight() const
approximateBlockChainHeight - returns approximate blockchain height calculated from date/time ...
Definition: wallet.cpp:709
void refreshAsync()
refreshAsync - refreshes wallet asynchronously.
Definition: wallet.cpp:774
int status() const
returns wallet status (Status_Ok | Status_Error)
Definition: wallet.cpp:587
std::atomic< bool > m_rebuildWalletCache
Definition: wallet.h:175
virtual bool verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const
verifySignedMessage - verify a signature matches a given message
Definition: wallet.cpp:1216
WalletListener * m_walletListener
Definition: wallet.h:155
std::string publicSpendKey() const
publicSpendKey - returns public spend key
Definition: wallet.cpp:644
Definition: wallet.cpp:66
std::string integratedAddress(const std::string &payment_id) const
integratedAddress - returns integrated address for current wallet address and given payment_id...
Definition: wallet.cpp:615
uint64_t balance() const
Definition: wallet.cpp:695
std::string path() const
Definition: wallet.cpp:624
virtual PendingTransaction * createSweepUnmixableTransaction()
createSweepUnmixableTransaction creates transaction with unmixable outputs.
Definition: wallet.cpp:1057
bool recover(const std::string &path, const std::string &seed)
Definition: wallet.cpp:512
Definition: wallet2_api.h:48
bool daemonSynced() const
Definition: wallet.cpp:754
Priority
Definition: wallet2_api.h:71
void setAutoRefreshInterval(int millis)
setAutoRefreshInterval - setup interval for automatic refresh.
Definition: wallet.cpp:781
virtual UnsignedTransaction * loadUnsignedTx(const std::string &unsigned_filename)
loadUnsignedTx - creates transaction from unsigned tx file
Definition: wallet.cpp:797
ConnectionStatus connected() const
connected - checks if the wallet connected to the daemon
Definition: wallet.cpp:1240
void setRefreshFromBlockHeight(uint64_t refresh_from_block_height)
setRefreshFromBlockHeight - start refresh from block height on recover
Definition: wallet.cpp:685
Definition: unsigned_transaction.h:42
Definition: pending_transaction.h:42
bool setPassword(const std::string &password)
Definition: wallet.cpp:597
std::atomic< int > m_status
Definition: wallet.h:150
bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit=0, const std::string &daemon_username="", const std::string &daemon_password="")
init - initializes wallet with daemon connection params. if daemon_address is local address...
Definition: wallet.cpp:677
boost::optional< epee::net_utils::http::login > m_daemon_login
Definition: wallet.h:178
uint64_t daemonBlockChainTargetHeight() const
daemonBlockChainTargetHeight - returns daemon blockchain target height
Definition: wallet.cpp:732
ConnectionStatus
Definition: wallet2_api.h:283
void refreshThreadFunc()
Definition: wallet.cpp:1272
bool store(const std::string &path)
store - stores wallet to file.
Definition: wallet.cpp:649
std::string secretSpendKey() const
secretSpendKey - returns secret spend key
Definition: wallet.cpp:639
bool importKeyImages(const std::string &filename)
importKeyImages - imports key images from file
Definition: wallet.cpp:864
virtual void startRefresh()
StartRefresh - Start/resume refresh thread (refresh every 10 seconds)
Definition: wallet.cpp:1333
void setTrustedDaemon(bool arg)
Definition: wallet.cpp:1251
std::string address() const
Definition: wallet.cpp:610
std::string errorString() const
in case error status, returns error string
Definition: wallet.cpp:592
void stopRefresh()
Definition: wallet.cpp:1344
std::atomic< bool > m_is_connected
Definition: wallet.h:177
int autoRefreshInterval() const
autoRefreshInterval - returns automatic refresh interval in millis
Definition: wallet.cpp:792
Interface for wallet operations. TODO: check if /include/IWallet.h is still actual.
Definition: wallet2_api.h:274
bool trustedDaemon() const
Definition: wallet.cpp:1256
std::string seed() const
Definition: wallet.cpp:569
TransactionHistoryImpl * m_history
Definition: wallet.h:153
Transaction-like interface for sending money.
Definition: wallet2_api.h:63
uint64_t get_refresh_from_block_height() const
Definition: wallet2.h:375
std::atomic< bool > m_refreshEnabled
Definition: wallet.h:160
void clearStatus() const
Definition: wallet.cpp:1266
AddressBookImpl * m_addressBook
Definition: wallet.h:157
boost::condition_variable m_refreshCV
Definition: wallet.h:168
virtual std::string getTxKey(const std::string &txid) const
Definition: wallet.cpp:1191
bool isNewWallet() const
Definition: wallet.cpp:1364
std::string m_errorString
Definition: wallet.h:151
void doRefresh()
Definition: wallet.cpp:1302
bool refresh()
refresh - refreshes the wallet, updating transactions from daemon
Definition: wallet.cpp:767
virtual std::string signMessage(const std::string &message)
Definition: wallet.cpp:1211
std::atomic< bool > m_refreshThreadDone
Definition: wallet.h:161
std::atomic< bool > m_synchronized
Definition: wallet.h:174
Definition: address_book.cpp:41
int l
Definition: base.py:3
bool close()
Definition: wallet.cpp:543
std::string secretViewKey() const
secretViewKey - returns secret view key
Definition: wallet.cpp:629
uint64_t blockChainHeight() const
blockChainHeight - returns current blockchain height
Definition: wallet.cpp:705
bool createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const
createWatchOnly - Creates a watch only wallet
Definition: wallet.cpp:354
uint8_t version
Definition: blockchain.cpp:86
Definition: address_book.h:39
void hardForkInfo(uint8_t &version, uint64_t &earliest_height) const
returns current hard fork info
Definition: wallet.cpp:1429
bool connectToDaemon()
connectToDaemon - connects to the daemon. TODO: check if it can be removed
Definition: wallet.cpp:1228
tools::wallet2 * m_wallet
Definition: wallet.h:149
POD_CLASS signature
Definition: crypto.h:99
virtual bool setUserNote(const std::string &txid, const std::string &note)
Definition: wallet.cpp:1170
internal::NamedArgWithType< char, T > arg(StringRef name, const T &arg)
Definition: format.h:3450
std::string keysFilename() const
keysFilename - returns keys filename. usually this formed as "wallet_filename".keys ...
Definition: wallet.cpp:672
bool exportKeyImages(const std::string &filename)
exportKeyImages - exports key images to file
Definition: wallet.cpp:836
bool create(const std::string &path, const std::string &password, const std::string &language)
Definition: wallet.cpp:317
virtual std::string getUserNote(const std::string &txid) const
getUserNote - return an arbitrary string note attached to a txid
Definition: wallet.cpp:1181
Definition: wallet2_api.h:227
bool submitTransaction(const std::string &fileName)
submitTransaction - submits transaction in signed tx file
Definition: wallet.cpp:816
bool testnet() const
Definition: wallet.h:106
void setRecoveringFromSeed(bool recoveringFromSeed)
setRecoveringFromSeed - set state recover form seed
Definition: wallet.cpp:690
The TransactionHistory - interface for displaying transaction history.
Definition: wallet2_api.h:171
virtual AddressBook * addressBook() const
Definition: wallet.cpp:1159
bool open(const std::string &path, const std::string &password)
Definition: wallet.cpp:487
Definition: transaction_history.h:39
uint64_t getRefreshFromBlockHeight() const
getRestoreHeight - get wallet creation height
Definition: wallet.h:102
bool useForkRules(uint8_t version, int64_t early_blocks) const
check if hard fork rules should be used
Definition: wallet.cpp:1434
PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id, optional< uint64_t > amount, uint32_t mixin_count, PendingTransaction::Priority priority=PendingTransaction::Priority_Low)
createTransaction creates transaction. if dst_addr is an integrated address, payment_id is ignored ...
Definition: wallet.cpp:894
bool watchOnly() const
watchOnly - checks if wallet is watch only
Definition: wallet.cpp:1261
uint64_t daemonBlockChainHeight() const
daemonBlockChainHeight - returns daemon blockchain height
Definition: wallet.cpp:713
bool doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit)
Definition: wallet.cpp:1374
std::atomic< int > m_refreshIntervalMillis
Definition: wallet.h:162
virtual TransactionHistory * history() const
Definition: wallet.cpp:1154
virtual std::string getDefaultDataDir() const
Definition: wallet.cpp:1404
Transaction-like interface for sending money.
Definition: wallet2_api.h:97
virtual void pauseRefresh()
pauseRefresh - pause refresh thread
Definition: wallet.cpp:1354
std::string filename() const
filename - returns wallet filename
Definition: wallet.cpp:667
std::string m_password
Definition: wallet.h:152
~WalletImpl()
Definition: wallet.cpp:300
The AddressBook - interface for Book.
Definition: wallet2_api.h:209
boost::mutex m_refreshMutex2
Definition: wallet.h:167
bool m_trustedDaemon
Definition: wallet.h:154
std::atomic< bool > m_recoveringFromSeed
Definition: wallet.h:173