diff --git a/cw_zano/lib/api/api_calls.dart b/cw_zano/lib/api/api_calls.dart index ac740a4cf..08f8a96d1 100644 --- a/cw_zano/lib/api/api_calls.dart +++ b/cw_zano/lib/api/api_calls.dart @@ -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'; diff --git a/cw_zano/lib/api/model/transfer.dart b/cw_zano/lib/api/model/transfer.dart index e5d7887ac..c91350268 100644 --- a/cw_zano/lib/api/model/transfer.dart +++ b/cw_zano/lib/api/model/transfer.dart @@ -48,33 +48,31 @@ class Transfer { factory Transfer.fromJson(Map json) => Transfer( comment: json['comment'] as String? ?? '', - employedEntries: EmployedEntries.fromJson( - json['employed_entries'] as Map? ?? {}), + employedEntries: EmployedEntries.fromJson(json['employed_entries'] as Map? ?? {}), 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).cast(), + remoteAddresses: json['remote_addresses'] == null ? [] : (json['remote_addresses'] as List).cast(), remoteAliases: json['remote_aliases'] == null ? [] : (json['remote_aliases'] as List).cast(), showSender: json['show_sender'] as bool? ?? false, - subtransfers: (json['subtransfers'] as List? ?? []) - .map((e) => Subtransfer.fromJson(e as Map)) - .toList(), + subtransfers: (json['subtransfers'] as List? ?? []).map((e) => Subtransfer.fromJson(e as Map)).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 makeMap(List transfers, Map zanoAssets, int currentDaemonHeight) => Map.fromIterable( + static Map makeMap(List transfers, Map 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]; diff --git a/cw_zano/lib/model/zano_asset.dart b/cw_zano/lib/model/zano_asset.dart index 902a391ca..9cee2b4c7 100644 --- a/cw_zano/lib/model/zano_asset.dart +++ b/cw_zano/lib/model/zano_asset.dart @@ -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, diff --git a/cw_zano/lib/model/zano_transaction_credentials.dart b/cw_zano/lib/model/zano_transaction_credentials.dart index d7a491b6a..dbbfe53c6 100644 --- a/cw_zano/lib/model/zano_transaction_credentials.dart +++ b/cw_zano/lib/model/zano_transaction_credentials.dart @@ -8,5 +8,4 @@ class ZanoTransactionCredentials { final List outputs; final MoneroTransactionPriority priority; final CryptoCurrency currency; - //final String assetType; } diff --git a/cw_zano/lib/zano_wallet.dart b/cw_zano/lib/zano_wallet.dart index 39bfd6171..3568a7a96 100644 --- a/cw_zano/lib/zano_wallet.dart +++ b/cw_zano/lib/zano_wallet.dart @@ -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 with Store, ZanoWalletApi { - static const int _autoSaveInterval = 30; + static const int _autoSaveIntervalSeconds = 30; + static const int _pollIntervalMilliseconds = 2000; - //List transfers = []; @override ZanoWalletAddresses walletAddresses; @@ -66,18 +58,11 @@ abstract class ZanoWalletBase extends WalletBase zanoAssetsBox; - List whitelists = []; - List get zanoAssets => zanoAssetsBox.values.toList(); - */ + Map 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 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 _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[]; 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 init(String address) async { - final boxName = '${walletInfo.name.replaceAll(' ', '_')}_${ZanoAsset.zanoAssetsBoxName}'; - /*zanoAssetsBox = await CakeHive.openBox(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 rescan({required int height}) => throw UnimplementedError(); - /*@override - Future 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 save() async { try { @@ -358,21 +296,13 @@ abstract class ZanoWalletBase extends WalletBase 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(Set set1, Set set2) => set1.length == set2.length && set1.every(set2.contains); - - Set getSetFromWhitelist(List whitelist, bool isInGlobalWhitelist) => - whitelist.where((item) => item.isInGlobalWhitelist == isInGlobalWhitelist).map((item) => item.assetId).toSet(); - bool areWhitelistsTheSame(List whitelist1, List 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 element is ZanoAsset && element.assetId == b.assetInfo.assetId)) { @@ -491,26 +345,10 @@ abstract class ZanoWalletBase extends WalletBase 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(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 key is ZanoAsset && key.assetId == asset.assetId); } } Future 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 getZanoAsset(String assetId) async { return await getAssetInfo(assetId); } - // List _getAllTransactions(dynamic _) => - // zano_transaction_history - // .getAllTransations() - // .map((row) => ZanoTransactionInfo.fromRow(row)) - // .toList(); - - // void _setListeners() { - // _listener?.stop(); - // _listener = zano_wallet.setListeners(_onNewBlock, _onNewTransaction); - // } - Future _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.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 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 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 getWalletInfo() async { final json = ApiCalls.getWalletInfo(hWallet); final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map); - 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 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); - 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 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 map; try { map = jsonDecode(invokeResult) as Map; } 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; } 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> 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?; _checkForErrors(map); List 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 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?; _checkForErrors(map); if (map!['result']!['result']!['status']! == 'OK') { final assetDescriptor = ZanoAsset.fromJson(map['result']!['result']!['asset_descriptor']! as Map); - _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 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?; _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?; 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); - _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); } catch (e) { - print('[error] store $e'); + error('store $e'); return null; } } Future 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?; _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?; 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)).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 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?; 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); - _info('create_wallet ${result.name} ${result.seed}'); + info('create_wallet ${result.name} ${result.seed}'); return result; } Future 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?; 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); - _info('restore_wallet ${result.name} ${result.wi.address}'); + info('restore_wallet ${result.name} ${result.wi.address}'); return result; } Future 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?; 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); - _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?; if (resultMap != null) { final transferResultMap = resultMap['result'] as Map?; 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 _writeLog(String method, String logMessage) async { + /*Future _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; } diff --git a/cw_zano/lib/zano_wallet_service.dart b/cw_zano/lib/zano_wallet_service.dart index 68d2772a5..78102812e 100644 --- a/cw_zano/lib/zano_wallet_service.dart +++ b/cw_zano/lib/zano_wallet_service.dart @@ -53,7 +53,7 @@ class ZanoWalletService extends WalletService 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); }