| 1 | import 'package:cw_core/payment_uris.dart'; |
| 2 | import 'package:cw_core/receive_page_option.dart'; |
| 3 | import 'package:cw_core/utils/print_verbose.dart'; |
| 4 | import 'package:cw_core/wallet_info.dart'; |
| 5 | import 'package:cw_core/wallet_type.dart'; |
| 6 | |
| 7 | abstract class WalletAddresses { |
| 8 | WalletAddresses(this.walletInfo, [this.isTestnet = false]) |
| 9 | : addressesMap = {}, |
| 10 | allAddressesMap = {}, |
| 11 | addressInfos = {}, |
| 12 | usedAddresses = {}, |
| 13 | hiddenAddresses = {}, |
| 14 | manualAddresses = {} { |
| 15 | walletInfo.getUsedAddresses().then((value) => usedAddresses = value); |
| 16 | walletInfo.getHiddenAddresses().then((value) => hiddenAddresses = value); |
| 17 | walletInfo.getManualAddresses().then((value) => manualAddresses = value); |
| 18 | } |
| 19 | |
| 20 | final WalletInfo walletInfo; |
| 21 | |
| 22 | final bool isTestnet; |
| 23 | |
| 24 | String get address; |
| 25 | |
| 26 | String get latestAddress { |
| 27 | if ([WalletType.monero, WalletType.wownero].contains(walletInfo.type)) { |
| 28 | if (addressesMap.keys.isEmpty) return address; |
| 29 | return addressesMap[addressesMap.keys.last] ?? address; |
| 30 | } |
| 31 | return _localAddress ?? address; |
| 32 | } |
| 33 | |
| 34 | String get primaryAddress => address; |
| 35 | |
| 36 | String? _localAddress; |
| 37 | |
| 38 | set address(String address) => _localAddress = address; |
| 39 | |
| 40 | String get addressForExchange => address; |
| 41 | |
| 42 | String get addressForBuy => address; |
| 43 | |
| 44 | Map<String, String> addressesMap; |
| 45 | Map<String, String> allAddressesMap; |
| 46 | |
| 47 | Map<String, String> get usableAddressesMap { |
| 48 | final tmp = addressesMap.map((key, value) => MapEntry(key, value)); // copy address map |
| 49 | tmp.removeWhere((key, value) => hiddenAddresses.contains(key) || manualAddresses.contains(key)); |
| 50 | return tmp; |
| 51 | } |
| 52 | |
| 53 | Map<String, String> get usableAllAddressesMap { |
| 54 | final tmp = allAddressesMap.map((key, value) => MapEntry(key, value)); // copy address map |
| 55 | tmp.removeWhere((key, value) => hiddenAddresses.contains(key) || manualAddresses.contains(key)); |
| 56 | return tmp; |
| 57 | } |
| 58 | |
| 59 | Map<int, List<WalletInfoAddressInfo>> addressInfos; |
| 60 | |
| 61 | Set<String> usedAddresses; |
| 62 | |
| 63 | Set<String> hiddenAddresses; |
| 64 | |
| 65 | Set<String> manualAddresses; |
| 66 | |
| 67 | Future<void> init(); |
| 68 | |
| 69 | Future<void> updateAddressesInBox(); |
| 70 | |
| 71 | Future<void> saveAddressesInBox() async { |
| 72 | try { |
| 73 | walletInfo.address = address; |
| 74 | // TODO: check if it will affect the performance of each wallet |
| 75 | await walletInfo.setAddresses(addressesMap); |
| 76 | await walletInfo.setAddressInfos(addressInfos); |
| 77 | await walletInfo.setUsedAddresses(usedAddresses.toList()); |
| 78 | await walletInfo.setHiddenAddresses(hiddenAddresses.toList()); |
| 79 | await walletInfo.setManualAddresses(manualAddresses.toList()); |
| 80 | |
| 81 | await walletInfo.save(); |
| 82 | } catch (e) { |
| 83 | printV(e.toString()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool containsAddress(String address) => |
| 88 | addressesMap.containsKey(address) || allAddressesMap.containsKey(address); |
| 89 | |
| 90 | List<ReceivePageOption> get receivePageOptions => ReceivePageOptions; |
| 91 | |
| 92 | /// Get a [PaymentURI] for the current [address] |
| 93 | /// e.g. ethereum:0x0 |
| 94 | PaymentURI getPaymentUri(String amount); |
| 95 | |
| 96 | /// Get a [PaymentURI] for the current [address] asynchronously |
| 97 | /// this can be used if a payment requires a api call beforehand |
| 98 | Future<PaymentURI> getPaymentRequestUri(String amount) async => getPaymentUri(amount); |
| 99 | } |