2023-11-02 15:52:47 +00:00
|
|
|
import 'package:cw_core/account.dart';
|
2023-08-29 16:11:51 +00:00
|
|
|
import 'package:cw_core/address_info.dart';
|
2023-11-02 15:52:47 +00:00
|
|
|
import 'package:cw_core/subaddress.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_core/wallet_addresses.dart';
|
|
|
|
import 'package:cw_core/wallet_info.dart';
|
2024-08-26 23:40:20 +00:00
|
|
|
import 'package:cw_monero/api/transaction_history.dart';
|
2023-08-29 16:11:51 +00:00
|
|
|
import 'package:cw_monero/api/wallet.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/monero_account_list.dart';
|
|
|
|
import 'package:cw_monero/monero_subaddress_list.dart';
|
2023-08-29 16:11:51 +00:00
|
|
|
import 'package:cw_monero/monero_transaction_history.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
part 'monero_wallet_addresses.g.dart';
|
|
|
|
|
2023-08-29 16:11:51 +00:00
|
|
|
class MoneroWalletAddresses = MoneroWalletAddressesBase with _$MoneroWalletAddresses;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
abstract class MoneroWalletAddressesBase extends WalletAddresses with Store {
|
2023-08-29 16:11:51 +00:00
|
|
|
MoneroWalletAddressesBase(
|
|
|
|
WalletInfo walletInfo, MoneroTransactionHistory moneroTransactionHistory)
|
|
|
|
: accountList = MoneroAccountList(),
|
|
|
|
_moneroTransactionHistory = moneroTransactionHistory,
|
|
|
|
subaddressList = MoneroSubaddressList(),
|
|
|
|
address = '',
|
|
|
|
super(walletInfo);
|
|
|
|
|
|
|
|
final MoneroTransactionHistory _moneroTransactionHistory;
|
2021-12-24 12:52:08 +00:00
|
|
|
@override
|
|
|
|
@observable
|
|
|
|
String address;
|
2023-08-29 16:11:51 +00:00
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
@observable
|
2022-10-12 17:09:57 +00:00
|
|
|
Account? account;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
@observable
|
2022-10-12 17:09:57 +00:00
|
|
|
Subaddress? subaddress;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
MoneroSubaddressList subaddressList;
|
|
|
|
|
|
|
|
MoneroAccountList accountList;
|
|
|
|
|
2024-08-26 23:40:20 +00:00
|
|
|
@override
|
|
|
|
Set<String> get usedAddresses {
|
|
|
|
final txs = getAllTransactions();
|
|
|
|
final adds = _originalUsedAddresses.toList();
|
|
|
|
for (var i = 0; i < txs.length; i++) {
|
|
|
|
for (var j = 0; j < txs[i].addressList.length; j++) {
|
|
|
|
adds.add(txs[i].addressList[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return adds.toSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
Set<String> _originalUsedAddresses = Set();
|
|
|
|
|
|
|
|
@override
|
|
|
|
set usedAddresses(Set<String> _usedAddresses) {
|
|
|
|
_originalUsedAddresses = _usedAddresses;
|
|
|
|
}
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
@override
|
|
|
|
Future<void> init() async {
|
|
|
|
accountList.update();
|
|
|
|
account = accountList.accounts.first;
|
2023-09-01 11:04:35 +00:00
|
|
|
updateSubaddressList(accountIndex: account?.id ?? 0);
|
2021-12-24 12:52:08 +00:00
|
|
|
await updateAddressesInBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateAddressesInBox() async {
|
|
|
|
try {
|
|
|
|
final _subaddressList = MoneroSubaddressList();
|
|
|
|
|
|
|
|
addressesMap.clear();
|
2023-08-29 16:11:51 +00:00
|
|
|
addressInfos.clear();
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
accountList.accounts.forEach((account) {
|
|
|
|
_subaddressList.update(accountIndex: account.id);
|
|
|
|
_subaddressList.subaddresses.forEach((subaddress) {
|
|
|
|
addressesMap[subaddress.address] = subaddress.label;
|
2023-08-29 16:11:51 +00:00
|
|
|
addressInfos[account.id] ??= [];
|
|
|
|
addressInfos[account.id]?.add(AddressInfo(
|
|
|
|
address: subaddress.address, label: subaddress.label, accountIndex: account.id));
|
2021-12-24 12:52:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await saveAddressesInBox();
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validate() {
|
|
|
|
accountList.update();
|
2023-08-29 16:11:51 +00:00
|
|
|
final accountListLength = accountList.accounts.length;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (accountListLength <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
subaddressList.update(accountIndex: accountList.accounts.first.id);
|
2023-08-29 16:11:51 +00:00
|
|
|
final subaddressListLength = subaddressList.subaddresses.length;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (subaddressListLength <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void updateSubaddressList({required int accountIndex}) {
|
2021-12-24 12:52:08 +00:00
|
|
|
subaddressList.update(accountIndex: accountIndex);
|
|
|
|
subaddress = subaddressList.subaddresses.first;
|
2022-10-12 17:09:57 +00:00
|
|
|
address = subaddress!.address;
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
2023-08-29 16:11:51 +00:00
|
|
|
|
|
|
|
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());
|
2024-08-07 16:12:49 +00:00
|
|
|
subaddress = (subaddressList.subaddresses.isEmpty) ? Subaddress(id: 0, address: address, label: defaultLabel) : subaddressList.subaddresses.last;
|
2023-08-29 16:11:51 +00:00
|
|
|
address = subaddress!.address;
|
|
|
|
}
|
2023-11-02 15:52:47 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool containsAddress(String address) =>
|
|
|
|
addressInfos[account?.id ?? 0]?.any((it) => it.address == address) ?? false;
|
2023-08-29 16:11:51 +00:00
|
|
|
}
|