From 939629e837d744118acd1191bd08f8627c704402 Mon Sep 17 00:00:00 2001 From: m2049r <30435443+m2049r@users.noreply.github.com> Date: Thu, 11 Jan 2018 16:12:27 +0100 Subject: [PATCH] Wallet API: all recover options with password also renamed memo => mnemonic in api method parms --- src/wallet/api/wallet.cpp | 21 ++++++++++++--- src/wallet/api/wallet.h | 11 ++++++++ src/wallet/api/wallet2_api.h | 43 ++++++++++++++++++++++++++++--- src/wallet/api/wallet_manager.cpp | 30 ++++++++++++++++++--- src/wallet/api/wallet_manager.h | 17 +++++++++++- 5 files changed, 110 insertions(+), 12 deletions(-) diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index fd0b65866..753ce259e 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -457,6 +457,16 @@ bool WalletImpl::recoverFromKeys(const std::string &path, const std::string &address_string, const std::string &viewkey_string, const std::string &spendkey_string) +{ + return recoverFromKeysWithPassword(path, "", language, address_string, viewkey_string, spendkey_string); +} + +bool WalletImpl::recoverFromKeysWithPassword(const std::string &path, + const std::string &password, + const std::string &language, + const std::string &address_string, + const std::string &viewkey_string, + const std::string &spendkey_string) { cryptonote::address_parse_info info; if(!get_account_address_from_str(info, m_wallet->testnet(), address_string)) @@ -524,12 +534,12 @@ bool WalletImpl::recoverFromKeys(const std::string &path, try { if (has_spendkey) { - m_wallet->generate(path, "", info.address, spendkey, viewkey); + m_wallet->generate(path, password, info.address, spendkey, viewkey); setSeedLanguage(language); LOG_PRINT_L1("Generated new wallet from keys with seed language: " + language); } else { - m_wallet->generate(path, "", info.address, viewkey); + m_wallet->generate(path, password, info.address, viewkey); LOG_PRINT_L1("Generated new view only wallet from keys"); } @@ -569,6 +579,11 @@ bool WalletImpl::open(const std::string &path, const std::string &password) } bool WalletImpl::recover(const std::string &path, const std::string &seed) +{ + return recover(path, "", seed); +} + +bool WalletImpl::recover(const std::string &path, const std::string &password, const std::string &seed) { clearStatus(); m_errorString.clear(); @@ -590,7 +605,7 @@ bool WalletImpl::recover(const std::string &path, const std::string &seed) try { m_wallet->set_seed_language(old_language); - m_wallet->generate(path, "", recovery_key, true, false); + m_wallet->generate(path, password, recovery_key, true, false); } catch (const std::exception &e) { m_status = Status_Critical; diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 01359ffc6..2ede5fdf6 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -59,7 +59,18 @@ public: bool createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const; bool open(const std::string &path, const std::string &password); + bool recover(const std::string &path,const std::string &password, + const std::string &seed); + bool recoverFromKeysWithPassword(const std::string &path, + const std::string &password, + const std::string &language, + const std::string &address_string, + const std::string &viewkey_string, + const std::string &spendkey_string = ""); + // following two methods are deprecated since they create passwordless wallets + // use the two equivalent methods above bool recover(const std::string &path, const std::string &seed); + // deprecated: use recoverFromKeysWithPassword() instead bool recoverFromKeys(const std::string &path, const std::string &language, const std::string &address_string, diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h index ab1a48d6e..ed20d8d4e 100644 --- a/src/wallet/api/wallet2_api.h +++ b/src/wallet/api/wallet2_api.h @@ -749,7 +749,7 @@ struct WalletManager * \brief Creates new wallet * \param path Name of wallet file * \param password Password of wallet file - * \param language Language to be used to generate electrum seed memo + * \param language Language to be used to generate electrum seed mnemonic * \return Wallet instance (Wallet::status() needs to be called to check if created successfully) */ virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet = false) = 0; @@ -763,16 +763,51 @@ struct WalletManager virtual Wallet * openWallet(const std::string &path, const std::string &password, bool testnet = false) = 0; /*! - * \brief recovers existing wallet using memo (electrum seed) + * \brief recovers existing wallet using mnemonic (electrum seed) * \param path Name of wallet file to be created - * \param memo memo (25 words electrum seed) + * \param password Password of wallet file + * \param mnemonic mnemonic (25 words electrum seed) * \param testnet testnet * \param restoreHeight restore from start height * \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully) */ - virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, bool testnet = false, uint64_t restoreHeight = 0) = 0; + virtual Wallet * recoveryWallet(const std::string &path, const std::string &password, const std::string &mnemonic, + bool testnet = false, uint64_t restoreHeight = 0) = 0; + + /*! + * \deprecated this method creates a wallet WITHOUT a passphrase, use the alternate recoverWallet() method + * \brief recovers existing wallet using mnemonic (electrum seed) + * \param path Name of wallet file to be created + * \param mnemonic mnemonic (25 words electrum seed) + * \param testnet testnet + * \param restoreHeight restore from start height + * \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully) + */ + virtual Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet = false, uint64_t restoreHeight = 0) = 0; + + /*! + * \brief recovers existing wallet using keys. Creates a view only wallet if spend key is omitted + * \param path Name of wallet file to be created + * \param password Password of wallet file + * \param language language + * \param testnet testnet + * \param restoreHeight restore from start height + * \param addressString public address + * \param viewKeyString view key + * \param spendKeyString spend key (optional) + * \return Wallet instance (Wallet::status() needs to be called to check if recovered successfully) + */ + virtual Wallet * createWalletFromKeys(const std::string &path, + const std::string &password, + const std::string &language, + bool testnet, + uint64_t restoreHeight, + const std::string &addressString, + const std::string &viewKeyString, + const std::string &spendKeyString = "") = 0; /*! + * \deprecated this method creates a wallet WITHOUT a passphrase, use createWalletFromKeys(..., password, ...) instead * \brief recovers existing wallet using keys. Creates a view only wallet if spend key is omitted * \param path Name of wallet file to be created * \param language language diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index a6e5e551e..2dce97f36 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -76,17 +76,39 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string return wallet; } -Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, bool testnet, uint64_t restoreHeight) +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet, uint64_t restoreHeight) +{ + return recoveryWallet(path, "", mnemonic, testnet, restoreHeight); +} + +Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path, + const std::string &language, + bool testnet, + uint64_t restoreHeight, + const std::string &addressString, + const std::string &viewKeyString, + const std::string &spendKeyString) +{ + return createWalletFromKeys(path, "", language, testnet, restoreHeight, + addressString, viewKeyString, spendKeyString); +} + +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, + const std::string &password, + const std::string &mnemonic, + bool testnet, + uint64_t restoreHeight) { WalletImpl * wallet = new WalletImpl(testnet); if(restoreHeight > 0){ wallet->setRefreshFromBlockHeight(restoreHeight); } - wallet->recover(path, memo); + wallet->recover(path, password, mnemonic); return wallet; } -Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path, +Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path, + const std::string &password, const std::string &language, bool testnet, uint64_t restoreHeight, @@ -98,7 +120,7 @@ Wallet *WalletManagerImpl::createWalletFromKeys(const std::string &path, if(restoreHeight > 0){ wallet->setRefreshFromBlockHeight(restoreHeight); } - wallet->recoverFromKeys(path, language, addressString, viewKeyString, spendKeyString); + wallet->recoverFromKeysWithPassword(path, password, language, addressString, viewKeyString, spendKeyString); return wallet; } diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h index ef5b8f91b..8b20a64b3 100644 --- a/src/wallet/api/wallet_manager.h +++ b/src/wallet/api/wallet_manager.h @@ -40,7 +40,22 @@ public: Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language, bool testnet); Wallet * openWallet(const std::string &path, const std::string &password, bool testnet); - virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, bool testnet, uint64_t restoreHeight); + virtual Wallet * recoveryWallet(const std::string &path, + const std::string &password, + const std::string &mnemonic, + bool testnet, + uint64_t restoreHeight); + virtual Wallet * createWalletFromKeys(const std::string &path, + const std::string &password, + const std::string &language, + bool testnet, + uint64_t restoreHeight, + const std::string &addressString, + const std::string &viewKeyString, + const std::string &spendKeyString = ""); + // next two methods are deprecated - use the above version which allow setting of a password + virtual Wallet * recoveryWallet(const std::string &path, const std::string &mnemonic, bool testnet, uint64_t restoreHeight); + // deprecated: use createWalletFromKeys(..., password, ...) instead virtual Wallet * createWalletFromKeys(const std::string &path, const std::string &language, bool testnet,