-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathimport_coldcard_page.dart
More file actions
157 lines (147 loc) · 5.57 KB
/
Copy pathimport_coldcard_page.dart
File metadata and controls
157 lines (147 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import 'package:bb_mobile/core/entities/signer_device_entity.dart';
import 'package:bb_mobile/core/themes/app_theme.dart';
import 'package:bb_mobile/core/utils/build_context_x.dart';
import 'package:bb_mobile/core/utils/constants.dart';
import 'package:bb_mobile/core/utils/logger.dart';
import 'package:bb_mobile/core/widgets/buttons/button.dart';
import 'package:bb_mobile/core/widgets/nfc_bottom_sheet.dart';
import 'package:bb_mobile/core/widgets/snackbar_utils.dart';
import 'package:bb_mobile/core/widgets/text/text.dart';
import 'package:bb_mobile/features/import_coldcard/instructions_bottom_sheet.dart';
import 'package:bb_mobile/features/import_watch_only_wallet/import_watch_only_router.dart';
import 'package:bb_mobile/features/import_watch_only_wallet/watch_only_wallet_entity.dart';
import 'package:bb_mobile/generated/flutter_gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:url_launcher/url_launcher.dart';
class ImportColdcardPage extends StatelessWidget {
static const _supportedDevices = {
SignerDeviceEntity.coldcardQ,
SignerDeviceEntity.coldcardMk4,
};
final SignerDeviceEntity signerDevice;
factory ImportColdcardPage({
Key? key,
required SignerDeviceEntity signerDevice,
}) {
if (!_supportedDevices.contains(signerDevice)) {
throw ArgumentError.value(
signerDevice,
'signerDevice',
'Only Coldcard Q and Coldcard MK4/MK5 are supported',
);
}
return ImportColdcardPage._(key: key, signerDevice: signerDevice);
}
const ImportColdcardPage._({super.key, required this.signerDevice});
Future<void> _handleNfcData(BuildContext context, String payload) async {
try {
final watchOnlyWallet = await WatchOnlyWalletEntity.parse(
payload,
signerDevice: signerDevice,
);
if (!context.mounted) return;
context.replaceNamed(
ImportWatchOnlyWalletRoutes.import.name,
extra: watchOnlyWallet,
);
} catch (e) {
log.warning(
'Failed to parse Coldcard wallet data received via NFC',
error: e,
);
if (!context.mounted) return;
SnackBarUtils.showSnackBar(
context,
context.loc.importColdcardInvalidWalletData,
);
}
}
@override
Widget build(BuildContext context) {
final deviceName = signerDevice.displayName;
final illustrationPath = Assets.misc.qRPlaceholder.path;
return Scaffold(
appBar: AppBar(
title: Text(context.loc.importColdcardConnectTitle(deviceName)),
),
body: Padding(
padding: EdgeInsets.symmetric(horizontal: Device.screen.width * 0.05),
child: Column(
crossAxisAlignment: .stretch,
children: [
BBText(
context.loc.importColdcardConnectDescription(deviceName),
style: context.font.bodyLarge,
textAlign: .center,
maxLines: 2,
),
Gap(Device.screen.height * 0.05),
Image.asset(
illustrationPath, // TODO: Mk4 needs a different illustration.
height: 200,
width: 200,
),
Gap(Device.screen.height * 0.05),
Column(
children: [
if (signerDevice == SignerDeviceEntity.coldcardQ) ...[
BBButton.small(
label: context.loc.importColdcardButtonOpenCamera,
onPressed: () => context.pushNamed(
ImportWatchOnlyWalletRoutes.scan.name,
extra: signerDevice,
),
bgColor: context.appColors.surface,
textColor: context.appColors.text,
outlined: true,
),
Gap(Device.screen.height * 0.02),
],
BBButton.small(
label: context.loc.scanNfcButton,
onPressed: () => NfcBottomSheet.showReadNfc(
context: context,
title: context.loc.importColdcardScanNfcSheetTitle(
deviceName,
),
onDataReceived: (payload) =>
_handleNfcData(context, payload),
),
bgColor: context.appColors.surface,
textColor: context.appColors.text,
outlined: true,
),
Gap(Device.screen.height * 0.02),
BBButton.small(
label: context.loc.importColdcardButtonInstructions,
onPressed: () {
if (signerDevice == SignerDeviceEntity.coldcardQ) {
ColdcardQInstructionsBottomSheet.show(context);
} else {
ColdcardMk4InstructionsBottomSheet.show(context);
}
},
bgColor: context.appColors.surface,
textColor: context.appColors.text,
outlined: true,
),
Gap(Device.screen.height * 0.02),
BBButton.small(
label: context.loc.importColdcardButtonPurchase,
onPressed: () => launchUrl(
Uri.parse('https://store.coinkite.com/promo/BULLBITCOIN'),
),
bgColor: context.appColors.surface,
textColor: context.appColors.text,
outlined: true,
),
],
),
],
),
),
);
}
}