Merge pull request #728

7df2baf fixed "undefined reference" for boost::system library (Ilya Kitaev)
62606f1 Wallet::store_to(path, password) implemented; (Ilya Kitaev)
19fcc74 Wallet::address implemented (Ilya Kitaev)
180ac6e WalletManager::recoveryWallet implemented (Ilya Kitaev)
5a4f099 Wallet::setPassword() method for wallet2_api (Ilya Kitaev)
57d7ffc changes in wallet2_api + implemented WalletManager::openWallet (Ilya Kitaev)
f1f9279 get_seed() included to interface (Ilya Kitaev)
930bed7 tests for wallet2_api (Ilya Kitaev)
318660d wallet2 public api. initial commit (Ilya Kitaev)
This commit is contained in:
Riccardo Spagni 2016-03-16 19:49:48 +02:00
commit 4d34682883
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
10 changed files with 845 additions and 5 deletions

View file

@ -26,9 +26,13 @@
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(wallet_sources
wallet2.cpp
wallet_rpc_server.cpp)
wallet_rpc_server.cpp
wallet2_api.cpp)
set(wallet_headers)
@ -37,7 +41,8 @@ set(wallet_private_headers
wallet_errors.h
wallet_rpc_server.h
wallet_rpc_server_commands_defs.h
wallet_rpc_server_error_codes.h)
wallet_rpc_server_error_codes.h
wallet2_api.h)
bitmonero_private_headers(wallet
${wallet_private_headers})
@ -53,4 +58,6 @@ target_link_libraries(wallet
${Boost_SERIALIZATION_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_REGEX_LIBRARY}
${EXTRA_LIBRARIES})

View file

@ -1461,6 +1461,78 @@ void wallet2::store()
THROW_WALLET_EXCEPTION_IF(e, error::file_save_error, m_wallet_file, e);
boost::filesystem::remove(old_file);
}
void wallet2::store_to(const std::string &path, const std::string &password)
{
// TODO: merge it with wallet2::store() function
// check if we want to store to directory which doesn't exists yet
boost::filesystem::path parent_path = boost::filesystem::path(path).parent_path();
// if path is not exists, try to create it
if (!parent_path.empty() && !boost::filesystem::exists(parent_path)) {
boost::system::error_code ec;
if (!boost::filesystem::create_directories(parent_path, ec)) {
throw std::logic_error(ec.message());
}
}
std::stringstream oss;
boost::archive::binary_oarchive ar(oss);
ar << *this;
wallet2::cache_file_data cache_file_data = boost::value_initialized<wallet2::cache_file_data>();
cache_file_data.cache_data = oss.str();
crypto::chacha8_key key;
generate_chacha8_key_from_secret_keys(key);
std::string cipher;
cipher.resize(cache_file_data.cache_data.size());
cache_file_data.iv = crypto::rand<crypto::chacha8_iv>();
crypto::chacha8(cache_file_data.cache_data.data(), cache_file_data.cache_data.size(), key, cache_file_data.iv, &cipher[0]);
cache_file_data.cache_data = cipher;
const std::string new_file = path;
const std::string old_file = m_wallet_file;
const std::string old_keys_file = m_keys_file;
const std::string old_address_file = m_wallet_file + ".address.txt";
// save to new file
std::ofstream ostr;
ostr.open(new_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
binary_archive<true> oar(ostr);
bool success = ::serialization::serialize(oar, cache_file_data);
ostr.close();
THROW_WALLET_EXCEPTION_IF(!success || !ostr.good(), error::file_save_error, new_file);
// save keys to the new file
// if we here, main wallet file is saved and we only need to save keys and address files
prepare_file_names(path);
store_keys(m_keys_file, password, false);
// save address to the new file
const std::string address_file = m_wallet_file + ".address.txt";
bool r = file_io_utils::save_string_to_file(address_file, m_account.get_public_address_str(m_testnet));
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_wallet_file);
// remove old wallet file
r = boost::filesystem::remove(old_file);
if (!r) {
LOG_ERROR("error removing file: " << old_file);
}
// remove old keys file
r = boost::filesystem::remove(old_keys_file);
if (!r) {
LOG_ERROR("error removing file: " << old_keys_file);
}
// remove old address file
r = boost::filesystem::remove(old_address_file);
if (!r) {
LOG_ERROR("error removing file: " << old_address_file);
}
}
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::unlocked_balance() const
{
@ -2695,6 +2767,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)

View file

@ -212,6 +212,12 @@ namespace tools
void write_watch_only_wallet(const std::string& wallet_name, const std::string& password);
void load(const std::string& wallet, const std::string& password);
void store();
/*!
* \brief store_to - stores wallet to another file(s), deleting old ones
* \param path - path to the wallet file (keys and address filenames will be generated based on this filename)
* \param password - password to protect new wallet (TODO: probably better save the password in the wallet object?)
*/
void store_to(const std::string &path, const std::string &password);
/*!
* \brief verifies given password is correct for default wallet keys file
@ -348,8 +354,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.

346
src/wallet/wallet2_api.cpp Normal file
View file

@ -0,0 +1,346 @@
// Copyright (c) 2014-2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include "wallet2_api.h"
#include "wallet2.h"
#include "mnemonics/electrum-words.h"
#include <memory>
namespace epee {
unsigned int g_test_dbg_lock_sleep = 0;
}
namespace Bitmonero {
struct WalletManagerImpl;
namespace {
static WalletManagerImpl * g_walletManager = nullptr;
}
Wallet::~Wallet() {}
///////////////////////// Wallet implementation ////////////////////////////////
class WalletImpl : public Wallet
{
public:
WalletImpl();
~WalletImpl();
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 recover(const std::string &path, const std::string &seed);
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;
bool setPassword(const std::string &password);
std::string address() const;
bool store(const std::string &path);
private:
void clearStatus();
private:
//std::unique_ptr<tools::wallet2> m_wallet;
tools::wallet2 * m_wallet;
int m_status;
std::string m_errorString;
std::string m_password;
};
WalletImpl::WalletImpl()
:m_wallet(nullptr), m_status(Wallet::Status_Ok)
{
m_wallet = new tools::wallet2();
}
WalletImpl::~WalletImpl()
{
delete m_wallet;
}
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);
// TODO: figure out how to setup logger;
LOG_PRINT_L3("wallet_path: " << path << "");
LOG_PRINT_L3("keys_file_exists: " << std::boolalpha << keys_file_exists << std::noboolalpha
<< " wallet_file_exists: " << std::boolalpha << wallet_file_exists << std::noboolalpha);
// add logic to error out if new wallet requested but named 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(m_errorString);
m_status = Status_Error;
return false;
}
// TODO: validate language
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);
m_password = password;
m_status = Status_Ok;
} 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();
try {
// TODO: handle "deprecated"
m_wallet->load(path, password);
m_password = password;
} catch (const std::exception &e) {
LOG_ERROR("Error opening wallet: " << e.what());
m_status = Status_Error;
m_errorString = e.what();
}
return m_status == Status_Ok;
}
bool WalletImpl::recover(const std::string &path, const std::string &seed)
{
clearStatus();
m_errorString.clear();
if (seed.empty()) {
m_errorString = "Electrum seed is empty";
LOG_ERROR(m_errorString);
m_status = Status_Error;
return false;
}
crypto::secret_key recovery_key;
std::string old_language;
if (!crypto::ElectrumWords::words_to_bytes(seed, recovery_key, old_language)) {
m_errorString = "Electrum-style word list failed verification";
m_status = Status_Error;
return false;
}
try {
m_wallet->set_seed_language(old_language);
m_wallet->generate(path, "", recovery_key, true, false);
// TODO: wallet->init(daemon_address);
} catch (const std::exception &e) {
m_status = Status_Error;
m_errorString = e.what();
}
return m_status == Status_Ok;
}
bool WalletImpl::close()
{
clearStatus();
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;
if (m_wallet)
m_wallet->get_seed(seed);
return seed;
}
std::string WalletImpl::getSeedLanguage() const
{
return m_wallet->get_seed_language();
}
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;
}
bool WalletImpl::setPassword(const std::string &password)
{
clearStatus();
try {
m_wallet->rewrite(m_wallet->get_wallet_file(), password);
m_password = password;
} catch (const std::exception &e) {
m_status = Status_Error;
m_errorString = e.what();
}
return m_status == Status_Ok;
}
std::string WalletImpl::address() const
{
return m_wallet->get_account().get_public_address_str(m_wallet->testnet());
}
bool WalletImpl::store(const std::string &path)
{
clearStatus();
try {
if (path.empty()) {
m_wallet->store();
} else {
m_wallet->store_to(path, m_password);
}
} catch (const std::exception &e) {
LOG_ERROR("Error storing wallet: " << e.what());
m_status = Status_Error;
m_errorString = e.what();
}
return m_status == Status_Ok;
}
void WalletImpl::clearStatus()
{
m_status = Status_Ok;
m_errorString.clear();
}
///////////////////////// WalletManager implementation /////////////////////////
class WalletManagerImpl : public WalletManager
{
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);
virtual bool closeWallet(Wallet *wallet);
bool walletExists(const std::string &path);
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();
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)
{
WalletImpl * wallet = new WalletImpl();
wallet->recover(path, memo);
return wallet;
}
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;
}
std::string WalletManagerImpl::errorString() const
{
return m_errorString;
}
///////////////////// WalletManagerFactory implementation //////////////////////
WalletManager *WalletManagerFactory::getWalletManager()
{
if (!g_walletManager) {
epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0);
g_walletManager = new WalletManagerImpl();
}
return g_walletManager;
}
}

124
src/wallet/wallet2_api.h Normal file
View file

@ -0,0 +1,124 @@
// Copyright (c) 2014-2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
#include <string>
// Public interface for libwallet library
namespace Bitmonero {
/**
* @brief Interface for wallet operations.
* TODO: check if /include/IWallet.h is still actual
*/
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;
virtual bool setPassword(const std::string &password) = 0;
virtual std::string address() const = 0;
virtual bool store(const std::string &path) = 0;
};
/**
* @brief WalletManager - provides functions to manage wallets
*/
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
* \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;
/*!
* \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;
/*!
* \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) = 0;
/*!
* \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
virtual bool walletExists(const std::string &path) = 0;
virtual std::string errorString() const = 0;
};
struct WalletManagerFactory
{
static WalletManager * getWalletManager();
};
}

View file

@ -210,7 +210,6 @@ namespace tools
//----------------------------------------------------------------------------------------------------
typedef file_error_base<file_exists_message_index> file_exists;
typedef file_error_base<file_not_found_message_index> file_not_found;
typedef file_error_base<file_not_found_message_index> file_not_found;
typedef file_error_base<file_read_error_message_index> file_read_error;
typedef file_error_base<file_save_error_message_index> file_save_error;
//----------------------------------------------------------------------------------------------------

View file

@ -54,7 +54,7 @@ else ()
endif()
endif ()
add_subdirectory(core_tests)
#add_subdirectory(core_tests)
add_subdirectory(crypto)
add_subdirectory(functional_tests)
add_subdirectory(performance_tests)
@ -63,6 +63,7 @@ add_subdirectory(unit_tests)
add_subdirectory(difficulty)
add_subdirectory(hash)
add_subdirectory(net_load_tests)
add_subdirectory(libwallet_api_tests)
# add_subdirectory(daemon_tests)

View file

@ -40,7 +40,8 @@ set(crypto_headers
add_executable(crypto-tests
${crypto_sources}
${crypto_headers})
target_link_libraries(crypto-tests)
target_link_libraries(crypto-tests
${Boost_SYSTEM_LIBRARY})
set_property(TARGET crypto-tests
PROPERTY
FOLDER "tests")

View file

@ -0,0 +1,59 @@
# Copyright (c) 2014-2016, The Monero Project
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set(libwallet_api_tests_sources
main.cpp
)
set(libwallet_api_tests_headers
)
add_executable(libwallet_api_tests
${libwallet_api_tests_sources}
${libwallet_api_tests_headers})
target_link_libraries(libwallet_api_tests
LINK_PRIVATE
wallet
${GTEST_MAIN_LIBRARIES}
${EXTRA_LIBRARIES})
set_property(TARGET libwallet_api_tests
PROPERTY
FOLDER "tests")
if (NOT MSVC)
set_property(TARGET libwallet_api_tests
APPEND_STRING
PROPERTY
COMPILE_FLAGS " -Wno-undef -Wno-sign-compare")
endif ()
add_test(
NAME libwallet_api_tests
COMMAND libwallet_api_tests)

View file

@ -0,0 +1,212 @@
// Copyright (c) 2014-2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include "gtest/gtest.h"
#include "wallet/wallet2_api.h"
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <vector>
using namespace std;
//unsigned int epee::g_test_dbg_lock_sleep = 0;
struct WalletManagerTest : public testing::Test
{
Bitmonero::WalletManager * wmgr;
const char * WALLET_NAME = "testwallet";
const char * WALLET_NAME_COPY = "testwallet_copy";
const char * WALLET_NAME_WITH_DIR = "walletdir/testwallet_test";
const char * WALLET_NAME_WITH_DIR_NON_WRITABLE = "/var/walletdir/testwallet_test";
const char * WALLET_PASS = "password";
const char * WALLET_PASS2 = "password22";
const char * WALLET_LANG = "English";
WalletManagerTest()
{
std::cout << __FUNCTION__ << std::endl;
wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
deleteWallet(WALLET_NAME);
deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string());
}
~WalletManagerTest()
{
std::cout << __FUNCTION__ << std::endl;
//deleteWallet(WALLET_NAME);
}
void deleteWallet(const std::string & walletname)
{
std::cout << "** deleting wallet: " << walletname << std::endl;
boost::filesystem::remove(walletname);
boost::filesystem::remove(walletname + ".address.txt");
boost::filesystem::remove(walletname + ".keys");
}
void deleteDir(const std::string &path)
{
std::cout << "** removing dir recursively: " << path << std::endl;
boost::filesystem::remove_all(path);
}
};
TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
{
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);
ASSERT_TRUE(words.size() == 25);
std::cout << "** seed: " << wallet->seed() << std::endl;
ASSERT_FALSE(wallet->address().empty());
std::cout << "** address: " << wallet->address() << 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::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);
}
TEST_F(WalletManagerTest, WalletManagerRecoversWallet)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
std::string address1 = wallet1->address();
ASSERT_FALSE(address1.empty());
ASSERT_TRUE(wmgr->closeWallet(wallet1));
deleteWallet(WALLET_NAME);
Bitmonero::Wallet * wallet2 = wmgr->recoveryWallet(WALLET_NAME, seed1);
ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok);
ASSERT_TRUE(wallet2->seed() == seed1);
ASSERT_TRUE(wallet2->address() == address1);
ASSERT_TRUE(wmgr->closeWallet(wallet2));
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet1)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
std::string address1 = wallet1->address();
ASSERT_TRUE(wallet1->store(""));
ASSERT_TRUE(wallet1->store(WALLET_NAME_COPY));
ASSERT_TRUE(wmgr->closeWallet(wallet1));
Bitmonero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME_COPY, WALLET_PASS);
ASSERT_TRUE(wallet2->status() == Bitmonero::Wallet::Status_Ok);
ASSERT_TRUE(wallet2->seed() == seed1);
ASSERT_TRUE(wallet2->address() == address1);
ASSERT_TRUE(wmgr->closeWallet(wallet2));
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet2)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
std::string address1 = wallet1->address();
ASSERT_TRUE(wallet1->store(WALLET_NAME_WITH_DIR));
ASSERT_TRUE(wmgr->closeWallet(wallet1));
wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR, WALLET_PASS);
ASSERT_TRUE(wallet1->status() == Bitmonero::Wallet::Status_Ok);
ASSERT_TRUE(wallet1->seed() == seed1);
ASSERT_TRUE(wallet1->address() == address1);
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet3)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
std::string address1 = wallet1->address();
ASSERT_FALSE(wallet1->store(WALLET_NAME_WITH_DIR_NON_WRITABLE));
ASSERT_TRUE(wmgr->closeWallet(wallet1));
wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR_NON_WRITABLE, WALLET_PASS);
ASSERT_FALSE(wallet1->status() == Bitmonero::Wallet::Status_Ok);
ASSERT_FALSE(wmgr->closeWallet(wallet1));
wallet1 = wmgr->openWallet(WALLET_NAME, WALLET_PASS);
ASSERT_TRUE(wallet1->status() == Bitmonero::Wallet::Status_Ok);
ASSERT_TRUE(wallet1->seed() == seed1);
ASSERT_TRUE(wallet1->address() == address1);
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
int main(int argc, char** argv)
{
//epee::debug::get_set_enable_assert(true, false);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}