switch wallet creation from constructors to static utilities

This commit is contained in:
woodser
2019-07-26 16:49:29 -04:00
parent 60dd56fbae
commit 1bef735e8e
4 changed files with 69 additions and 47 deletions
+2 -2
View File
@@ -285,7 +285,7 @@ target_link_libraries(monero-cpp
# Build C++ scratchpad # Build C++ scratchpad
######################## ########################
#[[ --- START BLOCK COMMENT --- ###[[ --- START BLOCK COMMENT ---
set( set(
SCRATCHPAD_SRC_FILES SCRATCHPAD_SRC_FILES
@@ -338,4 +338,4 @@ target_link_libraries(scratchpad
${EXTRA_LIBRARIES} ${EXTRA_LIBRARIES}
) )
#]] # --- END BLOCK COMMENT --- ###]] # --- END BLOCK COMMENT ---
+58 -32
View File
@@ -724,7 +724,7 @@ namespace monero {
} }
}; };
// ------------------------------- WALLET METHODS ----------------------------- // ---------------------------- WALLET MANAGEMENT ---------------------------
bool MoneroWallet::walletExists(const string& path) { bool MoneroWallet::walletExists(const string& path) {
MTRACE("walletExists(" << path << ")"); MTRACE("walletExists(" << path << ")");
@@ -734,30 +734,41 @@ namespace monero {
return walletFileExists; 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 << ")"); MTRACE("openWallet(" << path << ", " << password << ", " << networkType << ")");
wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<network_type>(networkType), 1, true)); MoneroWallet* wallet = new MoneroWallet();
wallet2->load(path, password); wallet->wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<network_type>(networkType), 1, true));
wallet2->init(""); wallet->wallet2->load(path, password);
initCommon(); wallet->wallet2->init("");
wallet->initCommon();
return wallet;
} }
MoneroWallet::MoneroWallet(const string& path, const string& password) { MoneroWallet* MoneroWallet::createWalletRandom(const string& path, const string& password) {
MTRACE("MoneroWallet()"); MTRACE("createWalletRandom(path, password)");
throw runtime_error("Not implemented"); throw runtime_error("Not implemented");
} }
MoneroWallet::MoneroWallet(const string& path, const string&password, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, const string& language) { MoneroWallet* MoneroWallet::createWalletRandom(const string& path, const string& password, const MoneroNetworkType networkType, const MoneroRpcConnection& daemonConnection, const string& language) {
MTRACE("MoneroWallet(3)"); MTRACE("createWalletRandom(path, password, networkType, daemonConnection, language)");
wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<network_type>(networkType), 1, true)); MoneroWallet* wallet = new MoneroWallet();
setDaemonConnection(daemonConnection); wallet->wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<network_type>(networkType), 1, true));
wallet2->set_seed_language(language); wallet->setDaemonConnection(daemonConnection);
wallet->wallet2->set_seed_language(language);
crypto::secret_key secret_key; crypto::secret_key secret_key;
wallet2->generate(path, password, secret_key, false, false); wallet->wallet2->generate(path, password, secret_key, false, false);
initCommon(); 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 // validate mnemonic and get recovery key and language
crypto::secret_key recoveryKey; crypto::secret_key recoveryKey;
@@ -767,16 +778,28 @@ namespace monero {
if (language == crypto::ElectrumWords::old_language_name) language = Language::English().get_language_name(); if (language == crypto::ElectrumWords::old_language_name) language = Language::English().get_language_name();
// initialize wallet // initialize wallet
wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(networkType), 1, true)); wallet->wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(networkType), 1, true));
setDaemonConnection(daemonConnection); wallet->setDaemonConnection(daemonConnection);
wallet2->set_seed_language(language); wallet->wallet2->set_seed_language(language);
wallet2->generate(path, password, recoveryKey, true, false); wallet->wallet2->generate(path, password, recoveryKey, true, false);
wallet2->set_refresh_from_block_height(restoreHeight); wallet->wallet2->set_refresh_from_block_height(restoreHeight);
initCommon(); 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) { MoneroWallet* MoneroWallet::createWalletFromKeys(const string& path, const string& password, const string& address, const string& viewKey, const string& spendKey, const MoneroNetworkType networkType) {
MTRACE("MoneroWallet(7)"); 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 // validate and parse address
cryptonote::address_parse_info info; cryptonote::address_parse_info info;
@@ -821,14 +844,15 @@ namespace monero {
} }
// initialize wallet // initialize wallet
wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(networkType), 1, true)); wallet->wallet2 = unique_ptr<tools::wallet2>(new tools::wallet2(static_cast<cryptonote::network_type>(networkType), 1, true));
if (hasSpendKey && hasViewKey) wallet2->generate(path, password, info.address, spendKeySK, viewKeySK); if (hasSpendKey && hasViewKey) wallet->wallet2->generate(path, password, info.address, spendKeySK, viewKeySK);
if (!hasSpendKey && hasViewKey) wallet2->generate(path, password, info.address, viewKeySK); if (!hasSpendKey && hasViewKey) wallet->wallet2->generate(path, password, info.address, viewKeySK);
if (hasSpendKey && !hasViewKey) wallet2->generate(path, password, spendKeySK, true, false); if (hasSpendKey && !hasViewKey) wallet->wallet2->generate(path, password, spendKeySK, true, false);
setDaemonConnection(daemonConnection); wallet->setDaemonConnection(daemonConnection);
wallet2->set_refresh_from_block_height(restoreHeight); wallet->wallet2->set_refresh_from_block_height(restoreHeight);
wallet2->set_seed_language(language); wallet->wallet2->set_seed_language(language);
initCommon(); wallet->initCommon();
return wallet;
} }
MoneroWallet::~MoneroWallet() { MoneroWallet::~MoneroWallet() {
@@ -836,6 +860,8 @@ namespace monero {
close(); close();
} }
// ----------------------------- WALLET METHODS -----------------------------
void MoneroWallet::setDaemonConnection(const string& uri, const string& username, const string& password) { void MoneroWallet::setDaemonConnection(const string& uri, const string& username, const string& password) {
MTRACE("setDaemonConnection(" << uri << ", " << username << ", " << password << ")"); MTRACE("setDaemonConnection(" << uri << ", " << username << ", " << password << ")");
+8 -12
View File
@@ -155,15 +155,7 @@ namespace monero {
* @param password is the password of the wallet file to open * @param password is the password of the wallet file to open
* @param networkType is the wallet's network type * @param networkType is the wallet's network type
*/ */
MoneroWallet(const string& path, const string& password, const MoneroNetworkType networkType); static MoneroWallet* openWallet(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);
/** /**
* Create a new wallet with a randomly generated seed. * 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 daemonConnection is connection information to a daemon (default = an unconnected wallet)
* @param language is the wallet and mnemonic's language (default = "English") * @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. * 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 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) * @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. * 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 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") * @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. * Deconstruct the wallet.
+1 -1
View File
@@ -70,7 +70,7 @@ int main(int argc, const char* argv[]) {
// load wallet // 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", "", ""); wallet->setDaemonConnection("http://localhost:38081", "", "");
// fetch txs // fetch txs