1
0
Fork 0
forked from lthn/blockchain

fixed a bug in new format seed phrase restoring

This commit is contained in:
sowle 2020-05-07 15:02:35 +03:00
parent 3701b138b6
commit a4b607e0c0
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
6 changed files with 28 additions and 26 deletions

View file

@ -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<std::string> 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<unsigned char> 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<uint64_t*>(&h) = m_creation_timestamp;
h = crypto::cn_fast_hash(&h, sizeof h);
uint64_t h_64 = *reinterpret_cast<uint64_t*>(&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<uint64_t*>(&h) = m_creation_timestamp;
h = crypto::cn_fast_hash(&h, sizeof h);
uint64_t h_64 = *reinterpret_cast<uint64_t*>(&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");

View file

@ -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; }

View file

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

View file

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

View file

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

View file

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