Skip to content

Commit

Permalink
ledger changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCyjaneK committed Aug 23, 2024
1 parent 9c5459c commit 3563384
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
79 changes: 79 additions & 0 deletions impls/monero.dart/lib/src/ledger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';
import 'package:ledger_flutter/src/ledger.dart';
import 'package:ledger_flutter/src/ledger/ledger_operation.dart';
import 'package:ledger_flutter/src/models/ledger_device.dart';
import 'package:ledger_flutter/src/utils/buffer.dart';
import 'package:monero/monero.dart' as monero;

Timer? _ledgerExchangeTimer;
Uint8List _lastLedgerRequest = Uint8List(0);

void enableLedgerExchange(
monero.wallet ptr, Ledger ledger, LedgerDevice device) {
_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 && _lastLedgerRequest != ledgerRequest) {
final response = await exchange(ledger, device, ledgerRequest);

final Pointer<Uint8> result = malloc<Uint8>(response.length);
result.asTypedList(response.length).addAll(response);
monero.Wallet_setDeviceReceivedData(
ptr, result.cast<UnsignedChar>(), response.length);

monero.Wallet_setDeviceSendData(
ptr, malloc<Uint8>(0).cast<UnsignedChar>(), 0);
}
});
}

void disableLedgerExchange() {
_ledgerExchangeTimer?.cancel();
}

Future<Uint8List> exchange(
Ledger ledger, LedgerDevice device, Uint8List data) async {
return ledger.sendOperation<Uint8List>(
device,
ExchangeOperation(
cla: 0x00,
ins: 0x00,
p1: 0x00,
p2: 0x00,
inputData: data,
),
);
}

class ExchangeOperation extends LedgerOperation<Uint8List> {
final int cla;
final int ins;
final int p1;
final int p2;
final Uint8List inputData;

ExchangeOperation({
required this.cla,
required this.ins,
required this.p1,
required this.p2,
required this.inputData,
});

@override
Future<Uint8List> read(ByteDataReader reader) async =>
reader.read(reader.remainingLength);

@override
Future<List<Uint8List>> write(ByteDataWriter writer) async {
writer.write(inputData);

return [writer.toBytes()];
}
}
6 changes: 5 additions & 1 deletion impls/monero.dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ environment:

dependencies:
ffi: ^2.1.0
ledger_flutter:
git:
url: https://github.com/MrCyjaneK/ledger-flutter
ref: d68f1be485717bedad0779ee6114afec1896fbc9

dev_dependencies:
lints: ^4.0.0
test: ^1.24.0
ffigen: ^13.0.0
ffigen: ^13.0.0

0 comments on commit 3563384

Please sign in to comment.