changes in wallet2_api + implemented WalletManager::openWallet

This commit is contained in:
Ilya Kitaev 2016-03-10 19:43:10 +03:00
parent f1f9279d90
commit 57d7ffc4d6
3 changed files with 155 additions and 33 deletions

View file

@ -32,7 +32,9 @@
#include "wallet2.h" #include "wallet2.h"
#include <memory> #include <memory>
unsigned int epee::g_test_dbg_lock_sleep = 0; namespace epee {
unsigned int g_test_dbg_lock_sleep = 0;
}
namespace Bitmonero { namespace Bitmonero {
@ -56,21 +58,29 @@ public:
bool create(const std::string &path, const std::string &password, bool create(const std::string &path, const std::string &password,
const std::string &language); const std::string &language);
bool open(const std::string &path, const std::string &password); bool open(const std::string &path, const std::string &password);
bool close();
std::string seed() const; std::string seed() const;
std::string getSeedLanguage() const; std::string getSeedLanguage() const;
void setSeedLanguage(const std::string &arg); void setSeedLanguage(const std::string &arg);
void setListener(Listener *) {}
int status() const;
std::string errorString() const;
private:
void clearStatus();
private: private:
//std::unique_ptr<tools::wallet2> m_wallet; //std::unique_ptr<tools::wallet2> m_wallet;
tools::wallet2 * m_wallet; tools::wallet2 * m_wallet;
int m_status;
std::string m_errorString;
}; };
WalletImpl::WalletImpl() WalletImpl::WalletImpl()
:m_wallet(nullptr) :m_wallet(nullptr), m_status(Wallet::Status_Ok)
{ {
m_wallet = new tools::wallet2();
} }
WalletImpl::~WalletImpl() WalletImpl::~WalletImpl()
@ -80,6 +90,9 @@ WalletImpl::~WalletImpl()
bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language) bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language)
{ {
clearStatus();
bool keys_file_exists; bool keys_file_exists;
bool wallet_file_exists; bool wallet_file_exists;
tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists); tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists);
@ -91,28 +104,59 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co
// add logic to error out if new wallet requested but named wallet file exists // add logic to error out if new wallet requested but named wallet file exists
if (keys_file_exists || wallet_file_exists) { if (keys_file_exists || wallet_file_exists) {
m_errorString = "attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting.";
LOG_ERROR("attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting."); LOG_ERROR(m_errorString);
m_status = Status_Error;
return false; return false;
} }
// TODO: validate language // TODO: validate language
// TODO: create wallet // TODO: create wallet
//m_wallet.reset(new tools::wallet2()); // m_wallet.reset(new tools::wallet2());
m_wallet = 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 {
recovery_val = m_wallet->generate(path, password, secret_key, false, false); recovery_val = m_wallet->generate(path, password, secret_key, false, false);
} catch (const std::exception& e) { } catch (const std::exception &e) {
// TODO: log exception LOG_ERROR("Error creating wallet: " << e.what());
m_status = Status_Error;
m_errorString = e.what();
return false; return false;
} }
return true; return true;
} }
bool WalletImpl::open(const std::string &path, const std::string &password)
{
clearStatus();
bool result = false;
try {
// TODO: handle "deprecated"
m_wallet->load(path, password);
result = true;
} catch (const tools::error::file_not_found &e) {
LOG_ERROR("Error opening wallet: " << e.what());
m_status = Status_Error;
m_errorString = e.what();
}
return result;
}
bool WalletImpl::close()
{
bool result = false;
try {
m_wallet->store();
m_wallet->stop();
result = true;
} catch (const std::exception &e) {
m_status = Status_Error;
m_errorString = e.what();
LOG_ERROR("Error closing wallet: " << e.what());
}
return result;
}
std::string WalletImpl::seed() const std::string WalletImpl::seed() const
{ {
std::string seed; std::string seed;
@ -131,6 +175,23 @@ void WalletImpl::setSeedLanguage(const std::string &arg)
m_wallet->set_seed_language(arg); m_wallet->set_seed_language(arg);
} }
int WalletImpl::status() const
{
return m_status;
}
std::string WalletImpl::errorString() const
{
return m_errorString;
}
void WalletImpl::clearStatus()
{
m_status = Status_Ok;
m_errorString.clear();
}
///////////////////////// WalletManager implementation ///////////////////////// ///////////////////////// WalletManager implementation /////////////////////////
class WalletManagerImpl : public WalletManager class WalletManagerImpl : public WalletManager
@ -139,46 +200,68 @@ public:
Wallet * createWallet(const std::string &path, const std::string &password, Wallet * createWallet(const std::string &path, const std::string &password,
const std::string &language); const std::string &language);
Wallet * openWallet(const std::string &path, const std::string &password); Wallet * openWallet(const std::string &path, const std::string &password);
virtual Wallet * recoveryWallet(const std::string &path, const std::string &memo, const std::string &language);
virtual bool closeWallet(Wallet *wallet);
bool walletExists(const std::string &path); bool walletExists(const std::string &path);
int lastError() const; std::string errorString() const;
private: private:
WalletManagerImpl() {} WalletManagerImpl() {}
friend struct WalletManagerFactory; friend struct WalletManagerFactory;
std::string m_errorString;
}; };
Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password, Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password,
const std::string &language) const std::string &language)
{ {
WalletImpl * wallet = new WalletImpl(); WalletImpl * wallet = new WalletImpl();
// TODO open wallet, set password, etc wallet->create(path, password, language);
if (!wallet->create(path, password, language)) {
delete wallet;
wallet = nullptr;
}
return wallet; return wallet;
} }
Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password) Wallet *WalletManagerImpl::openWallet(const std::string &path, const std::string &password)
{
WalletImpl * wallet = new WalletImpl();
wallet->open(path, password);
return wallet;
}
Wallet * WalletManagerImpl::recoveryWallet(const std::string &path, const std::string &memo, const std::string &language)
{ {
return nullptr; return nullptr;
} }
bool WalletManagerImpl::closeWallet(Wallet *wallet)
{
WalletImpl * wallet_ = dynamic_cast<WalletImpl*>(wallet);
bool result = wallet_->close();
if (!result) {
m_errorString = wallet_->errorString();
} else {
delete wallet_;
}
return result;
}
bool WalletManagerImpl::walletExists(const std::string &path) bool WalletManagerImpl::walletExists(const std::string &path)
{ {
return false; return false;
} }
int WalletManagerImpl::lastError() const std::string WalletManagerImpl::errorString() const
{ {
return 0; return m_errorString;
} }
///////////////////// WalletManagerFactory implementation ////////////////////// ///////////////////// WalletManagerFactory implementation //////////////////////
WalletManager *WalletManagerFactory::getWalletManager() WalletManager *WalletManagerFactory::getWalletManager()
{ {
// TODO: initialize logger here
epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0);
if (!g_walletManager) { if (!g_walletManager) {
g_walletManager = new WalletManagerImpl(); g_walletManager = new WalletManagerImpl();
} }
@ -186,8 +269,4 @@ WalletManager *WalletManagerFactory::getWalletManager()
return g_walletManager; return g_walletManager;
} }
} }

View file

@ -44,11 +44,26 @@ namespace Bitmonero {
struct Wallet struct Wallet
{ {
// TODO define wallet interface (decide what needed from wallet2) // TODO define wallet interface (decide what needed from wallet2)
enum Status {
Status_Ok,
Status_Error
};
struct Listener
{
// TODO
};
virtual ~Wallet() = 0; virtual ~Wallet() = 0;
virtual std::string seed() const = 0; virtual std::string seed() const = 0;
virtual std::string getSeedLanguage() const = 0; virtual std::string getSeedLanguage() const = 0;
virtual void setSeedLanguage(const std::string &arg) = 0; virtual void setSeedLanguage(const std::string &arg) = 0;
virtual void setListener(Listener * listener) = 0;
//! returns wallet status (Status_Ok | Status_Error)
virtual int status() const = 0;
//! in case error status, returns error string
virtual std::string errorString() const = 0;
}; };
/** /**
@ -58,12 +73,20 @@ struct WalletManager
{ {
//! creates new wallet //! creates new wallet
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 //! opens existing wallet
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)
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
virtual bool closeWallet(Wallet *wallet) = 0;
//! checks if wallet with the given name already exists
virtual bool walletExists(const std::string &path) = 0; virtual bool walletExists(const std::string &path) = 0;
virtual int lastError() const = 0; virtual std::string errorString() const = 0;
}; };

View file

@ -48,23 +48,27 @@ 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_LANG = "English";
WalletManagerTest() WalletManagerTest()
{ {
std::cout << __FUNCTION__ << std::endl;
wmgr = Bitmonero::WalletManagerFactory::getWalletManager(); wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
deleteWallet(WALLET_NAME); //deleteWallet(WALLET_NAME);
} }
~WalletManagerTest() ~WalletManagerTest()
{ {
std::cout << __FUNCTION__ << std::endl;
deleteWallet(WALLET_NAME); deleteWallet(WALLET_NAME);
} }
void deleteWallet(const std::string & walletname) void deleteWallet(const std::string & walletname)
{ {
std::cout << "** deleting wallet " << std::endl;
boost::filesystem::remove(walletname); boost::filesystem::remove(walletname);
boost::filesystem::remove(walletname + ".address.txt"); boost::filesystem::remove(walletname + ".address.txt");
boost::filesystem::remove(walletname + ".keys"); boost::filesystem::remove(walletname + ".keys");
@ -76,14 +80,30 @@ struct WalletManagerTest : public testing::Test
TEST_F(WalletManagerTest, WalletManagerCreatesWallet) TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
{ {
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, "English"); Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
EXPECT_TRUE(wallet != nullptr); ASSERT_TRUE(wallet->status() == Bitmonero::Wallet::Status_Ok);
EXPECT_TRUE(!wallet->seed().empty()); ASSERT_TRUE(!wallet->seed().empty());
std::vector<std::string> words; std::vector<std::string> words;
std::string seed = wallet->seed(); std::string seed = wallet->seed();
boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on);
EXPECT_TRUE(words.size() == 25); ASSERT_TRUE(words.size() == 25);
std::cout << "** seed: " << wallet->seed() << std::endl; std::cout << "** seed: " << wallet->seed() << std::endl;
ASSERT_TRUE(wmgr->closeWallet(wallet));
}
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<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;
} }
int main(int argc, char** argv) int main(int argc, char** argv)