Skip to content

Commit

Permalink
Add Monero Ledger keep connection alive
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinullrich committed Nov 9, 2024
1 parent 41205ab commit 00551f2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 0 additions & 2 deletions cw_monero/lib/api/wallet_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,6 @@ Future<void> loadWallet(
throw WalletOpeningException(message: err);
}

if (deviceType == 1) disableLedgerExchange();

wptr = newWptr;
openedWalletsByPath[path] = wptr!;
}
Expand Down
19 changes: 19 additions & 0 deletions cw_monero/lib/ledger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import 'package:monero/monero.dart' as monero;
LedgerConnection? gLedger;

Timer? _ledgerExchangeTimer;
Timer? _ledgerKeepAlive;

void enableLedgerExchange(monero.wallet ptr, LedgerConnection connection) {
_ledgerExchangeTimer?.cancel();
_ledgerExchangeTimer = Timer.periodic(Duration(milliseconds: 1), (_) async {
final ledgerRequestLength = monero.Wallet_getSendToDeviceLength(ptr);
final ledgerRequest = monero.Wallet_getSendToDevice(ptr)
.cast<Uint8>()
.asTypedList(ledgerRequestLength);
if (ledgerRequestLength > 0) {
_ledgerKeepAlive?.cancel();

final Pointer<Uint8> emptyPointer = malloc<Uint8>(0);
monero.Wallet_setDeviceSendData(
ptr, emptyPointer.cast<UnsignedChar>(), 0);
Expand All @@ -36,12 +40,27 @@ void enableLedgerExchange(monero.wallet ptr, LedgerConnection connection) {
monero.Wallet_setDeviceReceivedData(
ptr, result.cast<UnsignedChar>(), response.length);
malloc.free(result);
keepAlive(connection);
}
});
}

void keepAlive(LedgerConnection connection) {
if (connection.connectionType == ConnectionType.ble) {
_ledgerKeepAlive = Timer.periodic(Duration(seconds: 10), (_) async {
UniversalBle.setNotifiable(
connection.device.id,
connection.device.deviceInfo.serviceId,
connection.device.deviceInfo.notifyCharacteristicKey,
BleInputProperty.notification,
);
});
}
}

void disableLedgerExchange() {
_ledgerExchangeTimer?.cancel();
_ledgerKeepAlive?.cancel();
gLedger?.disconnect();
gLedger = null;
}
Expand Down
1 change: 1 addition & 0 deletions lib/monero/cw_monero.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,6 @@ class CWMonero extends Monero {
@override
void setGlobalLedgerConnection(ledger.LedgerConnection connection) {
gLedger = connection;
keepAlive(connection);
}
}

0 comments on commit 00551f2

Please sign in to comment.