Skip to content

Commit

Permalink
Separated imported Filecoin accounts (#13523)
Browse files Browse the repository at this point in the history
* Use current network instead of account selector for Filecoin
  • Loading branch information
spylogsster authored Jun 3, 2022
1 parent 9692682 commit 8990254
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 106 deletions.
154 changes: 141 additions & 13 deletions browser/brave_wallet/keyring_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,128 @@ TEST_F(KeyringServiceUnitTest, GetAccountPathByIndex) {
"m/44'/501'/3'/0'");
}

TEST_F(KeyringServiceUnitTest, MigrationImportedFilecoinPrefs) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
brave_wallet::features::kBraveWalletFilecoinFeature);
base::Value imported_accounts(base::Value::Type::LIST);
base::Value f_address(base::Value::Type::DICTIONARY);
f_address.SetStringKey(kAccountAddress,
"f1h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q");
f_address.SetStringKey("account_name", "1");
f_address.SetIntKey("coin_type", 461);
f_address.SetStringKey("encrypted_private_key", "key");
base::Value t_address(base::Value::Type::DICTIONARY);
t_address.SetStringKey(kAccountAddress,
"t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q");
t_address.SetStringKey("account_name", "2");
t_address.SetIntKey("coin_type", 461);
t_address.SetStringKey("encrypted_private_key", "key2");
imported_accounts.Append(f_address.Clone());
imported_accounts.Append(t_address.Clone());

base::Value filecoin_keyring(base::Value::Type::DICTIONARY);
filecoin_keyring.SetKey(kImportedAccounts, std::move(imported_accounts));
base::Value wallet_keyrings(base::Value::Type::DICTIONARY);
wallet_keyrings.SetKey("filecoin", std::move(filecoin_keyring));
GetPrefs()->Set(kBraveWalletKeyrings, std::move(wallet_keyrings));
KeyringService::MigrateObsoleteProfilePrefs(GetPrefs());
{
SetNetwork(brave_wallet::mojom::kFilecoinMainnet, mojom::CoinType::FIL);
auto* value = KeyringService::GetImportedAccountsPrefForKeyring(
GetPrefs(), mojom::kFilecoinKeyringId);
EXPECT_TRUE(value->is_list());
EXPECT_EQ(value->GetList().size(), 1u);
EXPECT_EQ(value->GetList()[0], f_address);
}
{
SetNetwork(brave_wallet::mojom::kFilecoinTestnet, mojom::CoinType::FIL);
auto* value = KeyringService::GetImportedAccountsPrefForKeyring(
GetPrefs(), mojom::kFilecoinKeyringId);
EXPECT_TRUE(value->is_list());
EXPECT_EQ(value->GetList().size(), 1u);
EXPECT_EQ(value->GetList()[0], t_address);
}
{
SetNetwork(brave_wallet::mojom::kLocalhostChainId, mojom::CoinType::FIL);
auto* value = KeyringService::GetImportedAccountsPrefForKeyring(
GetPrefs(), mojom::kFilecoinKeyringId);
EXPECT_TRUE(value->is_list());
EXPECT_EQ(value->GetList().size(), 1u);
EXPECT_EQ(value->GetList()[0], t_address);
}
}

TEST_F(KeyringServiceUnitTest, MigrationImportedHardwareFilecoinPrefs) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
brave_wallet::features::kBraveWalletFilecoinFeature);
base::Value account_metas(base::Value::Type::DICTIONARY);
base::Value f_address(base::Value::Type::DICTIONARY);
f_address.SetStringKey(kAccountAddress,
"f1h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q");
f_address.SetStringKey("account_name", "1");
f_address.SetIntKey("coin_type", 461);
f_address.SetStringKey("encrypted_private_key", "key");
base::Value t_address(base::Value::Type::DICTIONARY);
t_address.SetStringKey(kAccountAddress,
"t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q");
t_address.SetStringKey("account_name", "2");
t_address.SetIntKey("coin_type", 461);
t_address.SetStringKey("encrypted_private_key", "key2");
account_metas.SetKey("f1h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q",
f_address.Clone());
account_metas.SetKey("t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q",
t_address.Clone());
base::Value device(base::Value::Type::DICTIONARY);
EXPECT_EQ(account_metas.GetDict().size(), 2u);
device.SetKey("Ledger", std::move(account_metas));

base::Value filecoin_keyring(base::Value::Type::DICTIONARY);
filecoin_keyring.SetKey("hardware", std::move(device));
base::Value wallet_keyrings(base::Value::Type::DICTIONARY);
wallet_keyrings.SetKey("filecoin", std::move(filecoin_keyring));
GetPrefs()->Set(kBraveWalletKeyrings, std::move(wallet_keyrings));
KeyringService::MigrateObsoleteProfilePrefs(GetPrefs());
{
SetNetwork(brave_wallet::mojom::kFilecoinMainnet, mojom::CoinType::FIL);
auto* value = KeyringService::GetHardwareAccountsPrefForKeyringUpdate(
GetPrefs(), mojom::kFilecoinKeyringId);

EXPECT_TRUE(value->is_dict());
auto* device = value->FindDictPath("Ledger");
EXPECT_TRUE(device);
EXPECT_EQ(device->GetDict().size(), 1u);
EXPECT_EQ(
*value->FindPath("Ledger.f1h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"),
f_address);
}
{
SetNetwork(brave_wallet::mojom::kFilecoinTestnet, mojom::CoinType::FIL);
auto* value = KeyringService::GetHardwareAccountsPrefForKeyringUpdate(
GetPrefs(), mojom::kFilecoinKeyringId);
EXPECT_TRUE(value->is_dict());
auto* ledger = value->FindDictPath("Ledger");
EXPECT_TRUE(ledger);
EXPECT_EQ(ledger->GetDict().size(), 1u);
EXPECT_EQ(
*value->FindPath("Ledger.t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"),
t_address);
}
{
SetNetwork(brave_wallet::mojom::kLocalhostChainId, mojom::CoinType::FIL);
auto* value = KeyringService::GetHardwareAccountsPrefForKeyringUpdate(
GetPrefs(), mojom::kFilecoinKeyringId);
EXPECT_TRUE(value->is_dict());
auto* ledger = value->FindDictPath("Ledger");
EXPECT_TRUE(ledger);
EXPECT_EQ(ledger->GetDict().size(), 1u);
EXPECT_EQ(
*value->FindPath("Ledger.t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"),
t_address);
}
}

TEST_F(KeyringServiceUnitTest, MigrationPrefs) {
GetPrefs()->SetString(kBraveWalletPasswordEncryptorSalt, "test_salt");
GetPrefs()->SetString(kBraveWalletPasswordEncryptorNonce, "test_nonce");
Expand Down Expand Up @@ -2033,22 +2155,23 @@ TEST_F(KeyringServiceUnitTest, HardwareAccounts) {
"0x111", "m/44'/60'/1'/0/0", "name 1", "Ledger", "device1",
mojom::CoinType::ETH));
new_accounts.push_back(mojom::HardwareWalletAccount::New(
"0x264", "m/44'/461'/0'/0/0", "name 2", "Ledger", "device1",
mojom::CoinType::FIL));
"t1h3n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q", "m/44'/461'/0'/0/0",
"name 2", "Ledger", "device1", mojom::CoinType::FIL));
new_accounts.push_back(mojom::HardwareWalletAccount::New(
"0xEA0", "m/44'/60'/2'/0/0", "name 3", "Ledger", "device2",
mojom::CoinType::ETH));
new_accounts.push_back(mojom::HardwareWalletAccount::New(
"0xFIL", "m/44'/461'/2'/0/0", "filecoin 1", "Ledger", "device2",
mojom::CoinType::FIL));
"t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q", "m/44'/461'/2'/0/0",
"filecoin 1", "Ledger", "device2", mojom::CoinType::FIL));
new_accounts.push_back(mojom::HardwareWalletAccount::New(
"0x222", "m/44'/60'/3'/0/0", "name 4", "Ledger", "device1",
mojom::CoinType::ETH));

SetNetwork(brave_wallet::mojom::kFilecoinTestnet, mojom::CoinType::FIL);
EXPECT_FALSE(observer.AccountsChangedFired());
service.AddHardwareAccounts(std::move(new_accounts));
EXPECT_TRUE(service.IsHardwareAccount("0x111"));
EXPECT_TRUE(service.IsHardwareAccount("0x264"));
EXPECT_TRUE(
service.IsHardwareAccount("t1h3n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer.AccountsChangedFired());
observer.Reset();
Expand Down Expand Up @@ -2102,14 +2225,16 @@ TEST_F(KeyringServiceUnitTest, HardwareAccounts) {
const auto& accounts = keyring_info->account_infos;
EXPECT_EQ(accounts.size(), 2u);

EXPECT_EQ(accounts[0]->address, "0x264");
EXPECT_EQ(accounts[0]->address,
"t1h3n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q");
EXPECT_EQ(accounts[0]->name, "name 2");
EXPECT_EQ(accounts[0]->is_imported, false);
ASSERT_TRUE(accounts[0]->hardware);
EXPECT_EQ(accounts[0]->hardware->device_id, "device1");
EXPECT_EQ(accounts[0]->coin, mojom::CoinType::FIL);

EXPECT_EQ(accounts[1]->address, "0xFIL");
EXPECT_EQ(accounts[1]->address,
"t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q");
EXPECT_EQ(accounts[1]->name, "filecoin 1");
EXPECT_EQ(accounts[1]->is_imported, false);
ASSERT_TRUE(accounts[1]->hardware);
Expand All @@ -2126,22 +2251,23 @@ TEST_F(KeyringServiceUnitTest, HardwareAccounts) {
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer.AccountsChangedFired());
observer.Reset();

ASSERT_FALSE(GetPrefs()
->GetDictionary(kBraveWalletKeyrings)
->FindPath("default.hardware.device1.account_metas.0x111"));

ASSERT_FALSE(GetPrefs()
->GetDictionary(kBraveWalletKeyrings)
->FindPath("default.hardware.device1.account_metas.0x264"));
->FindPath("default.hardware.t.device1.account_metas."
"t1h3n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"));

ASSERT_TRUE(GetPrefs()
->GetDictionary(kBraveWalletKeyrings)
->FindPath("default.hardware.device2.account_metas.0xEA0"));

ASSERT_TRUE(GetPrefs()
->GetDictionary(kBraveWalletKeyrings)
->FindPath("filecoin.hardware.device2.account_metas.0xFIL"));
->FindPath("filecoin.hardware.t.device2.account_metas."
"t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"));

ASSERT_FALSE(observer.AccountsChangedFired());
service.RemoveHardwareAccount("0xEA0", mojom::CoinType::ETH);
Expand Down Expand Up @@ -2183,13 +2309,15 @@ TEST_F(KeyringServiceUnitTest, HardwareAccounts) {
->GetDictionary(kBraveWalletKeyrings)
->FindPath("default.hardware.device2"));

service.RemoveHardwareAccount("0xFIL", mojom::CoinType::FIL);
service.RemoveHardwareAccount("t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q",
mojom::CoinType::FIL);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer.AccountsChangedFired());
observer.Reset();
ASSERT_FALSE(GetPrefs()
->GetDictionary(kBraveWalletKeyrings)
->FindPath("filecoin.hardware.device2.account_metas.0xFIL"));
->FindPath("filecoin.hardware.device2.account_metas."
"t3h4n7rphclbmwyjcp6jrdiwlfcuwbroxy3jvg33q"));
}

TEST_F(KeyringServiceUnitTest, AutoLock) {
Expand Down
Loading

0 comments on commit 8990254

Please sign in to comment.