2021-12-24 12:52:08 +00:00
|
|
|
import 'dart:ffi';
|
2023-11-15 23:12:23 +00:00
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/convert_utf8_to_string.dart';
|
2023-11-15 23:12:23 +00:00
|
|
|
import 'package:cw_monero/api/exceptions/creation_transaction_exception.dart';
|
|
|
|
import 'package:cw_monero/api/monero_api.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/monero_output.dart';
|
2023-11-15 23:12:23 +00:00
|
|
|
import 'package:cw_monero/api/signatures.dart';
|
|
|
|
import 'package:cw_monero/api/structs/pending_transaction.dart';
|
|
|
|
import 'package:cw_monero/api/structs/transaction_info_row.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/structs/ut8_box.dart';
|
2023-11-15 23:12:23 +00:00
|
|
|
import 'package:cw_monero/api/types.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:ffi/ffi.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
|
|
|
final transactionsRefreshNative = moneroApi
|
|
|
|
.lookup<NativeFunction<transactions_refresh>>('transactions_refresh')
|
|
|
|
.asFunction<TransactionsRefresh>();
|
|
|
|
|
|
|
|
final transactionsCountNative = moneroApi
|
|
|
|
.lookup<NativeFunction<transactions_count>>('transactions_count')
|
|
|
|
.asFunction<TransactionsCount>();
|
|
|
|
|
|
|
|
final transactionsGetAllNative = moneroApi
|
|
|
|
.lookup<NativeFunction<transactions_get_all>>('transactions_get_all')
|
|
|
|
.asFunction<TransactionsGetAll>();
|
|
|
|
|
|
|
|
final transactionCreateNative = moneroApi
|
|
|
|
.lookup<NativeFunction<transaction_create>>('transaction_create')
|
|
|
|
.asFunction<TransactionCreate>();
|
|
|
|
|
|
|
|
final transactionCreateMultDestNative = moneroApi
|
|
|
|
.lookup<NativeFunction<transaction_create_mult_dest>>('transaction_create_mult_dest')
|
|
|
|
.asFunction<TransactionCreateMultDest>();
|
|
|
|
|
|
|
|
final transactionCommitNative = moneroApi
|
|
|
|
.lookup<NativeFunction<transaction_commit>>('transaction_commit')
|
|
|
|
.asFunction<TransactionCommit>();
|
|
|
|
|
2023-08-24 13:54:05 +00:00
|
|
|
final getTxKeyNative =
|
|
|
|
moneroApi.lookup<NativeFunction<get_tx_key>>('get_tx_key').asFunction<GetTxKey>();
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2023-11-15 23:12:23 +00:00
|
|
|
final getTransactionNative = moneroApi
|
|
|
|
.lookup<NativeFunction<get_transaction>>('get_transaction')
|
|
|
|
.asFunction<GetTransaction>();
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
String getTxKey(String txId) {
|
2022-10-12 17:09:57 +00:00
|
|
|
final txIdPointer = txId.toNativeUtf8();
|
2021-12-24 12:52:08 +00:00
|
|
|
final keyPointer = getTxKeyNative(txIdPointer);
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(txIdPointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (keyPointer != null) {
|
|
|
|
return convertUTF8ToString(pointer: keyPointer);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
return '';
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void refreshTransactions() => transactionsRefreshNative();
|
|
|
|
|
|
|
|
int countOfTransactions() => transactionsCountNative();
|
|
|
|
|
2023-09-08 17:12:54 +00:00
|
|
|
List<TransactionInfoRow> getAllTransactions() {
|
2021-12-24 12:52:08 +00:00
|
|
|
final size = transactionsCountNative();
|
|
|
|
final transactionsPointer = transactionsGetAllNative();
|
|
|
|
final transactionsAddresses = transactionsPointer.asTypedList(size);
|
|
|
|
|
|
|
|
return transactionsAddresses
|
|
|
|
.map((addr) => Pointer<TransactionInfoRow>.fromAddress(addr).ref)
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:12:23 +00:00
|
|
|
TransactionInfoRow getTransaction(String txId) {
|
|
|
|
final txIdPointer = txId.toNativeUtf8();
|
|
|
|
return getTransactionNative(txIdPointer).ref;
|
|
|
|
}
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
PendingTransactionDescription createTransactionSync(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required String address,
|
|
|
|
required String paymentId,
|
|
|
|
required int priorityRaw,
|
|
|
|
String? amount,
|
2023-08-24 13:54:05 +00:00
|
|
|
int accountIndex = 0,
|
|
|
|
List<String> preferredInputs = const []}) {
|
2022-10-12 17:09:57 +00:00
|
|
|
final addressPointer = address.toNativeUtf8();
|
|
|
|
final paymentIdPointer = paymentId.toNativeUtf8();
|
|
|
|
final amountPointer = amount != null ? amount.toNativeUtf8() : nullptr;
|
2023-08-24 13:54:05 +00:00
|
|
|
|
|
|
|
final int preferredInputsSize = preferredInputs.length;
|
|
|
|
final List<Pointer<Utf8>> preferredInputsPointers =
|
|
|
|
preferredInputs.map((output) => output.toNativeUtf8()).toList();
|
|
|
|
final Pointer<Pointer<Utf8>> preferredInputsPointerPointer = calloc(preferredInputsSize);
|
|
|
|
|
|
|
|
for (int i = 0; i < preferredInputsSize; i++) {
|
|
|
|
preferredInputsPointerPointer[i] = preferredInputsPointers[i];
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
final errorMessagePointer = calloc<Utf8Box>();
|
|
|
|
final pendingTransactionRawPointer = calloc<PendingTransactionRaw>();
|
2021-12-24 12:52:08 +00:00
|
|
|
final created = transactionCreateNative(
|
|
|
|
addressPointer,
|
|
|
|
paymentIdPointer,
|
|
|
|
amountPointer,
|
|
|
|
priorityRaw,
|
|
|
|
accountIndex,
|
2023-08-24 13:54:05 +00:00
|
|
|
preferredInputsPointerPointer,
|
|
|
|
preferredInputsSize,
|
2021-12-24 12:52:08 +00:00
|
|
|
errorMessagePointer,
|
|
|
|
pendingTransactionRawPointer) !=
|
|
|
|
0;
|
|
|
|
|
2023-08-24 13:54:05 +00:00
|
|
|
calloc.free(preferredInputsPointerPointer);
|
|
|
|
|
|
|
|
preferredInputsPointers.forEach((element) => calloc.free(element));
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(addressPointer);
|
|
|
|
calloc.free(paymentIdPointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (amountPointer != nullptr) {
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(amountPointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!created) {
|
|
|
|
final message = errorMessagePointer.ref.getValue();
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(errorMessagePointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
throw CreationTransactionException(message: message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PendingTransactionDescription(
|
|
|
|
amount: pendingTransactionRawPointer.ref.amount,
|
|
|
|
fee: pendingTransactionRawPointer.ref.fee,
|
|
|
|
hash: pendingTransactionRawPointer.ref.getHash(),
|
2022-07-28 17:03:16 +00:00
|
|
|
hex: pendingTransactionRawPointer.ref.getHex(),
|
|
|
|
txKey: pendingTransactionRawPointer.ref.getKey(),
|
2021-12-24 12:52:08 +00:00
|
|
|
pointerAddress: pendingTransactionRawPointer.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingTransactionDescription createTransactionMultDestSync(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required List<MoneroOutput> outputs,
|
2023-08-24 13:54:05 +00:00
|
|
|
required String paymentId,
|
|
|
|
required int priorityRaw,
|
|
|
|
int accountIndex = 0,
|
|
|
|
List<String> preferredInputs = const []}) {
|
2021-12-24 12:52:08 +00:00
|
|
|
final int size = outputs.length;
|
2023-08-24 13:54:05 +00:00
|
|
|
final List<Pointer<Utf8>> addressesPointers =
|
|
|
|
outputs.map((output) => output.address.toNativeUtf8()).toList();
|
2022-10-12 17:09:57 +00:00
|
|
|
final Pointer<Pointer<Utf8>> addressesPointerPointer = calloc(size);
|
2023-08-24 13:54:05 +00:00
|
|
|
final List<Pointer<Utf8>> amountsPointers =
|
|
|
|
outputs.map((output) => output.amount.toNativeUtf8()).toList();
|
2022-10-12 17:09:57 +00:00
|
|
|
final Pointer<Pointer<Utf8>> amountsPointerPointer = calloc(size);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
addressesPointerPointer[i] = addressesPointers[i];
|
|
|
|
amountsPointerPointer[i] = amountsPointers[i];
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:54:05 +00:00
|
|
|
final int preferredInputsSize = preferredInputs.length;
|
|
|
|
final List<Pointer<Utf8>> preferredInputsPointers =
|
|
|
|
preferredInputs.map((output) => output.toNativeUtf8()).toList();
|
|
|
|
final Pointer<Pointer<Utf8>> preferredInputsPointerPointer = calloc(preferredInputsSize);
|
|
|
|
|
|
|
|
for (int i = 0; i < preferredInputsSize; i++) {
|
|
|
|
preferredInputsPointerPointer[i] = preferredInputsPointers[i];
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
final paymentIdPointer = paymentId.toNativeUtf8();
|
|
|
|
final errorMessagePointer = calloc<Utf8Box>();
|
|
|
|
final pendingTransactionRawPointer = calloc<PendingTransactionRaw>();
|
2021-12-24 12:52:08 +00:00
|
|
|
final created = transactionCreateMultDestNative(
|
2023-08-24 13:54:05 +00:00
|
|
|
addressesPointerPointer,
|
|
|
|
paymentIdPointer,
|
|
|
|
amountsPointerPointer,
|
|
|
|
size,
|
|
|
|
priorityRaw,
|
|
|
|
accountIndex,
|
|
|
|
preferredInputsPointerPointer,
|
|
|
|
preferredInputsSize,
|
|
|
|
errorMessagePointer,
|
|
|
|
pendingTransactionRawPointer) !=
|
2021-12-24 12:52:08 +00:00
|
|
|
0;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(addressesPointerPointer);
|
|
|
|
calloc.free(amountsPointerPointer);
|
2023-08-24 13:54:05 +00:00
|
|
|
calloc.free(preferredInputsPointerPointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
addressesPointers.forEach((element) => calloc.free(element));
|
|
|
|
amountsPointers.forEach((element) => calloc.free(element));
|
2023-08-24 13:54:05 +00:00
|
|
|
preferredInputsPointers.forEach((element) => calloc.free(element));
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(paymentIdPointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (!created) {
|
|
|
|
final message = errorMessagePointer.ref.getValue();
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(errorMessagePointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
throw CreationTransactionException(message: message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PendingTransactionDescription(
|
|
|
|
amount: pendingTransactionRawPointer.ref.amount,
|
|
|
|
fee: pendingTransactionRawPointer.ref.fee,
|
|
|
|
hash: pendingTransactionRawPointer.ref.getHash(),
|
2022-07-28 17:03:16 +00:00
|
|
|
hex: pendingTransactionRawPointer.ref.getHex(),
|
|
|
|
txKey: pendingTransactionRawPointer.ref.getKey(),
|
2021-12-24 12:52:08 +00:00
|
|
|
pointerAddress: pendingTransactionRawPointer.address);
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:54:05 +00:00
|
|
|
void commitTransactionFromPointerAddress({required int address}) =>
|
|
|
|
commitTransaction(transactionPointer: Pointer<PendingTransactionRaw>.fromAddress(address));
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void commitTransaction({required Pointer<PendingTransactionRaw> transactionPointer}) {
|
|
|
|
final errorMessagePointer = calloc<Utf8Box>();
|
2023-08-24 13:54:05 +00:00
|
|
|
final isCommited = transactionCommitNative(transactionPointer, errorMessagePointer) != 0;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (!isCommited) {
|
|
|
|
final message = errorMessagePointer.ref.getValue();
|
2022-10-12 17:09:57 +00:00
|
|
|
calloc.free(errorMessagePointer);
|
2021-12-24 12:52:08 +00:00
|
|
|
throw CreationTransactionException(message: message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingTransactionDescription _createTransactionSync(Map args) {
|
|
|
|
final address = args['address'] as String;
|
|
|
|
final paymentId = args['paymentId'] as String;
|
2022-10-25 20:33:56 +00:00
|
|
|
final amount = args['amount'] as String?;
|
2021-12-24 12:52:08 +00:00
|
|
|
final priorityRaw = args['priorityRaw'] as int;
|
|
|
|
final accountIndex = args['accountIndex'] as int;
|
2023-08-24 13:54:05 +00:00
|
|
|
final preferredInputs = args['preferredInputs'] as List<String>;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
return createTransactionSync(
|
|
|
|
address: address,
|
|
|
|
paymentId: paymentId,
|
|
|
|
amount: amount,
|
|
|
|
priorityRaw: priorityRaw,
|
2023-08-24 13:54:05 +00:00
|
|
|
accountIndex: accountIndex,
|
|
|
|
preferredInputs: preferredInputs);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PendingTransactionDescription _createTransactionMultDestSync(Map args) {
|
|
|
|
final outputs = args['outputs'] as List<MoneroOutput>;
|
|
|
|
final paymentId = args['paymentId'] as String;
|
|
|
|
final priorityRaw = args['priorityRaw'] as int;
|
|
|
|
final accountIndex = args['accountIndex'] as int;
|
2023-08-24 13:54:05 +00:00
|
|
|
final preferredInputs = args['preferredInputs'] as List<String>;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
return createTransactionMultDestSync(
|
|
|
|
outputs: outputs,
|
|
|
|
paymentId: paymentId,
|
|
|
|
priorityRaw: priorityRaw,
|
2023-08-24 13:54:05 +00:00
|
|
|
accountIndex: accountIndex,
|
|
|
|
preferredInputs: preferredInputs);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<PendingTransactionDescription> createTransaction(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required String address,
|
|
|
|
required int priorityRaw,
|
|
|
|
String? amount,
|
2021-12-24 12:52:08 +00:00
|
|
|
String paymentId = '',
|
2023-08-24 13:54:05 +00:00
|
|
|
int accountIndex = 0,
|
|
|
|
List<String> preferredInputs = const []}) =>
|
2021-12-24 12:52:08 +00:00
|
|
|
compute(_createTransactionSync, {
|
|
|
|
'address': address,
|
|
|
|
'paymentId': paymentId,
|
|
|
|
'amount': amount,
|
|
|
|
'priorityRaw': priorityRaw,
|
2023-08-24 13:54:05 +00:00
|
|
|
'accountIndex': accountIndex,
|
|
|
|
'preferredInputs': preferredInputs
|
2021-12-24 12:52:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Future<PendingTransactionDescription> createTransactionMultDest(
|
2023-08-24 13:54:05 +00:00
|
|
|
{required List<MoneroOutput> outputs,
|
|
|
|
required int priorityRaw,
|
|
|
|
String paymentId = '',
|
|
|
|
int accountIndex = 0,
|
|
|
|
List<String> preferredInputs = const []}) =>
|
2021-12-24 12:52:08 +00:00
|
|
|
compute(_createTransactionMultDestSync, {
|
|
|
|
'outputs': outputs,
|
|
|
|
'paymentId': paymentId,
|
|
|
|
'priorityRaw': priorityRaw,
|
2023-08-24 13:54:05 +00:00
|
|
|
'accountIndex': accountIndex,
|
|
|
|
'preferredInputs': preferredInputs
|
2021-12-24 12:52:08 +00:00
|
|
|
});
|