diff --git a/cw_zano/lib/api/calls.dart b/cw_zano/lib/api/calls.dart index ddc731677..402ada3bd 100644 --- a/cw_zano/lib/api/calls.dart +++ b/cw_zano/lib/api/calls.dart @@ -4,11 +4,20 @@ import 'dart:convert'; import 'package:cw_zano/api/convert_utf8_to_string.dart'; import 'package:cw_zano/api/model/get_recent_txs_and_info_params.dart'; import 'package:cw_zano/api/model/transfer_params.dart'; +import 'package:cw_zano/api/structs/utf8_box.dart'; import 'package:cw_zano/api/zano_api.dart'; import 'package:ffi/ffi.dart'; import 'package:flutter/foundation.dart'; -// create_wallet +// bool setup_node(char *address, char *login, char *password, bool use_ssl, bool is_light_wallet, char *error) +final _setupNodeNative = + zanoApi.lookup>('setup_node').asFunction<_SetupNode>(); +typedef _setup_node = Int8 Function( + Pointer, Pointer?, Pointer?, Int8, Int8, Pointer); +typedef _SetupNode = int Function( + Pointer, Pointer?, Pointer?, int, int, Pointer); + +// char * create_wallet(char *path, char *password, char *language, int32_t networkType, char *error) final _createWalletNative = zanoApi.lookup>('create_wallet').asFunction<_CreateWallet>(); typedef _create_wallet = Pointer Function( @@ -16,66 +25,67 @@ typedef _create_wallet = Pointer Function( typedef _CreateWallet = Pointer Function( Pointer, Pointer, Pointer, int, Pointer); -// async_call +// char* async_call(char* method_name, uint64_t instance_id, char* params) final _asyncCallNative = zanoApi.lookup>('async_call').asFunction<_AsyncCall>(); typedef _async_call = Pointer Function(Pointer, Int64, Pointer); typedef _AsyncCall = Pointer Function( Pointer methodName, int hWallet, Pointer params); -// get_wallet_status +// char* get_wallet_status(uint64_t hwallet) final _getWalletStatusNative = zanoApi .lookup>('get_wallet_status') .asFunction<_GetWalletStatus>(); typedef _get_wallet_status = Pointer Function(Int64); typedef _GetWalletStatus = Pointer Function(int hWallet); -// get_wallet_info +// char *get_wallet_info(uint64_t hwallet) final _getWalletInfoNative = zanoApi .lookup>('get_wallet_info') .asFunction<_GetWalletInfo>(); typedef _get_wallet_info = Pointer Function(Int64); typedef _GetWalletInfo = Pointer Function(int hWallet); -// get_connectivity_status +// char* get_connectivity_status() final _getConnectivityStatusNative = zanoApi .lookup>('get_connectivity_status') .asFunction<_GetConnectivityStatus>(); typedef _get_connectivity_status = Pointer Function(); typedef _GetConnectivityStatus = Pointer Function(); -// get_version +// char* get_version() final _getVersionNative = zanoApi.lookup>('get_version').asFunction<_GetVersion>(); typedef _get_version = Pointer Function(); typedef _GetVersion = Pointer Function(); -// load_wallet +// char * load_wallet(char *path, char *password, int32_t nettype) final _loadWalletNative = zanoApi.lookup>('load_wallet').asFunction<_LoadWallet>(); typedef _load_wallet = Pointer Function(Pointer, Pointer, Int8); typedef _LoadWallet = Pointer Function(Pointer, Pointer, int); -// try_pull_result +// char* try_pull_result(uint64_t job_id) final _tryPullResultNative = zanoApi .lookup>('try_pull_result') .asFunction<_TryPullResult>(); typedef _try_pull_result = Pointer Function(Int64); typedef _TryPullResult = Pointer Function(int hWallet); -// close_wallet +// void close_wallet(uint64_t hwallet) final _closeWalletNative = zanoApi.lookup>('close_wallet').asFunction<_closeWalletStatus>(); typedef _close_wallet = Void Function(Int64); typedef _closeWalletStatus = void Function(int hWallet); -// get_current_tx_fee +// uint64_t get_current_tx_fee(uint64_t priority) final _getCurrentTxFeeNative = zanoApi .lookup>('get_current_tx_fee') .asFunction<_getCurrentTxFee>(); typedef _get_current_tx_fee = Int64 Function(Int64); typedef _getCurrentTxFee = int Function(int priority); +// char * restore_wallet_from_seed(char *path, char *password, char *seed, int32_t networkType, uint64_t restoreHeight, char *error) final _restoreWalletFromSeedNative = zanoApi .lookup>('restore_wallet_from_seed') .asFunction<_RestoreWalletFromSeed>(); @@ -84,7 +94,86 @@ typedef _restore_wallet_from_seed = Pointer Function( typedef _RestoreWalletFromSeed = Pointer Function( Pointer, Pointer, Pointer, int, int, Pointer); -String doAsyncCall({required String methodName, required int hWallet, required String params}) { +// char* set_password(uint64_t hwallet, char *password, Utf8Box &error) +final _setPasswordNative = + zanoApi.lookup>('set_password').asFunction<_SetPassword>(); +typedef _set_password = Pointer Function(Int64 hWallet, Pointer password, Pointer error); +typedef _SetPassword = Pointer Function(int hWallet, Pointer password, Pointer error); + +String setPassword({required int hWallet, required String password}) { + final passwordPointer = password.toNativeUtf8(); + final errorMessagePointer = calloc(); + final result = + convertUTF8ToString(pointer: _setPasswordNative(hWallet, passwordPointer, errorMessagePointer)); + calloc.free(passwordPointer); + + /*if (!changed) { + final message = errorMessagePointer.ref.getValue(); + calloc.free(errorMessagePointer); + throw Exception(message); + }*/ + + calloc.free(errorMessagePointer); + return result; +} + +// is_wallet_exist(char *path) +final _isWalletExistNative = zanoApi + .lookup>('is_wallet_exist') + .asFunction<_IsWalletExist>(); +typedef _is_wallet_exist = Int8 Function(Pointer); +typedef _IsWalletExist = int Function(Pointer); + +bool isWalletExist({required String path}) { + final pathPointer = path.toNativeUtf8(); + final isExist = _isWalletExistNative(pathPointer) != 0; + calloc.free(pathPointer); + return isExist; +} + +bool setupNode({ + required String address, + String? login, + String? password, + bool useSSL = false, + bool isLightWallet = false, + /*String? socksProxyAddress*/ +}) { + final addressPointer = address.toNativeUtf8(); + Pointer? loginPointer; + Pointer? socksProxyAddressPointer; + Pointer? passwordPointer; + + if (login != null) { + loginPointer = login.toNativeUtf8(); + } + + if (password != null) { + passwordPointer = password.toNativeUtf8(); + } + + final errorMessagePointer = ''.toNativeUtf8(); + debugPrint( + "setup_node address $address login $login password $password useSSL $useSSL isLightWallet $isLightWallet"); + final isSetupNode = _setupNodeNative(addressPointer, loginPointer, passwordPointer, + _boolToInt(useSSL), _boolToInt(isLightWallet), errorMessagePointer) != + 0; + debugPrint("setup_node result $isSetupNode"); + + calloc.free(addressPointer); + + if (loginPointer != null) { + calloc.free(loginPointer); + } + + if (passwordPointer != null) { + calloc.free(passwordPointer); + } + + return isSetupNode; +} + +String asyncCall({required String methodName, required int hWallet, required String params}) { final methodNamePointer = methodName.toNativeUtf8(); final paramsPointer = params.toNativeUtf8(); @@ -118,7 +207,7 @@ String createWallet( Future invokeMethod(int hWallet, String methodName, String params) async { debugPrint('invoke method $methodName params $params'); - final invokeResult = doAsyncCall( + final invokeResult = asyncCall( methodName: 'invoke', hWallet: hWallet, params: json.encode({ @@ -140,7 +229,7 @@ Future store(int hWallet) async { } Future transfer(int hWallet, TransferParams params) async { - final invokeResult = await doAsyncCall( + final invokeResult = await asyncCall( methodName: 'invoke', hWallet: hWallet, params: '{"method": "transfer","params": ${jsonEncode(params)}}', @@ -166,7 +255,10 @@ Future getRecentTxsAndInfo( 'get_recent_txs_and_info', json.encode( GetRecentTxsAndInfoParams( - offset: offset, count: count, updateProvisionInfo: updateProvisionInfo), + offset: offset, + count: count, + updateProvisionInfo: updateProvisionInfo, + ), ), ); } @@ -218,6 +310,7 @@ String restoreWalletFromSeed(String path, String password, String seed) { final result = convertUTF8ToString( pointer: _restoreWalletFromSeedNative( pathPointer, passwordPointer, seedPointer, 0, 0, errorMessagePointer)); + debugPrint('restore wallet from seed result $result'); return result; } @@ -238,3 +331,5 @@ String tryPullResult(int jobId) { debugPrint('try_pull_result result $result'); return result; } + +int _boolToInt(bool value) => value ? 1 : 0; diff --git a/cw_zano/lib/api/consts.dart b/cw_zano/lib/api/consts.dart new file mode 100644 index 000000000..06c3b314b --- /dev/null +++ b/cw_zano/lib/api/consts.dart @@ -0,0 +1,4 @@ +class Consts { + static const errorWrongSeed = 'WRONG_SEED'; + static const errorAlreadyExists = 'ALREADY_EXISTS'; +} \ No newline at end of file diff --git a/cw_zano/lib/api/cw_zano.dart b/cw_zano/lib/api/cw_zano.dart deleted file mode 100644 index 6ea49ef28..000000000 --- a/cw_zano/lib/api/cw_zano.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/services.dart'; - -class CwZano { - static const MethodChannel _channel = const MethodChannel('cw_zano'); - - static Future get platformVersion async { - final String version = - await _channel.invokeMethod('getPlatformVersion') ?? ''; - return version; - } -} diff --git a/cw_zano/lib/api/exceptions/already_exists_exception.dart b/cw_zano/lib/api/exceptions/already_exists_exception.dart new file mode 100644 index 000000000..a6d00fef9 --- /dev/null +++ b/cw_zano/lib/api/exceptions/already_exists_exception.dart @@ -0,0 +1,6 @@ +import 'package:cw_zano/api/consts.dart'; +import 'package:cw_zano/api/exceptions/api_exception.dart'; + +class AlreadyExistsException extends ApiException { + AlreadyExistsException(String message): super(Consts.errorAlreadyExists, message); +} \ No newline at end of file diff --git a/cw_zano/lib/api/exceptions/api_exception.dart b/cw_zano/lib/api/exceptions/api_exception.dart new file mode 100644 index 000000000..e9923942b --- /dev/null +++ b/cw_zano/lib/api/exceptions/api_exception.dart @@ -0,0 +1,9 @@ +class ApiException implements Exception { + final String code; + final String message; + + ApiException(this.code, this.message); + + @override + String toString() => '${this.runtimeType}(code: $code, message: $message)'; +} \ No newline at end of file diff --git a/cw_zano/lib/api/exceptions/restore_from_seed_exception.dart b/cw_zano/lib/api/exceptions/restore_from_seed_exception.dart new file mode 100644 index 000000000..3319cdaf1 --- /dev/null +++ b/cw_zano/lib/api/exceptions/restore_from_seed_exception.dart @@ -0,0 +1,5 @@ +import 'package:cw_zano/api/exceptions/api_exception.dart'; + +class RestoreFromSeedException extends ApiException { + RestoreFromSeedException(String code, String message): super(code, message); +} \ No newline at end of file diff --git a/cw_zano/lib/api/exceptions/transfer_exception.dart b/cw_zano/lib/api/exceptions/transfer_exception.dart new file mode 100644 index 000000000..d263ff646 --- /dev/null +++ b/cw_zano/lib/api/exceptions/transfer_exception.dart @@ -0,0 +1,5 @@ +import 'package:cw_zano/api/exceptions/api_exception.dart'; + +class TransferException extends ApiException { + TransferException(String code, String message): super(code, message); +} diff --git a/cw_zano/lib/api/exceptions/wrong_seed_exception.dart b/cw_zano/lib/api/exceptions/wrong_seed_exception.dart new file mode 100644 index 000000000..624fc24b9 --- /dev/null +++ b/cw_zano/lib/api/exceptions/wrong_seed_exception.dart @@ -0,0 +1,6 @@ +import 'package:cw_zano/api/consts.dart'; +import 'package:cw_zano/api/exceptions/api_exception.dart'; + +class WrongSeedException extends ApiException { + WrongSeedException(String message): super(Consts.errorWrongSeed, message); +} \ No newline at end of file diff --git a/cw_zano/lib/api/signatures.dart b/cw_zano/lib/api/signatures.dart deleted file mode 100644 index 64d096ca1..000000000 --- a/cw_zano/lib/api/signatures.dart +++ /dev/null @@ -1,151 +0,0 @@ -import 'dart:ffi'; -import 'package:cw_zano/api/structs/ut8_box.dart'; -import 'package:ffi/ffi.dart'; - -// typedef create_wallet = Pointer Function( -// Pointer, Pointer, Pointer, Int32, Pointer); - -typedef restore_wallet_from_seed = Int8 Function( - Pointer, Pointer, Pointer, Int32, Int64, Pointer); - -typedef restore_wallet_from_keys = Int8 Function( - Pointer, - Pointer, - Pointer, - Pointer, - Pointer, - Pointer, - Int32, - Int64, - Pointer); - -typedef is_wallet_exist = Int8 Function(Pointer); - -//typedef load_wallet = Int8 Function(Pointer, Pointer, Int8); - -typedef error_string = Pointer Function(); - -typedef get_filename = Pointer Function(); - -typedef get_seed = Pointer Function(); - -typedef get_address = Pointer Function(Int32, Int32); - -typedef get_full_balance = Pointer Function(Int32); - -typedef get_unlocked_balance = Pointer Function(Int32); - -typedef get_full_balanace = Int64 Function(Int32); - -typedef get_unlocked_balanace = Int64 Function(Int32); - -typedef get_current_height = Int64 Function(); - -typedef get_node_height = Int64 Function(); - -typedef is_connected = Int8 Function(); - -typedef setup_node = Int8 Function(Pointer, Pointer?, - Pointer?, Int8, Int8, Pointer); - -typedef start_refresh = Void Function(); - -typedef connect_to_node = Int8 Function(); - -typedef set_refresh_from_block_height = Void Function(Int64); - -typedef set_recovering_from_seed = Void Function(Int8); - -typedef store_c = Void Function(Pointer); - -typedef set_password = Int8 Function( - Pointer password, Pointer error); - -typedef set_listener = Void Function(); - -typedef get_syncing_height = Int64 Function(); - -typedef is_needed_to_refresh = Int8 Function(); - -// typedef is_new_transaction_exist = Int8 Function(); - -// typedef subaddrress_size = Int32 Function(); - -// typedef subaddrress_refresh = Void Function(Int32); - -// typedef subaddress_get_all = Pointer Function(); - -// typedef subaddress_add_new = Void Function( -// Int32 accountIndex, Pointer label); - -// typedef subaddress_set_label = Void Function( -// Int32 accountIndex, Int32 addressIndex, Pointer label); - -// typedef account_size = Int32 Function(); - -// typedef account_refresh = Void Function(); - -// typedef account_get_all = Pointer Function(); - -// typedef account_add_new = Void Function(Pointer label); - -// typedef account_set_label = Void Function( -// Int32 accountIndex, Pointer label); - -//typedef transactions_refresh = Void Function(); - -//typedef get_tx_key = Pointer? Function(Pointer txId); - -//typedef transactions_count = Int64 Function(); - -//typedef transactions_get_all = Pointer Function(); - -// typedef transaction_create = Int8 Function( -// Pointer address, -// Pointer assetType, -// Pointer paymentId, -// Pointer amount, -// Int8 priorityRaw, -// Pointer error, -// Pointer pendingTransaction); - -// typedef transaction_create_mult_dest = Int8 Function( -// Pointer> addresses, -// Pointer assetType, -// Pointer paymentId, -// Pointer> amounts, -// Int32 size, -// Int8 priorityRaw, -// Pointer error, -// Pointer pendingTransaction); - -// typedef transaction_commit = Int8 Function( -// Pointer, Pointer); - -typedef secret_view_key = Pointer Function(); - -typedef public_view_key = Pointer Function(); - -typedef secret_spend_key = Pointer Function(); - -typedef public_spend_key = Pointer Function(); - -typedef close_current_wallet = Void Function(); - -typedef on_startup = Void Function(); - -typedef rescan_blockchain = Void Function(); - -typedef asset_types = Pointer> Function(); - -typedef asset_types_size = Int32 Function(); - -typedef get_rate = Pointer Function(); - -typedef size_of_rate = Int32 Function(); - -typedef update_rate = Void Function(); - -typedef set_trusted_daemon = Void Function(Int8 trusted); - -typedef trusted_daemon = Int8 Function(); diff --git a/cw_zano/lib/api/structs/ut8_box.dart b/cw_zano/lib/api/structs/utf8_box.dart similarity index 100% rename from cw_zano/lib/api/structs/ut8_box.dart rename to cw_zano/lib/api/structs/utf8_box.dart diff --git a/cw_zano/lib/api/types.dart b/cw_zano/lib/api/types.dart deleted file mode 100644 index 09e83213e..000000000 --- a/cw_zano/lib/api/types.dart +++ /dev/null @@ -1,149 +0,0 @@ -import 'dart:ffi'; -import 'package:cw_zano/api/structs/ut8_box.dart'; -import 'package:ffi/ffi.dart'; - -typedef CreateWallet = Pointer Function( - Pointer, Pointer, Pointer, int, Pointer); - -typedef RestoreWalletFromSeed = int Function( - Pointer, Pointer, Pointer, int, int, Pointer); - -typedef RestoreWalletFromKeys = int Function( - Pointer, - Pointer, - Pointer, - Pointer, - Pointer, - Pointer, - int, - int, - Pointer); - -typedef IsWalletExist = int Function(Pointer); - -//typedef LoadWallet = int Function(Pointer, Pointer, int); - -typedef ErrorString = Pointer Function(); - -typedef GetFilename = Pointer Function(); - -typedef GetSeed = Pointer Function(); - -typedef GetAddress = Pointer Function(int, int); - -typedef GetZanoFullBalance = Pointer Function(int); - -typedef GetZanoUnlockedBalance = Pointer Function(int); - -typedef GetFullBalance = int Function(int); - -typedef GetUnlockedBalance = int Function(int); - -typedef GetCurrentHeight = int Function(); - -typedef GetNodeHeight = int Function(); - -typedef IsConnected = int Function(); - -typedef SetupNode = int Function(Pointer, Pointer?, Pointer?, - int, int, Pointer); - -typedef StartRefresh = void Function(); - -typedef ConnectToNode = int Function(); - -typedef SetRefreshFromBlockHeight = void Function(int); - -typedef SetRecoveringFromSeed = void Function(int); - -typedef Store = void Function(Pointer); - -typedef SetPassword = int Function( - Pointer password, Pointer error); - -typedef SetListener = void Function(); - -typedef GetSyncingHeight = int Function(); - -typedef IsNeededToRefresh = int Function(); - -typedef IsNewTransactionExist = int Function(); - -typedef SubaddressSize = int Function(); - -typedef SubaddressRefresh = void Function(int); - -typedef SubaddressGetAll = Pointer Function(); - -typedef SubaddressAddNew = void Function(int accountIndex, Pointer label); - -typedef SubaddressSetLabel = void Function( - int accountIndex, int addressIndex, Pointer label); - -typedef AccountSize = int Function(); - -typedef AccountRefresh = void Function(); - -typedef AccountGetAll = Pointer Function(); - -typedef AccountAddNew = void Function(Pointer label); - -typedef AccountSetLabel = void Function(int accountIndex, Pointer label); - -typedef TransactionsRefresh = void Function(); - -typedef GetTxKey = Pointer? Function(Pointer txId); - -typedef TransactionsCount = int Function(); - -typedef TransactionsGetAll = Pointer Function(); - -// typedef TransactionCreate = int Function( -// Pointer address, -// Pointer assetType, -// Pointer paymentId, -// Pointer amount, -// int priorityRaw, -// Pointer error, -// Pointer pendingTransaction); - -// typedef TransactionCreateMultDest = int Function( -// Pointer> addresses, -// Pointer assetType, -// Pointer paymentId, -// Pointer> amounts, -// int size, -// int priorityRaw, -// Pointer error, -// Pointer pendingTransaction); - -// typedef TransactionCommit = int Function( -// Pointer, Pointer); - -typedef SecretViewKey = Pointer Function(); - -typedef PublicViewKey = Pointer Function(); - -typedef SecretSpendKey = Pointer Function(); - -typedef PublicSpendKey = Pointer Function(); - -typedef CloseCurrentWallet = void Function(); - -typedef OnStartup = void Function(); - -typedef RescanBlockchainAsync = void Function(); - -typedef AssetTypes = Pointer> Function(); - -typedef AssetTypesSize = int Function(); - -typedef GetRate = Pointer Function(); - -typedef SizeOfRate = int Function(); - -typedef UpdateRate = void Function(); - -typedef SetTrustedDaemon = void Function(int); - -typedef TrustedDaemon = int Function(); diff --git a/cw_zano/lib/api/wallet.dart b/cw_zano/lib/api/wallet.dart index 3d996bf81..712f45459 100644 --- a/cw_zano/lib/api/wallet.dart +++ b/cw_zano/lib/api/wallet.dart @@ -1,142 +1,16 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:ffi'; + import 'package:cw_core/crypto_currency.dart'; +import 'package:cw_zano/api/calls.dart' as calls; 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/zano_wallet_keys.dart'; import 'package:cw_zano/zano_balance.dart'; import 'package:cw_zano/zano_wallet.dart'; -import 'package:ffi/ffi.dart'; -import 'package:cw_zano/api/structs/ut8_box.dart'; -import 'package:cw_zano/api/convert_utf8_to_string.dart'; -import 'package:cw_zano/api/signatures.dart'; -import 'package:cw_zano/api/types.dart'; -import 'package:cw_zano/api/zano_api.dart'; -import 'package:cw_zano/api/calls.dart' as calls; import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; import 'package:mobx/mobx.dart' as mobx; -int _boolToInt(bool value) => value ? 1 : 0; - -final getFileNameNative = - zanoApi.lookup>('get_filename').asFunction(); - -/*final getSeedNative = - zanoApi.lookup>('seed').asFunction();*/ - -final getAddressNative = - zanoApi.lookup>('get_address').asFunction(); - -final getFullBalanceNative = zanoApi - .lookup>('get_full_balance') - .asFunction(); - -final getUnlockedBalanceNative = zanoApi - .lookup>('get_unlocked_balance') - .asFunction(); - -/**final getCurrentHeightNative = zanoApi - .lookup>('get_current_height') - .asFunction();*/ - -// final getNodeHeightNative = zanoApi -// .lookup>('get_node_height') -// .asFunction(); - -final isConnectedNative = - zanoApi.lookup>('is_connected').asFunction(); - -final setupNodeNative = - zanoApi.lookup>('setup_node').asFunction(); - -// final startRefreshNative = zanoApi -// .lookup>('start_refresh') -// .asFunction(); - -final connecToNodeNative = - zanoApi.lookup>('connect_to_node').asFunction(); - -final setRefreshFromBlockHeightNative = zanoApi - .lookup>('set_refresh_from_block_height') - .asFunction(); - -// final setRecoveringFromSeedNative = zanoApi -// .lookup>('set_recovering_from_seed') -// .asFunction(); - - -final storeNative = zanoApi.lookup>('store').asFunction(); - -final setPasswordNative = - zanoApi.lookup>('set_password').asFunction(); - -/**final setListenerNative = zanoApi - .lookup>('set_listener') - .asFunction();*/ - -final getSyncingHeightNative = zanoApi - .lookup>('get_syncing_height') - .asFunction(); - -final isNeededToRefreshNative = zanoApi - .lookup>('is_needed_to_refresh') - .asFunction(); - -// final isNewTransactionExistNative = zanoApi -// .lookup>('is_new_transaction_exist') -// .asFunction(); - -final getSecretViewKeyNative = - zanoApi.lookup>('secret_view_key').asFunction(); - -final getPublicViewKeyNative = - zanoApi.lookup>('public_view_key').asFunction(); - -final getSecretSpendKeyNative = zanoApi - .lookup>('secret_spend_key') - .asFunction(); - -final getPublicSpendKeyNative = zanoApi - .lookup>('public_spend_key') - .asFunction(); - -final closeCurrentWalletNative = zanoApi - .lookup>('close_current_wallet') - .asFunction(); - -final onStartupNative = - zanoApi.lookup>('on_startup').asFunction(); - -final rescanBlockchainAsyncNative = zanoApi - .lookup>('rescan_blockchain') - .asFunction(); - -// final setTrustedDaemonNative = zanoApi -// .lookup>('set_trusted_daemon') -// .asFunction(); - -final trustedDaemonNative = - zanoApi.lookup>('trusted_daemon').asFunction(); - -int getSyncingHeight() => getSyncingHeightNative(); - -bool isNeededToRefresh() => isNeededToRefreshNative() != 0; - -//bool isNewTransactionExist() => isNewTransactionExistNative() != 0; - -String getFilename() => convertUTF8ToString(pointer: getFileNameNative()); - -/**String getSeed() => convertUTF8ToString(pointer: getSeedNative());*/ - -String getAddress({int accountIndex = 0, int addressIndex = 0}) => - convertUTF8ToString(pointer: getAddressNative(accountIndex, addressIndex)); - -int getFullBalance({int accountIndex = 0}) => getFullBalanceNative(accountIndex); - -int getUnlockedBalance({int accountIndex = 0}) => getUnlockedBalanceNative(accountIndex); - int getCurrentHeight(int hWallet) { final json = calls.getWalletStatus(hWallet); final walletStatus = GetWalletStatusResult.fromJson(jsonDecode(json) as Map); @@ -149,126 +23,6 @@ int getNodeHeightSync(int hWallet) { return walletStatus.currentDaemonHeight; } -// int getWalletInfo(int hWallet) { -// final json = calls.getWalletInfo(hWallet); -// final walletInfo = GetWalletInfoResult.fromJson(jsonDecode(json) as Map); -// zanoSeed = walletInfo.wiExtended.seed; -// zanoKeys = ZanoWalletKeys( -// privateSpendKey: walletInfo.wiExtended.spendPrivateKey, -// privateViewKey: walletInfo.wiExtended.viewPrivateKey, -// publicSpendKey: walletInfo.wiExtended.spendPublicKey, -// publicViewKey: walletInfo.wiExtended.viewPublicKey, -// ); -// return 0; -// } - -// int getTxFee(int priority) { -// return calls.getCurrentTxFee(priority); -// } - -bool isConnectedSync() => isConnectedNative() != 0; - -bool setupNodeSync({ - required String address, - String? login, - String? password, - bool useSSL = false, - bool isLightWallet = false, - /*String? socksProxyAddress*/ -}) { - final addressPointer = address.toNativeUtf8(); - Pointer? loginPointer; - Pointer? socksProxyAddressPointer; - Pointer? passwordPointer; - - if (login != null) { - loginPointer = login.toNativeUtf8(); - } - - if (password != null) { - passwordPointer = password.toNativeUtf8(); - } - - /*if (socksProxyAddress != null) { - socksProxyAddressPointer = socksProxyAddress.toNativeUtf8(); - }*/ - - final errorMessagePointer = ''.toNativeUtf8(); - debugPrint( - "setup_node address $address login $login password $password useSSL $useSSL isLightWallet $isLightWallet"); - // TODO: here can be ZERO! upd: no - final isSetupNode = setupNodeNative( - addressPointer, - loginPointer, - passwordPointer, - _boolToInt(useSSL), - _boolToInt(isLightWallet), - /*socksProxyAddressPointer,*/ - errorMessagePointer) != - 0; - debugPrint("setup_node result $isSetupNode"); - - calloc.free(addressPointer); - - if (loginPointer != null) { - calloc.free(loginPointer); - } - - if (passwordPointer != null) { - calloc.free(passwordPointer); - } - - // TODO: fix it - /**if (!isSetupNode) { - throw SetupWalletException( - message: convertUTF8ToString(pointer: errorMessagePointer)); - }*/ - - return isSetupNode; -} - -//void startRefreshSync() => startRefreshNative(); - -Future connectToNode() async => connecToNodeNative() != 0; - -void setRefreshFromBlockHeight({required int height}) => setRefreshFromBlockHeightNative(height); - -// void setRecoveringFromSeed({required bool isRecovery}) => -// setRecoveringFromSeedNative(_boolToInt(isRecovery)); - -void storeSync(int hWallet) { - calls.store(hWallet); - // TODO: fixit - /*final pathPointer = ''.toNativeUtf8(); - storeNative(pathPointer); - calloc.free(pathPointer);*/ -} - -void setPasswordSync(String password) { - final passwordPointer = password.toNativeUtf8(); - final errorMessagePointer = calloc(); - final changed = setPasswordNative(passwordPointer, errorMessagePointer) != 0; - calloc.free(passwordPointer); - - if (!changed) { - final message = errorMessagePointer.ref.getValue(); - calloc.free(errorMessagePointer); - throw Exception(message); - } - - calloc.free(errorMessagePointer); -} - -void closeCurrentWallet() => closeCurrentWalletNative(); - -String getSecretViewKey() => convertUTF8ToString(pointer: getSecretViewKeyNative()); - -String getPublicViewKey() => convertUTF8ToString(pointer: getPublicViewKeyNative()); - -String getSecretSpendKey() => convertUTF8ToString(pointer: getSecretSpendKeyNative()); - -String getPublicSpendKey() => convertUTF8ToString(pointer: getPublicSpendKeyNative()); - class SyncListener { SyncListener(this.onNewBlock, this.onNewTransaction) : _cachedBlockchainHeight = 0, @@ -300,15 +54,8 @@ class SyncListener { onNewTransaction?.call(); }*/ - ///var syncHeight = getSyncingHeight(); var syncHeight = getCurrentHeight(hWallet); - if (syncHeight <= 0) { - syncHeight = getCurrentHeight(hWallet); - } - - //getWalletInfo(hWallet); - final json = calls.getWalletInfo(hWallet); final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map); wallet.seed = result.wiExtended.seed; @@ -324,8 +71,6 @@ class SyncListener { wallet.balance = mobx.ObservableMap.of( {CryptoCurrency.zano: ZanoBalance(total: balance.total, unlocked: balance.unlocked)}); - ///getTxFee(hWallet); - if (_initialSyncHeight <= 0) { _initialSyncHeight = syncHeight; } @@ -360,59 +105,3 @@ SyncListener setListeners( /**setListenerNative();*/ return listener; } - -void onStartup() => onStartupNative(); - -void _storeSync(int hWallet) => storeSync(hWallet); - -bool _setupNodeSync(Map args) { - final address = args['address'] as String; - final login = (args['login'] ?? '') as String; - final password = (args['password'] ?? '') as String; - final useSSL = args['useSSL'] as bool; - final isLightWallet = args['isLightWallet'] as bool; - /*final socksProxyAddress = (args['socksProxyAddress'] ?? '') as String;*/ - - return setupNodeSync( - address: address, - login: login, - password: password, - useSSL: useSSL, - isLightWallet: isLightWallet, /*socksProxyAddress: socksProxyAddress*/ - ); -} - -bool _isConnected(Object _) => isConnectedSync(); - -//int _getNodeHeight(Object _) => getNodeHeightSync(); - -//void startRefresh() => startRefreshSync(); - -Future setupNode( - {required String address, - String? login, - String? password, - bool useSSL = false, - /*String? socksProxyAddress,*/ - bool isLightWallet = false}) => - compute, bool>(_setupNodeSync, { - 'address': address, - 'login': login, - 'password': password, - 'useSSL': useSSL, - 'isLightWallet': isLightWallet, - //'socksProxyAddress': socksProxyAddress - }); - -Future store(int hWallet) => compute(_storeSync, 0); - -Future isConnected() => compute(_isConnected, 0); - -//Future getNodeHeight() => compute(_getNodeHeight, 0); - -void rescanBlockchainAsync() => rescanBlockchainAsyncNative(); - -// Future setTrustedDaemon(bool trusted) async => -// setTrustedDaemonNative(_boolToInt(trusted)); - -Future trustedDaemon() async => trustedDaemonNative() != 0; diff --git a/cw_zano/lib/api/wallet_manager.dart b/cw_zano/lib/api/wallet_manager.dart deleted file mode 100644 index 997e02102..000000000 --- a/cw_zano/lib/api/wallet_manager.dart +++ /dev/null @@ -1,255 +0,0 @@ -import 'dart:ffi'; -import 'package:ffi/ffi.dart'; -import 'package:flutter/foundation.dart'; -import 'package:cw_zano/api/convert_utf8_to_string.dart'; -import 'package:cw_zano/api/signatures.dart'; -import 'package:cw_zano/api/types.dart'; -import 'package:cw_zano/api/zano_api.dart'; -import 'package:cw_zano/api/exceptions/wallet_restore_from_keys_exception.dart'; - -// final createWalletNative = zanoApi -// .lookup>('create_wallet') -// .asFunction(); - -final restoreWalletFromSeedNative = zanoApi - .lookup>( - 'restore_wallet_from_seed') - .asFunction(); - -final restoreWalletFromKeysNative = zanoApi - .lookup>( - 'restore_wallet_from_keys') - .asFunction(); - -final isWalletExistNative = zanoApi - .lookup>('is_wallet_exist') - .asFunction(); - -// final loadWalletNative = zanoApi -// .lookup>('load_wallet') -// .asFunction(); - -final errorStringNative = zanoApi - .lookup>('error_string') - .asFunction(); - -/*String createWalletSync( - {required String path, - required String password, - required String language, - int nettype = 0}) { - final pathPointer = path.toNativeUtf8(); - final passwordPointer = password.toNativeUtf8(); - final languagePointer = language.toNativeUtf8(); - final errorMessagePointer = ''.toNativeUtf8(); - debugPrint("create_wallet path $path password $password language $language"); - final result = convertUTF8ToString(pointer: createWalletNative(pathPointer, passwordPointer, - languagePointer, nettype, errorMessagePointer)); - //debugPrint("create_wallet $result"); - - calloc.free(pathPointer); - calloc.free(passwordPointer); - calloc.free(languagePointer); - - return result; - /*if (hWallet == 0) { - throw WalletCreationException( - message: convertUTF8ToString(pointer: errorMessagePointer)); - } - - return hWallet; - // setupNodeSync(address: "node.moneroworld.com:18089");*/ -}*/ - -bool isWalletExistSync({required String path}) { - final pathPointer = path.toNativeUtf8(); - final isExist = isWalletExistNative(pathPointer) != 0; - - calloc.free(pathPointer); - - return isExist; -} - -// void restoreWalletFromSeedSync( -// {required String path, -// required String password, -// required String seed, -// int nettype = 0, -// int restoreHeight = 0}) { -// final pathPointer = path.toNativeUtf8(); -// final passwordPointer = password.toNativeUtf8(); -// final seedPointer = seed.toNativeUtf8(); -// final errorMessagePointer = ''.toNativeUtf8(); -// final isWalletRestored = restoreWalletFromSeedNative( -// pathPointer, -// passwordPointer, -// seedPointer, -// nettype, -// restoreHeight, -// errorMessagePointer) != -// 0; - -// calloc.free(pathPointer); -// calloc.free(passwordPointer); -// calloc.free(seedPointer); - -// if (!isWalletRestored) { -// throw WalletRestoreFromSeedException( -// message: convertUTF8ToString(pointer: errorMessagePointer)); -// } -// } - -void restoreWalletFromKeysSync( - {required String path, - required String password, - required String language, - required String address, - required String viewKey, - required String spendKey, - int nettype = 0, - int restoreHeight = 0}) { - final pathPointer = path.toNativeUtf8(); - final passwordPointer = password.toNativeUtf8(); - final languagePointer = language.toNativeUtf8(); - final addressPointer = address.toNativeUtf8(); - final viewKeyPointer = viewKey.toNativeUtf8(); - final spendKeyPointer = spendKey.toNativeUtf8(); - final errorMessagePointer = ''.toNativeUtf8(); - final isWalletRestored = restoreWalletFromKeysNative( - pathPointer, - passwordPointer, - languagePointer, - addressPointer, - viewKeyPointer, - spendKeyPointer, - nettype, - restoreHeight, - errorMessagePointer) != - 0; - - calloc.free(pathPointer); - calloc.free(passwordPointer); - calloc.free(languagePointer); - calloc.free(addressPointer); - calloc.free(viewKeyPointer); - calloc.free(spendKeyPointer); - - if (!isWalletRestored) { - throw WalletRestoreFromKeysException( - message: convertUTF8ToString(pointer: errorMessagePointer)); - } -} - -// void loadWallet( -// {required String path, required String password, int nettype = 0}) { -// final pathPointer = path.toNativeUtf8(); -// final passwordPointer = password.toNativeUtf8(); -// final loaded = loadWalletNative(pathPointer, passwordPointer, nettype) != 0; -// calloc.free(pathPointer); -// calloc.free(passwordPointer); - -// if (!loaded) { -// throw WalletOpeningException( -// message: convertUTF8ToString(pointer: errorStringNative())); -// } -// } - -// String _createWallet(Map args) { -// final path = args['path'] as String; -// final password = args['password'] as String; -// final language = args['language'] as String; - -// return createWalletSync(path: path, password: password, language: language); -// } - -// void _restoreFromSeed(Map args) { -// final path = args['path'] as String; -// final password = args['password'] as String; -// final seed = args['seed'] as String; -// final restoreHeight = args['restoreHeight'] as int; - -// restoreWalletFromSeedSync( -// path: path, password: password, seed: seed, restoreHeight: restoreHeight); -// } - -void _restoreFromKeys(Map args) { - final path = args['path'] as String; - final password = args['password'] as String; - final language = args['language'] as String; - final restoreHeight = args['restoreHeight'] as int; - final address = args['address'] as String; - final viewKey = args['viewKey'] as String; - final spendKey = args['spendKey'] as String; - - restoreWalletFromKeysSync( - path: path, - password: password, - language: language, - restoreHeight: restoreHeight, - address: address, - viewKey: viewKey, - spendKey: spendKey); -} - -// Future _openWallet(Map args) async => loadWallet( -// path: args['path'] as String, password: args['password'] as String); - -bool _isWalletExist(String path) => isWalletExistSync(path: path); - -// void openWallet( -// {required String path, -// required String password, -// int nettype = 0}) async => -// loadWallet(path: path, password: password, nettype: nettype); - -// Future openWalletAsync(Map args) async => -// compute(_openWallet, args); - -// Future createWallet( -// {required String path, -// required String password, -// required String language, -// int nettype = 0}) async => -// compute, String>(_createWallet, { -// 'path': path, -// 'password': password, -// 'language': language, -// 'nettype': nettype -// }); - -// Future restoreFromSeed( -// {required String path, -// required String password, -// required String seed, -// int nettype = 0, -// int restoreHeight = 0}) async => -// compute, void>(_restoreFromSeed, { -// 'path': path, -// 'password': password, -// 'seed': seed, -// 'nettype': nettype, -// 'restoreHeight': restoreHeight -// }); - -Future restoreFromKeys( - {required String path, - required String password, - required String language, - required String address, - required String viewKey, - required String spendKey, - int nettype = 0, - int restoreHeight = 0}) async => - compute, void>(_restoreFromKeys, { - 'path': path, - 'password': password, - 'language': language, - 'address': address, - 'viewKey': viewKey, - 'spendKey': spendKey, - 'nettype': nettype, - 'restoreHeight': restoreHeight - }); - -Future isWalletExist({required String path}) => - compute(_isWalletExist, path); diff --git a/cw_zano/lib/pending_zano_transaction.dart b/cw_zano/lib/pending_zano_transaction.dart index 4f680463d..b3a293784 100644 --- a/cw_zano/lib/pending_zano_transaction.dart +++ b/cw_zano/lib/pending_zano_transaction.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:cw_zano/api/exceptions/transfer_exception.dart'; import 'package:cw_zano/api/model/destination.dart'; import 'package:cw_zano/api/model/transfer_params.dart'; import 'package:cw_zano/api/model/transfer_result.dart'; @@ -11,7 +12,8 @@ import 'package:cw_zano/zano_wallet.dart'; class PendingZanoTransaction with PendingTransaction { PendingZanoTransaction( - {required this.fee, + {required this.zanoWallet, + required this.fee, required this.intAmount, //required this.stringAmount, required this.hWallet, @@ -19,6 +21,7 @@ class PendingZanoTransaction with PendingTransaction { required this.assetId, required this.comment}); + final ZanoWalletBase zanoWallet; final int hWallet; final int intAmount; //final String stringAmount; @@ -52,7 +55,7 @@ class PendingZanoTransaction with PendingTransaction { TransferParams( destinations: [ Destination( - amount: intAmount.toString(), //stringAmount, + amount: intAmount.toString(), address: address, assetId: assetId, ) @@ -66,22 +69,23 @@ class PendingZanoTransaction with PendingTransaction { )); print('transfer result $result'); final map = jsonDecode(result); - if (map["result"] != null && map["result"]["result"] != null ) { + if (map['result'] != null && map['result']['result'] != null ) { transferResult = TransferResult.fromJson( - map["result"]["result"] as Map, + map['result']['result'] as Map, ); + await zanoWallet.fetchTransactions(); + } else if (map['result'] != null && map['result']['error'] != null) { + final String code; + if (map['result']['error']['code'] is int) { + code = (map['result']['error']['code'] as int).toString(); + } else if (map['result']['error']['code'] is String) { + code = map['result']['error']['code'] as String; + } else { + code = ''; + } + final message = map['result']['error']['message'] as String; + print('transfer error $code $message'); + throw TransferException(code, message); } - // try { - // zano_transaction_history.commitTransactionFromPointerAddress( - // address: pendingTransactionDescription.pointerAddress); - // } catch (e) { - // final message = e.toString(); - - // if (message.contains('Reason: double spend')) { - // throw DoubleSpendException(); - // } - - // rethrow; - // } } } diff --git a/cw_zano/lib/zano_transaction_info.dart b/cw_zano/lib/zano_transaction_info.dart index 02ff3fff2..c332bf4d6 100644 --- a/cw_zano/lib/zano_transaction_info.dart +++ b/cw_zano/lib/zano_transaction_info.dart @@ -32,37 +32,6 @@ class ZanoTransactionInfo extends TransactionInfo { assetType = 'ZANO', // TODO: FIXIT: recipientAddress = history.remoteAddresses.isNotEmpty ? history.remoteAddresses.first : ''; - /*ZanoTransactionInfo.fromMap(Map map) - : id = (map['hash'] ?? '') as String, - height = (map['height'] ?? 0) as int, - direction = - parseTransactionDirectionFromNumber(map['direction'] as String) ?? - TransactionDirection.incoming, - date = DateTime.fromMillisecondsSinceEpoch( - int.parse(map['timestamp'] as String? ?? '0') * 1000), - isPending = parseBoolFromString(map['isPending'] as String), - amount = map['amount'] as int, - accountIndex = int.parse(map['accountIndex'] as String), - addressIndex = map['addressIndex'] as int, - confirmations = map['confirmations'] as int, - key = getTxKey((map['hash'] ?? '') as String), - fee = map['fee'] as int? ?? 0;*/ - - /*ZanoTransactionInfo.fromRow(TransactionInfoRow row) - : id = row.getHash(), - height = row.blockHeight, - direction = parseTransactionDirectionFromInt(row.direction) ?? - TransactionDirection.incoming, - date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000), - isPending = row.isPending != 0, - amount = row.getAmount(), - accountIndex = row.subaddrAccount, - addressIndex = row.subaddrIndex, - confirmations = row.confirmations, - key = null, //getTxKey(row.getHash()), - fee = row.fee, - assetType = row.getAssetType();*/ - final String id; final int height; final TransactionDirection direction; diff --git a/cw_zano/lib/zano_wallet.dart b/cw_zano/lib/zano_wallet.dart index 9d7e8d5ae..bc7fa92f7 100644 --- a/cw_zano/lib/zano_wallet.dart +++ b/cw_zano/lib/zano_wallet.dart @@ -25,6 +25,7 @@ import 'package:cw_zano/zano_transaction_history.dart'; import 'package:cw_zano/zano_transaction_info.dart'; import 'package:cw_zano/zano_wallet_addresses.dart'; import 'package:ffi/ffi.dart'; +import 'package:flutter/material.dart'; import 'package:mobx/mobx.dart'; part 'zano_wallet.g.dart'; @@ -103,10 +104,10 @@ abstract class ZanoWalletBase await updateTransactions(); if (walletInfo.isRecovery) { - ///zano_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery); + debugPrint('setRecoveringFromSeed isRecovery ${walletInfo.isRecovery}'); if (zano_wallet.getCurrentHeight(hWallet) <= 1) { - zano_wallet.setRefreshFromBlockHeight(height: walletInfo.restoreHeight); + debugPrint('setRefreshFromBlockHeight height ${walletInfo.restoreHeight}'); } } @@ -128,7 +129,7 @@ abstract class ZanoWalletBase Future connectToNode({required Node node}) async { try { syncStatus = ConnectingSyncStatus(); - await zano_wallet.setupNode( + await calls.setupNode( address: "195.201.107.230:33336", // node.uriRaw, login: "", // node.login, password: "", // node.password, @@ -153,8 +154,7 @@ abstract class ZanoWalletBase try { syncStatus = AttemptingSyncStatus(); - //zano_wallet.startRefresh(); - print("start refresh"); + debugPrint("startRefresh"); _setListeners(); _listener?.start(this, hWallet); } catch (e) { @@ -172,31 +172,11 @@ abstract class ZanoWalletBase ? output.extractedAddress! : output.address; final stringAmount = output.sendAll ? null : output.cryptoAmount!.replaceAll(',', '.'); - //final int? formattedAmount = output.sendAll ? null : output.formattedCryptoAmount; final fee = calculateEstimatedFee(creds.priority); - // final result = await calls.transfer( - // hWallet, - // TransferParams( - // destinations: [ - // Destination( - // amount: amount!, - // address: address, - // assetId: assetId, - // ) - // ], - // fee: fee, - // mixin: zanoMixin, - // paymentId: '', // TODO: fixit - // comment: output.note ?? '', - // pushPayer: false, - // hideReceiver: false, - // )); final intAmount = (double.parse(stringAmount!) * pow(10, 12)).toInt(); - // final description = PendingTransactionDescription( - // amount: iAmount, fee: fee, hash: '', pointerAddress: 0); final transaction = PendingZanoTransaction(fee: fee, intAmount: intAmount, hWallet: hWallet, address: address, assetId: assetId, - comment: output.note ?? ''); + comment: output.note ?? '', zanoWallet: this); return transaction; /*final _credentials = credentials as ZanoTransactionCreationCredentials; @@ -277,7 +257,7 @@ abstract class ZanoWalletBase Future save() async { await walletAddresses.updateAddressesInBox(); await backupWalletFiles(name); - await zano_wallet.store(hWallet); + await calls.store(hWallet); } @override @@ -306,13 +286,9 @@ abstract class ZanoWalletBase @override Future changePassword(String password) async { - zano_wallet.setPasswordSync(password); + calls.setPassword(hWallet: hWallet, password: password); } - //Future getNodeHeight() async => zano_wallet.getNodeHeight(); - - Future isConnected() async => zano_wallet.isConnected(); - Future setAsRecovered() async { walletInfo.isRecovery = false; await walletInfo.save(); @@ -322,8 +298,8 @@ abstract class ZanoWalletBase Future rescan({required int height}) async { walletInfo.restoreHeight = height; walletInfo.isRecovery = true; - zano_wallet.setRefreshFromBlockHeight(height: height); - zano_wallet.rescanBlockchainAsync(); + debugPrint('setRefreshFromBlockHeight height $height'); + debugPrint('rescanBlockchainAsync'); await startSync(); _askForUpdateBalance(); /**walletAddresses.accountList.update();*/ @@ -332,9 +308,6 @@ abstract class ZanoWalletBase await walletInfo.save(); } - String getTransactionAddress(int accountIndex, int addressIndex) => - zano_wallet.getAddress(accountIndex: accountIndex, addressIndex: addressIndex); - Future _refreshTransactions() async { final result = await calls.getRecentTxsAndInfo(hWallet: hWallet, offset: 0, count: 30); final map = jsonDecode(result); @@ -403,36 +376,17 @@ abstract class ZanoWalletBase if (currentHeight <= 1) { final height = _getHeightByDate(walletInfo.date); - ///zano_wallet.setRecoveringFromSeed(isRecovery: true); - zano_wallet.setRefreshFromBlockHeight(height: height); + debugPrint('setRecoveringFromSeed isRecovery true'); + debugPrint('setRefreshFromBlockHeight height $height'); } } - // int _getHeightDistance(DateTime date) { - // final distance = - // DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch; - // final daysTmp = (distance / 86400).round(); - // final days = daysTmp < 1 ? 1 : daysTmp; - - // return days * 1000; - // } - int _getHeightByDate(DateTime date) { - // TODO: !!! 12/10 commented return 0; - // final nodeHeight = zano_wallet.getNodeHeightSync(); - // final heightDistance = _getHeightDistance(date); - - // if (nodeHeight <= 0) { - // return 0; - // } - - // return nodeHeight - heightDistance; } void _askForUpdateBalance() { - print("ask for update balance"); - //balance.addAll(getZanoBalance()); + debugPrint('askForUpdateBalance'); } Future _askForUpdateTransactionHistory() async => await updateTransactions(); diff --git a/cw_zano/lib/zano_wallet_service.dart b/cw_zano/lib/zano_wallet_service.dart index 9cca7c4f4..74b981724 100644 --- a/cw_zano/lib/zano_wallet_service.dart +++ b/cw_zano/lib/zano_wallet_service.dart @@ -11,8 +11,11 @@ import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_service.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:cw_zano/api/calls.dart' as calls; +import 'package:cw_zano/api/consts.dart'; +import 'package:cw_zano/api/exceptions/already_exists_exception.dart'; +import 'package:cw_zano/api/exceptions/restore_from_seed_exception.dart'; +import 'package:cw_zano/api/exceptions/wrong_seed_exception.dart'; import 'package:cw_zano/api/model/create_wallet_result.dart'; -import 'package:cw_zano/api/wallet_manager.dart' as zano_wallet_manager; import 'package:cw_zano/zano_balance.dart'; import 'package:cw_zano/zano_wallet.dart'; import 'package:hive/hive.dart'; @@ -106,7 +109,7 @@ class ZanoWalletService extends WalletService isWalletExit(String name) async { try { final path = await pathForWallet(name: name, type: getType()); - return zano_wallet_manager.isWalletExist(path: path); + return calls.isWalletExist(path: path); } catch (e) { // TODO: Implement Exception for wallet list service. print('ZanoWalletsManager Error: $e'); @@ -188,25 +191,7 @@ class ZanoWalletService extends WalletService restoreFromKeys(ZanoRestoreWalletFromKeysCredentials credentials) async { - try { - final path = await pathForWallet(name: credentials.name, type: getType()); - await zano_wallet_manager.restoreFromKeys( - path: path, - password: credentials.password!, - language: credentials.language, - restoreHeight: credentials.height!, - address: credentials.address, - viewKey: credentials.viewKey, - spendKey: credentials.spendKey); - final wallet = ZanoWallet(credentials.walletInfo!); - await wallet.init(); - - return wallet; - } catch (e) { - // TODO: Implement Exception for wallet list service. - print('ZanoWalletsManager Error: $e'); - rethrow; - } + throw UnimplementedError("Restore from keys not implemented"); } @override @@ -216,12 +201,20 @@ class ZanoWalletService extends WalletService; + final map = json.decode(result) as Map; if (map['result'] != null) { final createWalletResult = CreateWalletResult.fromJson(map['result'] as Map); _parseCreateWalletResult(createWalletResult, wallet); + } else if (map['error'] != null) { + final code = map['error']['code'] as String; + final message = map['error']['message'] as String; + if (code == Consts.errorWrongSeed) { + throw WrongSeedException(message); + } else if (code == Consts.errorAlreadyExists) { + throw AlreadyExistsException(message); + } + throw RestoreFromSeedException(code, message); } await calls.store(hWallet); await wallet.init(); diff --git a/lib/zano/cw_zano.dart b/lib/zano/cw_zano.dart index 28690bad0..9ef9c3983 100644 --- a/lib/zano/cw_zano.dart +++ b/lib/zano/cw_zano.dart @@ -218,7 +218,7 @@ class CWZano extends Zano { @override void onStartup() { - monero_wallet_api.onStartup(); + debugPrint("onStartup"); } @override @@ -232,11 +232,11 @@ class CWZano extends Zano { return ZanoWalletService(walletInfoSource); } - @override - String getTransactionAddress(Object wallet, int accountIndex, int addressIndex) { - final zanoWallet = wallet as ZanoWallet; - return zanoWallet.getTransactionAddress(accountIndex, addressIndex); - } + // @override + // String getTransactionAddress(Object wallet, int accountIndex, int addressIndex) { + // final zanoWallet = wallet as ZanoWallet; + // return zanoWallet.getTransactionAddress(accountIndex, addressIndex); + // } @override CryptoCurrency assetOfTransaction(TransactionInfo tx) { diff --git a/lib/zano/zano.dart b/lib/zano/zano.dart index 7f8a439ee..b4d22b1ac 100644 --- a/lib/zano/zano.dart +++ b/lib/zano/zano.dart @@ -19,11 +19,8 @@ import 'package:cw_zano/zano_wallet_service.dart'; import 'package:cw_zano/zano_wallet.dart'; import 'package:cw_zano/zano_transaction_info.dart'; import 'package:cw_zano/zano_transaction_history.dart'; -import 'package:cw_core/account.dart' as monero_account; -import 'package:cw_zano/api/wallet.dart' as monero_wallet_api; import 'package:cw_zano/mnemonics/english.dart'; import 'package:cw_zano/zano_transaction_creation_credentials.dart'; -import 'package:cw_zano/api/balance_list.dart'; part 'cw_zano.dart'; @@ -95,7 +92,7 @@ abstract class Zano { ZanoWalletDetails getZanoWalletDetails(Object wallet); - String getTransactionAddress(Object wallet, int accountIndex, int addressIndex); + // String getTransactionAddress(Object wallet, int accountIndex, int addressIndex); int getHeightByDate({required DateTime date}); Future getCurrentHeight();