From a4b607e0c008d76866831ef3bc26c1b90b033cc5 Mon Sep 17 00:00:00 2001 From: sowle Date: Thu, 7 May 2020 15:02:35 +0300 Subject: [PATCH] fixed a bug in new format seed phrase restoring --- src/currency_core/account.cpp | 39 ++++++++++++++---------------- src/currency_core/account.h | 2 +- src/currency_core/currency_basic.h | 5 ++++ src/wallet/wallet2.cpp | 4 +-- src/wallet/wallet2.h | 2 +- src/wallet/wallets_manager.cpp | 2 +- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/currency_core/account.cpp b/src/currency_core/account.cpp index d732bd62..1e492975 100644 --- a/src/currency_core/account.cpp +++ b/src/currency_core/account.cpp @@ -97,7 +97,7 @@ namespace currency return true; } //----------------------------------------------------------------- - bool account_base::restore_keys_from_braindata(const std::string& seed_phrase) + bool account_base::restore_from_braindata(const std::string& seed_phrase) { //cut the last timestamp word from restore_dats std::list words; @@ -126,33 +126,30 @@ namespace currency return false; } - uint64_t auditable_flag_and_checksum = 0; - try - { + uint64_t auditable_flag_and_checksum = UINT64_MAX; + if (!auditable_flag_and_checksum_word.empty()) auditable_flag_and_checksum = tools::mnemonic_encoding::num_by_word(auditable_flag_and_checksum_word); - } - catch(...) - { - LOG_ERROR("cannot convert seed word: " << auditable_flag_and_checksum_word); - return false; - } - - bool auditable_flag = (auditable_flag_and_checksum & 1) != 0; // auditable flag is the lower 1 bit - uint16_t checksum = auditable_flag_and_checksum >> 1; // checksum -- everything else - constexpr uint16_t checksum_max = tools::mnemonic_encoding::NUMWORDS >> 1; // maximum value of checksum std::vector keys_seed_binary = tools::mnemonic_encoding::text2binary(keys_seed_text); CHECK_AND_ASSERT_MES(keys_seed_binary.size(), false, "text2binary failed to convert the given text"); // don't prints event incorrect seed into the log for security m_creation_timestamp = get_timstamp_from_word(timestamp_word); - // check the checksum - crypto::hash h = crypto::cn_fast_hash(keys_seed_binary.data(), keys_seed_binary.size()); - *reinterpret_cast(&h) = m_creation_timestamp; - h = crypto::cn_fast_hash(&h, sizeof h); - uint64_t h_64 = *reinterpret_cast(&h); - uint16_t checksum_calculated = h_64 % (checksum_max + 1); - CHECK_AND_ASSERT_MES(checksum == checksum_calculated, false, "seed phase has invalid checksum: " << checksum_calculated << ", while " << checksum << " is expected, check your words"); + bool auditable_flag = false; + + // check the checksum if checksum word provided + if (auditable_flag_and_checksum != UINT64_MAX) + { + auditable_flag = (auditable_flag_and_checksum & 1) != 0; // auditable flag is the lower 1 bit + uint16_t checksum = auditable_flag_and_checksum >> 1; // checksum -- everything else + constexpr uint16_t checksum_max = tools::mnemonic_encoding::NUMWORDS >> 1; // maximum value of checksum + crypto::hash h = crypto::cn_fast_hash(keys_seed_binary.data(), keys_seed_binary.size()); + *reinterpret_cast(&h) = m_creation_timestamp; + h = crypto::cn_fast_hash(&h, sizeof h); + uint64_t h_64 = *reinterpret_cast(&h); + uint16_t checksum_calculated = h_64 % (checksum_max + 1); + CHECK_AND_ASSERT_MES(checksum == checksum_calculated, false, "seed phase has invalid checksum: " << checksum_calculated << ", while " << checksum << " is expected, check your words"); + } bool r = restore_keys(keys_seed_binary); CHECK_AND_ASSERT_MES(r, false, "restore_keys failed"); diff --git a/src/currency_core/account.h b/src/currency_core/account.h index 01134418..c11cb4a2 100644 --- a/src/currency_core/account.h +++ b/src/currency_core/account.h @@ -54,7 +54,7 @@ namespace currency std::string get_public_address_str() const; std::string get_restore_braindata() const; - bool restore_keys_from_braindata(const std::string& seed_phrase); + bool restore_from_braindata(const std::string& seed_phrase); uint64_t get_createtime() const { return m_creation_timestamp; } void set_createtime(uint64_t val) { m_creation_timestamp = val; } diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index 8c7c9b2e..9a61c524 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -103,6 +103,11 @@ namespace currency KV_SERIALIZE(flags) END_KV_SERIALIZE_MAP() + bool is_auditable() const + { + return (flags & ACCOUNT_PUBLIC_ADDRESS_FLAG_AUDITABLE) != 0; + } + static account_public_address from_old(const account_public_address_old& rhs) { account_public_address result = AUTO_VAL_INIT(result); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 637d0b3a..455c38b9 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2018,12 +2018,12 @@ void wallet2::generate(const std::wstring& path, const std::string& pass, bool a store(); } //---------------------------------------------------------------------------------------------------- -void wallet2::restore(const std::wstring& path, const std::string& pass, const std::string& restore_key) +void wallet2::restore(const std::wstring& path, const std::string& pass, const std::string& seed_phrase) { clear(); prepare_file_names(path); m_password = pass; - bool r = m_account.restore_keys_from_braindata(restore_key); + bool r = m_account.restore_from_braindata(seed_phrase); init_log_prefix(); THROW_IF_TRUE_WALLET_EX(!r, error::wallet_wrong_seed_error, epee::string_encoding::convert_to_ansii(m_wallet_file)); boost::system::error_code ignored_ec; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 0f258c03..fa2be59b 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -450,7 +450,7 @@ namespace tools }; void assign_account(const currency::account_base& acc); void generate(const std::wstring& path, const std::string& password, bool auditable_wallet); - void restore(const std::wstring& path, const std::string& pass, const std::string& restore_key); + void restore(const std::wstring& path, const std::string& pass, const std::string& seed_phrase); void load(const std::wstring& path, const std::string& password); void store(); void store(const std::wstring& path); diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 9b921be8..5b73eaf6 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -886,7 +886,7 @@ std::string wallets_manager::is_pos_allowed() std::string wallets_manager::is_valid_brain_restore_data(const std::string& brain_text) { currency::account_base acc; - if (acc.restore_keys_from_braindata(brain_text)) + if (acc.restore_from_braindata(brain_text)) return API_RETURN_CODE_TRUE; else return API_RETURN_CODE_FALSE;