monero-cpp-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_synced() const;
275 
281  string get_path() const;
282 
288  monero_network_type get_network_type() const;
289 
295  string get_seed() const;
296 
302  string get_mnemonic() const;
303 
309  string get_language() const;
310 
316  vector<string> get_languages() const;
317 
323  string get_public_view_key() const;
324 
330  string get_private_view_key() const;
331 
337  string get_public_spend_key() const;
338 
344  string get_private_spend_key() const;
345 
351  string get_primary_address() const;
352 
360  string get_address(const uint32_t account_idx, const uint32_t subaddress_idx) const;
361 
369  monero_subaddress get_address_index(const string& address) const;
370 
378  monero_integrated_address get_integrated_address(const string& standard_address = "", const string& payment_id = "") const;
379 
386  monero_integrated_address decode_integrated_address(const string& integrated_address) const;
387 
393  void add_listener(monero_wallet_listener& listener);
394 
400  void remove_listener(monero_wallet_listener& listener);
401 
405  set<monero_wallet_listener*> get_listeners();
406 
412  monero_sync_result sync();
413 
421 
428  monero_sync_result sync(uint64_t start_height);
429 
437  monero_sync_result sync(uint64_t start_height, monero_sync_listener& listener);
438 
442  void start_syncing();
443 
447  void stop_syncing();
448 
456  void rescan_blockchain();
457 
458 // /**
459 // * Indicates if importing multisig data is needed for returning a correct balance.
460 // *
461 // * @return true if importing multisig data is needed for returning a correct balance, false otherwise
462 // */
463 // public boolean isMultisigImportNeeded();
464 
470  uint64_t get_balance() const;
471 
478  uint64_t get_balance(uint32_t account_idx) const;
479 
487  uint64_t get_balance(uint32_t account_idx, uint32_t subaddress_idx) const;
488 
494  uint64_t get_unlocked_balance() const;
495 
502  uint64_t get_unlocked_balance(uint32_t account_idx) const;
503 
511  uint64_t get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const;
512 
518  vector<monero_account> get_accounts() const;
519 
526  vector<monero_account> get_accounts(bool include_subaddresses) const;
527 
534  vector<monero_account> get_accounts(const string& tag) const;
535 
543  vector<monero_account> get_accounts(bool include_subaddresses, const string& tag) const;
544 
551  monero_account get_account(uint32_t account_idx) const;
552 
560  monero_account get_account(const uint32_t account_idx, bool include_subaddresses) const;
561 
568  monero_account create_account(const string& label = "");
569 
576  vector<monero_subaddress> get_subaddresses(const uint32_t account_idx) const;
577 
585  vector<monero_subaddress> get_subaddresses(const uint32_t account_idx, const vector<uint32_t>& subaddress_indices) const;
586 
594  monero_subaddress getSubaddress(const uint32_t account_idx, const uint32_t subaddress_idx) const;
595 
603  monero_subaddress create_subaddress(uint32_t account_idx, const string& label = "");
604 
605 // /**
606 // * Get a wallet transaction by id.
607 // *
608 // * @param tx_id is an id of a transaction to get
609 // * @return monero_tx_wallet is the identified transactions
610 // */
611 // public monero_tx_wallet getTx(string tx_id);
612 
619  vector<shared_ptr<monero_tx_wallet>> get_txs() const;
620 
621 // /**
622 // * Get wallet transactions by id.
623 // *
624 // * @param tx_ids are ids of transactions to get
625 // * @return the identified transactions
626 // */
627 // public List<monero_tx_wallet> get_txs(Collection<string> tx_ids);
628 
641  vector<shared_ptr<monero_tx_wallet>> get_txs(const monero_tx_query& request) const;
642 
643 // /**
644 // * Get all incoming and outgoing transfers to and from this wallet. An
645 // * outgoing transfer represents a total amount sent from one or more
646 // * subaddresses within an account to individual destination addresses, each
647 // * with their own amount. An incoming transfer represents a total amount
648 // * received into a subaddress within an account. Transfers belong to
649 // * transactions which are stored on the blockchain.
650 // *
651 // * @return all wallet transfers
652 // */
653 // public List<monero_transfer> get_transfers();
654 //
655 // /**
656 // * Get incoming and outgoing transfers to and from an account. An outgoing
657 // * transfer represents a total amount sent from one or more subaddresses
658 // * within an account to individual destination addresses, each with their
659 // * own amount. An incoming transfer represents a total amount received into
660 // * a subaddress within an account. Transfers belong to transactions which
661 // * are stored on the blockchain.
662 // *
663 // * @param account_idx is the index of the account to get transfers from
664 // * @return transfers to/from the account
665 // */
666 // public List<monero_transfer> get_transfers(int account_idx);
667 //
668 // /**
669 // * Get incoming and outgoing transfers to and from a subaddress. An outgoing
670 // * transfer represents a total amount sent from one or more subaddresses
671 // * within an account to individual destination addresses, each with their
672 // * own amount. An incoming transfer represents a total amount received into
673 // * a subaddress within an account. Transfers belong to transactions which
674 // * are stored on the blockchain.
675 // *
676 // * @param account_idx is the index of the account to get transfers from
677 // * @param subaddress_idx is the index of the subaddress to get transfers from
678 // * @return transfers to/from the subaddress
679 // */
680 // public List<monero_transfer> get_transfers(int account_idx, int subaddress_idx);
681 
698  vector<shared_ptr<monero_transfer>> get_transfers(const monero_transfer_query& request) const;
699 
700 // /**
701 // * Get outputs created from previous transactions that belong to the wallet
702 // * (i.e. that the wallet can spend one time). Outputs are part of
703 // * transactions which are stored in blocks on the blockchain.
704 // *
705 // * @return List<monero_output_wallet> are all wallet outputs
706 // */
707 // public List<monero_output_wallet> get_outputs();
708 
721  vector<shared_ptr<monero_output_wallet>> get_outputs(const monero_output_query& request) const;
722 
728  string get_outputs_hex() const;
729 
736  int import_outputs_hex(const string& outputs_hex);
737 
743  vector<shared_ptr<monero_key_image>> get_key_images() const;
744 
751  shared_ptr<monero_key_image_import_result> import_key_images(const vector<shared_ptr<monero_key_image>>& key_images);
752 
753 // /**
754 // * Get new key images from the last imported outputs.
755 // *
756 // * @return the key images from the last imported outputs
757 // */
758 // public List<monero_key_image> getNewKeyImagesFromLastImport();
759 
767  shared_ptr<monero_tx_wallet> create_tx(monero_send_request& request);
768 
778  shared_ptr<monero_tx_wallet> create_tx(uint32_t account_index, string address, uint64_t amount);
779 
790  shared_ptr<monero_tx_wallet> create_tx(int account_index, string address, uint64_t amount, monero_send_priority priority);
791 
799  vector<shared_ptr<monero_tx_wallet>> create_txs(monero_send_request& request);
800 
807  string relay_tx(const string& tx_metadata);
808 
815  string relay_tx(const monero_tx_wallet& tx);
816 
823  vector<string> relay_txs(const vector<string>& tx_metadatas);
824 
831  vector<string> relay_txs(const vector<shared_ptr<monero_tx_wallet>>& txs);
832 
840  shared_ptr<monero_tx_wallet> send(monero_send_request& request);
841 
851  shared_ptr<monero_tx_wallet> send(uint32_t account_index, string address, uint64_t amount);
852 
863  shared_ptr<monero_tx_wallet> send(uint32_t account_index, string address, uint64_t amount, monero_send_priority priority);
864 
872  vector<shared_ptr<monero_tx_wallet>> send_split(const monero_send_request& request);
873 
874 //
875 // /**
876 // * Create and relay a transaction which transfers funds from this wallet to
877 // * a destination address.
878 // *
879 // * @param account_index is the index of the account to draw funds from
880 // * @param address is the destination address to send funds to
881 // * @param sendAmount is the amount being sent
882 // * @return the resulting transaction
883 // */
884 // public monero_tx_wallet send(int account_index, string address, BigInteger sendAmount);
885 //
886 // /**
887 // * Create and relay a transaction which transfers funds from this wallet to
888 // * a destination address.
889 // *
890 // * @param account_index is the index of the account to draw funds from
891 // * @param address is the destination address to send funds to
892 // * @param sendAmount is the amount being sent
893 // * @param priority is the send priority (default normal)
894 // * @return the resulting transaction
895 // */
896 // public monero_tx_wallet send(int account_index, string address, BigInteger sendAmount, monero_send_priority priority);
897 //
898 // /**
899 // * Create and relay (depending on configuration) one or more transactions
900 // * which transfer funds from this wallet to one or more destination.
901 // *
902 // * @param request configures the transactions
903 // * @return the resulting transactions
904 // */
905 // public List<monero_tx_wallet> send_split(monero_send_request request);
906 //
907 // /**
908 // * Create and relay one or more transactions which transfer funds from this
909 // * wallet to one or more destination.
910 // *
911 // * @param account_index is the index of the account to draw funds from
912 // * @param address is the destination address to send funds to
913 // * @param sendAmount is the amount being sent
914 // * @return the resulting transactions
915 // */
916 // public List<monero_tx_wallet> send_split(int account_index, string address, BigInteger sendAmount);
917 //
918 // /**
919 // * Create and relay one or more transactions which transfer funds from this
920 // * wallet to one or more destination.
921 // *
922 // * @param account_index is the index of the account to draw funds from
923 // * @param address is the destination address to send funds to
924 // * @param sendAmount is the amount being sent
925 // * @param priority is the send priority (default normal)
926 // * @return the resulting transactions
927 // */
928 // public List<monero_tx_wallet> send_split(int account_index, string address, BigInteger sendAmount, monero_send_priority priority);
929 
936  shared_ptr<monero_tx_wallet> sweep_output(const monero_send_request& request) const;
937 
938 // /**
939 // * Sweep an output with a given key image.
940 // *
941 // * @param address is the destination address to send to
942 // * @param key_image is the key image hex of the output to sweep
943 // * @return the resulting transaction from sweeping an output
944 // */
945 // public monero_tx_wallet sweep_output(string address, string key_image);
946 //
947 // /**
948 // * Sweep an output with a given key image.
949 // *
950 // * @param address is the destination address to send to
951 // * @param key_image is the key image hex of the output to sweep
952 // * @param priority is the transaction priority (optional)
953 // * @return the resulting transaction from sweeping an output
954 // */
955 // public monero_tx_wallet sweep_output(string address, string key_image, monero_send_priority priority);
956 //
957 // /**
958 // * Sweep a subaddress's unlocked funds to an address.
959 // *
960 // * @param account_idx is the index of the account
961 // * @param subaddress_idx is the index of the subaddress
962 // * @param address is the address to sweep the subaddress's funds to
963 // * @return the resulting transactions
964 // */
965 // public List<monero_tx_wallet> sweepSubaddress(int account_idx, int subaddress_idx, string address);
966 //
967 // /**
968 // * Sweep an acount's unlocked funds to an address.
969 // *
970 // * @param account_idx is the index of the account
971 // * @param address is the address to sweep the account's funds to
972 // * @return the resulting transactions
973 // */
974 // public List<monero_tx_wallet> sweepAccount(int account_idx, string address);
975 //
976 // /**
977 // * Sweep the wallet's unlocked funds to an address.
978 // *
979 // * @param address is the address to sweep the wallet's funds to
980 // * @return the resulting transactions
981 // */
982 // public List<monero_tx_wallet> sweepWallet(string address);
983 //
984 // /**
985 // * Sweep all unlocked funds according to the given request.
986 // *
987 // * @param request is the sweep configuration
988 // * @return the resulting transactions
989 // */
990 // public List<monero_tx_wallet> sweepAllUnlocked(monero_send_request request);
991 //
992 // /**
993 // * Sweep all unmixable dust outputs back to the wallet to make them easier to spend and mix.
994 // *
995 // * NOTE: Dust only exists pre RCT, so this method will return "no dust to sweep" on new wallets.
996 // *
997 // * @return the resulting transactions from sweeping dust
998 // */
999 // public List<monero_tx_wallet> sweep_dust();
1000 
1007  vector<shared_ptr<monero_tx_wallet>> sweep_dust(bool do_not_relay = false);
1008 
1015  string get_tx_note(const string& tx_id) const;
1016 
1023  vector<string> get_tx_notes(const vector<string>& tx_ids) const;
1024 
1031  void set_tx_note(const string& tx_id, const string& note);
1032 
1039  void set_tx_notes(const vector<string>& tx_ids, const vector<string>& notes);
1040 
1047  string sign(const string& msg) const;
1048 
1057  bool verify(const string& msg, const string& address, const string& signature) const;
1058 
1065  string get_tx_key(const string& tx_id) const;
1066 
1075  shared_ptr<monero_check_tx> check_tx_key(const string& tx_id, const string& txKey, const string& address) const;
1076 
1077 // /**
1078 // * Get a transaction signature to prove it.
1079 // *
1080 // * @param tx_id specifies the transaction to prove
1081 // * @param address is the destination public address of the transaction
1082 // * @return the transaction signature
1083 // */
1084 // string get_tx_proof(const string& tx_id, const string& address) const;
1085 
1094  string get_tx_proof(const string& tx_id, const string& address, const string& message) const;
1095 
1105  shared_ptr<monero_check_tx> check_tx_proof(const string& tx_id, const string& address, const string& message, const string& signature) const;
1106 
1107 // /**
1108 // * Generate a signature to prove a spend. Unlike proving a transaction, it does not require the destination public address.
1109 // *
1110 // * @param tx_id specifies the transaction to prove
1111 // * @return the transaction signature
1112 // */
1113 // string get_spend_proof(const string& tx_id) const;
1114 
1122  string get_spend_proof(const string& tx_id, const string& message) const;
1123 
1132  bool check_spend_proof(const string& tx_id, const string& message, const string& signature) const;
1133 
1140  string get_reserve_proof_wallet(const string& message) const;
1141 
1150  string get_reserve_proof_account(uint32_t account_idx, uint64_t amount, const string& message) const;
1151 
1160  shared_ptr<monero_check_reserve> check_reserve_proof(const string& address, const string& message, const string& signature) const;
1161 
1162 // /**
1163 // * Get all address book entries.
1164 // *
1165 // * @return the address book entries
1166 // */
1167 // public List<MoneroAddressBookEntry> getAddressBookEntries();
1168 //
1169 // /**
1170 // * Get address book entries.
1171 // *
1172 // * @param entryIndices are indices of the entries to get
1173 // * @return the address book entries
1174 // */
1175 // public List<MoneroAddressBookEntry> getAddressBookEntries(Collection<Integer> entryIndices);
1176 //
1177 // /**
1178 // * Add an address book entry.
1179 // *
1180 // * @param address is the entry address
1181 // * @param description is the entry description (optional)
1182 // * @return the index of the added entry
1183 // */
1184 // public int addAddressBookEntry(string address, string description);
1185 //
1186 // /**
1187 // * Add an address book entry.
1188 // *
1189 // * @param address is the entry address
1190 // * @param description is the entry description (optional)
1191 // * @param payment_id is the entry paymet id (optional)
1192 // * @return the index of the added entry
1193 // */
1194 // public int addAddressBookEntry(string address, string description, string payment_id);
1195 //
1196 // /**
1197 // * Delete an address book entry.
1198 // *
1199 // * @param entryIdx is the index of the entry to delete
1200 // */
1201 // public void deleteAddressBookEntry(int entryIdx);
1202 //
1203 // /**
1204 // * Tag accounts.
1205 // *
1206 // * @param tag is the tag to apply to the specified accounts
1207 // * @param accountIndices are the indices of the accounts to tag
1208 // */
1209 // public void tagAccounts(string tag, Collection<Integer> accountIndices);
1210 //
1211 // /**
1212 // * Untag acconts.
1213 // *
1214 // * @param accountIndices are the indices of the accounts to untag
1215 // */
1216 // public void untagAccounts(Collection<Integer> accountIndices);
1217 //
1218 // /**
1219 // * Return all account tags.
1220 // *
1221 // * @return the wallet's account tags
1222 // */
1223 // public List<MoneroAccountTag> getAccountTags();
1224 //
1225 // /**
1226 // * Sets a human-readable description for a tag.
1227 // *
1228 // * @param tag is the tag to set a description for
1229 // * @param label is the label to set for the tag
1230 // */
1231 // public void setAccountTagLabel(string tag, string label);
1232 
1239  string create_payment_uri(const monero_send_request& request) const;
1240 
1247  shared_ptr<monero_send_request> parse_payment_uri(const string& uri) const;
1248 
1255  void set_attribute(const string& key, const string& val);
1256 
1263  string get_attribute(const string& key) const;
1264 
1272  void start_mining(boost::optional<uint64_t> num_threads, boost::optional<bool> background_mining, boost::optional<bool> ignore_battery);
1273 
1277  void stop_mining();
1278 
1284  uint64_t wait_for_next_block();
1285 
1289  void save();
1290 
1297  void move_to(string path, string password);
1298 
1302  void close();
1303 
1304  // --------------------------------- PRIVATE --------------------------------
1305 
1306  private:
1307  friend struct wallet2_listener;
1308  unique_ptr<tools::wallet2> m_w2; // internal wallet implementation
1309  unique_ptr<wallet2_listener> m_w2_listener; // internal wallet implementation listener
1310  set<monero_wallet_listener*> m_listeners; // external wallet listeners
1311 
1312  void init_common();
1313  vector<monero_subaddress> get_subaddresses_aux(uint32_t account_idx, const vector<uint32_t>& subaddress_indices, const vector<tools::wallet2::transfer_details>& transfers) const;
1314 
1315  // blockchain sync management
1316  mutable std::atomic<bool> m_is_synced; // whether or not wallet is synced
1317  mutable std::atomic<bool> m_is_connected; // cache connection status to avoid unecessary RPC calls
1318  boost::condition_variable m_sync_cv; // to make sync threads woke
1319  boost::mutex m_sync_mutex; // synchronize sync() and syncAsync() requests
1320  std::atomic<bool> m_rescan_on_sync; // whether or not to rescan on sync
1321  std::atomic<bool> m_syncing_enabled; // whether or not auto sync is enabled
1322  std::atomic<int> m_syncing_interval; // auto sync loop interval in milliseconds
1323  boost::thread m_syncing_thread; // thread for auto sync loop
1324  boost::mutex m_syncing_mutex; // synchronize auto sync loop
1325  std::atomic<bool> m_syncing_thread_done; // whether or not the syncing thread is shut down
1326  void sync_thread_func(); // function to run thread with syncing loop
1327  monero_sync_result lock_and_sync(boost::optional<uint64_t> start_height = boost::none); // internal function to synchronize request to sync and rescan
1328  monero_sync_result sync_aux(boost::optional<uint64_t> start_height = boost::none); // internal function to immediately block, sync, and report progress
1329  };
1330 }
Definition: monero_daemon.cpp:58
Definition: monero_wallet.h:129
Definition: monero_wallet_model.h:297
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:273
Definition: monero_wallet_model.h:254
monero_send_priority
Definition: monero_wallet_model.h:308
Definition: monero_wallet_model.h:68
virtual void on_output_received(const monero_output_wallet &output)
Definition: monero_wallet.h:111
Definition: monero_wallet_model.h:239
Definition: monero_wallet_model.h:318
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:219
Definition: monero_wallet_model.h:199
monero_network_type
Definition: monero_daemon_model.h:88
Definition: monero_wallet.cpp:657