Skip to content

Commit 2038fbc

Browse files
committed
add working xpub settings view with click to copy to clipboard
1 parent d3f45f1 commit 2038fbc

File tree

3 files changed

+94
-18
lines changed

3 files changed

+94
-18
lines changed

lib/pages/settings_views/global_settings_view/xpub_view.dart

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:flutter/services.dart';
25
import 'package:flutter_riverpod/flutter_riverpod.dart';
6+
import 'package:qr_flutter/qr_flutter.dart';
7+
import 'package:stackwallet/notifications/show_flush_bar.dart';
8+
import 'package:stackwallet/utilities/assets.dart';
9+
import 'package:stackwallet/utilities/clipboard_interface.dart';
310
import 'package:stackwallet/utilities/text_styles.dart';
411
import 'package:stackwallet/utilities/theme/stack_colors.dart';
512
import 'package:stackwallet/widgets/background.dart';
613
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
14+
import 'package:stackwallet/widgets/rounded_white_container.dart';
715

816
class XPubView extends ConsumerStatefulWidget {
9-
const XPubView({Key? key}) : super(key: key);
17+
const XPubView({
18+
Key? key,
19+
this.xpub,
20+
this.clipboardInterface = const ClipboardWrapper(),
21+
}) : super(key: key);
22+
23+
final String? xpub;
24+
final ClipboardInterface clipboardInterface;
1025

1126
static const String routeName = "/xpub";
1227

@@ -15,8 +30,11 @@ class XPubView extends ConsumerStatefulWidget {
1530
}
1631

1732
class _XPubViewState extends ConsumerState<XPubView> {
33+
late ClipboardInterface _clipboardInterface;
34+
1835
@override
1936
void initState() {
37+
_clipboardInterface = widget.clipboardInterface;
2038
super.initState();
2139
}
2240

@@ -25,6 +43,16 @@ class _XPubViewState extends ConsumerState<XPubView> {
2543
super.dispose();
2644
}
2745

46+
Future<void> _copy() async {
47+
await _clipboardInterface.setData(ClipboardData(text: widget.xpub));
48+
unawaited(showFloatingFlushBar(
49+
type: FlushBarType.info,
50+
message: "Copied to clipboard",
51+
iconAsset: Assets.svg.copy,
52+
context: context,
53+
));
54+
}
55+
2856
@override
2957
Widget build(BuildContext context) {
3058
return Background(
@@ -47,7 +75,25 @@ class _XPubViewState extends ConsumerState<XPubView> {
4775
left: 16,
4876
right: 16,
4977
),
50-
child: Text("TODO"),
78+
child: Column(children: [
79+
if (widget.xpub != null)
80+
RoundedWhiteContainer(
81+
padding: const EdgeInsets.all(12),
82+
child: QrImage(data: widget.xpub!),
83+
onPressed: () => _copy(),
84+
),
85+
if (widget.xpub != null)
86+
const SizedBox(
87+
height: 8,
88+
),
89+
if (widget.xpub != null)
90+
RoundedWhiteContainer(
91+
padding: const EdgeInsets.all(12),
92+
child: Text(widget.xpub!,
93+
style: STextStyles.largeMedium14(context)),
94+
onPressed: () => _copy(),
95+
)
96+
]),
5197
),
5298
),
5399
);

lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:async';
22

3+
import 'package:bip32/bip32.dart' as bip32;
4+
import 'package:bip39/bip39.dart' as bip39;
35
import 'package:event_bus/event_bus.dart';
46
import 'package:flutter/material.dart';
57
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -58,6 +60,7 @@ class WalletSettingsView extends StatefulWidget {
5860
class _WalletSettingsViewState extends State<WalletSettingsView> {
5961
late final String walletId;
6062
late final Coin coin;
63+
late String xpub;
6164
late final bool xPubEnabled;
6265

6366
late final EventBus eventBus;
@@ -74,6 +77,7 @@ class _WalletSettingsViewState extends State<WalletSettingsView> {
7477
coin = widget.coin;
7578
xPubEnabled =
7679
coin != Coin.monero && coin != Coin.wownero && coin != Coin.epicCash;
80+
xpub = "";
7781

7882
_currentSyncStatus = widget.initialSyncStatus;
7983
// _currentNodeStatus = widget.initialNodeStatus;
@@ -274,6 +278,37 @@ class _WalletSettingsViewState extends State<WalletSettingsView> {
274278
SyncingPreferencesView.routeName);
275279
},
276280
),
281+
if (xPubEnabled)
282+
const SizedBox(
283+
height: 8,
284+
),
285+
if (xPubEnabled)
286+
Consumer(
287+
builder: (_, ref, __) {
288+
return SettingsListButton(
289+
iconAssetName: Assets.svg.eye,
290+
title: "Wallet xPub",
291+
onPressed: () async {
292+
final List<String> mnemonic = await ref
293+
.read(
294+
walletsChangeNotifierProvider)
295+
.getManager(widget.walletId)
296+
.mnemonic;
297+
298+
final seed = bip39.mnemonicToSeed(
299+
mnemonic.join(' '));
300+
final node =
301+
bip32.BIP32.fromSeed(seed);
302+
final xpub =
303+
node.neutered().toBase58();
304+
305+
Navigator.of(context).pushNamed(
306+
XPubView.routeName,
307+
arguments: xpub);
308+
},
309+
);
310+
},
311+
),
277312
const SizedBox(
278313
height: 8,
279314
),
@@ -285,19 +320,6 @@ class _WalletSettingsViewState extends State<WalletSettingsView> {
285320
.pushNamed(DebugView.routeName);
286321
},
287322
),
288-
if (xPubEnabled)
289-
const SizedBox(
290-
height: 8,
291-
),
292-
if (xPubEnabled)
293-
SettingsListButton(
294-
iconAssetName: Assets.svg.eye,
295-
title: "Wallet xPub",
296-
onPressed: () {
297-
Navigator.of(context)
298-
.pushNamed(XPubView.routeName);
299-
},
300-
),
301323
],
302324
),
303325
),

lib/route_generator.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,18 @@ class RouteGenerator {
399399
settings: RouteSettings(name: settings.name));
400400

401401
case XPubView.routeName:
402-
return getRoute(
402+
if (args is String) {
403+
return getRoute(
403404
shouldUseMaterialRoute: useMaterialPageRoute,
404-
builder: (_) => const XPubView(),
405-
settings: RouteSettings(name: settings.name));
405+
builder: (_) => XPubView(
406+
xpub: args,
407+
),
408+
settings: RouteSettings(
409+
name: settings.name,
410+
),
411+
);
412+
}
413+
return _routeError("${settings.name} invalid args: ${args.toString()}");
406414

407415
case AppearanceSettingsView.routeName:
408416
return getRoute(

0 commit comments

Comments
 (0)