From 23485a4bab30387dced863f2ce27fb05513bdb57 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 10 Mar 2024 02:51:30 +0000 Subject: [PATCH] fixed syncing sync status, decimal division, safe null json parsing --- cw_core/lib/amount_converter.dart | 12 +- cw_core/pubspec.lock | 16 ++ cw_core/pubspec.yaml | 1 + cw_zano/lib/api/model/asset_info.dart | 16 +- cw_zano/lib/api/model/balance.dart | 10 +- .../lib/api/model/create_wallet_result.dart | 18 +- cw_zano/lib/api/model/destination.dart | 12 +- .../api/model/get_address_info_result.dart | 8 +- cw_zano/lib/api/model/history.dart | 32 ++-- cw_zano/lib/api/model/receive.dart | 6 +- cw_zano/lib/api/model/recent_history.dart | 4 +- cw_zano/lib/api/model/store_result.dart | 2 +- cw_zano/lib/api/model/subtransfer.dart | 6 +- cw_zano/lib/api/model/transfer_params.dart | 18 +- cw_zano/lib/api/model/wi_extended.dart | 10 +- cw_zano/lib/api/zano_api.dart | 4 +- cw_zano/lib/zano_balance.dart | 6 +- cw_zano/lib/zano_wallet.dart | 179 +++++++++++------- cw_zano/lib/zano_wallet_service.dart | 23 ++- 19 files changed, 232 insertions(+), 151 deletions(-) diff --git a/cw_core/lib/amount_converter.dart b/cw_core/lib/amount_converter.dart index cdd79bc35..adf7532f6 100644 --- a/cw_core/lib/amount_converter.dart +++ b/cw_core/lib/amount_converter.dart @@ -1,5 +1,8 @@ +import 'package:decimal/decimal.dart'; +import 'package:decimal/intl.dart'; import 'package:intl/intl.dart'; import 'package:cw_core/crypto_currency.dart'; +import 'package:rational/rational.dart'; class AmountConverter { static const _moneroAmountLength = 12; @@ -97,7 +100,7 @@ class AmountConverter { case CryptoCurrency.xusd: return _moneroAmountToString(amount); case CryptoCurrency.zano: - return _moneroAmountToString(amount); + return _moneroAmountToStringUsingDecimals(amount); default: return ''; } @@ -106,9 +109,16 @@ class AmountConverter { static double cryptoAmountToDouble({required num amount, required num divider}) => amount / divider; + static Decimal cryptoAmountToDecimal({required int amount, required int divider}) => + (Decimal.fromInt(amount) / Decimal.fromInt(divider)).toDecimal(); + static String _moneroAmountToString(int amount) => _moneroAmountFormat.format( cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider)); + static String _moneroAmountToStringUsingDecimals(int amount) => _moneroAmountFormat.format( + DecimalIntl(cryptoAmountToDecimal(amount: amount, divider: _moneroAmountDivider))); + + static double _moneroAmountToDouble(int amount) => cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider); diff --git a/cw_core/pubspec.lock b/cw_core/pubspec.lock index e399526fd..d476f1751 100644 --- a/cw_core/pubspec.lock +++ b/cw_core/pubspec.lock @@ -177,6 +177,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.4" + decimal: + dependency: "direct main" + description: + name: decimal + sha256: "24a261d5d5c87e86c7651c417a5dbdf8bcd7080dd592533910e8d0505a279f21" + url: "https://pub.dev" + source: hosted + version: "2.3.3" encrypt: dependency: "direct main" description: @@ -507,6 +515,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.1" + rational: + dependency: transitive + description: + name: rational + sha256: ba58e9e18df9abde280e8b10051e4bce85091e41e8e7e411b6cde2e738d357cf + url: "https://pub.dev" + source: hosted + version: "2.2.2" shelf: dependency: transitive description: diff --git a/cw_core/pubspec.yaml b/cw_core/pubspec.yaml index 9dcb7eaba..1274e5aa2 100644 --- a/cw_core/pubspec.yaml +++ b/cw_core/pubspec.yaml @@ -19,6 +19,7 @@ dependencies: flutter_mobx: ^2.0.6+1 intl: ^0.18.0 encrypt: ^5.0.1 + decimal: ^2.3.3 dev_dependencies: flutter_test: diff --git a/cw_zano/lib/api/model/asset_info.dart b/cw_zano/lib/api/model/asset_info.dart index 0b7605a17..ddf1a1d2d 100644 --- a/cw_zano/lib/api/model/asset_info.dart +++ b/cw_zano/lib/api/model/asset_info.dart @@ -21,14 +21,14 @@ class AssetInfo { required this.totalMaxSupply}); factory AssetInfo.fromJson(Map json) => AssetInfo( - assetId: json['asset_id'] as String, - currentSupply: json['current_supply'] as int, - decimalPoint: json['decimal_point'] as int, - fullName: json['full_name'] as String, + assetId: json['asset_id'] as String? ?? '', + currentSupply: json['current_supply'] as int? ?? 0, + decimalPoint: json['decimal_point'] as int? ?? 0, + fullName: json['full_name'] as String? ?? '', hiddenSupply: json['hidden_supply'] as bool, - metaInfo: json['meta_info'] as String, - owner: json['owner'] as String, - ticker: json['ticker'] as String, - totalMaxSupply: json['total_max_supply'] as int, + metaInfo: json['meta_info'] as String? ?? '', + owner: json['owner'] as String? ?? '', + ticker: json['ticker'] as String? ?? '', + totalMaxSupply: json['total_max_supply'] as int? ?? 0, ); } diff --git a/cw_zano/lib/api/model/balance.dart b/cw_zano/lib/api/model/balance.dart index 8ff18d891..a46922a9b 100644 --- a/cw_zano/lib/api/model/balance.dart +++ b/cw_zano/lib/api/model/balance.dart @@ -16,10 +16,10 @@ class Balance { factory Balance.fromJson(Map json) => Balance( assetInfo: - AssetInfo.fromJson(json['asset_info'] as Map), - awaitingIn: json['awaiting_in'] as int, - awaitingOut: json['awaiting_out'] as int, - total: json['total'] as int, - unlocked: json['unlocked'] as int, + AssetInfo.fromJson(json['asset_info'] as Map? ?? {}), + awaitingIn: json['awaiting_in'] as int? ?? 0, + awaitingOut: json['awaiting_out'] as int? ?? 0, + total: json['total'] as int? ?? 0, + unlocked: json['unlocked'] as int? ?? 0, ); } diff --git a/cw_zano/lib/api/model/create_wallet_result.dart b/cw_zano/lib/api/model/create_wallet_result.dart index 7b07f9045..91b6fc00b 100644 --- a/cw_zano/lib/api/model/create_wallet_result.dart +++ b/cw_zano/lib/api/model/create_wallet_result.dart @@ -25,15 +25,15 @@ class CreateWalletResult { factory CreateWalletResult.fromJson(Map json) => CreateWalletResult( - name: json['name'] as String, - pass: json['pass'] as String, + name: json['name'] as String? ?? '', + pass: json['pass'] as String? ?? '', recentHistory: RecentHistory.fromJson( - json['recent_history'] as Map), - recovered: json['recovered'] as bool, - seed: json['seed'] as String, - walletFileSize: json['wallet_file_size'] as int, - walletId: json['wallet_id'] as int, - walletLocalBcSize: json['wallet_local_bc_size'] as int, - wi: Wi.fromJson(json['wi'] as Map), + json['recent_history'] as Map? ?? {}), + recovered: json['recovered'] as bool? ?? false, + seed: json['seed'] as String? ?? '', + walletFileSize: json['wallet_file_size'] as int? ?? 0, + walletId: json['wallet_id'] as int? ?? 0, + walletLocalBcSize: json['wallet_local_bc_size'] as int? ?? 0, + wi: Wi.fromJson(json['wi'] as Map? ?? {}), ); } diff --git a/cw_zano/lib/api/model/destination.dart b/cw_zano/lib/api/model/destination.dart index f8028a8b7..dd3867511 100644 --- a/cw_zano/lib/api/model/destination.dart +++ b/cw_zano/lib/api/model/destination.dart @@ -7,14 +7,14 @@ class Destination { {required this.amount, required this.address, required this.assetId}); factory Destination.fromJson(Map json) => Destination( - amount: int.parse(json['amount'] as String), - address: json['address'] as String, - assetId: json['asset_id'] as String, + amount: int.parse(json['amount'] as String? ?? '0'), + address: json['address'] as String? ?? '', + assetId: json['asset_id'] as String? ?? '', ); Map toJson() => { - "amount": amount.toString(), - "address": address, - "asset_id": assetId, + 'amount': amount.toString(), + 'address': address, + 'asset_id': assetId, }; } diff --git a/cw_zano/lib/api/model/get_address_info_result.dart b/cw_zano/lib/api/model/get_address_info_result.dart index 4d3db12f6..e8399adb1 100644 --- a/cw_zano/lib/api/model/get_address_info_result.dart +++ b/cw_zano/lib/api/model/get_address_info_result.dart @@ -8,9 +8,9 @@ class GetAddressInfoResult { {required this.valid, required this.auditable, required this.paymentId, required this.wrap}); factory GetAddressInfoResult.fromJson(Map json) => GetAddressInfoResult( - valid: json['valid'] as bool, - auditable: json['auditable'] as bool, - paymentId: json['payment_id'] as bool, - wrap: json['wrap'] as bool, + valid: json['valid'] as bool? ?? false, + auditable: json['auditable'] as bool? ?? false, + paymentId: json['payment_id'] as bool? ?? false, + wrap: json['wrap'] as bool? ?? false, ); } diff --git a/cw_zano/lib/api/model/history.dart b/cw_zano/lib/api/model/history.dart index 2310e8519..faadfcdc2 100644 --- a/cw_zano/lib/api/model/history.dart +++ b/cw_zano/lib/api/model/history.dart @@ -43,27 +43,27 @@ class History { }); factory History.fromJson(Map json) => History( - comment: json['comment'] as String, + comment: json['comment'] as String? ?? '', employedEntries: EmployedEntries.fromJson( - json['employed_entries'] as Map), - fee: json['fee'] as int, - height: json['height'] as int, - isMining: json['is_mining'] as bool, - isMixing: json['is_mixing'] as bool, - isService: json['is_service'] as bool, - paymentId: json['payment_id'] as String, + 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(), remoteAliases: json['remote_aliases'] == null ? [] : (json['remote_aliases'] as List).cast(), - showSender: json['show_sender'] as bool, - subtransfers: (json['subtransfers'] as List) + showSender: json['show_sender'] as bool? ?? false, + subtransfers: (json['subtransfers'] as List? ?? []) .map((e) => Subtransfer.fromJson(e as Map)) .toList(), - timestamp: json['timestamp'] as int, - transferInternalIndex: 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, - txHash: json['tx_hash'] as String, - txType: json['tx_type'] as int, - unlockTime: json['unlock_time'] as int, + 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, + 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, ); } diff --git a/cw_zano/lib/api/model/receive.dart b/cw_zano/lib/api/model/receive.dart index bb5817c94..da0443b29 100644 --- a/cw_zano/lib/api/model/receive.dart +++ b/cw_zano/lib/api/model/receive.dart @@ -6,8 +6,8 @@ class Receive { Receive({required this.amount, required this.assetId, required this.index}); factory Receive.fromJson(Map json) => Receive( - amount: json['amount'] as int, - assetId: json['asset_id'] as String, - index: json['index'] as int, + amount: json['amount'] as int? ?? 0, + assetId: json['asset_id'] as String? ?? '', + index: json['index'] as int? ?? 0, ); } diff --git a/cw_zano/lib/api/model/recent_history.dart b/cw_zano/lib/api/model/recent_history.dart index 87d1aafa7..82e71ef44 100644 --- a/cw_zano/lib/api/model/recent_history.dart +++ b/cw_zano/lib/api/model/recent_history.dart @@ -14,7 +14,7 @@ class RecentHistory { history: json['history'] == null ? null : (json['history'] as List) .map((e) => History.fromJson(e as Map)) .toList(), - lastItemIndex: json['last_item_index'] as int, - totalHistoryItems: json['total_history_items'] as int, + lastItemIndex: json['last_item_index'] as int? ?? 0, + totalHistoryItems: json['total_history_items'] as int? ?? 0, ); } diff --git a/cw_zano/lib/api/model/store_result.dart b/cw_zano/lib/api/model/store_result.dart index 5bc022a76..0ff6625c1 100644 --- a/cw_zano/lib/api/model/store_result.dart +++ b/cw_zano/lib/api/model/store_result.dart @@ -4,6 +4,6 @@ class StoreResult { StoreResult({required this.walletFileSize}); factory StoreResult.fromJson(Map json) => StoreResult( - walletFileSize: json['wallet_file_size'] as int, + walletFileSize: json['wallet_file_size'] as int? ?? 0, ); } \ No newline at end of file diff --git a/cw_zano/lib/api/model/subtransfer.dart b/cw_zano/lib/api/model/subtransfer.dart index 56e6283e6..807134be3 100644 --- a/cw_zano/lib/api/model/subtransfer.dart +++ b/cw_zano/lib/api/model/subtransfer.dart @@ -7,8 +7,8 @@ class Subtransfer { {required this.amount, required this.assetId, required this.isIncome}); factory Subtransfer.fromJson(Map json) => Subtransfer( - amount: json['amount'] as int, - assetId: json['asset_id'] as String, - isIncome: json['is_income'] as bool, + amount: json['amount'] as int? ?? 0, + assetId: json['asset_id'] as String? ?? '', + isIncome: json['is_income'] as bool? ?? false, ); } diff --git a/cw_zano/lib/api/model/transfer_params.dart b/cw_zano/lib/api/model/transfer_params.dart index 8fddf31ac..4f252c402 100644 --- a/cw_zano/lib/api/model/transfer_params.dart +++ b/cw_zano/lib/api/model/transfer_params.dart @@ -20,13 +20,13 @@ class TransferParams { }); Map toJson() => { - "destinations": destinations, - "fee": fee, - "mixin": mixin, - "payment_id": paymentId, - "comment": comment, - "push_payer": pushPayer, - "hide_receiver": hideReceiver, + 'destinations': destinations, + 'fee': fee, + 'mixin': mixin, + 'payment_id': paymentId, + 'comment': comment, + 'push_payer': pushPayer, + 'hide_receiver': hideReceiver, }; factory TransferParams.fromJson(Map json) => TransferParams( @@ -35,7 +35,7 @@ class TransferParams { mixin: json['mixin'] as int? ?? 0, paymentId: json['payment_id'] as String? ?? '', comment: json['comment'] as String? ?? '', - pushPayer: json["push_payer"] as bool? ?? false, - hideReceiver: json["hide_receiver"] as bool? ?? false, + pushPayer: json['push_payer'] as bool? ?? false, + hideReceiver: json['hide_receiver'] as bool? ?? false, ); } diff --git a/cw_zano/lib/api/model/wi_extended.dart b/cw_zano/lib/api/model/wi_extended.dart index 0cc4b90d3..ab7e8efbd 100644 --- a/cw_zano/lib/api/model/wi_extended.dart +++ b/cw_zano/lib/api/model/wi_extended.dart @@ -8,10 +8,10 @@ class WiExtended { WiExtended({required this.seed, required this.spendPrivateKey, required this.spendPublicKey, required this.viewPrivateKey, required this.viewPublicKey}); factory WiExtended.fromJson(Map json) => WiExtended( - seed: json["seed"] as String? ?? '', - spendPrivateKey: json["spend_private_key"] as String? ?? '', - spendPublicKey: json["spend_public_key"] as String? ?? '', - viewPrivateKey: json["view_private_key"] as String? ?? '', - viewPublicKey: json["view_public_key"] as String? ?? '', + seed: json['seed'] as String? ?? '', + spendPrivateKey: json['spend_private_key'] as String? ?? '', + spendPublicKey: json['spend_public_key'] as String? ?? '', + viewPrivateKey: json['view_private_key'] as String? ?? '', + viewPublicKey: json['view_public_key'] as String? ?? '', ); } \ No newline at end of file diff --git a/cw_zano/lib/api/zano_api.dart b/cw_zano/lib/api/zano_api.dart index e135c4128..0bcff04e1 100644 --- a/cw_zano/lib/api/zano_api.dart +++ b/cw_zano/lib/api/zano_api.dart @@ -2,5 +2,5 @@ import 'dart:ffi'; import 'dart:io'; final DynamicLibrary zanoApi = Platform.isAndroid - ? DynamicLibrary.open("libcw_zano.so") - : DynamicLibrary.open("cw_zano.framework/cw_zano"); + ? DynamicLibrary.open('libcw_zano.so') + : DynamicLibrary.open('cw_zano.framework/cw_zano'); diff --git a/cw_zano/lib/zano_balance.dart b/cw_zano/lib/zano_balance.dart index 9bbfb1ce1..c1566a81c 100644 --- a/cw_zano/lib/zano_balance.dart +++ b/cw_zano/lib/zano_balance.dart @@ -1,4 +1,6 @@ +import 'package:cw_core/amount_converter.dart'; import 'package:cw_core/balance.dart'; +import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/monero_amount_format.dart'; class ZanoBalance extends Balance { @@ -7,10 +9,10 @@ class ZanoBalance extends Balance { ZanoBalance({required this.total, required this.unlocked}): super(unlocked, total-unlocked); @override - String get formattedAdditionalBalance => moneroAmountToString(amount: total-unlocked); + String get formattedAdditionalBalance => AmountConverter.amountIntToString(CryptoCurrency.zano, total-unlocked); @override - String get formattedAvailableBalance => moneroAmountToString(amount: unlocked); + String get formattedAvailableBalance => AmountConverter.amountIntToString(CryptoCurrency.zano, unlocked); @override String get formattedFrozenBalance => ''; diff --git a/cw_zano/lib/zano_wallet.dart b/cw_zano/lib/zano_wallet.dart index 8417f6a44..2baf1aa8b 100644 --- a/cw_zano/lib/zano_wallet.dart +++ b/cw_zano/lib/zano_wallet.dart @@ -40,28 +40,32 @@ const moneroBlockSize = 1000; class ZanoWallet = ZanoWalletBase with _$ZanoWallet; -typedef _load_wallet = Pointer Function(Pointer, Pointer, Int8); +typedef _load_wallet = Pointer Function( + Pointer, Pointer, Int8); typedef _LoadWallet = Pointer Function(Pointer, Pointer, int); const int zanoMixin = 10; -abstract class ZanoWalletBase extends WalletBase with Store { +abstract class ZanoWalletBase + extends WalletBase + with Store { ZanoWalletBase(WalletInfo walletInfo) - : balance = ObservableMap.of({CryptoCurrency.zano: ZanoBalance(total: 0, unlocked: 0)}), + : balance = ObservableMap.of( + {CryptoCurrency.zano: ZanoBalance(total: 0, unlocked: 0)}), _isTransactionUpdating = false, _hasSyncAfterStartup = false, walletAddresses = ZanoWalletAddresses(walletInfo), syncStatus = NotConnectedSyncStatus(), super(walletInfo) { transactionHistory = ZanoTransactionHistory(); - /*_onAccountChangeReaction = - reaction((_) => walletAddresses.account, (Account? account) { - if (account == null) { - return; - } - balance.addAll(getZanoBalance(accountIndex: account.id)); - /**walletAddresses.updateSubaddressList(accountIndex: account.id);*/ - });*/ + // _onAccountChangeReaction = + // reaction((_) => walletAddresses.account, (Account? account) { + // if (account == null) { + // return; + // } + // balance.addAll(getZanoBalance(accountIndex: account.id)); + // /**walletAddresses.updateSubaddressList(accountIndex: account.id);*/ + // }); } List history = []; @@ -86,10 +90,14 @@ abstract class ZanoWalletBase extends WalletBase await save()); + _autoSaveTimer = Timer.periodic( + Duration(seconds: _autoSaveInterval), (_) async => await save()); } @override @@ -122,9 +131,10 @@ abstract class ZanoWalletBase extends WalletBase startSync() async { try { @@ -156,51 +191,31 @@ abstract class ZanoWalletBase extends WalletBase destinations; if (hasMultiDestination) { - if (outputs.any((output) => output.sendAll || (output.formattedCryptoAmount ?? 0) <= 0)) { + if (outputs.any((output) => + output.sendAll || (output.formattedCryptoAmount ?? 0) <= 0)) { throw ZanoTransactionCreationException("You don't have enough coins."); } - final int totalAmount = outputs.fold(0, (acc, value) => acc + (value.formattedCryptoAmount ?? 0)); + final int totalAmount = outputs.fold( + 0, (acc, value) => acc + (value.formattedCryptoAmount ?? 0)); if (totalAmount + fee > unlockedBalance) { throw ZanoTransactionCreationException( "You don't have enough coins (required: ${moneroAmountToString(amount: totalAmount + fee)}, unlocked ${moneroAmountToString(amount: unlockedBalance)})."); @@ -230,7 +247,9 @@ abstract class ZanoWalletBase extends WalletBase Destination( amount: output.formattedCryptoAmount ?? 0, - address: output.isParsedAddress ? output.extractedAddress! : output.address, + address: output.isParsedAddress + ? output.extractedAddress! + : output.address, assetId: defaultAsssetId, )) .toList(); @@ -249,13 +268,16 @@ abstract class ZanoWalletBase extends WalletBase); + final _ = + StoreResult.fromJson(map['result']['result'] as Map); } catch (e) { print(e.toString()); } @@ -344,7 +368,8 @@ abstract class ZanoWalletBase extends WalletBase _refreshTransactions() async { try { - final result = await invokeMethod('get_recent_txs_and_info', GetRecentTxsAndInfoParams(offset: 0, count: 30)); + final result = await invokeMethod('get_recent_txs_and_info', + GetRecentTxsAndInfoParams(offset: 0, count: 30)); final map = jsonDecode(result) as Map?; if (map == null) { print('get_recent_txs_and_info empty response'); @@ -368,7 +393,9 @@ abstract class ZanoWalletBase extends WalletBase History.fromJson(e as Map)).toList(); + history = transfers + .map((e) => History.fromJson(e as Map)) + .toList(); } catch (e) { print(e.toString()); } @@ -378,7 +405,10 @@ abstract class ZanoWalletBase extends WalletBase> fetchTransactions() async { try { await _refreshTransactions(); - return history.map((history) => ZanoTransactionInfo.fromHistory(history)).fold>( + return history + .map( + (history) => ZanoTransactionInfo.fromHistory(history)) + .fold>( {}, (Map acc, ZanoTransactionInfo tx) { acc[tx.id] = tx; @@ -420,10 +450,12 @@ abstract class ZanoWalletBase extends WalletBase _askForUpdateTransactionHistory() async => await updateTransactions(); + Future _askForUpdateTransactionHistory() async => + await updateTransactions(); void _onNewBlock(int height, int blocksLeft, double ptc) async { try { @@ -473,7 +505,10 @@ abstract class ZanoWalletBase extends WalletBase invokeMethod(String methodName, Object params) async { - var invokeResult = ApiCalls.asyncCall(methodName: 'invoke', hWallet: hWallet, params: '{"method": "$methodName","params": ${jsonEncode(params)}}'); + var invokeResult = ApiCalls.asyncCall( + methodName: 'invoke', + hWallet: hWallet, + params: '{"method": "$methodName","params": ${jsonEncode(params)}}'); var map = jsonDecode(invokeResult) as Map; int attempts = 0; if (map['job_id'] != null) { @@ -482,7 +517,9 @@ abstract class ZanoWalletBase extends WalletBase; - if (map['status'] != null && map['status'] == _statusDelivered && map['result'] != null) { + if (map['status'] != null && + map['status'] == _statusDelivered && + map['result'] != null) { return result; } } while (++attempts < _maxAttempts); @@ -493,14 +530,16 @@ abstract class ZanoWalletBase extends WalletBase); + final result = + GetWalletInfoResult.fromJson(jsonDecode(json) as Map); return result; } GetWalletStatusResult getWalletStatus() { final json = ApiCalls.getWalletStatus(hWallet: hWallet); print('wallet status $json'); // TODO: remove - final status = GetWalletStatusResult.fromJson(jsonDecode(json) as Map); + final status = GetWalletStatusResult.fromJson( + jsonDecode(json) as Map); return status; } } diff --git a/cw_zano/lib/zano_wallet_service.dart b/cw_zano/lib/zano_wallet_service.dart index 7fc528a26..c19578e1e 100644 --- a/cw_zano/lib/zano_wallet_service.dart +++ b/cw_zano/lib/zano_wallet_service.dart @@ -63,9 +63,9 @@ class ZanoWalletService extends WalletService; - if (map['result'] == null) throw CreateWalletException(''); + _checkForCreateWalletError(map); final createWalletResult = CreateWalletResult.fromJson(map['result'] as Map); _parseCreateWalletResult(createWalletResult, wallet); await wallet.store(); @@ -103,9 +103,9 @@ class ZanoWalletService extends WalletService; - if (map['result'] == null) throw CreateWalletException(''); + _checkForCreateWalletError(map); final createWalletResult = CreateWalletResult.fromJson(map['result'] as Map); _parseCreateWalletResult(createWalletResult, wallet); await wallet.store(); @@ -113,6 +113,19 @@ class ZanoWalletService extends WalletService map) { + if (map['error'] != null) { + final code = map['error']!['code'] ?? ''; + final message = map['error']!['message'] ?? ''; + throw CreateWalletException('Error creating/loading wallet $code $message'); + } + if (map['result'] == null) { + throw CreateWalletException('Error creating/loading wallet, empty response'); } } @@ -158,7 +171,7 @@ class ZanoWalletService extends WalletService restoreFromKeys(ZanoRestoreWalletFromKeysCredentials credentials) async { - throw UnimplementedError("Restore from keys not implemented"); + throw UnimplementedError('Restore from keys not implemented'); } @override