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

This commit is contained in:
Adegoke David 2023-12-28 02:37:36 +01:00 committed by GitHub
parent c8856f5ca1
commit 92914a8532
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 66 deletions

View file

@ -138,7 +138,7 @@ class EvmChainServiceImpl implements ChainService {
try { try {
// Load the private key // Load the private key
final List<ChainKeyModel> keys = wcKeyService final List<ChainKeyModel> keys = wcKeyService
.getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); .getKeysForChain(appStore.wallet!);
final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey); final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey);
@ -177,7 +177,7 @@ class EvmChainServiceImpl implements ChainService {
try { try {
// Load the private key // Load the private key
final List<ChainKeyModel> keys = wcKeyService final List<ChainKeyModel> keys = wcKeyService
.getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); .getKeysForChain(appStore.wallet!);
final EthPrivateKey credentials = EthPrivateKey.fromHex(keys[0].privateKey); final EthPrivateKey credentials = EthPrivateKey.fromHex(keys[0].privateKey);
@ -215,7 +215,7 @@ class EvmChainServiceImpl implements ChainService {
// Load the private key // Load the private key
final List<ChainKeyModel> keys = wcKeyService final List<ChainKeyModel> keys = wcKeyService
.getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); .getKeysForChain(appStore.wallet!);
final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey); final Credentials credentials = EthPrivateKey.fromHex(keys[0].privateKey);
@ -275,7 +275,7 @@ class EvmChainServiceImpl implements ChainService {
} }
final List<ChainKeyModel> keys = wcKeyService final List<ChainKeyModel> keys = wcKeyService
.getKeysForChain(getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type)); .getKeysForChain(appStore.wallet!);
return EthSigUtil.signTypedData( return EthSigUtil.signTypedData(
privateKey: keys[0].privateKey, privateKey: keys[0].privateKey,

View file

@ -1,48 +1,22 @@
import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/ethereum/ethereum.dart';
import 'package:cake_wallet/core/wallet_connect/models/chain_key_model.dart'; import 'package:cake_wallet/core/wallet_connect/models/chain_key_model.dart';
import 'package:cake_wallet/polygon/polygon.dart'; import 'package:cake_wallet/polygon/polygon.dart';
import 'package:cw_core/balance.dart'; import 'package:cake_wallet/reactions/wallet_connect.dart';
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
abstract class WalletConnectKeyService { abstract class WalletConnectKeyService {
/// Returns a list of all the keys. /// Returns a list of all the keys.
List<ChainKeyModel> getKeys(); List<ChainKeyModel> getKeys(WalletBase wallet);
/// Returns a list of all the chain ids.
List<String> getChains();
/// Returns a list of all the keys for a given chain id. /// Returns a list of all the keys for a given chain id.
/// If the chain is not found, returns an empty list. /// If the chain is not found, returns an empty list.
/// - [chain]: The chain to get the keys for. /// - [chain]: The chain to get the keys for.
List<ChainKeyModel> getKeysForChain(String chain); List<ChainKeyModel> getKeysForChain(WalletBase wallet);
/// Returns a list of all the accounts in namespace:chainId:address format.
List<String> getAllAccounts();
} }
class KeyServiceImpl implements WalletConnectKeyService { 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<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet;
late final List<ChainKeyModel> _keys;
static String _getPrivateKeyForWallet(WalletBase wallet) { static String _getPrivateKeyForWallet(WalletBase wallet) {
switch (wallet.type) { switch (wallet.type) {
case WalletType.ethereum: case WalletType.ethereum:
@ -64,31 +38,31 @@ class KeyServiceImpl implements WalletConnectKeyService {
return ''; return '';
} }
} }
@override @override
List<String> getChains() { List<ChainKeyModel> getKeys(WalletBase wallet) {
final List<String> chainIds = []; final keys = [
for (final ChainKeyModel key in _keys) { ChainKeyModel(
chainIds.addAll(key.chains); chains: [
} 'eip155:1',
return chainIds; 'eip155:5',
'eip155:137',
'eip155:42161',
'eip155:80001',
],
privateKey: _getPrivateKeyForWallet(wallet),
publicKey: _getPublicKeyForWallet(wallet),
),
];
return keys;
} }
@override @override
List<ChainKeyModel> getKeys() => _keys; List<ChainKeyModel> getKeysForChain(WalletBase wallet) {
final chain = getChainNameSpaceAndIdBasedOnWalletType(wallet.type);
@override final keys = getKeys(wallet);
List<ChainKeyModel> getKeysForChain(String chain) {
return _keys.where((e) => e.chains.contains(chain)).toList();
}
@override return keys.where((e) => e.chains.contains(chain)).toList();
List<String> getAllAccounts() {
final List<String> accounts = [];
for (final ChainKeyModel key in _keys) {
for (final String chain in key.chains) {
accounts.add('$chain:${key.publicKey}');
}
}
return accounts;
} }
} }

View file

@ -68,7 +68,7 @@ abstract class Web3WalletServiceBase with Store {
); );
// Setup our accounts // Setup our accounts
List<ChainKeyModel> chainKeys = walletKeyService.getKeys(); List<ChainKeyModel> chainKeys = walletKeyService.getKeys(appStore.wallet!);
for (final chainKey in chainKeys) { for (final chainKey in chainKeys) {
for (final chainId in chainKey.chains) { for (final chainId in chainKey.chains) {
_web3Wallet.registerAccount( _web3Wallet.registerAccount(
@ -136,6 +136,7 @@ abstract class Web3WalletServiceBase with Store {
_web3Wallet.onAuthRequest.unsubscribe(_onAuthRequest); _web3Wallet.onAuthRequest.unsubscribe(_onAuthRequest);
_web3Wallet.core.pairing.onPairingDelete.unsubscribe(_onPairingDelete); _web3Wallet.core.pairing.onPairingDelete.unsubscribe(_onPairingDelete);
_web3Wallet.core.pairing.onPairingExpire.unsubscribe(_onPairingDelete); _web3Wallet.core.pairing.onPairingExpire.unsubscribe(_onPairingDelete);
isInitialized = false;
} }
Web3Wallet getWeb3Wallet() { Web3Wallet getWeb3Wallet() {
@ -236,7 +237,7 @@ abstract class Web3WalletServiceBase with Store {
Future<void> _onAuthRequest(AuthRequest? args) async { Future<void> _onAuthRequest(AuthRequest? args) async {
if (args != null) { if (args != null) {
final chaindIdNamespace = getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type); final chaindIdNamespace = getChainNameSpaceAndIdBasedOnWalletType(appStore.wallet!.type);
List<ChainKeyModel> chainKeys = walletKeyService.getKeysForChain(chaindIdNamespace); List<ChainKeyModel> chainKeys = walletKeyService.getKeysForChain(appStore.wallet!);
// Create the message to be signed // Create the message to be signed
final String iss = 'did:pkh:$chaindIdNamespace:${chainKeys.first.publicKey}'; final String iss = 'did:pkh:$chaindIdNamespace:${chainKeys.first.publicKey}';
final Widget modalWidget = Web3RequestModal( final Widget modalWidget = Web3RequestModal(

View file

@ -474,7 +474,7 @@ Future<void> setup({
final appStore = getIt.get<AppStore>(); final appStore = getIt.get<AppStore>();
getIt.registerLazySingleton<WalletConnectKeyService>(() => KeyServiceImpl(appStore.wallet!)); getIt.registerLazySingleton<WalletConnectKeyService>(() => KeyServiceImpl());
getIt.registerLazySingleton<Web3WalletService>(() { getIt.registerLazySingleton<Web3WalletService>(() {
final Web3WalletService web3WalletService = Web3WalletService( final Web3WalletService web3WalletService = Web3WalletService(

View file

@ -22,7 +22,7 @@ Future<void> loadCurrentWallet() async {
final type = deserializeFromInt(typeRaw); final type = deserializeFromInt(typeRaw);
final walletLoadingService = getIt.get<WalletLoadingService>(); final walletLoadingService = getIt.get<WalletLoadingService>();
final wallet = await walletLoadingService.load(type, name); final wallet = await walletLoadingService.load(type, name);
appStore.changeCurrentWallet(wallet); await appStore.changeCurrentWallet(wallet);
getIt.get<BackgroundTasks>().registerSyncTask(); getIt.get<BackgroundTasks>().registerSyncTask();
} }

View file

@ -26,8 +26,7 @@ abstract class AppStoreBase with Store {
AuthenticationStore authenticationStore; AuthenticationStore authenticationStore;
@observable @observable
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>? WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>? wallet;
wallet;
WalletListStore walletList; WalletListStore walletList;
@ -36,16 +35,16 @@ abstract class AppStoreBase with Store {
NodeListStore nodeListStore; NodeListStore nodeListStore;
@action @action
void changeCurrentWallet( Future<void> changeCurrentWallet(
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet) async {
TransactionInfo>
wallet) {
this.wallet?.close(); this.wallet?.close();
this.wallet = wallet; this.wallet = wallet;
this.wallet!.setExceptionHandler(ExceptionHandler.onError); this.wallet!.setExceptionHandler(ExceptionHandler.onError);
if (isEVMCompatibleChain(wallet.type)) { if (isEVMCompatibleChain(wallet.type)) {
getIt.get<Web3WalletService>().init(); await getIt.get<Web3WalletService>().onDispose();
getIt.get<Web3WalletService>().create();
await getIt.get<Web3WalletService>().init();
} }
} }
} }

View file

@ -74,7 +74,7 @@ abstract class WalletCreationVMBase with Store {
: await process(credentials); : await process(credentials);
walletInfo.address = wallet.walletAddresses.address; walletInfo.address = wallet.walletAddresses.address;
await _walletInfoSource.add(walletInfo); await _walletInfoSource.add(walletInfo);
_appStore.changeCurrentWallet(wallet); await _appStore.changeCurrentWallet(wallet);
getIt.get<BackgroundTasks>().registerSyncTask(); getIt.get<BackgroundTasks>().registerSyncTask();
_appStore.authenticationStore.allowed(); _appStore.authenticationStore.allowed();
state = ExecutedSuccessfullyState(); state = ExecutedSuccessfullyState();

View file

@ -45,7 +45,7 @@ abstract class WalletListViewModelBase with Store {
@action @action
Future<void> loadWallet(WalletListItem walletItem) async { Future<void> loadWallet(WalletListItem walletItem) async {
final wallet = await _walletLoadingService.load(walletItem.type, walletItem.name); final wallet = await _walletLoadingService.load(walletItem.type, walletItem.name);
_appStore.changeCurrentWallet(wallet); await _appStore.changeCurrentWallet(wallet);
} }
WalletListOrderType? get orderType => _appStore.settingsStore.walletListOrder; WalletListOrderType? get orderType => _appStore.settingsStore.walletListOrder;