2023-05-26 21:21:16 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Stack Wallet.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-05-26 15:45:45 +00:00
|
|
|
import 'dart:async';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:stackwallet/notifications/show_flush_bar.dart';
|
|
|
|
import 'package:stackwallet/pages/home_view/home_view.dart';
|
|
|
|
import 'package:stackwallet/providers/global/prefs_provider.dart';
|
2022-11-09 22:43:26 +00:00
|
|
|
import 'package:stackwallet/providers/global/secure_store_provider.dart';
|
2023-05-09 21:57:40 +00:00
|
|
|
import 'package:stackwallet/themes/stack_colors.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/assets.dart';
|
|
|
|
import 'package:stackwallet/utilities/biometrics.dart';
|
|
|
|
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
|
|
|
import 'package:stackwallet/utilities/text_styles.dart';
|
2022-11-25 19:24:01 +00:00
|
|
|
import 'package:stackwallet/widgets/background.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
|
|
|
|
import 'package:stackwallet/widgets/custom_pin_put/custom_pin_put.dart';
|
|
|
|
|
|
|
|
class CreatePinView extends ConsumerStatefulWidget {
|
|
|
|
const CreatePinView({
|
|
|
|
Key? key,
|
|
|
|
this.popOnSuccess = false,
|
|
|
|
this.biometrics = const Biometrics(),
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
static const String routeName = "/createPin";
|
|
|
|
|
|
|
|
final Biometrics biometrics;
|
|
|
|
final bool popOnSuccess;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<CreatePinView> createState() => _CreatePinViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CreatePinViewState extends ConsumerState<CreatePinView> {
|
|
|
|
BoxDecoration get _pinPutDecoration {
|
|
|
|
return BoxDecoration(
|
2023-05-26 15:45:45 +00:00
|
|
|
color: Theme.of(context).extension<StackColors>()!.infoItemIcons,
|
2022-09-22 23:48:50 +00:00
|
|
|
border: Border.all(
|
2023-05-26 15:45:45 +00:00
|
|
|
width: 1,
|
|
|
|
color: Theme.of(context).extension<StackColors>()!.infoItemIcons,
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
borderRadius: BorderRadius.circular(6),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final PageController _pageController =
|
|
|
|
PageController(initialPage: 0, keepPage: true);
|
|
|
|
|
|
|
|
// Attributes for Page 1 of the pageview
|
|
|
|
final TextEditingController _pinPutController1 = TextEditingController();
|
|
|
|
final FocusNode _pinPutFocusNode1 = FocusNode();
|
|
|
|
|
|
|
|
// Attributes for Page 2 of the pageview
|
|
|
|
final TextEditingController _pinPutController2 = TextEditingController();
|
|
|
|
final FocusNode _pinPutFocusNode2 = FocusNode();
|
|
|
|
|
2022-11-09 23:48:43 +00:00
|
|
|
late SecureStorageInterface _secureStore;
|
2022-08-26 08:11:35 +00:00
|
|
|
late Biometrics biometrics;
|
|
|
|
|
2023-05-26 15:45:45 +00:00
|
|
|
int pinCount = 1;
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
@override
|
|
|
|
initState() {
|
2022-11-09 22:43:26 +00:00
|
|
|
_secureStore = ref.read(secureStoreProvider);
|
2022-08-26 08:11:35 +00:00
|
|
|
biometrics = widget.biometrics;
|
2023-05-26 15:45:45 +00:00
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_pageController.dispose();
|
|
|
|
_pinPutController1.dispose();
|
|
|
|
_pinPutController2.dispose();
|
|
|
|
_pinPutFocusNode1.dispose();
|
|
|
|
_pinPutFocusNode2.dispose();
|
2023-05-26 15:45:45 +00:00
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-26 15:45:45 +00:00
|
|
|
// int pinCount = 1;
|
2022-11-25 19:24:01 +00:00
|
|
|
return Background(
|
|
|
|
child: Scaffold(
|
|
|
|
backgroundColor: Theme.of(context).extension<StackColors>()!.background,
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: AppBarBackButton(
|
|
|
|
onPressed: () async {
|
|
|
|
if (FocusScope.of(context).hasFocus) {
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
await Future<void>.delayed(const Duration(milliseconds: 70));
|
|
|
|
}
|
|
|
|
if (mounted) {
|
|
|
|
Navigator.of(context).pop(widget.popOnSuccess);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
body: SafeArea(
|
|
|
|
child: PageView(
|
|
|
|
controller: _pageController,
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
children: [
|
|
|
|
// page 1
|
|
|
|
Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
"Create a PIN",
|
|
|
|
style: STextStyles.pageTitleH1(context),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 8,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"This PIN protects access to your wallet.",
|
|
|
|
style: STextStyles.subtitle(context),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
const SizedBox(
|
|
|
|
height: 36,
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
CustomPinPut(
|
2023-05-26 15:45:45 +00:00
|
|
|
fieldsCount: pinCount,
|
2022-11-25 19:24:01 +00:00
|
|
|
eachFieldHeight: 12,
|
|
|
|
eachFieldWidth: 12,
|
|
|
|
textStyle: STextStyles.label(context).copyWith(
|
|
|
|
fontSize: 1,
|
|
|
|
),
|
|
|
|
focusNode: _pinPutFocusNode1,
|
|
|
|
controller: _pinPutController1,
|
|
|
|
useNativeKeyboard: false,
|
|
|
|
obscureText: "",
|
|
|
|
inputDecoration: InputDecoration(
|
|
|
|
border: InputBorder.none,
|
|
|
|
enabledBorder: InputBorder.none,
|
|
|
|
focusedBorder: InputBorder.none,
|
|
|
|
disabledBorder: InputBorder.none,
|
|
|
|
errorBorder: InputBorder.none,
|
|
|
|
focusedErrorBorder: InputBorder.none,
|
|
|
|
fillColor: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.background,
|
|
|
|
counterText: "",
|
|
|
|
),
|
2023-05-01 22:41:13 +00:00
|
|
|
isRandom:
|
|
|
|
ref.read(prefsChangeNotifierProvider).randomizePIN,
|
2023-05-26 15:45:45 +00:00
|
|
|
submittedFieldDecoration: _pinPutDecoration,
|
2022-11-25 19:24:01 +00:00
|
|
|
selectedFieldDecoration: _pinPutDecoration,
|
|
|
|
followingFieldDecoration: _pinPutDecoration,
|
2023-05-26 15:45:45 +00:00
|
|
|
onPinLengthChanged: (newLength) {
|
|
|
|
setState(() {
|
|
|
|
pinCount = newLength;
|
|
|
|
});
|
|
|
|
},
|
2022-11-25 19:24:01 +00:00
|
|
|
onSubmit: (String pin) {
|
2023-05-26 15:45:45 +00:00
|
|
|
if (pin.length < 4) {
|
|
|
|
showFloatingFlushBar(
|
|
|
|
type: FlushBarType.warning,
|
|
|
|
message: "PIN not long enough!",
|
|
|
|
iconAsset: Assets.svg.alertCircle,
|
|
|
|
context: context,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-25 19:24:01 +00:00
|
|
|
_pageController.nextPage(
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
curve: Curves.linear,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
],
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
// page 2
|
|
|
|
Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
"Confirm PIN",
|
|
|
|
style: STextStyles.pageTitleH1(context),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 8,
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
Text(
|
|
|
|
"This PIN protects access to your wallet.",
|
|
|
|
style: STextStyles.subtitle(context),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
const SizedBox(
|
|
|
|
height: 36,
|
|
|
|
),
|
|
|
|
CustomPinPut(
|
2023-05-26 15:45:45 +00:00
|
|
|
fieldsCount: pinCount,
|
2022-11-25 19:24:01 +00:00
|
|
|
eachFieldHeight: 12,
|
|
|
|
eachFieldWidth: 12,
|
|
|
|
textStyle: STextStyles.infoSmall(context).copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.textSubtitle3,
|
|
|
|
fontSize: 1,
|
|
|
|
),
|
|
|
|
focusNode: _pinPutFocusNode2,
|
|
|
|
controller: _pinPutController2,
|
|
|
|
useNativeKeyboard: false,
|
|
|
|
obscureText: "",
|
|
|
|
inputDecoration: InputDecoration(
|
|
|
|
border: InputBorder.none,
|
|
|
|
enabledBorder: InputBorder.none,
|
|
|
|
focusedBorder: InputBorder.none,
|
|
|
|
disabledBorder: InputBorder.none,
|
|
|
|
errorBorder: InputBorder.none,
|
|
|
|
focusedErrorBorder: InputBorder.none,
|
|
|
|
fillColor: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.background,
|
|
|
|
counterText: "",
|
|
|
|
),
|
|
|
|
submittedFieldDecoration: _pinPutDecoration.copyWith(
|
2022-09-22 23:48:50 +00:00
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.infoItemIcons,
|
2022-11-25 19:24:01 +00:00
|
|
|
border: Border.all(
|
|
|
|
width: 1,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.extension<StackColors>()!
|
|
|
|
.infoItemIcons,
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
2022-11-25 19:24:01 +00:00
|
|
|
selectedFieldDecoration: _pinPutDecoration,
|
|
|
|
followingFieldDecoration: _pinPutDecoration,
|
2023-05-01 22:41:13 +00:00
|
|
|
isRandom:
|
|
|
|
ref.read(prefsChangeNotifierProvider).randomizePIN,
|
2022-11-25 19:24:01 +00:00
|
|
|
onSubmit: (String pin) async {
|
|
|
|
// _onSubmitCount++;
|
|
|
|
// if (_onSubmitCount - _onSubmitFailCount > 1) return;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
if (_pinPutController1.text == _pinPutController2.text) {
|
|
|
|
// ask if want to use biometrics
|
|
|
|
final bool useBiometrics = (Platform.isLinux)
|
|
|
|
? false
|
|
|
|
: await biometrics.authenticate(
|
|
|
|
cancelButtonText: "SKIP",
|
|
|
|
localizedReason:
|
|
|
|
"You can use your fingerprint to unlock the wallet and confirm transactions.",
|
|
|
|
title: "Enable fingerprint authentication",
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
//TODO investigate why this crashes IOS, maybe ios persists securestorage even after an uninstall?
|
|
|
|
// This should never fail as we are writing a new pin
|
|
|
|
// assert(
|
|
|
|
// (await _secureStore.read(key: "stack_pin")) == null);
|
|
|
|
// possible alternative to the above but it does not guarantee we aren't overwriting a pin
|
|
|
|
// if (!Platform.isLinux)
|
|
|
|
// assert((await _secureStore.read(key: "stack_pin")) ==
|
|
|
|
// null);
|
|
|
|
assert(ref.read(prefsChangeNotifierProvider).hasPin ==
|
|
|
|
false);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
await _secureStore.write(key: "stack_pin", value: pin);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
ref.read(prefsChangeNotifierProvider).useBiometrics =
|
|
|
|
useBiometrics;
|
|
|
|
ref.read(prefsChangeNotifierProvider).hasPin = true;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
await Future<void>.delayed(
|
|
|
|
const Duration(milliseconds: 200));
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
if (mounted) {
|
|
|
|
if (!widget.popOnSuccess) {
|
|
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
|
|
HomeView.routeName,
|
|
|
|
(route) => false,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
2022-11-25 19:24:01 +00:00
|
|
|
} else {
|
|
|
|
// _onSubmitFailCount++;
|
|
|
|
_pageController.animateTo(
|
|
|
|
0,
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
curve: Curves.linear,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
showFloatingFlushBar(
|
|
|
|
type: FlushBarType.warning,
|
|
|
|
message: "PIN codes do not match. Try again.",
|
|
|
|
context: context,
|
|
|
|
iconAsset: Assets.svg.alertCircle,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2022-11-25 19:24:01 +00:00
|
|
|
_pinPutController1.text = '';
|
|
|
|
_pinPutController2.text = '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-08-26 08:11:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|