Skip to content

Commit

Permalink
Merge pull request #1646 from bitmark-inc/load_mnemonics
Browse files Browse the repository at this point in the history
fix: Load mnemonics in memory on the recovery page instead of wallet page
  • Loading branch information
phuocbitmark authored Apr 24, 2024
2 parents 83931f6 + 3b03017 commit 044b252
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 132 deletions.
253 changes: 134 additions & 119 deletions lib/screen/account/recovery_phrase_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:feralfile_app_theme/feral_file_app_theme.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:libauk_dart/libauk_dart.dart';
import 'package:open_settings/open_settings.dart';
import 'package:permission_handler/permission_handler.dart';

class RecoveryPhrasePayload {
final List<String> words;
final String passphrase;
final WalletStorage wallet;

RecoveryPhrasePayload({required this.words, required this.passphrase});
RecoveryPhrasePayload({required this.wallet});
}

class RecoveryPhrasePage extends StatefulWidget {
Expand All @@ -44,6 +44,8 @@ class RecoveryPhrasePage extends StatefulWidget {

class _RecoveryPhrasePageState extends State<RecoveryPhrasePage> {
bool _isShow = false;
List<String>? _words;
String _passphrase = '';
bool? _isBackUpAvailable;

@override
Expand All @@ -52,6 +54,7 @@ class _RecoveryPhrasePageState extends State<RecoveryPhrasePage> {
SecureScreenChannel.setSecureFlag(true);
WidgetsBinding.instance.addPostFrameCallback((context) {
unawaited(_checkBackUpAvailable());
unawaited(_loadRecoveryPhrase());
});
}

Expand All @@ -74,133 +77,144 @@ class _RecoveryPhrasePageState extends State<RecoveryPhrasePage> {
setState(() {});
}

Future<void> _loadRecoveryPhrase() async {
final words = await widget.payload.wallet.exportMnemonicWords();
final passphrase = await widget.payload.wallet.exportMnemonicPassphrase();
setState(() {
_words = words.split(' ');
_passphrase = passphrase;
});
}

@override
Widget build(BuildContext context) {
final roundNumber =
widget.payload.words.length ~/ 2 + widget.payload.words.length % 2;
final theme = Theme.of(context);
Widget build(BuildContext context) => Scaffold(
appBar: getBackAppBar(
context,
title: 'your_recovery_phrase'.tr(),
onBack: () {
Navigator.of(context).pop();
},
),
body: _body(context, _words, _passphrase),
);

return Scaffold(
appBar: getBackAppBar(
context,
title: 'your_recovery_phrase'.tr(),
onBack: () {
Navigator.of(context).pop();
},
),
body: Container(
margin: ResponsiveLayout.pageHorizontalEdgeInsetsWithSubmitButton,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
addTitleSpace(),
_getBackUpState(context),
Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Table(
children: List.generate(
roundNumber,
(index) =>
_tableRow(context, index, roundNumber),
),
border: TableBorder.all(
color: AppColor.auLightGrey,
borderRadius: BorderRadius.circular(10)),
Widget _body(BuildContext context, List<String>? words, String passphrase) {
if (words == null) {
return const SizedBox();
}
final theme = Theme.of(context);
final roundNumber = words.length ~/ 2 + words.length % 2;
return Container(
margin: ResponsiveLayout.pageHorizontalEdgeInsetsWithSubmitButton,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
addTitleSpace(),
_getBackUpState(context),
Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Table(
children: List.generate(
roundNumber,
(index) =>
_tableRow(context, index, roundNumber, words),
),
if (widget.payload.passphrase.isNotEmpty) ...[
const SizedBox(height: 20),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
border:
Border.all(color: AppColor.auLightGrey),
borderRadius: BorderRadius.circular(5),
),
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: 'passphrase'.tr(),
style: theme
.textTheme.ppMori400FFQuickSilver14,
),
const TextSpan(text: ' '),
TextSpan(
text: widget.payload.passphrase,
style:
theme.textTheme.ppMori400Black14),
],
),
border: TableBorder.all(
color: AppColor.auLightGrey,
borderRadius: BorderRadius.circular(10)),
),
if (_passphrase.isNotEmpty) ...[
const SizedBox(height: 20),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: AppColor.auLightGrey),
borderRadius: BorderRadius.circular(5),
),
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: 'passphrase'.tr(),
style: theme
.textTheme.ppMori400FFQuickSilver14,
),
const TextSpan(text: ' '),
TextSpan(
text: passphrase,
style:
theme.textTheme.ppMori400Black14),
],
),
),
]
],
),
if (!_isShow)
Positioned.fill(
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints.tightFor(
width: 168, height: 45),
child: PrimaryButton(
text: 'tap_to_reveal'.tr(),
onTap: () async {
final didAuthenticate =
await LocalAuthenticationService
.checkLocalAuth();
),
]
],
),
if (!_isShow)
Positioned.fill(
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints.tightFor(
width: 168, height: 45),
child: PrimaryButton(
text: 'tap_to_reveal'.tr(),
onTap: () async {
final didAuthenticate =
await LocalAuthenticationService
.checkLocalAuth();

if (!didAuthenticate) {
return;
}
setState(() {
_isShow = !_isShow;
});
Future.delayed(
const Duration(seconds: 60),
() {
if (mounted) {
setState(() {
_isShow = !_isShow;
});
}
},
);
},
),
)),
if (!didAuthenticate) {
return;
}
setState(() {
_isShow = !_isShow;
});
Future.delayed(
const Duration(seconds: 60),
() {
if (mounted) {
setState(() {
_isShow = !_isShow;
});
}
},
);
},
),
)),
),
],
),
_recommend(context),
const SizedBox(height: 30),
],
),
)),
),
],
),
_recommend(context),
const SizedBox(height: 30),
],
),
),
_settingButton(context)
],
),
),
_settingButton(context)
],
),
);
}

Widget _rowItem(BuildContext context, int index) {
Widget _rowItem(BuildContext context, int index, List<String> words) {
final theme = Theme.of(context);
final isNull = index >= widget.payload.words.length;
final word = isNull ? '' : widget.payload.words[index];
final isNull = index >= words.length;
final word = isNull ? '' : words[index];
NumberFormat formatter = NumberFormat('00');

return Padding(
Expand Down Expand Up @@ -381,9 +395,10 @@ class _RecoveryPhrasePageState extends State<RecoveryPhrasePage> {
);
}

TableRow _tableRow(BuildContext context, int index, int itemsEachCol) =>
TableRow _tableRow(BuildContext context, int index, int itemsEachCol,
List<String> words) =>
TableRow(children: [
_rowItem(context, index),
_rowItem(context, index + itemsEachCol),
_rowItem(context, index, words),
_rowItem(context, index + itemsEachCol, words),
]);
}
17 changes: 4 additions & 13 deletions lib/screen/settings/crypto/wallet_detail/wallet_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -592,19 +592,10 @@ class _WalletDetailPageState extends State<WalletDetailPage> with RouteAware {
style: theme.textTheme.ppMori400Black14,
),
onTap: () async {
final words =
await widget.payload.persona.wallet().exportMnemonicWords();
final passphrase =
await widget.payload.persona.wallet().exportMnemonicPassphrase();
if (!context.mounted) {
return;
}
unawaited(
Navigator.of(context).pushNamed(
AppRouter.recoveryPhrasePage,
arguments: RecoveryPhrasePayload(
words: words.split(' '), passphrase: passphrase),
),
await Navigator.of(context).pushNamed(
AppRouter.recoveryPhrasePage,
arguments:
RecoveryPhrasePayload(wallet: widget.payload.persona.wallet()),
);
},
),
Expand Down

0 comments on commit 044b252

Please sign in to comment.