exchange method

Future<List<Uint8List>> exchange(
  1. List<Uint8List> apdus, {
  2. int timeout = 2000,
})

Implementation

Future<List<Uint8List>> exchange(
  List<Uint8List> apdus, {
  int timeout = 2000,
}) async {
  final packer = UsbPacker();
  final output = <Uint8List>[];

  // Frame the packets
  final channel = (Random().nextDouble() * 0xffff).floor();
  const packetSize = 64;

  for (var apdu in apdus) {
    final blocks = await packer.pack(apdu, channel, packetSize);

    for (var block in blocks) {
      await LedgerUsbPlatform.instance.transferOut(block, timeout);
    }

    ResponseAcc? acc;
    while (packer.getReducedResult(acc) == null) {
      final response =
          await LedgerUsbPlatform.instance.transferIn(packetSize, timeout);
      if (response != null && response.isNotEmpty) {
        acc = packer.reduceResponse(acc, channel, response);
      }
    }

    final result = packer.getReducedResult(acc) ?? Uint8List.fromList([]);
    output.add(result);
  }

  return output;
}