From 3f2e397aaf5ea0c95a2641d85b0f7547786e1646 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Thu, 3 Dec 2026 15:40:58 -0600 Subject: [PATCH 1/2] style: dart format restore_options_view.dart --- .../restore_options_view.dart | 106 ++++++++---------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart b/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart index e650564a1..f6b66df93 100644 --- a/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart +++ b/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart @@ -273,9 +273,8 @@ class _RestoreOptionsViewState extends ConsumerState { onPressed: () { if (textFieldFocusNode.hasFocus) { textFieldFocusNode.unfocus(); - Future.delayed( - const Duration(milliseconds: 100), - ).then((value) => Navigator.of(context).pop()); + Future.delayed(const Duration(milliseconds: 100)) + .then((value) => Navigator.of(context).pop()); } else { Navigator.of(context).pop(); } @@ -310,12 +309,12 @@ class _RestoreOptionsViewState extends ConsumerState { key: UniqueKey(), onText: "Seed", offText: "View Only", - onColor: Theme.of( - context, - ).extension()!.popupBG, - offColor: Theme.of( - context, - ).extension()!.textFieldDefaultBG, + onColor: Theme.of(context) + .extension()! + .popupBG, + offColor: Theme.of(context) + .extension()! + .textFieldDefaultBG, isOn: _showViewOnlyOption, onValueChanged: (value) { setState(() { @@ -434,9 +433,9 @@ class _SeedRestoreOptionState extends ConsumerState { ref.watch(_pIsUsingDate) ? "Choose start date" : "Block height", style: Util.isDesktop ? STextStyles.desktopTextExtraSmall(context).copyWith( - color: Theme.of( - context, - ).extension()!.textDark3, + color: Theme.of(context) + .extension()! + .textDark3, ) : STextStyles.smallMed12(context), textAlign: TextAlign.left, @@ -474,9 +473,8 @@ class _SeedRestoreOptionState extends ConsumerState { inputFormatters: [FilteringTextInputFormatter.digitsOnly], textInputAction: TextInputAction.done, style: Util.isDesktop - ? STextStyles.desktopTextMedium( - context, - ).copyWith(height: 2) + ? STextStyles.desktopTextMedium(context) + .copyWith(height: 2) : STextStyles.field(context), onChanged: (value) { setState(() { @@ -492,8 +490,7 @@ class _SeedRestoreOptionState extends ConsumerState { suffixIcon: UnconstrainedBox( child: TextFieldIconButton( child: Semantics( - label: - "Clear Block Height Field Button. Clears the block height field", + label: "Clear Block Height Field Button. Clears the block height field", excludeSemantics: true, child: !_blockFieldEmpty ? XIcon( @@ -528,9 +525,9 @@ class _SeedRestoreOptionState extends ConsumerState { : "Enter the initial block height of the wallet", style: Util.isDesktop ? STextStyles.desktopTextExtraSmall(context).copyWith( - color: Theme.of( - context, - ).extension()!.textSubtitle1, + color: Theme.of(context) + .extension()! + .textSubtitle1, ) : STextStyles.smallMed12(context).copyWith(fontSize: 10), ), @@ -582,9 +579,9 @@ class _SeedRestoreOptionState extends ConsumerState { Assets.svg.chevronDown, width: 12, height: 6, - color: Theme.of( - context, - ).extension()!.textFieldActiveSearchIconRight, + color: Theme.of(context) + .extension()! + .textFieldActiveSearchIconRight, ), ), ), @@ -592,9 +589,9 @@ class _SeedRestoreOptionState extends ConsumerState { offset: const Offset(0, -10), elevation: 0, decoration: BoxDecoration( - color: Theme.of( - context, - ).extension()!.textFieldDefaultBG, + color: Theme.of(context) + .extension()! + .textFieldDefaultBG, borderRadius: BorderRadius.circular( Constants.size.circularBorderRadius, ), @@ -632,13 +629,12 @@ class _SeedRestoreOptionState extends ConsumerState { Text( "Advanced", style: Util.isDesktop - ? STextStyles.desktopTextExtraExtraSmall( - context, - ).copyWith( - color: Theme.of( - context, - ).extension()!.textDark3, - ) + ? STextStyles.desktopTextExtraExtraSmall(context) + .copyWith( + color: Theme.of(context) + .extension()! + .textDark3, + ) : STextStyles.smallMed12(context), textAlign: TextAlign.left, ), @@ -670,9 +666,8 @@ class _SeedRestoreOptionState extends ConsumerState { focusNode: widget.pwFocusNode, controller: widget.pwController, style: Util.isDesktop - ? STextStyles.desktopTextMedium( - context, - ).copyWith(height: 2) + ? STextStyles.desktopTextMedium(context) + .copyWith(height: 2) : STextStyles.field(context), obscureText: _hidePassword, enableSuggestions: false, @@ -706,9 +701,9 @@ class _SeedRestoreOptionState extends ConsumerState { _hidePassword ? Assets.svg.eye : Assets.svg.eyeSlash, - color: Theme.of( - context, - ).extension()!.textDark3, + color: Theme.of(context) + .extension()! + .textDark3, width: Util.isDesktop ? 24 : 16, height: Util.isDesktop ? 24 : 16, ), @@ -734,13 +729,12 @@ class _SeedRestoreOptionState extends ConsumerState { "was created with an optional BIP39 passphrase " "you can enter it here.", style: Util.isDesktop - ? STextStyles.desktopTextExtraSmall( - context, - ).copyWith( - color: Theme.of( - context, - ).extension()!.textSubtitle1, - ) + ? STextStyles.desktopTextExtraSmall(context) + .copyWith( + color: Theme.of(context) + .extension()! + .textSubtitle1, + ) : STextStyles.itemSubtitle(context), ), ), @@ -800,9 +794,9 @@ class _ViewOnlyRestoreOptionState extends ConsumerState { ref.watch(_pIsUsingDate) ? "Choose start date" : "Block height", style: Util.isDesktop ? STextStyles.desktopTextExtraExtraSmall(context).copyWith( - color: Theme.of( - context, - ).extension()!.textDark3, + color: Theme.of(context) + .extension()! + .textDark3, ) : STextStyles.smallMed12(context), textAlign: TextAlign.left, @@ -837,9 +831,8 @@ class _ViewOnlyRestoreOptionState extends ConsumerState { inputFormatters: [FilteringTextInputFormatter.digitsOnly], textInputAction: TextInputAction.done, style: Util.isDesktop - ? STextStyles.desktopTextMedium( - context, - ).copyWith(height: 2) + ? STextStyles.desktopTextMedium(context) + .copyWith(height: 2) : STextStyles.field(context), onChanged: (value) { setState(() { @@ -855,8 +848,7 @@ class _ViewOnlyRestoreOptionState extends ConsumerState { suffixIcon: UnconstrainedBox( child: TextFieldIconButton( child: Semantics( - label: - "Clear Block Height Field Button. Clears the block height field", + label: "Clear Block Height Field Button. Clears the block height field", excludeSemantics: true, child: !_blockFieldEmpty ? XIcon( @@ -886,9 +878,9 @@ class _ViewOnlyRestoreOptionState extends ConsumerState { : "Enter the initial block height of the wallet", style: Util.isDesktop ? STextStyles.desktopTextExtraSmall(context).copyWith( - color: Theme.of( - context, - ).extension()!.textSubtitle1, + color: Theme.of(context) + .extension()! + .textSubtitle1, ) : STextStyles.smallMed12(context).copyWith(fontSize: 10), ), From caed3d64d1ce7f0085bfe792562cf668865f178f Mon Sep 17 00:00:00 2001 From: sneurlax Date: Thu, 3 Dec 2026 16:18:32 -0600 Subject: [PATCH 2/2] feat: integrate Zcash with the wallet UI --- README.md | 1 + .../wallet_creation/coin_matrix.dart | 1 + ...w_wallet_recovery_phrase_warning_view.dart | 29 ++++ .../restore_options_view.dart | 69 ++++----- .../restore_wallet_view.dart | 30 +++- lib/pages/send_view/send_view.dart | 16 ++- .../helpers/restore_create_backup.dart | 28 ++++ .../wallet_view/sub_widgets/desktop_send.dart | 12 +- lib/providers/wallet/send_fee_providers.dart | 5 +- lib/utilities/test_node_connection.dart | 22 +++ .../intermediate/zcash_account_session.dart | 21 +++ scripts/app_config/configure_stack_wallet.sh | 1 + scripts/ensure_test_app_config.sh | 1 + scripts/test_wallet_creation.dart | 4 +- .../tests/wallet_creation_runner_test.dart | 6 +- .../zcash_restore_options_test.dart | 135 ++++++++++++++++++ test/utilities/native_library_check_test.dart | 4 +- test/wallets/zcash_account_session_test.dart | 29 ++++ 18 files changed, 370 insertions(+), 44 deletions(-) create mode 100644 test/pages/add_wallet_views/zcash_restore_options_test.dart diff --git a/README.md b/README.md index 70bf3f836..8acb064b7 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Highlights include: - [Tezos](https://tezos.com/) - [Wownero](https://wownero.org/) - [Xelis](https://xelis.org/) + - [Zcash](https://z.cash/) - All private keys and seeds stay on device and are never shared. - Easy backup and restore feature to save all the information that's important to you. - Trading cryptocurrencies through our partners. diff --git a/integration_test/wallet_creation/coin_matrix.dart b/integration_test/wallet_creation/coin_matrix.dart index 8a051daf8..c910d7c77 100644 --- a/integration_test/wallet_creation/coin_matrix.dart +++ b/integration_test/wallet_creation/coin_matrix.dart @@ -24,4 +24,5 @@ const defaultWordCounts = { 'tezos': 24, 'wownero': 16, 'xelis': 25, + 'zcash': 24, }; diff --git a/lib/pages/add_wallet_views/new_wallet_recovery_phrase_warning_view/new_wallet_recovery_phrase_warning_view.dart b/lib/pages/add_wallet_views/new_wallet_recovery_phrase_warning_view/new_wallet_recovery_phrase_warning_view.dart index b6651294f..1cd0a14dc 100644 --- a/lib/pages/add_wallet_views/new_wallet_recovery_phrase_warning_view/new_wallet_recovery_phrase_warning_view.dart +++ b/lib/pages/add_wallet_views/new_wallet_recovery_phrase_warning_view/new_wallet_recovery_phrase_warning_view.dart @@ -30,12 +30,14 @@ import '../../../utilities/text_styles.dart'; import '../../../utilities/util.dart'; import '../../../wallets/crypto_currency/crypto_currency.dart'; import '../../../wallets/isar/models/wallet_info.dart'; +import '../../../wallets/wallet/impl/zcash_wallet.dart'; import '../../../wallets/wallet/intermediate/cryptonote_wallet.dart'; import '../../../wallets/wallet/wallet.dart'; import '../../../wallets/wallet/wallet_mixin_interfaces/mnemonic_interface.dart'; import '../../../widgets/custom_buttons/app_bar_icon_button.dart'; import '../../../widgets/desktop/desktop_app_bar.dart'; import '../../../widgets/desktop/desktop_scaffold.dart'; +import '../../../widgets/dialogs/tor_warning_dialog.dart'; import '../../../widgets/rounded_container.dart'; import '../../../widgets/rounded_white_container.dart'; import '../../../widgets/stack_dialog.dart'; @@ -67,6 +69,18 @@ class _NewWalletRecoveryPhraseWarningViewState late final bool isDesktop; Future _initNewWallet() async { + // A new Zcash wallet asks its node for the chain tip, and Zcash does not + // connect through Tor. + if (coin is Zcash && ref.read(prefsChangeNotifierProvider).useTor) { + final shouldContinue = + await showDialog( + context: context, + builder: (_) => TorWarningDialog(coin: coin), + ) ?? + false; + if (!shouldContinue || !mounted) return; + } + Exception? ex; final result = await showLoading( whileFuture: _initNewFuture(), @@ -240,6 +254,21 @@ class _NewWalletRecoveryPhraseWarningViewState if (wallet is CryptonoteWallet) { await wallet.init(wordCount: wordCount); + } else if (wallet is ZcashWallet) { + // A new wallet starts scanning at the chain tip, which it asks its + // node for. Without an answer there is no wallet to keep. + final wallets = ref.read(pWallets); + final secureStore = ref.read(secureStoreProvider); + try { + await wallet.init(isRestore: false); + } catch (e) { + await wallet.exit(); + await wallets.deleteWallet(info, secureStore); + throw Exception( + "A new Zcash wallet asks its node for the current block. " + "Check the connection and the node, then try again.\n\n$e", + ); + } } else { await wallet.init(); } diff --git a/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart b/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart index f6b66df93..372965d1e 100644 --- a/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart +++ b/lib/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart @@ -103,7 +103,13 @@ class _RestoreOptionsViewState extends ConsumerState { _blockHeightController.addListener(() { WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted) { - if (!ref.read(_pIsUsingDate)) { + if (coin is Zcash) { + // Zcash cannot scan from height 0. + setState(() { + _hasBlockHeight = + (int.tryParse(_blockHeightController.text) ?? 0) > 0; + }); + } else if (!ref.read(_pIsUsingDate)) { setState(() { _hasBlockHeight = _blockHeightController.text.isNotEmpty; }); @@ -138,7 +144,7 @@ class _RestoreOptionsViewState extends ConsumerState { if (mounted) { int height = 0; - if (ref.read(_pIsUsingDate)) { + if (coin is! Zcash && ref.read(_pIsUsingDate)) { height = getBlockHeightFromDate(_restoreFromDate); } else { height = int.tryParse(_blockHeightController.text) ?? 0; @@ -357,7 +363,9 @@ class _RestoreOptionsViewState extends ConsumerState { SizedBox(height: isDesktop ? 32 : 12), RestoreOptionsNextButton( isDesktop: isDesktop, - onPressed: ref.watch(_pIsUsingDate) || _hasBlockHeight + onPressed: + (coin is! Zcash && ref.watch(_pIsUsingDate)) || + _hasBlockHeight ? nextPressed : null, ), @@ -410,6 +418,16 @@ class _SeedRestoreOptionState extends ConsumerState { final isCnAnd25 = widget.coin is CryptonoteCurrency && currentLength == 25; + // A Zcash wallet scans from the block height it was made at. Nothing + // here turns a date into a Zcash height, so it takes the height only. + final heightOnly = widget.coin is Zcash; + final hasRestoreHeight = + isCnAnd25 || + widget.coin is Epiccash || + widget.coin is Mimblewimblecoin || + heightOnly; + final usingDate = !heightOnly && ref.watch(_pIsUsingDate); + final bool supportsPassphrase; if (widget.coin.hasMnemonicPassphraseSupport) { supportsPassphrase = true; @@ -423,14 +441,12 @@ class _SeedRestoreOptionState extends ConsumerState { return Column( children: [ - if (isCnAnd25 || - widget.coin is Epiccash || - widget.coin is Mimblewimblecoin) + if (hasRestoreHeight) Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( - ref.watch(_pIsUsingDate) ? "Choose start date" : "Block height", + usingDate ? "Choose start date" : "Block height", style: Util.isDesktop ? STextStyles.desktopTextExtraSmall(context).copyWith( color: Theme.of(context) @@ -440,24 +456,17 @@ class _SeedRestoreOptionState extends ConsumerState { : STextStyles.smallMed12(context), textAlign: TextAlign.left, ), - CustomTextButton( - text: ref.watch(_pIsUsingDate) - ? "Use block height" - : "Use date", - onTap: () => ref.read(_pIsUsingDate.notifier).state = !ref.read( - _pIsUsingDate, + if (!heightOnly) + CustomTextButton( + text: usingDate ? "Use block height" : "Use date", + onTap: () => ref.read(_pIsUsingDate.notifier).state = !ref + .read(_pIsUsingDate), ), - ), ], ), - if (isCnAnd25 || - widget.coin is Epiccash || - widget.coin is Mimblewimblecoin) - SizedBox(height: Util.isDesktop ? 16 : 8), - if (isCnAnd25 || - widget.coin is Epiccash || - widget.coin is Mimblewimblecoin) - ref.watch(_pIsUsingDate) + if (hasRestoreHeight) SizedBox(height: Util.isDesktop ? 16 : 8), + if (hasRestoreHeight) + usingDate ? RestoreFromDatePicker( onTap: widget.dateChooserFunction, controller: widget.dateController, @@ -510,17 +519,12 @@ class _SeedRestoreOptionState extends ConsumerState { ), ), ), - if (isCnAnd25 || - widget.coin is Epiccash || - widget.coin is Mimblewimblecoin) - const SizedBox(height: 8), - if (isCnAnd25 || - widget.coin is Epiccash || - widget.coin is Mimblewimblecoin) + if (hasRestoreHeight) const SizedBox(height: 8), + if (hasRestoreHeight) RoundedWhiteContainer( child: Center( child: Text( - ref.watch(_pIsUsingDate) + usingDate ? "Choose the date you made the wallet (approximate is fine)" : "Enter the initial block height of the wallet", style: Util.isDesktop @@ -533,10 +537,7 @@ class _SeedRestoreOptionState extends ConsumerState { ), ), ), - if (isCnAnd25 || - widget.coin is Epiccash || - widget.coin is Mimblewimblecoin) - SizedBox(height: Util.isDesktop ? 24 : 16), + if (hasRestoreHeight) SizedBox(height: Util.isDesktop ? 24 : 16), Text( "Choose recovery phrase length", style: Util.isDesktop diff --git a/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart b/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart index 8da30ef48..27a1c150a 100644 --- a/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart +++ b/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart @@ -44,6 +44,7 @@ import '../../../wallets/isar/models/wallet_info.dart'; import '../../../wallets/wallet/impl/epiccash_wallet.dart'; import '../../../wallets/wallet/impl/mimblewimblecoin_wallet.dart'; import '../../../wallets/wallet/impl/xelis_wallet.dart'; +import '../../../wallets/wallet/impl/zcash_wallet.dart'; import '../../../wallets/wallet/intermediate/cryptonote_wallet.dart'; import '../../../wallets/wallet/intermediate/external_wallet.dart'; import '../../../wallets/wallet/supporting/epiccash_wallet_info_extension.dart'; @@ -54,6 +55,7 @@ import '../../../widgets/custom_buttons/app_bar_icon_button.dart'; import '../../../widgets/desktop/desktop_app_bar.dart'; import '../../../widgets/desktop/desktop_scaffold.dart'; import '../../../widgets/desktop/primary_button.dart'; +import '../../../widgets/dialogs/tor_warning_dialog.dart'; import '../../../widgets/icon_widgets/clipboard_icon.dart'; import '../../../widgets/icon_widgets/qrcode_icon.dart'; import '../../../widgets/table_view/table_view.dart'; @@ -211,6 +213,19 @@ class _RestoreWalletViewState extends ConsumerState { Future attemptRestore() async { if (_formKey.currentState!.validate()) { + // A Zcash restore asks its node about the birthday, and Zcash does not + // connect through Tor. + if (widget.coin is Zcash && + ref.read(prefsChangeNotifierProvider).useTor) { + final shouldContinue = + await showDialog( + context: context, + builder: (_) => TorWarningDialog(coin: widget.coin), + ) ?? + false; + if (!shouldContinue || !mounted) return; + } + if (mounted) setState(() => _hideSeedWords = true); String mnemonic = ""; @@ -307,13 +322,17 @@ class _RestoreWalletViewState extends ConsumerState { if (mounted) setState(() => _hideSeedWords = false); + final cancelled = restored; + // A Zcash restore saves its scan state as it goes, so + // it stops before its files are deleted. + if (cancelled is ZcashWallet) await cancelled.exit(); + await ref .read(pWallets) .deleteWallet(info, ref.read(secureStoreProvider)); // Stop the cancelled restore's network work now: its // recover() fails and the wallet is never added. - final cancelled = restored; if (cancelled is ElectrumXInterface) { await cancelled.electrumXClient.closeAdapter(); } @@ -368,6 +387,10 @@ class _RestoreWalletViewState extends ConsumerState { await wallet.init(isRestore: true); break; + case ZcashWallet(): + await wallet.init(isRestore: true); + break; + default: await wallet.init(); } @@ -496,6 +519,11 @@ class _RestoreWalletViewState extends ConsumerState { !Wallets.sharedInstance.wallets.contains(unused)) { await unused.electrumXClient.closeAdapter(); } + // Nor its connection to a Zcash node. + if (unused is ZcashWallet && + !Wallets.sharedInstance.wallets.contains(unused)) { + await unused.exit(); + } } if (!Platform.isLinux && !isDesktop) { diff --git a/lib/pages/send_view/send_view.dart b/lib/pages/send_view/send_view.dart index 32d6231a0..e5c4aefa0 100644 --- a/lib/pages/send_view/send_view.dart +++ b/lib/pages/send_view/send_view.dart @@ -1003,7 +1003,9 @@ class _SendViewState extends ConsumerState with SendFeeForm { ), ); } else { - final memo = coin is Stellar ? memoController.text : null; + final memo = coin is Stellar || coin is Zcash + ? memoController.text + : null; txDataFuture = wallet.prepareSend( txData: TxData( xelisSendAll: coin is Xelis && _xelisSendAll, @@ -1174,6 +1176,16 @@ class _SendViewState extends ConsumerState with SendFeeForm { : ref.read(pWalletBalanceSecondary(walletId)).spendable; break; } + } else if (coin is Zcash) { + // A Zcash send does not take its fee out of the amount, so the amount + // leaves room for it. + final spendable = ref.read(pWalletBalance(walletId)).spendable; + final fee = await ref + .read(pWallets) + .getWallet(walletId) + .estimateFeeFor(spendable, BigInt.zero); + if (!mounted) return; + amount = fee < spendable ? spendable - fee : spendable; } else { amount = ref.read(pWalletBalance(walletId)).spendable; } @@ -1212,7 +1224,7 @@ class _SendViewState extends ConsumerState with SendFeeForm { coin = widget.coin; isFiro = coin is Firo; isEth = coin is Ethereum; - hasOptionalMemo = coin is Stellar || coin is Solana; + hasOptionalMemo = coin is Stellar || coin is Solana || coin is Zcash; _data = widget.autoFillData; walletId = widget.walletId; diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart index b131db831..98e73a3ce 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart @@ -49,7 +49,9 @@ import '../../../../../utilities/flutter_secure_storage_interface.dart'; import '../../../../../utilities/format.dart'; import '../../../../../utilities/logger.dart'; import '../../../../../utilities/prefs.dart'; +import '../../../../../utilities/stack_file_system.dart'; import '../../../../../utilities/util.dart'; +import '../../../../../wallets/crypto_currency/coins/zcash.dart'; import '../../../../../wallets/crypto_currency/intermediate/frost_currency.dart'; import '../../../../../wallets/isar/models/frost_wallet_info.dart'; import '../../../../../wallets/isar/models/wallet_info.dart'; @@ -57,7 +59,9 @@ import '../../../../../wallets/wallet/impl/bitcoin_frost_wallet.dart'; import '../../../../../wallets/wallet/impl/epiccash_wallet.dart'; import '../../../../../wallets/wallet/impl/mimblewimblecoin_wallet.dart'; import '../../../../../wallets/wallet/impl/xelis_wallet.dart'; +import '../../../../../wallets/wallet/impl/zcash_wallet.dart'; import '../../../../../wallets/wallet/intermediate/cryptonote_wallet.dart'; +import '../../../../../wallets/wallet/intermediate/zcash_wallet_persistence.dart'; import '../../../../../wallets/wallet/wallet.dart'; import '../../../../../wallets/wallet/wallet_mixin_interfaces/mnemonic_interface.dart'; import '../../../../../wallets/wallet/wallet_mixin_interfaces/private_key_interface.dart'; @@ -495,6 +499,10 @@ abstract class SWB { await wallet.init(isRestore: true); break; + case ZcashWallet(): + await wallet.init(isRestore: true); + break; + default: await wallet.init(); } @@ -1132,6 +1140,26 @@ abstract class SWB { .findAll()) .toSet(); final walletIdsToDelete = allWalletIds.difference(revertToState.walletIds); + // A Zcash wallet also has its scan state on disk, and the key to it in + // secure storage. + final added = await MainDB.instance.isar.walletInfo.getAllByWalletId( + walletIdsToDelete.toList(), + ); + for (final info in added.nonNulls.where((info) => info.coin is Zcash)) { + try { + await deleteZcashWalletPersistence( + applicationRoot: await StackFileSystem.applicationRootDirectory(), + walletId: info.walletId, + secureStorage: secureStorageInterface, + ); + } catch (e, s) { + Logging.instance.w( + "SWB revert: Zcash state of ${info.walletId}", + error: e, + stackTrace: s, + ); + } + } await MainDB.instance.isar.writeTxn(() async { await MainDB.instance.isar.walletInfo.deleteAllByWalletId( walletIdsToDelete.toList(), diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_send.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_send.dart index d464487ac..417f98d2b 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_send.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_send.dart @@ -1288,6 +1288,16 @@ class _DesktopSendState extends ConsumerState { : ref.read(pWalletBalanceSecondary(walletId)).spendable; break; } + } else if (coin is Zcash) { + // A Zcash send does not take its fee out of the amount, so the amount + // leaves room for it. + final spendable = ref.read(pWalletBalance(walletId)).spendable; + final fee = await ref + .read(pWallets) + .getWallet(walletId) + .estimateFeeFor(spendable, BigInt.zero); + if (!mounted) return; + amount = fee < spendable ? spendable - fee : spendable; } else { amount = ref.read(pWalletBalance(walletId)).spendable; } @@ -1324,7 +1334,7 @@ class _DesktopSendState extends ConsumerState { coin = ref.read(pWalletInfo(walletId)).coin; clipboard = widget.clipboard; - hasOptionalMemo = coin is Stellar || coin is Solana; + hasOptionalMemo = coin is Stellar || coin is Solana || coin is Zcash; usesSlatepacks = ref.read(pWallets).getWallet(walletId) is SlatepackInterface; diff --git a/lib/providers/wallet/send_fee_providers.dart b/lib/providers/wallet/send_fee_providers.dart index aa60861b2..ee5bf8980 100644 --- a/lib/providers/wallet/send_fee_providers.dart +++ b/lib/providers/wallet/send_fee_providers.dart @@ -193,7 +193,10 @@ bool _isFeeAmountIndependent(CryptoCurrency coin) => /// Stellar and Cardano give the same fee at every rate. bool canPickFeeRateType(CryptoCurrency coin, BalanceType balanceType) => - !_isSparkFee(coin, balanceType) && coin is! Stellar && coin is! Cardano; + !_isSparkFee(coin, balanceType) && + coin is! Stellar && + coin is! Cardano && + coin is! Zcash; bool canUseCustomFee(CryptoCurrency coin) => coin is ElectrumXCurrencyInterface || coin is Ethereum; diff --git a/lib/utilities/test_node_connection.dart b/lib/utilities/test_node_connection.dart index 811e74664..d4ae021bc 100644 --- a/lib/utilities/test_node_connection.dart +++ b/lib/utilities/test_node_connection.dart @@ -8,6 +8,7 @@ import 'package:on_chain/ada/ada.dart'; import 'package:socks5_proxy/socks.dart'; import '../app_config.dart'; +import '../models/node_model.dart'; import '../networking/http.dart'; import '../pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart'; import '../providers/global/prefs_provider.dart'; @@ -20,6 +21,7 @@ import '../wallets/crypto_currency/interfaces/electrumx_currency_interface.dart' import '../wallets/crypto_currency/intermediate/cryptonote_currency.dart'; import '../wallets/crypto_currency/intermediate/nano_currency.dart'; import '../wallets/wallet/impl/solana_wallet.dart'; +import '../wallets/wallet/intermediate/zcash_account_session.dart'; import '../wl_gen/interfaces/lib_xelis_interface.dart'; import '../widgets/tls_certificate_dialog.dart' as electrum_tls; import 'connection_check/electrum_connection_check.dart'; @@ -368,6 +370,26 @@ Future testNodeConnection({ testPassed = false; } break; + + case Zcash(): + testPassed = await ZcashAccountSession.answers( + NodeModel( + host: formData.host!, + port: formData.port!, + name: "", + id: "", + useSSL: formData.useSSL ?? false, + enabled: true, + coinName: cryptoCurrency.identifier, + isFailover: false, + isDown: false, + torEnabled: false, + clearnetEnabled: true, + isPrimary: false, + ), + cryptoCurrency.nativeNetwork, + ); + break; } return testPassed; diff --git a/lib/wallets/wallet/intermediate/zcash_account_session.dart b/lib/wallets/wallet/intermediate/zcash_account_session.dart index f796f4789..a8b8dec6a 100644 --- a/lib/wallets/wallet/intermediate/zcash_account_session.dart +++ b/lib/wallets/wallet/intermediate/zcash_account_session.dart @@ -340,6 +340,27 @@ class ZcashReadinessTimeoutException implements Exception { class ZcashAccountSession { static const int maximumBlockHeight = 0xffffffff; + /// Whether [node] is a lightwalletd of [network] that answers, for the + /// node form's connection test. + static Future answers( + NodeModel node, + zcash.ZcashNetwork network, { + ZcashLightwalletdClientFactory? clientFactory, + }) async { + final session = ZcashAccountSession( + network: network, + clientFactory: clientFactory, + ); + try { + await session.replaceNode(node); + return true; + } catch (_) { + return false; + } finally { + await session.close(); + } + } + ZcashAccountSession({ required this.network, ZcashLightwalletdClientFactory? clientFactory, diff --git a/scripts/app_config/configure_stack_wallet.sh b/scripts/app_config/configure_stack_wallet.sh index 3503b3a2e..a8453c057 100755 --- a/scripts/app_config/configure_stack_wallet.sh +++ b/scripts/app_config/configure_stack_wallet.sh @@ -122,6 +122,7 @@ final List _supportedCoins = List.unmodifiable([ Tezos(CryptoCurrencyNetwork.main), Wownero(CryptoCurrencyNetwork.main), Xelis(CryptoCurrencyNetwork.main), + Zcash(CryptoCurrencyNetwork.main), Bitcoin(CryptoCurrencyNetwork.test), Bitcoin(CryptoCurrencyNetwork.test4), Bitcoincash(CryptoCurrencyNetwork.test), diff --git a/scripts/ensure_test_app_config.sh b/scripts/ensure_test_app_config.sh index 696c853b4..0078cd06e 100755 --- a/scripts/ensure_test_app_config.sh +++ b/scripts/ensure_test_app_config.sh @@ -66,6 +66,7 @@ final List _supportedCoins = List.unmodifiable([ Tezos(CryptoCurrencyNetwork.main), Wownero(CryptoCurrencyNetwork.main), Xelis(CryptoCurrencyNetwork.main), + Zcash(CryptoCurrencyNetwork.main), Bitcoin(CryptoCurrencyNetwork.test), Bitcoin(CryptoCurrencyNetwork.test4), Bitcoincash(CryptoCurrencyNetwork.test), diff --git a/scripts/test_wallet_creation.dart b/scripts/test_wallet_creation.dart index 153279efe..2d58c2591 100644 --- a/scripts/test_wallet_creation.dart +++ b/scripts/test_wallet_creation.dart @@ -35,7 +35,9 @@ ArgParser argumentParser() => ArgParser() ..addFlag( 'all-coins', negatable: false, - help: 'Accumulate all 24 wallets in one installation.', + help: + 'Accumulate all ${defaultWordCounts.length} wallets in one ' + 'installation.', ) ..addFlag( 'disposable-mobile', diff --git a/scripts/tests/wallet_creation_runner_test.dart b/scripts/tests/wallet_creation_runner_test.dart index 664254582..bb4d8518f 100644 --- a/scripts/tests/wallet_creation_runner_test.dart +++ b/scripts/tests/wallet_creation_runner_test.dart @@ -69,8 +69,8 @@ void main() { .map((line) => jsonDecode(line) as Map) .toList(); - test('the shared matrix covers the 24 base wallets and excludes FROST', () { - expect(defaultWordCounts, hasLength(24)); + test('the shared matrix covers the 25 base wallets and excludes FROST', () { + expect(defaultWordCounts, hasLength(25)); expect( defaultWordCounts.keys, containsAll(['eCash', 'epicCash', 'monero', 'solana']), @@ -269,7 +269,7 @@ void main() { final suite = runner(omitCoin: 'xelis'); expect(await suite.run(options(['--device', 'linux', '--all-coins'])), 1); expect(suite.results.first['failure'], 'incomplete_results'); - expect(suite.results.first['completed_coins'], hasLength(23)); + expect(suite.results.first['completed_coins'], hasLength(24)); expect(await events(), hasLength(1)); }, ); diff --git a/test/pages/add_wallet_views/zcash_restore_options_test.dart b/test/pages/add_wallet_views/zcash_restore_options_test.dart new file mode 100644 index 000000000..ff9491cdd --- /dev/null +++ b/test/pages/add_wallet_views/zcash_restore_options_test.dart @@ -0,0 +1,135 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:platform/platform.dart' as platform; +import 'package:stackwallet/models/isar/stack_theme.dart'; +import 'package:stackwallet/pages/add_wallet_views/restore_wallet_view/restore_options_view/restore_options_view.dart'; +import 'package:stackwallet/pages/add_wallet_views/restore_wallet_view/restore_options_view/sub_widgets/restore_options_next_button.dart'; +import 'package:stackwallet/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart'; +import 'package:stackwallet/providers/ui/verify_recovery_phrase/mnemonic_word_count_state_provider.dart'; +import 'package:stackwallet/themes/stack_colors.dart'; +import 'package:stackwallet/themes/theme_service.dart'; +import 'package:stackwallet/utilities/util.dart'; +import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; +import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart'; +import 'package:tuple/tuple.dart'; + +import '../../sample_data/theme_json.dart'; + +void main() { + late platform.Platform originalPlatform; + + setUp(() => originalPlatform = Util.layoutPlatform); + tearDown(() => Util.layoutPlatform = originalPlatform); + + /// Pumps the restore options of [coin] and returns the arguments the + /// restore view is opened with, once it is. + Future> pump(WidgetTester tester, CryptoCurrency coin) async { + final theme = StackTheme.fromJson(json: lightThemeJsonMap); + final opened = []; + tester.view.physicalSize = const Size(1200, 2400); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + + await tester.pumpWidget( + ProviderScope( + overrides: [ + pThemeService.overrideWithValue(_TestThemeService(theme)), + // The screen before this one sets the phrase length. + mnemonicWordCountStateProvider.overrideWithValue( + StateController(coin.defaultSeedPhraseLength), + ), + ], + child: MaterialApp( + theme: ThemeData( + extensions: [StackColors.fromStackColorTheme(theme)], + ), + home: RestoreOptionsView(walletName: 'restored', coin: coin), + onGenerateRoute: (settings) { + expect(settings.name, RestoreWalletView.routeName); + opened.add(settings.arguments); + return MaterialPageRoute(builder: (_) => const SizedBox()); + }, + ), + ), + ); + await tester.pump(); + return opened; + } + + Finder toggle(String text) => find.byWidgetPredicate( + (widget) => widget is CustomTextButton && widget.text == text, + ); + + bool nextEnabled(WidgetTester tester) => + tester + .widget( + find.byType(RestoreOptionsNextButton), + ) + .onPressed != + null; + + Future enterHeight(WidgetTester tester, String height) async { + await tester.enterText( + find.widgetWithText(TextField, 'Start scanning from...'), + height, + ); + // The view reads the field in a frame callback. + await tester.pump(); + await tester.pump(); + } + + for (final os in ['android', 'linux']) { + testWidgets('$os: a Zcash restore asks for a block height and waits for ' + 'one', (tester) async { + Util.layoutPlatform = platform.FakePlatform(operatingSystem: os); + final opened = await pump(tester, Zcash(CryptoCurrencyNetwork.main)); + + // A date cannot be turned into a Zcash height here, and a restore from + // the wrong height would miss the wallet's funds. + expect(find.text('Block height'), findsOneWidget); + expect(toggle('Use date'), findsNothing); + expect(toggle('Use block height'), findsNothing); + expect(find.text('Choose start date'), findsNothing); + expect(nextEnabled(tester), isFalse); + + await enterHeight(tester, '0'); + expect(nextEnabled(tester), isFalse); + + await enterHeight(tester, '2500000'); + expect(nextEnabled(tester), isTrue); + + await tester.tap(find.byType(RestoreOptionsNextButton)); + await tester.pumpAndSettle(); + final arguments = opened.single! as Tuple5; + expect(arguments.item2, Zcash(CryptoCurrencyNetwork.main)); + expect(arguments.item4, 2500000); + }); + } + + testWidgets('other coins keep the choice between a date and a height', ( + tester, + ) async { + Util.layoutPlatform = platform.FakePlatform(operatingSystem: 'android'); + await pump(tester, Epiccash(CryptoCurrencyNetwork.main)); + + expect(find.text('Choose start date'), findsOneWidget); + expect(toggle('Use block height'), findsOneWidget); + // A restore by date needs no height. + expect(nextEnabled(tester), isTrue); + + await tester.tap(toggle('Use block height')); + await tester.pump(); + expect(find.text('Block height'), findsOneWidget); + expect(toggle('Use date'), findsOneWidget); + }); +} + +class _TestThemeService extends Fake implements ThemeService { + _TestThemeService(this.theme); + + final StackTheme theme; + + @override + StackTheme? getTheme({required String themeId}) => theme; +} diff --git a/test/utilities/native_library_check_test.dart b/test/utilities/native_library_check_test.dart index a2d6061b7..126d8201d 100644 --- a/test/utilities/native_library_check_test.dart +++ b/test/utilities/native_library_check_test.dart @@ -207,7 +207,9 @@ void main() { os: "windows", executable: _exe, open: (name) { - if (name != "salvium_libwallet2_api_c.dll") { + // Zcash's loads too, in an app that lists Zcash. + if (name != "salvium_libwallet2_api_c.dll" && + name != "zcash_ffi.dll") { throw ArgumentError(_notFound); } }, diff --git a/test/wallets/zcash_account_session_test.dart b/test/wallets/zcash_account_session_test.dart index 9acf2a5e0..377f9f481 100644 --- a/test/wallets/zcash_account_session_test.dart +++ b/test/wallets/zcash_account_session_test.dart @@ -226,6 +226,35 @@ void main() { }, ); + test('the node form\'s test passes for a node of the wallet\'s network ' + 'only', () async { + final clients = []; + Future answers(String chainName, {NodeModel? at}) => + ZcashAccountSession.answers( + at ?? node(), + zcash.ZcashNetwork.mainnet, + clientFactory: (_) { + clients.add( + FakeLightwalletdClient(chainName: chainName, tipHeight: 42), + ); + return clients.last; + }, + ); + + expect(await answers('main'), isTrue); + // A testnet node would have the wallet scan another chain. + expect(await answers('test'), isFalse); + // Every connection the test opens is closed again. + expect(clients.map((client) => client.closeCalls), [1, 1]); + + // An address that cannot be a node is a failed test, with no connection. + expect( + await answers('main', at: node(host: 'ftp://node.example')), + isFalse, + ); + expect(clients, hasLength(2)); + }); + test('rejects and closes an endpoint on the wrong Zcash network', () async { final client = FakeLightwalletdClient(chainName: 'test', tipHeight: 42); final session = ZcashAccountSession(