import 'dart:async'; import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'package:cw_wownero/api/convert_utf8_to_string.dart'; import 'package:cw_wownero/api/signatures.dart'; import 'package:cw_wownero/api/types.dart'; import 'package:cw_wownero/api/wownero_api.dart'; import 'package:cw_wownero/api/exceptions/setup_wallet_exception.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; int _boolToInt(bool value) => value ? 1 : 0; final getFileNameNative = wowneroApi .lookup>('get_filename') .asFunction(); final getSeedNative = wowneroApi.lookup>('seed').asFunction(); final getAddressNative = wowneroApi .lookup>('get_address') .asFunction(); final getFullBalanceNative = wowneroApi .lookup>('get_full_balance') .asFunction(); final getUnlockedBalanceNative = wowneroApi .lookup>('get_unlocked_balance') .asFunction(); final getCurrentHeightNative = wowneroApi .lookup>('get_current_height') .asFunction(); final getNodeHeightNative = wowneroApi .lookup>('get_node_height') .asFunction(); final getSeedHeightNative = wowneroApi .lookup>('get_seed_height') .asFunction(); final isConnectedNative = wowneroApi .lookup>('is_connected') .asFunction(); final setupNodeNative = wowneroApi .lookup>('setup_node') .asFunction(); final startRefreshNative = wowneroApi .lookup>('start_refresh') .asFunction(); final connecToNodeNative = wowneroApi .lookup>('connect_to_node') .asFunction(); final setRefreshFromBlockHeightNative = wowneroApi .lookup>( 'set_refresh_from_block_height') .asFunction(); final setRecoveringFromSeedNative = wowneroApi .lookup>( 'set_recovering_from_seed') .asFunction(); final storeNative = wowneroApi.lookup>('store').asFunction(); final setListenerNative = wowneroApi .lookup>('set_listener') .asFunction(); final getSyncingHeightNative = wowneroApi .lookup>('get_syncing_height') .asFunction(); final isNeededToRefreshNative = wowneroApi .lookup>('is_needed_to_refresh') .asFunction(); final isNewTransactionExistNative = wowneroApi .lookup>( 'is_new_transaction_exist') .asFunction(); final getSecretViewKeyNative = wowneroApi .lookup>('secret_view_key') .asFunction(); final getPublicViewKeyNative = wowneroApi .lookup>('public_view_key') .asFunction(); final getSecretSpendKeyNative = wowneroApi .lookup>('secret_spend_key') .asFunction(); final getPublicSpendKeyNative = wowneroApi .lookup>('public_spend_key') .asFunction(); final closeCurrentWalletNative = wowneroApi .lookup>('close_current_wallet') .asFunction(); final onStartupNative = wowneroApi .lookup>('on_startup') .asFunction(); final rescanBlockchainAsyncNative = wowneroApi .lookup>('rescan_blockchain') .asFunction(); final getSubaddressLabelNative = wowneroApi .lookup>('get_subaddress_label') .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() => getCurrentHeightNative(); int getNodeHeightSync() => getNodeHeightNative(); int getSeedHeightSync(String seed) { final seedPointer = Utf8.toUtf8(seed); final ret = getSeedHeightNative(seedPointer); free(seedPointer); return ret; } bool isConnectedSync() => isConnectedNative() != 0; bool setupNodeSync( {String address, String login, String password, bool useSSL = false, bool isLightWallet = false}) { final addressPointer = Utf8.toUtf8(address); Pointer loginPointer; Pointer passwordPointer; if (login != null) { loginPointer = Utf8.toUtf8(login); } if (password != null) { passwordPointer = Utf8.toUtf8(password); } final errorMessagePointer = allocate(); final isSetupNode = setupNodeNative( addressPointer, loginPointer, passwordPointer, _boolToInt(useSSL), _boolToInt(isLightWallet), errorMessagePointer) != 0; free(addressPointer); free(loginPointer); free(passwordPointer); if (!isSetupNode) { throw SetupWalletException( message: convertUTF8ToString(pointer: errorMessagePointer)); } return isSetupNode; } void startRefreshSync() => startRefreshNative(); Future connectToNode() async => connecToNodeNative() != 0; void setRefreshFromBlockHeight({int height}) => setRefreshFromBlockHeightNative(height); void setRecoveringFromSeed({bool isRecovery}) => setRecoveringFromSeedNative(_boolToInt(isRecovery)); void storeSync() { final pathPointer = Utf8.toUtf8(''); storeNative(pathPointer); free(pathPointer); } 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; _lastKnownBlockHeight = 0; _initialSyncHeight = 0; } void Function(int, int, double) onNewBlock; void Function() onNewTransaction; Timer _updateSyncInfoTimer; int _cachedBlockchainHeight; int _lastKnownBlockHeight; int _initialSyncHeight; Future getNodeHeightOrUpdate(int baseHeight) async { if (_cachedBlockchainHeight < baseHeight || _cachedBlockchainHeight == 0) { _cachedBlockchainHeight = await getNodeHeight(); } return _cachedBlockchainHeight; } void start() { _cachedBlockchainHeight = 0; _lastKnownBlockHeight = 0; _initialSyncHeight = 0; _updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: 1200), (_) async { if (isNewTransactionExist()) { onNewTransaction?.call(); } var syncHeight = getSyncingHeight(); if (syncHeight <= 0) { syncHeight = getCurrentHeight(); } if (_initialSyncHeight <= 0) { _initialSyncHeight = syncHeight; } final bchHeight = await getNodeHeightOrUpdate(syncHeight); if (_lastKnownBlockHeight == syncHeight || syncHeight == null) { return; } _lastKnownBlockHeight = syncHeight; final track = bchHeight - _initialSyncHeight; final diff = track - (bchHeight - syncHeight); final ptc = diff <= 0 ? 0.0 : diff / track; final left = bchHeight - syncHeight; if (syncHeight < 0 || left < 0) { return; } // 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents; onNewBlock?.call(syncHeight, left, ptc); }); } void stop() => _updateSyncInfoTimer?.cancel(); } SyncListener setListeners(void Function(int, int, double) onNewBlock, void Function() onNewTransaction) { final listener = SyncListener(onNewBlock, onNewTransaction); setListenerNative(); return listener; } void onStartup() => onStartupNative(); void _storeSync(Object _) => storeSync(); 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; return setupNodeSync( address: address, login: login, password: password, useSSL: useSSL, isLightWallet: isLightWallet); } bool _isConnected(Object _) => isConnectedSync(); int _getNodeHeight(Object _) => getNodeHeightSync(); void startRefresh() => startRefreshSync(); Future setupNode( {String address, String login, String password, bool useSSL = false, bool isLightWallet = false}) => compute, void>(_setupNodeSync, { 'address': address, 'login': login, 'password': password, 'useSSL': useSSL, 'isLightWallet': isLightWallet }); Future store() => compute(_storeSync, 0); Future isConnected() => compute(_isConnected, 0); Future getNodeHeight() => compute(_getNodeHeight, 0); void rescanBlockchainAsync() => rescanBlockchainAsyncNative(); String getSubaddressLabel(int accountIndex, int addressIndex) { return convertUTF8ToString(pointer: getSubaddressLabelNative(accountIndex, addressIndex)); }