Monero C++ Library
monero_wallet_core.h
1 
53 #pragma once
54 
55 #include "monero_wallet.h"
56 #include "wallet/wallet2.h"
57 
58 #include <boost/thread/mutex.hpp>
59 #include <boost/thread/thread.hpp>
60 #include <boost/thread/condition_variable.hpp>
61 
62 using namespace std;
63 using namespace crypto;
64 using namespace monero;
65 
69 namespace monero {
70 
71  // -------------------------------- LISTENERS -------------------------------
72 
73  // forward declaration of internal wallet2 listener
74  struct wallet2_listener;
75 
76  // --------------------------- STATIC WALLET UTILS --------------------------
77 
82 
83  public:
84 
91  static bool wallet_exists(const string& path);
92 
100  static monero_wallet_core* open_wallet(const string& path, const string& password, const monero_network_type network_type);
101 
111  static monero_wallet_core* create_wallet_random(const string& path, const string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection, const string& language = "English");
112 
124  static monero_wallet_core* create_wallet_from_mnemonic(const string& path, const string& password, const monero_network_type network_type, const string& mnemonic, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const string& seed_offset);
125 
139  static monero_wallet_core* create_wallet_from_keys(const string& path, const string& password, const monero_network_type network_type, const string& address, const string& view_key, const string& spend_key, const monero_rpc_connection& daemon_connection, uint64_t restore_height, const string& language);
140 
146  static vector<string> get_mnemonic_languages();
147 
148  // ----------------------------- WALLET METHODS -----------------------------
149 
154 
158  void set_daemon_connection(const string& uri, const string& username = "", const string& password = "");
159  void set_daemon_connection(const monero_rpc_connection& connection);
160  shared_ptr<monero_rpc_connection> get_daemon_connection() const;
161  bool is_connected() const;
162  bool is_daemon_synced() const;
163  bool is_daemon_trusted() const;
164  bool is_synced() const;
165  monero_version get_version() const;
166  string get_path() const;
167  monero_network_type get_network_type() const;
168  string get_mnemonic() const;
169  string get_mnemonic_language() const;
170  string get_public_view_key() const;
171  string get_private_view_key() const;
172  string get_public_spend_key() const;
173  string get_private_spend_key() const;
174  string get_address(const uint32_t account_idx, const uint32_t subaddress_idx) const;
175  monero_subaddress get_address_index(const string& address) const;
176  monero_integrated_address get_integrated_address(const string& standard_address = "", const string& payment_id = "") const;
177  monero_integrated_address decode_integrated_address(const string& integrated_address) const;
178  uint64_t get_height() const;
179  uint64_t get_restore_height() const;
180  void set_restore_height(uint64_t restore_height);
181  uint64_t get_daemon_height() const;
182  uint64_t get_daemon_max_peer_height() const;
183  void add_listener(monero_wallet_listener& listener);
184  void remove_listener(monero_wallet_listener& listener);
185  set<monero_wallet_listener*> get_listeners();
186  monero_sync_result sync();
188  monero_sync_result sync(uint64_t start_height);
189  monero_sync_result sync(uint64_t start_height, monero_sync_listener& listener);
190  void start_syncing();
191  void stop_syncing();
192  void rescan_spent();
193  void rescan_blockchain();
194  uint64_t get_balance() const;
195  uint64_t get_balance(uint32_t account_idx) const;
196  uint64_t get_balance(uint32_t account_idx, uint32_t subaddress_idx) const;
197  uint64_t get_unlocked_balance() const;
198  uint64_t get_unlocked_balance(uint32_t account_idx) const;
199  uint64_t get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const;
200  vector<monero_account> get_accounts(bool include_subaddresses, const string& tag) const;
201  monero_account get_account(const uint32_t account_idx, bool include_subaddresses) const;
202  monero_account create_account(const string& label = "");
203  vector<monero_subaddress> get_subaddresses(const uint32_t account_idx, const vector<uint32_t>& subaddress_indices) const;
204  monero_subaddress create_subaddress(uint32_t account_idx, const string& label = "");
205  vector<shared_ptr<monero_tx_wallet>> get_txs() const;
206  vector<shared_ptr<monero_tx_wallet>> get_txs(const monero_tx_query& query) const;
207  vector<shared_ptr<monero_transfer>> get_transfers(const monero_transfer_query& query) const;
208  vector<shared_ptr<monero_output_wallet>> get_outputs(const monero_output_query& query) const;
209  string get_outputs_hex() const;
210  int import_outputs_hex(const string& outputs_hex);
211  vector<shared_ptr<monero_key_image>> get_key_images() const;
212  shared_ptr<monero_key_image_import_result> import_key_images(const vector<shared_ptr<monero_key_image>>& key_images);
213  monero_tx_set create_tx(monero_send_request& request);
214  monero_tx_set create_tx(uint32_t account_index, string address, uint64_t amount);
215  monero_tx_set create_tx(int account_index, string address, uint64_t amount, monero_send_priority priority);
216  monero_tx_set create_txs(monero_send_request& request);
217  string relay_tx(const string& tx_metadata);
218  string relay_tx(const monero_tx_wallet& tx);
219  vector<string> relay_txs(const vector<string>& tx_metadatas);
220  vector<string> relay_txs(const vector<shared_ptr<monero_tx_wallet>>& txs);
221  monero_tx_set send(const monero_send_request& request);
222  monero_tx_set send(uint32_t account_index, string address, uint64_t amount);
223  monero_tx_set send(uint32_t account_index, string address, uint64_t amount, monero_send_priority priority);
224  monero_tx_set send_split(const monero_send_request& request);
225  vector<monero_tx_set> sweep_unlocked(const monero_send_request& request);
226  monero_tx_set sweep_output(const monero_send_request& request);
227  monero_tx_set sweep_dust(bool do_not_relay = false);
228  monero_tx_set parse_tx_set(const monero_tx_set& tx_set);
229  string sign(const string& msg) const;
230  bool verify(const string& msg, const string& address, const string& signature) const;
231  string get_tx_key(const string& tx_hash) const;
232  shared_ptr<monero_check_tx> check_tx_key(const string& tx_hash, const string& txKey, const string& address) const;
233  string get_tx_proof(const string& tx_hash, const string& address, const string& message) const;
234  shared_ptr<monero_check_tx> check_tx_proof(const string& tx_hash, const string& address, const string& message, const string& signature) const;
235  string get_spend_proof(const string& tx_hash, const string& message) const;
236  bool check_spend_proof(const string& tx_hash, const string& message, const string& signature) const;
237  string get_reserve_proof_wallet(const string& message) const;
238  string get_reserve_proof_account(uint32_t account_idx, uint64_t amount, const string& message) const;
239  shared_ptr<monero_check_reserve> check_reserve_proof(const string& address, const string& message, const string& signature) const;
240  string get_tx_note(const string& tx_hash) const;
241  vector<string> get_tx_notes(const vector<string>& tx_hashes) const;
242  void set_tx_note(const string& tx_hash, const string& note);
243  void set_tx_notes(const vector<string>& tx_hashes, const vector<string>& notes);
244  vector<monero_address_book_entry> get_address_book_entries(const vector<uint64_t>& indices) const;
245  uint64_t add_address_book_entry(const string& address, const string& description, const string& payment_id = "");
246  void edit_address_book_entry(uint64_t index, bool set_address, const string& address, bool set_description, const string& description, bool set_payment_id = false, const string& payment_id = "");
247  void delete_address_book_entry(uint64_t index);
248  string create_payment_uri(const monero_send_request& request) const;
249  shared_ptr<monero_send_request> parse_payment_uri(const string& uri) const;
250  bool get_attribute(const string& key, string& value) const;
251  void set_attribute(const string& key, const string& val);
252  void start_mining(boost::optional<uint64_t> num_threads, boost::optional<bool> background_mining, boost::optional<bool> ignore_battery);
253  void stop_mining();
254  uint64_t wait_for_next_block();
255  bool is_multisig_import_needed() const;
256  monero_multisig_info get_multisig_info();
257  string prepare_multisig();
258  monero_multisig_init_result make_multisig(const vector<string>& multisig_hexes, int threshold, const string& password);
259  monero_multisig_init_result exchange_multisig_keys(const vector<string>& mutisig_hexes, const string& password);
260  string get_multisig_hex();
261  int import_multisig_hex(const vector<string>& multisig_hexes);
262  monero_multisig_sign_result sign_multisig_tx_hex(const string& multisig_tx_hex);
263  vector<string> submit_multisig_tx_hex(const string& signed_multisig_tx_hex);
264  void save();
265  void move_to(string path, string password);
266  void close(bool save = false);
267 
268  // --------------------------------- PRIVATE --------------------------------
269 
270  private:
271  friend struct wallet2_listener;
272  unique_ptr<tools::wallet2> m_w2; // internal wallet implementation
273  unique_ptr<wallet2_listener> m_w2_listener; // internal wallet implementation listener
274  set<monero_wallet_listener*> m_listeners; // external wallet listeners
275 
276  void init_common();
277  vector<monero_subaddress> get_subaddresses_aux(uint32_t account_idx, const vector<uint32_t>& subaddress_indices, const vector<tools::wallet2::transfer_details>& transfers) const;
278  monero_tx_set sweep_account(const monero_send_request& request); // sweeps unlocked funds within an account; private helper to sweep_unlocked()
279 
280  // blockchain sync management
281  mutable std::atomic<bool> m_is_synced; // whether or not wallet is synced
282  mutable std::atomic<bool> m_is_connected; // cache connection status to avoid unecessary RPC calls
283  boost::condition_variable m_sync_cv; // to make sync threads woke
284  boost::mutex m_sync_mutex; // synchronize sync() and syncAsync() requests
285  std::atomic<bool> m_rescan_on_sync; // whether or not to rescan on sync
286  std::atomic<bool> m_syncing_enabled; // whether or not auto sync is enabled
287  std::atomic<int> m_syncing_interval; // auto sync loop interval in milliseconds
288  boost::thread m_syncing_thread; // thread for auto sync loop
289  boost::mutex m_syncing_mutex; // synchronize auto sync loop
290  std::atomic<bool> m_syncing_thread_done; // whether or not the syncing thread is shut down
291  void sync_thread_func(); // function to run thread with syncing loop
292  monero_sync_result lock_and_sync(boost::optional<uint64_t> start_height = boost::none); // internal function to synchronize request to sync and rescan
293  monero_sync_result sync_aux(boost::optional<uint64_t> start_height = boost::none); // internal function to immediately block, sync, and report progress
294  };
295 }
Definition: monero_wallet_model.h:407
Definition: monero_daemon.cpp:58
Definition: monero_wallet.h:123
Definition: monero_wallet_model.h:318
Definition: monero_wallet.h:90
Definition: monero_wallet.h:72
Definition: monero_wallet_model.h:421
Definition: monero_wallet_model.h:274
Definition: monero_wallet_model.h:431
Definition: monero_daemon_model.h:101
Definition: monero_wallet_model.h:246
monero_send_priority
Definition: monero_wallet_model.h:329
Definition: monero_wallet_model.h:68
Definition: monero_wallet_model.h:304
Definition: monero_wallet_model.h:225
Definition: monero_wallet_model.h:339
Definition: monero_wallet_model.h:97
Definition: monero_daemon_model.h:113
Definition: monero_wallet_model.h:80
Definition: monero_wallet_model.h:183
monero_network_type
Definition: monero_daemon_model.h:92
Definition: monero_wallet_core.h:81
Definition: monero_wallet_core.cpp:657