From 826bdff8e8d630b54fc50e907ce01d971d9ab073 Mon Sep 17 00:00:00 2001 From: oisupov Date: Thu, 1 Sep 2022 01:24:42 +0700 Subject: [PATCH 1/2] Select default account when create or restore wallet Resolves https://github.com/brave/brave-browser/issues/24800 --- .../brave_wallet/keyring_service_unittest.cc | 44 ++++++++++++++++-- .../brave_wallet/browser/keyring_service.cc | 46 +++++++++++++++---- 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/browser/brave_wallet/keyring_service_unittest.cc b/browser/brave_wallet/keyring_service_unittest.cc index dd2f502cecaf..cfbae2246b76 100644 --- a/browser/brave_wallet/keyring_service_unittest.cc +++ b/browser/brave_wallet/keyring_service_unittest.cc @@ -1466,6 +1466,12 @@ TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) { std::vector account_infos = service.GetAccountInfosForKeyring(mojom::kDefaultKeyringId); EXPECT_EQ(account_infos.size(), 1u); + EXPECT_EQ(account_infos[0]->address, + service.GetSelectedAccount(mojom::CoinType::ETH).value()); + EXPECT_FALSE(service.GetSelectedAccount(mojom::CoinType::SOL)); + EXPECT_FALSE(service.GetFilecoinSelectedAccount(mojom::kFilecoinMainnet)); + EXPECT_FALSE(service.GetFilecoinSelectedAccount(mojom::kFilecoinTestnet)); + EXPECT_FALSE(account_infos[0]->address.empty()); const std::string address0 = account_infos[0]->address; EXPECT_EQ(account_infos[0]->name, "Account 1"); @@ -1476,11 +1482,39 @@ TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) { [&mnemonic_to_be_restored, &service, &address0]() { EXPECT_TRUE( RestoreWallet(&service, *mnemonic_to_be_restored, "brave1", false)); - std::vector account_infos = - service.GetAccountInfosForKeyring(mojom::kDefaultKeyringId); - EXPECT_EQ(account_infos.size(), 1u); - EXPECT_EQ(account_infos[0]->address, address0); - EXPECT_EQ(account_infos[0]->name, "Account 1"); + { + std::vector account_infos = + service.GetAccountInfosForKeyring(mojom::kDefaultKeyringId); + EXPECT_EQ(account_infos.size(), 1u); + EXPECT_EQ(account_infos[0]->address, + service.GetSelectedAccount(mojom::CoinType::ETH).value()); + EXPECT_EQ(account_infos[0]->address, address0); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + } + + { + std::vector account_infos = + service.GetAccountInfosForKeyring( + mojom::kFilecoinTestnetKeyringId); + EXPECT_EQ(account_infos.size(), 0u); + EXPECT_FALSE( + service.GetFilecoinSelectedAccount(mojom::kFilecoinTestnet)); + } + + { + std::vector account_infos = + service.GetAccountInfosForKeyring(mojom::kFilecoinKeyringId); + EXPECT_EQ(account_infos.size(), 0u); + EXPECT_FALSE( + service.GetFilecoinSelectedAccount(mojom::kFilecoinMainnet)); + } + + { + std::vector account_infos = + service.GetAccountInfosForKeyring(mojom::kSolanaKeyringId); + EXPECT_EQ(account_infos.size(), 0u); + EXPECT_FALSE(service.GetSelectedAccount(mojom::CoinType::SOL)); + } }); verify_restore_wallet.Run(); diff --git a/components/brave_wallet/browser/keyring_service.cc b/components/brave_wallet/browser/keyring_service.cc index c5f4a2ab936d..0b16c993ec74 100644 --- a/components/brave_wallet/browser/keyring_service.cc +++ b/components/brave_wallet/browser/keyring_service.cc @@ -614,7 +614,6 @@ HDKeyring* KeyringService::RestoreKeyring(const std::string& keyring_id, bool is_legacy_brave_wallet) { if (!IsValidMnemonic(mnemonic)) return nullptr; - // Try getting existing mnemonic first if (CreateEncryptorForKeyring(password, keyring_id)) { const std::string current_mnemonic = GetMnemonicForKeyringImpl(keyring_id); @@ -695,7 +694,12 @@ void KeyringService::CreateWallet(const std::string& password, auto* keyring = CreateKeyring(mojom::kDefaultKeyringId, password); if (keyring) { - AddAccountForKeyring(mojom::kDefaultKeyringId, GetAccountName(1)); + auto address = + AddAccountForKeyring(mojom::kDefaultKeyringId, GetAccountName(1)); + if (address) { + SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), + mojom::kDefaultKeyringId); + } } // keep encryptor pre-created @@ -725,30 +729,53 @@ void KeyringService::RestoreWallet(const std::string& mnemonic, auto* keyring = RestoreKeyring(mojom::kDefaultKeyringId, mnemonic, password, is_legacy_brave_wallet); if (keyring && !keyring->GetAccountsNumber()) { - AddAccountForKeyring(mojom::kDefaultKeyringId, GetAccountName(1)); + auto address = + AddAccountForKeyring(mojom::kDefaultKeyringId, GetAccountName(1)); + if (address) { + SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), + mojom::kDefaultKeyringId); + } } if (IsFilecoinEnabled()) { // Restore mainnet filecoin acc auto* filecoin_keyring = RestoreKeyring(mojom::kFilecoinKeyringId, mnemonic, password, is_legacy_brave_wallet); - if (filecoin_keyring && !filecoin_keyring->GetAccountsNumber()) - AddAccountForKeyring(mojom::kFilecoinKeyringId, GetAccountName(1)); + if (filecoin_keyring && !filecoin_keyring->GetAccountsNumber()) { + auto address = + AddAccountForKeyring(mojom::kFilecoinKeyringId, GetAccountName(1)); + if (address) { + SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), + mojom::kFilecoinKeyringId); + } + } // Restore testnet filecoin acc auto* testnet_filecoin_keyring = RestoreKeyring(mojom::kFilecoinTestnetKeyringId, mnemonic, password, is_legacy_brave_wallet); if (testnet_filecoin_keyring && - !testnet_filecoin_keyring->GetAccountsNumber()) - AddAccountForKeyring(mojom::kFilecoinTestnetKeyringId, GetAccountName(1)); + !testnet_filecoin_keyring->GetAccountsNumber()) { + auto address = AddAccountForKeyring(mojom::kFilecoinTestnetKeyringId, + GetAccountName(1)); + if (address) { + SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), + mojom::kFilecoinTestnetKeyringId); + } + } } if (IsSolanaEnabled()) { auto* solana_keyring = RestoreKeyring(mojom::kSolanaKeyringId, mnemonic, password, is_legacy_brave_wallet); - if (solana_keyring && !solana_keyring->GetAccountsNumber()) - AddAccountForKeyring(mojom::kSolanaKeyringId, GetAccountName(1)); + if (solana_keyring && !solana_keyring->GetAccountsNumber()) { + auto address = + AddAccountForKeyring(mojom::kSolanaKeyringId, GetAccountName(1)); + if (address) { + SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), + mojom::kSolanaKeyringId); + } + } } if (keyring) { @@ -1129,7 +1156,6 @@ bool KeyringService::SetSelectedAccountForCoinSilently( if (keyring_id.empty()) { return false; } - SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(address), keyring_id); if (coin == mojom::CoinType::FIL) { json_rpc_service_->SetNetwork(keyring_id == mojom::kFilecoinKeyringId From 46f709f583c1763ead6e143fb76f439fee365259 Mon Sep 17 00:00:00 2001 From: oisupov Date: Fri, 16 Sep 2022 20:29:45 +0700 Subject: [PATCH 2/2] Create default solana acc when create\restore the wallet Resolves https://github.com/brave/brave-browser/issues/25432 Review fix --- .../brave_wallet/brave_wallet_p3a_unittest.cc | 12 ++ .../brave_wallet/keyring_service_unittest.cc | 103 ++++++++++++++++-- .../solana_provider_browsertest.cc | 15 ++- .../browser/brave_wallet_utils.cc | 6 + .../brave_wallet/browser/brave_wallet_utils.h | 1 + .../browser/eth_tx_manager_unittest.cc | 13 +++ .../brave_wallet/browser/keyring_service.cc | 20 ++++ .../brave_wallet/browser/keyring_service.h | 4 + components/brave_wallet/common/features.cc | 2 + components/brave_wallet/common/features.h | 6 +- 10 files changed, 165 insertions(+), 17 deletions(-) diff --git a/browser/brave_wallet/brave_wallet_p3a_unittest.cc b/browser/brave_wallet/brave_wallet_p3a_unittest.cc index 452aa0f697d1..24375eded8ac 100644 --- a/browser/brave_wallet/brave_wallet_p3a_unittest.cc +++ b/browser/brave_wallet/brave_wallet_p3a_unittest.cc @@ -7,8 +7,10 @@ #include "brave/browser/brave_wallet/brave_wallet_service_factory.h" #include "brave/browser/brave_wallet/keyring_service_factory.h" #include "brave/components/brave_wallet/browser/brave_wallet_service.h" +#include "brave/components/brave_wallet/browser/brave_wallet_utils.h" #include "brave/components/brave_wallet/browser/keyring_service.h" #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" +#include "brave/components/brave_wallet/common/features.h" #include "chrome/test/base/testing_profile_manager.h" #include "content/public/test/browser_task_environment.h" #include "testing/gtest/include/gtest/gtest.h" @@ -105,6 +107,16 @@ TEST_F(BraveWalletP3AUnitTest, DefaultSolanaWalletSetting) { } TEST_F(BraveWalletP3AUnitTest, KeyringCreated) { + base::test::ScopedFeatureList feature_list; + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + histogram_tester_->ExpectBucketCount("Brave.Wallet.KeyringCreated", 0, 1); keyring_service_->CreateWallet("testing123", base::DoNothing()); WaitForResponse(); diff --git a/browser/brave_wallet/keyring_service_unittest.cc b/browser/brave_wallet/keyring_service_unittest.cc index cfbae2246b76..96a6bb868761 100644 --- a/browser/brave_wallet/keyring_service_unittest.cc +++ b/browser/brave_wallet/keyring_service_unittest.cc @@ -1260,6 +1260,7 @@ TEST_F(KeyringServiceUnitTest, LockAndUnlock) { {brave_wallet::features::kBraveWalletFilecoinFeature, brave_wallet::features::kBraveWalletSolanaFeature}, {}); + { KeyringService service(json_rpc_service(), GetPrefs()); // No encryptor @@ -1445,6 +1446,16 @@ TEST_F(KeyringServiceUnitTest, AccountMetasForKeyring) { } TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) { + base::test::ScopedFeatureList feature_list; + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + KeyringService service(json_rpc_service(), GetPrefs()); TestKeyringServiceObserver observer; @@ -1538,6 +1549,28 @@ TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) { EXPECT_FALSE(observer.IsKeyringRestored(mojom::kSolanaKeyringId)); } +TEST_F(KeyringServiceUnitTest, DefaultSolanaAccountCreated) { + KeyringService service(json_rpc_service(), GetPrefs()); + ASSERT_NE(CreateWallet(&service, "brave"), absl::nullopt); + + std::vector account_infos = + service.GetAccountInfosForKeyring(mojom::kSolanaKeyringId); + EXPECT_EQ(account_infos.size(), 1u); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "Solana Account 1"); +} + +TEST_F(KeyringServiceUnitTest, DefaultSolanaAccountRestored) { + KeyringService service(json_rpc_service(), GetPrefs()); + ASSERT_TRUE(RestoreWallet(&service, kMnemonic1, "brave", false)); + + std::vector account_infos = + service.GetAccountInfosForKeyring(mojom::kSolanaKeyringId); + EXPECT_EQ(account_infos.size(), 1u); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "Solana Account 1"); +} + TEST_F(KeyringServiceUnitTest, AddAccount) { KeyringService service(json_rpc_service(), GetPrefs()); ASSERT_NE(CreateWallet(&service, "brave"), absl::nullopt); @@ -1825,8 +1858,15 @@ TEST_F(KeyringServiceUnitTest, ImportedAccountFromJson) { TEST_F(KeyringServiceUnitTest, GetPrivateKeyForKeyringAccount) { base::test::ScopedFeatureList feature_list; - feature_list.InitAndEnableFeature( - brave_wallet::features::kBraveWalletSolanaFeature); + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + KeyringService service(json_rpc_service(), GetPrefs()); ASSERT_TRUE(RestoreWallet(&service, kMnemonic1, "brave", false)); @@ -2398,6 +2438,16 @@ TEST_F(KeyringServiceUnitTest, NotifyUserInteraction) { } TEST_F(KeyringServiceUnitTest, SelectAddedAccount) { + base::test::ScopedFeatureList feature_list; + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + KeyringService service(json_rpc_service(), GetPrefs()); ASSERT_TRUE(CreateWallet(&service, "brave")); @@ -3358,6 +3408,16 @@ TEST_F(KeyringServiceUnitTest, ImportFilecoinAccounts) { } TEST_F(KeyringServiceUnitTest, PreCreateEncryptors) { + base::test::ScopedFeatureList feature_list; + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + { // Create default wallet with disabled filecoin feature. // Solana feature is enabled on desktop and disabled on Android. @@ -3429,10 +3489,19 @@ TEST_F(KeyringServiceUnitTest, PreCreateEncryptors) { #endif base::test::ScopedFeatureList feature_list; - feature_list.InitWithFeatures( - {brave_wallet::features::kBraveWalletFilecoinFeature, - brave_wallet::features::kBraveWalletSolanaFeature}, - {}); + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector + enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletFilecoinFeature, + base::FieldTrialParams()); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + service.Reset(); ASSERT_TRUE( RestoreWallet(&service, *mnemonic_to_be_restored, "brave", false)); @@ -3453,8 +3522,15 @@ TEST_F(KeyringServiceUnitTest, PreCreateEncryptors) { TEST_F(KeyringServiceUnitTest, SolanaKeyring) { base::test::ScopedFeatureList feature_list; - feature_list.InitAndEnableFeature( - brave_wallet::features::kBraveWalletSolanaFeature); + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + { KeyringService service(json_rpc_service(), GetPrefs()); TestKeyringServiceObserver observer; @@ -3622,8 +3698,15 @@ TEST_F(KeyringServiceUnitTest, SolanaKeyring) { TEST_F(KeyringServiceUnitTest, SignMessage) { base::test::ScopedFeatureList feature_list; - feature_list.InitAndEnableFeature( - brave_wallet::features::kBraveWalletSolanaFeature); + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list.InitWithFeaturesAndParameters(enabled_features, {}); + KeyringService service(json_rpc_service(), GetPrefs()); ASSERT_TRUE(RestoreWallet(&service, kMnemonic1, "brave", false)); base::RunLoop().RunUntilIdle(); diff --git a/browser/brave_wallet/solana_provider_browsertest.cc b/browser/brave_wallet/solana_provider_browsertest.cc index 9693f0b1f4fa..26dcdca5f0c3 100644 --- a/browser/brave_wallet/solana_provider_browsertest.cc +++ b/browser/brave_wallet/solana_provider_browsertest.cc @@ -215,10 +215,17 @@ class SolanaProviderTest : public InProcessBrowserTest { SolanaProviderTest() : https_server_for_files_(net::EmbeddedTestServer::TYPE_HTTPS), https_server_for_rpc_(net::EmbeddedTestServer::TYPE_HTTPS) { - feature_list_.InitWithFeatures( - {brave_wallet::features::kBraveWalletSolanaFeature, - brave_wallet::features::kBraveWalletSolanaProviderFeature}, - {}); + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + + std::vector + enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaProviderFeature, + base::FieldTrialParams()); + feature_list_.InitWithFeaturesAndParameters(enabled_features, {}); } ~SolanaProviderTest() override = default; diff --git a/components/brave_wallet/browser/brave_wallet_utils.cc b/components/brave_wallet/browser/brave_wallet_utils.cc index 82d9c3a36c68..25c02bb60b44 100644 --- a/components/brave_wallet/browser/brave_wallet_utils.cc +++ b/components/brave_wallet/browser/brave_wallet_utils.cc @@ -17,6 +17,7 @@ #include "base/environment.h" #include "base/feature_list.h" #include "base/logging.h" +#include "base/metrics/field_trial_params.h" #include "base/no_destructor.h" #include "base/notreached.h" #include "base/strings/strcat.h" @@ -699,6 +700,11 @@ bool IsSolanaEnabled() { brave_wallet::features::kBraveWalletSolanaFeature); } +bool ShouldCreateDefaultSolanaAccount() { + return IsSolanaEnabled() && + brave_wallet::features::kCreateDefaultSolanaAccount.Get(); +} + std::vector GetAllKnownNetworksForTesting() { std::vector result; diff --git a/components/brave_wallet/browser/brave_wallet_utils.h b/components/brave_wallet/browser/brave_wallet_utils.h index 2a9704bfbd2e..db924ae347cc 100644 --- a/components/brave_wallet/browser/brave_wallet_utils.h +++ b/components/brave_wallet/browser/brave_wallet_utils.h @@ -27,6 +27,7 @@ namespace brave_wallet { bool IsNativeWalletEnabled(); bool IsFilecoinEnabled(); bool IsSolanaEnabled(); +bool ShouldCreateDefaultSolanaAccount(); bool IsDappsSupportEnabled(); // Generate mnemonic from random entropy following BIP39. diff --git a/components/brave_wallet/browser/eth_tx_manager_unittest.cc b/components/brave_wallet/browser/eth_tx_manager_unittest.cc index 43d81aec7e93..725f8de54f81 100644 --- a/components/brave_wallet/browser/eth_tx_manager_unittest.cc +++ b/components/brave_wallet/browser/eth_tx_manager_unittest.cc @@ -14,9 +14,11 @@ #include "base/callback_helpers.h" #include "base/json/json_reader.h" #include "base/test/bind.h" +#include "base/test/scoped_feature_list.h" #include "base/test/task_environment.h" #include "brave/components/brave_wallet/browser/brave_wallet_constants.h" #include "brave/components/brave_wallet/browser/brave_wallet_prefs.h" +#include "brave/components/brave_wallet/browser/brave_wallet_utils.h" #include "brave/components/brave_wallet/browser/eip1559_transaction.h" #include "brave/components/brave_wallet/browser/eth_data_parser.h" #include "brave/components/brave_wallet/browser/eth_nonce_tracker.h" @@ -29,6 +31,7 @@ #include "brave/components/brave_wallet/browser/pref_names.h" #include "brave/components/brave_wallet/browser/tx_service.h" #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" +#include "brave/components/brave_wallet/common/features.h" #include "brave/components/brave_wallet/common/hex_utils.h" #include "components/sync_preferences/testing_pref_service_syncable.h" #include "mojo/public/cpp/bindings/pending_remote.h" @@ -176,6 +179,15 @@ class EthTxManagerUnitTest : public testing::Test { &url_loader_factory_)) {} void SetUp() override { + base::FieldTrialParams parameters; + parameters[features::kCreateDefaultSolanaAccount.name] = "false"; + std::vector + enabled_features; + enabled_features.emplace_back( + brave_wallet::features::kBraveWalletSolanaFeature, parameters); + + feature_list_.InitWithFeaturesAndParameters(enabled_features, {}); + url_loader_factory_.SetInterceptor(base::BindLambdaForTesting( [&](const network::ResourceRequest& request) { url_loader_factory_.ClearResponses(); @@ -449,6 +461,7 @@ class EthTxManagerUnitTest : public testing::Test { } protected: + base::test::ScopedFeatureList feature_list_; base::test::TaskEnvironment task_environment_; sync_preferences::TestingPrefServiceSyncable prefs_; network::TestURLLoaderFactory url_loader_factory_; diff --git a/components/brave_wallet/browser/keyring_service.cc b/components/brave_wallet/browser/keyring_service.cc index 0b16c993ec74..6667924514e9 100644 --- a/components/brave_wallet/browser/keyring_service.cc +++ b/components/brave_wallet/browser/keyring_service.cc @@ -688,6 +688,23 @@ void KeyringService::GetMnemonicForDefaultKeyring( std::move(callback).Run(GetMnemonicForKeyringImpl(mojom::kDefaultKeyringId)); } +void KeyringService::MaybeCreateDefaultSolanaAccount() { + if (ShouldCreateDefaultSolanaAccount() && + LazilyCreateKeyring(mojom::kSolanaKeyringId)) { + auto address = AddAccountForKeyring(mojom::kSolanaKeyringId, + "Solana " + GetAccountName(1)); + if (address) { + SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), + mojom::kSolanaKeyringId); + SetSelectedCoin(prefs_, mojom::CoinType::SOL); + // This is needed for Android to select default coin, because they listen + // to network change events. + json_rpc_service_->SetNetwork(brave_wallet::mojom::kSolanaMainnet, + mojom::CoinType::SOL, false); + } + } +} + void KeyringService::CreateWallet(const std::string& password, CreateWalletCallback callback) { prefs_->SetBoolean(kBraveWalletKeyringEncryptionKeysMigrated, true); @@ -717,6 +734,7 @@ void KeyringService::CreateWallet(const std::string& password, if (!CreateEncryptorForKeyring(password, mojom::kSolanaKeyringId)) { VLOG(1) << "Unable to create solana encryptor"; } + MaybeCreateDefaultSolanaAccount(); } std::move(callback).Run(GetMnemonicForKeyringImpl(mojom::kDefaultKeyringId)); @@ -775,6 +793,8 @@ void KeyringService::RestoreWallet(const std::string& mnemonic, SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address), mojom::kSolanaKeyringId); } + } else { + MaybeCreateDefaultSolanaAccount(); } } diff --git a/components/brave_wallet/browser/keyring_service.h b/components/brave_wallet/browser/keyring_service.h index 143498c203f6..4c83795f24ea 100644 --- a/components/brave_wallet/browser/keyring_service.h +++ b/components/brave_wallet/browser/keyring_service.h @@ -126,6 +126,7 @@ class KeyringService : public KeyedService, public mojom::KeyringService { // Must unlock before using this API otherwise it will return empty string void GetMnemonicForDefaultKeyring( GetMnemonicForDefaultKeyringCallback callback) override; + void MaybeCreateDefaultSolanaAccount(); void CreateWallet(const std::string& password, CreateWalletCallback callback) override; void RestoreWallet(const std::string& mnemonic, @@ -288,6 +289,9 @@ class KeyringService : public KeyedService, public mojom::KeyringService { FRIEND_TEST_ALL_PREFIXES(KeyringServiceUnitTest, ImportFilecoinAccounts); FRIEND_TEST_ALL_PREFIXES(KeyringServiceUnitTest, PreCreateEncryptors); FRIEND_TEST_ALL_PREFIXES(KeyringServiceUnitTest, HardwareAccounts); + FRIEND_TEST_ALL_PREFIXES(KeyringServiceUnitTest, DefaultSolanaAccountCreated); + FRIEND_TEST_ALL_PREFIXES(KeyringServiceUnitTest, + DefaultSolanaAccountRestored); FRIEND_TEST_ALL_PREFIXES(KeyringServiceAccountDiscoveryUnitTest, AccountDiscovery); diff --git a/components/brave_wallet/common/features.cc b/components/brave_wallet/common/features.cc index c8aca52b81db..039553c86225 100644 --- a/components/brave_wallet/common/features.cc +++ b/components/brave_wallet/common/features.cc @@ -23,6 +23,8 @@ const base::Feature kBraveWalletFilecoinFeature{ const base::Feature kBraveWalletSolanaFeature{"BraveWalletSolana", base::FEATURE_ENABLED_BY_DEFAULT}; +const base::FeatureParam kCreateDefaultSolanaAccount{ + &kBraveWalletSolanaFeature, "create_default_solana_account", true}; const base::Feature kBraveWalletSolanaProviderFeature{ #if BUILDFLAG(IS_ANDROID) diff --git a/components/brave_wallet/common/features.h b/components/brave_wallet/common/features.h index bc31735a54cf..55c54163fbbf 100644 --- a/components/brave_wallet/common/features.h +++ b/components/brave_wallet/common/features.h @@ -6,9 +6,8 @@ #ifndef BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_FEATURES_H_ #define BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_FEATURES_H_ -namespace base { -struct Feature; -} // namespace base +#include "base/feature_list.h" +#include "base/metrics/field_trial_params.h" namespace brave_wallet { namespace features { @@ -16,6 +15,7 @@ namespace features { extern const base::Feature kNativeBraveWalletFeature; extern const base::Feature kBraveWalletFilecoinFeature; extern const base::Feature kBraveWalletSolanaFeature; +extern const base::FeatureParam kCreateDefaultSolanaAccount; extern const base::Feature kBraveWalletSolanaProviderFeature; extern const base::Feature kBraveWalletDappsSupportFeature;