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';
|
2024-12-09 18:23:59 +00:00
|
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_core/wallet_addresses.dart';
|
|
|
|
import 'package:cw_core/wallet_info.dart';
|
2024-09-28 02:38:23 +00:00
|
|
|
import 'package:cw_monero/api/subaddress_list.dart' as subaddress_list;
|
|
|
|
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
|
|
|
|
2024-10-18 02:58:57 +00:00
|
|
|
@override
|
|
|
|
String get primaryAddress => getAddress(accountIndex: account?.id ?? 0, addressIndex: 0);
|
|
|
|
|
2024-09-28 02:38:23 +00:00
|
|
|
@override
|
|
|
|
String get latestAddress {
|
|
|
|
var addressIndex = subaddress_list.numSubaddresses(account?.id??0) - 1;
|
|
|
|
var address = getAddress(accountIndex: account?.id??0, addressIndex: addressIndex);
|
|
|
|
while (hiddenAddresses.contains(address)) {
|
|
|
|
addressIndex++;
|
|
|
|
address = getAddress(accountIndex: account?.id??0, addressIndex: addressIndex);
|
|
|
|
subaddressList.update(accountIndex: account?.id??0);
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get addressForExchange {
|
|
|
|
var addressIndex = subaddress_list.numSubaddresses(account?.id??0) - 1;
|
|
|
|
var address = getAddress(accountIndex: account?.id??0, addressIndex: addressIndex);
|
|
|
|
while (hiddenAddresses.contains(address) || manualAddresses.contains(address) || subaddress_list.getRawLabel(accountIndex: account?.id??0, addressIndex: addressIndex).isNotEmpty) {
|
|
|
|
addressIndex++;
|
|
|
|
address = getAddress(accountIndex: account?.id??0, addressIndex: addressIndex);
|
|
|
|
subaddressList.update(accountIndex: account?.id??0);
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
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-09-28 02:38:23 +00:00
|
|
|
Set<String> usedAddresses = Set();
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
@override
|
|
|
|
Future<void> init() async {
|
|
|
|
accountList.update();
|
2024-09-28 02:38:23 +00:00
|
|
|
account = accountList.accounts.isEmpty ? Account(id: 0, label: "Primary address") : 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) {
|
2024-12-09 18:23:59 +00:00
|
|
|
printV(e.toString());
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2024-09-28 02:38:23 +00:00
|
|
|
address = subaddressList.subaddresses.isNotEmpty
|
|
|
|
? subaddressList.subaddresses.first.address
|
|
|
|
: getAddress();
|
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-09-28 02:38:23 +00:00
|
|
|
subaddress = (subaddressList.subaddresses.isEmpty) ? Subaddress(id: 0, address: address, label: defaultLabel, balance: '0', txCount: 0) : subaddressList.subaddresses.last;
|
|
|
|
if (num.tryParse(subaddress!.balance??'0') != 0) {
|
|
|
|
getAddress(accountIndex: accountIndex, addressIndex: (subaddress?.id??0)+1);
|
|
|
|
}
|
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
|
|
|
}
|