2022-03-30 15:57:04 +00:00
|
|
|
import 'package:cw_core/wallet_addresses_with_account.dart';
|
|
|
|
import 'package:cw_core/wallet_info.dart';
|
|
|
|
import 'package:cw_core/account.dart';
|
|
|
|
import 'package:cw_haven/haven_account_list.dart';
|
|
|
|
import 'package:cw_haven/haven_subaddress_list.dart';
|
|
|
|
import 'package:cw_core/subaddress.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
part 'haven_wallet_addresses.g.dart';
|
|
|
|
|
|
|
|
class HavenWalletAddresses = HavenWalletAddressesBase
|
|
|
|
with _$HavenWalletAddresses;
|
|
|
|
|
|
|
|
abstract class HavenWalletAddressesBase extends WalletAddressesWithAccount<Account> with Store {
|
2022-10-12 17:09:57 +00:00
|
|
|
HavenWalletAddressesBase(WalletInfo walletInfo)
|
|
|
|
: accountList = HavenAccountList(),
|
|
|
|
subaddressList = HavenSubaddressList(),
|
|
|
|
address = '',
|
|
|
|
super(walletInfo);
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
@observable
|
|
|
|
String address;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
// @override
|
2022-03-30 15:57:04 +00:00
|
|
|
@observable
|
2022-10-12 17:09:57 +00:00
|
|
|
Account? account;
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
@observable
|
2022-10-12 17:09:57 +00:00
|
|
|
Subaddress? subaddress;
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
HavenSubaddressList subaddressList;
|
|
|
|
|
|
|
|
HavenAccountList accountList;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> init() async {
|
|
|
|
accountList.update();
|
|
|
|
account = accountList.accounts.first;
|
2022-10-12 17:09:57 +00:00
|
|
|
updateSubaddressList(accountIndex: account?.id ?? 0);
|
2022-03-30 15:57:04 +00:00
|
|
|
await updateAddressesInBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateAddressesInBox() async {
|
|
|
|
try {
|
|
|
|
final _subaddressList = HavenSubaddressList();
|
|
|
|
|
|
|
|
addressesMap.clear();
|
|
|
|
|
|
|
|
accountList.accounts.forEach((account) {
|
|
|
|
_subaddressList.update(accountIndex: account.id);
|
|
|
|
_subaddressList.subaddresses.forEach((subaddress) {
|
|
|
|
addressesMap[subaddress.address] = subaddress.label;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await saveAddressesInBox();
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validate() {
|
|
|
|
accountList.update();
|
2022-10-12 17:09:57 +00:00
|
|
|
final accountListLength = accountList.accounts.length ?? 0;
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
if (accountListLength <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
subaddressList.update(accountIndex: accountList.accounts.first.id);
|
2022-10-12 17:09:57 +00:00
|
|
|
final subaddressListLength = subaddressList.subaddresses.length ?? 0;
|
2022-03-30 15:57:04 +00:00
|
|
|
|
|
|
|
if (subaddressListLength <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void updateSubaddressList({required int accountIndex}) {
|
2022-03-30 15:57:04 +00:00
|
|
|
subaddressList.update(accountIndex: accountIndex);
|
|
|
|
subaddress = subaddressList.subaddresses.first;
|
2022-10-12 17:09:57 +00:00
|
|
|
address = subaddress!.address;
|
2022-03-30 15:57:04 +00:00
|
|
|
}
|
|
|
|
}
|