From 1bef735e8e0ed8abd75b44ea27cbe52d609fa6e4 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 26 Jul 2019 16:49:29 -0400 Subject: [PATCH] switch wallet creation from constructors to static utilities --- CMakeLists.txt | 4 +- src/wallet/MoneroWallet.cpp | 90 ++++++++++++++++++++++++------------- src/wallet/MoneroWallet.h | 20 ++++----- test/Scratchpad.cpp | 2 +- 4 files changed, 69 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec80057..5096061 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,7 +285,7 @@ target_link_libraries(monero-cpp # Build C++ scratchpad ######################## -#[[ --- START BLOCK COMMENT --- +###[[ --- START BLOCK COMMENT --- set( SCRATCHPAD_SRC_FILES @@ -338,4 +338,4 @@ target_link_libraries(scratchpad ${EXTRA_LIBRARIES} ) -#]] # --- END BLOCK COMMENT --- \ No newline at end of file +###]] # --- END BLOCK COMMENT --- \ No newline at end of file diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index 1cda816..0041b8c 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -724,7 +724,7 @@ namespace monero { } }; - // ------------------------------- WALLET METHODS ----------------------------- + // ---------------------------- WALLET MANAGEMENT --------------------------- bool MoneroWallet::walletExists(const string& path) { MTRACE("walletExists(" << path << ")"); @@ -734,30 +734,41 @@ namespace monero { return walletFileExists; } - MoneroWallet::MoneroWallet(const string& path, const string& password, const MoneroNetworkType networkType) { + MoneroWallet* MoneroWallet::openWallet(const string& path, const string& password, const MoneroNetworkType networkType) { MTRACE("openWallet(" << path << ", " << password << ", " << networkType << ")"); - wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); - wallet2->load(path, password); - wallet2->init(""); - initCommon(); + MoneroWallet* wallet = new MoneroWallet(); + wallet->wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); + wallet->wallet2->load(path, password); + wallet->wallet2->init(""); + wallet->initCommon(); + return wallet; } - MoneroWallet::MoneroWallet(const string& path, const string& password) { - MTRACE("MoneroWallet()"); + MoneroWallet* MoneroWallet::createWalletRandom(const string& path, const string& password) { + MTRACE("createWalletRandom(path, password)"); throw runtime_error("Not implemented"); } - MoneroWallet::MoneroWallet(const string& path, const string&password, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, const string& language) { - MTRACE("MoneroWallet(3)"); - wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); - setDaemonConnection(daemonConnection); - wallet2->set_seed_language(language); + MoneroWallet* MoneroWallet::createWalletRandom(const string& path, const string& password, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, const string& language) { + MTRACE("createWalletRandom(path, password, networkType, daemonConnection, language)"); + MoneroWallet* wallet = new MoneroWallet(); + wallet->wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); + wallet->setDaemonConnection(daemonConnection); + wallet->wallet2->set_seed_language(language); crypto::secret_key secret_key; - wallet2->generate(path, password, secret_key, false, false); - initCommon(); + wallet->wallet2->generate(path, password, secret_key, false, false); + wallet->initCommon(); + return wallet; } - MoneroWallet::MoneroWallet(const string& path, const string& password, const string& mnemonic, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight) { + MoneroWallet* MoneroWallet::createWalletFromMnemonic(const string& path, const string& password, const string& mnemonic, const MoneroNetworkType networkType) { + MTRACE("createWalletFromMnemonic(path, password, mnemonic, networkType)"); + throw runtime_error("Not implemented"); + } + + MoneroWallet* MoneroWallet::createWalletFromMnemonic(const string& path, const string& password, const string& mnemonic, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight) { + MTRACE("createWalletFromMnemonic(path, password, mnemonic, networkType, daemonConnection, restoreHeight)"); + MoneroWallet* wallet = new MoneroWallet(); // validate mnemonic and get recovery key and language crypto::secret_key recoveryKey; @@ -767,16 +778,28 @@ namespace monero { if (language == crypto::ElectrumWords::old_language_name) language = Language::English().get_language_name(); // initialize wallet - wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); - setDaemonConnection(daemonConnection); - wallet2->set_seed_language(language); - wallet2->generate(path, password, recoveryKey, true, false); - wallet2->set_refresh_from_block_height(restoreHeight); - initCommon(); + wallet->wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); + wallet->setDaemonConnection(daemonConnection); + wallet->wallet2->set_seed_language(language); + wallet->wallet2->generate(path, password, recoveryKey, true, false); + wallet->wallet2->set_refresh_from_block_height(restoreHeight); + wallet->initCommon(); + return wallet; } - MoneroWallet::MoneroWallet(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight, const string& language) { - MTRACE("MoneroWallet(7)"); + MoneroWallet* MoneroWallet::createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType) { + MTRACE("createWalletFromKeys(path, password, address, viewKey, spendKey, networkType)"); + throw runtime_error("Not implemented"); + } + + MoneroWallet* MoneroWallet::createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight) { + MTRACE("createWalletFromKeys(path, password, address, viewKey, spendKey, networkType, daemonConnection, restoreHeight)"); + throw runtime_error("Not implemented"); + } + + MoneroWallet* MoneroWallet::createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight, const string& language) { + MTRACE("createWalletFromKeys(path, password, address, viewKey, spendKey, networkType, daemonConnection, restoreHeight, language)"); + MoneroWallet* wallet = new MoneroWallet(); // validate and parse address cryptonote::address_parse_info info; @@ -821,14 +844,15 @@ namespace monero { } // initialize wallet - wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); - if (hasSpendKey && hasViewKey) wallet2->generate(path, password, info.address, spendKeySK, viewKeySK); - if (!hasSpendKey && hasViewKey) wallet2->generate(path, password, info.address, viewKeySK); - if (hasSpendKey && !hasViewKey) wallet2->generate(path, password, spendKeySK, true, false); - setDaemonConnection(daemonConnection); - wallet2->set_refresh_from_block_height(restoreHeight); - wallet2->set_seed_language(language); - initCommon(); + wallet->wallet2 = unique_ptr(new tools::wallet2(static_cast(networkType), 1, true)); + if (hasSpendKey && hasViewKey) wallet->wallet2->generate(path, password, info.address, spendKeySK, viewKeySK); + if (!hasSpendKey && hasViewKey) wallet->wallet2->generate(path, password, info.address, viewKeySK); + if (hasSpendKey && !hasViewKey) wallet->wallet2->generate(path, password, spendKeySK, true, false); + wallet->setDaemonConnection(daemonConnection); + wallet->wallet2->set_refresh_from_block_height(restoreHeight); + wallet->wallet2->set_seed_language(language); + wallet->initCommon(); + return wallet; } MoneroWallet::~MoneroWallet() { @@ -836,6 +860,8 @@ namespace monero { close(); } + // ----------------------------- WALLET METHODS ----------------------------- + void MoneroWallet::setDaemonConnection(const string& uri, const string& username, const string& password) { MTRACE("setDaemonConnection(" << uri << ", " << username << ", " << password << ")"); diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 85e71bb..279098e 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -155,15 +155,7 @@ namespace monero { * @param password is the password of the wallet file to open * @param networkType is the wallet's network type */ - MoneroWallet(const string& path, const string& password, const MoneroNetworkType networkType); - - /** - * Create a new wallet with a randomly generated seed on mainnet in English. - * - * @param path is the path to create the wallet - * @param password is the password encrypt the wallet - */ - MoneroWallet(const string& path, const string& password); + static MoneroWallet* openWallet(const string& path, const string& password, const MoneroNetworkType networkType); /** * Create a new wallet with a randomly generated seed. @@ -174,7 +166,8 @@ namespace monero { * @param daemonConnection is connection information to a daemon (default = an unconnected wallet) * @param language is the wallet and mnemonic's language (default = "English") */ - MoneroWallet(const string& path, const string& password, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, const string& language); + static MoneroWallet* createWalletRandom(const string& path, const string& password); + static MoneroWallet* createWalletRandom(const string& path, const string& password, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, const string& language); /** * Create a wallet from an existing mnemonic phrase. @@ -186,7 +179,8 @@ namespace monero { * @param daemonConnection is connection information to a daemon (default = an unconnected wallet) * @param restoreHeight is the block height to restore (i.e. scan the chain) from (default = 0) */ - MoneroWallet(const string& path, const string& password, const string& mnemonic, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight); + static MoneroWallet* createWalletFromMnemonic(const string& path, const string& password, const string& mnemonic, const MoneroNetworkType networkType); + static MoneroWallet* createWalletFromMnemonic(const string& path, const string& password, const string& mnemonic, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight); /** * Create a wallet from an address, view key, and spend key. @@ -201,7 +195,9 @@ namespace monero { * @param restoreHeight is the block height to restore (i.e. scan the chain) from (default = 0) * @param language is the wallet and mnemonic's language (default = "English") */ - MoneroWallet(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight, const string& language); + static MoneroWallet* createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType); + static MoneroWallet* createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight); + static MoneroWallet* createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, uint64_t restoreHeight, const string& language); /** * Deconstruct the wallet. diff --git a/test/Scratchpad.cpp b/test/Scratchpad.cpp index 651f229..1bdecd4 100644 --- a/test/Scratchpad.cpp +++ b/test/Scratchpad.cpp @@ -70,7 +70,7 @@ int main(int argc, const char* argv[]) { // load wallet - MoneroWallet* wallet = new MoneroWallet("../../test_wallets/test_wallet_1", "supersecretpassword123", MoneroNetworkType::STAGENET); + MoneroWallet* wallet = MoneroWallet::openWallet("../../test_wallets/test_wallet_1", "supersecretpassword123", MoneroNetworkType::STAGENET); wallet->setDaemonConnection("http://localhost:38081", "", ""); // fetch txs