aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/wallet2.cpp51
-rw-r--r--src/wallet/wallet2.h13
-rw-r--r--src/wallet/wallet_errors.h37
-rw-r--r--src/wallet/wallet_rpc_server.cpp4
4 files changed, 78 insertions, 27 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index 48aa164ab..9ba6f245a 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -35,6 +35,7 @@
#include "include_base_utils.h"
using namespace epee;
+#include "cryptonote_config.h"
#include "wallet2.h"
#include "cryptonote_core/cryptonote_format_utils.h"
#include "rpc/core_rpc_server_commands_defs.h"
@@ -420,9 +421,6 @@ bool wallet2::clear()
{
m_blockchain.clear();
m_transfers.clear();
- cryptonote::block b;
- cryptonote::generate_genesis_block(b);
- m_blockchain.push_back(get_block_hash(b));
m_local_bc_height = 1;
return true;
}
@@ -498,9 +496,13 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri
bool r = store_keys(m_keys_file, password);
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
- r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str());
+ r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str(m_testnet));
if(!r) LOG_PRINT_RED_L0("String with address text not saved");
+ cryptonote::block b;
+ generate_genesis(b);
+ m_blockchain.push_back(get_block_hash(b));
+
store();
return retval;
}
@@ -541,8 +543,12 @@ bool wallet2::check_connection()
net_utils::http::url_content u;
net_utils::parse_url(m_daemon_address, u);
+
if(!u.port)
- u.port = RPC_DEFAULT_PORT;
+ {
+ u.port = m_testnet ? config::testnet::RPC_DEFAULT_PORT : config::RPC_DEFAULT_PORT;
+ }
+
return m_http_client.connect(u.host, std::to_string(u.port), WALLET_RCP_CONNECTION_TIMEOUT);
}
//----------------------------------------------------------------------------------------------------
@@ -556,7 +562,7 @@ void wallet2::load(const std::string& wallet_, const std::string& password)
THROW_WALLET_EXCEPTION_IF(e || !exists, error::file_not_found, m_keys_file);
load_keys(m_keys_file, password);
- LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str());
+ LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str(m_testnet));
//keys loaded ok!
//try to load wallet file. but even if we failed, it is not big problem
@@ -573,15 +579,28 @@ void wallet2::load(const std::string& wallet_, const std::string& password)
m_account_public_address.m_view_public_key != m_account.get_keys().m_account_address.m_view_public_key,
error::wallet_files_doesnt_correspond, m_keys_file, m_wallet_file);
- if(m_blockchain.empty())
+ cryptonote::block genesis;
+ generate_genesis(genesis);
+ crypto::hash genesis_hash = get_block_hash(genesis);
+
+ if (m_blockchain.empty())
+ {
+ m_blockchain.push_back(genesis_hash);
+ }
+ else
{
- cryptonote::block b;
- cryptonote::generate_genesis_block(b);
- m_blockchain.push_back(get_block_hash(b));
+ check_genesis(genesis_hash);
}
+
m_local_bc_height = m_blockchain.size();
}
//----------------------------------------------------------------------------------------------------
+void wallet2::check_genesis(const crypto::hash& genesis_hash) {
+ std::string what("Genesis block missmatch. You probably use wallet without testnet flag with blockchain from test network or vice versa");
+
+ THROW_WALLET_EXCEPTION_IF(genesis_hash != m_blockchain[0], error::wallet_internal_error, what);
+}
+//----------------------------------------------------------------------------------------------------
void wallet2::store()
{
bool r = tools::serialize_obj_to_file(*this, m_wallet_file);
@@ -918,4 +937,16 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions(std::vector<crypto
}
}
}
+
+//----------------------------------------------------------------------------------------------------
+void wallet2::generate_genesis(cryptonote::block& b) {
+ if (m_testnet)
+ {
+ cryptonote::generate_genesis_block(b, config::testnet::GENESIS_TX, config::testnet::GENESIS_NONCE);
+ }
+ else
+ {
+ cryptonote::generate_genesis_block(b, config::GENESIS_TX, config::GENESIS_NONCE);
+ }
+}
}
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index a6f4c5c17..3311e3438 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -80,9 +80,9 @@ namespace tools
class wallet2
{
- wallet2(const wallet2&) : m_run(true), m_callback(0) {};
+ wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false) {};
public:
- wallet2() : m_run(true), m_callback(0) {};
+ wallet2(bool testnet = false) : m_run(true), m_callback(0), m_testnet(testnet) {};
struct transfer_details
{
uint64_t m_block_height;
@@ -158,6 +158,8 @@ namespace tools
void refresh(uint64_t start_height, size_t & blocks_fetched, bool& received_money);
bool refresh(size_t & blocks_fetched, bool& received_money, bool& ok);
+ bool testnet() { return m_testnet; }
+
uint64_t balance();
uint64_t unlocked_balance();
template<typename T>
@@ -209,6 +211,8 @@ namespace tools
bool prepare_file_names(const std::string& file_path);
void process_unconfirmed(const cryptonote::transaction& tx);
void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t change_amount);
+ void generate_genesis(cryptonote::block& b);
+ void check_genesis(const crypto::hash& genesis_hash); //throws
cryptonote::account_base m_account;
std::string m_daemon_address;
@@ -228,6 +232,7 @@ namespace tools
std::atomic<bool> m_run;
i_wallet2_callback* m_callback;
+ bool m_testnet;
};
}
BOOST_CLASS_VERSION(tools::wallet2, 7)
@@ -357,7 +362,7 @@ namespace tools
{
THROW_WALLET_EXCEPTION_IF(0 == dt.amount, error::zero_destination);
needed_money += dt.amount;
- THROW_WALLET_EXCEPTION_IF(needed_money < dt.amount, error::tx_sum_overflow, dsts, fee);
+ THROW_WALLET_EXCEPTION_IF(needed_money < dt.amount, error::tx_sum_overflow, dsts, fee, m_testnet);
}
// randomly select inputs for transaction
@@ -462,7 +467,7 @@ namespace tools
}
bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time);
- THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time);
+ THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet);
THROW_WALLET_EXCEPTION_IF(m_upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, m_upper_transaction_size_limit);
std::string key_images;
diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h
index aa29f281a..7914ff8e1 100644
--- a/src/wallet/wallet_errors.h
+++ b/src/wallet/wallet_errors.h
@@ -376,11 +376,18 @@ namespace tools
typedef std::vector<cryptonote::tx_source_entry> sources_t;
typedef std::vector<cryptonote::tx_destination_entry> destinations_t;
- explicit tx_not_constructed(std::string&& loc, const sources_t& sources, const destinations_t& destinations, uint64_t unlock_time)
- : transfer_error(std::move(loc), "transaction was not constructed")
- , m_sources(sources)
- , m_destinations(destinations)
- , m_unlock_time(unlock_time)
+ explicit tx_not_constructed(
+ std::string && loc
+ , sources_t const & sources
+ , destinations_t const & destinations
+ , uint64_t unlock_time
+ , bool testnet
+ )
+ : transfer_error {std::move(loc), "transaction was not constructed"}
+ , m_sources {sources}
+ , m_destinations {destinations}
+ , m_unlock_time {unlock_time}
+ , m_testnet {testnet}
{
}
@@ -414,7 +421,7 @@ namespace tools
for (size_t i = 0; i < m_destinations.size(); ++i)
{
const cryptonote::tx_destination_entry& dst = m_destinations[i];
- ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(dst.addr) << " " <<
+ ss << "\n " << i << ": " << cryptonote::get_account_address_as_str(m_testnet, dst.addr) << " " <<
cryptonote::print_money(dst.amount);
}
@@ -427,6 +434,7 @@ namespace tools
sources_t m_sources;
destinations_t m_destinations;
uint64_t m_unlock_time;
+ bool m_testnet;
};
//----------------------------------------------------------------------------------------------------
struct tx_rejected : public transfer_error
@@ -457,10 +465,16 @@ namespace tools
//----------------------------------------------------------------------------------------------------
struct tx_sum_overflow : public transfer_error
{
- explicit tx_sum_overflow(std::string&& loc, const std::vector<cryptonote::tx_destination_entry>& destinations, uint64_t fee)
- : transfer_error(std::move(loc), "transaction sum + fee exceeds " + cryptonote::print_money(std::numeric_limits<uint64_t>::max()))
- , m_destinations(destinations)
- , m_fee(fee)
+ explicit tx_sum_overflow(
+ std::string && loc
+ , const std::vector<cryptonote::tx_destination_entry>& destinations
+ , uint64_t fee
+ , bool testnet
+ )
+ : transfer_error {std::move(loc), "transaction sum + fee exceeds " + cryptonote::print_money(std::numeric_limits<uint64_t>::max())}
+ , m_destinations {destinations}
+ , m_fee {fee}
+ , m_testnet {testnet}
{
}
@@ -475,7 +489,7 @@ namespace tools
", destinations:";
for (const auto& dst : m_destinations)
{
- ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(dst.addr);
+ ss << '\n' << cryptonote::print_money(dst.amount) << " -> " << cryptonote::get_account_address_as_str(m_testnet, dst.addr);
}
return ss.str();
}
@@ -483,6 +497,7 @@ namespace tools
private:
std::vector<cryptonote::tx_destination_entry> m_destinations;
uint64_t m_fee;
+ bool m_testnet;
};
//----------------------------------------------------------------------------------------------------
struct tx_too_big : public transfer_error
diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp
index fa0a5445e..995e97ede 100644
--- a/src/wallet/wallet_rpc_server.cpp
+++ b/src/wallet/wallet_rpc_server.cpp
@@ -101,7 +101,7 @@ namespace tools
{
try
{
- res.address = m_wallet.get_account().get_public_address_str();
+ res.address = m_wallet.get_account().get_public_address_str(m_wallet.testnet());
}
catch (std::exception& e)
{
@@ -118,7 +118,7 @@ namespace tools
for (auto it = destinations.begin(); it != destinations.end(); it++)
{
cryptonote::tx_destination_entry de;
- if(!get_account_address_from_str(de.addr, it->address))
+ if(!get_account_address_from_str(de.addr, m_wallet.testnet(), it->address))
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS;
er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address;