some refactoring

This commit is contained in:
leo 2024-04-09 12:17:25 +00:00
parent 9f93cb3f92
commit 0a6f162991
7 changed files with 90 additions and 295 deletions

View file

@ -1,6 +1,5 @@
import 'dart:ffi';
import 'package:cw_zano/api/utf8.dart';
import 'package:cw_zano/api/utf8_box.dart';
import 'package:cw_zano/api/zano_api.dart';
import 'package:ffi/ffi.dart';

View file

@ -48,33 +48,31 @@ class Transfer {
factory Transfer.fromJson(Map<String, dynamic> json) => Transfer(
comment: json['comment'] as String? ?? '',
employedEntries: EmployedEntries.fromJson(
json['employed_entries'] as Map<String, dynamic>? ?? {}),
employedEntries: EmployedEntries.fromJson(json['employed_entries'] as Map<String, dynamic>? ?? {}),
fee: json['fee'] as int? ?? 0,
height: json['height'] as int? ?? 0,
isMining: json['is_mining'] as bool? ?? false,
isMixing: json['is_mixing'] as bool? ?? false,
isService: json['is_service'] as bool? ?? false,
paymentId: json['payment_id'] as String? ?? '',
remoteAddresses: json['remote_addresses'] == null ? [] :
(json['remote_addresses'] as List<dynamic>).cast<String>(),
remoteAddresses: json['remote_addresses'] == null ? [] : (json['remote_addresses'] as List<dynamic>).cast<String>(),
remoteAliases: json['remote_aliases'] == null ? [] : (json['remote_aliases'] as List<dynamic>).cast<String>(),
showSender: json['show_sender'] as bool? ?? false,
subtransfers: (json['subtransfers'] as List<dynamic>? ?? [])
.map((e) => Subtransfer.fromJson(e as Map<String, dynamic>))
.toList(),
subtransfers: (json['subtransfers'] as List<dynamic>? ?? []).map((e) => Subtransfer.fromJson(e as Map<String, dynamic>)).toList(),
timestamp: json['timestamp'] as int? ?? 0,
transferInternalIndex: json['transfer_internal_index'] == null ? 0 : json['transfer_internal_index'] is double ? (json['transfer_internal_index'] as double).toInt() : json['transfer_internal_index'] as int,
transferInternalIndex: json['transfer_internal_index'] == null
? 0
: json['transfer_internal_index'] is double
? (json['transfer_internal_index'] as double).toInt()
: json['transfer_internal_index'] as int,
txBlobSize: json['tx_blob_size'] as int? ?? 0,
txHash: json['tx_hash'] as String? ?? '',
txType: json['tx_type'] as int? ?? 0,
unlockTime: json['unlock_time'] as int? ?? 0,
);
//static const String zanoAssetId = 'd6329b5b1f7c0805b5c345f4957554002a2f557845f64d7645dae0e051a6498a';
static Map<String, ZanoTransactionInfo> makeMap(List<Transfer> transfers, Map<String, ZanoAsset> zanoAssets, int currentDaemonHeight) => Map.fromIterable(
static Map<String, ZanoTransactionInfo> makeMap(List<Transfer> transfers, Map<String, ZanoAsset> zanoAssets, int currentDaemonHeight) =>
Map.fromIterable(
transfers,
key: (item) => (item as Transfer).txHash,
value: (transfer) {
@ -89,7 +87,8 @@ class Transfer {
bool isSimple = single != null;
// TODO: for complex transactions we show zano or any other transaction, will fix it later
if (!isSimple) {
single = transfer.subtransfers.firstWhereOrNull((element) => element.assetId == ZanoWalletBase.zanoAssetId) ?? transfer.subtransfers.first;
single =
transfer.subtransfers.firstWhereOrNull((element) => element.assetId == ZanoWalletBase.zanoAssetId) ?? transfer.subtransfers.first;
}
if (single.assetId != ZanoWalletBase.zanoAssetId) {
final asset = zanoAssets[single.assetId];

View file

@ -45,7 +45,6 @@ class ZanoAsset extends CryptoCurrency with HiveObjectMixin {
this.decimalPoint = ZanoFormatter.defaultDecimalPoint,
bool enabled = true,
this.iconPath,
//this.tag,
this.owner = defaultOwner,
this.metaInfo = '',
this.currentSupply = 0,
@ -62,13 +61,12 @@ class ZanoAsset extends CryptoCurrency with HiveObjectMixin {
decimals: decimalPoint,
);
ZanoAsset.copyWith(ZanoAsset other, {String? icon, /*String? tag,*/ String? assetId, bool enabled = true})
ZanoAsset.copyWith(ZanoAsset other, {String? icon, String? assetId, bool enabled = true})
: this.fullName = other.fullName,
this.ticker = other.ticker,
this.assetId = assetId ?? other.assetId,
this.decimalPoint = other.decimalPoint,
this._enabled = enabled && other.enabled,
//this.tag = tag,
this.iconPath = icon,
this.currentSupply = other.currentSupply,
this.hiddenSupply = other.hiddenSupply,

View file

@ -8,5 +8,4 @@ class ZanoTransactionCredentials {
final List<OutputInfo> outputs;
final MoneroTransactionPriority priority;
final CryptoCurrency currency;
//final String assetType;
}

View file

@ -1,26 +1,20 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/monero_transaction_priority.dart';
import 'package:cw_core/monero_wallet_utils.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/transaction_direction.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_zano/api/model/balance.dart';
import 'package:cw_zano/api/model/create_wallet_result.dart';
import 'package:cw_zano/api/model/destination.dart';
import 'package:cw_zano/api/model/get_recent_txs_and_info_result.dart';
import 'package:cw_zano/api/model/get_wallet_status_result.dart';
import 'package:cw_zano/api/model/subtransfer.dart';
import 'package:cw_zano/api/model/transfer.dart';
import 'package:cw_zano/model/pending_zano_transaction.dart';
import 'package:cw_zano/model/zano_asset.dart';
@ -36,18 +30,16 @@ import 'package:cw_zano/zano_wallet_api.dart';
import 'package:cw_zano/zano_wallet_exceptions.dart';
import 'package:cw_zano/zano_wallet_service.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:collection/collection.dart';
part 'zano_wallet.g.dart';
class ZanoWallet = ZanoWalletBase with _$ZanoWallet;
abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHistory, ZanoTransactionInfo> with Store, ZanoWalletApi {
static const int _autoSaveInterval = 30;
static const int _autoSaveIntervalSeconds = 30;
static const int _pollIntervalMilliseconds = 2000;
//List<Transfer> transfers = [];
@override
ZanoWalletAddresses walletAddresses;
@ -66,18 +58,11 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
ZanoWalletKeys keys = ZanoWalletKeys(privateSpendKey: '', privateViewKey: '', publicSpendKey: '', publicViewKey: '');
static const String zanoAssetId = 'd6329b5b1f7c0805b5c345f4957554002a2f557845f64d7645dae0e051a6498a';
/*
late final Box<ZanoAsset> zanoAssetsBox;
List<ZanoAsset> whitelists = [];
List<ZanoAsset> get zanoAssets => zanoAssetsBox.values.toList();
*/
Map<String, ZanoAsset> zanoAssets = {};
//zano_wallet.SyncListener? _listener;
// ReactionDisposer? _onAccountChangeReaction;
Timer? _updateSyncInfoTimer;
int _cachedBlockchainHeight = 0;
int _lastKnownBlockHeight = 0;
int _initialSyncHeight = 0;
int currentDaemonHeight = 0;
@ -101,14 +86,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
if (!CakeHive.isAdapterRegistered(ZanoAsset.typeId)) {
CakeHive.registerAdapter(ZanoAssetAdapter());
}
// _onAccountChangeReaction =
// reaction((_) => walletAddresses.account, (Account? account) {
// if (account == null) {
// return;
// }
// balance.addAll(getZanoBalance(accountIndex: account.id));
// /**walletAddresses.updateSubaddressList(accountIndex: account.id);*/
// });
}
@override
@ -125,7 +102,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
final path = await pathForWallet(name: credentials.name, type: credentials.walletInfo!.type);
final createWalletResult = await wallet.createWallet(path, credentials.password!);
await _parseCreateWalletResult(createWalletResult, wallet);
//await wallet.store(); // TODO: unnecessary here?
await wallet.init(createWalletResult.wi.address);
return wallet;
}
@ -136,7 +112,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
final path = await pathForWallet(name: credentials.name, type: credentials.walletInfo!.type);
final createWalletResult = await wallet.restoreWalletFromSeed(path, credentials.password!, credentials.mnemonic);
await _parseCreateWalletResult(createWalletResult, wallet);
//await wallet.store(); // TODO: unnecessary here?
await wallet.init(createWalletResult.wi.address);
return wallet;
}
@ -147,14 +122,13 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
await wallet.connectToNode(node: Node());
final createWalletResult = await wallet.loadWallet(path, password);
await _parseCreateWalletResult(createWalletResult, wallet);
//await wallet.store(); // TODO: unnecessary here?
await wallet.init(createWalletResult.wi.address);
return wallet;
}
static Future<void> _parseCreateWalletResult(CreateWalletResult result, ZanoWallet wallet) async {
wallet.hWallet = result.walletId;
_info('setting hWallet = ${result.walletId}');
ZanoWalletApi.info('setting hWallet = ${result.walletId}');
wallet.walletAddresses.address = result.wi.address;
for (final item in result.wi.balances) {
if (item.assetInfo.assetId == zanoAssetId) {
@ -176,8 +150,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
void close() {
closeWallet();
_updateSyncInfoTimer?.cancel();
//_listener?.stop();
// _onAccountChangeReaction?.reaction.dispose();
_autoSaveTimer?.cancel();
}
@ -186,13 +158,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
syncStatus = ConnectingSyncStatus();
await setupNode();
syncStatus = ConnectedSyncStatus();
// if (await setupNode() == false) {
// syncStatus = FailedSyncStatus();
// // TODO: what's going on?
// //throw 'error connecting to zano node';
// } else {
// syncStatus = ConnectedSyncStatus();
// }
}
@override
@ -258,9 +223,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
)
];
}
destinations.forEach((destination) {
debugPrint('destination ${destination.address} ${destination.amount} ${destination.assetId}');
});
return PendingZanoTransaction(
zanoWallet: this,
destinations: destinations,
@ -278,12 +240,8 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
try {
final transfers = <Transfer>[];
late GetRecentTxsAndInfoResult result;
bool first = true;
do {
result = await getRecentTxsAndInfo(offset: _lastTxIndex, count: _txChunkSize);
// TODO: remove this, just for debug purposes
if (first && result.transfers.isEmpty) return {};
first = false;
_lastTxIndex += result.transfers.length;
transfers.addAll(result.transfers);
} while (result.lastItemIndex + 1 < result.totalTransfers);
@ -295,18 +253,10 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
}
Future<void> init(String address) async {
final boxName = '${walletInfo.name.replaceAll(' ', '_')}_${ZanoAsset.zanoAssetsBoxName}';
/*zanoAssetsBox = await CakeHive.openBox<ZanoAsset>(boxName);
print(
'assets in box total: ${zanoAssetsBox.length} ${zanoAssetsBox.values} active: ${zanoAssetsBox.values.where((element) => element.enabled).length} ${zanoAssetsBox.values.where((element) => element.enabled)}');
for (final asset in zanoAssetsBox.values) {
if (asset.enabled) balance[asset] = ZanoBalance.empty(decimalPoint: asset.decimalPoint);
}*/
await walletAddresses.init();
await walletAddresses.updateAddress(address);
//_setListeners();
await updateTransactions();
_autoSaveTimer = Timer.periodic(Duration(seconds: _autoSaveInterval), (_) async => await save());
_autoSaveTimer = Timer.periodic(Duration(seconds: _autoSaveIntervalSeconds), (_) async => await save());
}
@override
@ -335,19 +285,7 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
@override
Future<void> rescan({required int height}) => throw UnimplementedError();
/*@override
Future<void> rescan({required int height}) async {
walletInfo.restoreHeight = height;
walletInfo.isRecovery = true;
debugPrint('setRefreshFromBlockHeight height $height');
debugPrint('rescanBlockchainAsync');
await startSync();
/**walletAddresses.accountList.update();*/
await _askForUpdateTransactionHistory();
await save();
await walletInfo.save();
}*/
@override
Future<void> save() async {
try {
@ -358,21 +296,13 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
}
}
int _counter = 0;
bool _sent = false;
@override
Future<void> startSync() async {
try {
syncStatus = AttemptingSyncStatus();
_cachedBlockchainHeight = 0;
_lastKnownBlockHeight = 0;
_initialSyncHeight = 0;
_updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: /*1200*/ 5000), (_) async {
/*if (isNewTransactionExist()) {
onNewTransaction?.call();
}*/
_updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: _pollIntervalMilliseconds), (_) async {
final walletStatus = await getWalletStatus();
currentDaemonHeight = walletStatus.currentDaemonHeight;
_updateSyncProgress(walletStatus);
@ -388,82 +318,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
publicViewKey: walletInfo.wiExtended.viewPublicKey,
);
/*bool areSetsEqual<T>(Set<T> set1, Set<T> set2) => set1.length == set2.length && set1.every(set2.contains);
Set<String> getSetFromWhitelist(List<ZanoAsset> whitelist, bool isInGlobalWhitelist) =>
whitelist.where((item) => item.isInGlobalWhitelist == isInGlobalWhitelist).map((item) => item.assetId).toSet();
bool areWhitelistsTheSame(List<ZanoAsset> whitelist1, List<ZanoAsset> whitelist2) {
return areSetsEqual(getSetFromWhitelist(whitelist1, true), getSetFromWhitelist(whitelist2, true)) &&
areSetsEqual(getSetFromWhitelist(whitelist1, false), getSetFromWhitelist(whitelist2, false));
}*/
/*void addOrUpdateBalance(ZanoAsset asset, Balance? _balance) {
if (balance.keys.any((element) => element is ZanoAsset && element.assetId == asset.assetId)) {
balance[balance.keys.firstWhere((element) => element is ZanoAsset && element.assetId == asset.assetId)] = _balance == null
? ZanoBalance.empty(decimalPoint: asset.decimalPoint)
: ZanoBalance(total: _balance.total, unlocked: _balance.unlocked, decimalPoint: asset.decimalPoint);
} else {
balance[asset] = _balance == null
? ZanoBalance.empty(decimalPoint: asset.decimalPoint)
: ZanoBalance(total: _balance.total, unlocked: _balance.unlocked, decimalPoint: asset.decimalPoint);
}
}*/
/*final whitelistsFromServer = await getAssetsWhitelist();
void loadWhitelists() {
debugPrint('loadWhitelists');
final globalWhitelist = whitelistsFromServer.where((item) => item.isInGlobalWhitelist);
final globalWhitelistIds = globalWhitelist.map((item) => item.assetId).toSet();
final localWhitelist = whitelistsFromServer.where((item) => !item.isInGlobalWhitelist && !globalWhitelistIds.contains(item.assetId));
for (final asset in globalWhitelist.followedBy(localWhitelist)) {
// we have two options:
// 1. adding as active (enabled) and adding to balance (even there's no balance for this asset)
// 2. checking if there's a balance, then setting enabled true or false
bool firstOption = 1 == 0;
if (firstOption) {
asset.enabled = true;
zanoAssetsBox.put(asset.assetId, ZanoAsset.copyWith(asset, _getIconPath(asset.title), enabled: true));
addOrUpdateBalance(asset, walletInfo.wi.balances.firstWhereOrNull((item) => item.assetId == asset.assetId));
} else {
final _balance = walletInfo.wi.balances.firstWhereOrNull((item) => item.assetId == asset.assetId);
zanoAssetsBox.put(asset.assetId, ZanoAsset.copyWith(asset, _getIconPath(asset.title), enabled: _balance != null));
addOrUpdateBalance(asset, _balance);
}
}
}
if (this.whitelists.isEmpty) {
if (zanoAssetsBox.isEmpty) loadWhitelists();
this.whitelists = whitelistsFromServer;
} else if (!areWhitelistsTheSame(whitelistsFromServer, this.whitelists)) {
// // updating whitelists from server
// if (zanoAssetsBox.isEmpty) {
// debugPrint('first loading of whitelists');
// loadWhitelists();
// } else {
// debugPrint('later updating of whitelists');
// }
debugPrint('whitelists changed!');
if (zanoAssetsBox.isEmpty) loadWhitelists();
this.whitelists = whitelistsFromServer;
}
// TODO: here should be synchronization of whitelists
// for (final item in whitelists) {
// if (!zanoAssets.containsKey(item.assetId)) zanoAssets[item.assetId] = item;
// }
// // removing assets missing in whitelists (in case some were removed since last time)
// zanoAssets.removeWhere((key, _) => !whitelists.any((element) => element.assetId == key));
for (final asset in balance.keys) {
if (asset == CryptoCurrency.zano) {
final _balance = walletInfo.wi.balances.firstWhere((element) => element.assetId == zanoAssetId);
balance[asset] = ZanoBalance(total: _balance.total, unlocked: _balance.unlocked);
} else if (asset is ZanoAsset) {
addOrUpdateBalance(asset, walletInfo.wi.balances.firstWhereOrNull((element) => element.assetId == asset.assetId));
}
}
*/
final assets = await getAssetsWhitelist();
zanoAssets = {};
for (final asset in assets) {
@ -480,7 +334,7 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
} else {
final asset = zanoAssets[b.assetId];
if (asset == null) {
debugPrint('balance for an unknown asset ${b.assetInfo.assetId}');
ZanoWalletApi.error('balance for an unknown asset ${b.assetInfo.assetId}');
continue;
}
if (balance.keys.any((element) => element is ZanoAsset && element.assetId == b.assetInfo.assetId)) {
@ -491,26 +345,10 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
}
}
}
// removing balances for assets missing in wallet info balances (in case they were removed for some reason)
// removing balances for assets missing in wallet info balances
balance.removeWhere(
(key, _) => key != CryptoCurrency.zano && !walletInfo.wi.balances.any((element) => element.assetId == (key as ZanoAsset).assetId),
);
if (_counter++ % 10 == 0 && !_sent) {
final fee = BigInt.from(calculateEstimatedFee(MoneroTransactionPriority.fastest));
final leo8 = 'ZxD9oVwGwW6ULix9Pqttnr7JDpaoLvDVA1KJ9eA9KRxPMRZT5X7WwtU94XH1Z6q6XTMxNbHmbV2xfZ429XxV6fST2DxEg4BQV';
final ct = 'cc4e69455e63f4a581257382191de6856c2156630b3fba0db4bdd73ffcfb36b6';
final test = '62af227aa643dd10a71c7f00a9d873006c0c0de3d59196e8c64cec0810bd874a';
final bbq = 'bb9590162509f956ff79851fb1bc0ced6646f5d5ba7eae847a9f21c92c39437c';
final destinations = <Destination>[
Destination(amount: BigInt.from(55.6677 * pow(10, 12)), address: leo8, assetId: ct),
Destination(amount: BigInt.from(555 * pow(10, 10)), address: leo8, assetId: test),
Destination(amount: BigInt.from(111 * pow(10, 10)), address: leo8, assetId: bbq),
Destination(amount: BigInt.from(333 * pow(10, 12)), address: leo8, assetId: zanoAssetId),
];
//await transfer(destinations, fee, 'new 4 destinations');
_sent = true;
}
}
});
} catch (e) {
@ -564,48 +402,30 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
if (asset.enabled) {
final assetDescriptor = await addAssetsWhitelist(asset.assetId);
if (assetDescriptor == null) {
print('error adding zano asset');
print('Error adding zano asset');
}
//balance[asset] = ZanoBalance.empty(decimalPoint: asset.decimalPoint);
} else {
final result = await removeAssetsWhitelist(asset.assetId);
if (result == false) {
print('error removing zano asset');
print('Error removing zano asset');
}
//balance.removeWhere((key, _) => key is ZanoAsset && key.assetId == asset.assetId);
}
}
Future<void> deleteZanoAsset(ZanoAsset asset) async {
final result = await removeAssetsWhitelist(asset.assetId);
//if (result == false) return;
//if (asset.isInBox) await asset.delete();
//balance.removeWhere((key, _) => key is ZanoAsset && key.assetId == asset.assetId);
final _ = await removeAssetsWhitelist(asset.assetId);
}
Future<ZanoAsset?> getZanoAsset(String assetId) async {
return await getAssetInfo(assetId);
}
// List<ZanoTransactionInfo> _getAllTransactions(dynamic _) =>
// zano_transaction_history
// .getAllTransations()
// .map((row) => ZanoTransactionInfo.fromRow(row))
// .toList();
// void _setListeners() {
// _listener?.stop();
// _listener = zano_wallet.setListeners(_onNewBlock, _onNewTransaction);
// }
Future<void> _askForUpdateTransactionHistory() async => await updateTransactions();
void _onNewBlock(int height, int blocksLeft, double ptc) async {
try {
if (blocksLeft < 1000) {
// TODO: we can't update transactions history before loading all balances and whitelists
await _askForUpdateTransactionHistory();
/*walletAddresses.accountList.update();*/
syncStatus = SyncedSyncStatus();
if (!_hasSyncAfterStartup) {
@ -620,15 +440,6 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
}
}
void _onNewTransaction() async {
try {
await _askForUpdateTransactionHistory();
await Future<void>.delayed(Duration(seconds: 1)); // TODO: ???
} catch (e) {
print(e.toString());
}
}
void _updateSyncProgress(GetWalletStatusResult walletStatus) {
final syncHeight = walletStatus.currentWalletHeight;
if (_initialSyncHeight <= 0) {
@ -654,7 +465,4 @@ abstract class ZanoWalletBase extends WalletBase<ZanoBalance, ZanoTransactionHis
_onNewBlock.call(syncHeight, left, ptc);
}
static void _info(String s) {
debugPrint('[info] $s');
}
}

View file

@ -1,5 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'package:cw_core/transaction_priority.dart';
import 'package:cw_zano/api/api_calls.dart';
@ -14,21 +13,18 @@ import 'package:cw_zano/api/model/get_wallet_info_result.dart';
import 'package:cw_zano/api/model/get_wallet_status_result.dart';
import 'package:cw_zano/api/model/proxy_to_daemon_params.dart';
import 'package:cw_zano/api/model/proxy_to_daemon_result.dart';
import 'package:cw_zano/api/model/store_result.dart';
import 'package:cw_zano/api/model/transfer.dart';
import 'package:cw_zano/api/model/transfer_params.dart';
import 'package:cw_zano/api/model/transfer_result.dart';
import 'package:cw_zano/model/zano_asset.dart';
import 'package:cw_zano/zano_wallet_exceptions.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'api/model/store_result.dart';
mixin ZanoWalletApi {
static const _defaultNodeUri = '195.201.107.230:33336';
static const _statusDelivered = 'delivered';
static const _maxAttempts = 10;
static const _maxInvokeAttempts = 10;
static const _logInfo = true;
static const _logError = true;
static const _logJson = false;
@ -47,13 +43,13 @@ mixin ZanoWalletApi {
void setPassword(String password) => ApiCalls.setPassword(hWallet: hWallet, password: password);
void closeWallet([int? walletToClose]) {
_info('close_wallet ${walletToClose ?? hWallet}');
info('close_wallet ${walletToClose ?? hWallet}');
final result = ApiCalls.closeWallet(hWallet: walletToClose ?? hWallet);
_info('close_wallet result $result');
info('close_wallet result $result');
}
Future<bool> setupNode() async {
_info('init $_defaultNodeUri');
info('init $_defaultNodeUri');
final result = ApiCalls.setupNode(
address: _defaultNodeUri,
login: '',
@ -61,43 +57,43 @@ mixin ZanoWalletApi {
useSSL: false,
isLightWallet: false,
);
_info('init result $result');
info('init result $result');
return result;
}
Future<GetWalletInfoResult> getWalletInfo() async {
final json = ApiCalls.getWalletInfo(hWallet);
final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
if (_logJson) debugPrint('get_wallet_info $json');
await _writeLog('get_wallet_info', 'get_wallet_info result $json');
_info('get_wallet_info got ${result.wi.balances.length} balances: ${result.wi.balances} seed: ${_shorten(result.wiExtended.seed)}');
_json('get_wallet_info', json);
//await _writeLog('get_wallet_info', 'get_wallet_info result $json');
info('get_wallet_info got ${result.wi.balances.length} balances: ${result.wi.balances} seed: ${_shorten(result.wiExtended.seed)}');
return result;
}
Future<GetWalletStatusResult> getWalletStatus() async {
final json = ApiCalls.getWalletStatus(hWallet: hWallet);
if (json == Consts.errorWalletWrongId) {
print('wrong wallet id');
error('wrong wallet id');
throw ZanoWalletException('Wrong wallet id');
}
final status = GetWalletStatusResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
if (_logJson) debugPrint('get_wallet_status $json');
await _writeLog('get_wallet_status', 'get_wallet_status result $json');
_json('get_wallet_status', json);
//await _writeLog('get_wallet_status', 'get_wallet_status result $json');
if (_logInfo)
_info(
info(
'get_wallet_status connected: ${status.isDaemonConnected} in refresh: ${status.isInLongRefresh} progress: ${status.progress} wallet state: ${status.walletState}');
return status;
}
Future<String> invokeMethod(String methodName, Object params) async {
await _writeLog(methodName, 'invoke method $methodName params: ${jsonEncode(params)} hWallet: $hWallet');
//await _writeLog(methodName, 'invoke method $methodName params: ${jsonEncode(params)} hWallet: $hWallet');
var invokeResult =
ApiCalls.asyncCall(methodName: 'invoke', hWallet: hWallet, params: '{"method": "$methodName","params": ${jsonEncode(params)}}');
Map<String, dynamic> map;
try {
map = jsonDecode(invokeResult) as Map<String, dynamic>;
} catch (e) {
debugPrint('exception in parsing json in invokeMethod: $invokeResult');
error('exception in parsing json in invokeMethod: $invokeResult');
rethrow;
}
int attempts = 0;
@ -109,23 +105,23 @@ mixin ZanoWalletApi {
try {
map = jsonDecode(result) as Map<String, dynamic>;
} catch (e) {
debugPrint('exception in parsing json in invokeMethod: $result');
error('exception in parsing json in invokeMethod: $result');
rethrow;
}
if (map['status'] != null && map['status'] == _statusDelivered && map['result'] != null) {
await _writeLog(methodName, 'invoke method $methodName result $result');
//await _writeLog(methodName, 'invoke method $methodName result $result');
return result;
}
} while (++attempts < _maxAttempts);
} while (++attempts < _maxInvokeAttempts);
}
await _writeLog(methodName, 'invoke method $methodName result: $invokeResult');
//await _writeLog(methodName, 'invoke method $methodName result: $invokeResult');
return invokeResult;
}
Future<List<ZanoAsset>> getAssetsWhitelist() async {
try {
final json = await invokeMethod('assets_whitelist_get', '{}');
if (_logJson) debugPrint('assets_whitelist_get $json');
_json('assets_whitelist_get', json);
final map = jsonDecode(json) as Map<String, dynamic>?;
_checkForErrors(map);
List<ZanoAsset> assets(String type, bool isGlobalWhitelist) =>
@ -137,12 +133,12 @@ mixin ZanoWalletApi {
final globalWhitelist = assets('global_whitelist', true);
final ownAssets = assets('own_assets', false);
if (_logInfo)
_info('assets_whitelist_get got local whitelist: ${localWhitelist.length} ($localWhitelist); '
info('assets_whitelist_get got local whitelist: ${localWhitelist.length} ($localWhitelist); '
'global whitelist: ${globalWhitelist.length} ($globalWhitelist); '
'own assets: ${ownAssets.length} ($ownAssets)');
return [...globalWhitelist, ...localWhitelist, ...ownAssets];
} catch (e) {
print('[error] assets_whitelist_get $e');
error('assets_whitelist_get $e');
return [];
}
}
@ -150,19 +146,19 @@ mixin ZanoWalletApi {
Future<ZanoAsset?> addAssetsWhitelist(String assetId) async {
try {
final json = await invokeMethod('assets_whitelist_add', AssetIdParams(assetId: assetId));
if (_logJson) print('assets_whitelist_add $assetId $json');
_json('assets_whitelist_add $assetId', json);
final map = jsonDecode(json) as Map<String, dynamic>?;
_checkForErrors(map);
if (map!['result']!['result']!['status']! == 'OK') {
final assetDescriptor = ZanoAsset.fromJson(map['result']!['result']!['asset_descriptor']! as Map<String, dynamic>);
_info('assets_whitelist_add added ${assetDescriptor.fullName} ${assetDescriptor.ticker}');
info('assets_whitelist_add added ${assetDescriptor.fullName} ${assetDescriptor.ticker}');
return assetDescriptor;
} else {
_info('assets_whitelist_add status ${map['result']!['result']!['status']!}');
info('assets_whitelist_add status ${map['result']!['result']!['status']!}');
return null;
}
} catch (e) {
print('[error] assets_whitelist_add $e');
error('assets_whitelist_add $e');
return null;
}
}
@ -170,13 +166,13 @@ mixin ZanoWalletApi {
Future<bool> removeAssetsWhitelist(String assetId) async {
try {
final json = await invokeMethod('assets_whitelist_remove', AssetIdParams(assetId: assetId));
if (_logJson) print('assets_whitelist_remove $assetId $json');
_json('assets_whitelist_remove $assetId', json);
final map = jsonDecode(json) as Map<String, dynamic>?;
_checkForErrors(map);
_info('assets_whitelist_remove status ${map!['result']!['result']!['status']!}');
return (map!['result']!['result']!['status']! == 'OK');
info('assets_whitelist_remove status ${map!['result']!['result']!['status']!}');
return (map['result']!['result']!['status']! == 'OK');
} catch (e) {
print('[error] assets_whitelist_remove $e');
error('assets_whitelist_remove $e');
return false;
}
}
@ -192,21 +188,21 @@ mixin ZanoWalletApi {
final methodName = 'get_asset_info';
final params = AssetIdParams(assetId: assetId);
final result = await _proxyToDaemon('/json_rpc', '{"method": "$methodName","params": ${jsonEncode(params)}}');
if (_logJson) print('$methodName $assetId ${result?.body}');
_json('$methodName $assetId', result?.body ?? '');
if (result == null) {
debugPrint('get_asset_info empty result');
return null;
}
final map = jsonDecode(result.body) as Map<String, dynamic>?;
if (map!['error'] != null) {
_info('get_asset_info $assetId error ${map['error']!['code']} ${map['error']!['message']}');
info('get_asset_info $assetId error ${map['error']!['code']} ${map['error']!['message']}');
return null;
} else if (map['result']!['status']! == 'OK') {
final assetDescriptor = ZanoAsset.fromJson(map['result']!['asset_descriptor']! as Map<String, dynamic>);
_info('get_asset_info $assetId ${assetDescriptor.fullName} ${assetDescriptor.ticker}');
info('get_asset_info $assetId ${assetDescriptor.fullName} ${assetDescriptor.ticker}');
return assetDescriptor;
} else {
_info('get_asset_info $assetId status ${map['result']!['status']!}');
info('get_asset_info $assetId status ${map['result']!['status']!}');
return null;
}
}
@ -218,33 +214,33 @@ mixin ZanoWalletApi {
_checkForErrors(map);
return StoreResult.fromJson(map!['result']['result'] as Map<String, dynamic>);
} catch (e) {
print('[error] store $e');
error('store $e');
return null;
}
}
Future<GetRecentTxsAndInfoResult> getRecentTxsAndInfo({required int offset, required int count}) async {
_info('get_recent_txs_and_info $offset $count');
info('get_recent_txs_and_info $offset $count');
try {
final json = await invokeMethod('get_recent_txs_and_info', GetRecentTxsAndInfoParams(offset: offset, count: count));
if (_logJson) debugPrint('get_recent_txs_and_info $json');
_json('get_recent_txs_and_info', json);
final map = jsonDecode(json) as Map<String, dynamic>?;
_checkForErrors(map);
final lastItemIndex = map?['result']?['result']?['last_item_index'] as int?;
final totalTransfers = map?['result']?['result']?['total_transfers'] as int?;
final transfers = map?['result']?['result']?['transfers'] as List<dynamic>?;
if (transfers == null || lastItemIndex == null || totalTransfers == null) {
_error('get_recent_txs_and_info empty transfers');
error('get_recent_txs_and_info empty transfers');
return GetRecentTxsAndInfoResult.empty();
}
_info('get_recent_txs_and_info transfers.length: ${transfers.length}');
info('get_recent_txs_and_info transfers.length: ${transfers.length}');
return GetRecentTxsAndInfoResult(
transfers: transfers.map((e) => Transfer.fromJson(e as Map<String, dynamic>)).toList(),
lastItemIndex: lastItemIndex,
totalTransfers: totalTransfers,
);
} catch (e) {
_error('get_recent_txs_and_info $e');
error('get_recent_txs_and_info $e');
return GetRecentTxsAndInfoResult.empty();
}
}
@ -256,11 +252,11 @@ mixin ZanoWalletApi {
String _shorten(String s) => s.length > 10 ? '${s.substring(0, 4)}...${s.substring(s.length - 4)}' : s;
Future<CreateWalletResult> createWallet(String path, String password) async {
_info('create_wallet path $path password ${_shorten(password)}');
await _writeLog('create_wallet', 'create_wallet path $path password ${_shorten(password)}');
info('create_wallet path $path password ${_shorten(password)}');
//await _writeLog('create_wallet', 'create_wallet path $path password ${_shorten(password)}');
final json = ApiCalls.createWallet(path: path, password: password);
if (_logJson) debugPrint('create_wallet $json');
await _writeLog('create_wallet', 'create_wallet result $json');
_json('create_wallet', json);
//await _writeLog('create_wallet', 'create_wallet result $json');
final map = jsonDecode(json) as Map<String, dynamic>?;
if (map?['error'] != null) {
final code = map!['error']!['code'] ?? '';
@ -271,16 +267,16 @@ mixin ZanoWalletApi {
throw ZanoWalletException('Error creating wallet file, empty response');
}
final result = CreateWalletResult.fromJson(map!['result'] as Map<String, dynamic>);
_info('create_wallet ${result.name} ${result.seed}');
info('create_wallet ${result.name} ${result.seed}');
return result;
}
Future<CreateWalletResult> restoreWalletFromSeed(String path, String password, String seed) async {
_info('restore_wallet path $path password ${_shorten(password)} seed ${_shorten(seed)}');
await _writeLog('restore_wallet', 'restore_wallet path $path password ${_shorten(password)} seed ${_shorten(seed)}');
info('restore_wallet path $path password ${_shorten(password)} seed ${_shorten(seed)}');
//await _writeLog('restore_wallet', 'restore_wallet path $path password ${_shorten(password)} seed ${_shorten(seed)}');
final json = ApiCalls.restoreWalletFromSeed(path: path, password: password, seed: seed);
if (_logJson) debugPrint('restore_wallet $json');
await _writeLog('restore_wallet', 'restore_wallet result $json');
_json('restore_wallet', json);
//await _writeLog('restore_wallet', 'restore_wallet result $json');
final map = jsonDecode(json) as Map<String, dynamic>?;
if (map?['error'] != null) {
final code = map!['error']!['code'] ?? '';
@ -296,23 +292,23 @@ mixin ZanoWalletApi {
throw RestoreFromKeysException('Error restoring wallet, empty response');
}
final result = CreateWalletResult.fromJson(map!['result'] as Map<String, dynamic>);
_info('restore_wallet ${result.name} ${result.wi.address}');
info('restore_wallet ${result.name} ${result.wi.address}');
return result;
}
Future<CreateWalletResult> loadWallet(String path, String password, [int attempt = 0]) async {
_info('load_wallet path $path password ${_shorten(password)}');
await _writeLog('load_wallet', 'load_wallet path $path password ${_shorten(password)}');
info('load_wallet path $path password ${_shorten(password)}');
//await _writeLog('load_wallet', 'load_wallet path $path password ${_shorten(password)}');
final json = ApiCalls.loadWallet(path: path, password: password);
if (_logJson) debugPrint('load_wallet $json');
await _writeLog('load_wallet', 'load_wallet result $json');
_json('load_wallet', json);
//await _writeLog('load_wallet', 'load_wallet result $json');
final map = jsonDecode(json) as Map<String, dynamic>?;
if (map?['error'] != null) {
final code = map?['error']!['code'] ?? '';
final message = map?['error']!['message'] ?? '';
if (code == Consts.errorAlreadyExists && attempt < 5) {
if (code == Consts.errorAlreadyExists && attempt <= 5) {
// already connected to this wallet. closing and trying to reopen
_info('already connected. closing and reopen wallet (attempt $attempt)');
info('already connected. closing and reopen wallet (attempt $attempt)');
closeWallet(attempt);
await Future.delayed(const Duration(milliseconds: 500));
return await loadWallet(path, password, attempt + 1);
@ -323,7 +319,7 @@ mixin ZanoWalletApi {
throw ZanoWalletException('Error loading wallet, empty response');
}
final result = CreateWalletResult.fromJson(map!['result'] as Map<String, dynamic>);
_info('load_wallet ${result.name} ${result.wi.address}');
info('load_wallet ${result.name} ${result.wi.address}');
return result;
}
@ -338,14 +334,14 @@ mixin ZanoWalletApi {
hideReceiver: true,
);
final json = await invokeMethod('transfer', params);
if (_logJson) debugPrint('transfer $json');
_json('transfer', json);
final map = jsonDecode(json);
final resultMap = map['result'] as Map<String, dynamic>?;
if (resultMap != null) {
final transferResultMap = resultMap['result'] as Map<String, dynamic>?;
if (transferResultMap != null) {
final transferResult = TransferResult.fromJson(transferResultMap);
debugPrint('transfer success hash ${transferResult.txHash}');
info('transfer success hash ${transferResult.txHash}');
return transferResult;
} else {
final errorCode = resultMap['error']['code'];
@ -374,7 +370,7 @@ mixin ZanoWalletApi {
}
}
Future<void> _writeLog(String method, String logMessage) async {
/*Future<void> _writeLog(String method, String logMessage) async {
final dir = await getDownloadsDirectory();
final logFile = File('${dir!.path}/$method.txt');
final date = DateTime.now();
@ -382,13 +378,9 @@ mixin ZanoWalletApi {
String removeCRandLF(String input) => input.replaceAll(RegExp('\r|\n'), '');
await logFile.writeAsString('${twoDigits(date.hour)}:${twoDigits(date.minute)}:${twoDigits(date.second)} ${removeCRandLF(logMessage)}\n',
mode: FileMode.append);
RegExp regExp = RegExp(r'"fee":\s*(\d+(?:\.\d+)?)');
final matches = regExp.allMatches(logMessage);
if (matches.isNotEmpty) {
await logFile.writeAsString(' ' + matches.map((element) => '${element.group(0)}').join(', ') + '\n', mode: FileMode.append);
}
}
}*/
static void _info(String s) => _logInfo ? debugPrint('[info] $s') : null;
static void _error(String s) => _logError ? debugPrint('[error] $s') : null;
static void info(String s) => _logInfo ? debugPrint('[info] $s') : null;
static void error(String s) => _logError ? debugPrint('[error] $s') : null;
static void _json(String methodName, String json) => _logJson ? debugPrint('$methodName $json') : null;
}

View file

@ -53,7 +53,7 @@ class ZanoWalletService extends WalletService<ZanoNewWalletCredentials, ZanoRest
@override
Future<ZanoWallet> create(WalletCredentials credentials, {bool? isTestnet}) async {
print('zanowallet service create isTestnet $isTestnet'); // TODO: remove
print('zanowallet service create isTestnet $isTestnet');
return await ZanoWalletBase.create(credentials: credentials);
}