Skip to content

Commit

Permalink
Merge pull request #13359 from brave/open-account-in-dapp-panel
Browse files Browse the repository at this point in the history
feat(wallet): implement account selector
  • Loading branch information
Pavneet Singh authored May 19, 2022
2 parents cf2e51d + 6e727df commit 151ab22
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 41 deletions.
2 changes: 2 additions & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ brave_java_resources = [
"java/res/layout/activity_account_detail.xml",
"java/res/layout/activity_account_details_with_qr.xml",
"java/res/layout/activity_account_private_key.xml",
"java/res/layout/activity_account_selector.xml",
"java/res/layout/activity_add_account.xml",
"java/res/layout/activity_advance_tx_setting.xml",
"java/res/layout/activity_asset_detail.xml",
Expand Down Expand Up @@ -954,6 +955,7 @@ brave_java_resources = [
"java/res/layout/wallet_coin_list_item.xml",
"java/res/layout/web_notification_icon_frame_brave.xml",
"java/res/menu/exit_settings_menu.xml",
"java/res/menu/menu_account_selector.xml",
"java/res/menu/menu_brave_vpn.xml",
"java/res/menu/menu_dapps_panel.xml",
"java/res/menu/monthly_contribution_popup_menu.xml",
Expand Down
3 changes: 3 additions & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/app/appmenu/AppMenuIconRowFooter.java",
"../../brave/android/java/org/chromium/chrome/browser/app/appmenu/BraveAppMenuPropertiesDelegateImpl.java",
"../../brave/android/java/org/chromium/chrome/browser/app/domain/CryptoModel.java",
"../../brave/android/java/org/chromium/chrome/browser/app/domain/CryptoSharedData.java",
"../../brave/android/java/org/chromium/chrome/browser/app/domain/KeyringModel.java",
"../../brave/android/java/org/chromium/chrome/browser/app/domain/NetworkModel.java",
"../../brave/android/java/org/chromium/chrome/browser/app/domain/WalletModel.java",
"../../brave/android/java/org/chromium/chrome/browser/appmenu/BraveTabbedAppMenuPropertiesDelegate.java",
"../../brave/android/java/org/chromium/chrome/browser/autofill/BraveAutofillPopupBridge.java",
Expand All @@ -70,6 +72,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountDetailActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountDetailsWithQrActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountPrivateKeyActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AccountSelectorActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AddAccountActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AdvanceTxSettingActivity.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java",
Expand Down
5 changes: 5 additions & 0 deletions android/java/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
android:screenOrientation="sensorPortrait"
tools:ignore="LockedOrientationActivity"/>

<activity android:name="org.chromium.chrome.browser.crypto_wallet.activities.AccountSelectorActivity"
android:theme="@style/Theme.Chromium.Activity"
android:screenOrientation="sensorPortrait"
tools:ignore="LockedOrientationActivity"/>

<activity android:name="org.chromium.chrome.browser.crypto_wallet.activities.AdvanceTxSettingActivity"
android:theme="@style/Theme.Chromium.Activity"
android:screenOrientation="sensorPortrait"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
package org.chromium.chrome.browser.app.domain;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;

import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.AssetRatioService;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.EthTxManagerProxy;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.SolanaTxManagerProxy;
import org.chromium.brave_wallet.mojom.TransactionInfo;
import org.chromium.brave_wallet.mojom.TransactionStatus;
import org.chromium.brave_wallet.mojom.TxService;
Expand All @@ -24,28 +32,61 @@ public class CryptoModel {
private PendingTxHelper mPendingTxHelper;
private KeyringService mKeyringService;
private LiveData<List<TransactionInfo>> mPendingTransactions;
// Todo: create a models for network and portfolio
private BlockchainRegistry mBlockchainRegistry;
private JsonRpcService mJsonRpcService;
private EthTxManagerProxy mEthTxManagerProxy;
private SolanaTxManagerProxy mSolanaTxManagerProxy;
private BraveWalletService mBraveWalletService;
private AssetRatioService mAssetRatioService;
private CryptoSharedData mSharedData;
private final MutableLiveData<Integer> _mCoinTypeMutableLiveData =
new MutableLiveData<>(CoinType.ETH);
public final LiveData<Integer> mCoinTypeMutableLiveData = _mCoinTypeMutableLiveData;

private NetworkModel mNetworkModel;
// Todo: create a models for portfolio
// Todo: create method to create and return new models for Asset, Account,
// TransactionConfirmation

public CryptoModel(TxService txService, KeyringService keyringService) {
this.mTxService = txService;
this.mKeyringService = keyringService;
public CryptoModel(TxService mTxService, KeyringService mKeyringService,
BlockchainRegistry mBlockchainRegistry, JsonRpcService mJsonRpcService,
EthTxManagerProxy mEthTxManagerProxy, SolanaTxManagerProxy mSolanaTxManagerProxy,
BraveWalletService mBraveWalletService, AssetRatioService mAssetRatioService) {
this.mTxService = mTxService;
this.mKeyringService = mKeyringService;
this.mBlockchainRegistry = mBlockchainRegistry;
this.mJsonRpcService = mJsonRpcService;
this.mEthTxManagerProxy = mEthTxManagerProxy;
this.mSolanaTxManagerProxy = mSolanaTxManagerProxy;
this.mBraveWalletService = mBraveWalletService;
this.mAssetRatioService = mAssetRatioService;
mSharedData = new CryptoSharedDataImpl();
mPendingTxHelper = new PendingTxHelper(mTxService, new AccountInfo[0], true, null, true);
init();
mNetworkModel = new NetworkModel(mJsonRpcService, mSharedData);
}

public void resetServices(TxService txService, KeyringService keyringService) {
mTxService = txService;
mKeyringService = keyringService;
mPendingTxHelper.setTxService(txService);
public void resetServices(TxService mTxService, KeyringService mKeyringService,
BlockchainRegistry mBlockchainRegistry, JsonRpcService mJsonRpcService,
EthTxManagerProxy mEthTxManagerProxy, SolanaTxManagerProxy mSolanaTxManagerProxy,
BraveWalletService mBraveWalletService, AssetRatioService mAssetRatioService) {
this.mTxService = mTxService;
this.mKeyringService = mKeyringService;
this.mBlockchainRegistry = mBlockchainRegistry;
this.mJsonRpcService = mJsonRpcService;
this.mEthTxManagerProxy = mEthTxManagerProxy;
this.mSolanaTxManagerProxy = mSolanaTxManagerProxy;
this.mBraveWalletService = mBraveWalletService;
this.mAssetRatioService = mAssetRatioService;
mPendingTxHelper.setTxService(mTxService);
init();
}

private void init() {
public void init() {
getPendingTxHelper().fetchTransactions(null);
mKeyringService.getKeyringInfo(BraveWalletConstants.DEFAULT_KEYRING_ID,
keyringInfo -> { mPendingTxHelper.setAccountInfos(keyringInfo.accountInfos); });

// filter out a separate list of unapproved transactions
mPendingTransactions =
Transformations.map(mPendingTxHelper.mTransactionInfoLd, transactionInfos -> {
List<TransactionInfo> pendingTransactionInfo = new ArrayList<>();
Expand All @@ -56,6 +97,7 @@ private void init() {
}
return pendingTransactionInfo;
});
mNetworkModel.init();
}

public void refreshTransactions() {
Expand All @@ -77,4 +119,30 @@ public LiveData<List<TransactionInfo>> getAllTransactions() {
public PendingTxHelper getPendingTxHelper() {
return mPendingTxHelper;
}

public CryptoSharedData getSharedData() {
return mSharedData;
}

/*
* A container class to share the required data throughout the domain model classes.
* Note: It should only be used/accessed within the domain package
*/
class CryptoSharedDataImpl implements CryptoSharedData {
@Override
public int getCoinType() {
if (mCoinTypeMutableLiveData.getValue() == null) {
return CoinType.ETH;
}
return mCoinTypeMutableLiveData.getValue();
}

@Override
public String getChainId() {
if (mNetworkModel.mChainId == null) {
return BraveWalletConstants.MAINNET_CHAIN_ID;
}
return mNetworkModel.mChainId.getValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.chromium.chrome.browser.app.domain;

public interface CryptoSharedData {
int getCoinType();
String getChainId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.KeyringInfo;
import org.chromium.brave_wallet.mojom.KeyringService;
Expand All @@ -18,29 +19,55 @@ public class KeyringModel implements KeyringServiceObserver {
private KeyringService mKeyringService;
private MutableLiveData<KeyringInfo> _mKeyringInfoLiveData;
public LiveData<KeyringInfo> mKeyringInfoLiveData;
private final MutableLiveData<AccountInfo> _mSelectedAccount;
public LiveData<AccountInfo> mSelectedAccount;
private CryptoSharedData mSharedData;
// Todo: create method to interact with keyring

public KeyringModel(KeyringService mKeyringService) {
public KeyringModel(KeyringService mKeyringService, CryptoSharedData sharedData) {
this.mKeyringService = mKeyringService;
mSharedData = sharedData;
_mKeyringInfoLiveData = new MutableLiveData<>(null);
mKeyringInfoLiveData = _mKeyringInfoLiveData;
init();
_mSelectedAccount = new MutableLiveData<>();
mSelectedAccount = _mSelectedAccount;
}

private void init() {
public void init() {
mKeyringService.addObserver(this);
}

private void update() {
mKeyringService.getKeyringInfo(BraveWalletConstants.DEFAULT_KEYRING_ID,
keyringInfo -> { _mKeyringInfoLiveData.postValue(keyringInfo); });
mKeyringService.getKeyringInfo(BraveWalletConstants.DEFAULT_KEYRING_ID, keyringInfo -> {
_mKeyringInfoLiveData.postValue(keyringInfo);
Integer coinType = mSharedData.getCoinType();
mKeyringService.getSelectedAccount(coinType, accountAddress -> {
if (keyringInfo.accountInfos.length > 0) {
AccountInfo selectedAccount = keyringInfo.accountInfos[0];
for (AccountInfo accountInfo : keyringInfo.accountInfos) {
if (accountInfo.address.equals(accountAddress)) {
selectedAccount = accountInfo;
break;
}
}
_mSelectedAccount.postValue(selectedAccount);
}
});
});
}

public void setSelectedAccount(String accountAddress, int coin) {
mKeyringService.setSelectedAccount(accountAddress, coin, isAccountSelected -> {});
}

public KeyringInfo getKeyringInfo() {
return _mKeyringInfoLiveData.getValue();
}

public void setKeyringService(KeyringService keyringService) {
public void resetService(KeyringService keyringService) {
if (mKeyringService == keyringService) {
return;
}
this.mKeyringService = keyringService;
init();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.chromium.chrome.browser.app.domain;

import android.text.TextUtils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.JsonRpcServiceObserver;
import org.chromium.mojo.system.MojoException;

public class NetworkModel implements JsonRpcServiceObserver {
private final JsonRpcService mJsonRpcService;
private final MutableLiveData<String> _mChainId;
public LiveData<String> mChainId;
private final CryptoSharedData mSharedData;

public NetworkModel(JsonRpcService jsonRpcService, CryptoSharedData sharedData) {
mJsonRpcService = jsonRpcService;
mSharedData = sharedData;
_mChainId = new MutableLiveData<>(BraveWalletConstants.MAINNET_CHAIN_ID);
mChainId = _mChainId;
jsonRpcService.addObserver(this);
}

public void init() {
mJsonRpcService.getChainId(mSharedData.getCoinType(), chainId -> {
String id = BraveWalletConstants.MAINNET_CHAIN_ID;
if (TextUtils.isEmpty(chainId)) {
mJsonRpcService.setNetwork(id, mSharedData.getCoinType(), hasSetNetwork -> {});
} else {
id = chainId;
}
_mChainId.postValue(id);
});
}

@Override
public void chainChangedEvent(String chainId, int coin) {
_mChainId.postValue(chainId);
}

@Override
public void onAddEthereumChainRequestCompleted(String chainId, String error) {}

@Override
public void onIsEip1559Changed(String chainId, boolean isEip1559) {}

@Override
public void onConnectionError(MojoException e) {}

@Override
public void close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.SolanaTxManagerProxy;
import org.chromium.brave_wallet.mojom.TxService;
import org.chromium.chrome.browser.app.domain.CryptoModel;

// Under development, some parts not tested so use with caution
// A container for all the native services and APIs
Expand Down Expand Up @@ -41,8 +40,11 @@ public WalletModel(KeyringService keyringService, BlockchainRegistry blockchainR
mSolanaTxManagerProxy = solanaTxManagerProxy;
mAssetRatioService = assetRatioService;
mBraveWalletService = braveWalletService;
mCryptoModel = new CryptoModel(mTxService, mKeyringService);
mKeyringModel = new KeyringModel(keyringService);
mCryptoModel = new CryptoModel(mTxService, mKeyringService, mBlockchainRegistry,
mJsonRpcService, mEthTxManagerProxy, mSolanaTxManagerProxy, mBraveWalletService,
mAssetRatioService);
mKeyringModel = new KeyringModel(keyringService, mCryptoModel.getSharedData());
init();
}

public void resetServices(KeyringService keyringService, BlockchainRegistry blockchainRegistry,
Expand All @@ -57,8 +59,19 @@ public void resetServices(KeyringService keyringService, BlockchainRegistry bloc
setSolanaTxManagerProxy(solanaTxManagerProxy);
setAssetRatioService(assetRatioService);
setBraveWalletService(braveWalletService);
mCryptoModel.resetServices(mTxService, mKeyringService);
mKeyringModel.setKeyringService(mKeyringService);
mCryptoModel.resetServices(mTxService, mKeyringService, mBlockchainRegistry,
mJsonRpcService, mEthTxManagerProxy, mSolanaTxManagerProxy, mBraveWalletService,
mAssetRatioService);
mKeyringModel.resetService(mKeyringService);
init();
}

/*
* Explicit method to ensure the sage initialisation to start the required data process
*/
private void init() {
mCryptoModel.init();
mKeyringModel.init();
}

public KeyringModel getKeyringModel() {
Expand Down
Loading

0 comments on commit 151ab22

Please sign in to comment.