import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'package:flutter/foundation.dart'; import 'package:cw_monero/api/signatures.dart'; import 'package:cw_monero/api/types.dart'; import 'package:cw_monero/api/monero_api.dart'; import 'package:cw_monero/api/structs/subaddress_row.dart'; import 'package:cw_monero/api/wallet.dart'; final subaddressSizeNative = moneroApi .lookup>('subaddrress_size') .asFunction(); final subaddressRefreshNative = moneroApi .lookup>('subaddress_refresh') .asFunction(); final subaddrressGetAllNative = moneroApi .lookup>('subaddrress_get_all') .asFunction(); final subaddrressAddNewNative = moneroApi .lookup>('subaddress_add_row') .asFunction(); final subaddrressSetLabelNative = moneroApi .lookup>('subaddress_set_label') .asFunction(); bool isUpdating = false; void refreshSubaddresses({required int accountIndex}) { try { isUpdating = true; subaddressRefreshNative(accountIndex); isUpdating = false; } catch (e) { isUpdating = false; rethrow; } } List getAllSubaddresses() { final size = subaddressSizeNative(); final subaddressAddressesPointer = subaddrressGetAllNative(); final subaddressAddresses = subaddressAddressesPointer.asTypedList(size); return subaddressAddresses .map((addr) => Pointer.fromAddress(addr).ref) .toList(); } void addSubaddressSync({required int accountIndex, required String label}) { final labelPointer = label.toNativeUtf8(); subaddrressAddNewNative(accountIndex, labelPointer); calloc.free(labelPointer); } void setLabelForSubaddressSync( {required int accountIndex, required int addressIndex, required String label}) { final labelPointer = label.toNativeUtf8(); subaddrressSetLabelNative(accountIndex, addressIndex, labelPointer); calloc.free(labelPointer); } void _addSubaddress(Map args) { final label = args['label'] as String; final accountIndex = args['accountIndex'] as int; addSubaddressSync(accountIndex: accountIndex, label: label); } void _setLabelForSubaddress(Map args) { final label = args['label'] as String; final accountIndex = args['accountIndex'] as int; final addressIndex = args['addressIndex'] as int; setLabelForSubaddressSync( accountIndex: accountIndex, addressIndex: addressIndex, label: label); } Future addSubaddress({required int accountIndex, required String label}) async { await compute, void>( _addSubaddress, {'accountIndex': accountIndex, 'label': label}); await store(); } Future setLabelForSubaddress( {required int accountIndex, required int addressIndex, required String label}) async { await compute, void>(_setLabelForSubaddress, { 'accountIndex': accountIndex, 'addressIndex': addressIndex, 'label': label }); await store(); }