2023-07-24 16:56:20 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
|
|
|
import 'package:cw_core/transaction_priority.dart';
|
|
|
|
import 'package:cw_core/monero_amount_format.dart';
|
|
|
|
import 'package:cw_core/monero_wallet_utils.dart';
|
2023-07-24 20:23:09 +00:00
|
|
|
import 'package:cw_nano/nano_balance.dart';
|
2023-07-24 16:56:20 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cw_core/monero_wallet_keys.dart';
|
|
|
|
import 'package:cw_core/monero_balance.dart';
|
|
|
|
import 'package:cw_core/account.dart';
|
|
|
|
import 'package:cw_core/pending_transaction.dart';
|
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/sync_status.dart';
|
|
|
|
import 'package:cw_core/wallet_info.dart';
|
|
|
|
import 'package:cw_core/node.dart';
|
|
|
|
import 'package:cw_core/monero_transaction_priority.dart';
|
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
|
|
|
|
part 'nano_wallet.g.dart';
|
|
|
|
|
|
|
|
const moneroBlockSize = 1000;
|
|
|
|
|
|
|
|
class NanoWallet = NanoWalletBase with _$NanoWallet;
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
abstract class NanoWalletBase
|
|
|
|
extends WalletBase<NanoBalance, NanoTransactionHistory, NanoTransactionInfo> with Store {
|
2023-07-24 16:56:20 +00:00
|
|
|
NanoWalletBase({required WalletInfo walletInfo})
|
2023-07-24 20:23:09 +00:00
|
|
|
: balance = ObservableMap<CryptoCurrency, NanoBalance>.of({
|
|
|
|
CryptoCurrency.nano: NanoBalance(
|
|
|
|
currentBalance: nano_wallet.getFullBalance(accountIndex: 0),
|
|
|
|
receivableBalance: nano_wallet.getFullBalance(accountIndex: 0))
|
|
|
|
}),
|
2023-07-24 16:56:20 +00:00
|
|
|
_isTransactionUpdating = false,
|
|
|
|
_hasSyncAfterStartup = false,
|
|
|
|
walletAddresses = NanoWalletAddresses(walletInfo),
|
|
|
|
syncStatus = NotConnectedSyncStatus(),
|
|
|
|
super(walletInfo) {
|
|
|
|
transactionHistory = MoneroTransactionHistory();
|
2023-07-24 20:23:09 +00:00
|
|
|
_onAccountChangeReaction = reaction((_) => walletAddresses.account, (Account? account) {
|
2023-07-24 16:56:20 +00:00
|
|
|
if (account == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
balance = ObservableMap<CryptoCurrency, MoneroBalance>.of(<CryptoCurrency, MoneroBalance>{
|
|
|
|
currency: MoneroBalance(
|
2023-07-24 16:56:20 +00:00
|
|
|
fullBalance: monero_wallet.getFullBalance(accountIndex: account.id),
|
2023-07-24 20:23:09 +00:00
|
|
|
unlockedBalance: monero_wallet.getUnlockedBalance(accountIndex: account.id))
|
|
|
|
});
|
2023-07-24 16:56:20 +00:00
|
|
|
walletAddresses.updateSubaddressList(accountIndex: account.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int _autoSaveInterval = 30;
|
|
|
|
|
|
|
|
@override
|
|
|
|
NanoWalletAddresses walletAddresses;
|
|
|
|
|
|
|
|
@override
|
|
|
|
@observable
|
|
|
|
SyncStatus syncStatus;
|
|
|
|
|
|
|
|
@override
|
|
|
|
@observable
|
|
|
|
ObservableMap<CryptoCurrency, MoneroBalance> balance;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get seed => monero_wallet.getSeed();
|
|
|
|
|
|
|
|
@override
|
|
|
|
NanoWalletKeys get keys => NanoWalletKeys(
|
|
|
|
privateSpendKey: monero_wallet.getSecretSpendKey(),
|
|
|
|
privateViewKey: monero_wallet.getSecretViewKey(),
|
|
|
|
publicSpendKey: monero_wallet.getPublicSpendKey(),
|
|
|
|
publicViewKey: monero_wallet.getPublicViewKey());
|
|
|
|
|
|
|
|
SyncListener? _listener;
|
|
|
|
ReactionDisposer? _onAccountChangeReaction;
|
|
|
|
bool _isTransactionUpdating;
|
|
|
|
bool _hasSyncAfterStartup;
|
|
|
|
Timer? _autoSaveTimer;
|
|
|
|
|
|
|
|
Future<void> init() async {
|
|
|
|
await walletAddresses.init();
|
2023-07-24 20:23:09 +00:00
|
|
|
balance = ObservableMap<CryptoCurrency, MoneroBalance>.of(<CryptoCurrency, MoneroBalance>{
|
|
|
|
currency: MoneroBalance(
|
|
|
|
fullBalance: monero_wallet.getFullBalance(accountIndex: walletAddresses.account!.id),
|
|
|
|
unlockedBalance:
|
|
|
|
monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id))
|
|
|
|
});
|
2023-07-24 16:56:20 +00:00
|
|
|
_setListeners();
|
|
|
|
await updateTransactions();
|
|
|
|
|
|
|
|
if (walletInfo.isRecovery) {
|
|
|
|
monero_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery);
|
|
|
|
|
|
|
|
if (monero_wallet.getCurrentHeight() <= 1) {
|
2023-07-24 20:23:09 +00:00
|
|
|
monero_wallet.setRefreshFromBlockHeight(height: walletInfo.restoreHeight);
|
2023-07-24 16:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// _autoSaveTimer = Timer.periodic(
|
|
|
|
// Duration(seconds: _autoSaveInterval),
|
|
|
|
// (_) async => await save());
|
|
|
|
}
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-24 16:56:20 +00:00
|
|
|
@override
|
|
|
|
Future<void>? updateBalance() => null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void close() {
|
|
|
|
_listener?.stop();
|
|
|
|
_onAccountChangeReaction?.reaction.dispose();
|
|
|
|
_autoSaveTimer?.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> connectToNode({required Node node}) async {
|
|
|
|
try {
|
|
|
|
syncStatus = ConnectingSyncStatus();
|
|
|
|
await monero_wallet.setupNode(
|
2023-07-24 20:23:09 +00:00
|
|
|
address: node.uri.toString(), useSSL: node.isSSL, isLightWallet: false);
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
monero_wallet.setTrustedDaemon(node.trusted);
|
|
|
|
syncStatus = ConnectedSyncStatus();
|
|
|
|
} catch (e) {
|
|
|
|
syncStatus = FailedSyncStatus();
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> startSync() async {
|
|
|
|
try {
|
|
|
|
_setInitialHeight();
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
syncStatus = AttemptingSyncStatus();
|
|
|
|
monero_wallet.startRefresh();
|
|
|
|
_setListeners();
|
|
|
|
_listener?.start();
|
|
|
|
} catch (e) {
|
|
|
|
syncStatus = FailedSyncStatus();
|
|
|
|
print(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<PendingTransaction> createTransaction(Object credentials) async {
|
2023-07-24 20:23:09 +00:00
|
|
|
// final _credentials = credentials as MoneroTransactionCreationCredentials;
|
|
|
|
// return null;
|
|
|
|
throw UnimplementedError();
|
2023-07-24 16:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int calculateEstimatedFee(TransactionPriority priority, int? amount) {
|
|
|
|
// FIXME: hardcoded value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> save() async {
|
|
|
|
await walletAddresses.updateAddressesInBox();
|
|
|
|
await backupWalletFiles(name);
|
|
|
|
await monero_wallet.store();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> renameWalletFiles(String newWalletName) async {
|
|
|
|
final currentWalletDirPath = await pathForWalletDir(name: name, type: type);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// -- rename the waller folder --
|
2023-07-24 20:23:09 +00:00
|
|
|
final currentWalletDir = Directory(await pathForWalletDir(name: name, type: type));
|
|
|
|
final newWalletDirPath = await pathForWalletDir(name: newWalletName, type: type);
|
2023-07-24 16:56:20 +00:00
|
|
|
await currentWalletDir.rename(newWalletDirPath);
|
|
|
|
|
|
|
|
// -- use new waller folder to rename files with old names still --
|
|
|
|
final renamedWalletPath = newWalletDirPath + '/$name';
|
|
|
|
|
|
|
|
final currentCacheFile = File(renamedWalletPath);
|
|
|
|
final currentKeysFile = File('$renamedWalletPath.keys');
|
|
|
|
final currentAddressListFile = File('$renamedWalletPath.address.txt');
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
final newWalletPath = await pathForWallet(name: newWalletName, type: type);
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
if (currentCacheFile.existsSync()) {
|
|
|
|
await currentCacheFile.rename(newWalletPath);
|
|
|
|
}
|
|
|
|
if (currentKeysFile.existsSync()) {
|
|
|
|
await currentKeysFile.rename('$newWalletPath.keys');
|
|
|
|
}
|
|
|
|
if (currentAddressListFile.existsSync()) {
|
|
|
|
await currentAddressListFile.rename('$newWalletPath.address.txt');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
final currentWalletPath = await pathForWallet(name: name, type: type);
|
|
|
|
|
|
|
|
final currentCacheFile = File(currentWalletPath);
|
|
|
|
final currentKeysFile = File('$currentWalletPath.keys');
|
|
|
|
final currentAddressListFile = File('$currentWalletPath.address.txt');
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
final newWalletPath = await pathForWallet(name: newWalletName, type: type);
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
// Copies current wallet files into new wallet name's dir and files
|
|
|
|
if (currentCacheFile.existsSync()) {
|
|
|
|
await currentCacheFile.copy(newWalletPath);
|
|
|
|
}
|
|
|
|
if (currentKeysFile.existsSync()) {
|
|
|
|
await currentKeysFile.copy('$newWalletPath.keys');
|
|
|
|
}
|
|
|
|
if (currentAddressListFile.existsSync()) {
|
|
|
|
await currentAddressListFile.copy('$newWalletPath.address.txt');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete old name's dir and files
|
|
|
|
await Directory(currentWalletDirPath).delete(recursive: true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> changePassword(String password) async {
|
|
|
|
monero_wallet.setPasswordSync(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> getNodeHeight() async => monero_wallet.getNodeHeight();
|
|
|
|
|
|
|
|
Future<bool> isConnected() async => monero_wallet.isConnected();
|
|
|
|
|
|
|
|
Future<void> setAsRecovered() async {
|
|
|
|
walletInfo.isRecovery = false;
|
|
|
|
await walletInfo.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> rescan({required int height}) async {
|
|
|
|
walletInfo.restoreHeight = height;
|
|
|
|
walletInfo.isRecovery = true;
|
|
|
|
monero_wallet.setRefreshFromBlockHeight(height: height);
|
|
|
|
monero_wallet.rescanBlockchainAsync();
|
|
|
|
await startSync();
|
|
|
|
_askForUpdateBalance();
|
|
|
|
walletAddresses.accountList.update();
|
|
|
|
await _askForUpdateTransactionHistory();
|
|
|
|
await save();
|
|
|
|
await walletInfo.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getTransactionAddress(int accountIndex, int addressIndex) =>
|
2023-07-24 20:23:09 +00:00
|
|
|
monero_wallet.getAddress(accountIndex: accountIndex, addressIndex: addressIndex);
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Map<String, MoneroTransactionInfo>> fetchTransactions() async {
|
|
|
|
monero_transaction_history.refreshTransactions();
|
2023-07-24 20:23:09 +00:00
|
|
|
return _getAllTransactions(null)
|
|
|
|
.fold<Map<String, MoneroTransactionInfo>>(<String, MoneroTransactionInfo>{},
|
|
|
|
(Map<String, MoneroTransactionInfo> acc, MoneroTransactionInfo tx) {
|
2023-07-24 16:56:20 +00:00
|
|
|
acc[tx.id] = tx;
|
|
|
|
return acc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateTransactions() async {
|
|
|
|
try {
|
|
|
|
if (_isTransactionUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isTransactionUpdating = true;
|
|
|
|
final transactions = await fetchTransactions();
|
|
|
|
transactionHistory.addMany(transactions);
|
|
|
|
await transactionHistory.save();
|
|
|
|
_isTransactionUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
_isTransactionUpdating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String getSubaddressLabel(int accountIndex, int addressIndex) {
|
|
|
|
return monero_wallet.getSubaddressLabel(accountIndex, addressIndex);
|
|
|
|
}
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
List<MoneroTransactionInfo> _getAllTransactions(dynamic _) => monero_transaction_history
|
|
|
|
.getAllTransations()
|
|
|
|
.map((row) => MoneroTransactionInfo.fromRow(row))
|
|
|
|
.toList();
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
void _setListeners() {
|
|
|
|
_listener?.stop();
|
|
|
|
_listener = monero_wallet.setListeners(_onNewBlock, _onNewTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setInitialHeight() {
|
|
|
|
if (walletInfo.isRecovery) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final currentHeight = getCurrentHeight();
|
|
|
|
|
|
|
|
if (currentHeight <= 1) {
|
|
|
|
final height = _getHeightByDate(walletInfo.date);
|
|
|
|
monero_wallet.setRecoveringFromSeed(isRecovery: true);
|
|
|
|
monero_wallet.setRefreshFromBlockHeight(height: height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int _getHeightDistance(DateTime date) {
|
2023-07-24 20:23:09 +00:00
|
|
|
final distance = DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
|
2023-07-24 16:56:20 +00:00
|
|
|
final daysTmp = (distance / 86400).round();
|
|
|
|
final days = daysTmp < 1 ? 1 : daysTmp;
|
|
|
|
|
|
|
|
return days * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _getHeightByDate(DateTime date) {
|
|
|
|
final nodeHeight = monero_wallet.getNodeHeightSync();
|
|
|
|
final heightDistance = _getHeightDistance(date);
|
|
|
|
|
|
|
|
if (nodeHeight <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeHeight - heightDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _askForUpdateBalance() {
|
|
|
|
final unlockedBalance = _getUnlockedBalance();
|
|
|
|
final fullBalance = _getFullBalance();
|
|
|
|
|
|
|
|
if (balance[currency]!.fullBalance != fullBalance ||
|
|
|
|
balance[currency]!.unlockedBalance != unlockedBalance) {
|
2023-07-24 20:23:09 +00:00
|
|
|
balance[currency] = MoneroBalance(fullBalance: fullBalance, unlockedBalance: unlockedBalance);
|
2023-07-24 16:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
Future<void> _askForUpdateTransactionHistory() async => await updateTransactions();
|
2023-07-24 16:56:20 +00:00
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
int _getFullBalance() => monero_wallet.getFullBalance(accountIndex: walletAddresses.account!.id);
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
int _getUnlockedBalance() =>
|
|
|
|
monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account!.id);
|
|
|
|
|
|
|
|
void _onNewBlock(int height, int blocksLeft, double ptc) async {
|
|
|
|
try {
|
|
|
|
if (walletInfo.isRecovery) {
|
|
|
|
await _askForUpdateTransactionHistory();
|
|
|
|
_askForUpdateBalance();
|
|
|
|
walletAddresses.accountList.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blocksLeft < 100) {
|
|
|
|
await _askForUpdateTransactionHistory();
|
|
|
|
_askForUpdateBalance();
|
|
|
|
walletAddresses.accountList.update();
|
|
|
|
syncStatus = SyncedSyncStatus();
|
|
|
|
|
|
|
|
if (!_hasSyncAfterStartup) {
|
2023-07-24 20:23:09 +00:00
|
|
|
_hasSyncAfterStartup = true;
|
|
|
|
await save();
|
|
|
|
}
|
2023-07-24 16:56:20 +00:00
|
|
|
|
|
|
|
if (walletInfo.isRecovery) {
|
|
|
|
await setAsRecovered();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
syncStatus = SyncingSyncStatus(blocksLeft, ptc);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onNewTransaction() async {
|
|
|
|
try {
|
|
|
|
await _askForUpdateTransactionHistory();
|
|
|
|
_askForUpdateBalance();
|
|
|
|
await Future<void>.delayed(Duration(seconds: 1));
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|