mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-26 06:08:40 +01:00
100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'package:cw_core/payment_uris.dart';
|
|
import 'package:cw_core/receive_page_option.dart';
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
|
import 'package:cw_core/wallet_info.dart';
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
abstract class WalletAddresses {
|
|
WalletAddresses(this.walletInfo, [this.isTestnet = false])
|
|
: addressesMap = {},
|
|
allAddressesMap = {},
|
|
addressInfos = {},
|
|
usedAddresses = {},
|
|
hiddenAddresses = {},
|
|
manualAddresses = {} {
|
|
walletInfo.getUsedAddresses().then((value) => usedAddresses = value);
|
|
walletInfo.getHiddenAddresses().then((value) => hiddenAddresses = value);
|
|
walletInfo.getManualAddresses().then((value) => manualAddresses = value);
|
|
}
|
|
|
|
final WalletInfo walletInfo;
|
|
|
|
final bool isTestnet;
|
|
|
|
String get address;
|
|
|
|
String get latestAddress {
|
|
if ([WalletType.monero, WalletType.wownero].contains(walletInfo.type)) {
|
|
if (addressesMap.keys.isEmpty) return address;
|
|
return addressesMap[addressesMap.keys.last] ?? address;
|
|
}
|
|
return _localAddress ?? address;
|
|
}
|
|
|
|
String get primaryAddress => address;
|
|
|
|
String? _localAddress;
|
|
|
|
set address(String address) => _localAddress = address;
|
|
|
|
String get addressForExchange => address;
|
|
|
|
String get addressForBuy => address;
|
|
|
|
Map<String, String> addressesMap;
|
|
Map<String, String> allAddressesMap;
|
|
|
|
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;
|
|
}
|
|
|
|
Map<int, List<WalletInfoAddressInfo>> addressInfos;
|
|
|
|
Set<String> usedAddresses;
|
|
|
|
Set<String> hiddenAddresses;
|
|
|
|
Set<String> manualAddresses;
|
|
|
|
Future<void> init();
|
|
|
|
Future<void> updateAddressesInBox();
|
|
|
|
Future<void> saveAddressesInBox() async {
|
|
try {
|
|
walletInfo.address = address;
|
|
// TODO: check if it will affect the performance of each wallet
|
|
await walletInfo.setAddresses(addressesMap);
|
|
await walletInfo.setAddressInfos(addressInfos);
|
|
await walletInfo.setUsedAddresses(usedAddresses.toList());
|
|
await walletInfo.setHiddenAddresses(hiddenAddresses.toList());
|
|
await walletInfo.setManualAddresses(manualAddresses.toList());
|
|
|
|
await walletInfo.save();
|
|
} catch (e) {
|
|
printV(e.toString());
|
|
}
|
|
}
|
|
|
|
bool containsAddress(String address) =>
|
|
addressesMap.containsKey(address) || allAddressesMap.containsKey(address);
|
|
|
|
List<ReceivePageOption> get receivePageOptions => ReceivePageOptions;
|
|
|
|
/// Get a [PaymentURI] for the current [address]
|
|
/// e.g. ethereum:0x0
|
|
PaymentURI getPaymentUri(String amount);
|
|
|
|
/// Get a [PaymentURI] for the current [address] asynchronously
|
|
/// this can be used if a payment requires a api call beforehand
|
|
Future<PaymentURI> getPaymentRequestUri(String amount) async => getPaymentUri(amount);
|
|
}
|