feat: enhance Bitbox ViewModel with connection tracking and refactor imports (#3706)
What changed, and why it matters
This commit is a routine feature and cleanup change for the BitBox hardware wallet integration in Cake Wallet. It adds a simple connection-tracking timer, refactors import statements (mostly changing quote style and sorting), and removes an unnecessary 'await' keyword. There is nothing in the diff that introduces a security vulnerability or fixes an obvious one.
No security action required. Review as normal code-quality/feature change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies lib/view_model/hardware_wallet/bitbox_view_model.dart. Functionally, it adds a 1-second periodic Timer that checks whether the previously connected BitBox device is still present in the USB device list, updating _connectedDevice and isConnected() accordingly. It also removes ‘await’ from calls to getHardwareWalletService() inside initWallet() because that method is now synchronous (it returns a HardwareWalletService, not a Future). The remaining changes are cosmetic: converting single quotes to double quotes, sorting imports alphabetically, and adding a TODO owner tag. No security-sensitive logic changes are present.
Changed components
lib/view_model/hardware_wallet/bitbox_view_model.dartInspect captured patch +55 / −32
### lib/view_model/hardware_wallet/bitbox_view_model.dart
@@ -1,33 +1,36 @@
-import 'dart:async';
-import 'dart:io';
-
-import 'package:cake_wallet/bitcoin/bitcoin.dart';
-import 'package:cake_wallet/entities/hardware_wallet/hardware_wallet_device.dart';
-import 'package:cake_wallet/evm/evm.dart';
-import 'package:cake_wallet/view_model/hardware_wallet/hardware_wallet_view_model.dart';
-import 'package:cake_wallet/wallet_type_utils.dart';
-import 'package:cw_core/hardware/hardware_wallet_service.dart';
-import 'package:cw_core/root_dir.dart';
-import 'package:cw_core/utils/print_verbose.dart';
-import 'package:cw_core/wallet_base.dart';
-import 'package:cw_core/wallet_info.dart';
-import 'package:cw_core/wallet_type.dart';
-import 'package:bitbox_flutter/bitbox_flutter.dart' as sdk;
-import 'package:mobx/mobx.dart';
-
-part 'bitbox_view_model.g.dart';
+import "dart:async";
+import "dart:io";
+
+import "package:bitbox_flutter/bitbox_flutter.dart" as sdk;
+import "package:cake_wallet/bitcoin/bitcoin.dart";
+import "package:cake_wallet/entities/hardware_wallet/hardware_wallet_device.dart";
+import "package:cake_wallet/evm/evm.dart";
+import "package:cake_wallet/view_model/hardware_wallet/hardware_wallet_view_model.dart";
+import "package:cake_wallet/wallet_type_utils.dart";
+import "package:cw_core/hardware/hardware_wallet_service.dart";
+import "package:cw_core/root_dir.dart";
+import "package:cw_core/utils/print_verbose.dart";
+import "package:cw_core/wallet_base.dart";
+import "package:cw_core/wallet_info.dart";
+import "package:cw_core/wallet_type.dart";
+import "package:mobx/mobx.dart";
+
+part "bitbox_view_model.g.dart";
class BitboxViewModel = BitboxViewModelBase with _$BitboxViewModel;
abstract class BitboxViewModelBase extends HardwareWalletViewModel with Store {
- late final sdk.BitboxManager bitboxManager;
-
BitboxViewModelBase() {
if (!Platform.isIOS && !isMoneroOnly) {
bitboxManager = sdk.BitboxManager();
}
}
+ late final sdk.BitboxManager bitboxManager;
+
+ BitboxHardwareWalletDevice? _connectedDevice;
+ Timer? _isConnectedTimer;
+
@override
HardwareWalletType get hardwareWalletType => HardwareWalletType.bitbox;
@@ -40,7 +43,7 @@ abstract class BitboxViewModelBase extends HardwareWalletViewModel with Store {
bool isConnecting = false;
@override
- bool get hasBluetooth => Platform.isIOS && false; // TODO: remove when we enable bluetooth
+ bool get hasBluetooth => Platform.isIOS && false; // TODO(Konsti): remove when we enable bluetooth
@override
Future<void> updateBleState() async {}
@@ -50,38 +53,58 @@ abstract class BitboxViewModelBase extends HardwareWalletViewModel with Store {
@override
Future<List<HardwareWalletDevice>> getAllUsbDevices() => bitboxManager.devices
- .then((devices) => devices.map((d) => BitboxHardwareWalletDevice(d)).toList());
+ .then((devices) => devices.map(BitboxHardwareWalletDevice.new).toList());
@override
Future<void> stopScanning() async {}
@override
@action
Future<bool> connectDevice(HardwareWalletDevice device, WalletType type) async {
- if (!(device is BitboxHardwareWalletDevice)) return false;
- if (isConnecting) return false;
+ if (device is! BitboxHardwareWalletDevice) {
+ return false;
+ }
+ if (isConnecting) {
+ return false;
+ }
isConnecting = true;
try {
final appDocDir = await getAppDir();
await bitboxManager.connect(device.device);
- printV("Got Connection", file: '${appDocDir.path}/error.txt');
+ printV("Got Connection", file: "${appDocDir.path}/error.txt");
await bitboxManager.initBitBox();
- printV("Bitbox initialized!", file: '${appDocDir.path}/error.txt');
+ printV("Bitbox initialized!", file: "${appDocDir.path}/error.txt");
await bitboxManager.channelHashVerify();
- printV("Bitbox channel-hash verified!", file: '${appDocDir.path}/error.txt');
+ printV("Bitbox channel-hash verified!", file: "${appDocDir.path}/error.txt");
+
+ _connectedDevice = device;
+ _isConnectedTimer = Timer.periodic(const Duration(seconds: 1), (_) => _isStillConnected());
isConnecting = false;
return true;
} catch (e) {
printV(e);
}
isConnecting = false;
+ _connectedDevice = null;
+ _isConnectedTimer?.cancel();
+ _isConnectedTimer = null;
return false;
}
@override
- bool isConnected(WalletType type) => false;
+ bool isConnected(WalletType type) => _connectedDevice != null;
+
+ Future<void> _isStillConnected() async {
+ final devices = await getAllUsbDevices();
+
+ if (!devices.any((d) => d.name == _connectedDevice?.name)) {
+ _connectedDevice = null;
+ _isConnectedTimer?.cancel();
+ _isConnectedTimer = null;
+ }
+ }
@override
HardwareWalletService getHardwareWalletService(WalletType type) {
@@ -99,17 +122,17 @@ abstract class BitboxViewModelBase extends HardwareWalletViewModel with Store {
}
@override
- Future<void> initWallet(WalletBase wallet) async {
+ Future<void> initWallet(WalletBase wallet) {
switch (wallet.type) {
case WalletType.bitcoin:
case WalletType.litecoin:
return bitcoin!
- .setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
+ .setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
case WalletType.ethereum:
case WalletType.polygon:
- return evm!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
+ return evm!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
default:
- throw Exception('Unexpected wallet type: ${wallet.type} for bitbox');
+ throw Exception("Unexpected wallet type: ${wallet.type} for bitbox");
}
}
}Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.