diff --git a/cw_monero/ios/Classes/monero_api.cpp b/cw_monero/ios/Classes/monero_api.cpp index 87be785ac..01a8d9a51 100644 --- a/cw_monero/ios/Classes/monero_api.cpp +++ b/cw_monero/ios/Classes/monero_api.cpp @@ -399,6 +399,7 @@ extern "C" return false; } + wallet->store(std::string(path)); change_current_wallet(wallet); return true; } @@ -464,6 +465,16 @@ extern "C" return strdup(get_current_wallet()->address(account_index, address_index).c_str()); } + char *get_cache_attribute(char *name) + { + return strdup(get_current_wallet()->getCacheAttribute(std::string(name)).c_str()); + } + + bool set_cache_attribute(char *name, char *value) + { + get_current_wallet()->setCacheAttribute(std::string(name), std::string(value)); + return true; + } const char *seed() { diff --git a/cw_monero/lib/api/signatures.dart b/cw_monero/lib/api/signatures.dart index bba7b5528..40f338c8c 100644 --- a/cw_monero/lib/api/signatures.dart +++ b/cw_monero/lib/api/signatures.dart @@ -154,3 +154,7 @@ typedef freeze_coin = Void Function(Int32 index); typedef thaw_coin = Void Function(Int32 index); typedef sign_message = Pointer Function(Pointer message, Pointer address); + +typedef get_cache_attribute = Pointer Function(Pointer name); + +typedef set_cache_attribute = Int8 Function(Pointer name, Pointer value); diff --git a/cw_monero/lib/api/types.dart b/cw_monero/lib/api/types.dart index 87d8a153e..6b36ab5e3 100644 --- a/cw_monero/lib/api/types.dart +++ b/cw_monero/lib/api/types.dart @@ -154,3 +154,7 @@ typedef FreezeCoin = void Function(int); typedef ThawCoin = void Function(int); typedef SignMessage = Pointer Function(Pointer, Pointer); + +typedef GetCacheAttribute = Pointer Function(Pointer); + +typedef SetCacheAttribute = int Function(Pointer, Pointer); diff --git a/cw_monero/lib/api/wallet.dart b/cw_monero/lib/api/wallet.dart index ffa5fe13b..448c661e6 100644 --- a/cw_monero/lib/api/wallet.dart +++ b/cw_monero/lib/api/wallet.dart @@ -1,26 +1,24 @@ import 'dart:async'; import 'dart:ffi'; -import 'package:ffi/ffi.dart'; -import 'package:cw_monero/api/structs/ut8_box.dart'; + import 'package:cw_monero/api/convert_utf8_to_string.dart'; -import 'package:cw_monero/api/signatures.dart'; -import 'package:cw_monero/api/types.dart'; -import 'package:cw_monero/api/monero_api.dart'; import 'package:cw_monero/api/exceptions/setup_wallet_exception.dart'; +import 'package:cw_monero/api/monero_api.dart'; +import 'package:cw_monero/api/signatures.dart'; +import 'package:cw_monero/api/structs/ut8_box.dart'; +import 'package:cw_monero/api/types.dart'; +import 'package:ffi/ffi.dart'; import 'package:flutter/foundation.dart'; int _boolToInt(bool value) => value ? 1 : 0; -final getFileNameNative = moneroApi - .lookup>('get_filename') - .asFunction(); +final getFileNameNative = + moneroApi.lookup>('get_filename').asFunction(); -final getSeedNative = - moneroApi.lookup>('seed').asFunction(); +final getSeedNative = moneroApi.lookup>('seed').asFunction(); -final getAddressNative = moneroApi - .lookup>('get_address') - .asFunction(); +final getAddressNative = + moneroApi.lookup>('get_address').asFunction(); final getFullBalanceNative = moneroApi .lookup>('get_full_balance') @@ -38,41 +36,34 @@ final getNodeHeightNative = moneroApi .lookup>('get_node_height') .asFunction(); -final isConnectedNative = moneroApi - .lookup>('is_connected') - .asFunction(); +final isConnectedNative = + moneroApi.lookup>('is_connected').asFunction(); -final setupNodeNative = moneroApi - .lookup>('setup_node') - .asFunction(); +final setupNodeNative = + moneroApi.lookup>('setup_node').asFunction(); -final startRefreshNative = moneroApi - .lookup>('start_refresh') - .asFunction(); +final startRefreshNative = + moneroApi.lookup>('start_refresh').asFunction(); final connecToNodeNative = moneroApi .lookup>('connect_to_node') .asFunction(); final setRefreshFromBlockHeightNative = moneroApi - .lookup>( - 'set_refresh_from_block_height') + .lookup>('set_refresh_from_block_height') .asFunction(); final setRecoveringFromSeedNative = moneroApi - .lookup>( - 'set_recovering_from_seed') + .lookup>('set_recovering_from_seed') .asFunction(); -final storeNative = - moneroApi.lookup>('store').asFunction(); +final storeNative = moneroApi.lookup>('store').asFunction(); final setPasswordNative = moneroApi.lookup>('set_password').asFunction(); -final setListenerNative = moneroApi - .lookup>('set_listener') - .asFunction(); +final setListenerNative = + moneroApi.lookup>('set_listener').asFunction(); final getSyncingHeightNative = moneroApi .lookup>('get_syncing_height') @@ -83,8 +74,7 @@ final isNeededToRefreshNative = moneroApi .asFunction(); final isNewTransactionExistNative = moneroApi - .lookup>( - 'is_new_transaction_exist') + .lookup>('is_new_transaction_exist') .asFunction(); final getSecretViewKeyNative = moneroApi @@ -107,9 +97,8 @@ final closeCurrentWalletNative = moneroApi .lookup>('close_current_wallet') .asFunction(); -final onStartupNative = moneroApi - .lookup>('on_startup') - .asFunction(); +final onStartupNative = + moneroApi.lookup>('on_startup').asFunction(); final rescanBlockchainAsyncNative = moneroApi .lookup>('rescan_blockchain') @@ -123,13 +112,19 @@ final setTrustedDaemonNative = moneroApi .lookup>('set_trusted_daemon') .asFunction(); -final trustedDaemonNative = moneroApi - .lookup>('trusted_daemon') - .asFunction(); +final trustedDaemonNative = + moneroApi.lookup>('trusted_daemon').asFunction(); -final signMessageNative = moneroApi - .lookup>('sign_message') - .asFunction(); +final signMessageNative = + moneroApi.lookup>('sign_message').asFunction(); + +final getCacheAttributeNative = moneroApi + .lookup>('get_cache_attribute') + .asFunction(); + +final setCacheAttributeNative = moneroApi + .lookup>('set_cache_attribute') + .asFunction(); int getSyncingHeight() => getSyncingHeightNative(); @@ -144,11 +139,9 @@ 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 getFullBalance({int accountIndex = 0}) => getFullBalanceNative(accountIndex); -int getUnlockedBalance({int accountIndex = 0}) => - getUnlockedBalanceNative(accountIndex); +int getUnlockedBalance({int accountIndex = 0}) => getUnlockedBalanceNative(accountIndex); int getCurrentHeight() => getCurrentHeightNative(); @@ -187,7 +180,7 @@ bool setupNodeSync( passwordPointer, _boolToInt(useSSL), _boolToInt(isLightWallet), - socksProxyAddressPointer, + socksProxyAddressPointer, errorMessagePointer) != 0; @@ -202,8 +195,7 @@ bool setupNodeSync( } if (!isSetupNode) { - throw SetupWalletException( - message: convertUTF8ToString(pointer: errorMessagePointer)); + throw SetupWalletException(message: convertUTF8ToString(pointer: errorMessagePointer)); } return isSetupNode; @@ -213,8 +205,7 @@ void startRefreshSync() => startRefreshNative(); Future connectToNode() async => connecToNodeNative() != 0; -void setRefreshFromBlockHeight({required int height}) => - setRefreshFromBlockHeightNative(height); +void setRefreshFromBlockHeight({required int height}) => setRefreshFromBlockHeightNative(height); void setRecoveringFromSeed({required bool isRecovery}) => setRecoveringFromSeedNative(_boolToInt(isRecovery)); @@ -230,7 +221,7 @@ void setPasswordSync(String password) { final errorMessagePointer = calloc(); final changed = setPasswordNative(passwordPointer, errorMessagePointer) != 0; calloc.free(passwordPointer); - + if (!changed) { final message = errorMessagePointer.ref.getValue(); calloc.free(errorMessagePointer); @@ -242,24 +233,19 @@ void setPasswordSync(String password) { void closeCurrentWallet() => closeCurrentWalletNative(); -String getSecretViewKey() => - convertUTF8ToString(pointer: getSecretViewKeyNative()); +String getSecretViewKey() => convertUTF8ToString(pointer: getSecretViewKeyNative()); -String getPublicViewKey() => - convertUTF8ToString(pointer: getPublicViewKeyNative()); +String getPublicViewKey() => convertUTF8ToString(pointer: getPublicViewKeyNative()); -String getSecretSpendKey() => - convertUTF8ToString(pointer: getSecretSpendKeyNative()); +String getSecretSpendKey() => convertUTF8ToString(pointer: getSecretSpendKeyNative()); -String getPublicSpendKey() => - convertUTF8ToString(pointer: getPublicSpendKeyNative()); +String getPublicSpendKey() => convertUTF8ToString(pointer: getPublicSpendKeyNative()); class SyncListener { - SyncListener(this.onNewBlock, this.onNewTransaction) - : _cachedBlockchainHeight = 0, - _lastKnownBlockHeight = 0, - _initialSyncHeight = 0; - + SyncListener(this.onNewBlock, this.onNewTransaction) + : _cachedBlockchainHeight = 0, + _lastKnownBlockHeight = 0, + _initialSyncHeight = 0; void Function(int, int, double) onNewBlock; void Function() onNewTransaction; @@ -281,8 +267,7 @@ class SyncListener { _cachedBlockchainHeight = 0; _lastKnownBlockHeight = 0; _initialSyncHeight = 0; - _updateSyncInfoTimer ??= - Timer.periodic(Duration(milliseconds: 1200), (_) async { + _updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: 1200), (_) async { if (isNewTransactionExist()) { onNewTransaction(); } @@ -321,8 +306,8 @@ class SyncListener { void stop() => _updateSyncInfoTimer?.cancel(); } -SyncListener setListeners(void Function(int, int, double) onNewBlock, - void Function() onNewTransaction) { +SyncListener setListeners( + void Function(int, int, double) onNewBlock, void Function() onNewTransaction) { final listener = SyncListener(onNewBlock, onNewTransaction); setListenerNative(); return listener; @@ -364,7 +349,7 @@ Future setupNode( bool isLightWallet = false}) => compute, void>(_setupNodeSync, { 'address': address, - 'login': login , + 'login': login, 'password': password, 'useSSL': useSSL, 'isLightWallet': isLightWallet, @@ -397,3 +382,23 @@ String signMessage(String message, {String address = ""}) { return signature; } + +bool setCacheAttribute(String name, String value) { + final namePointer = name.toNativeUtf8(); + final valuePointer = value.toNativeUtf8(); + + final isSet = setCacheAttributeNative(namePointer, valuePointer); + calloc.free(namePointer); + calloc.free(valuePointer); + + return isSet == 1; +} + +String getCacheAttribute(String name) { + final namePointer = name.toNativeUtf8(); + + final value = convertUTF8ToString(pointer: getCacheAttributeNative(namePointer)); + calloc.free(namePointer); + + return value; +}