From f450f0a181ec171436bf6971951579f2a0d45cbd Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 8 Feb 2021 13:36:50 +0100 Subject: [PATCH] Split walletOptions and changePin provider; Apply change PIN lenght +- 3Go RAM --- .gitignore | 2 + lib/globals.dart | 1 + lib/main.dart | 5 + lib/models/changePin.dart | 45 +++++++ lib/models/walletOptions.dart | 10 +- lib/screens/myWallets/changePin.dart | 143 +++++++++++++---------- lib/screens/myWallets/walletOptions.dart | 18 ++- 7 files changed, 152 insertions(+), 72 deletions(-) create mode 100644 lib/models/changePin.dart diff --git a/.gitignore b/.gitignore index 263a9d0..0e4fa11 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,5 @@ packages/dubp_rs/lib/ffi.dart # Rust things /target + +pubkeys.txt diff --git a/lib/globals.dart b/lib/globals.dart index b3a3170..a248f6c 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -6,3 +6,4 @@ Directory walletsDirectory; String appVersion; SharedPreferences prefs; String endPointGVA; +int ramSys; diff --git a/lib/main.dart b/lib/main.dart index 94db7af..8f5312d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'package:dubp/dubp.dart'; import 'package:gecko/globals.dart'; import 'package:gecko/models/cesiumPlus.dart'; +import 'package:gecko/models/changePin.dart'; import 'package:gecko/models/generateWallets.dart'; import 'package:gecko/models/history.dart'; import 'package:gecko/models/home.dart'; @@ -14,6 +15,7 @@ import 'package:provider/provider.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import "package:system_info/system_info.dart"; final bool enableSentry = true; @@ -28,6 +30,8 @@ Future main() async { await _homeProvider.createDefaultAvatar(); appVersion = await _homeProvider.getAppVersion(); prefs = await SharedPreferences.getInstance(); + ramSys = SysInfo.getTotalPhysicalMemory() ~/ 800000; + print("Votre appareil fait $ramSys de RAM."); final HiveStore _store = await HiveStore.open(path: '${appPath.path}/gqlCache'); @@ -91,6 +95,7 @@ class Gecko extends StatelessWidget { ChangeNotifierProvider(create: (_) => MyWalletsProvider()), ChangeNotifierProvider(create: (_) => GenerateWalletsProvider()), ChangeNotifierProvider(create: (_) => WalletOptionsProvider()), + ChangeNotifierProvider(create: (_) => ChangePinProvider()), ChangeNotifierProvider(create: (_) => CesiumPlusProvider()) ], child: GraphQLProvider( diff --git a/lib/models/changePin.dart b/lib/models/changePin.dart new file mode 100644 index 0000000..87bd694 --- /dev/null +++ b/lib/models/changePin.dart @@ -0,0 +1,45 @@ +import 'dart:io'; +import 'package:dubp/dubp.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'dart:async'; +import 'package:gecko/globals.dart'; + +class ChangePinProvider with ChangeNotifier { + bool ischangedPin = false; + TextEditingController newPin = new TextEditingController(); + + Future get badWallet => null; + + Future changePin(_name, _oldPin) async { + try { + final _walletFile = Directory('${walletsDirectory.path}/$_name'); + final _dewif = + File(_walletFile.path + '/wallet.dewif').readAsLinesSync()[0]; + + NewWallet newWalletFile = await DubpRust.changeDewifPin( + dewif: _dewif, + oldPin: _oldPin, + ); + + newPin.text = newWalletFile.pin; + ischangedPin = true; + notifyListeners(); + return newWalletFile; + } catch (e) { + print('Impossible de changer le code PIN.'); + return badWallet; + } + } + + Future storeWallet(context, _name, _newWalletFile) async { + final Directory walletNameDirectory = + Directory('${walletsDirectory.path}/$_name'); + final walletFile = File('${walletNameDirectory.path}/wallet.dewif'); + print(_newWalletFile); + + walletFile.writeAsString('${_newWalletFile.dewif}'); + Navigator.pop(context); + return _name; + } +} diff --git a/lib/models/walletOptions.dart b/lib/models/walletOptions.dart index eef7a76..cde9a44 100644 --- a/lib/models/walletOptions.dart +++ b/lib/models/walletOptions.dart @@ -15,7 +15,7 @@ class WalletOptionsProvider with ChangeNotifier { Future get badWallet => null; - Future _getPubkeyFromDewif(_dewif, _pin) async { + Future _getPubkeyFromDewif(_dewif, _pin, _pinLenght) async { String _pubkey; RegExp regExp = new RegExp( r'^[A-Z0-9]+$', @@ -23,7 +23,7 @@ class WalletOptionsProvider with ChangeNotifier { multiLine: false, ); - if (regExp.hasMatch(_pin) == true && _pin.length == 6) { + if (regExp.hasMatch(_pin) == true && _pin.length == _pinLenght) { print("Le format du code PIN est correct."); } else { print('Format de code PIN invalide'); @@ -50,14 +50,15 @@ class WalletOptionsProvider with ChangeNotifier { } } - Future readLocalWallet(String _name, String _pin) async { + Future readLocalWallet(String _name, String _pin, _pinLenght) async { isWalletUnlock = false; try { File _walletFile = File('${walletsDirectory.path}/$_name/wallet.dewif'); String _localDewif = await _walletFile.readAsString(); String _localPubkey; - if ((_localPubkey = await _getPubkeyFromDewif(_localDewif, _pin)) != + if ((_localPubkey = + await _getPubkeyFromDewif(_localDewif, _pin, _pinLenght)) != 'false') { this.pubkey.text = _localPubkey; isWalletUnlock = true; @@ -201,6 +202,7 @@ class WalletOptionsProvider with ChangeNotifier { final Directory walletNameDirectory = Directory('${walletsDirectory.path}/$_name'); final walletFile = File('${walletNameDirectory.path}/wallet.dewif'); + print(_newWalletFile); walletFile.writeAsString('${_newWalletFile.dewif}'); Navigator.pop(context); diff --git a/lib/screens/myWallets/changePin.dart b/lib/screens/myWallets/changePin.dart index 77251aa..8141211 100644 --- a/lib/screens/myWallets/changePin.dart +++ b/lib/screens/myWallets/changePin.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:dubp/dubp.dart'; -import 'package:gecko/models/walletOptions.dart'; +import 'package:gecko/models/changePin.dart'; import 'dart:io'; import 'package:provider/provider.dart'; @@ -17,68 +17,85 @@ class ChangePinScreen extends StatelessWidget with ChangeNotifier { @override Widget build(BuildContext context) { - WalletOptionsProvider _walletOptions = - Provider.of(context); - _walletOptions.changePin(walletName, oldPin); - return Scaffold( - resizeToAvoidBottomInset: false, - appBar: AppBar( - title: SizedBox( - height: 22, - child: Text(walletName), - )), - body: Center( - child: SafeArea( - child: Column(children: [ - SizedBox(height: 80), - Text( - 'Choisissez un code secret autogénéré :', - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 17.0, - color: Colors.grey[600], - fontWeight: FontWeight.w400), - ), - SizedBox(height: 30), - Container( - child: Stack( - alignment: Alignment.centerRight, - children: [ - TextField( - enabled: true, - controller: _walletOptions.newPin, - maxLines: 1, - textAlign: TextAlign.center, - decoration: InputDecoration(), - style: TextStyle( - fontSize: 30.0, - color: Colors.black, - fontWeight: FontWeight.bold)), - IconButton( - icon: Icon(Icons.replay), - color: Color(0xffD28928), - onPressed: () async { - _newWalletFile = - await _walletOptions.changePin(walletName, oldPin); - }, + ChangePinProvider _changePin = Provider.of(context); + // _walletOptions.changePin(walletName, oldPin); + // _walletOptions.newPin.text = _tmpPin; + return WillPopScope( + onWillPop: () { + _changePin.newPin.text = ''; + return Future.value(true); + }, + child: Scaffold( + resizeToAvoidBottomInset: false, + appBar: AppBar( + leading: IconButton( + icon: Icon(Icons.arrow_back, color: Colors.black), + onPressed: () { + _changePin.newPin.text = ''; + Navigator.of(context).pop(); + }), + title: SizedBox( + height: 22, + child: Text(walletName), + )), + body: Center( + child: SafeArea( + child: Column(children: [ + SizedBox(height: 80), + Text( + 'Choisissez un code secret autogénéré :', + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 17.0, + color: Colors.grey[600], + fontWeight: FontWeight.w400), + ), + SizedBox(height: 30), + Container( + child: Stack( + alignment: Alignment.centerRight, + children: [ + TextField( + enabled: true, + controller: _changePin.newPin, + maxLines: 1, + textAlign: TextAlign.center, + decoration: InputDecoration(), + style: TextStyle( + fontSize: 30.0, + color: Colors.black, + fontWeight: FontWeight.bold)), + IconButton( + icon: Icon(Icons.replay), + color: Color(0xffD28928), + onPressed: () async { + _newWalletFile = + await _changePin.changePin(walletName, oldPin); + }, + ), + ], ), - ], - ), - ), - SizedBox(height: 30), - SizedBox( - width: 200, - height: 50, - child: ElevatedButton( - style: ElevatedButton.styleFrom( - elevation: 12, - primary: Colors.green[400], //Color(0xffFFD68E), // background - onPrimary: Colors.black, // foreground - ), - onPressed: () => _walletOptions.storeWallet( - context, walletName, _newWalletFile), - child: Text('Confirmer', style: TextStyle(fontSize: 28))), - ) - ])))); + ), + SizedBox(height: 30), + SizedBox( + width: 200, + height: 50, + child: ElevatedButton( + style: ElevatedButton.styleFrom( + elevation: 12, + primary: + Colors.green[400], //Color(0xffFFD68E), // background + onPrimary: Colors.black, // foreground + ), + onPressed: _changePin.newPin.text != '' + ? () { + _changePin.newPin.text = ''; + _changePin.storeWallet( + context, walletName, _newWalletFile); + } + : null, + child: Text('Confirmer', style: TextStyle(fontSize: 28))), + ) + ]))))); } } diff --git a/lib/screens/myWallets/walletOptions.dart b/lib/screens/myWallets/walletOptions.dart index 5816744..caa9478 100644 --- a/lib/screens/myWallets/walletOptions.dart +++ b/lib/screens/myWallets/walletOptions.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:dubp/dubp.dart'; +import 'package:gecko/globals.dart'; import 'package:gecko/models/myWallets.dart'; import 'package:gecko/models/walletOptions.dart'; import 'package:gecko/screens/myWallets/changePin.dart'; @@ -21,6 +22,7 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { bool hasError = false; var pinColor = Color(0xffF9F9F1); var walletPin = ''; + int _pinLenght; Future get badWallet => null; @@ -33,6 +35,11 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { Provider.of(context); errorController = StreamController(); // _walletOptions.isWalletUnlock = false; + if (ramSys <= 3000) { + _pinLenght = 6; + } else { + _pinLenght = 5; + } return WillPopScope( onWillPop: () { @@ -45,8 +52,8 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { leading: IconButton( icon: Icon(Icons.arrow_back, color: Colors.black), onPressed: () { - Navigator.of(context).pop(); _walletOptions.isWalletUnlock = false; + Navigator.of(context).pop(); }), title: SizedBox( height: 22, @@ -201,13 +208,13 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { color: Colors.green.shade600, fontWeight: FontWeight.bold, ), - length: 6, + length: _pinLenght, obscureText: false, obscuringCharacter: '*', animationType: AnimationType.fade, validator: (v) { - if (v.length < 6) { - return "Votre code PIN fait 6 caractères"; + if (v.length < _pinLenght) { + return "Votre code PIN fait $_pinLenght caractères"; } else { return null; } @@ -243,7 +250,8 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { final resultWallet = await _walletOptions.readLocalWallet( this.walletName, - _pin.toUpperCase()); + _pin.toUpperCase(), + _pinLenght); if (resultWallet == 'bad') { errorController.add(ErrorAnimationType .shake); // Triggering error shake animation