cake_wallet/cw_zano/lib/api/wallet.dart

419 lines
14 KiB
Dart
Raw Normal View History

2023-10-02 14:17:35 +00:00
import 'dart:async';
2023-12-14 04:51:16 +00:00
import 'dart:convert';
2023-10-02 14:17:35 +00:00
import 'dart:ffi';
2023-12-14 04:51:16 +00:00
import 'package:cw_core/crypto_currency.dart';
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';
2023-10-02 14:17:35 +00:00
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';
2023-11-17 17:40:23 +00:00
import 'package:cw_zano/api/calls.dart' as calls;
2023-10-02 14:17:35 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
2023-12-14 04:51:16 +00:00
import 'package:mobx/mobx.dart' as mobx;
2023-10-02 14:17:35 +00:00
int _boolToInt(bool value) => value ? 1 : 0;
2023-12-14 04:51:16 +00:00
final getFileNameNative =
zanoApi.lookup<NativeFunction<get_filename>>('get_filename').asFunction<GetFilename>();
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
/*final getSeedNative =
zanoApi.lookup<NativeFunction<get_seed>>('seed').asFunction<GetSeed>();*/
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final getAddressNative =
zanoApi.lookup<NativeFunction<get_address>>('get_address').asFunction<GetAddress>();
2023-10-02 14:17:35 +00:00
final getFullBalanceNative = zanoApi
.lookup<NativeFunction<get_full_balanace>>('get_full_balance')
.asFunction<GetFullBalance>();
final getUnlockedBalanceNative = zanoApi
.lookup<NativeFunction<get_unlocked_balanace>>('get_unlocked_balance')
.asFunction<GetUnlockedBalance>();
2023-11-17 17:40:23 +00:00
/**final getCurrentHeightNative = zanoApi
2023-10-02 14:17:35 +00:00
.lookup<NativeFunction<get_current_height>>('get_current_height')
2023-11-17 17:40:23 +00:00
.asFunction<GetCurrentHeight>();*/
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
// final getNodeHeightNative = zanoApi
// .lookup<NativeFunction<get_node_height>>('get_node_height')
// .asFunction<GetNodeHeight>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final isConnectedNative =
zanoApi.lookup<NativeFunction<is_connected>>('is_connected').asFunction<IsConnected>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final setupNodeNative =
zanoApi.lookup<NativeFunction<setup_node>>('setup_node').asFunction<SetupNode>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
// final startRefreshNative = zanoApi
// .lookup<NativeFunction<start_refresh>>('start_refresh')
// .asFunction<StartRefresh>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final connecToNodeNative =
zanoApi.lookup<NativeFunction<connect_to_node>>('connect_to_node').asFunction<ConnectToNode>();
2023-10-02 14:17:35 +00:00
final setRefreshFromBlockHeightNative = zanoApi
2023-12-14 04:51:16 +00:00
.lookup<NativeFunction<set_refresh_from_block_height>>('set_refresh_from_block_height')
2023-10-02 14:17:35 +00:00
.asFunction<SetRefreshFromBlockHeight>();
2023-12-14 04:51:16 +00:00
// final setRecoveringFromSeedNative = zanoApi
// .lookup<NativeFunction<set_recovering_from_seed>>('set_recovering_from_seed')
// .asFunction<SetRecoveringFromSeed>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final storeNative = zanoApi.lookup<NativeFunction<store_c>>('store').asFunction<Store>();
final setPasswordNative =
zanoApi.lookup<NativeFunction<set_password>>('set_password').asFunction<SetPassword>();
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
/**final setListenerNative = zanoApi
2023-10-02 14:17:35 +00:00
.lookup<NativeFunction<set_listener>>('set_listener')
2023-11-17 17:40:23 +00:00
.asFunction<SetListener>();*/
2023-10-02 14:17:35 +00:00
final getSyncingHeightNative = zanoApi
.lookup<NativeFunction<get_syncing_height>>('get_syncing_height')
.asFunction<GetSyncingHeight>();
final isNeededToRefreshNative = zanoApi
.lookup<NativeFunction<is_needed_to_refresh>>('is_needed_to_refresh')
.asFunction<IsNeededToRefresh>();
2023-12-14 04:51:16 +00:00
// final isNewTransactionExistNative = zanoApi
// .lookup<NativeFunction<is_new_transaction_exist>>('is_new_transaction_exist')
// .asFunction<IsNewTransactionExist>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final getSecretViewKeyNative =
zanoApi.lookup<NativeFunction<secret_view_key>>('secret_view_key').asFunction<SecretViewKey>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final getPublicViewKeyNative =
zanoApi.lookup<NativeFunction<public_view_key>>('public_view_key').asFunction<PublicViewKey>();
2023-10-02 14:17:35 +00:00
final getSecretSpendKeyNative = zanoApi
.lookup<NativeFunction<secret_spend_key>>('secret_spend_key')
.asFunction<SecretSpendKey>();
final getPublicSpendKeyNative = zanoApi
.lookup<NativeFunction<secret_view_key>>('public_spend_key')
.asFunction<PublicSpendKey>();
final closeCurrentWalletNative = zanoApi
.lookup<NativeFunction<close_current_wallet>>('close_current_wallet')
.asFunction<CloseCurrentWallet>();
2023-12-14 04:51:16 +00:00
final onStartupNative =
zanoApi.lookup<NativeFunction<on_startup>>('on_startup').asFunction<OnStartup>();
2023-10-02 14:17:35 +00:00
final rescanBlockchainAsyncNative = zanoApi
.lookup<NativeFunction<rescan_blockchain>>('rescan_blockchain')
.asFunction<RescanBlockchainAsync>();
2023-12-14 04:51:16 +00:00
// final setTrustedDaemonNative = zanoApi
// .lookup<NativeFunction<set_trusted_daemon>>('set_trusted_daemon')
// .asFunction<SetTrustedDaemon>();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final trustedDaemonNative =
zanoApi.lookup<NativeFunction<trusted_daemon>>('trusted_daemon').asFunction<TrustedDaemon>();
2023-10-02 14:17:35 +00:00
int getSyncingHeight() => getSyncingHeightNative();
bool isNeededToRefresh() => isNeededToRefreshNative() != 0;
2023-12-14 04:51:16 +00:00
//bool isNewTransactionExist() => isNewTransactionExistNative() != 0;
2023-10-02 14:17:35 +00:00
String getFilename() => convertUTF8ToString(pointer: getFileNameNative());
2023-11-17 17:40:23 +00:00
/**String getSeed() => convertUTF8ToString(pointer: getSeedNative());*/
2023-10-02 14:17:35 +00:00
String getAddress({int accountIndex = 0, int addressIndex = 0}) =>
convertUTF8ToString(pointer: getAddressNative(accountIndex, addressIndex));
2023-12-14 04:51:16 +00:00
int getFullBalance({int accountIndex = 0}) => getFullBalanceNative(accountIndex);
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
int getUnlockedBalance({int accountIndex = 0}) => getUnlockedBalanceNative(accountIndex);
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
int getCurrentHeight(int hWallet) {
2023-12-14 04:51:16 +00:00
final json = calls.getWalletStatus(hWallet);
final walletStatus = GetWalletStatusResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
return walletStatus.currentWalletHeight;
2023-11-17 17:40:23 +00:00
}
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
int getNodeHeightSync(int hWallet) {
final json = calls.getWalletStatus(hWallet);
final walletStatus = GetWalletStatusResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
return walletStatus.currentDaemonHeight;
}
// int getWalletInfo(int hWallet) {
// final json = calls.getWalletInfo(hWallet);
// final walletInfo = GetWalletInfoResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
// 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);
// }
2023-10-02 14:17:35 +00:00
bool isConnectedSync() => isConnectedNative() != 0;
2023-12-14 04:51:16 +00:00
bool setupNodeSync({
required String address,
String? login,
String? password,
bool useSSL = false,
bool isLightWallet = false,
/*String? socksProxyAddress*/
}) {
2023-10-02 14:17:35 +00:00
final addressPointer = address.toNativeUtf8();
Pointer<Utf8>? loginPointer;
Pointer<Utf8>? socksProxyAddressPointer;
Pointer<Utf8>? passwordPointer;
if (login != null) {
loginPointer = login.toNativeUtf8();
}
if (password != null) {
passwordPointer = password.toNativeUtf8();
}
2023-11-17 17:40:23 +00:00
/*if (socksProxyAddress != null) {
2023-10-02 14:17:35 +00:00
socksProxyAddressPointer = socksProxyAddress.toNativeUtf8();
2023-11-17 17:40:23 +00:00
}*/
2023-10-02 14:17:35 +00:00
final errorMessagePointer = ''.toNativeUtf8();
2023-12-14 04:51:16 +00:00
debugPrint(
"setup_node address $address login $login password $password useSSL $useSSL isLightWallet $isLightWallet");
2023-11-17 17:40:23 +00:00
// TODO: here can be ZERO! upd: no
2023-10-02 14:17:35 +00:00
final isSetupNode = setupNodeNative(
addressPointer,
loginPointer,
passwordPointer,
_boolToInt(useSSL),
_boolToInt(isLightWallet),
2023-11-17 17:40:23 +00:00
/*socksProxyAddressPointer,*/
2023-10-02 14:17:35 +00:00
errorMessagePointer) !=
0;
2023-11-17 17:40:23 +00:00
debugPrint("setup_node result $isSetupNode");
2023-10-02 14:17:35 +00:00
calloc.free(addressPointer);
if (loginPointer != null) {
calloc.free(loginPointer);
}
if (passwordPointer != null) {
calloc.free(passwordPointer);
}
2023-11-17 17:40:23 +00:00
// TODO: fix it
/**if (!isSetupNode) {
2023-10-02 14:17:35 +00:00
throw SetupWalletException(
message: convertUTF8ToString(pointer: errorMessagePointer));
2023-11-17 17:40:23 +00:00
}*/
2023-10-02 14:17:35 +00:00
return isSetupNode;
}
2023-12-14 04:51:16 +00:00
//void startRefreshSync() => startRefreshNative();
2023-10-02 14:17:35 +00:00
Future<bool> connectToNode() async => connecToNodeNative() != 0;
2023-12-14 04:51:16 +00:00
void setRefreshFromBlockHeight({required int height}) => setRefreshFromBlockHeightNative(height);
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
// void setRecoveringFromSeed({required bool isRecovery}) =>
// setRecoveringFromSeedNative(_boolToInt(isRecovery));
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
void storeSync(int hWallet) {
calls.store(hWallet);
// TODO: fixit
/*final pathPointer = ''.toNativeUtf8();
2023-10-02 14:17:35 +00:00
storeNative(pathPointer);
2023-11-17 17:40:23 +00:00
calloc.free(pathPointer);*/
2023-10-02 14:17:35 +00:00
}
void setPasswordSync(String password) {
final passwordPointer = password.toNativeUtf8();
final errorMessagePointer = calloc<Utf8Box>();
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();
2023-12-14 04:51:16 +00:00
String getSecretViewKey() => convertUTF8ToString(pointer: getSecretViewKeyNative());
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
String getPublicViewKey() => convertUTF8ToString(pointer: getPublicViewKeyNative());
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
String getSecretSpendKey() => convertUTF8ToString(pointer: getSecretSpendKeyNative());
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
String getPublicSpendKey() => convertUTF8ToString(pointer: getPublicSpendKeyNative());
2023-10-02 14:17:35 +00:00
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;
2023-12-14 04:51:16 +00:00
Future<int> getNodeHeightOrUpdate(int hWallet, int baseHeight) async {
2023-10-02 14:17:35 +00:00
if (_cachedBlockchainHeight < baseHeight || _cachedBlockchainHeight == 0) {
2023-12-14 04:51:16 +00:00
_cachedBlockchainHeight = await compute<int, int>(getNodeHeightSync, hWallet);
2023-10-02 14:17:35 +00:00
}
return _cachedBlockchainHeight;
}
2023-12-14 04:51:16 +00:00
void start(ZanoWalletBase wallet, int hWallet) async {
2023-10-02 14:17:35 +00:00
_cachedBlockchainHeight = 0;
_lastKnownBlockHeight = 0;
_initialSyncHeight = 0;
2023-12-14 04:51:16 +00:00
_updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: 1200), (_) async {
/**if (isNewTransactionExist()) {
2023-10-02 14:17:35 +00:00
onNewTransaction?.call();
2023-12-14 04:51:16 +00:00
}*/
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
///var syncHeight = getSyncingHeight();
var syncHeight = getCurrentHeight(hWallet);
2023-10-02 14:17:35 +00:00
if (syncHeight <= 0) {
2023-12-14 04:51:16 +00:00
syncHeight = getCurrentHeight(hWallet);
2023-10-02 14:17:35 +00:00
}
2023-12-14 04:51:16 +00:00
//getWalletInfo(hWallet);
final json = calls.getWalletInfo(hWallet);
final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
wallet.seed = result.wiExtended.seed;
wallet.keys = ZanoWalletKeys(
privateSpendKey: result.wiExtended.spendPrivateKey,
privateViewKey: result.wiExtended.viewPrivateKey,
publicSpendKey: result.wiExtended.spendPublicKey,
publicViewKey: result.wiExtended.viewPublicKey,
);
final balance = result.wi.balances.first;
wallet.assetId = balance.assetInfo.assetId;
wallet.balance = mobx.ObservableMap.of(
{CryptoCurrency.zano: ZanoBalance(total: balance.total, unlocked: balance.unlocked)});
///getTxFee(hWallet);
2023-12-14 04:51:16 +00:00
2023-10-02 14:17:35 +00:00
if (_initialSyncHeight <= 0) {
_initialSyncHeight = syncHeight;
}
2023-12-14 04:51:16 +00:00
final bchHeight = await getNodeHeightOrUpdate(hWallet, syncHeight);
2023-10-02 14:17:35 +00:00
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();
}
2023-12-14 04:51:16 +00:00
SyncListener setListeners(
void Function(int, int, double) onNewBlock, void Function() onNewTransaction) {
2023-10-02 14:17:35 +00:00
final listener = SyncListener(onNewBlock, onNewTransaction);
2023-11-17 17:40:23 +00:00
/**setListenerNative();*/
2023-10-02 14:17:35 +00:00
return listener;
}
void onStartup() => onStartupNative();
2023-11-17 17:40:23 +00:00
void _storeSync(int hWallet) => storeSync(hWallet);
2023-10-02 14:17:35 +00:00
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;
2023-11-17 17:40:23 +00:00
/*final socksProxyAddress = (args['socksProxyAddress'] ?? '') as String;*/
2023-10-02 14:17:35 +00:00
return setupNodeSync(
2023-12-14 04:51:16 +00:00
address: address,
login: login,
password: password,
useSSL: useSSL,
isLightWallet: isLightWallet, /*socksProxyAddress: socksProxyAddress*/
);
2023-10-02 14:17:35 +00:00
}
bool _isConnected(Object _) => isConnectedSync();
2023-12-14 04:51:16 +00:00
//int _getNodeHeight(Object _) => getNodeHeightSync();
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
//void startRefresh() => startRefreshSync();
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
Future<bool> setupNode(
2023-10-02 14:17:35 +00:00
{required String address,
String? login,
String? password,
bool useSSL = false,
2023-11-17 17:40:23 +00:00
/*String? socksProxyAddress,*/
2023-10-02 14:17:35 +00:00
bool isLightWallet = false}) =>
2023-11-17 17:40:23 +00:00
compute<Map<String, Object?>, bool>(_setupNodeSync, {
2023-10-02 14:17:35 +00:00
'address': address,
'login': login,
'password': password,
'useSSL': useSSL,
'isLightWallet': isLightWallet,
2023-11-17 17:40:23 +00:00
//'socksProxyAddress': socksProxyAddress
2023-10-02 14:17:35 +00:00
});
2023-11-17 17:40:23 +00:00
Future<void> store(int hWallet) => compute<int, void>(_storeSync, 0);
2023-10-02 14:17:35 +00:00
Future<bool> isConnected() => compute(_isConnected, 0);
2023-12-14 04:51:16 +00:00
//Future<int> getNodeHeight() => compute(_getNodeHeight, 0);
2023-10-02 14:17:35 +00:00
void rescanBlockchainAsync() => rescanBlockchainAsyncNative();
2023-12-14 04:51:16 +00:00
// Future setTrustedDaemon(bool trusted) async =>
// setTrustedDaemonNative(_boolToInt(trusted));
2023-10-02 14:17:35 +00:00
Future<bool> trustedDaemon() async => trustedDaemonNative() != 0;