Monero C++ Library
monero_wallet.h
1 
53 #pragma once
54 
55 #include "monero_wallet_model.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 
79  public:
80 
90  virtual void on_sync_progress(uint64_t height, uint64_t start_height, uint64_t end_height, double percent_done, const string& message) {}
91  };
92 
97  public:
98 
104  virtual void on_new_block(uint64_t height) {};
105 
111  virtual void on_output_received(const monero_output_wallet& output) {};
112 
118  virtual void on_output_spent(const monero_output_wallet& output) {};
119  };
120 
121  // forward declaration of internal wallet2 listener
122  struct wallet2_listener;
123 
124  // ---------------------------- WALLET INTERFACE ----------------------------
125 
130 
131  public:
132 
139  static bool wallet_exists(const string& path);
140 
148  static monero_wallet* open_wallet(const string& path, const string& password, const monero_network_type network_type);
149 
159  static monero_wallet* create_wallet_random(const string& path, const string& password);
160  static monero_wallet* create_wallet_random(const string& path, const string& password, const monero_network_type network_type, const monero_rpc_connection& daemon_connection, const string& language);
161 
172  static monero_wallet* create_wallet_from_mnemonic(const string& path, const string& password, const monero_network_type network_type, const string& mnemonic);
173  static monero_wallet* 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);
174 
188  static monero_wallet* 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);
189  static monero_wallet* 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);
190  static monero_wallet* 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);
191 
195  ~monero_wallet();
196 
202  uint64_t get_height() const;
203 
209  uint64_t get_restore_height() const;
210 
216  void set_restore_height(uint64_t restore_height);
217 
223  uint64_t get_daemon_height() const;
224 
230  uint64_t get_daemon_max_peer_height() const;
231 
239  void set_daemon_connection(const string& uri, const string& username = "", const string& password = "");
240 
246  void set_daemon_connection(const monero_rpc_connection& connection);
247 
253  shared_ptr<monero_rpc_connection> get_daemon_connection() const;
254 
260  bool is_connected() const;
261 
267  bool is_daemon_synced() const;
268 
274  bool is_daemon_trusted() const;
275 
281  bool is_synced() const;
282 
288  string get_path() const;
289 
295  monero_network_type get_network_type() const;
296 
302  string get_seed() const;
303 
309  string get_mnemonic() const;
310 
316  string get_language() const;
317 
323  vector<string> get_languages() const;
324 
330  string get_public_view_key() const;
331 
337  string get_private_view_key() const;
338 
344  string get_public_spend_key() const;
345 
351  string get_private_spend_key() const;
352 
358  string get_primary_address() const;
359 
367  string get_address(const uint32_t account_idx, const uint32_t subaddress_idx) const;
368 
376  monero_subaddress get_address_index(const string& address) const;
377 
385  monero_integrated_address get_integrated_address(const string& standard_address = "", const string& payment_id = "") const;
386 
393  monero_integrated_address decode_integrated_address(const string& integrated_address) const;
394 
400  void add_listener(monero_wallet_listener& listener);
401 
407  void remove_listener(monero_wallet_listener& listener);
408 
412  set<monero_wallet_listener*> get_listeners();
413 
419  monero_sync_result sync();
420 
428 
435  monero_sync_result sync(uint64_t start_height);
436 
444  monero_sync_result sync(uint64_t start_height, monero_sync_listener& listener);
445 
449  void start_syncing();
450 
454  void stop_syncing();
455 
465  void rescan_spent();
466 
474  void rescan_blockchain();
475 
476 // /**
477 // * Indicates if importing multisig data is needed for returning a correct balance.
478 // *
479 // * @return true if importing multisig data is needed for returning a correct balance, false otherwise
480 // */
481 // public boolean isMultisigImportNeeded();
482 
488  uint64_t get_balance() const;
489 
496  uint64_t get_balance(uint32_t account_idx) const;
497 
505  uint64_t get_balance(uint32_t account_idx, uint32_t subaddress_idx) const;
506 
512  uint64_t get_unlocked_balance() const;
513 
520  uint64_t get_unlocked_balance(uint32_t account_idx) const;
521 
529  uint64_t get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const;
530 
536  vector<monero_account> get_accounts() const;
537 
544  vector<monero_account> get_accounts(bool include_subaddresses) const;
545 
552  vector<monero_account> get_accounts(const string& tag) const;
553 
561  vector<monero_account> get_accounts(bool include_subaddresses, const string& tag) const;
562 
569  monero_account get_account(uint32_t account_idx) const;
570 
578  monero_account get_account(const uint32_t account_idx, bool include_subaddresses) const;
579 
586  monero_account create_account(const string& label = "");
587 
594  vector<monero_subaddress> get_subaddresses(const uint32_t account_idx) const;
595 
603  vector<monero_subaddress> get_subaddresses(const uint32_t account_idx, const vector<uint32_t>& subaddress_indices) const;
604 
612  monero_subaddress getSubaddress(const uint32_t account_idx, const uint32_t subaddress_idx) const;
613 
621  monero_subaddress create_subaddress(uint32_t account_idx, const string& label = "");
622 
623 // /**
624 // * Get a wallet transaction by id.
625 // *
626 // * @param tx_id is an id of a transaction to get
627 // * @return monero_tx_wallet is the identified transactions
628 // */
629 // public monero_tx_wallet getTx(string tx_id);
630 
637  vector<shared_ptr<monero_tx_wallet>> get_txs() const;
638 
639 // /**
640 // * Get wallet transactions by id.
641 // *
642 // * @param tx_ids are ids of transactions to get
643 // * @return the identified transactions
644 // */
645 // public List<monero_tx_wallet> get_txs(Collection<string> tx_ids);
646 
659  vector<shared_ptr<monero_tx_wallet>> get_txs(const monero_tx_query& request) const;
660 
661 // /**
662 // * Get all incoming and outgoing transfers to and from this wallet. An
663 // * outgoing transfer represents a total amount sent from one or more
664 // * subaddresses within an account to individual destination addresses, each
665 // * with their own amount. An incoming transfer represents a total amount
666 // * received into a subaddress within an account. Transfers belong to
667 // * transactions which are stored on the blockchain.
668 // *
669 // * @return all wallet transfers
670 // */
671 // public List<monero_transfer> get_transfers();
672 //
673 // /**
674 // * Get incoming and outgoing transfers to and from an account. An outgoing
675 // * transfer represents a total amount sent from one or more subaddresses
676 // * within an account to individual destination addresses, each with their
677 // * own amount. An incoming transfer represents a total amount received into
678 // * a subaddress within an account. Transfers belong to transactions which
679 // * are stored on the blockchain.
680 // *
681 // * @param account_idx is the index of the account to get transfers from
682 // * @return transfers to/from the account
683 // */
684 // public List<monero_transfer> get_transfers(int account_idx);
685 //
686 // /**
687 // * Get incoming and outgoing transfers to and from a subaddress. An outgoing
688 // * transfer represents a total amount sent from one or more subaddresses
689 // * within an account to individual destination addresses, each with their
690 // * own amount. An incoming transfer represents a total amount received into
691 // * a subaddress within an account. Transfers belong to transactions which
692 // * are stored on the blockchain.
693 // *
694 // * @param account_idx is the index of the account to get transfers from
695 // * @param subaddress_idx is the index of the subaddress to get transfers from
696 // * @return transfers to/from the subaddress
697 // */
698 // public List<monero_transfer> get_transfers(int account_idx, int subaddress_idx);
699 
716  vector<shared_ptr<monero_transfer>> get_transfers(const monero_transfer_query& request) const;
717 
718 // /**
719 // * Get outputs created from previous transactions that belong to the wallet
720 // * (i.e. that the wallet can spend one time). Outputs are part of
721 // * transactions which are stored in blocks on the blockchain.
722 // *
723 // * @return List<monero_output_wallet> are all wallet outputs
724 // */
725 // public List<monero_output_wallet> get_outputs();
726 
739  vector<shared_ptr<monero_output_wallet>> get_outputs(const monero_output_query& request) const;
740 
746  string get_outputs_hex() const;
747 
754  int import_outputs_hex(const string& outputs_hex);
755 
761  vector<shared_ptr<monero_key_image>> get_key_images() const;
762 
769  shared_ptr<monero_key_image_import_result> import_key_images(const vector<shared_ptr<monero_key_image>>& key_images);
770 
771 // /**
772 // * Get new key images from the last imported outputs.
773 // *
774 // * @return the key images from the last imported outputs
775 // */
776 // public List<monero_key_image> getNewKeyImagesFromLastImport();
777 
785  monero_tx_set create_tx(monero_send_request& request);
786 
796  monero_tx_set create_tx(uint32_t account_index, string address, uint64_t amount);
797 
808  monero_tx_set create_tx(int account_index, string address, uint64_t amount, monero_send_priority priority);
809 
817  monero_tx_set create_txs(monero_send_request& request);
818 
825  string relay_tx(const string& tx_metadata);
826 
833  string relay_tx(const monero_tx_wallet& tx);
834 
841  vector<string> relay_txs(const vector<string>& tx_metadatas);
842 
849  vector<string> relay_txs(const vector<shared_ptr<monero_tx_wallet>>& txs);
850 
858  monero_tx_set send(const monero_send_request& request);
859 
869  monero_tx_set send(uint32_t account_index, string address, uint64_t amount);
870 
881  monero_tx_set send(uint32_t account_index, string address, uint64_t amount, monero_send_priority priority);
882 
890  monero_tx_set send_split(const monero_send_request& request);
891 
892  // /**
893  // * Create and relay one or more transactions which transfer funds from this
894  // * wallet to one or more destination.
895  // *
896  // * @param account_index is the index of the account to draw funds from
897  // * @param address is the destination address to send funds to
898  // * @param sendAmount is the amount being sent
899  // * @return the resulting transactions
900  // */
901  // public List<monero_tx_wallet> send_split(int account_index, string address, BigInteger sendAmount);
902  //
903  // /**
904  // * Create and relay one or more transactions which transfer funds from this
905  // * wallet to one or more destination.
906  // *
907  // * @param account_index is the index of the account to draw funds from
908  // * @param address is the destination address to send funds to
909  // * @param sendAmount is the amount being sent
910  // * @param priority is the send priority (default normal)
911  // * @return the resulting transactions
912  // */
913  // public List<monero_tx_wallet> send_split(int account_index, string address, BigInteger sendAmount, monero_send_priority priority);
914 
921  vector<monero_tx_set> sweep_unlocked(const monero_send_request& request);
922 
929  monero_tx_set sweep_output(const monero_send_request& request);
930 
931 // /**
932 // * Sweep an output with a given key image.
933 // *
934 // * @param address is the destination address to send to
935 // * @param key_image is the key image hex of the output to sweep
936 // * @return the resulting transaction from sweeping an output
937 // */
938 // public monero_tx_wallet sweep_output(string address, string key_image);
939 //
940 // /**
941 // * Sweep an output with a given key image.
942 // *
943 // * @param address is the destination address to send to
944 // * @param key_image is the key image hex of the output to sweep
945 // * @param priority is the transaction priority (optional)
946 // * @return the resulting transaction from sweeping an output
947 // */
948 // public monero_tx_wallet sweep_output(string address, string key_image, monero_send_priority priority);
949 //
950 // /**
951 // * Sweep a subaddress's unlocked funds to an address.
952 // *
953 // * @param account_idx is the index of the account
954 // * @param subaddress_idx is the index of the subaddress
955 // * @param address is the address to sweep the subaddress's funds to
956 // * @return the resulting transactions
957 // */
958 // public List<monero_tx_wallet> sweepSubaddress(int account_idx, int subaddress_idx, string address);
959 //
960 // /**
961 // * Sweep an acount's unlocked funds to an address.
962 // *
963 // * @param account_idx is the index of the account
964 // * @param address is the address to sweep the account's funds to
965 // * @return the resulting transactions
966 // */
967 // public List<monero_tx_wallet> sweepAccount(int account_idx, string address);
968 //
969 // /**
970 // * Sweep the wallet's unlocked funds to an address.
971 // *
972 // * @param address is the address to sweep the wallet's funds to
973 // * @return the resulting transactions
974 // */
975 // public List<monero_tx_wallet> sweepWallet(string address);
976 //
977 // /**
978 // * Sweep all unlocked funds according to the given request.
979 // *
980 // * @param request is the sweep configuration
981 // * @return the resulting transactions
982 // */
983 // public List<monero_tx_wallet> sweepAllUnlocked(monero_send_request request);
984 //
985 // /**
986 // * Sweep all unmixable dust outputs back to the wallet to make them easier to spend and mix.
987 // *
988 // * NOTE: Dust only exists pre RCT, so this method will return "no dust to sweep" on new wallets.
989 // *
990 // * @return the resulting transactions from sweeping dust
991 // */
992 // public List<monero_tx_wallet> sweep_dust();
993 
1000  monero_tx_set sweep_dust(bool do_not_relay = false);
1001 
1008  string sign(const string& msg) const;
1009 
1018  bool verify(const string& msg, const string& address, const string& signature) const;
1019 
1026  string get_tx_key(const string& tx_id) const;
1027 
1036  shared_ptr<monero_check_tx> check_tx_key(const string& tx_id, const string& txKey, const string& address) const;
1037 
1038 // /**
1039 // * Get a transaction signature to prove it.
1040 // *
1041 // * @param tx_id specifies the transaction to prove
1042 // * @param address is the destination public address of the transaction
1043 // * @return the transaction signature
1044 // */
1045 // string get_tx_proof(const string& tx_id, const string& address) const;
1046 
1055  string get_tx_proof(const string& tx_id, const string& address, const string& message) const;
1056 
1066  shared_ptr<monero_check_tx> check_tx_proof(const string& tx_id, const string& address, const string& message, const string& signature) const;
1067 
1068 // /**
1069 // * Generate a signature to prove a spend. Unlike proving a transaction, it does not require the destination public address.
1070 // *
1071 // * @param tx_id specifies the transaction to prove
1072 // * @return the transaction signature
1073 // */
1074 // string get_spend_proof(const string& tx_id) const;
1075 
1083  string get_spend_proof(const string& tx_id, const string& message) const;
1084 
1093  bool check_spend_proof(const string& tx_id, const string& message, const string& signature) const;
1094 
1101  string get_reserve_proof_wallet(const string& message) const;
1102 
1111  string get_reserve_proof_account(uint32_t account_idx, uint64_t amount, const string& message) const;
1112 
1121  shared_ptr<monero_check_reserve> check_reserve_proof(const string& address, const string& message, const string& signature) const;
1122 
1129  string get_tx_note(const string& tx_id) const;
1130 
1137  vector<string> get_tx_notes(const vector<string>& tx_ids) const;
1138 
1145  void set_tx_note(const string& tx_id, const string& note);
1146 
1153  void set_tx_notes(const vector<string>& tx_ids, const vector<string>& notes);
1154 
1155 // /**
1156 // * Get all address book entries.
1157 // *
1158 // * @return the address book entries
1159 // */
1160 // public List<MoneroAddressBookEntry> getAddressBookEntries();
1161 //
1162 // /**
1163 // * Get address book entries.
1164 // *
1165 // * @param entryIndices are indices of the entries to get
1166 // * @return the address book entries
1167 // */
1168 // public List<MoneroAddressBookEntry> getAddressBookEntries(Collection<Integer> entryIndices);
1169 //
1170 // /**
1171 // * Add an address book entry.
1172 // *
1173 // * @param address is the entry address
1174 // * @param description is the entry description (optional)
1175 // * @return the index of the added entry
1176 // */
1177 // public int addAddressBookEntry(string address, string description);
1178 //
1179 // /**
1180 // * Add an address book entry.
1181 // *
1182 // * @param address is the entry address
1183 // * @param description is the entry description (optional)
1184 // * @param payment_id is the entry paymet id (optional)
1185 // * @return the index of the added entry
1186 // */
1187 // public int addAddressBookEntry(string address, string description, string payment_id);
1188 //
1189 // /**
1190 // * Delete an address book entry.
1191 // *
1192 // * @param entryIdx is the index of the entry to delete
1193 // */
1194 // public void deleteAddressBookEntry(int entryIdx);
1195 //
1196 // /**
1197 // * Tag accounts.
1198 // *
1199 // * @param tag is the tag to apply to the specified accounts
1200 // * @param accountIndices are the indices of the accounts to tag
1201 // */
1202 // public void tagAccounts(string tag, Collection<Integer> accountIndices);
1203 //
1204 // /**
1205 // * Untag acconts.
1206 // *
1207 // * @param accountIndices are the indices of the accounts to untag
1208 // */
1209 // public void untagAccounts(Collection<Integer> accountIndices);
1210 //
1211 // /**
1212 // * Return all account tags.
1213 // *
1214 // * @return the wallet's account tags
1215 // */
1216 // public List<MoneroAccountTag> getAccountTags();
1217 //
1218 // /**
1219 // * Sets a human-readable description for a tag.
1220 // *
1221 // * @param tag is the tag to set a description for
1222 // * @param label is the label to set for the tag
1223 // */
1224 // public void setAccountTagLabel(string tag, string label);
1225 
1232  string create_payment_uri(const monero_send_request& request) const;
1233 
1240  shared_ptr<monero_send_request> parse_payment_uri(const string& uri) const;
1241 
1248  string get_attribute(const string& key) const;
1249 
1256  void set_attribute(const string& key, const string& val);
1257 
1265  void start_mining(boost::optional<uint64_t> num_threads, boost::optional<bool> background_mining, boost::optional<bool> ignore_battery);
1266 
1270  void stop_mining();
1271 
1277  uint64_t wait_for_next_block();
1278 
1284  bool is_multisig_import_needed() const;
1285 
1286  // bool is_multisig()
1287 
1293  monero_multisig_info get_multisig_info();
1294 
1301  string prepare_multisig();
1302 
1311  monero_multisig_init_result make_multisig(const vector<string>& multisig_hexes, int threshold, const string& password);
1312 
1322  string finalize_multisig(const vector<string>& multisig_hexes, const string& password);
1323 
1333  monero_multisig_init_result exchange_multisig_keys(const vector<string>& mutisig_hexes, const string& password);
1334 
1340  string get_multisig_hex();
1341 
1352  int import_multisig_hex(const vector<string>& multisig_hexes);
1353 
1360  monero_multisig_sign_result sign_multisig_tx_hex(const string& multisig_tx_hex);
1361 
1368  vector<string> submit_multisig_tx_hex(const string& signed_multisig_tx_hex);
1369 
1373  void save();
1374 
1381  void move_to(string path, string password);
1382 
1388  void close(bool save = false);
1389 
1390  // --------------------------------- PRIVATE --------------------------------
1391 
1392  private:
1393  friend struct wallet2_listener;
1394  unique_ptr<tools::wallet2> m_w2; // internal wallet implementation
1395  unique_ptr<wallet2_listener> m_w2_listener; // internal wallet implementation listener
1396  set<monero_wallet_listener*> m_listeners; // external wallet listeners
1397 
1398  void init_common();
1399  vector<monero_subaddress> get_subaddresses_aux(uint32_t account_idx, const vector<uint32_t>& subaddress_indices, const vector<tools::wallet2::transfer_details>& transfers) const;
1400  monero_tx_set sweep_account(const monero_send_request& request); // sweeps unlocked funds within an account; private helper to sweep_unlocked()
1401 
1402  // blockchain sync management
1403  mutable std::atomic<bool> m_is_synced; // whether or not wallet is synced
1404  mutable std::atomic<bool> m_is_connected; // cache connection status to avoid unecessary RPC calls
1405  boost::condition_variable m_sync_cv; // to make sync threads woke
1406  boost::mutex m_sync_mutex; // synchronize sync() and syncAsync() requests
1407  std::atomic<bool> m_rescan_on_sync; // whether or not to rescan on sync
1408  std::atomic<bool> m_syncing_enabled; // whether or not auto sync is enabled
1409  std::atomic<int> m_syncing_interval; // auto sync loop interval in milliseconds
1410  boost::thread m_syncing_thread; // thread for auto sync loop
1411  boost::mutex m_syncing_mutex; // synchronize auto sync loop
1412  std::atomic<bool> m_syncing_thread_done; // whether or not the syncing thread is shut down
1413  void sync_thread_func(); // function to run thread with syncing loop
1414  monero_sync_result lock_and_sync(boost::optional<uint64_t> start_height = boost::none); // internal function to synchronize request to sync and rescan
1415  monero_sync_result sync_aux(boost::optional<uint64_t> start_height = boost::none); // internal function to immediately block, sync, and report progress
1416  };
1417 }
Definition: monero_wallet_model.h:406
Definition: monero_daemon.cpp:58
Definition: monero_wallet.h:129
Definition: monero_wallet_model.h:317
virtual void on_output_spent(const monero_output_wallet &output)
Definition: monero_wallet.h:118
Definition: monero_wallet.h:96
Definition: monero_wallet.h:78
Definition: monero_wallet_model.h:420
Definition: monero_wallet_model.h:276
Definition: monero_wallet_model.h:430
Definition: monero_wallet_model.h:256
monero_send_priority
Definition: monero_wallet_model.h:328
Definition: monero_wallet_model.h:68
Definition: monero_wallet_model.h:305
virtual void on_output_received(const monero_output_wallet &output)
Definition: monero_wallet.h:111
Definition: monero_wallet_model.h:240
Definition: monero_wallet_model.h:338
virtual void on_new_block(uint64_t height)
Definition: monero_wallet.h:104
Definition: monero_wallet_model.h:107
Definition: monero_daemon_model.h:99
Definition: monero_wallet_model.h:78
virtual void on_sync_progress(uint64_t height, uint64_t start_height, uint64_t end_height, double percent_done, const string &message)
Definition: monero_wallet.h:90
Definition: monero_wallet_model.h:220
Definition: monero_wallet_model.h:200
monero_network_type
Definition: monero_daemon_model.h:88
Definition: monero_wallet.cpp:670