diff options
author | warptangent <warptangent@inbox.com> | 2014-12-06 06:55:56 -0800 |
---|---|---|
committer | warptangent <warptangent@inbox.com> | 2014-12-08 21:57:03 -0800 |
commit | 26b87dfdc28728aaafc92f28871888a64048c30b (patch) | |
tree | 4cc37fb82f7e108d1bd253ed58959d0c65faa357 /src/wallet/wallet2.cpp | |
parent | version bump to 0.8.8.6 (diff) | |
download | monero-26b87dfdc28728aaafc92f28871888a64048c30b.tar.xz |
Add wallet2::verify_password method
Allows wallet password to be verified without changing wallet state.
Diffstat (limited to 'src/wallet/wallet2.cpp')
-rw-r--r-- | src/wallet/wallet2.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 163e19df4..cbf0ac4be 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -571,6 +571,54 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa } /*! + * \brief verify password for default wallet keys file. + * \param password Password to verify + * + * for verification only + * should not mutate state, unlike load_keys() + * can be used prior to rewriting wallet keys file, to ensure user has entered the correct password + * + */ +bool wallet2::verify_password(const std::string& password) +{ + const std::string keys_file_name = m_keys_file; + wallet2::keys_file_data keys_file_data; + std::string buf; + bool r = epee::file_io_utils::load_file_to_string(keys_file_name, buf); + THROW_WALLET_EXCEPTION_IF(!r, error::file_read_error, keys_file_name); + + // Decrypt the contents + r = ::serialization::parse_binary(buf, keys_file_data); + THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "internal error: failed to deserialize \"" + keys_file_name + '\"'); + crypto::chacha8_key key; + crypto::generate_chacha8_key(password, key); + std::string account_data; + account_data.resize(keys_file_data.account_data.size()); + crypto::chacha8(keys_file_data.account_data.data(), keys_file_data.account_data.size(), key, keys_file_data.iv, &account_data[0]); + + // The contents should be JSON if the wallet follows the new format. + rapidjson::Document json; + if (json.Parse(account_data.c_str(), keys_file_data.account_data.size()).HasParseError()) + { + // old format before JSON wallet key file format + } + else + { + account_data = std::string(json["key_data"].GetString(), json["key_data"].GetString() + + json["key_data"].GetStringLength()); + } + + cryptonote::account_base account_data_check; + + r = epee::serialization::load_t_from_binary(account_data_check, account_data); + const cryptonote::account_keys& keys = account_data_check.get_keys(); + + r = r && verify_keys(keys.m_view_secret_key, keys.m_account_address.m_view_public_key); + r = r && verify_keys(keys.m_spend_secret_key, keys.m_account_address.m_spend_public_key); + return r; +} + +/*! * \brief Generates a wallet or restores one. * \param wallet_ Name of wallet file * \param password Password of wallet file |