2023-08-29 16:11:51 +00:00
|
|
|
import 'package:cw_core/address_info.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_info.dart';
|
2024-09-28 02:38:23 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
abstract class WalletAddresses {
|
2022-10-12 17:09:57 +00:00
|
|
|
WalletAddresses(this.walletInfo)
|
2024-03-29 18:51:34 +00:00
|
|
|
: addressesMap = {},
|
|
|
|
allAddressesMap = {},
|
2024-09-28 02:38:23 +00:00
|
|
|
addressInfos = {},
|
|
|
|
usedAddresses = {},
|
|
|
|
hiddenAddresses = walletInfo.hiddenAddresses?.toSet() ?? {},
|
|
|
|
manualAddresses = walletInfo.manualAddresses?.toSet() ?? {};
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
final WalletInfo walletInfo;
|
|
|
|
|
|
|
|
String get address;
|
|
|
|
|
2024-09-28 02:38:23 +00:00
|
|
|
String get latestAddress {
|
|
|
|
if (walletInfo.type == WalletType.monero || walletInfo.type == WalletType.wownero) {
|
|
|
|
if (addressesMap.keys.length == 0) return address;
|
|
|
|
return addressesMap[addressesMap.keys.last] ?? address;
|
|
|
|
}
|
|
|
|
return _localAddress ?? address;
|
|
|
|
}
|
|
|
|
|
2024-10-21 10:03:53 +00:00
|
|
|
String get primaryAddress => address;
|
2024-01-11 15:29:51 +00:00
|
|
|
|
2024-09-28 02:38:23 +00:00
|
|
|
String? _localAddress;
|
|
|
|
|
|
|
|
set address(String address) => _localAddress = address;
|
|
|
|
|
|
|
|
String get addressForExchange => address;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
Map<String, String> addressesMap;
|
2024-03-29 18:51:34 +00:00
|
|
|
Map<String, String> allAddressesMap;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2024-09-28 02:38:23 +00:00
|
|
|
Map<String, String> get usableAddressesMap {
|
|
|
|
final tmp = addressesMap.map((key, value) => MapEntry(key, value)); // copy address map
|
|
|
|
tmp.removeWhere((key, value) => hiddenAddresses.contains(key) || manualAddresses.contains(key));
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, String> get usableAllAddressesMap {
|
|
|
|
final tmp = allAddressesMap.map((key, value) => MapEntry(key, value)); // copy address map
|
|
|
|
tmp.removeWhere((key, value) => hiddenAddresses.contains(key) || manualAddresses.contains(key));
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-08-29 16:11:51 +00:00
|
|
|
Map<int, List<AddressInfo>> addressInfos;
|
|
|
|
|
2024-09-28 02:38:23 +00:00
|
|
|
Set<String> usedAddresses;
|
|
|
|
|
|
|
|
Set<String> hiddenAddresses;
|
|
|
|
|
|
|
|
Set<String> manualAddresses;
|
2023-08-29 16:11:51 +00:00
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
Future<void> init();
|
|
|
|
|
|
|
|
Future<void> updateAddressesInBox();
|
|
|
|
|
|
|
|
Future<void> saveAddressesInBox() async {
|
|
|
|
try {
|
|
|
|
walletInfo.address = address;
|
|
|
|
walletInfo.addresses = addressesMap;
|
2023-08-29 16:11:51 +00:00
|
|
|
walletInfo.addressInfos = addressInfos;
|
|
|
|
walletInfo.usedAddresses = usedAddresses.toList();
|
2024-09-28 02:38:23 +00:00
|
|
|
walletInfo.hiddenAddresses = hiddenAddresses.toList();
|
|
|
|
walletInfo.manualAddresses = manualAddresses.toList();
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (walletInfo.isInBox) {
|
|
|
|
await walletInfo.save();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-12-09 18:23:59 +00:00
|
|
|
printV(e.toString());
|
2021-12-24 12:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-02 15:52:47 +00:00
|
|
|
|
2024-04-12 14:01:21 +00:00
|
|
|
bool containsAddress(String address) =>
|
|
|
|
addressesMap.containsKey(address) || allAddressesMap.containsKey(address);
|
2023-11-02 15:52:47 +00:00
|
|
|
}
|