| 1 | import 'dart:ffi'; |
| 2 | import 'dart:isolate'; |
| 3 | |
| 4 | import 'package:cw_monero/api/account_list.dart'; |
| 5 | import 'package:monero/monero.dart' as monero; |
| 6 | import 'package:monero/src/wallet2.dart'; |
| 7 | import 'package:mutex/mutex.dart'; |
| 8 | |
| 9 | Wallet2Coins? coins = null; |
| 10 | final coinsMutex = Mutex(); |
| 11 | |
| 12 | Future<void> refreshCoins(int accountIndex) async { |
| 13 | if (coinsMutex.isLocked) { |
| 14 | return; |
| 15 | } |
| 16 | coins = currentWallet!.coins(); |
| 17 | final coinsPtr = coins!.ffiAddress(); |
| 18 | await coinsMutex.acquire(); |
| 19 | await Isolate.run(() => monero.Coins_refresh(Pointer.fromAddress(coinsPtr))); |
| 20 | coinsMutex.release(); |
| 21 | } |
| 22 | |
| 23 | Future<int> countOfCoins() async { |
| 24 | await coinsMutex.acquire(); |
| 25 | final count = coins!.count(); |
| 26 | coinsMutex.release(); |
| 27 | return count; |
| 28 | } |
| 29 | |
| 30 | Future<Wallet2CoinsInfo> getCoin(int index) async { |
| 31 | await coinsMutex.acquire(); |
| 32 | final coin = coins!.coin(index); |
| 33 | coinsMutex.release(); |
| 34 | return coin; |
| 35 | } |
| 36 | |
| 37 | Future<int?> getCoinByKeyImage(String keyImage) async { |
| 38 | final count = await countOfCoins(); |
| 39 | for (int i = 0; i < count; i++) { |
| 40 | final coin = await getCoin(i); |
| 41 | if (keyImage == coin.keyImage()) { |
| 42 | return i; |
| 43 | } |
| 44 | } |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | Future<void> freezeCoin(int index) async { |
| 49 | await coinsMutex.acquire(); |
| 50 | final coinsPtr = coins!.ffiAddress(); |
| 51 | await Isolate.run(() => monero.Coins_setFrozen(Pointer.fromAddress(coinsPtr), index: index)); |
| 52 | coinsMutex.release(); |
| 53 | } |
| 54 | |
| 55 | Future<void> thawCoin(int index) async { |
| 56 | await coinsMutex.acquire(); |
| 57 | final coinsPtr = coins!.ffiAddress(); |
| 58 | await Isolate.run(() => monero.Coins_thaw(Pointer.fromAddress(coinsPtr), index: index)); |
| 59 | coinsMutex.release(); |
| 60 | } |