Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sowle committed Feb 18, 2019
2 parents 4abce77 + 5f1b927 commit 5fce38e
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 77 deletions.
52 changes: 48 additions & 4 deletions contrib/epee/include/file_io_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <iostream>
#include <boost/filesystem.hpp>

#include <boost/filesystem/fstream.hpp>

#ifndef MAKE64
#define MAKE64(low,high) ((__int64)(((DWORD)(low)) | ((__int64)((DWORD)(high))) << 32))
Expand Down Expand Up @@ -261,15 +261,14 @@ namespace file_io_utils
typedef int native_filesystem_handle;
#endif


inline
bool save_string_to_file(const std::string& path_to_file, const std::string& str)
{

try
{
std::ofstream fstream;
fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fstream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
fstream.open(path_to_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
fstream << str;
fstream.close();
Expand All @@ -282,7 +281,25 @@ namespace file_io_utils
}
}

inline bool save_string_to_file(const std::wstring& path_to_file, const std::string& str)
{

try
{
boost::filesystem::ofstream fstream;
fstream.exceptions(boost::filesystem::ofstream::failbit | boost::filesystem::ofstream::badbit);
fstream.open(path_to_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
fstream << str;
fstream.close();
return true;
}

catch (...)
{
return false;
}
}


inline
bool save_buff_to_file(const std::string& path_to_file, const void* pbuff, size_t counter)
Expand Down Expand Up @@ -352,7 +369,6 @@ namespace file_io_utils
return false;
}


inline
bool load_file_to_string(const std::string& path_to_file, std::string& target_str)
{
Expand Down Expand Up @@ -382,6 +398,34 @@ namespace file_io_utils
}
}

inline bool load_file_to_string(const std::wstring& path_to_file, std::string& target_str)
{
try
{
boost::filesystem::ifstream fstream;
fstream.exceptions(boost::filesystem::ifstream::failbit | boost::filesystem::ifstream::badbit);
fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);

boost::filesystem::ifstream::pos_type file_size = fstream.tellg();

if (file_size > 1000000000)
return false;//don't go crazy
size_t file_size_t = static_cast<size_t>(file_size);

target_str.resize(file_size_t);

fstream.seekg(0, std::ios::beg);
fstream.read((char*)target_str.data(), target_str.size());
fstream.close();
return true;
}

catch (...)
{
return false;
}
}

template<class t_value>
bool load_file_to_vector(const std::string& path_to_file, std::vector<t_value>& res)
{
Expand Down
6 changes: 6 additions & 0 deletions contrib/epee/include/string_coding.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ namespace epee
{
namespace string_encoding
{

inline std::wstring utf8_to_wstring(const std::string& str)
{
return boost::locale::conv::utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}

inline std::string wstring_to_utf8(const std::wstring& str)
{
return boost::locale::conv::utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
Expand Down
28 changes: 23 additions & 5 deletions contrib/epee/include/string_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,18 @@ POP_WARNINGS
return res;
}
//----------------------------------------------------------------------------
inline std::string get_filename_from_path(const std::string& str)
inline std::wstring get_extension(const std::wstring& str)
{
std::wstring res;
std::wstring::size_type pos = str.rfind('.');
if (std::wstring::npos == pos)
return res;

res = str.substr(pos + 1, str.size() - pos);
return res;
}
//----------------------------------------------------------------------------
inline std::string get_filename_from_path(const std::string& str)
{
std::string res;
std::string::size_type pos = str.rfind('\\');
Expand All @@ -613,9 +624,6 @@ POP_WARNINGS
return res;
}
//----------------------------------------------------------------------------



inline std::string cut_off_extension(const std::string& str)
{
std::string res;
Expand All @@ -626,8 +634,18 @@ POP_WARNINGS
res = str.substr(0, pos);
return res;
}
//----------------------------------------------------------------------------
inline std::wstring cut_off_extension(const std::wstring& str)
{
std::wstring res;
std::wstring::size_type pos = str.rfind('.');
if (std::wstring::npos == pos)
return str;

//----------------------------------------------------------------------------
res = str.substr(0, pos);
return res;
}
//----------------------------------------------------------------------------
#ifdef _WININET_
inline std::string get_string_from_systemtime(const SYSTEMTIME& sys_time)
{
Expand Down
14 changes: 7 additions & 7 deletions src/common/boost_serialization_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

#include <boost/filesystem/fstream.hpp>

#define CHECK_PROJECT_NAME() std::string project_name = CURRENCY_NAME; ar & project_name; if(project_name != CURRENCY_NAME) {throw std::runtime_error(std::string("wrong storage file: project name in file: ") + project_name + ", expected: " + CURRENCY_NAME );}


namespace tools
{
template<class t_object>
bool serialize_obj_to_file(t_object& obj, const std::string& file_path)
template<class t_object, class t_string>
bool serialize_obj_to_file(t_object& obj, const t_string& file_path)
{
TRY_ENTRY();
std::ofstream data_file;
boost::filesystem::ofstream data_file;
data_file.open( file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc);
if(data_file.fail())
return false;
Expand All @@ -29,12 +29,12 @@ namespace tools
CATCH_ENTRY_L0("serialize_obj_to_file", false);
}

template<class t_object>
bool unserialize_obj_from_file(t_object& obj, const std::string& file_path)
template<class t_object, class t_string>
bool unserialize_obj_from_file(t_object& obj, const t_string& file_path)
{
TRY_ENTRY();

std::ifstream data_file;
boost::filesystem::ifstream data_file;
data_file.open( file_path, std::ios_base::binary | std::ios_base::in);
if(data_file.fail())
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/currency_core/currency_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ namespace currency
txin_to_key& tk = boost::get<txin_to_key>(in);
tei.ins.back().amount = tk.amount;
tei.ins.back().kimage_or_ms_id = epee::string_tools::pod_to_hex(tk.k_image);
std::vector<size_t> absolute_offsets = relative_output_offsets_to_absolute(tk.key_offsets);
std::vector<uint64_t> absolute_offsets = relative_output_offsets_to_absolute(tk.key_offsets);
for (auto& ao : absolute_offsets)
{
tei.ins.back().global_indexes.push_back(0);
Expand Down
11 changes: 5 additions & 6 deletions src/gui/qt-daemon/html5applicationviewer/daemon_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "currency_core/alias_helper.h"
#include "crypto/mnemonic-encoding.h"
#include "common/pre_download.h"
#include "string_coding.h"

daemon_backend::daemon_backend():m_pview(&m_view_stub),
m_stop_singal_sent(false),
Expand Down Expand Up @@ -487,7 +488,7 @@ void daemon_backend::loop()
}
}

bool daemon_backend::open_wallet(const std::string& path, const std::string& password)
bool daemon_backend::open_wallet(const std::wstring& path, const std::string& password)
{
CRITICAL_REGION_LOCAL(m_wallet_lock);
try
Expand Down Expand Up @@ -534,8 +535,7 @@ bool daemon_backend::load_recent_transfers()
return m_pview->set_recent_transfers(tr_hist);
}

bool daemon_backend::generate_wallet(const std::string& path,
const std::string& password, std::string& restore_seed)
bool daemon_backend::generate_wallet(const std::wstring& path, const std::string& password, std::string& restore_seed)
{
CRITICAL_REGION_LOCAL(m_wallet_lock);
try
Expand Down Expand Up @@ -565,8 +565,7 @@ bool daemon_backend::generate_wallet(const std::string& path,

}

bool daemon_backend::restore_wallet(const std::string& path,
const std::string& restore_text, const std::string& password)
bool daemon_backend::restore_wallet(const std::wstring& path, const std::string& restore_text, const std::string& password)
{
CRITICAL_REGION_LOCAL(m_wallet_lock);
try
Expand Down Expand Up @@ -752,7 +751,7 @@ bool daemon_backend::update_wallet_info()
wi.balance = m_wallet->balance();
wi.unlocked_balance = m_wallet->unlocked_balance();
wi.unconfirmed_balance = m_wallet->unconfirmed_balance();
wi.path = m_wallet->get_wallet_path();
wi.path = epee::string_encoding::wstring_to_utf8(m_wallet->get_wallet_path());
m_pview->update_wallet_info(wi);
return true;
}
Expand Down
8 changes: 3 additions & 5 deletions src/gui/qt-daemon/html5applicationviewer/daemon_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ class daemon_backend : public tools::i_wallet2_callback
bool start(int argc, char* argv[], view::i_view* pview_handler);
bool stop();
bool send_stop_signal();
bool open_wallet(const std::string& path, const std::string& password);
bool generate_wallet(const std::string& path, const std::string& password,
std::string& restore_seed);
bool restore_wallet(const std::string& path, const std::string& restore_text,
const std::string& password);
bool open_wallet(const std::wstring& path, const std::string& password);
bool generate_wallet(const std::wstring& path, const std::string& password, std::string& restore_seed);
bool restore_wallet(const std::wstring& path, const std::string& restore_text, const std::string& password);
bool close_wallet();
bool transfer(const view::transfer_params& tp, currency::transaction& res_tx);
bool get_aliases(view::alias_set& al_set);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QClipboard>
#include "warnings.h"
#include "net/http_client.h"
#include "string_coding.h"

Html5ApplicationViewer::Html5ApplicationViewer():
m_quit_requested(false),
Expand Down Expand Up @@ -541,7 +542,7 @@ QString Html5ApplicationViewer::generate_wallet()
return QString();

std::string seed; //not used yet
m_backend.generate_wallet(path.toStdString(), pass.toStdString(), seed);
m_backend.generate_wallet(epee::string_encoding::utf8_to_wstring(path.toStdString()), pass.toStdString(), seed);
QString res = seed.c_str();
//
// QString message = "This is your wallet seed phrase:\n\n";
Expand Down Expand Up @@ -589,7 +590,7 @@ bool Html5ApplicationViewer::restore_wallet(const QString& restore_text, const Q

m_config.wallets_last_used_dir = boost::filesystem::path(path.toStdString()).parent_path().string();

return m_backend.restore_wallet(path.toStdString(), restore_text.toStdString(), password.toStdString());
return m_backend.restore_wallet(epee::string_encoding::utf8_to_wstring(path.toStdString()), restore_text.toStdString(), password.toStdString());
}

void Html5ApplicationViewer::place_to_clipboard(const QString& data)
Expand Down Expand Up @@ -655,7 +656,7 @@ void Html5ApplicationViewer::open_wallet()
if (!ok)
return;

m_backend.open_wallet(path.toStdString(), pass.toStdString());
m_backend.open_wallet(epee::string_encoding::utf8_to_wstring(path.toStdString()), pass.toStdString());
}

QString Html5ApplicationViewer::get_gui_lang()
Expand Down
11 changes: 6 additions & 5 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "wallet/wallet_rpc_server.h"
#include "crypto/mnemonic-encoding.h"
#include "version.h"
#include "string_coding.h"

#if defined(WIN32)
#include <crtdbg.h>
Expand Down Expand Up @@ -337,7 +338,7 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
std::vector<unsigned char> restore_seed;
try
{
restore_seed = m_wallet->generate(wallet_file, password);
restore_seed = m_wallet->generate(string_encoding::convert_to_unicode(wallet_file), password);
message_writer(epee::log_space::console_color_white, true) << "Generated new wallet: " << m_wallet->get_account().get_public_address_str();
std::cout << "view key: " << string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key) << std::endl << std::flush;
}
Expand Down Expand Up @@ -375,7 +376,7 @@ bool simple_wallet::restore_wallet(const std::string &wallet_file, const std::st
try
{
std::vector<unsigned char> seed = crypto::mnemonic_encoding::text2binary(restore_seed);
m_wallet->restore(wallet_file, seed, password);
m_wallet->restore(epee::string_encoding::convert_to_unicode(wallet_file), seed, password);
message_writer(epee::log_space::console_color_white, true) << "Wallet restored: " << m_wallet->get_account().get_public_address_str();
std::cout << "view key: " << string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key) << std::endl << std::flush;
}
Expand Down Expand Up @@ -408,7 +409,7 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa

try
{
m_wallet->load(m_wallet_file, password);
m_wallet->load(epee::string_encoding::convert_to_unicode(m_wallet_file), password);
message_writer(epee::log_space::console_color_white, true) << "Opened wallet" << (m_wallet->is_view_only() ? "watch-only" : "" ) << ": " << m_wallet->get_account().get_public_address_str();
}
catch (const std::exception& e)
Expand Down Expand Up @@ -1042,7 +1043,7 @@ bool simple_wallet::save_watch_only(const std::vector<std::string> &args)
}
try
{
m_wallet->store_keys(args[0], args[1], true);
m_wallet->store_keys(string_encoding::convert_to_unicode(args[0]), args[1], true);
success_msg_writer() << "Keys stored to " << args[0];
}
catch (const std::exception& e)
Expand Down Expand Up @@ -1825,7 +1826,7 @@ int main(int argc, char* argv[])
try
{
LOG_PRINT_L0("Loading wallet...");
wal.load(wallet_file, wallet_password);
wal.load(string_encoding::convert_to_unicode(wallet_file), wallet_password);
wal.init(daemon_address);
if (!offline_mode)
wal.refresh();
Expand Down
Loading

0 comments on commit 5fce38e

Please sign in to comment.