From e4c775bffe886f36b7f1a87cbfdb3cb9218584b4 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 16 Dec 2023 15:00:22 +0000 Subject: [PATCH] send + receive qr code --- cw_zano/lib/api/calls.dart | 17 +++++++ .../exceptions/create_wallet_exception.dart | 7 +++ .../api/model/get_address_info_result.dart | 16 +++++++ .../api/model/get_wallet_status_result.dart | 9 ---- cw_zano/lib/zano_utils.dart | 17 +++++++ cw_zano/lib/zano_wallet.dart | 3 +- cw_zano/lib/zano_wallet_addresses.dart | 7 ++- cw_zano/lib/zano_wallet_service.dart | 47 ++++++------------- lib/core/address_validator.dart | 7 ++- lib/core/validator.dart | 2 +- .../exchange/exchange_view_model.dart | 7 ++- lib/zano.dart | 3 +- lib/zano/cw_zano.dart | 5 +- 13 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 cw_zano/lib/api/exceptions/create_wallet_exception.dart create mode 100644 cw_zano/lib/api/model/get_address_info_result.dart create mode 100644 cw_zano/lib/zano_utils.dart diff --git a/cw_zano/lib/api/calls.dart b/cw_zano/lib/api/calls.dart index 402ada3bd..5a7593c77 100644 --- a/cw_zano/lib/api/calls.dart +++ b/cw_zano/lib/api/calls.dart @@ -131,6 +131,23 @@ bool isWalletExist({required String path}) { return isExist; } +// char* get_address_info(char* address) +final _getAddressInfoNative = zanoApi + .lookup>('get_address_info') + .asFunction<_GetAddressInfo>(); +typedef _get_address_info = Pointer Function(Pointer password); +typedef _GetAddressInfo = Pointer Function(Pointer address); + +String getAddressInfo(String address) { + debugPrint('get address info $address'); + final addressPointer = address.toNativeUtf8(); + final result = + convertUTF8ToString(pointer: _getAddressInfoNative(addressPointer)); + debugPrint('get address info result $result'); + calloc.free(addressPointer); + return result; +} + bool setupNode({ required String address, String? login, diff --git a/cw_zano/lib/api/exceptions/create_wallet_exception.dart b/cw_zano/lib/api/exceptions/create_wallet_exception.dart new file mode 100644 index 000000000..199f743a0 --- /dev/null +++ b/cw_zano/lib/api/exceptions/create_wallet_exception.dart @@ -0,0 +1,7 @@ +class CreateWalletException implements Exception { + final String message; + + CreateWalletException(this.message): super(); + @override + String toString() => '${this.runtimeType}(message: $message)'; +} \ No newline at end of file diff --git a/cw_zano/lib/api/model/get_address_info_result.dart b/cw_zano/lib/api/model/get_address_info_result.dart new file mode 100644 index 000000000..4d3db12f6 --- /dev/null +++ b/cw_zano/lib/api/model/get_address_info_result.dart @@ -0,0 +1,16 @@ +class GetAddressInfoResult { + final bool valid; + final bool auditable; + final bool paymentId; + final bool wrap; + + 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, + ); +} diff --git a/cw_zano/lib/api/model/get_wallet_status_result.dart b/cw_zano/lib/api/model/get_wallet_status_result.dart index f58d16a01..85a80279f 100644 --- a/cw_zano/lib/api/model/get_wallet_status_result.dart +++ b/cw_zano/lib/api/model/get_wallet_status_result.dart @@ -23,13 +23,4 @@ class GetWalletStatusResult { progress: json['progress'] as int, walletState: json['wallet_state'] as int, ); - /* - "current_daemon_height": 238049, - "current_wallet_height": 238038, - "is_daemon_connected": true, - "is_in_long_refresh": true, - "progress": 0, - "wallet_state": 1 - - */ } diff --git a/cw_zano/lib/zano_utils.dart b/cw_zano/lib/zano_utils.dart new file mode 100644 index 000000000..54235e3fa --- /dev/null +++ b/cw_zano/lib/zano_utils.dart @@ -0,0 +1,17 @@ +import 'dart:convert'; + +import 'package:cw_zano/api/calls.dart' as calls; +import 'package:cw_zano/api/model/get_address_info_result.dart'; + +class ZanoUtils { + static bool validateAddress(String address) { + try { + final result = GetAddressInfoResult.fromJson( + jsonDecode(calls.getAddressInfo(address)) as Map, + ); + return result.valid; + } catch (err) { + return false; + } + } +} diff --git a/cw_zano/lib/zano_wallet.dart b/cw_zano/lib/zano_wallet.dart index bc7fa92f7..b071cb67b 100644 --- a/cw_zano/lib/zano_wallet.dart +++ b/cw_zano/lib/zano_wallet.dart @@ -97,8 +97,9 @@ abstract class ZanoWalletBase _hWallet = value; } - Future init() async { + Future init(String address) async { await walletAddresses.init(); + await walletAddresses.updateAddress(address); ///balance.addAll(getZanoBalance(/**accountIndex: walletAddresses.account?.id ?? 0*/)); _setListeners(); await updateTransactions(); diff --git a/cw_zano/lib/zano_wallet_addresses.dart b/cw_zano/lib/zano_wallet_addresses.dart index 888a59051..d2c7d14cd 100644 --- a/cw_zano/lib/zano_wallet_addresses.dart +++ b/cw_zano/lib/zano_wallet_addresses.dart @@ -32,7 +32,12 @@ abstract class ZanoWalletAddressesBase extends WalletAddresses with Store { /*accountList.update(); account = accountList.accounts.first;*/ /**updateSubaddressList(accountIndex: account?.id ?? 0);*/ - address = walletInfo.address; + //address = walletInfo.address; + //await updateAddressesInBox(); + } + + Future updateAddress(String address) async { + this.address = address; await updateAddressesInBox(); } diff --git a/cw_zano/lib/zano_wallet_service.dart b/cw_zano/lib/zano_wallet_service.dart index 74b981724..d057c72ff 100644 --- a/cw_zano/lib/zano_wallet_service.dart +++ b/cw_zano/lib/zano_wallet_service.dart @@ -13,6 +13,7 @@ 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/create_wallet_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'; @@ -65,38 +66,19 @@ class ZanoWalletService extends WalletService WalletType.zano; - // @override - // Future create(WalletCredentials credentials) async { - // try { - // final wallet = ZanoWallet(credentials.walletInfo!); - // wallet.connectToNode(node: Node()); // TODO: Node() ??? - // //wallet.setupNode(address: "195.201.107.230:33336", login: "", password: ""); - // final path = await pathForWallet(name: credentials.name, type: getType()); - // wallet.createWallet(path: path, password: credentials.password!); - // return wallet; - // } catch (e) { - // print("ZanoWalletService.create error $e"); - // rethrow; - // } - // } - @override Future create(WalletCredentials credentials) async { try { final wallet = ZanoWallet(credentials.walletInfo!); await wallet.connectToNode(node: Node()); final path = await pathForWallet(name: credentials.name, type: getType()); - final result = calls.createWallet( - language: "", path: path, password: credentials.password!); + final result = calls.createWallet(language: "", path: path, password: credentials.password!); final map = json.decode(result) as Map; - if (map['result'] != null) { - final createWalletResult = - CreateWalletResult.fromJson(map['result'] as Map); - _parseCreateWalletResult(createWalletResult, wallet); - } - // TODO: remove it TODO why? + if (map['result'] == null) throw CreateWalletException(''); + final createWalletResult = CreateWalletResult.fromJson(map['result'] as Map); + _parseCreateWalletResult(createWalletResult, wallet); await calls.store(hWallet); - await wallet.init(); + await wallet.init(createWalletResult.wi.address); return wallet; } catch (e) { // TODO: Implement Exception for wallet list service. @@ -133,13 +115,11 @@ class ZanoWalletService extends WalletService; - if (map['result'] != null) { - final createWalletResult = - CreateWalletResult.fromJson(map['result'] as Map); - _parseCreateWalletResult(createWalletResult, wallet); - } + if (map['result'] == null) throw CreateWalletException(''); + final createWalletResult = CreateWalletResult.fromJson(map['result'] as Map); + _parseCreateWalletResult(createWalletResult, wallet); await calls.store(hWallet); - await wallet.init(); + await wallet.init(createWalletResult.wi.address); return wallet; } catch (e) { rethrow; @@ -206,6 +186,9 @@ class ZanoWalletService extends WalletService); _parseCreateWalletResult(createWalletResult, wallet); + await calls.store(hWallet); + await wallet.init(createWalletResult.wi.address); + return wallet; } else if (map['error'] != null) { final code = map['error']['code'] as String; final message = map['error']['message'] as String; @@ -216,9 +199,7 @@ class ZanoWalletService extends WalletService { final valueMatched = match(value); final valueValidated = useAdditionalValidation != null - ? useAdditionalValidation!(value) || valueMatched + ? useAdditionalValidation!(value) && valueMatched : valueMatched; return valueValidated; diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index f046d0006..754fff23c 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -695,10 +695,9 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with receiveCurrency = CryptoCurrency.xmr; break; case WalletType.zano: - // TODO: !!! - // depositCurrency = CryptoCurrency.zano; - // receiveCurrency = ??? - throw UnimplementedError(); + depositCurrency = CryptoCurrency.zano; + receiveCurrency = CryptoCurrency.btc; + break; case WalletType.dummy: // TODO: !!! // depositCurrency = CryptoCurrency.dummy; diff --git a/lib/zano.dart b/lib/zano.dart index b20bbc973..b12864da4 100644 --- a/lib/zano.dart +++ b/lib/zano.dart @@ -11,7 +11,6 @@ import 'package:cw_zano/api/calls.dart' as calls; import 'package:cw_zano/api/model/balance.dart'; import 'package:cw_zano/api/model/create_wallet_result.dart'; import 'package:cw_zano/api/wallet.dart' as zano_wallet; -import 'package:cw_zano/api/wallet_manager.dart' as zano_wallet_manager; import 'package:cw_zano/zano_wallet_service.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -85,7 +84,7 @@ const walletName = 'walletName'; Future init() async { version = calls.getVersion(); - final setupNode = await zano_wallet.setupNode( + final setupNode = await calls.setupNode( address: '195.201.107.230:33336', login: '', password: '', diff --git a/lib/zano/cw_zano.dart b/lib/zano/cw_zano.dart index 9ef9c3983..7c283759e 100644 --- a/lib/zano/cw_zano.dart +++ b/lib/zano/cw_zano.dart @@ -69,10 +69,7 @@ class CWZanoWalletDetails extends ZanoWalletDetails { ZanoBalance get balance { final zanoWallet = _wallet as ZanoWallet; final balance = zanoWallet.balance; - throw Exception('Unimplemented'); - //return ZanoBalance( - // fullBalance: balance.fullBalance, - // unlockedBalance: balance.unlockedBalance); + return ZanoBalance(fullBalance: balance[CryptoCurrency.zano]!.total, unlockedBalance: balance[CryptoCurrency.zano]!.unlocked); } }