forked from lthn/blockchain
fixed a bug in new format seed phrase restoring
This commit is contained in:
parent
3701b138b6
commit
a4b607e0c0
6 changed files with 28 additions and 26 deletions
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue