Merge pull request #324

dc4dbc1 simplewallet: allow creating a wallet from a public address and view secret key (moneromooo-monero)
6a0f61d account: allow creating an account from a public address and view secret key (moneromooo-monero)
e05a58a wallet2: fix write_watch_only_wallet comment description (moneromooo-monero)
4bf6f0d simplewallet: forbid seed commands for watch only wallets (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2015-06-20 22:40:41 +02:00
commit 4790db9d04
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
6 changed files with 153 additions and 10 deletions

View file

@ -93,6 +93,22 @@ DISABLE_VS_WARNINGS(4244 4345)
return first;
}
//-----------------------------------------------------------------
void account_base::create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey)
{
m_keys.m_account_address = address;
m_keys.m_view_secret_key = viewkey;
struct tm timestamp;
timestamp.tm_year = 2014 - 1900; // year 2014
timestamp.tm_mon = 4 - 1; // month april
timestamp.tm_mday = 15; // 15th of april
timestamp.tm_hour = 0;
timestamp.tm_min = 0;
timestamp.tm_sec = 0;
m_creation_timestamp = mktime(&timestamp);
}
//-----------------------------------------------------------------
const account_keys& account_base::get_keys() const
{
return m_keys;

View file

@ -58,6 +58,7 @@ namespace cryptonote
public:
account_base();
crypto::secret_key generate(const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false, bool two_random = false);
void create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey);
const account_keys& get_keys() const;
std::string get_public_address_str(bool testnet) const;
std::string get_public_integrated_address_str(const crypto::hash &payment_id, bool testnet) const;

View file

@ -76,6 +76,7 @@ namespace
{
const command_line::arg_descriptor<std::string> arg_wallet_file = {"wallet-file", "Use wallet <arg>", ""};
const command_line::arg_descriptor<std::string> arg_generate_new_wallet = {"generate-new-wallet", "Generate new wallet and save it to <arg> or <address>.wallet by default", ""};
const command_line::arg_descriptor<std::string> arg_generate_from_view_key = {"generate-from-view-key", "Generate wallet from (address:viewkey:filename) and save it to <filename>", ""};
const command_line::arg_descriptor<std::string> arg_daemon_address = {"daemon-address", "Use daemon instance at <host>:<port>", ""};
const command_line::arg_descriptor<std::string> arg_daemon_host = {"daemon-host", "Use daemon instance at host <arg> instead of localhost", ""};
const command_line::arg_descriptor<std::string> arg_password = {"password", "Wallet password", "", true};
@ -224,6 +225,11 @@ bool simple_wallet::seed(const std::vector<std::string> &args/* = std::vector<st
bool success = false;
std::string electrum_words;
if (m_wallet->watch_only())
{
fail_msg_writer() << "This wallet is watch-only and cannot have a seed.";
return true;
}
if (m_wallet->is_deterministic())
{
if (m_wallet->get_seed_language().empty())
@ -249,6 +255,11 @@ bool simple_wallet::seed(const std::vector<std::string> &args/* = std::vector<st
bool simple_wallet::seed_set_language(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
bool success = false;
if (m_wallet->watch_only())
{
fail_msg_writer() << "This wallet is watch-only and doesn't have a seed.";
return true;
}
if (!m_wallet->is_deterministic())
{
fail_msg_writer() << "This wallet is non-deterministic and doesn't have a seed.";
@ -390,7 +401,7 @@ bool simple_wallet::ask_wallet_create_if_needed()
// add logic to error out if new wallet requested but named wallet file exists
if (keys_file_exists || wallet_file_exists)
{
if (!m_generate_new.empty() || m_restore_deterministic_wallet)
if (!m_generate_new.empty() || m_restore_deterministic_wallet || !m_generate_from_view_key.empty())
{
fail_msg_writer() << "Attempting to generate or restore wallet, but specified file(s) exist. Exiting to not risk overwriting.";
return false;
@ -445,12 +456,12 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
return false;
}
if(!m_generate_new.empty() && !m_wallet_file.empty())
if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) > 1)
{
fail_msg_writer() << "Specifying both --generate-new-wallet=\"wallet_name\" and --wallet-file=\"wallet_name\" doesn't make sense!";
fail_msg_writer() << "Specifying more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\" and --generate-from-keys doesn't make sense!";
return false;
}
else if (m_generate_new.empty() && m_wallet_file.empty())
else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty())
{
if(!ask_wallet_create_if_needed()) return false;
}
@ -483,7 +494,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
}
}
if (!m_generate_new.empty() || m_restore_deterministic_wallet)
if (!m_generate_new.empty() || m_restore_deterministic_wallet || !m_generate_from_view_key.empty())
{
if (m_wallet_file.empty()) m_wallet_file = m_generate_new; // alias for simplicity later
@ -513,9 +524,50 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
return false;
}
}
bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet,
m_non_deterministic, testnet, old_language);
CHECK_AND_ASSERT_MES(r, false, "account creation failed");
if (!m_generate_from_view_key.empty())
{
// split address:viewkey:filename triple
std::vector<std::string> parts;
boost::split(parts,m_generate_from_view_key, boost::is_any_of(":"));
if (parts.size() < 3)
{
fail_msg_writer() << "--generate-from-view-key needs a address:viewkey:filename triple";
return false;
}
// parse address
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash new_payment_id;
if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, testnet, parts[0]))
{
fail_msg_writer() << "Failed to parse address";
return false;
}
// parse view secret key
cryptonote::blobdata viewkey_data;
if(!epee::string_tools::parse_hexstr_to_binbuff(parts[1], viewkey_data))
{
fail_msg_writer() << "Failed to parse view key secret key";
return false;
}
crypto::secret_key viewkey = *reinterpret_cast<const crypto::secret_key*>(viewkey_data.data());
// parse filename
m_wallet_file = parts[2];
for (size_t n = 3; n < parts.size(); ++n)
m_wallet_file += std::string(":") + parts[n];
bool r = new_wallet(m_wallet_file, pwd_container.password(), address, viewkey, testnet);
CHECK_AND_ASSERT_MES(r, false, "account creation failed");
}
else
{
bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet,
m_non_deterministic, testnet, old_language);
CHECK_AND_ASSERT_MES(r, false, "account creation failed");
}
}
else
{
@ -538,6 +590,7 @@ void simple_wallet::handle_command_line(const boost::program_options::variables_
{
m_wallet_file = command_line::get_arg(vm, arg_wallet_file);
m_generate_new = command_line::get_arg(vm, arg_generate_new_wallet);
m_generate_from_view_key = command_line::get_arg(vm, arg_generate_from_view_key);
m_daemon_address = command_line::get_arg(vm, arg_daemon_address);
m_daemon_host = command_line::get_arg(vm, arg_daemon_host);
m_daemon_port = command_line::get_arg(vm, arg_daemon_port);
@ -667,6 +720,32 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string
return true;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address,
const crypto::secret_key& viewkey, bool testnet)
{
m_wallet_file = wallet_file;
m_wallet.reset(new tools::wallet2(testnet));
m_wallet->callback(this);
try
{
m_wallet->generate(wallet_file, password, address, viewkey);
message_writer(epee::log_space::console_color_white, true) << "Generated new watch-only wallet: "
<< m_wallet->get_account().get_public_address_str(m_wallet->testnet());
std::cout << "view key: " << string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key) << ENDL;
}
catch (const std::exception& e)
{
fail_msg_writer() << "failed to generate new wallet: " << e.what();
return false;
}
m_wallet->init(m_daemon_address);
return true;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::open_wallet(const string &wallet_file, const std::string& password, bool testnet)
{
if (!tools::wallet2::wallet_valid_path_format(wallet_file))
@ -1585,6 +1664,7 @@ int main(int argc, char* argv[])
po::options_description desc_params("Wallet options");
command_line::add_arg(desc_params, arg_wallet_file);
command_line::add_arg(desc_params, arg_generate_new_wallet);
command_line::add_arg(desc_params, arg_generate_from_view_key);
command_line::add_arg(desc_params, arg_password);
command_line::add_arg(desc_params, arg_daemon_address);
command_line::add_arg(desc_params, arg_daemon_host);

View file

@ -76,6 +76,8 @@ namespace cryptonote
bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key,
bool recover, bool two_random, bool testnet, const std::string &old_language);
bool new_wallet(const std::string &wallet_file, const std::string& password, const cryptonote::account_public_address& address,
const crypto::secret_key& viewkey, bool testnet);
bool open_wallet(const std::string &wallet_file, const std::string& password, bool testnet);
bool close_wallet();
@ -194,6 +196,7 @@ namespace cryptonote
private:
std::string m_wallet_file;
std::string m_generate_new;
std::string m_generate_from_view_key;
std::string m_import_path;
std::string m_electrum_seed; // electrum-style seed parameter

View file

@ -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)
@ -689,8 +723,8 @@ void wallet2::rewrite(const std::string& wallet_name, const std::string& passwor
THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file);
}
/*!
* \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)
* \brief Writes to a file named based on the normal wallet (doesn't generate key, assumes it's already there)
* \param wallet_name Base name of wallet file
* \param password Password for wallet file
*/
void wallet2::write_watch_only_wallet(const std::string& wallet_name, const std::string& password)

View file

@ -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)