Wallet::setPassword() method for wallet2_api

This commit is contained in:
Ilya Kitaev 2016-03-11 17:05:36 +03:00
parent 57d7ffc4d6
commit 5a4f099540
5 changed files with 80 additions and 13 deletions

View File

@ -2695,6 +2695,16 @@ bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) c
return true; 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) { void wallet2::generate_genesis(cryptonote::block& b) {
if (m_testnet) if (m_testnet)

View File

@ -348,8 +348,11 @@ namespace tools
bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const; bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const;
bool use_fork_rules(uint8_t version); bool use_fork_rules(uint8_t version);
std::string get_wallet_file() const;
std::string get_keys_file() const;
private: private:
/*! /*!
* \brief Stores wallet information to wallet file. * \brief Stores wallet information to wallet file.

View File

@ -65,6 +65,7 @@ public:
void setListener(Listener *) {} void setListener(Listener *) {}
int status() const; int status() const;
std::string errorString() const; std::string errorString() const;
bool setPassword(const std::string &password);
private: private:
void clearStatus(); void clearStatus();
@ -110,9 +111,6 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co
return false; return false;
} }
// TODO: validate language // TODO: validate language
// TODO: create wallet
// m_wallet.reset(new tools::wallet2());
m_wallet->set_seed_language(language); m_wallet->set_seed_language(language);
crypto::secret_key recovery_val, secret_key; crypto::secret_key recovery_val, secret_key;
try { try {
@ -134,7 +132,7 @@ bool WalletImpl::open(const std::string &path, const std::string &password)
// TODO: handle "deprecated" // TODO: handle "deprecated"
m_wallet->load(path, password); m_wallet->load(path, password);
result = true; result = true;
} catch (const tools::error::file_not_found &e) { } catch (const std::exception &e) {
LOG_ERROR("Error opening wallet: " << e.what()); LOG_ERROR("Error opening wallet: " << e.what());
m_status = Status_Error; m_status = Status_Error;
m_errorString = e.what(); m_errorString = e.what();
@ -185,6 +183,20 @@ std::string WalletImpl::errorString() const
return m_errorString; 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() void WalletImpl::clearStatus()
{ {
m_status = Status_Ok; m_status = Status_Ok;
@ -228,9 +240,10 @@ Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string
return wallet; 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; return nullptr;
} }
bool WalletManagerImpl::closeWallet(Wallet *wallet) bool WalletManagerImpl::closeWallet(Wallet *wallet)

View File

@ -64,6 +64,7 @@ struct Wallet
virtual int status() const = 0; virtual int status() const = 0;
//! in case error status, returns error string //! in case error status, returns error string
virtual std::string errorString() const = 0; virtual std::string errorString() const = 0;
virtual bool setPassword(const std::string &password) = 0;
}; };
/** /**
@ -71,15 +72,37 @@ struct Wallet
*/ */
struct WalletManager 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; 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; 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; 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; virtual bool closeWallet(Wallet *wallet) = 0;
//! checks if wallet with the given name already exists //! checks if wallet with the given name already exists

View File

@ -48,6 +48,7 @@ struct WalletManagerTest : public testing::Test
const char * WALLET_NAME = "testwallet"; const char * WALLET_NAME = "testwallet";
const char * WALLET_PASS = "password"; const char * WALLET_PASS = "password";
const char * WALLET_PASS2 = "password22";
const char * WALLET_LANG = "English"; const char * WALLET_LANG = "English";
@ -93,19 +94,36 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
TEST_F(WalletManagerTest, WalletManagerOpensWallet) TEST_F(WalletManagerTest, WalletManagerOpensWallet)
{ {
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG); Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed(); std::string seed1 = wallet1->seed();
ASSERT_TRUE(wmgr->closeWallet(wallet1)); ASSERT_TRUE(wmgr->closeWallet(wallet1));
Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS); Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS);
ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok); ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok);
ASSERT_TRUE(wallet2->seed() == seed1); ASSERT_TRUE(wallet2->seed() == seed1);
std::vector<std::string> 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; 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) int main(int argc, char** argv)
{ {
//epee::debug::get_set_enable_assert(true, false); //epee::debug::get_set_enable_assert(true, false);