From 92914a85329df467789b7af6f894edba1fbd7bf8 Mon Sep 17 00:00:00 2001 From: Adegoke David <64401859+Blazebrain@users.noreply.github.com> Date: Thu, 28 Dec 2023 02:37:36 +0100 Subject: [PATCH] fix: Fix walletconnect connecting only with the first wallet (#1247) --- .../wallet_connect/evm_chain_service.dart | 8 +-- .../wallet_connect_key_service.dart | 72 ++++++------------- .../wallet_connect/web3wallet_service.dart | 5 +- lib/di.dart | 2 +- lib/entities/load_current_wallet.dart | 2 +- lib/store/app_store.dart | 13 ++-- lib/view_model/wallet_creation_vm.dart | 2 +- .../wallet_list/wallet_list_view_model.dart | 2 +- 8 files changed, 40 insertions(+), 66 deletions(-) diff --git a/lib/core/wallet_connect/evm_chain_service.dart b/lib/core/wallet_connect/evm_chain_service.dart index 836d1a7c8..81b934e08 100644 --- a/lib/core/wallet_connect/evm_chain_service.dart +++ b/lib/core/wallet_connect/evm_chain_service.dart @@ -138,7 +138,7 @@ class EvmChainServiceImpl implements ChainService { try { // Load the private key final List keys = wcKeyService - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); + .getKeysForChain(appStore.wallet!); final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey); @@ -177,7 +177,7 @@ class EvmChainServiceImpl implements ChainService { try { // Load the private key final List keys = wcKeyService - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); + .getKeysForChain(appStore.wallet!); final EthPrivateKey credentials = EthPrivateKey.fromHex(keys[0].privateKey); @@ -215,7 +215,7 @@ class EvmChainServiceImpl implements ChainService { // Load the private key final List keys = wcKeyService - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); + .getKeysForChain(appStore.wallet!); final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey); @@ -275,7 +275,7 @@ class EvmChainServiceImpl implements ChainService { } final List keys = wcKeyService - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); + .getKeysForChain(appStore.wallet!); return EthSigUtil.signTypedData( privateKey: keys[0].privateKey, diff --git a/lib/core/wallet_connect/wallet_connect_key_service.dart b/lib/core/wallet_connect/wallet_connect_key_service.dart index bb0c9d14c..33d721073 100644 --- a/lib/core/wallet_connect/wallet_connect_key_service.dart +++ b/lib/core/wallet_connect/wallet_connect_key_service.dart @@ -1,48 +1,22 @@ import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/core/wallet_connect/models/chain_key_model.dart'; import 'package:cake_wallet/polygon/polygon.dart'; -import 'package:cw_core/balance.dart'; -import 'package:cw_core/transaction_history.dart'; -import 'package:cw_core/transaction_info.dart'; +import 'package:cake_wallet/reactions/wallet_connect.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_type.dart'; abstract class WalletConnectKeyService { /// Returns a list of all the keys. - List getKeys(); - - /// Returns a list of all the chain ids. - List getChains(); + List getKeys(WalletBase wallet); /// Returns a list of all the keys for a given chain id. /// If the chain is not found, returns an empty list. /// - [chain]: The chain to get the keys for. - List getKeysForChain(String chain); + List getKeysForChain(WalletBase wallet); - /// Returns a list of all the accounts in namespace:chainId:address format. - List getAllAccounts(); } class KeyServiceImpl implements WalletConnectKeyService { - KeyServiceImpl(this.wallet) - : _keys = [ - ChainKeyModel( - chains: [ - 'eip155:1', - 'eip155:5', - 'eip155:137', - 'eip155:42161', - 'eip155:80001', - ], - privateKey: _getPrivateKeyForWallet(wallet), - publicKey: _getPublicKeyForWallet(wallet), - ), - ]; - - late final WalletBase, TransactionInfo> wallet; - - late final List _keys; - static String _getPrivateKeyForWallet(WalletBase wallet) { switch (wallet.type) { case WalletType.ethereum: @@ -64,31 +38,31 @@ class KeyServiceImpl implements WalletConnectKeyService { return ''; } } + @override - List getChains() { - final List chainIds = []; - for (final ChainKeyModel key in _keys) { - chainIds.addAll(key.chains); - } - return chainIds; + List getKeys(WalletBase wallet) { + final keys = [ + ChainKeyModel( + chains: [ + 'eip155:1', + 'eip155:5', + 'eip155:137', + 'eip155:42161', + 'eip155:80001', + ], + privateKey: _getPrivateKeyForWallet(wallet), + publicKey: _getPublicKeyForWallet(wallet), + ), + ]; + return keys; } @override - List getKeys() => _keys; + List getKeysForChain(WalletBase wallet) { + final chain = getChainNameSpaceAndIdBasedOnWalletType(wallet.type); - @override - List getKeysForChain(String chain) { - return _keys.where((e) => e.chains.contains(chain)).toList(); - } + final keys = getKeys(wallet); - @override - List getAllAccounts() { - final List accounts = []; - for (final ChainKeyModel key in _keys) { - for (final String chain in key.chains) { - accounts.add('$chain:${key.publicKey}'); - } - } - return accounts; + return keys.where((e) => e.chains.contains(chain)).toList(); } } diff --git a/lib/core/wallet_connect/web3wallet_service.dart b/lib/core/wallet_connect/web3wallet_service.dart index 7a1dac6cc..ee560a0e0 100644 --- a/lib/core/wallet_connect/web3wallet_service.dart +++ b/lib/core/wallet_connect/web3wallet_service.dart @@ -68,7 +68,7 @@ abstract class Web3WalletServiceBase with Store { ); // Setup our accounts - List chainKeys = walletKeyService.getKeys(); + List chainKeys = walletKeyService.getKeys(appStore.wallet!); for (final chainKey in chainKeys) { for (final chainId in chainKey.chains) { _web3Wallet.registerAccount( @@ -136,6 +136,7 @@ abstract class Web3WalletServiceBase with Store { _web3Wallet.onAuthRequest.unsubscribe(_onAuthRequest); _web3Wallet.core.pairing.onPairingDelete.unsubscribe(_onPairingDelete); _web3Wallet.core.pairing.onPairingExpire.unsubscribe(_onPairingDelete); + isInitialized = false; } Web3Wallet getWeb3Wallet() { @@ -236,7 +237,7 @@ abstract class Web3WalletServiceBase with Store { Future _onAuthRequest(AuthRequest? args) async { if (args != null) { final chaindIdNamespace = getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type); - List chainKeys = walletKeyService.getKeysForChain(chaindIdNamespace); + List chainKeys = walletKeyService.getKeysForChain(appStore.wallet!); // Create the message to be signed final String iss = 'did:pkh:$chaindIdNamespace:${chainKeys.first.publicKey}'; final Widget modalWidget = Web3RequestModal( diff --git a/lib/di.dart b/lib/di.dart index c98b98bc8..03b7bfec8 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -474,7 +474,7 @@ Future setup({ final appStore = getIt.get(); - getIt.registerLazySingleton(() => KeyServiceImpl(appStore.wallet!)); + getIt.registerLazySingleton(() => KeyServiceImpl()); getIt.registerLazySingleton(() { final Web3WalletService web3WalletService = Web3WalletService( diff --git a/lib/entities/load_current_wallet.dart b/lib/entities/load_current_wallet.dart index d758b6697..595bc2233 100644 --- a/lib/entities/load_current_wallet.dart +++ b/lib/entities/load_current_wallet.dart @@ -22,7 +22,7 @@ Future loadCurrentWallet() async { final type = deserializeFromInt(typeRaw); final walletLoadingService = getIt.get(); final wallet = await walletLoadingService.load(type, name); - appStore.changeCurrentWallet(wallet); + await appStore.changeCurrentWallet(wallet); getIt.get().registerSyncTask(); } diff --git a/lib/store/app_store.dart b/lib/store/app_store.dart index 46ac2cf1a..a5a2b95e0 100644 --- a/lib/store/app_store.dart +++ b/lib/store/app_store.dart @@ -26,8 +26,7 @@ abstract class AppStoreBase with Store { AuthenticationStore authenticationStore; @observable - WalletBase, TransactionInfo>? - wallet; + WalletBase, TransactionInfo>? wallet; WalletListStore walletList; @@ -36,16 +35,16 @@ abstract class AppStoreBase with Store { NodeListStore nodeListStore; @action - void changeCurrentWallet( - WalletBase, - TransactionInfo> - wallet) { + Future changeCurrentWallet( + WalletBase, TransactionInfo> wallet) async { this.wallet?.close(); this.wallet = wallet; this.wallet!.setExceptionHandler(ExceptionHandler.onError); if (isEVMCompatibleChain(wallet.type)) { - getIt.get().init(); + await getIt.get().onDispose(); + getIt.get().create(); + await getIt.get().init(); } } } diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index aa1efe4d3..45306905c 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -74,7 +74,7 @@ abstract class WalletCreationVMBase with Store { : await process(credentials); walletInfo.address = wallet.walletAddresses.address; await _walletInfoSource.add(walletInfo); - _appStore.changeCurrentWallet(wallet); + await _appStore.changeCurrentWallet(wallet); getIt.get().registerSyncTask(); _appStore.authenticationStore.allowed(); state = ExecutedSuccessfullyState(); diff --git a/lib/view_model/wallet_list/wallet_list_view_model.dart b/lib/view_model/wallet_list/wallet_list_view_model.dart index 3df6491bb..78211bb3e 100644 --- a/lib/view_model/wallet_list/wallet_list_view_model.dart +++ b/lib/view_model/wallet_list/wallet_list_view_model.dart @@ -45,7 +45,7 @@ abstract class WalletListViewModelBase with Store { @action Future loadWallet(WalletListItem walletItem) async { final wallet = await _walletLoadingService.load(walletItem.type, walletItem.name); - _appStore.changeCurrentWallet(wallet); + await _appStore.changeCurrentWallet(wallet); } WalletListOrderType? get orderType => _appStore.settingsStore.walletListOrder;