import 'dart:ffi'; import 'package:cw_zano/api/convert_utf8_to_string.dart'; import 'package:cw_zano/api/structs/utf8_box.dart'; import 'package:cw_zano/api/zano_api.dart'; import 'package:ffi/ffi.dart'; // char * create_wallet(char *path, char *password, char *language, int32_t networkType, char *error) typedef _create_wallet = Pointer Function(Pointer, Pointer, Pointer, Int32, Pointer); typedef _CreateWallet = Pointer Function(Pointer, Pointer, Pointer, int, Pointer); // char * restore_wallet_from_seed(char *path, char *password, char *seed, int32_t networkType, uint64_t restoreHeight, char *error) typedef _restore_wallet_from_seed = Pointer Function(Pointer, Pointer, Pointer, Int32, Int64, Pointer); typedef _RestoreWalletFromSeed = Pointer Function(Pointer, Pointer, Pointer, int, int, Pointer); // char * load_wallet(char *path, char *password, int32_t nettype) typedef _load_wallet = Pointer Function(Pointer, Pointer, Int8); typedef _LoadWallet = Pointer Function(Pointer, Pointer, int); // is_wallet_exist(char *path) typedef _is_wallet_exist = Int8 Function(Pointer); typedef _IsWalletExist = int Function(Pointer); // void close_wallet(uint64_t hwallet) typedef _close_wallet = Void Function(Int64); typedef _closeWallet = void Function(int hWallet); // uint64_t get_current_tx_fee(uint64_t priority) typedef _get_current_tx_fee = Int64 Function(Int64); typedef _getCurrentTxFee = int Function(int priority); // char* get_address_info(char* address) typedef _GetAddressInfo = Pointer Function(Pointer address); // char* async_call(char* method_name, uint64_t instance_id, char* params) typedef _async_call = Pointer Function(Pointer, Int64, Pointer); typedef _AsyncCall = Pointer Function(Pointer, int, Pointer); // // char* try_pull_result(uint64_t job_id) // // char *get_wallet_info(uint64_t hwallet) // // char* get_wallet_status(uint64_t hwallet) typedef _stringFunctionWithInt64 = Pointer Function(Int64); typedef _StringFunctionWithIntHWallet = Pointer Function(int); // bool setup_node(char *address, char *login, char *password, bool use_ssl, bool is_light_wallet, char *error) typedef _setup_node = Int8 Function(Pointer, Pointer?, Pointer?, Int8, Int8, Pointer); typedef _SetupNode = int Function(Pointer, Pointer?, Pointer?, int, int, Pointer); // char* set_password(uint64_t hwallet, char *password, Utf8Box &error) typedef _set_password = Pointer Function(Int64 hWallet, Pointer password, Pointer error); typedef _SetPassword = Pointer Function(int hWallet, Pointer password, Pointer error); // char* get_connectivity_status() // char* get_version() typedef _stringFunction = Pointer Function(); class ApiCalls { static String _performApiCall( Pointer Function() apiCall, { List>? pointersToFree, }) { try { return convertUTF8ToString(pointer: apiCall()); } finally { if (pointersToFree != null) { for (var pointer in pointersToFree) { calloc.free(pointer); } } } } static final _createWalletNative = zanoApi.lookup>('create_wallet').asFunction<_CreateWallet>(); static String createWallet({ required String path, required String password, String language = '', int nettype = 0, }) { final pathPointer = path.toNativeUtf8(); final passwordPointer = password.toNativeUtf8(); final languagePointer = language.toNativeUtf8(); final errorMessagePointer = ''.toNativeUtf8(); final result = _performApiCall( () => _createWalletNative( pathPointer, passwordPointer, languagePointer, nettype, errorMessagePointer, ), pointersToFree: [pathPointer, passwordPointer, languagePointer, errorMessagePointer]); return result; } static final _restoreWalletFromSeedNative = zanoApi.lookup>('restore_wallet_from_seed').asFunction<_RestoreWalletFromSeed>(); static String restoreWalletFromSeed({ required String path, required String password, required String seed, }) { final pathPointer = path.toNativeUtf8(); final passwordPointer = password.toNativeUtf8(); final seedPointer = seed.toNativeUtf8(); final errorMessagePointer = ''.toNativeUtf8(); final result = _performApiCall( () => _restoreWalletFromSeedNative( pathPointer, passwordPointer, seedPointer, 0, 0, errorMessagePointer, ), pointersToFree: [ pathPointer, passwordPointer, seedPointer, errorMessagePointer, ], ); return result; } static final _loadWalletNative = zanoApi.lookup>('load_wallet').asFunction<_LoadWallet>(); static String loadWallet({ required String path, required String password, int nettype = 0, }) { final pathPointer = path.toNativeUtf8(); final passwordPointer = password.toNativeUtf8(); final result = _performApiCall( () => _loadWalletNative( pathPointer, passwordPointer, nettype, ), pointersToFree: [ pathPointer, passwordPointer, ], ); return result; } static final _isWalletExistNative = zanoApi.lookup>('is_wallet_exist').asFunction<_IsWalletExist>(); static bool isWalletExist({required String path}) { final pathPointer = path.toNativeUtf8(); final isExist = _isWalletExistNative(pathPointer) != 0; calloc.free(pathPointer); return isExist; } static final _closeWalletNative = zanoApi.lookup>('close_wallet').asFunction<_closeWallet>(); static void closeWallet({required int hWallet}) => _closeWalletNative(hWallet); static final _getWalletInfoNative = zanoApi.lookup>('get_wallet_info').asFunction<_StringFunctionWithIntHWallet>(); static String getWalletInfo(int hWallet) => _performApiCall(() => _getWalletInfoNative(hWallet)); static final _getWalletStatusNative = zanoApi.lookup>('get_wallet_status').asFunction<_StringFunctionWithIntHWallet>(); static String getWalletStatus({required int hWallet}) => _performApiCall(() => _getWalletStatusNative(hWallet)); static final _getCurrentTxFeeNative = zanoApi.lookup>('get_current_tx_fee').asFunction<_getCurrentTxFee>(); static int getCurrentTxFee({required int priority}) => _getCurrentTxFeeNative(priority); static final _getConnectivityStatusNative = zanoApi.lookup>('get_connectivity_status').asFunction<_stringFunction>(); static String getConnectivityStatus() => _performApiCall(() => _getConnectivityStatusNative()); static final _getAddressInfoNative = zanoApi.lookup>('get_address_info').asFunction<_GetAddressInfo>(); static String getAddressInfo({required String address}) { final addressPointer = address.toNativeUtf8(); final result = _performApiCall( () => _getAddressInfoNative(addressPointer), pointersToFree: [addressPointer], ); return result; } static final _asyncCallNative = zanoApi.lookup>('async_call').asFunction<_AsyncCall>(); static final _syncCallNative = zanoApi.lookup>('sync_call').asFunction<_AsyncCall>(); static String syncCall({required String methodName, required int hWallet, required String params}) { final methodNamePointer = methodName.toNativeUtf8(); final paramsPointer = params.toNativeUtf8(); final result = _performApiCall( () => _syncCallNative( methodNamePointer, hWallet, paramsPointer, ), pointersToFree: [ methodNamePointer, paramsPointer, ], ); return result; } static String asyncCall({required String methodName, required int hWallet, required String params}) { final methodNamePointer = methodName.toNativeUtf8(); final paramsPointer = params.toNativeUtf8(); final result = _performApiCall( () => _asyncCallNative( methodNamePointer, hWallet, paramsPointer, ), pointersToFree: [ methodNamePointer, paramsPointer, ], ); return result; } static final _tryPullResultNative = zanoApi.lookup>('try_pull_result').asFunction<_StringFunctionWithIntHWallet>(); static String tryPullResult(int jobId) { final result = _performApiCall(() => _tryPullResultNative(jobId)); return result; } static final _setupNodeNative = zanoApi.lookup>('setup_node').asFunction<_SetupNode>(); static bool setupNode({ required String address, required String login, required String password, bool useSSL = false, bool isLightWallet = false, }) { final addressPointer = address.toNativeUtf8(); final loginPointer = login.toNativeUtf8(); final passwordPointer = password.toNativeUtf8(); final errorMessagePointer = ''.toNativeUtf8(); final isSetupNode = _setupNodeNative( addressPointer, loginPointer, passwordPointer, _boolToInt(useSSL), _boolToInt(isLightWallet), errorMessagePointer, ) != 0; calloc.free(addressPointer); calloc.free(loginPointer); calloc.free(passwordPointer); return isSetupNode; } static final _setPasswordNative = zanoApi.lookup>('set_password').asFunction<_SetPassword>(); static String setPassword({required int hWallet, required String password}) { final passwordPointer = password.toNativeUtf8(); final errorMessagePointer = calloc(); final result = _performApiCall( () => _setPasswordNative( hWallet, passwordPointer, errorMessagePointer, ), pointersToFree: [passwordPointer], ); calloc.free(errorMessagePointer); return result; } static final _getVersionNative = zanoApi.lookup>('get_version').asFunction<_stringFunction>(); static String getVersion() { final result = _performApiCall(() => _getVersionNative()); return result; } static int _boolToInt(bool value) => value ? 1 : 0; }