fix: Fix walletconnect connecting only with the first wallet (#1247)

Adegoke David committed Dec 28, 2023 at 02:37 UTC 92914a85329df467789b7af6f894edba1fbd7bf8
8 files changed +40 -66
lib/core/wallet_connect/evm_chain_service.dart
+4 -4
@@ -138,7 +138,7 @@ class EvmChainServiceImpl implements ChainService {
138 try {
139 // Load the private key
140 final List<ChainKeyModel> keys = wcKeyService
141 - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type));
141 + .getKeysForChain(appStore.wallet!);
142
143 final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey);
144
@@ -177,7 +177,7 @@ class EvmChainServiceImpl implements ChainService {
177 try {
178 // Load the private key
179 final List<ChainKeyModel> keys = wcKeyService
180 - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type));
180 + .getKeysForChain(appStore.wallet!);
181
182 final EthPrivateKey credentials = EthPrivateKey.fromHex(keys[0].privateKey);
183
@@ -215,7 +215,7 @@ class EvmChainServiceImpl implements ChainService {
215
216 // Load the private key
217 final List<ChainKeyModel> keys = wcKeyService
218 - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type));
218 + .getKeysForChain(appStore.wallet!);
219
220 final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey);
221
@@ -275,7 +275,7 @@ class EvmChainServiceImpl implements ChainService {
275 }
276
277 final List<ChainKeyModel> keys = wcKeyService
278 - .getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type));
278 + .getKeysForChain(appStore.wallet!);
279
280 return EthSigUtil.signTypedData(
281 privateKey: keys[0].privateKey,
lib/core/wallet_connect/wallet_connect_key_service.dart
+23 -49
@@ -1,48 +1,22 @@
1 import 'package:cake_wallet/ethereum/ethereum.dart';
2 import 'package:cake_wallet/core/wallet_connect/models/chain_key_model.dart';
3 import 'package:cake_wallet/polygon/polygon.dart';
4 -import 'package:cw_core/balance.dart';
5 -import 'package:cw_core/transaction_history.dart';
6 -import 'package:cw_core/transaction_info.dart';
4 +import 'package:cake_wallet/reactions/wallet_connect.dart';
5 import 'package:cw_core/wallet_base.dart';
6 import 'package:cw_core/wallet_type.dart';
7
8 abstract class WalletConnectKeyService {
9 /// Returns a list of all the keys.
12 - List<ChainKeyModel> getKeys();
13 -
14 - /// Returns a list of all the chain ids.
15 - List<String> getChains();
10 + List<ChainKeyModel> getKeys(WalletBase wallet);
11
12 /// Returns a list of all the keys for a given chain id.
13 /// If the chain is not found, returns an empty list.
14 /// - [chain]: The chain to get the keys for.
20 - List<ChainKeyModel> getKeysForChain(String chain);
15 + List<ChainKeyModel> getKeysForChain(WalletBase wallet);
16
22 - /// Returns a list of all the accounts in namespace:chainId:address format.
23 - List<String> getAllAccounts();
17 }
18
19 class KeyServiceImpl implements WalletConnectKeyService {
27 - KeyServiceImpl(this.wallet)
28 - : _keys = [
29 - ChainKeyModel(
30 - chains: [
31 - 'eip155:1',
32 - 'eip155:5',
33 - 'eip155:137',
34 - 'eip155:42161',
35 - 'eip155:80001',
36 - ],
37 - privateKey: _getPrivateKeyForWallet(wallet),
38 - publicKey: _getPublicKeyForWallet(wallet),
39 - ),
40 - ];
41 -
42 - late final WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet;
43 -
44 - late final List<ChainKeyModel> _keys;
45 -
20 static String _getPrivateKeyForWallet(WalletBase wallet) {
21 switch (wallet.type) {
22 case WalletType.ethereum:
@@ -64,31 +38,31 @@ class KeyServiceImpl implements WalletConnectKeyService {
38 return '';
39 }
40 }
41 +
42 @override
68 - List<String> getChains() {
69 - final List<String> chainIds = [];
70 - for (final ChainKeyModel key in _keys) {
71 - chainIds.addAll(key.chains);
72 - }
73 - return chainIds;
43 + List<ChainKeyModel> getKeys(WalletBase wallet) {
44 + final keys = [
45 + ChainKeyModel(
46 + chains: [
47 + 'eip155:1',
48 + 'eip155:5',
49 + 'eip155:137',
50 + 'eip155:42161',
51 + 'eip155:80001',
52 + ],
53 + privateKey: _getPrivateKeyForWallet(wallet),
54 + publicKey: _getPublicKeyForWallet(wallet),
55 + ),
56 + ];
57 + return keys;
58 }
59
60 @override
77 - List<ChainKeyModel> getKeys() => _keys;
61 + List<ChainKeyModel> getKeysForChain(WalletBase wallet) {
62 + final chain = getChainNameSpaceAndIdBasedOnWalletType(wallet.type);
63
79 - @override
80 - List<ChainKeyModel> getKeysForChain(String chain) {
81 - return _keys.where((e) => e.chains.contains(chain)).toList();
82 - }
64 + final keys = getKeys(wallet);
65
84 - @override
85 - List<String> getAllAccounts() {
86 - final List<String> accounts = [];
87 - for (final ChainKeyModel key in _keys) {
88 - for (final String chain in key.chains) {
89 - accounts.add('$chain:${key.publicKey}');
90 - }
91 - }
92 - return accounts;
66 + return keys.where((e) => e.chains.contains(chain)).toList();
67 }
68 }
lib/core/wallet_connect/web3wallet_service.dart
+3 -2
@@ -68,7 +68,7 @@ abstract class Web3WalletServiceBase with Store {
68 );
69
70 // Setup our accounts
71 - List<ChainKeyModel> chainKeys = walletKeyService.getKeys();
71 + List<ChainKeyModel> chainKeys = walletKeyService.getKeys(appStore.wallet!);
72 for (final chainKey in chainKeys) {
73 for (final chainId in chainKey.chains) {
74 _web3Wallet.registerAccount(
@@ -136,6 +136,7 @@ abstract class Web3WalletServiceBase with Store {
136 _web3Wallet.onAuthRequest.unsubscribe(_onAuthRequest);
137 _web3Wallet.core.pairing.onPairingDelete.unsubscribe(_onPairingDelete);
138 _web3Wallet.core.pairing.onPairingExpire.unsubscribe(_onPairingDelete);
139 + isInitialized = false;
140 }
141
142 Web3Wallet getWeb3Wallet() {
@@ -236,7 +237,7 @@ abstract class Web3WalletServiceBase with Store {
237 Future<void> _onAuthRequest(AuthRequest? args) async {
238 if (args != null) {
239 final chaindIdNamespace = getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type);
239 - List<ChainKeyModel> chainKeys = walletKeyService.getKeysForChain(chaindIdNamespace);
240 + List<ChainKeyModel> chainKeys = walletKeyService.getKeysForChain(appStore.wallet!);
241 // Create the message to be signed
242 final String iss = 'did:pkh:$chaindIdNamespace:${chainKeys.first.publicKey}';
243 final Widget modalWidget = Web3RequestModal(
lib/di.dart
+1 -1
@@ -474,7 +474,7 @@ Future<void> setup({
474
475 final appStore = getIt.get<AppStore>();
476
477 - getIt.registerLazySingleton<WalletConnectKeyService>(() => KeyServiceImpl(appStore.wallet!));
477 + getIt.registerLazySingleton<WalletConnectKeyService>(() => KeyServiceImpl());
478
479 getIt.registerLazySingleton<Web3WalletService>(() {
480 final Web3WalletService web3WalletService = Web3WalletService(
lib/entities/load_current_wallet.dart
+1 -1
@@ -22,7 +22,7 @@ Future<void> loadCurrentWallet() async {
22 final type = deserializeFromInt(typeRaw);
23 final walletLoadingService = getIt.get<WalletLoadingService>();
24 final wallet = await walletLoadingService.load(type, name);
25 - appStore.changeCurrentWallet(wallet);
25 + await appStore.changeCurrentWallet(wallet);
26
27 getIt.get<BackgroundTasks>().registerSyncTask();
28 }
lib/store/app_store.dart
+6 -7
@@ -26,8 +26,7 @@ abstract class AppStoreBase with Store {
26 AuthenticationStore authenticationStore;
27
28 @observable
29 - WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>?
30 - wallet;
29 + WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>? wallet;
30
31 WalletListStore walletList;
32
@@ -36,16 +35,16 @@ abstract class AppStoreBase with Store {
35 NodeListStore nodeListStore;
36
37 @action
39 - void changeCurrentWallet(
40 - WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
41 - TransactionInfo>
42 - wallet) {
38 + Future<void> changeCurrentWallet(
39 + WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet) async {
40 this.wallet?.close();
41 this.wallet = wallet;
42 this.wallet!.setExceptionHandler(ExceptionHandler.onError);
43
44 if (isEVMCompatibleChain(wallet.type)) {
48 - getIt.get<Web3WalletService>().init();
45 + await getIt.get<Web3WalletService>().onDispose();
46 + getIt.get<Web3WalletService>().create();
47 + await getIt.get<Web3WalletService>().init();
48 }
49 }
50 }
lib/view_model/wallet_creation_vm.dart
+1 -1
@@ -74,7 +74,7 @@ abstract class WalletCreationVMBase with Store {
74 : await process(credentials);
75 walletInfo.address = wallet.walletAddresses.address;
76 await _walletInfoSource.add(walletInfo);
77 - _appStore.changeCurrentWallet(wallet);
77 + await _appStore.changeCurrentWallet(wallet);
78 getIt.get<BackgroundTasks>().registerSyncTask();
79 _appStore.authenticationStore.allowed();
80 state = ExecutedSuccessfullyState();
lib/view_model/wallet_list/wallet_list_view_model.dart
+1 -1
@@ -45,7 +45,7 @@ abstract class WalletListViewModelBase with Store {
45 @action
46 Future<void> loadWallet(WalletListItem walletItem) async {
47 final wallet = await _walletLoadingService.load(walletItem.type, walletItem.name);
48 - _appStore.changeCurrentWallet(wallet);
48 + await _appStore.changeCurrentWallet(wallet);
49 }
50
51 WalletListOrderType? get orderType => _appStore.settingsStore.walletListOrder;