diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 389286ce..c4f60306 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2695,6 +2695,16 @@ bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) c return true; } +std::string wallet2::get_wallet_file() const +{ + return m_wallet_file; +} + +std::string wallet2::get_keys_file() const +{ + return m_keys_file; +} + //---------------------------------------------------------------------------------------------------- void wallet2::generate_genesis(cryptonote::block& b) { if (m_testnet) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 6c689d4b..2835815b 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -348,8 +348,11 @@ namespace tools bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; + bool use_fork_rules(uint8_t version); + std::string get_wallet_file() const; + std::string get_keys_file() const; private: /*! * \brief Stores wallet information to wallet file. diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index 3a80b422..2dfc7b29 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -65,6 +65,7 @@ public: void setListener(Listener *) {} int status() const; std::string errorString() const; + bool setPassword(const std::string &password); private: void clearStatus(); @@ -110,9 +111,6 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co return false; } // TODO: validate language - // TODO: create wallet - // m_wallet.reset(new tools::wallet2()); - m_wallet->set_seed_language(language); crypto::secret_key recovery_val, secret_key; try { @@ -134,7 +132,7 @@ bool WalletImpl::open(const std::string &path, const std::string &password) // TODO: handle "deprecated" m_wallet->load(path, password); result = true; - } catch (const tools::error::file_not_found &e) { + } catch (const std::exception &e) { LOG_ERROR("Error opening wallet: " << e.what()); m_status = Status_Error; m_errorString = e.what(); @@ -185,6 +183,20 @@ std::string WalletImpl::errorString() const return m_errorString; } +bool WalletImpl::setPassword(const std::string &password) +{ + bool result = false; + try { + m_wallet->rewrite(m_wallet->get_wallet_file(), password); + result = true; + } catch (const std::exception &e) { + result = false; + m_status = Status_Error; + m_errorString = e.what(); + } + return result; +} + void WalletImpl::clearStatus() { m_status = Status_Ok; @@ -228,9 +240,10 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string return wallet; } -Wallet * WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) +Wallet *WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language) { return nullptr; + } bool WalletManagerImpl::closeWallet(Wallet *wallet) diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index b89c74e2..c985581d 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -64,6 +64,7 @@ struct Wallet virtual int status() const = 0; //! in case error status, returns error string virtual std::string errorString() const = 0; + virtual bool setPassword(const std::string &password) = 0; }; /** @@ -71,15 +72,37 @@ struct Wallet */ struct WalletManager { - //! creates new wallet + + /*! + * \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 + * \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) = 0; - //! opens existing wallet + /*! + * \brief Opens existing wallet + * \param path Name of wallet file + * \param password Password of wallet file + * \return Wallet instance (Wallet::status() needs to be called to check if opened successfully) + */ virtual Wallet * openWallet(const std::string &path, const std::string &password) = 0; - //! recovers existing wallet using memo (electrum words) + /*! + * \brief recovers existing wallet using memo (electrum seed) + * \param path Name of wallet file to be created + * \param memo memo (25 words electrum seed) + * \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, const std::string &language) = 0; - //! closes wallet. in case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted + + /*! + * \brief Closes wallet. In case operation succeded, wallet object deleted. in case operation failed, wallet object not deleted + * \param wallet previously opened / created wallet instance + * \return None + */ virtual bool closeWallet(Wallet *wallet) = 0; //! checks if wallet with the given name already exists diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index 9f8913c3..fe6cd556 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -48,6 +48,7 @@ struct WalletManagerTest : public testing::Test const char * WALLET_NAME = "testwallet"; const char * WALLET_PASS = "password"; + const char * WALLET_PASS2 = "password22"; const char * WALLET_LANG = "English"; @@ -93,19 +94,36 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet) TEST_F(WalletManagerTest, WalletManagerOpensWallet) { + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); std::string seed1 = wallet1->seed(); ASSERT_TRUE(wmgr->closeWallet(wallet1)); Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->seed() == seed1); - std::vector words; - std::string seed = wallet2->seed(); - boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); - ASSERT_TRUE(words.size() == 25); std::cout << "** seed: " << wallet2->seed() << std::endl; } + +TEST_F(WalletManagerTest, WalletManagerChangesPassword) +{ + + Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); + std::string seed1 = wallet1->seed(); + ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2)); + ASSERT_TRUE(wmgr->closeWallet(wallet1)); + Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS2); + ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); + ASSERT_TRUE(wallet2->seed() == seed1); + ASSERT_TRUE(wmgr->closeWallet(wallet2)); + Bitmonero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); + ASSERT_FALSE(wallet3->status() == Bitmonero::Wallet::Status_Ok); + +} + + + + int main(int argc, char** argv) { //epee::debug::get_set_enable_assert(true, false);