cake_wallet/cw_monero/lib/account_list.dart

77 lines
2.3 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:cw_monero/signatures.dart';
import 'package:cw_monero/types.dart';
import 'package:cw_monero/monero_api.dart';
import 'package:cw_monero/structs/account_row.dart';
import 'package:flutter/foundation.dart';
final accountSizeNative = moneroApi
.lookup<NativeFunction<account_size>>('account_size')
.asFunction<SubaddressSize>();
final accountRefreshNative = moneroApi
.lookup<NativeFunction<account_refresh>>('account_refresh')
.asFunction<AccountRefresh>();
final accountGetAllNative = moneroApi
.lookup<NativeFunction<account_get_all>>('account_get_all')
.asFunction<AccountGetAll>();
final accountAddNewNative = moneroApi
.lookup<NativeFunction<account_add_new>>('account_add_row')
.asFunction<AccountAddNew>();
final accountSetLabelNative = moneroApi
.lookup<NativeFunction<account_set_label>>('account_set_label_row')
.asFunction<AccountSetLabel>();
2020-10-29 10:49:07 +00:00
bool isUpdating = false;
void refreshAccounts() {
try {
isUpdating = true;
accountRefreshNative();
isUpdating = false;
} catch (e) {
isUpdating = false;
rethrow;
}
}
2020-01-04 19:31:52 +00:00
List<AccountRow> getAllAccount() {
final size = accountSizeNative();
final accountAddressesPointer = accountGetAllNative();
final accountAddresses = accountAddressesPointer.asTypedList(size);
return accountAddresses
.map((addr) => Pointer<AccountRow>.fromAddress(addr).ref)
.toList();
}
2020-01-08 12:26:34 +00:00
void addAccountSync({String label}) {
2020-01-04 19:31:52 +00:00
final labelPointer = Utf8.toUtf8(label);
accountAddNewNative(labelPointer);
free(labelPointer);
}
2020-01-08 12:26:34 +00:00
void setLabelForAccountSync({int accountIndex, String label}) {
2020-01-04 19:31:52 +00:00
final labelPointer = Utf8.toUtf8(label);
accountSetLabelNative(accountIndex, labelPointer);
free(labelPointer);
}
2020-01-08 12:26:34 +00:00
void _addAccount(String label) => addAccountSync(label: label);
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
void _setLabelForAccount(Map<String, dynamic> args) {
final label = args['label'] as String;
final accountIndex = args['accountIndex'] as int;
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
setLabelForAccountSync(label: label, accountIndex: accountIndex);
}
Future<void> addAccount({String label}) async => compute(_addAccount, label);
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
Future<void> setLabelForAccount({int accountIndex, String label}) async =>
compute(
2020-10-29 10:49:07 +00:00
_setLabelForAccount, {'accountIndex': accountIndex, 'label': label});