Skip to content

Commit

Permalink
Uplift of #13125 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Apr 22, 2022
1 parent 973fe14 commit b30a557
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
30 changes: 24 additions & 6 deletions browser/brave_wallet/external_wallets_importer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void ExternalWalletsImporter::GetMnemonic(bool is_legacy_crypto_wallets,
return;
}

const std::string* mnemonic = nullptr;
absl::optional<std::string> mnemonic = absl::nullopt;
absl::optional<int> number_of_accounts = absl::nullopt;
for (const auto& keyring : keyrings->GetList()) {
DCHECK(keyring.is_dict());
Expand All @@ -403,11 +403,29 @@ void ExternalWalletsImporter::GetMnemonic(bool is_legacy_crypto_wallets,
}
if (*type != "HD Key Tree")
continue;
mnemonic = keyring.FindStringPath("data.mnemonic");
if (!mnemonic) {
VLOG(0) << "keyring.data.menmonic is missing";
std::move(callback).Run(false, ImportInfo(), ImportError::kJsonError);
return;
const std::string* str_mnemonic = keyring.FindStringPath("data.mnemonic");
// data.mnemonic is not string, try utf8 encoded byte array
if (!str_mnemonic) {
const auto& dict = keyring.GetDict();
const auto* list = dict.FindListByDottedPath("data.mnemonic");
std::vector<uint8_t> utf8_encoded_mnemonic;
if (list) {
for (const auto& item : *list) {
if (!item.is_int())
break;
utf8_encoded_mnemonic.push_back(item.GetInt());
}
}
if (utf8_encoded_mnemonic.empty()) {
VLOG(0) << "keyring.data.menmonic is missing";

std::move(callback).Run(false, ImportInfo(), ImportError::kJsonError);
return;
}
mnemonic = std::string(utf8_encoded_mnemonic.begin(),
utf8_encoded_mnemonic.end());
} else {
mnemonic = *str_mnemonic;
}
number_of_accounts = keyring.FindIntPath("data.numberOfAccounts");
break;
Expand Down
32 changes: 32 additions & 0 deletions browser/brave_wallet/external_wallets_importer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ const char* valid_data =
"\\\",\\\"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salt\\\":"
"\\\"ZNDNQqgIaLswCtSH72AwaaymPQqmO6VCgpbfAmAuw5s=\\\"}\"}}}";

const char* valid_legacy_mnemonic =
"cushion pitch impact album daring marine much annual budget social "
"clarify "
Expand Down Expand Up @@ -208,6 +227,19 @@ TEST_F(ExternalWalletsImporterUnitTest, OnGetImportInfo) {
EXPECT_EQ(info.number_of_accounts, 1u);
}

TEST_F(ExternalWalletsImporterUnitTest, OnGetImportInfo_UTF8Mnemonic) {
bool result = false;
ImportInfo info;
ImportError error;
SimulateGetImportInfo("brave4ever", valid_data_with_utf8_mnemonic, &result,
&info, &error);
EXPECT_TRUE(result);
EXPECT_EQ(error, ImportError::kNone);
EXPECT_EQ(info.mnemonic, valid_mnemonic);
EXPECT_FALSE(info.is_legacy_crypto_wallets);
EXPECT_EQ(info.number_of_accounts, 1u);
}

TEST_F(ExternalWalletsImporterUnitTest, ImportLegacyWalletError) {
bool result = true;
// argonParams is not a dict
Expand Down

0 comments on commit b30a557

Please sign in to comment.