2023-10-02 14:17:35 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
2023-12-14 04:51:16 +00:00
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
import 'package:cw_core/cake_hive.dart';
|
2023-10-02 14:17:35 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2023-12-14 04:51:16 +00:00
|
|
|
import 'package:cw_core/node.dart';
|
2023-10-02 14:17:35 +00:00
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
2023-12-14 04:51:16 +00:00
|
|
|
import 'package:cw_core/pending_transaction.dart';
|
|
|
|
import 'package:cw_core/sync_status.dart';
|
2023-10-02 14:17:35 +00:00
|
|
|
import 'package:cw_core/transaction_priority.dart';
|
2023-12-14 04:51:16 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_core/wallet_credentials.dart';
|
2023-12-14 04:51:16 +00:00
|
|
|
import 'package:cw_core/wallet_info.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/api/model/create_wallet_result.dart';
|
2024-03-08 10:50:34 +00:00
|
|
|
import 'package:cw_zano/api/model/destination.dart';
|
2024-04-09 10:59:43 +00:00
|
|
|
import 'package:cw_zano/api/model/get_recent_txs_and_info_result.dart';
|
2024-03-08 10:50:34 +00:00
|
|
|
import 'package:cw_zano/api/model/get_wallet_status_result.dart';
|
2024-03-16 10:55:03 +00:00
|
|
|
import 'package:cw_zano/api/model/transfer.dart';
|
2024-03-19 15:51:08 +00:00
|
|
|
import 'package:cw_zano/model/pending_zano_transaction.dart';
|
|
|
|
import 'package:cw_zano/model/zano_asset.dart';
|
|
|
|
import 'package:cw_zano/model/zano_balance.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/model/zano_transaction_creation_exception.dart';
|
2024-03-19 15:51:08 +00:00
|
|
|
import 'package:cw_zano/model/zano_transaction_credentials.dart';
|
|
|
|
import 'package:cw_zano/model/zano_transaction_info.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/model/zano_wallet_keys.dart';
|
|
|
|
import 'package:cw_zano/zano_formatter.dart';
|
|
|
|
import 'package:cw_zano/zano_transaction_history.dart';
|
2023-12-14 04:51:16 +00:00
|
|
|
import 'package:cw_zano/zano_wallet_addresses.dart';
|
2024-03-15 12:42:27 +00:00
|
|
|
import 'package:cw_zano/zano_wallet_api.dart';
|
2024-04-09 10:59:43 +00:00
|
|
|
import 'package:cw_zano/zano_wallet_exceptions.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/zano_wallet_service.dart';
|
2023-12-16 12:19:11 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-12-14 04:51:16 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
part 'zano_wallet.g.dart';
|
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
class ZanoWallet = ZanoWalletBase with _$ZanoWallet;
|
2023-12-14 04:51:16 +00:00
|
|
|
|
2024-03-15 12:42:27 +00:00
|
|
|
abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHistory, ZanoTransactionInfo> with Store, ZanoWalletApi {
|
2024-04-09 12:17:25 +00:00
|
|
|
static const int _autoSaveIntervalSeconds = 30;
|
|
|
|
static const int _pollIntervalMilliseconds = 2000;
|
2024-03-14 06:28:29 +00:00
|
|
|
|
2023-10-02 14:17:35 +00:00
|
|
|
@override
|
|
|
|
ZanoWalletAddresses walletAddresses;
|
|
|
|
|
|
|
|
@override
|
|
|
|
@observable
|
|
|
|
SyncStatus syncStatus;
|
|
|
|
|
|
|
|
@override
|
|
|
|
@observable
|
2023-11-17 17:40:23 +00:00
|
|
|
ObservableMap<CryptoCurrency, ZanoBalance> balance;
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
2023-12-14 04:51:16 +00:00
|
|
|
String seed = '';
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
2024-03-14 06:28:29 +00:00
|
|
|
ZanoWalletKeys keys = ZanoWalletKeys(privateSpendKey: '', privateViewKey: '', publicSpendKey: '', publicViewKey: '');
|
|
|
|
|
2024-03-16 10:55:03 +00:00
|
|
|
static const String zanoAssetId = 'd6329b5b1f7c0805b5c345f4957554002a2f557845f64d7645dae0e051a6498a';
|
2024-04-09 12:17:25 +00:00
|
|
|
|
2024-04-09 10:59:43 +00:00
|
|
|
Map<String, ZanoAsset> zanoAssets = {};
|
2023-10-02 14:17:35 +00:00
|
|
|
|
2024-03-08 10:50:34 +00:00
|
|
|
Timer? _updateSyncInfoTimer;
|
2024-03-14 06:28:29 +00:00
|
|
|
|
2024-03-08 10:50:34 +00:00
|
|
|
int _lastKnownBlockHeight = 0;
|
|
|
|
int _initialSyncHeight = 0;
|
2024-04-09 10:59:43 +00:00
|
|
|
int currentDaemonHeight = 0;
|
2023-10-02 14:17:35 +00:00
|
|
|
bool _isTransactionUpdating;
|
|
|
|
bool _hasSyncAfterStartup;
|
|
|
|
Timer? _autoSaveTimer;
|
2023-11-17 17:40:23 +00:00
|
|
|
|
2024-04-09 10:59:43 +00:00
|
|
|
/// index of last transaction fetched
|
|
|
|
int _lastTxIndex = 0;
|
|
|
|
/// number of transactions in each request
|
|
|
|
static const int _txChunkSize = 30;
|
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
ZanoWalletBase(WalletInfo walletInfo)
|
2024-04-06 10:03:11 +00:00
|
|
|
: balance = ObservableMap.of({CryptoCurrency.zano: ZanoBalance.empty()}),
|
2024-03-14 06:28:29 +00:00
|
|
|
_isTransactionUpdating = false,
|
|
|
|
_hasSyncAfterStartup = false,
|
|
|
|
walletAddresses = ZanoWalletAddresses(walletInfo),
|
|
|
|
syncStatus = NotConnectedSyncStatus(),
|
|
|
|
super(walletInfo) {
|
|
|
|
transactionHistory = ZanoTransactionHistory();
|
2024-03-15 12:42:27 +00:00
|
|
|
if (!CakeHive.isAdapterRegistered(ZanoAsset.typeId)) {
|
2024-03-14 06:28:29 +00:00
|
|
|
CakeHive.registerAdapter(ZanoAssetAdapter());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-15 12:42:27 +00:00
|
|
|
int calculateEstimatedFee(TransactionPriority priority, [int? amount = null]) => getCurrentTxFee(priority);
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
2024-03-14 06:28:29 +00:00
|
|
|
Future<void> changePassword(String password) async {
|
2024-03-15 12:42:27 +00:00
|
|
|
setPassword(password);
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
2023-10-02 14:17:35 +00:00
|
|
|
|
2024-04-03 15:14:53 +00:00
|
|
|
static Future<ZanoWallet> create({required WalletCredentials credentials}) async {
|
|
|
|
final wallet = ZanoWallet(credentials.walletInfo!);
|
|
|
|
await wallet.connectToNode(node: Node());
|
|
|
|
final path = await pathForWallet(name: credentials.name, type: credentials.walletInfo!.type);
|
|
|
|
final createWalletResult = await wallet.createWallet(path, credentials.password!);
|
2024-04-09 10:59:43 +00:00
|
|
|
await _parseCreateWalletResult(createWalletResult, wallet);
|
2024-04-03 15:14:53 +00:00
|
|
|
await wallet.init(createWalletResult.wi.address);
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<ZanoWallet> restore({required ZanoRestoreWalletFromSeedCredentials credentials}) async {
|
|
|
|
final wallet = ZanoWallet(credentials.walletInfo!);
|
|
|
|
await wallet.connectToNode(node: Node());
|
|
|
|
final path = await pathForWallet(name: credentials.name, type: credentials.walletInfo!.type);
|
|
|
|
final createWalletResult = await wallet.restoreWalletFromSeed(path, credentials.password!, credentials.mnemonic);
|
2024-04-09 10:59:43 +00:00
|
|
|
await _parseCreateWalletResult(createWalletResult, wallet);
|
2024-04-03 15:14:53 +00:00
|
|
|
await wallet.init(createWalletResult.wi.address);
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<ZanoWallet> open({required String name, required String password, required WalletInfo walletInfo}) async {
|
|
|
|
final path = await pathForWallet(name: name, type: walletInfo.type);
|
|
|
|
final wallet = ZanoWallet(walletInfo);
|
|
|
|
await wallet.connectToNode(node: Node());
|
|
|
|
final createWalletResult = await wallet.loadWallet(path, password);
|
2024-04-09 10:59:43 +00:00
|
|
|
await _parseCreateWalletResult(createWalletResult, wallet);
|
2024-04-03 15:14:53 +00:00
|
|
|
await wallet.init(createWalletResult.wi.address);
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
2024-04-09 10:59:43 +00:00
|
|
|
static Future<void> _parseCreateWalletResult(CreateWalletResult result, ZanoWallet wallet) async {
|
2024-04-03 15:14:53 +00:00
|
|
|
wallet.hWallet = result.walletId;
|
2024-04-09 12:17:25 +00:00
|
|
|
ZanoWalletApi.info('setting hWallet = ${result.walletId}');
|
2024-04-03 15:14:53 +00:00
|
|
|
wallet.walletAddresses.address = result.wi.address;
|
|
|
|
for (final item in result.wi.balances) {
|
2024-04-09 10:59:43 +00:00
|
|
|
if (item.assetInfo.assetId == zanoAssetId) {
|
2024-04-03 15:14:53 +00:00
|
|
|
wallet.balance[CryptoCurrency.zano] = ZanoBalance(
|
|
|
|
total: item.total,
|
|
|
|
unlocked: item.unlocked,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result.recentHistory.history != null) {
|
2024-04-09 10:59:43 +00:00
|
|
|
final transfers = result.recentHistory.history!;
|
|
|
|
final transactions = Transfer.makeMap(transfers, wallet.zanoAssets, wallet.currentDaemonHeight);
|
|
|
|
wallet.transactionHistory.addMany(transactions);
|
|
|
|
await wallet.transactionHistory.save();
|
2024-04-03 15:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 14:17:35 +00:00
|
|
|
@override
|
|
|
|
void close() {
|
2024-03-15 12:42:27 +00:00
|
|
|
closeWallet();
|
2024-03-08 10:50:34 +00:00
|
|
|
_updateSyncInfoTimer?.cancel();
|
2023-10-02 14:17:35 +00:00
|
|
|
_autoSaveTimer?.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> connectToNode({required Node node}) async {
|
2024-03-15 12:42:27 +00:00
|
|
|
syncStatus = ConnectingSyncStatus();
|
|
|
|
await setupNode();
|
|
|
|
syncStatus = ConnectedSyncStatus();
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<PendingTransaction> createTransaction(Object credentials) async {
|
2024-03-16 10:55:03 +00:00
|
|
|
credentials as ZanoTransactionCredentials;
|
2024-03-19 15:51:08 +00:00
|
|
|
final isZano = credentials.currency == CryptoCurrency.zano;
|
2024-03-16 10:55:03 +00:00
|
|
|
final outputs = credentials.outputs;
|
2023-10-02 14:17:35 +00:00
|
|
|
final hasMultiDestination = outputs.length > 1;
|
2024-03-16 10:55:03 +00:00
|
|
|
final unlockedBalanceZano = BigInt.from(balance[CryptoCurrency.zano]?.unlocked ?? 0);
|
|
|
|
final unlockedBalanceCurrency = BigInt.from(balance[credentials.currency]?.unlocked ?? 0);
|
|
|
|
final fee = BigInt.from(calculateEstimatedFee(credentials.priority));
|
|
|
|
late BigInt totalAmount;
|
|
|
|
void checkForEnoughBalances() {
|
2024-03-19 15:51:08 +00:00
|
|
|
if (isZano) {
|
2024-03-16 10:55:03 +00:00
|
|
|
if (totalAmount + fee > unlockedBalanceZano) {
|
|
|
|
throw ZanoTransactionCreationException(
|
|
|
|
"You don't have enough coins (required: ${ZanoFormatter.bigIntAmountToString(totalAmount + fee)} ZANO, unlocked ${ZanoFormatter.bigIntAmountToString(unlockedBalanceZano)} ZANO).");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fee > unlockedBalanceZano) {
|
|
|
|
throw ZanoTransactionCreationException(
|
|
|
|
"You don't have enough coins (required: ${ZanoFormatter.bigIntAmountToString(fee)} ZANO, unlocked ${ZanoFormatter.bigIntAmountToString(unlockedBalanceZano)} ZANO).");
|
|
|
|
}
|
|
|
|
if (totalAmount > unlockedBalanceCurrency) {
|
|
|
|
throw ZanoTransactionCreationException(
|
|
|
|
"You don't have enough coins (required: ${ZanoFormatter.bigIntAmountToString(totalAmount)} ${credentials.currency.title}, unlocked ${ZanoFormatter.bigIntAmountToString(unlockedBalanceZano)} ${credentials.currency.title}).");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:51:08 +00:00
|
|
|
final assetId = isZano ? zanoAssetId : (credentials.currency as ZanoAsset).assetId;
|
2024-03-08 10:50:34 +00:00
|
|
|
late List<Destination> destinations;
|
2023-10-02 14:17:35 +00:00
|
|
|
if (hasMultiDestination) {
|
2024-03-14 06:28:29 +00:00
|
|
|
if (outputs.any((output) => output.sendAll || (output.formattedCryptoAmount ?? 0) <= 0)) {
|
2024-03-08 10:50:34 +00:00
|
|
|
throw ZanoTransactionCreationException("You don't have enough coins.");
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
2024-03-16 10:55:03 +00:00
|
|
|
totalAmount = outputs.fold(BigInt.zero, (acc, value) => acc + BigInt.from(value.formattedCryptoAmount ?? 0));
|
|
|
|
checkForEnoughBalances();
|
2024-03-08 10:50:34 +00:00
|
|
|
destinations = outputs
|
|
|
|
.map((output) => Destination(
|
2024-03-16 10:55:03 +00:00
|
|
|
amount: BigInt.from(output.formattedCryptoAmount ?? 0),
|
2024-03-14 06:28:29 +00:00
|
|
|
address: output.isParsedAddress ? output.extractedAddress! : output.address,
|
2024-03-16 10:55:03 +00:00
|
|
|
assetId: assetId,
|
2024-03-08 10:50:34 +00:00
|
|
|
))
|
2023-10-02 14:17:35 +00:00
|
|
|
.toList();
|
|
|
|
} else {
|
|
|
|
final output = outputs.first;
|
2024-03-08 10:50:34 +00:00
|
|
|
if (output.sendAll) {
|
2024-03-19 15:51:08 +00:00
|
|
|
if (isZano) {
|
2024-03-16 10:55:03 +00:00
|
|
|
totalAmount = unlockedBalanceZano - fee;
|
|
|
|
} else {
|
|
|
|
totalAmount = unlockedBalanceCurrency;
|
|
|
|
}
|
2024-03-08 10:50:34 +00:00
|
|
|
} else {
|
2024-03-16 10:55:03 +00:00
|
|
|
totalAmount = BigInt.from(output.formattedCryptoAmount!);
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
2024-03-16 10:55:03 +00:00
|
|
|
checkForEnoughBalances();
|
2024-03-08 10:50:34 +00:00
|
|
|
destinations = [
|
|
|
|
Destination(
|
2024-03-16 10:55:03 +00:00
|
|
|
amount: totalAmount,
|
2024-03-14 06:28:29 +00:00
|
|
|
address: output.isParsedAddress ? output.extractedAddress! : output.address,
|
2024-03-16 10:55:03 +00:00
|
|
|
assetId: assetId,
|
2024-03-08 10:50:34 +00:00
|
|
|
)
|
|
|
|
];
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
2024-03-08 10:50:34 +00:00
|
|
|
return PendingZanoTransaction(
|
|
|
|
zanoWallet: this,
|
|
|
|
destinations: destinations,
|
|
|
|
fee: fee,
|
|
|
|
comment: outputs.first.note ?? '',
|
2024-03-16 10:55:03 +00:00
|
|
|
assetId: assetId,
|
|
|
|
ticker: credentials.currency.title,
|
2024-03-20 11:17:48 +00:00
|
|
|
decimalPoint: credentials.currency.decimals,
|
2024-03-16 10:55:03 +00:00
|
|
|
amount: totalAmount,
|
2024-03-08 10:50:34 +00:00
|
|
|
);
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-14 06:28:29 +00:00
|
|
|
Future<Map<String, ZanoTransactionInfo>> fetchTransactions() async {
|
2024-03-06 06:48:59 +00:00
|
|
|
try {
|
2024-04-09 10:59:43 +00:00
|
|
|
final transfers = <Transfer>[];
|
|
|
|
late GetRecentTxsAndInfoResult result;
|
|
|
|
do {
|
|
|
|
result = await getRecentTxsAndInfo(offset: _lastTxIndex, count: _txChunkSize);
|
|
|
|
_lastTxIndex += result.transfers.length;
|
|
|
|
transfers.addAll(result.transfers);
|
|
|
|
} while (result.lastItemIndex + 1 < result.totalTransfers);
|
|
|
|
return Transfer.makeMap(transfers, zanoAssets, currentDaemonHeight);
|
2024-03-06 06:48:59 +00:00
|
|
|
} catch (e) {
|
2024-03-14 06:28:29 +00:00
|
|
|
print(e);
|
|
|
|
return {};
|
2024-03-06 06:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
Future<void> init(String address) async {
|
|
|
|
await walletAddresses.init();
|
|
|
|
await walletAddresses.updateAddress(address);
|
|
|
|
await updateTransactions();
|
2024-04-09 12:17:25 +00:00
|
|
|
_autoSaveTimer = Timer.periodic(Duration(seconds: _autoSaveIntervalSeconds), (_) async => await save());
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 14:17:35 +00:00
|
|
|
@override
|
|
|
|
Future<void> renameWalletFiles(String newWalletName) async {
|
|
|
|
final currentWalletPath = await pathForWallet(name: name, type: type);
|
|
|
|
final currentCacheFile = File(currentWalletPath);
|
|
|
|
final currentKeysFile = File('$currentWalletPath.keys');
|
|
|
|
final currentAddressListFile = File('$currentWalletPath.address.txt');
|
|
|
|
|
|
|
|
final newWalletPath = await pathForWallet(name: newWalletName, type: type);
|
|
|
|
|
|
|
|
// Copies current wallet files into new wallet name's dir and files
|
|
|
|
if (currentCacheFile.existsSync()) {
|
|
|
|
await currentCacheFile.copy(newWalletPath);
|
|
|
|
}
|
|
|
|
if (currentKeysFile.existsSync()) {
|
|
|
|
await currentKeysFile.copy('$newWalletPath.keys');
|
|
|
|
}
|
|
|
|
if (currentAddressListFile.existsSync()) {
|
|
|
|
await currentAddressListFile.copy('$newWalletPath.address.txt');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete old name's dir and files
|
|
|
|
await Directory(currentWalletPath).delete(recursive: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-04-03 15:14:53 +00:00
|
|
|
Future<void> rescan({required int height}) => throw UnimplementedError();
|
2024-04-09 12:17:25 +00:00
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
@override
|
|
|
|
Future<void> save() async {
|
2024-03-06 06:48:59 +00:00
|
|
|
try {
|
2024-03-14 06:28:29 +00:00
|
|
|
await store();
|
2024-04-03 15:14:53 +00:00
|
|
|
await walletAddresses.updateAddressesInBox();
|
2024-03-14 06:28:29 +00:00
|
|
|
} catch (e) {
|
|
|
|
print('Error while saving Zano wallet file ${e.toString()}');
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 06:48:59 +00:00
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
@override
|
|
|
|
Future<void> startSync() async {
|
|
|
|
try {
|
|
|
|
syncStatus = AttemptingSyncStatus();
|
|
|
|
_lastKnownBlockHeight = 0;
|
|
|
|
_initialSyncHeight = 0;
|
2024-04-09 12:17:25 +00:00
|
|
|
_updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: _pollIntervalMilliseconds), (_) async {
|
2024-04-03 15:14:53 +00:00
|
|
|
final walletStatus = await getWalletStatus();
|
2024-04-09 10:59:43 +00:00
|
|
|
currentDaemonHeight = walletStatus.currentDaemonHeight;
|
2024-03-14 06:28:29 +00:00
|
|
|
_updateSyncProgress(walletStatus);
|
2024-04-03 15:14:53 +00:00
|
|
|
|
|
|
|
// we can call getWalletInfo ONLY if getWalletStatus returns NOT is in long refresh and wallet state is 2 (ready)
|
2024-03-14 06:28:29 +00:00
|
|
|
if (!walletStatus.isInLongRefresh && walletStatus.walletState == 2) {
|
2024-04-03 15:14:53 +00:00
|
|
|
final walletInfo = await getWalletInfo();
|
2024-03-14 06:28:29 +00:00
|
|
|
seed = walletInfo.wiExtended.seed;
|
|
|
|
keys = ZanoWalletKeys(
|
|
|
|
privateSpendKey: walletInfo.wiExtended.spendPrivateKey,
|
|
|
|
privateViewKey: walletInfo.wiExtended.viewPrivateKey,
|
|
|
|
publicSpendKey: walletInfo.wiExtended.spendPublicKey,
|
|
|
|
publicViewKey: walletInfo.wiExtended.viewPublicKey,
|
|
|
|
);
|
2024-03-06 06:48:59 +00:00
|
|
|
|
2024-04-09 10:59:43 +00:00
|
|
|
final assets = await getAssetsWhitelist();
|
|
|
|
zanoAssets = {};
|
|
|
|
for (final asset in assets) {
|
|
|
|
final newAsset = ZanoAsset.copyWith(asset,
|
|
|
|
icon: _getIconPath(asset.title), enabled: walletInfo.wi.balances.any((element) => element.assetId == asset.assetId));
|
|
|
|
zanoAssets.putIfAbsent(asset.assetId, () => newAsset);
|
|
|
|
}
|
2024-04-03 15:14:53 +00:00
|
|
|
// matching balances and whitelists
|
|
|
|
// 1. show only balances available in whitelists
|
|
|
|
// 2. set whitelists available in balances as 'enabled' ('disabled' by default)
|
2024-04-06 10:03:11 +00:00
|
|
|
for (final b in walletInfo.wi.balances) {
|
2024-04-09 10:59:43 +00:00
|
|
|
if (b.assetId == zanoAssetId) {
|
|
|
|
balance[CryptoCurrency.zano] = ZanoBalance(total: b.total, unlocked: b.unlocked);
|
2024-03-15 12:42:27 +00:00
|
|
|
} else {
|
2024-04-09 10:59:43 +00:00
|
|
|
final asset = zanoAssets[b.assetId];
|
2024-04-03 15:14:53 +00:00
|
|
|
if (asset == null) {
|
2024-04-09 12:17:25 +00:00
|
|
|
ZanoWalletApi.error('balance for an unknown asset ${b.assetInfo.assetId}');
|
2024-04-03 15:14:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-04-06 10:03:11 +00:00
|
|
|
if (balance.keys.any((element) => element is ZanoAsset && element.assetId == b.assetInfo.assetId)) {
|
|
|
|
balance[balance.keys.firstWhere((element) => element is ZanoAsset && element.assetId == b.assetInfo.assetId)] =
|
|
|
|
ZanoBalance(total: b.total, unlocked: b.unlocked, decimalPoint: asset.decimalPoint);
|
2024-04-03 15:14:53 +00:00
|
|
|
} else {
|
2024-04-06 10:03:11 +00:00
|
|
|
balance[asset] = ZanoBalance(total: b.total, unlocked: b.unlocked, decimalPoint: asset.decimalPoint);
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 12:17:25 +00:00
|
|
|
// removing balances for assets missing in wallet info balances
|
2024-04-03 15:14:53 +00:00
|
|
|
balance.removeWhere(
|
2024-04-09 10:59:43 +00:00
|
|
|
(key, _) => key != CryptoCurrency.zano && !walletInfo.wi.balances.any((element) => element.assetId == (key as ZanoAsset).assetId),
|
|
|
|
);
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
|
|
|
});
|
2024-03-06 06:48:59 +00:00
|
|
|
} catch (e) {
|
2024-03-14 06:28:29 +00:00
|
|
|
syncStatus = FailedSyncStatus();
|
|
|
|
print(e);
|
|
|
|
rethrow;
|
2023-12-14 04:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-02 14:17:35 +00:00
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
@override
|
|
|
|
Future<void>? updateBalance() => null;
|
|
|
|
|
2023-10-02 14:17:35 +00:00
|
|
|
Future<void> updateTransactions() async {
|
|
|
|
try {
|
|
|
|
if (_isTransactionUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_isTransactionUpdating = true;
|
|
|
|
final transactions = await fetchTransactions();
|
|
|
|
transactionHistory.addMany(transactions);
|
|
|
|
await transactionHistory.save();
|
|
|
|
_isTransactionUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
_isTransactionUpdating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:42:27 +00:00
|
|
|
Future<CryptoCurrency> addZanoAssetById(String assetId) async {
|
2024-04-09 10:59:43 +00:00
|
|
|
if (zanoAssets.containsKey(assetId)) {
|
|
|
|
throw ZanoWalletException('zano asset with id $assetId already added');
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
|
|
|
final assetDescriptor = await addAssetsWhitelist(assetId);
|
|
|
|
if (assetDescriptor == null) {
|
2024-04-09 10:59:43 +00:00
|
|
|
throw ZanoWalletException("there's no zano asset with id $assetId");
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
2024-04-09 10:59:43 +00:00
|
|
|
final asset = ZanoAsset.copyWith(assetDescriptor, icon: _getIconPath(assetDescriptor.title), assetId: assetId, enabled: true);
|
|
|
|
zanoAssets[asset.assetId] = asset;
|
2024-04-06 10:03:11 +00:00
|
|
|
balance[asset] = ZanoBalance.empty(decimalPoint: asset.decimalPoint);
|
2024-03-15 12:42:27 +00:00
|
|
|
return asset;
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-06 10:03:11 +00:00
|
|
|
String? _getIconPath(String title) {
|
2024-03-14 06:28:29 +00:00
|
|
|
try {
|
2024-04-06 10:03:11 +00:00
|
|
|
return CryptoCurrency.all.firstWhere((element) => element.title.toUpperCase() == title.toUpperCase()).iconPath;
|
2024-03-14 06:28:29 +00:00
|
|
|
} catch (_) {}
|
2024-04-06 10:03:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> changeZanoAssetAvailability(ZanoAsset asset) async {
|
2024-03-14 06:28:29 +00:00
|
|
|
if (asset.enabled) {
|
2024-03-15 12:42:27 +00:00
|
|
|
final assetDescriptor = await addAssetsWhitelist(asset.assetId);
|
|
|
|
if (assetDescriptor == null) {
|
2024-04-09 12:17:25 +00:00
|
|
|
print('Error adding zano asset');
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
2024-03-14 06:28:29 +00:00
|
|
|
} else {
|
2024-03-15 12:42:27 +00:00
|
|
|
final result = await removeAssetsWhitelist(asset.assetId);
|
|
|
|
if (result == false) {
|
2024-04-09 12:17:25 +00:00
|
|
|
print('Error removing zano asset');
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:42:27 +00:00
|
|
|
Future<void> deleteZanoAsset(ZanoAsset asset) async {
|
2024-04-09 12:17:25 +00:00
|
|
|
final _ = await removeAssetsWhitelist(asset.assetId);
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<ZanoAsset?> getZanoAsset(String assetId) async {
|
2024-03-19 15:51:08 +00:00
|
|
|
return await getAssetInfo(assetId);
|
2024-03-14 06:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _askForUpdateTransactionHistory() async => await updateTransactions();
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
void _onNewBlock(int height, int blocksLeft, double ptc) async {
|
|
|
|
try {
|
|
|
|
if (blocksLeft < 1000) {
|
|
|
|
await _askForUpdateTransactionHistory();
|
|
|
|
syncStatus = SyncedSyncStatus();
|
|
|
|
|
|
|
|
if (!_hasSyncAfterStartup) {
|
|
|
|
_hasSyncAfterStartup = true;
|
|
|
|
await save();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
syncStatus = SyncingSyncStatus(blocksLeft, ptc);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
void _updateSyncProgress(GetWalletStatusResult walletStatus) {
|
|
|
|
final syncHeight = walletStatus.currentWalletHeight;
|
|
|
|
if (_initialSyncHeight <= 0) {
|
|
|
|
_initialSyncHeight = syncHeight;
|
|
|
|
}
|
|
|
|
final bchHeight = walletStatus.currentDaemonHeight;
|
2024-03-08 10:50:34 +00:00
|
|
|
|
2024-03-14 06:28:29 +00:00
|
|
|
if (_lastKnownBlockHeight == syncHeight) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastKnownBlockHeight = syncHeight;
|
|
|
|
final track = bchHeight - _initialSyncHeight;
|
|
|
|
final diff = track - (bchHeight - syncHeight);
|
|
|
|
final ptc = diff <= 0 ? 0.0 : diff / track;
|
|
|
|
final left = bchHeight - syncHeight;
|
|
|
|
|
|
|
|
if (syncHeight < 0 || left < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents;
|
|
|
|
_onNewBlock.call(syncHeight, left, ptc);
|
2024-03-08 10:50:34 +00:00
|
|
|
}
|
2024-04-06 10:03:11 +00:00
|
|
|
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|