From 3025fc359ac1c44c411f117966cf8eabbee4dd68 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Wed, 3 Jul 2024 17:01:47 -0500 Subject: [PATCH] add address type to desktop receive --- .../sub_widgets/desktop_receive.dart | 143 +++++++++++++++--- 1 file changed, 121 insertions(+), 22 deletions(-) diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_receive.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_receive.dart index 0da2a6607..a4e4a16cb 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_receive.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_receive.dart @@ -29,11 +29,15 @@ import '../../../../utilities/address_utils.dart'; import '../../../../utilities/assets.dart'; import '../../../../utilities/clipboard_interface.dart'; import '../../../../utilities/constants.dart'; +import '../../../../utilities/enums/derive_path_type_enum.dart'; import '../../../../utilities/text_styles.dart'; import '../../../../utilities/util.dart'; import '../../../../wallets/crypto_currency/crypto_currency.dart'; import '../../../../wallets/isar/providers/eth/current_token_wallet_provider.dart'; import '../../../../wallets/isar/providers/wallet_info_provider.dart'; +import '../../../../wallets/wallet/impl/bitcoin_wallet.dart'; +import '../../../../wallets/wallet/intermediate/bip39_hd_wallet.dart'; +import '../../../../wallets/wallet/wallet_mixin_interfaces/bcash_interface.dart'; import '../../../../wallets/wallet/wallet_mixin_interfaces/multi_address_interface.dart'; import '../../../../wallets/wallet/wallet_mixin_interfaces/spark_interface.dart'; import '../../../../widgets/conditional_parent.dart'; @@ -52,6 +56,8 @@ class DesktopReceive extends ConsumerStatefulWidget { this.clipboard = const ClipboardWrapper(), }); + static const String routeName = "/desktopReceive"; + final String walletId; final String? contractAddress; final ClipboardInterface clipboard; @@ -65,11 +71,18 @@ class _DesktopReceiveState extends ConsumerState { late final String walletId; late final ClipboardInterface clipboard; late final bool supportsSpark; + late final bool showMultiType; String? _sparkAddress; String? _qrcodeContent; bool _showSparkAddress = true; + int _currentIndex = 0; + + final List _walletAddressTypes = []; + final Map _addressMap = {}; + final Map> _addressSubMap = {}; + Future generateNewAddress() async { final wallet = ref.read(pWallets).getWallet(walletId); if (wallet is MultiAddressInterface) { @@ -97,10 +110,32 @@ class _DesktopReceiveState extends ConsumerState { await wallet.generateNewReceivingAddress(); + final Address? address; + if (wallet is Bip39HDWallet && wallet is! BCashInterface) { + final type = DerivePathType.values.firstWhere( + (e) => e.getAddressType() == _walletAddressTypes[_currentIndex], + ); + address = await wallet.generateNextReceivingAddress( + derivePathType: type, + ); + await ref.read(mainDBProvider).isar.writeTxn(() async { + await ref.read(mainDBProvider).isar.addresses.put(address!); + }); + } else { + await wallet.generateNewReceivingAddress(); + address = null; + } + shouldPop = true; if (mounted) { - Navigator.of(context, rootNavigator: true).pop(); + Navigator.of(context) + .popUntil(ModalRoute.withName(DesktopReceive.routeName)); + + setState(() { + _addressMap[_walletAddressTypes[_currentIndex]] = + address?.value ?? ref.read(pWalletReceivingAddress(walletId)); + }); } } } @@ -155,7 +190,57 @@ class _DesktopReceiveState extends ConsumerState { walletId = widget.walletId; coin = ref.read(pWalletInfo(walletId)).coin; clipboard = widget.clipboard; + final wallet = ref.read(pWallets).getWallet(walletId); supportsSpark = ref.read(pWallets).getWallet(walletId) is SparkInterface; + showMultiType = supportsSpark || + ref.read(pWallets).getWallet(walletId) is MultiAddressInterface; + + _walletAddressTypes.add(wallet.info.mainAddressType); + + if (showMultiType) { + if (supportsSpark) { + _walletAddressTypes.insert(0, AddressType.spark); + } else { + _walletAddressTypes.addAll( + (wallet as Bip39HDWallet) + .supportedAddressTypes + .where((e) => e != wallet.info.mainAddressType), + ); + } + } + + if (_walletAddressTypes.length > 1 && wallet is BitcoinWallet) { + _walletAddressTypes.removeWhere((e) => e == AddressType.p2pkh); + } + + _addressMap[_walletAddressTypes[_currentIndex]] = + ref.read(pWalletReceivingAddress(walletId)); + + if (showMultiType) { + for (final type in _walletAddressTypes) { + _addressSubMap[type] = ref + .read(mainDBProvider) + .isar + .addresses + .where() + .walletIdEqualTo(walletId) + .filter() + .typeEqualTo(type) + .sortByDerivationIndexDesc() + .findFirst() + .asStream() + .listen((event) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + setState(() { + _addressMap[type] = + event?.value ?? _addressMap[type] ?? "[No address yet]"; + }); + } + }); + }); + } + } if (supportsSpark) { _streamSub = ref @@ -193,6 +278,13 @@ class _DesktopReceiveState extends ConsumerState { Widget build(BuildContext context) { debugPrint("BUILD: $runtimeType"); + final String address; + if (showMultiType) { + address = _addressMap[_walletAddressTypes[_currentIndex]]!; + } else { + address = ref.watch(pWalletReceivingAddress(walletId)); + } + if (supportsSpark) { if (_showSparkAddress) { _qrcodeContent = _sparkAddress; @@ -207,33 +299,30 @@ class _DesktopReceiveState extends ConsumerState { crossAxisAlignment: CrossAxisAlignment.stretch, children: [ ConditionalParent( - condition: supportsSpark, + condition: showMultiType, builder: (child) => Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ DropdownButtonHideUnderline( - child: DropdownButton2( - value: _showSparkAddress, + child: DropdownButton2( + value: _currentIndex, items: [ - DropdownMenuItem( - value: true, - child: Text( - "Spark address", - style: STextStyles.desktopTextMedium(context), + for (int i = 0; i < _walletAddressTypes.length; i++) + DropdownMenuItem( + value: i, + child: Text( + supportsSpark && + _walletAddressTypes[i] == AddressType.p2pkh + ? "Transparent address" + : "${_walletAddressTypes[i].readableName} address", + style: STextStyles.w500_14(context), + ), ), - ), - DropdownMenuItem( - value: false, - child: Text( - "Transparent address", - style: STextStyles.desktopTextMedium(context), - ), - ), ], onChanged: (value) { - if (value is bool && value != _showSparkAddress) { + if (value != null && value != _currentIndex) { setState(() { - _showSparkAddress = value; + _currentIndex = value; }); } }, @@ -251,6 +340,16 @@ class _DesktopReceiveState extends ConsumerState { ), ), ), + buttonStyleData: ButtonStyleData( + decoration: BoxDecoration( + color: Theme.of(context) + .extension()! + .textFieldDefaultBG, + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, + ), + ), + ), dropdownStyleData: DropdownStyleData( offset: const Offset(0, -10), elevation: 0, @@ -280,7 +379,7 @@ class _DesktopReceiveState extends ConsumerState { child: GestureDetector( onTap: () { clipboard.setData( - ClipboardData(text: _sparkAddress ?? "Error"), + ClipboardData(text: address), ); showFloatingFlushBar( type: FlushBarType.info, @@ -311,7 +410,7 @@ class _DesktopReceiveState extends ConsumerState { pCurrentTokenWallet.select( (value) => value!.tokenContract.symbol, ), - )} SPARK address", + )}${supportsSpark ? " SPARK" : ""} address", style: STextStyles.itemSubtitle(context), ), const Spacer(), @@ -343,7 +442,7 @@ class _DesktopReceiveState extends ConsumerState { children: [ Expanded( child: Text( - _sparkAddress ?? "Error", + address, style: STextStyles.desktopTextExtraExtraSmall( context,