mirror of
https://github.com/monero-project/monero.git
synced 2024-12-30 16:56:11 -05:00
changes in wallet2_api + implemented WalletManager::openWallet
This commit is contained in:
parent
f1f9279d90
commit
57d7ffc4d6
@ -32,7 +32,9 @@
|
||||
#include "wallet2.h"
|
||||
#include <memory>
|
||||
|
||||
unsigned int epee::g_test_dbg_lock_sleep = 0;
|
||||
namespace epee {
|
||||
unsigned int g_test_dbg_lock_sleep = 0;
|
||||
}
|
||||
|
||||
namespace Bitmonero {
|
||||
|
||||
@ -56,21 +58,29 @@ public:
|
||||
bool create(const std::string &path, const std::string &password,
|
||||
const std::string &language);
|
||||
bool open(const std::string &path, const std::string &password);
|
||||
bool close();
|
||||
std::string seed() const;
|
||||
std::string getSeedLanguage() const;
|
||||
void setSeedLanguage(const std::string &arg);
|
||||
void setListener(Listener *) {}
|
||||
int status() const;
|
||||
std::string errorString() const;
|
||||
|
||||
private:
|
||||
void clearStatus();
|
||||
|
||||
private:
|
||||
//std::unique_ptr<tools::wallet2> m_wallet;
|
||||
tools::wallet2 * m_wallet;
|
||||
int m_status;
|
||||
std::string m_errorString;
|
||||
|
||||
};
|
||||
|
||||
|
||||
WalletImpl::WalletImpl()
|
||||
:m_wallet(nullptr)
|
||||
:m_wallet(nullptr), m_status(Wallet::Status_Ok)
|
||||
{
|
||||
|
||||
m_wallet = new tools::wallet2();
|
||||
}
|
||||
|
||||
WalletImpl::~WalletImpl()
|
||||
@ -80,6 +90,9 @@ WalletImpl::~WalletImpl()
|
||||
|
||||
bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language)
|
||||
{
|
||||
|
||||
clearStatus();
|
||||
|
||||
bool keys_file_exists;
|
||||
bool 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
|
||||
if (keys_file_exists || wallet_file_exists) {
|
||||
|
||||
LOG_ERROR("attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting.");
|
||||
m_errorString = "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;
|
||||
|
||||
}
|
||||
// TODO: validate language
|
||||
// TODO: create wallet
|
||||
//m_wallet.reset(new tools::wallet2());
|
||||
m_wallet = new tools::wallet2();
|
||||
m_wallet->set_seed_language(language);
|
||||
// m_wallet.reset(new tools::wallet2());
|
||||
|
||||
m_wallet->set_seed_language(language);
|
||||
crypto::secret_key recovery_val, secret_key;
|
||||
try {
|
||||
recovery_val = m_wallet->generate(path, password, secret_key, false, false);
|
||||
} catch (const std::exception& e) {
|
||||
// TODO: log exception
|
||||
} catch (const std::exception &e) {
|
||||
LOG_ERROR("Error creating wallet: " << e.what());
|
||||
m_status = Status_Error;
|
||||
m_errorString = e.what();
|
||||
return false;
|
||||
}
|
||||
|
||||
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 seed;
|
||||
@ -131,6 +175,23 @@ void WalletImpl::setSeedLanguage(const std::string &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 /////////////////////////
|
||||
class WalletManagerImpl : public WalletManager
|
||||
@ -139,46 +200,68 @@ public:
|
||||
Wallet * createWallet(const std::string &path, const std::string &password,
|
||||
const std::string &language);
|
||||
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);
|
||||
int lastError() const;
|
||||
std::string errorString() const;
|
||||
|
||||
|
||||
private:
|
||||
WalletManagerImpl() {}
|
||||
friend struct WalletManagerFactory;
|
||||
|
||||
std::string m_errorString;
|
||||
};
|
||||
|
||||
Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password,
|
||||
const std::string &language)
|
||||
{
|
||||
WalletImpl * wallet = new WalletImpl();
|
||||
// TODO open wallet, set password, etc
|
||||
if (!wallet->create(path, password, language)) {
|
||||
delete wallet;
|
||||
wallet = nullptr;
|
||||
}
|
||||
wallet->create(path, password, language);
|
||||
return wallet;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int WalletManagerImpl::lastError() const
|
||||
std::string WalletManagerImpl::errorString() const
|
||||
{
|
||||
return 0;
|
||||
return m_errorString;
|
||||
}
|
||||
|
||||
|
||||
|
||||
///////////////////// WalletManagerFactory implementation //////////////////////
|
||||
WalletManager *WalletManagerFactory::getWalletManager()
|
||||
{
|
||||
// TODO: initialize logger here
|
||||
epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0);
|
||||
if (!g_walletManager) {
|
||||
g_walletManager = new WalletManagerImpl();
|
||||
}
|
||||
@ -186,8 +269,4 @@ WalletManager *WalletManagerFactory::getWalletManager()
|
||||
return g_walletManager;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -44,11 +44,26 @@ namespace Bitmonero {
|
||||
struct Wallet
|
||||
{
|
||||
// TODO define wallet interface (decide what needed from wallet2)
|
||||
|
||||
enum Status {
|
||||
Status_Ok,
|
||||
Status_Error
|
||||
};
|
||||
|
||||
struct Listener
|
||||
{
|
||||
// TODO
|
||||
};
|
||||
|
||||
virtual ~Wallet() = 0;
|
||||
virtual std::string seed() const = 0;
|
||||
virtual std::string getSeedLanguage() const = 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
|
||||
virtual Wallet * createWallet(const std::string &path, const std::string &password, const std::string &language) = 0;
|
||||
|
||||
//! opens existing wallet
|
||||
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 int lastError() const = 0;
|
||||
virtual std::string errorString() const = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -48,23 +48,27 @@ struct WalletManagerTest : public testing::Test
|
||||
|
||||
const char * WALLET_NAME = "testwallet";
|
||||
const char * WALLET_PASS = "password";
|
||||
const char * WALLET_LANG = "English";
|
||||
|
||||
|
||||
WalletManagerTest()
|
||||
{
|
||||
std::cout << __FUNCTION__ << std::endl;
|
||||
wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
|
||||
deleteWallet(WALLET_NAME);
|
||||
//deleteWallet(WALLET_NAME);
|
||||
}
|
||||
|
||||
|
||||
~WalletManagerTest()
|
||||
{
|
||||
std::cout << __FUNCTION__ << std::endl;
|
||||
deleteWallet(WALLET_NAME);
|
||||
}
|
||||
|
||||
|
||||
void deleteWallet(const std::string & walletname)
|
||||
{
|
||||
std::cout << "** deleting wallet " << std::endl;
|
||||
boost::filesystem::remove(walletname);
|
||||
boost::filesystem::remove(walletname + ".address.txt");
|
||||
boost::filesystem::remove(walletname + ".keys");
|
||||
@ -76,14 +80,30 @@ struct WalletManagerTest : public testing::Test
|
||||
TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
|
||||
{
|
||||
|
||||
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, "English");
|
||||
EXPECT_TRUE(wallet != nullptr);
|
||||
EXPECT_TRUE(!wallet->seed().empty());
|
||||
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
|
||||
ASSERT_TRUE(wallet->status() == Bitmonero::Wallet::Status_Ok);
|
||||
ASSERT_TRUE(!wallet->seed().empty());
|
||||
std::vector<std::string> words;
|
||||
std::string seed = wallet->seed();
|
||||
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;
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user