2021-12-24 12:52:08 +00:00
|
|
|
import 'dart:async';
|
2024-05-11 20:27:34 +00:00
|
|
|
import 'dart:ffi';
|
|
|
|
import 'dart:isolate';
|
2024-04-12 12:54:24 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
import 'package:cw_monero/api/account_list.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/exceptions/setup_wallet_exception.dart';
|
2024-04-10 12:27:10 +00:00
|
|
|
import 'package:monero/monero.dart' as monero;
|
2024-05-11 20:27:34 +00:00
|
|
|
import 'package:mutex/mutex.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-20 17:19:13 +00:00
|
|
|
int getSyncingHeight() {
|
|
|
|
// final height = monero.MONERO_cw_WalletListener_height(getWlptr());
|
|
|
|
final h2 = monero.Wallet_blockChainHeight(wptr!);
|
|
|
|
// print("height: $height / $h2");
|
|
|
|
return h2;
|
|
|
|
}
|
2024-05-11 20:27:34 +00:00
|
|
|
|
2024-04-16 15:15:20 +00:00
|
|
|
bool isNeededToRefresh() {
|
|
|
|
final ret = monero.MONERO_cw_WalletListener_isNeedToRefresh(getWlptr());
|
|
|
|
monero.MONERO_cw_WalletListener_resetNeedToRefresh(getWlptr());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNewTransactionExist() {
|
|
|
|
final ret = monero.MONERO_cw_WalletListener_isNewTransactionExist(getWlptr());
|
|
|
|
monero.MONERO_cw_WalletListener_resetIsNewTransactionExist(getWlptr());
|
|
|
|
return ret;
|
|
|
|
}
|
2024-05-11 20:27:34 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
String getFilename() => monero.Wallet_filename(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-20 17:19:13 +00:00
|
|
|
String getSeed() {
|
2024-04-23 12:31:00 +00:00
|
|
|
// monero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed);
|
2024-05-11 20:27:34 +00:00
|
|
|
final cakepolyseed =
|
|
|
|
monero.Wallet_getCacheAttribute(wptr!, key: "cakewallet.seed");
|
2024-04-23 12:31:00 +00:00
|
|
|
if (cakepolyseed != "") {
|
|
|
|
return cakepolyseed;
|
|
|
|
}
|
2024-04-20 17:19:13 +00:00
|
|
|
final polyseed = monero.Wallet_getPolyseed(wptr!, passphrase: '');
|
2024-04-23 12:31:00 +00:00
|
|
|
if (polyseed != "") {
|
|
|
|
return polyseed;
|
2024-04-20 17:19:13 +00:00
|
|
|
}
|
2024-04-23 12:31:00 +00:00
|
|
|
final legacy = monero.Wallet_seed(wptr!, seedOffset: '');
|
|
|
|
return legacy;
|
2024-04-20 17:19:13 +00:00
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-05-11 20:27:34 +00:00
|
|
|
String getAddress({int accountIndex = 0, int addressIndex = 1}) =>
|
|
|
|
monero.Wallet_address(wptr!,
|
|
|
|
accountIndex: accountIndex, addressIndex: addressIndex);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-05-11 20:27:34 +00:00
|
|
|
int getFullBalance({int accountIndex = 0}) =>
|
|
|
|
monero.Wallet_balance(wptr!, accountIndex: accountIndex);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-05-11 20:27:34 +00:00
|
|
|
int getUnlockedBalance({int accountIndex = 0}) =>
|
|
|
|
monero.Wallet_unlockedBalance(wptr!, accountIndex: accountIndex);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
int getCurrentHeight() => monero.Wallet_blockChainHeight(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
int getNodeHeightSync() => monero.Wallet_daemonBlockChainHeight(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
bool isConnectedSync() => monero.Wallet_connected(wptr!) != 0;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
bool setupNodeSync(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required String address,
|
|
|
|
String? login,
|
|
|
|
String? password,
|
2021-12-24 12:52:08 +00:00
|
|
|
bool useSSL = false,
|
2023-08-02 11:48:52 +00:00
|
|
|
bool isLightWallet = false,
|
|
|
|
String? socksProxyAddress}) {
|
2024-04-12 12:54:24 +00:00
|
|
|
print('''
|
|
|
|
{
|
|
|
|
wptr!,
|
|
|
|
daemonAddress: $address,
|
|
|
|
useSsl: $useSSL,
|
|
|
|
proxyAddress: $socksProxyAddress ?? '',
|
|
|
|
daemonUsername: $login ?? '',
|
|
|
|
daemonPassword: $password ?? ''
|
|
|
|
}
|
|
|
|
''');
|
2024-05-11 20:27:34 +00:00
|
|
|
monero.Wallet_init(wptr!,
|
|
|
|
daemonAddress: address,
|
|
|
|
useSsl: useSSL,
|
|
|
|
proxyAddress: socksProxyAddress ?? '',
|
|
|
|
daemonUsername: login ?? '',
|
|
|
|
daemonPassword: password ?? '');
|
2024-04-10 12:27:10 +00:00
|
|
|
// monero.Wallet_init3(wptr!, argv0: '', defaultLogBaseName: 'moneroc', console: true);
|
2024-05-11 20:27:34 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
final status = monero.Wallet_status(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-12 12:54:24 +00:00
|
|
|
if (status != 0) {
|
|
|
|
final error = monero.Wallet_errorString(wptr!);
|
|
|
|
print("error: $error");
|
|
|
|
throw SetupWalletException(message: error);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
return status == 0;
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 17:19:13 +00:00
|
|
|
void startRefreshSync() {
|
|
|
|
monero.Wallet_refreshAsync(wptr!);
|
|
|
|
monero.Wallet_startRefresh(wptr!);
|
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
Future<bool> connectToNode() async {
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-05-11 20:27:34 +00:00
|
|
|
void setRefreshFromBlockHeight({required int height}) =>
|
|
|
|
monero.Wallet_setRefreshFromBlockHeight(wptr!,
|
|
|
|
refresh_from_block_height: height);
|
|
|
|
|
|
|
|
void setRecoveringFromSeed({required bool isRecovery}) =>
|
|
|
|
monero.Wallet_setRecoveringFromSeed(wptr!, recoveringFromSeed: isRecovery);
|
|
|
|
|
|
|
|
final storeMutex = Mutex();
|
|
|
|
void storeSync() async {
|
|
|
|
await storeMutex.acquire();
|
|
|
|
final addr = wptr!.address;
|
|
|
|
Isolate.run(() {
|
|
|
|
monero.Wallet_store(Pointer.fromAddress(addr));
|
|
|
|
});
|
|
|
|
storeMutex.release();
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:29:28 +00:00
|
|
|
void setPasswordSync(String password) {
|
2024-04-10 12:27:10 +00:00
|
|
|
monero.Wallet_setPassword(wptr!, password: password);
|
|
|
|
|
|
|
|
final status = monero.Wallet_status(wptr!);
|
|
|
|
if (status == 0) {
|
|
|
|
throw Exception(monero.Wallet_errorString(wptr!));
|
|
|
|
}
|
2022-07-19 14:29:28 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
void closeCurrentWallet() {
|
|
|
|
monero.Wallet_stop(wptr!);
|
|
|
|
}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
String getSecretViewKey() => monero.Wallet_secretViewKey(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
String getPublicViewKey() => monero.Wallet_publicViewKey(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
String getSecretSpendKey() => monero.Wallet_secretSpendKey(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
String getPublicSpendKey() => monero.Wallet_publicSpendKey(wptr!);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
class SyncListener {
|
2024-05-11 20:27:34 +00:00
|
|
|
SyncListener(this.onNewBlock, this.onNewTransaction)
|
|
|
|
: _cachedBlockchainHeight = 0,
|
|
|
|
_lastKnownBlockHeight = 0,
|
|
|
|
_initialSyncHeight = 0;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
void Function(int, int, double) onNewBlock;
|
|
|
|
void Function() onNewTransaction;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Timer? _updateSyncInfoTimer;
|
2021-12-24 12:52:08 +00:00
|
|
|
int _cachedBlockchainHeight;
|
|
|
|
int _lastKnownBlockHeight;
|
|
|
|
int _initialSyncHeight;
|
|
|
|
|
|
|
|
Future<int> 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()) {
|
2022-10-12 17:09:57 +00:00
|
|
|
onNewTransaction();
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var syncHeight = getSyncingHeight();
|
|
|
|
|
|
|
|
if (syncHeight <= 0) {
|
|
|
|
syncHeight = getCurrentHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_initialSyncHeight <= 0) {
|
|
|
|
_initialSyncHeight = syncHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
final bchHeight = await getNodeHeightOrUpdate(syncHeight);
|
|
|
|
|
2023-12-08 14:05:52 +00:00
|
|
|
if (_lastKnownBlockHeight == syncHeight) {
|
2021-12-24 12:52:08 +00:00
|
|
|
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;
|
2023-12-08 14:05:52 +00:00
|
|
|
onNewBlock.call(syncHeight, left, ptc);
|
2021-12-24 12:52:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop() => _updateSyncInfoTimer?.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncListener setListeners(void Function(int, int, double) onNewBlock,
|
|
|
|
void Function() onNewTransaction) {
|
|
|
|
final listener = SyncListener(onNewBlock, onNewTransaction);
|
2024-04-10 12:27:10 +00:00
|
|
|
// setListenerNative();
|
2021-12-24 12:52:08 +00:00
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
void onStartup() {}
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
void _storeSync(Object _) => storeSync();
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
bool _setupNodeSync(Map<String, Object?> args) {
|
2021-12-24 12:52:08 +00:00
|
|
|
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-08-02 11:48:52 +00:00
|
|
|
final socksProxyAddress = (args['socksProxyAddress'] ?? '') as String;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
return setupNodeSync(
|
|
|
|
address: address,
|
|
|
|
login: login,
|
|
|
|
password: password,
|
|
|
|
useSSL: useSSL,
|
2023-08-02 11:48:52 +00:00
|
|
|
isLightWallet: isLightWallet,
|
|
|
|
socksProxyAddress: socksProxyAddress);
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _isConnected(Object _) => isConnectedSync();
|
|
|
|
|
|
|
|
int _getNodeHeight(Object _) => getNodeHeightSync();
|
|
|
|
|
|
|
|
void startRefresh() => startRefreshSync();
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> setupNode(
|
|
|
|
{required String address,
|
|
|
|
String? login,
|
|
|
|
String? password,
|
2021-12-24 12:52:08 +00:00
|
|
|
bool useSSL = false,
|
2023-08-02 11:48:52 +00:00
|
|
|
String? socksProxyAddress,
|
2024-04-10 12:27:10 +00:00
|
|
|
bool isLightWallet = false}) async =>
|
|
|
|
_setupNodeSync({
|
2021-12-24 12:52:08 +00:00
|
|
|
'address': address,
|
2024-05-11 20:27:34 +00:00
|
|
|
'login': login,
|
2021-12-24 12:52:08 +00:00
|
|
|
'password': password,
|
|
|
|
'useSSL': useSSL,
|
2023-08-02 11:48:52 +00:00
|
|
|
'isLightWallet': isLightWallet,
|
|
|
|
'socksProxyAddress': socksProxyAddress
|
2021-12-24 12:52:08 +00:00
|
|
|
});
|
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
Future<void> store() async => _storeSync(0);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
Future<bool> isConnected() async => _isConnected(0);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
Future<int> getNodeHeight() async => _getNodeHeight(0);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
void rescanBlockchainAsync() => monero.Wallet_rescanBlockchainAsync(wptr!);
|
2022-04-12 15:38:47 +00:00
|
|
|
|
|
|
|
String getSubaddressLabel(int accountIndex, int addressIndex) {
|
2024-05-11 20:27:34 +00:00
|
|
|
return monero.Wallet_getSubaddressLabel(wptr!,
|
|
|
|
accountIndex: accountIndex, addressIndex: addressIndex);
|
2022-10-07 13:25:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-11 20:27:34 +00:00
|
|
|
Future setTrustedDaemon(bool trusted) async =>
|
|
|
|
monero.Wallet_setTrustedDaemon(wptr!, arg: trusted);
|
2022-10-07 13:25:20 +00:00
|
|
|
|
2024-04-10 12:27:10 +00:00
|
|
|
Future<bool> trustedDaemon() async => monero.Wallet_trustedDaemon(wptr!);
|
2023-12-08 14:05:52 +00:00
|
|
|
|
|
|
|
String signMessage(String message, {String address = ""}) {
|
2024-04-10 12:27:10 +00:00
|
|
|
return monero.Wallet_signMessage(wptr!, message: message, address: address);
|
2023-12-08 14:05:52 +00:00
|
|
|
}
|