Skip to content

Commit

Permalink
Create default solana acc when create\restore the wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Sep 19, 2022
1 parent f175d8e commit 3a250f1
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 14 deletions.
12 changes: 12 additions & 0 deletions browser/brave_wallet/brave_wallet_p3a_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<base::test::ScopedFeatureList::FeatureAndParams> 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();
Expand Down
113 changes: 103 additions & 10 deletions browser/brave_wallet/keyring_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ TEST_F(KeyringServiceUnitTest, LockAndUnlock) {
{brave_wallet::features::kBraveWalletFilecoinFeature,
brave_wallet::features::kBraveWalletSolanaFeature},
{});

{
KeyringService service(json_rpc_service(), GetPrefs());
// No encryptor
Expand Down Expand Up @@ -1472,6 +1473,16 @@ TEST_F(KeyringServiceUnitTest, AccountMetasForKeyring) {
}

TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) {
base::test::ScopedFeatureList feature_list;
base::FieldTrialParams parameters;
parameters[features::kCreateDefaultSolanaAccount.name] = "false";

std::vector<base::test::ScopedFeatureList::FeatureAndParams> enabled_features;
enabled_features.emplace_back(
brave_wallet::features::kBraveWalletSolanaFeature, parameters);

feature_list.InitWithFeaturesAndParameters(enabled_features, {});

KeyringService service(json_rpc_service(), GetPrefs());

TestKeyringServiceObserver observer;
Expand Down Expand Up @@ -1565,6 +1576,38 @@ 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<mojom::AccountInfoPtr> 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) {
base::test::ScopedFeatureList feature_list;
base::FieldTrialParams parameters;
parameters[features::kCreateDefaultSolanaAccount.name] = "true";

std::vector<base::test::ScopedFeatureList::FeatureAndParams> 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));

std::vector<mojom::AccountInfoPtr> 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);
Expand Down Expand Up @@ -1870,8 +1913,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<base::test::ScopedFeatureList::FeatureAndParams> 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));

Expand Down Expand Up @@ -2473,6 +2523,16 @@ TEST_F(KeyringServiceUnitTest, NotifyUserInteraction) {
}

TEST_F(KeyringServiceUnitTest, SelectAddedAccount) {
base::test::ScopedFeatureList feature_list;
base::FieldTrialParams parameters;
parameters[features::kCreateDefaultSolanaAccount.name] = "false";

std::vector<base::test::ScopedFeatureList::FeatureAndParams> 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"));

Expand Down Expand Up @@ -3437,6 +3497,16 @@ TEST_F(KeyringServiceUnitTest, ImportFilecoinAccounts) {
}

TEST_F(KeyringServiceUnitTest, PreCreateEncryptors) {
base::test::ScopedFeatureList feature_list;
base::FieldTrialParams parameters;
parameters[features::kCreateDefaultSolanaAccount.name] = "false";

std::vector<base::test::ScopedFeatureList::FeatureAndParams> 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.
Expand Down Expand Up @@ -3508,10 +3578,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<base::test::ScopedFeatureList::FeatureAndParams>
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));
Expand All @@ -3532,8 +3611,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<base::test::ScopedFeatureList::FeatureAndParams> enabled_features;
enabled_features.emplace_back(
brave_wallet::features::kBraveWalletSolanaFeature, parameters);

feature_list.InitWithFeaturesAndParameters(enabled_features, {});

{
KeyringService service(json_rpc_service(), GetPrefs());
TestKeyringServiceObserver observer;
Expand Down Expand Up @@ -3701,8 +3787,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<base::test::ScopedFeatureList::FeatureAndParams> 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();
Expand Down
15 changes: 11 additions & 4 deletions browser/brave_wallet/solana_provider_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<base::test::ScopedFeatureList::FeatureAndParams>
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;
Expand Down
6 changes: 6 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -722,6 +723,11 @@ bool IsSolanaEnabled() {
brave_wallet::features::kBraveWalletSolanaFeature);
}

bool ShouldCreateDefaultSolanaAccount() {
return IsSolanaEnabled() &&
brave_wallet::features::kCreateDefaultSolanaAccount.Get();
}

std::vector<brave_wallet::mojom::NetworkInfoPtr>
GetAllKnownNetworksForTesting() {
std::vector<brave_wallet::mojom::NetworkInfoPtr> result;
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace brave_wallet {
bool IsNativeWalletEnabled();
bool IsFilecoinEnabled();
bool IsSolanaEnabled();
bool ShouldCreateDefaultSolanaAccount();
bool IsDappsSupportEnabled();

// Generate mnemonic from random entropy following BIP39.
Expand Down
13 changes: 13 additions & 0 deletions components/brave_wallet/browser/eth_tx_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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<base::test::ScopedFeatureList::FeatureAndParams>
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();
Expand Down Expand Up @@ -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_;
Expand Down
20 changes: 20 additions & 0 deletions components/brave_wallet/browser/keyring_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,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);
Expand Down Expand Up @@ -723,6 +740,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));
Expand Down Expand Up @@ -781,6 +799,8 @@ void KeyringService::RestoreWallet(const std::string& mnemonic,
SetPrefForKeyring(prefs_, kSelectedAccount, base::Value(*address),
mojom::kSolanaKeyringId);
}
} else {
MaybeCreateDefaultSolanaAccount();
}
}

Expand Down
4 changes: 4 additions & 0 deletions components/brave_wallet/browser/keyring_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class KeyringService : public KeyedService, public mojom::KeyringService {
void GetMnemonicForDefaultKeyring(
const std::string& password,
GetMnemonicForDefaultKeyringCallback callback) override;
void MaybeCreateDefaultSolanaAccount();
void CreateWallet(const std::string& password,
CreateWalletCallback callback) override;
void RestoreWallet(const std::string& mnemonic,
Expand Down Expand Up @@ -293,6 +294,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);
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const base::Feature kBraveWalletFilecoinFeature{

const base::Feature kBraveWalletSolanaFeature{"BraveWalletSolana",
base::FEATURE_ENABLED_BY_DEFAULT};
const base::FeatureParam<bool> kCreateDefaultSolanaAccount{
&kBraveWalletSolanaFeature, "create_default_solana_account", true};

const base::Feature kBraveWalletSolanaProviderFeature{
#if BUILDFLAG(IS_ANDROID)
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#define BRAVE_COMPONENTS_BRAVE_WALLET_COMMON_FEATURES_H_

#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"

namespace brave_wallet {
namespace features {

extern const base::Feature kNativeBraveWalletFeature;
extern const base::Feature kBraveWalletFilecoinFeature;
extern const base::Feature kBraveWalletSolanaFeature;
extern const base::FeatureParam<bool> kCreateDefaultSolanaAccount;
extern const base::Feature kBraveWalletSolanaProviderFeature;
extern const base::Feature kBraveWalletDappsSupportFeature;
extern const base::Feature kBraveWalletENSL2Feature;
Expand Down

0 comments on commit 3a250f1

Please sign in to comment.