mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-27 05:59:38 +00:00
e5d99313eb
* CW-498 Call updateTransactions on account change * CW-498 Code Cleanup * CW-498 Add Wallet Address to trade to match transactions to trades * CW-498 Fix containsAddress to include only addresses of account * Handle nullability for fromWalletAddress --------- Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
119 lines
3.6 KiB
Dart
119 lines
3.6 KiB
Dart
import 'package:cw_core/account.dart';
|
|
import 'package:cw_core/address_info.dart';
|
|
import 'package:cw_core/subaddress.dart';
|
|
import 'package:cw_core/wallet_addresses.dart';
|
|
import 'package:cw_core/wallet_info.dart';
|
|
import 'package:cw_monero/api/wallet.dart';
|
|
import 'package:cw_monero/monero_account_list.dart';
|
|
import 'package:cw_monero/monero_subaddress_list.dart';
|
|
import 'package:cw_monero/monero_transaction_history.dart';
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
part 'monero_wallet_addresses.g.dart';
|
|
|
|
class MoneroWalletAddresses = MoneroWalletAddressesBase with _$MoneroWalletAddresses;
|
|
|
|
abstract class MoneroWalletAddressesBase extends WalletAddresses with Store {
|
|
MoneroWalletAddressesBase(
|
|
WalletInfo walletInfo, MoneroTransactionHistory moneroTransactionHistory)
|
|
: accountList = MoneroAccountList(),
|
|
_moneroTransactionHistory = moneroTransactionHistory,
|
|
subaddressList = MoneroSubaddressList(),
|
|
address = '',
|
|
super(walletInfo);
|
|
|
|
final MoneroTransactionHistory _moneroTransactionHistory;
|
|
@override
|
|
@observable
|
|
String address;
|
|
|
|
@observable
|
|
Account? account;
|
|
|
|
@observable
|
|
Subaddress? subaddress;
|
|
|
|
MoneroSubaddressList subaddressList;
|
|
|
|
MoneroAccountList accountList;
|
|
|
|
@override
|
|
Future<void> init() async {
|
|
accountList.update();
|
|
account = accountList.accounts.first;
|
|
updateSubaddressList(accountIndex: account?.id ?? 0);
|
|
await updateAddressesInBox();
|
|
}
|
|
|
|
@override
|
|
Future<void> updateAddressesInBox() async {
|
|
try {
|
|
final _subaddressList = MoneroSubaddressList();
|
|
|
|
addressesMap.clear();
|
|
addressInfos.clear();
|
|
|
|
accountList.accounts.forEach((account) {
|
|
_subaddressList.update(accountIndex: account.id);
|
|
_subaddressList.subaddresses.forEach((subaddress) {
|
|
addressesMap[subaddress.address] = subaddress.label;
|
|
addressInfos[account.id] ??= [];
|
|
addressInfos[account.id]?.add(AddressInfo(
|
|
address: subaddress.address, label: subaddress.label, accountIndex: account.id));
|
|
});
|
|
});
|
|
|
|
await saveAddressesInBox();
|
|
} catch (e) {
|
|
print(e.toString());
|
|
}
|
|
}
|
|
|
|
bool validate() {
|
|
accountList.update();
|
|
final accountListLength = accountList.accounts.length;
|
|
|
|
if (accountListLength <= 0) {
|
|
return false;
|
|
}
|
|
|
|
subaddressList.update(accountIndex: accountList.accounts.first.id);
|
|
final subaddressListLength = subaddressList.subaddresses.length;
|
|
|
|
if (subaddressListLength <= 0) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void updateSubaddressList({required int accountIndex}) {
|
|
subaddressList.update(accountIndex: accountIndex);
|
|
subaddress = subaddressList.subaddresses.first;
|
|
address = subaddress!.address;
|
|
}
|
|
|
|
Future<void> updateUsedSubaddress() async {
|
|
final transactions = _moneroTransactionHistory.transactions.values.toList();
|
|
|
|
transactions.forEach((element) {
|
|
final accountIndex = element.accountIndex;
|
|
final addressIndex = element.addressIndex;
|
|
usedAddresses.add(getAddress(accountIndex: accountIndex, addressIndex: addressIndex));
|
|
});
|
|
}
|
|
|
|
Future<void> updateUnusedSubaddress(
|
|
{required int accountIndex, required String defaultLabel}) async {
|
|
await subaddressList.updateWithAutoGenerate(
|
|
accountIndex: accountIndex,
|
|
defaultLabel: defaultLabel,
|
|
usedAddresses: usedAddresses.toList());
|
|
subaddress = subaddressList.subaddresses.last;
|
|
address = subaddress!.address;
|
|
}
|
|
|
|
@override
|
|
bool containsAddress(String address) =>
|
|
addressInfos[account?.id ?? 0]?.any((it) => it.address == address) ?? false;
|
|
}
|