2024-03-15 12:42:27 +00:00
|
|
|
import 'dart:convert';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'dart:io';
|
2024-03-15 12:42:27 +00:00
|
|
|
|
|
|
|
import 'package:cw_core/transaction_priority.dart';
|
|
|
|
import 'package:cw_zano/api/api_calls.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/api/consts.dart';
|
2024-03-19 15:51:08 +00:00
|
|
|
import 'package:cw_zano/api/model/asset_id_params.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/api/model/create_wallet_result.dart';
|
|
|
|
import 'package:cw_zano/api/model/destination.dart';
|
2024-03-19 15:51:08 +00:00
|
|
|
import 'package:cw_zano/api/model/get_address_info_result.dart';
|
2024-03-15 12:42:27 +00:00
|
|
|
import 'package:cw_zano/api/model/get_recent_txs_and_info_params.dart';
|
2024-04-09 10:59:43 +00:00
|
|
|
import 'package:cw_zano/api/model/get_recent_txs_and_info_result.dart';
|
2024-03-15 12:42:27 +00:00
|
|
|
import 'package:cw_zano/api/model/get_wallet_info_result.dart';
|
|
|
|
import 'package:cw_zano/api/model/get_wallet_status_result.dart';
|
2024-03-19 15:51:08 +00:00
|
|
|
import 'package:cw_zano/api/model/proxy_to_daemon_params.dart';
|
|
|
|
import 'package:cw_zano/api/model/proxy_to_daemon_result.dart';
|
2024-03-16 10:55:03 +00:00
|
|
|
import 'package:cw_zano/api/model/transfer.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/api/model/transfer_params.dart';
|
|
|
|
import 'package:cw_zano/api/model/transfer_result.dart';
|
2024-03-19 15:51:08 +00:00
|
|
|
import 'package:cw_zano/model/zano_asset.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:cw_zano/zano_wallet_exceptions.dart';
|
2024-03-16 10:55:03 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2024-04-03 15:14:53 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2024-03-15 12:42:27 +00:00
|
|
|
|
|
|
|
import 'api/model/store_result.dart';
|
|
|
|
|
|
|
|
|
|
|
|
mixin ZanoWalletApi {
|
|
|
|
static const _defaultNodeUri = '195.201.107.230:33336';
|
|
|
|
static const _statusDelivered = 'delivered';
|
|
|
|
static const _maxAttempts = 10;
|
2024-04-03 15:14:53 +00:00
|
|
|
static const _logInfo = true;
|
2024-04-09 10:59:43 +00:00
|
|
|
static const _logError = true;
|
2024-04-09 11:29:43 +00:00
|
|
|
static const _logJson = false;
|
2024-04-03 15:14:53 +00:00
|
|
|
static const int _zanoMixinValue = 10;
|
2024-03-15 12:42:27 +00:00
|
|
|
|
|
|
|
int _hWallet = 0;
|
|
|
|
|
|
|
|
int get hWallet => _hWallet;
|
|
|
|
|
|
|
|
set hWallet(int value) {
|
|
|
|
_hWallet = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getCurrentTxFee(TransactionPriority priority) => ApiCalls.getCurrentTxFee(priority: priority.raw);
|
|
|
|
|
|
|
|
void setPassword(String password) => ApiCalls.setPassword(hWallet: hWallet, password: password);
|
|
|
|
|
2024-04-09 11:29:43 +00:00
|
|
|
void closeWallet([int? walletToClose]) {
|
|
|
|
_info('close_wallet ${walletToClose ?? hWallet}');
|
|
|
|
final result = ApiCalls.closeWallet(hWallet: walletToClose ?? hWallet);
|
|
|
|
_info('close_wallet result $result');
|
2024-04-09 10:59:43 +00:00
|
|
|
}
|
2024-03-15 12:42:27 +00:00
|
|
|
|
2024-04-03 15:14:53 +00:00
|
|
|
Future<bool> setupNode() async {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('init $_defaultNodeUri');
|
2024-04-03 15:14:53 +00:00
|
|
|
final result = ApiCalls.setupNode(
|
|
|
|
address: _defaultNodeUri,
|
|
|
|
login: '',
|
|
|
|
password: '',
|
|
|
|
useSSL: false,
|
|
|
|
isLightWallet: false,
|
|
|
|
);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('init result $result');
|
2024-04-03 15:14:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
2024-03-15 12:42:27 +00:00
|
|
|
|
2024-04-03 15:14:53 +00:00
|
|
|
Future<GetWalletInfoResult> getWalletInfo() async {
|
2024-03-15 12:42:27 +00:00
|
|
|
final json = ApiCalls.getWalletInfo(hWallet);
|
|
|
|
final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logJson) debugPrint('get_wallet_info $json');
|
|
|
|
await _writeLog('get_wallet_info', 'get_wallet_info result $json');
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('get_wallet_info got ${result.wi.balances.length} balances: ${result.wi.balances} seed: ${_shorten(result.wiExtended.seed)}');
|
2024-03-15 12:42:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-03 15:14:53 +00:00
|
|
|
Future<GetWalletStatusResult> getWalletStatus() async {
|
2024-03-15 12:42:27 +00:00
|
|
|
final json = ApiCalls.getWalletStatus(hWallet: hWallet);
|
2024-04-03 15:14:53 +00:00
|
|
|
if (json == Consts.errorWalletWrongId) {
|
|
|
|
print('wrong wallet id');
|
|
|
|
throw ZanoWalletException('Wrong wallet id');
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
2024-04-03 15:14:53 +00:00
|
|
|
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');
|
|
|
|
if (_logInfo)
|
2024-04-09 10:59:43 +00:00
|
|
|
_info(
|
|
|
|
'get_wallet_status connected: ${status.isDaemonConnected} in refresh: ${status.isInLongRefresh} progress: ${status.progress} wallet state: ${status.walletState}');
|
2024-03-15 12:42:27 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> invokeMethod(String methodName, Object params) async {
|
2024-04-03 15:14:53 +00:00
|
|
|
await _writeLog(methodName, 'invoke method $methodName params: ${jsonEncode(params)} hWallet: $hWallet');
|
2024-03-15 12:42:27 +00:00
|
|
|
var invokeResult =
|
|
|
|
ApiCalls.asyncCall(methodName: 'invoke', hWallet: hWallet, params: '{"method": "$methodName","params": ${jsonEncode(params)}}');
|
2024-04-06 10:03:11 +00:00
|
|
|
Map<String, dynamic> map;
|
|
|
|
try {
|
|
|
|
map = jsonDecode(invokeResult) as Map<String, dynamic>;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint('exception in parsing json in invokeMethod: $invokeResult');
|
|
|
|
rethrow;
|
|
|
|
}
|
2024-03-15 12:42:27 +00:00
|
|
|
int attempts = 0;
|
|
|
|
if (map['job_id'] != null) {
|
|
|
|
final jobId = map['job_id'] as int;
|
|
|
|
do {
|
|
|
|
await Future.delayed(Duration(milliseconds: attempts < 2 ? 100 : 500));
|
|
|
|
final result = ApiCalls.tryPullResult(jobId);
|
2024-04-06 10:03:11 +00:00
|
|
|
try {
|
|
|
|
map = jsonDecode(result) as Map<String, dynamic>;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint('exception in parsing json in invokeMethod: $result');
|
|
|
|
rethrow;
|
|
|
|
}
|
2024-03-15 12:42:27 +00:00
|
|
|
if (map['status'] != null && map['status'] == _statusDelivered && map['result'] != null) {
|
2024-04-03 15:14:53 +00:00
|
|
|
await _writeLog(methodName, 'invoke method $methodName result $result');
|
2024-03-15 12:42:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} while (++attempts < _maxAttempts);
|
|
|
|
}
|
2024-04-03 15:14:53 +00:00
|
|
|
await _writeLog(methodName, 'invoke method $methodName result: $invokeResult');
|
2024-03-15 12:42:27 +00:00
|
|
|
return invokeResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<ZanoAsset>> getAssetsWhitelist() async {
|
|
|
|
try {
|
|
|
|
final json = await invokeMethod('assets_whitelist_get', '{}');
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logJson) debugPrint('assets_whitelist_get $json');
|
2024-03-15 12:42:27 +00:00
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
_checkForErrors(map);
|
2024-04-06 10:03:11 +00:00
|
|
|
List<ZanoAsset> assets(String type, bool isGlobalWhitelist) =>
|
|
|
|
(map?['result']?['result']?[type] as List<dynamic>?)
|
|
|
|
?.map((e) => ZanoAsset.fromJson(e as Map<String, dynamic>, isInGlobalWhitelist: isGlobalWhitelist))
|
|
|
|
.toList() ??
|
|
|
|
[];
|
|
|
|
final localWhitelist = assets('local_whitelist', false);
|
|
|
|
final globalWhitelist = assets('global_whitelist', true);
|
|
|
|
final ownAssets = assets('own_assets', false);
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logInfo)
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('assets_whitelist_get got local whitelist: ${localWhitelist.length} ($localWhitelist); '
|
2024-03-19 15:51:08 +00:00
|
|
|
'global whitelist: ${globalWhitelist.length} ($globalWhitelist); '
|
|
|
|
'own assets: ${ownAssets.length} ($ownAssets)');
|
2024-04-09 10:59:43 +00:00
|
|
|
return [...globalWhitelist, ...localWhitelist, ...ownAssets];
|
2024-03-15 12:42:27 +00:00
|
|
|
} catch (e) {
|
2024-04-03 15:14:53 +00:00
|
|
|
print('[error] assets_whitelist_get $e');
|
2024-03-15 12:42:27 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<ZanoAsset?> addAssetsWhitelist(String assetId) async {
|
|
|
|
try {
|
2024-03-19 15:51:08 +00:00
|
|
|
final json = await invokeMethod('assets_whitelist_add', AssetIdParams(assetId: assetId));
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logJson) print('assets_whitelist_add $assetId $json');
|
2024-03-15 12:42:27 +00:00
|
|
|
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>);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('assets_whitelist_add added ${assetDescriptor.fullName} ${assetDescriptor.ticker}');
|
2024-03-15 12:42:27 +00:00
|
|
|
return assetDescriptor;
|
|
|
|
} else {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('assets_whitelist_add status ${map['result']!['result']!['status']!}');
|
2024-03-15 12:42:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-04-03 15:14:53 +00:00
|
|
|
print('[error] assets_whitelist_add $e');
|
2024-03-15 12:42:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> removeAssetsWhitelist(String assetId) async {
|
|
|
|
try {
|
2024-03-19 15:51:08 +00:00
|
|
|
final json = await invokeMethod('assets_whitelist_remove', AssetIdParams(assetId: assetId));
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logJson) print('assets_whitelist_remove $assetId $json');
|
2024-03-15 12:42:27 +00:00
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
_checkForErrors(map);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('assets_whitelist_remove status ${map!['result']!['result']!['status']!}');
|
2024-03-15 12:42:27 +00:00
|
|
|
return (map!['result']!['result']!['status']! == 'OK');
|
|
|
|
} catch (e) {
|
2024-04-03 15:14:53 +00:00
|
|
|
print('[error] assets_whitelist_remove $e');
|
2024-03-15 12:42:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:51:08 +00:00
|
|
|
Future<ProxyToDaemonResult?> _proxyToDaemon(String uri, String body) async {
|
|
|
|
final json = await invokeMethod('proxy_to_daemon', ProxyToDaemonParams(body: body, uri: uri));
|
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
_checkForErrors(map);
|
|
|
|
return ProxyToDaemonResult.fromJson(map!['result']['result'] as Map<String, dynamic>);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<ZanoAsset?> getAssetInfo(String assetId) async {
|
|
|
|
final methodName = 'get_asset_info';
|
|
|
|
final params = AssetIdParams(assetId: assetId);
|
|
|
|
final result = await _proxyToDaemon('/json_rpc', '{"method": "$methodName","params": ${jsonEncode(params)}}');
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logJson) print('$methodName $assetId ${result?.body}');
|
2024-03-19 15:51:08 +00:00
|
|
|
if (result == null) {
|
|
|
|
debugPrint('get_asset_info empty result');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final map = jsonDecode(result.body) as Map<String, dynamic>?;
|
|
|
|
if (map!['error'] != null) {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('get_asset_info $assetId error ${map['error']!['code']} ${map['error']!['message']}');
|
2024-03-19 15:51:08 +00:00
|
|
|
return null;
|
|
|
|
} else if (map['result']!['status']! == 'OK') {
|
|
|
|
final assetDescriptor = ZanoAsset.fromJson(map['result']!['asset_descriptor']! as Map<String, dynamic>);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('get_asset_info $assetId ${assetDescriptor.fullName} ${assetDescriptor.ticker}');
|
2024-03-19 15:51:08 +00:00
|
|
|
return assetDescriptor;
|
|
|
|
} else {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('get_asset_info $assetId status ${map['result']!['status']!}');
|
2024-03-19 15:51:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:42:27 +00:00
|
|
|
Future<StoreResult?> store() async {
|
|
|
|
try {
|
|
|
|
final json = await invokeMethod('store', '{}');
|
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
_checkForErrors(map);
|
|
|
|
return StoreResult.fromJson(map!['result']['result'] as Map<String, dynamic>);
|
|
|
|
} catch (e) {
|
2024-04-03 15:14:53 +00:00
|
|
|
print('[error] store $e');
|
2024-03-15 12:42:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 10:59:43 +00:00
|
|
|
Future<GetRecentTxsAndInfoResult> getRecentTxsAndInfo({required int offset, required int count}) async {
|
|
|
|
_info('get_recent_txs_and_info $offset $count');
|
2024-03-15 12:42:27 +00:00
|
|
|
try {
|
2024-04-09 10:59:43 +00:00
|
|
|
final json = await invokeMethod('get_recent_txs_and_info', GetRecentTxsAndInfoParams(offset: offset, count: count));
|
2024-04-03 15:14:53 +00:00
|
|
|
if (_logJson) debugPrint('get_recent_txs_and_info $json');
|
2024-03-15 12:42:27 +00:00
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
_checkForErrors(map);
|
2024-04-09 10:59:43 +00:00
|
|
|
final lastItemIndex = map?['result']?['result']?['last_item_index'] as int?;
|
|
|
|
final totalTransfers = map?['result']?['result']?['total_transfers'] as int?;
|
2024-03-15 12:42:27 +00:00
|
|
|
final transfers = map?['result']?['result']?['transfers'] as List<dynamic>?;
|
2024-04-09 10:59:43 +00:00
|
|
|
if (transfers == null || lastItemIndex == null || totalTransfers == null) {
|
|
|
|
_error('get_recent_txs_and_info empty transfers');
|
|
|
|
return GetRecentTxsAndInfoResult.empty();
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
2024-04-09 10:59:43 +00:00
|
|
|
_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,
|
|
|
|
);
|
2024-03-15 12:42:27 +00:00
|
|
|
} catch (e) {
|
2024-04-09 10:59:43 +00:00
|
|
|
_error('get_recent_txs_and_info $e');
|
|
|
|
return GetRecentTxsAndInfoResult.empty();
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:51:08 +00:00
|
|
|
GetAddressInfoResult getAddressInfo(String address) => GetAddressInfoResult.fromJson(
|
|
|
|
jsonDecode(ApiCalls.getAddressInfo(address: address)) as Map<String, dynamic>,
|
|
|
|
);
|
|
|
|
|
2024-04-03 15:14:53 +00:00
|
|
|
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 {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('create_wallet path $path password ${_shorten(password)}');
|
2024-04-03 15:14:53 +00:00
|
|
|
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');
|
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
if (map?['error'] != null) {
|
|
|
|
final code = map!['error']!['code'] ?? '';
|
|
|
|
final message = map['error']!['message'] ?? '';
|
|
|
|
throw ZanoWalletException('Error creating wallet file, $message ($code)');
|
|
|
|
}
|
|
|
|
if (map?['result'] == null) {
|
|
|
|
throw ZanoWalletException('Error creating wallet file, empty response');
|
|
|
|
}
|
|
|
|
final result = CreateWalletResult.fromJson(map!['result'] as Map<String, dynamic>);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('create_wallet ${result.name} ${result.seed}');
|
2024-04-03 15:14:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<CreateWalletResult> restoreWalletFromSeed(String path, String password, String seed) async {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('restore_wallet path $path password ${_shorten(password)} seed ${_shorten(seed)}');
|
2024-04-03 15:14:53 +00:00
|
|
|
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');
|
|
|
|
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.errorWrongSeed) {
|
|
|
|
throw RestoreFromKeysException('Error restoring wallet, wrong seed');
|
|
|
|
} else if (code == Consts.errorAlreadyExists) {
|
|
|
|
throw RestoreFromKeysException('Error restoring wallet, already exists');
|
|
|
|
}
|
|
|
|
throw RestoreFromKeysException('Error restoring wallet, $message ($code)');
|
|
|
|
}
|
|
|
|
if (map?['result'] == null) {
|
|
|
|
throw RestoreFromKeysException('Error restoring wallet, empty response');
|
|
|
|
}
|
|
|
|
final result = CreateWalletResult.fromJson(map!['result'] as Map<String, dynamic>);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('restore_wallet ${result.name} ${result.wi.address}');
|
2024-04-03 15:14:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-09 11:29:43 +00:00
|
|
|
Future<CreateWalletResult> loadWallet(String path, String password, [int attempt = 0]) async {
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('load_wallet path $path password ${_shorten(password)}');
|
2024-04-03 15:14:53 +00:00
|
|
|
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');
|
|
|
|
final map = jsonDecode(json) as Map<String, dynamic>?;
|
|
|
|
if (map?['error'] != null) {
|
|
|
|
final code = map?['error']!['code'] ?? '';
|
|
|
|
final message = map?['error']!['message'] ?? '';
|
2024-04-09 11:29:43 +00:00
|
|
|
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)');
|
|
|
|
closeWallet(attempt);
|
|
|
|
await Future.delayed(const Duration(milliseconds: 500));
|
|
|
|
return await loadWallet(path, password, attempt + 1);
|
2024-04-03 15:14:53 +00:00
|
|
|
}
|
|
|
|
throw ZanoWalletException('Error loading wallet, $message ($code)');
|
|
|
|
}
|
|
|
|
if (map?['result'] == null) {
|
|
|
|
throw ZanoWalletException('Error loading wallet, empty response');
|
|
|
|
}
|
|
|
|
final result = CreateWalletResult.fromJson(map!['result'] as Map<String, dynamic>);
|
2024-04-06 10:03:11 +00:00
|
|
|
_info('load_wallet ${result.name} ${result.wi.address}');
|
2024-04-03 15:14:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<TransferResult> transfer(List<Destination> destinations, BigInt fee, String comment) async {
|
|
|
|
final params = TransferParams(
|
|
|
|
destinations: destinations,
|
|
|
|
fee: fee,
|
|
|
|
mixin: _zanoMixinValue,
|
|
|
|
paymentId: '',
|
|
|
|
comment: comment,
|
|
|
|
pushPayer: false,
|
|
|
|
hideReceiver: true,
|
|
|
|
);
|
|
|
|
final json = await invokeMethod('transfer', params);
|
|
|
|
if (_logJson) debugPrint('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}');
|
|
|
|
return transferResult;
|
|
|
|
} else {
|
|
|
|
final errorCode = resultMap['error']['code'];
|
|
|
|
final code = errorCode is int ? errorCode.toString() : errorCode as String? ?? '';
|
|
|
|
final message = resultMap['error']['message'] as String? ?? '';
|
|
|
|
debugPrint('transfer error $code $message');
|
|
|
|
throw TransferException('Transfer error, $message ($code)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debugPrint('transfer error empty result');
|
|
|
|
throw TransferException('Transfer error, empty result');
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:42:27 +00:00
|
|
|
void _checkForErrors(Map<String, dynamic>? map) {
|
|
|
|
if (map == null) {
|
2024-04-03 15:14:53 +00:00
|
|
|
throw ZanoWalletException('Empty response');
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
|
|
|
final result = map['result'];
|
|
|
|
if (result == null) {
|
2024-04-03 15:14:53 +00:00
|
|
|
throw ZanoWalletException('Empty response');
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
|
|
|
if (result['error'] != null) {
|
|
|
|
final code = result['error']!['code'] ?? '';
|
|
|
|
final message = result['error']!['message'] ?? '';
|
2024-04-03 15:14:53 +00:00
|
|
|
throw ZanoWalletException('Error, $message ($code)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _writeLog(String method, String logMessage) async {
|
|
|
|
final dir = await getDownloadsDirectory();
|
|
|
|
final logFile = File('${dir!.path}/$method.txt');
|
|
|
|
final date = DateTime.now();
|
|
|
|
String twoDigits(int value) => value.toString().padLeft(2, '0');
|
|
|
|
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);
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-06 10:03:11 +00:00
|
|
|
|
2024-04-09 10:59:43 +00:00
|
|
|
static void _info(String s) => _logInfo ? debugPrint('[info] $s') : null;
|
|
|
|
static void _error(String s) => _logError ? debugPrint('[error] $s') : null;
|
2024-03-15 12:42:27 +00:00
|
|
|
}
|