Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/pages/wallet_view/wallet_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import '../../wallets/wallet/impl/bitcoin_frost_wallet.dart';
import '../../wallets/wallet/impl/firo_wallet.dart';
import '../../wallets/wallet/impl/namecoin_wallet.dart';
import '../../wallets/wallet/impl/salvium_wallet.dart';
import '../../wallets/wallet/impl/zcash_wallet.dart';
import '../../wallets/wallet/intermediate/cryptonote_wallet.dart';
import '../../wallets/wallet/intermediate/lib_salvium_wallet.dart';
import '../../wallets/wallet/wallet_mixin_interfaces/slatepack_interface.dart';
Expand Down Expand Up @@ -109,6 +110,7 @@ import '../settings_views/wallet_settings_view/wallet_settings_view.dart';
import '../signing/signing_view.dart';
import '../spark_names/spark_names_home_view.dart';
import '../token_view/my_tokens_view.dart';
import '../zcash_migration/zcash_migration_view.dart';
import 'sub_widgets/transactions_list.dart';
import 'sub_widgets/wallet_summary.dart';
import 'transaction_views/all_transactions_view.dart';
Expand Down Expand Up @@ -1327,6 +1329,27 @@ class _WalletViewState extends ConsumerState<WalletView> {
);
},
),
if (wallet is ZcashWallet)
WalletNavigationBarItemData(
label: "Migrate",
icon: SvgPicture.asset(
Assets.svg.recycle,
height: 20,
width: 20,
colorFilter: ColorFilter.mode(
Theme.of(context)
.extension<StackColors>()!
.bottomNavIconIcon,
BlendMode.srcIn,
),
),
onTap: () {
Navigator.of(context).pushNamed(
ZcashMigrationView.routeName,
arguments: walletId,
);
},
),
],
),
),
Expand Down
273 changes: 273 additions & 0 deletions lib/pages/zcash_migration/zcash_migration_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../pages_desktop_specific/desktop_home_view.dart';
import '../../providers/providers.dart';
import '../../route_generator.dart';
import '../../themes/stack_colors.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/amount/amount_formatter.dart';
import '../../utilities/show_loading.dart';
import '../../utilities/text_styles.dart';
import '../../utilities/util.dart';
import '../../wallets/models/tx_data.dart';
import '../../wallets/wallet/impl/zcash_wallet.dart';
import '../../wallets/wallet/intermediate/zcash_send.dart';
import '../../widgets/background.dart';
import '../../widgets/conditional_parent.dart';
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
import '../../widgets/desktop/desktop_app_bar.dart';
import '../../widgets/desktop/desktop_dialog.dart';
import '../../widgets/desktop/desktop_scaffold.dart';
import '../../widgets/desktop/primary_button.dart';
import '../../widgets/loading_indicator.dart';
import '../../widgets/rounded_white_container.dart';
import '../../widgets/stack_dialog.dart';
import '../send_view/confirm_transaction_view.dart';

/// Moves what a Zcash wallet holds in the Sapling pool, and its transparent
/// coins, to Ironwood, the pool a send spends from.
class ZcashMigrationView extends ConsumerStatefulWidget {
const ZcashMigrationView({super.key, required this.walletId});

static const routeName = '/zcashMigration';

final String walletId;

@override
ConsumerState<ZcashMigrationView> createState() => _ZcashMigrationViewState();
}

class _ZcashMigrationViewState extends ConsumerState<ZcashMigrationView> {
late Future<Map<ZcashMovablePool, Amount>> _funds;
bool _preparing = false;

ZcashWallet get _wallet =>
ref.read(pWallets).getWallet(widget.walletId) as ZcashWallet;

@override
void initState() {
super.initState();
_funds = _wallet.movableFunds();
}

Future<void> _move(ZcashMovablePool pool) async {
if (_preparing) return;
_preparing = true;
try {
Exception? failure;
final prepared = await showLoading<TxData>(
whileFuture: _wallet.prepareMoveToIronwood(pool),
context: context,
message: 'Preparing transaction',
rootNavigator: Util.isDesktop,
onException: (e) => failure = e,
);
if (!mounted) return;
if (prepared == null) {
await showDialog<void>(
context: context,
builder: (_) => StackOkDialog(
title: 'The funds were not moved',
message: switch (failure) {
final ZcashMoveException e => e.message,
final ZcashSaplingParametersUnavailableException e =>
e.toString(),
_ =>
'The transaction could not be prepared. Check that the '
'wallet is synced, then try again.\n\n$failure',
},
desktopPopRootNavigator: Util.isDesktop,
),
);
return;
}
// The mobile confirmation shows the note, which a send's form sets.
final txData = prepared.copyWith(note: '');
void reload() {
if (mounted) setState(() => _funds = _wallet.movableFunds());
}

if (Util.isDesktop) {
await showDialog<void>(
context: context,
builder: (context) => DesktopDialog(
maxHeight: MediaQuery.of(context).size.height - 64,
maxWidth: 580,
child: ConfirmTransactionView(
txData: txData,
walletId: widget.walletId,
onSuccess: reload,
routeOnSuccessName: DesktopHomeView.routeName,
),
),
);
} else {
await Navigator.of(context).push(
RouteGenerator.getRoute<void>(
shouldUseMaterialRoute: RouteGenerator.useMaterialPageRoute,
builder: (_) => ConfirmTransactionView(
txData: txData,
walletId: widget.walletId,
onSuccess: reload,
),
settings: const RouteSettings(
name: ConfirmTransactionView.routeName,
),
),
);
}
} finally {
_preparing = false;
}
}

@override
Widget build(BuildContext context) {
final isDesktop = Util.isDesktop;
final formatter = ref.watch(pAmountFormatter(_wallet.cryptoCurrency));
return ConditionalParent(
condition: !isDesktop,
builder: (child) => Background(child: child),
child: MasterScaffold(
isDesktop: isDesktop,
appBar: isDesktop
? DesktopAppBar(
isCompactHeight: true,
background: Theme.of(context).extension<StackColors>()!.popupBG,
leading: Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 24, right: 20),
child: AppBarBackButton(
size: 32,
onPressed: Navigator.of(context).pop,
),
),
Text(
'Migrate funds',
style: STextStyles.desktopH3(context),
),
],
),
)
: AppBar(
leading: AppBarBackButton(onPressed: Navigator.of(context).pop),
titleSpacing: 0,
title: Text(
'Migrate funds',
style: STextStyles.navBarTitle(context),
),
),
body: ConditionalParent(
condition: !isDesktop,
builder: (child) => SafeArea(
child: Padding(padding: const EdgeInsets.all(16), child: child),
),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 640),
child: FutureBuilder<Map<ZcashMovablePool, Amount>>(
future: _funds,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(
'The funds of this wallet could not be read.',
textAlign: TextAlign.center,
style: STextStyles.smallMed14(context),
),
);
}
final funds = snapshot.data;
if (funds == null) return const LoadingIndicator();
return ZcashMigrationPanel(
funds: funds,
format: formatter.format,
onMove: (pool) => unawaited(_move(pool)),
);
},
),
),
),
),
),
);
}
}

/// The pools of [funds], each with what it holds and a button that moves it.
class ZcashMigrationPanel extends StatelessWidget {
const ZcashMigrationPanel({
super.key,
required this.funds,
required this.format,
required this.onMove,
});

final Map<ZcashMovablePool, Amount> funds;
final String Function(Amount amount) format;
final ValueChanged<ZcashMovablePool> onMove;

static String _title(ZcashMovablePool pool) => switch (pool) {
ZcashMovablePool.sapling => 'Sapling funds',
ZcashMovablePool.transparent => 'Transparent funds',
};

static String _detail(ZcashMovablePool pool) => switch (pool) {
ZcashMovablePool.sapling =>
'Funds received on a Sapling address of this wallet. Moving them '
'needs the Sapling parameters, which are downloaded first if they '
'are not on this device.',
ZcashMovablePool.transparent =>
'Funds received on a transparent address of this wallet. The '
'transaction that moves them shows their amount and where they '
'came from on the chain.',
};

@override
Widget build(BuildContext context) => ListView(
padding: Util.isDesktop ? const EdgeInsets.all(24) : EdgeInsets.zero,
children: [
Text(
'A Zcash send spends the funds of the Ironwood pool. Funds this '
'wallet holds in another pool count towards its balance and can be '
'moved to Ironwood here, in a transaction to the wallet itself that '
'you confirm like a send. A transaction moves up to 50 notes or '
'coins.',
style: STextStyles.smallMed14(context),
),
const SizedBox(height: 16),
for (final pool in ZcashMovablePool.values)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: RoundedWhiteContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(_title(pool), style: STextStyles.w600_14(context)),
const SizedBox(height: 4),
Text(_detail(pool), style: STextStyles.smallMed12(context)),
const SizedBox(height: 12),
Text(
format(funds[pool]!),
key: Key('zcash_movable_${pool.name}'),
style: STextStyles.w500_14(context),
),
const SizedBox(height: 12),
PrimaryButton(
key: Key('zcash_move_${pool.name}'),
buttonHeight: ButtonHeight.l,
label: 'Move to Ironwood',
enabled: funds[pool]!.raw > BigInt.zero,
onPressed: () => onMove(pool),
),
],
),
),
),
],
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import '../../../../pages/paynym/paynym_home_view.dart';
import '../../../../pages/salvium_stake/salvium_create_stake_view.dart';
import '../../../../pages/signing/signing_view.dart';
import '../../../../pages/spark_names/spark_names_home_view.dart';
import '../../../../pages/zcash_migration/zcash_migration_view.dart';
import '../../../../providers/desktop/current_desktop_menu_item.dart';
import '../../../../providers/global/paynym_api_provider.dart';
import '../../../../providers/providers.dart';
Expand All @@ -45,6 +46,7 @@ import '../../../../wallets/wallet/impl/bitcoin_wallet.dart';
import '../../../../wallets/wallet/impl/firo_wallet.dart';
import '../../../../wallets/wallet/impl/namecoin_wallet.dart';
import '../../../../wallets/wallet/impl/salvium_wallet.dart';
import '../../../../wallets/wallet/impl/zcash_wallet.dart';
import '../../../../wallets/wallet/intermediate/cryptonote_wallet.dart';
import '../../../../wallets/wallet/intermediate/lib_salvium_wallet.dart';
import '../../../../wallets/wallet/wallet.dart' show Wallet;
Expand Down Expand Up @@ -95,6 +97,7 @@ enum WalletFeature {
salviumStaking("Staking", "Staking"),
sign("Sign/Verify", "Sign / Verify messages"),
masternodes("Masternodes", "Manage masternodes"),
zcashMigration("Migrate", "Move Sapling and transparent funds to Ironwood"),

// special cases
clearSparkCache("", ""),
Expand Down Expand Up @@ -369,6 +372,11 @@ class _DesktopWalletFeaturesState extends ConsumerState<DesktopWalletFeatures> {
.pushNamed(DesktopChurningView.routeName, arguments: widget.walletId);
}

void _onZcashMigrationPressed() {
Navigator.of(context)
.pushNamed(ZcashMigrationView.routeName, arguments: widget.walletId);
}

void _onNamesPressed() {
Navigator.of(context)
.pushNamed(NamecoinNamesHomeView.routeName, arguments: widget.walletId);
Expand Down Expand Up @@ -535,6 +543,13 @@ class _DesktopWalletFeaturesState extends ConsumerState<DesktopWalletFeatures> {
if (!isViewOnly && (wallet is CryptonoteWallet))
(WalletFeature.churn, Assets.svg.churn, _onChurnPressed),

if (wallet is ZcashWallet)
(
WalletFeature.zcashMigration,
Assets.svg.recycle,
_onZcashMigrationPressed,
),

if (wallet is NamecoinWallet)
(WalletFeature.namecoinName, Assets.svg.robotHead, _onNamesPressed),
];
Expand Down
11 changes: 11 additions & 0 deletions lib/route_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ import 'pages/wallet_view/transaction_views/tx_v2/transaction_v2_details_view.da
import 'pages/wallet_view/wallet_view.dart';
import 'pages/wallets_view/wallets_overview.dart';
import 'pages/wallets_view/wallets_view.dart';
import 'pages/zcash_migration/zcash_migration_view.dart';
import 'pages_desktop_specific/address_book_view/desktop_address_book.dart';
import 'pages_desktop_specific/addresses/desktop_wallet_addresses_view.dart';
import 'pages_desktop_specific/cashfusion/desktop_cashfusion_view.dart';
Expand Down Expand Up @@ -1037,6 +1038,16 @@ class RouteGenerator {
}
return _routeError("${settings.name} invalid args: ${args.toString()}");

case ZcashMigrationView.routeName:
if (args is String) {
return getRoute(
shouldUseMaterialRoute: useMaterialPageRoute,
builder: (_) => ZcashMigrationView(walletId: args),
settings: RouteSettings(name: settings.name),
);
}
return _routeError("${settings.name} invalid args: ${args.toString()}");

case ChurningView.routeName:
if (args is String) {
return getRoute(
Expand Down
Loading