From dc4dbc1cebc51e2567572c33571f943c0e30cc38 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sat, 20 Jun 2015 12:31:53 +0100 Subject: simplewallet: allow creating a wallet from a public address and view secret key The needed information is supplied via a triple: --generate-from-view-key address:viewkey:filename --- src/wallet/wallet2.cpp | 34 ++++++++++++++++++++++++++++++++++ src/wallet/wallet2.h | 9 +++++++++ 2 files changed, 43 insertions(+) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 0b63ecd05..5dd20cfdd 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -675,6 +675,40 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri return retval; } +/*! +* \brief Creates a watch only wallet from a public address and a view secret key. +* \param wallet_ Name of wallet file +* \param password Password of wallet file +* \param viewkey view secret key +*/ +void wallet2::generate(const std::string& wallet_, const std::string& password, + const cryptonote::account_public_address &account_public_address, + const crypto::secret_key& viewkey) +{ + clear(); + prepare_file_names(wallet_); + + boost::system::error_code ignored_ec; + THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_exists, m_wallet_file); + THROW_WALLET_EXCEPTION_IF(boost::filesystem::exists(m_keys_file, ignored_ec), error::file_exists, m_keys_file); + + m_account.create_from_viewkey(account_public_address, viewkey); + m_account_public_address = account_public_address; + m_watch_only = true; + + bool r = store_keys(m_keys_file, password, true); + 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(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(); +} + /*! * \brief Rewrites to the wallet file for wallet upgrade (doesn't generate key, assumes it's already there) * \param wallet_name Name of wallet file (should exist) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 4a07de8d1..cae21463e 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -145,6 +145,15 @@ namespace tools crypto::secret_key generate(const std::string& wallet, const std::string& password, const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false, bool two_random = false); + /*! + * \brief Creates a watch only wallet from a public address and a view secret key. + * \param wallet_ Name of wallet file + * \param password Password of wallet file + * \param viewkey view secret key + */ + void generate(const std::string& wallet, const std::string& password, + const cryptonote::account_public_address &account_public_address, + const crypto::secret_key& viewkey = crypto::secret_key()); /*! * \brief Rewrites to the wallet file for wallet upgrade (doesn't generate key, assumes it's already there) * \param wallet_name Name of wallet file (should exist) -- cgit v1.2.3