| 1 | import "package:cake_wallet/core/wallet_loading_service.dart"; |
| 2 | import "package:cake_wallet/store/app_store.dart"; |
| 3 | import "package:cw_core/exceptions.dart"; |
| 4 | import "package:cw_core/wallet_info.dart"; |
| 5 | |
| 6 | class WalletSwitchService { |
| 7 | WalletSwitchService({ |
| 8 | required WalletLoadingService walletLoadingService, |
| 9 | required AppStore appStore, |
| 10 | }) : _walletLoadingService = walletLoadingService, |
| 11 | _appStore = appStore; |
| 12 | |
| 13 | final AppStore _appStore; |
| 14 | final WalletLoadingService _walletLoadingService; |
| 15 | |
| 16 | Future<void> switchToWallet(WalletInfo walletInfo) async { |
| 17 | final wallet = await _walletLoadingService.load(walletInfo.type, walletInfo.name); |
| 18 | |
| 19 | // load() recovers from a corrupted wallet by opening any other wallet it can, |
| 20 | // so the one it returns may not be the one that was requested. |
| 21 | if (wallet.name != walletInfo.name || wallet.type != walletInfo.type) { |
| 22 | throw WalletSwitchException( |
| 23 | "wallet switch loaded ${wallet.name} instead of ${walletInfo.name}", |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | await _appStore.changeCurrentWallet(wallet); |
| 28 | } |
| 29 | } |