From a2698a244b281f716b10f57d83b195882318e19b Mon Sep 17 00:00:00 2001 From: poka Date: Wed, 13 Jan 2021 00:00:11 +0100 Subject: [PATCH] Separate screen for generating wallets; Rename wallets screens files; --- lib/main.dart | 10 +- lib/ui/generateWallets.dart | 429 ------------------ lib/ui/home.dart | 4 +- lib/ui/myWallets/confirmWalletStorage.dart | 117 +++++ lib/ui/myWallets/generateWalletsScreen.dart | 194 ++++++++ .../myWalletsList.dart} | 17 +- lib/ui/myWallets/walletsHome.dart | 146 ++++++ pubspec.lock | 2 +- 8 files changed, 478 insertions(+), 441 deletions(-) delete mode 100644 lib/ui/generateWallets.dart create mode 100644 lib/ui/myWallets/confirmWalletStorage.dart create mode 100644 lib/ui/myWallets/generateWalletsScreen.dart rename lib/ui/{myWallets.dart => myWallets/myWalletsList.dart} (95%) create mode 100644 lib/ui/myWallets/walletsHome.dart diff --git a/lib/main.dart b/lib/main.dart index 6ca2db6..2554404 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -28,13 +28,13 @@ Future getRandomEndpoint() async { // print(_json); // final _list = _json[]; - final _list = ['https://g1.librelois.fr/gva']; - final _endpoint = getRandomElement(_list); + final _listEndpoints = ['https://g1.librelois.fr/gva']; + final _endpoint = getRandomElement(_listEndpoints); print('ENDPOINT: ' + _endpoint); // http.post(_endpoint); final response = await http.post(_endpoint); - if (response.statusCode != 200) { + if (response.statusCode != 400) { print('Endpoint statutcode: ' + response.statusCode.toString()); // _endpoint = getRandomElement(_list); return 'HS'; @@ -47,13 +47,13 @@ Future main() async { String randomEndpoint; // = await getRandomEndpoint(); int i = 0; do { - print(i); if (i >= 3) { print('NO VALID ENDPOINT FOUND !'); break; } if (i != 0) { - await Future.delayed(Duration(milliseconds: 500)); + print(i.toString() + ' ème essai de recherche de endpoint GVA.'); + await Future.delayed(Duration(milliseconds: 300)); } randomEndpoint = await getRandomEndpoint(); i++; diff --git a/lib/ui/generateWallets.dart b/lib/ui/generateWallets.dart deleted file mode 100644 index 8f1f326..0000000 --- a/lib/ui/generateWallets.dart +++ /dev/null @@ -1,429 +0,0 @@ -import 'package:gecko/ui/myWallets.dart'; -import 'package:flutter/foundation.dart'; -import 'package:flutter/material.dart'; -import 'package:dubp/dubp.dart'; -import 'package:sentry/sentry.dart' as sentry; -import 'dart:io'; -import 'dart:async'; -import 'package:path_provider/path_provider.dart'; - -class GenerateWalletScreen extends StatefulWidget { - const GenerateWalletScreen({Key keyGenWallet}) : super(key: keyGenWallet); - @override - GenerateWalletState createState() => GenerateWalletState(); -} - -class GenerateWalletState extends State { - GlobalKey _keyMyWallets = GlobalKey(); - GlobalKey _keyValidWallets = GlobalKey(); - void initState() { - super.initState(); - DubpRust.setup(); - } - - TextEditingController _mnemonicController = new TextEditingController(); - TextEditingController _pubkey = new TextEditingController(); - TextEditingController _pin = new TextEditingController(); - String generatedMnemonic; - bool walletIsGenerated = false; - NewWallet actualWallet; - final formKey = GlobalKey(); - - bool hasError = false; - String validPin = 'NO PIN'; - String currentText = ""; - var pinColor = Colors.grey[300]; - - @override - Widget build(BuildContext context) { - return Scaffold( - floatingActionButton: Container( - height: 80.0, - width: 80.0, - child: FittedBox( - child: FloatingActionButton( - heroTag: "buttonGenerateWallet", - onPressed: () async { - await generateMnemonic(); - // print(resultScan); - // if (resultScan != 'false') { - // onTabTapped(0); - // } - }, - child: Container( - height: 40.0, - width: 40.0, - child: Icon(Icons.person_add_alt_1_rounded)), - backgroundColor: Color( - 0xffEFEFBF), //Color(0xffFFD68E), //Color.fromARGB(500, 204, 255, 255), - ), - ), - ), - body: SafeArea( - child: Column(children: [ - FutureBuilder( - future: checkIfWalletExist('tata'), - builder: (context, fSnapshot) { - if (fSnapshot.hasData) - return Visibility( - visible: (!fSnapshot.data && !walletIsGenerated), - child: Column(children: [ - SizedBox(height: 80), - Center( - child: Text( - "Vous n'avez encore généré aucun portefeuille.", - style: TextStyle( - fontSize: 20, fontWeight: FontWeight.w500), - textAlign: TextAlign.center)), - SizedBox(height: 50), - RaisedButton( - color: Color(0xffFFD68E), - onPressed: () => generateMnemonic(), - child: Text('Générer un portefeuille', - style: TextStyle(fontSize: 20))), - SizedBox(height: 15), - Center( - child: Text("ou", - style: TextStyle( - fontSize: 20, fontWeight: FontWeight.w500), - textAlign: TextAlign.center)), - SizedBox(height: 15), - RaisedButton( - color: Color(0xffFFD68E), - onPressed: () => importWallet(), - child: Text('Importer un portefeuille existant', - style: TextStyle(fontSize: 20))), - ])); // WelcomeWalletScreen()); - return Center(child: CircularProgressIndicator()); - }), - FutureBuilder( - future: checkIfWalletExist('tata'), - builder: (context, fSnapshot) { - if (fSnapshot.hasData) - return Visibility( - visible: fSnapshot.data, - child: MyWalletsScreen(keyMyWallets: _keyMyWallets)); - return Center(child: CircularProgressIndicator()); - }), - SizedBox(height: 8), - Visibility( - visible: walletIsGenerated, - child: Column(children: [ - Text( - 'Clé publique:', - style: TextStyle( - fontSize: 15.0, - color: Colors.grey[600], - fontWeight: FontWeight.w400), - ), - TextField( - enabled: false, - controller: this._pubkey, - maxLines: 1, - textAlign: TextAlign.center, - decoration: InputDecoration(), - style: TextStyle( - fontSize: 14.0, - color: Colors.black, - fontWeight: FontWeight.bold)), - SizedBox(height: 8), - Text( - 'Phrase de restauration:', - style: TextStyle( - fontSize: 15.0, - color: Colors.grey[600], - fontWeight: FontWeight.w400), - ), - TextField( - enabled: false, - controller: this._mnemonicController, - maxLines: 3, - textAlign: TextAlign.center, - decoration: InputDecoration( - contentPadding: EdgeInsets.all(15.0), - ), - style: TextStyle( - fontSize: 22.0, - color: Colors.black, - fontWeight: FontWeight.w400)), - SizedBox(height: 8), - Text( - 'Code secret:', - style: TextStyle( - fontSize: 15.0, - color: Colors.grey[600], - fontWeight: FontWeight.w400), - ), - TextField( - enabled: false, - controller: this._pin, - maxLines: 1, - textAlign: TextAlign.center, - decoration: InputDecoration(), - style: TextStyle( - fontSize: 30.0, - color: Colors.black, - fontWeight: FontWeight.bold)), - SizedBox(height: 20), - // Expanded(child: Align(alignment: Alignment.bottomCenter)), - new RaisedButton( - color: Color(0xffFFD68E), - onPressed: walletIsGenerated - ? () { - Navigator.push( - context, - MaterialPageRoute(builder: (context) { - return ValidStoreWalletScreen( - validationKey: _keyValidWallets, - generatedMnemonic: this.generatedMnemonic, - generatedWallet: this.actualWallet); - }), - ).then((value) => setState(() { - if (value != null) { - print( - 'TODO: Fix resetWalletState()'); //TODO: Fix resetWalletState() - _pin.clear(); - _mnemonicController.clear(); - _pubkey.clear(); - this.generatedMnemonic = null; - this.actualWallet = null; - this.walletIsGenerated = false; - resetWalletState(); - // setState(() {}); - // getAllWalletsNames(); - // checkIfWalletExist('tata'); - // _keyMyWallets.currentState.getAllWalletsNames(); - } - })); - } - : null, - child: Text('Enregistrer ce portefeuille', - style: TextStyle(fontSize: 20))), - SizedBox(height: 20) - ]), -// Visibility( -// visible: walletIsGenerated, -// child: Text(""" -// Notez bien votre phrase de restauration sur un papier ou imprimez le, ainsi que votre code secret, que vous devrez retenir. -// Pour payer ou manipuler votre wallet, seul votre code secret vous sera nécessaire. -// Votre phrase de restauration quant à elle, constitué de 12 mots, vous permet de restaurer votre wallet sur un autre appareil. Gardez la précieusement, en lieu sûr. -// """, -// style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold), -// textAlign: TextAlign.center), -// ) - ) - ]))); - } - - Future resetWalletState() async { - final bool _isExist = await checkIfWalletExist('tata'); - print('The wallet exist in resetWalletState(): ' + _isExist.toString()); - // initState(); - // _keyMyWallets.currentState.setState(() {}); - // _keyMyWallets.currentState.initAppDirectory(); - setState(() { - // getAllWalletsNames(); - // this.walletIsGenerated = true; - }); - } - - Future generateMnemonic() async { - try { - this.generatedMnemonic = - await DubpRust.genMnemonic(language: Language.french); - } catch (e, stack) { - print(e); - if (kReleaseMode) { - await sentry.Sentry.captureException( - e, - stackTrace: stack, - ); - } - } - this.actualWallet = await generateWallet(this.generatedMnemonic); - this.walletIsGenerated = true; - // await checkIfWalletExist(); - return this.generatedMnemonic; - } - - Future generateWallet(generatedMnemonic) async { - try { - this.actualWallet = await DubpRust.genWalletFromMnemonic( - language: Language.french, - mnemonic: generatedMnemonic, - secretCodeType: SecretCodeType.letters); - } catch (e, stack) { - print(e); - if (kReleaseMode) { - await sentry.Sentry.captureException( - e, - stackTrace: stack, - ); - } - } - - setState(() { - this._mnemonicController.text = generatedMnemonic; - this._pubkey.text = actualWallet.publicKey; - this._pin.text = actualWallet.pin; - }); - - return actualWallet; - } - - Future checkIfWalletExist(_name) async { - final appPath = await _localPath; - final _walletFile = File('$appPath/wallets/$_name/wallet.dewif'); - - // deleteWallet(); - print(_walletFile.path); - final bool isExist = await File(_walletFile.path).exists(); - print('Wallet existe ? : ' + isExist.toString()); - print('Is wallet generated ? : ' + walletIsGenerated.toString()); - if (isExist) { - print('Un wallet existe !'); - return true; - } else { - return false; - } - } - - Future getAllWalletsNames() async { - print('Je suis getAllWalletsNames() !!'); - final _appPath = await getApplicationDocumentsDirectory(); - // List _listWallets = []; - // _listWallets.add('tortuuue'); - _keyValidWallets.currentState._listWallets - .clear(); //TODO: Fix: The getter '_listWallets' was called on null. - print(_appPath); - print('Je suis getAllWalletsNames() !! 2'); - - _appPath - .list(recursive: false, followLinks: false) - .listen((FileSystemEntity entity) { - print(entity.path.split('/').last); - _keyValidWallets.currentState._listWallets - .add(entity.path.split('/').last); - }); - - return _keyValidWallets.currentState._listWallets; - // final _local = await _appPath.path.list().toList(); - } - - Future importWallet() async {} - - Future get _localPath async { - final directory = await getApplicationDocumentsDirectory(); - return directory.path; - } -} - -/* *** ValidStoreWalletScreen Class *** */ - -class ValidStoreWalletScreen extends StatefulWidget { - final String generatedMnemonic; - final NewWallet generatedWallet; - - ValidStoreWalletScreen( - {Key validationKey, - @required this.generatedMnemonic, - @required this.generatedWallet}) - : super(key: validationKey); - - @override - ValidStoreWalletState createState() => ValidStoreWalletState(); -} - -class ValidStoreWalletState extends State { - GlobalKey _keyValidWallets = GlobalKey(); - void initState() { - super.initState(); - // DubpRust.setup(); - this._mnemonicController.text = widget.generatedMnemonic; - this._pubkey.text = widget.generatedWallet.publicKey; - } - - TextEditingController _mnemonicController = new TextEditingController(); - TextEditingController _pubkey = new TextEditingController(); - TextEditingController _pin = new TextEditingController(); - String walletName = 'tata'; - List _listWallets = []; - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(), - body: Center( - child: Column(children: [ - TextField( - enabled: false, - controller: this._mnemonicController, - maxLines: 2, - textAlign: TextAlign.center, - decoration: InputDecoration(), - style: TextStyle( - fontSize: 15.0, - color: Colors.black, - fontWeight: FontWeight.bold)), - TextField( - enabled: false, - controller: this._pubkey, - maxLines: 1, - textAlign: TextAlign.center, - decoration: InputDecoration(), - style: TextStyle( - fontSize: 14.0, - color: Colors.black, - fontWeight: FontWeight.bold)), - new RaisedButton( - color: Color(0xffFFD68E), - onPressed: () => storeWallet(), - child: Text('Confirmer', style: TextStyle(fontSize: 20))), - ]), - ), - ); - } - - Future storeWallet() async { - final appPath = await _localPath; - final walletFile = File('$appPath/wallets/${this.walletName}/wallet.dewif'); - - final isExist = await Directory('$appPath/wallets').exists(); - if (isExist == false) { - new Directory('$appPath/wallets').createSync(); - } - - new Directory('$appPath/wallets/${this.walletName}').createSync(); - walletFile.writeAsString('${widget.generatedWallet.dewif}'); - _pin.clear(); - - await getAllWalletsNames(); - Navigator.pop(context, true); - FocusScope.of(context).unfocus(); - - return this.walletName; - } - - Future get _localPath async { - final directory = await getApplicationDocumentsDirectory(); - return directory.path; - } - - Future getAllWalletsNames() async { - final _appPath = await getApplicationDocumentsDirectory(); - // List _listWallets = []; - // _listWallets.add('tortuuue'); - this._listWallets.clear(); - print(_appPath); - - _appPath - .list(recursive: false, followLinks: false) - .listen((FileSystemEntity entity) { - print(entity.path.split('/').last); - this._listWallets.add(entity.path.split('/').last); - }); - - return _listWallets; - // final _local = await _appPath.path.list().toList(); - } -} diff --git a/lib/ui/home.dart b/lib/ui/home.dart index 4668e80..6a65ae9 100644 --- a/lib/ui/home.dart +++ b/lib/ui/home.dart @@ -1,8 +1,8 @@ import 'package:gecko/ui/historyScreen.dart'; -import 'package:gecko/ui/generateWallets.dart'; import 'package:flutter/material.dart'; import 'dart:typed_data'; import 'dart:ui'; +import 'package:gecko/ui/myWallets/walletsHome.dart'; //ignore: must_be_immutable class HomeScreen extends StatefulWidget { @@ -38,7 +38,7 @@ class HomeScreenState extends State { HistoryScreen( keyHistory: _keyHistory, ), - GenerateWalletScreen(), + WalletsHome(), ], ), ), diff --git a/lib/ui/myWallets/confirmWalletStorage.dart b/lib/ui/myWallets/confirmWalletStorage.dart new file mode 100644 index 0000000..19f9f4e --- /dev/null +++ b/lib/ui/myWallets/confirmWalletStorage.dart @@ -0,0 +1,117 @@ +import 'dart:io'; +import 'package:dubp/dubp.dart'; +import 'package:flutter/material.dart'; +import 'package:path_provider/path_provider.dart'; + +class ConfirmStoreWallet extends StatefulWidget { + final String generatedMnemonic; + final NewWallet generatedWallet; + + ConfirmStoreWallet( + {Key validationKey, + @required this.generatedMnemonic, + @required this.generatedWallet}) + : super(key: validationKey); + + @override + ConfirmStoreWalletState createState() => ConfirmStoreWalletState(); +} + +class ConfirmStoreWalletState extends State { + // GlobalKey _keyValidWallets = GlobalKey(); + void initState() { + super.initState(); + // DubpRust.setup(); + this._mnemonicController.text = widget.generatedMnemonic; + this._pubkey.text = widget.generatedWallet.publicKey; + } + + TextEditingController _mnemonicController = new TextEditingController(); + TextEditingController _pubkey = new TextEditingController(); + TextEditingController _pin = new TextEditingController(); + String walletName = 'MonWallet'; + List _listWallets = []; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(), + body: Center( + child: Column(children: [ + TextField( + enabled: false, + controller: this._mnemonicController, + maxLines: 2, + textAlign: TextAlign.center, + decoration: InputDecoration(), + style: TextStyle( + fontSize: 15.0, + color: Colors.black, + fontWeight: FontWeight.bold)), + TextField( + enabled: false, + controller: this._pubkey, + maxLines: 1, + textAlign: TextAlign.center, + decoration: InputDecoration(), + style: TextStyle( + fontSize: 14.0, + color: Colors.black, + fontWeight: FontWeight.bold)), + new ElevatedButton( + style: ElevatedButton.styleFrom( + primary: Color(0xffFFD68E), // background + onPrimary: Colors.black, // foreground + ), + onPressed: () => storeWallet(), + child: Text('Confirmer', style: TextStyle(fontSize: 20))), + ]), + ), + ); + } + + Future storeWallet() async { + final appPath = await _localPath; + final walletFile = File('$appPath/wallets/${this.walletName}/wallet.dewif'); + + final isExist = await Directory('$appPath/wallets').exists(); + if (isExist == false) { + new Directory('$appPath/wallets').createSync(); + } + + new Directory('$appPath/wallets/${this.walletName}').createSync(); + walletFile.writeAsString('${widget.generatedWallet.dewif}'); + _pin.clear(); + + await getAllWalletsNames(); + Navigator.pop(context, true); + Navigator.pop(context, true); + // setState(() {}); + // FocusScope.of(context).unfocus(); + + return this.walletName; + } + + Future get _localPath async { + final directory = await getApplicationDocumentsDirectory(); + return directory.path; + } + + Future getAllWalletsNames() async { + final _appPath = await getApplicationDocumentsDirectory(); + // List _listWallets = []; + // _listWallets.add('tortuuue'); + this._listWallets.clear(); + print(_appPath); + + _appPath + .list(recursive: false, followLinks: false) + .listen((FileSystemEntity entity) { + print(entity.path.split('/').last); + this._listWallets.add(entity.path.split('/').last); + }); + + return _listWallets; + // final _local = await _appPath.path.list().toList(); + } +} diff --git a/lib/ui/myWallets/generateWalletsScreen.dart b/lib/ui/myWallets/generateWalletsScreen.dart new file mode 100644 index 0000000..20f16f3 --- /dev/null +++ b/lib/ui/myWallets/generateWalletsScreen.dart @@ -0,0 +1,194 @@ +import 'package:gecko/ui/myWallets/confirmWalletStorage.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:sentry/sentry.dart' as sentry; +import 'package:dubp/dubp.dart'; + +class GenerateWalletsScreen extends StatefulWidget { + const GenerateWalletsScreen({Key keyGenWallet}) : super(key: keyGenWallet); + @override + GenerateWalletsState createState() => GenerateWalletsState(); +} + +class GenerateWalletsState extends State { + // GlobalKey _keyMyWallets = GlobalKey(); + // GlobalKey _keyValidWallets = GlobalKey(); + void initState() { + super.initState(); + DubpRust.setup(); + generateMnemonic(); + } + + TextEditingController _mnemonicController = new TextEditingController(); + TextEditingController _pubkey = new TextEditingController(); + TextEditingController _pin = new TextEditingController(); + String generatedMnemonic; + bool walletIsGenerated = false; + NewWallet actualWallet; + // final formKey = GlobalKey(); + + bool hasError = false; + String validPin = 'NO PIN'; + String currentText = ""; + var pinColor = Colors.grey[300]; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(), + floatingActionButton: Container( + height: 80.0, + width: 80.0, + child: FittedBox( + child: FloatingActionButton( + heroTag: "buttonGenerateWallet", + onPressed: () => generateMnemonic(), + // print(resultScan); + // if (resultScan != 'false') { + // onTabTapped(0); + // } + child: Container( + height: 40.0, width: 40.0, child: Icon(Icons.replay)), + backgroundColor: Color( + 0xffEFEFBF), //Color(0xffFFD68E), //Color.fromARGB(500, 204, 255, 255), + ))), + body: SafeArea( + child: Column(children: [ + SizedBox(height: 20), + Text( + 'Clé publique:', + style: TextStyle( + fontSize: 15.0, + color: Colors.grey[600], + fontWeight: FontWeight.w400), + ), + TextField( + enabled: false, + controller: this._pubkey, + maxLines: 1, + textAlign: TextAlign.center, + decoration: InputDecoration(), + style: TextStyle( + fontSize: 14.0, + color: Colors.black, + fontWeight: FontWeight.bold)), + SizedBox(height: 8), + Text( + 'Phrase de restauration:', + style: TextStyle( + fontSize: 15.0, + color: Colors.grey[600], + fontWeight: FontWeight.w400), + ), + TextField( + enabled: false, + controller: this._mnemonicController, + maxLines: 3, + textAlign: TextAlign.center, + decoration: InputDecoration( + contentPadding: EdgeInsets.all(15.0), + ), + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.w400)), + SizedBox(height: 8), + Text( + 'Code secret:', + style: TextStyle( + fontSize: 15.0, + color: Colors.grey[600], + fontWeight: FontWeight.w400), + ), + TextField( + enabled: false, + controller: this._pin, + maxLines: 1, + textAlign: TextAlign.center, + decoration: InputDecoration(), + style: TextStyle( + fontSize: 30.0, + color: Colors.black, + fontWeight: FontWeight.bold)), + SizedBox(height: 20), + // Expanded(child: Align(alignment: Alignment.bottomCenter)), + new ElevatedButton( + style: ElevatedButton.styleFrom( + primary: Color(0xffFFD68E), // background + onPrimary: Colors.black, // foreground + ), + onPressed: walletIsGenerated + ? () { + Navigator.push( + context, + MaterialPageRoute(builder: (context) { + return ConfirmStoreWallet( + // validationKey: _keyValidWallets, + generatedMnemonic: this.generatedMnemonic, + generatedWallet: this.actualWallet); + }), + ) + // .then((value) => setState(() { + // if (value != null) { + // _pin.clear(); + // _mnemonicController.clear(); + // _pubkey.clear(); + // this.generatedMnemonic = null; + // this.actualWallet = null; + // this.walletIsGenerated = false; + // } + // })) + ; + } + : null, + child: Text('Enregistrer ce portefeuille', + style: TextStyle(fontSize: 20))), + SizedBox(height: 20) + ]), + )); + } + + Future generateMnemonic() async { + try { + this.generatedMnemonic = + await DubpRust.genMnemonic(language: Language.french); + this.actualWallet = await generateWallet(this.generatedMnemonic); + this.walletIsGenerated = true; + } catch (e, stack) { + print(e); + if (kReleaseMode) { + await sentry.Sentry.captureException( + e, + stackTrace: stack, + ); + } + } + // await checkIfWalletExist(); + return this.generatedMnemonic; + } + + Future generateWallet(generatedMnemonic) async { + try { + this.actualWallet = await DubpRust.genWalletFromMnemonic( + language: Language.french, + mnemonic: generatedMnemonic, + secretCodeType: SecretCodeType.letters); + } catch (e, stack) { + print(e); + if (kReleaseMode) { + await sentry.Sentry.captureException( + e, + stackTrace: stack, + ); + } + } + + setState(() { + this._mnemonicController.text = generatedMnemonic; + this._pubkey.text = actualWallet.publicKey; + this._pin.text = actualWallet.pin; + }); + + return actualWallet; + } +} diff --git a/lib/ui/myWallets.dart b/lib/ui/myWallets/myWalletsList.dart similarity index 95% rename from lib/ui/myWallets.dart rename to lib/ui/myWallets/myWalletsList.dart index 04c1bee..af8b244 100644 --- a/lib/ui/myWallets.dart +++ b/lib/ui/myWallets/myWalletsList.dart @@ -1,4 +1,4 @@ -import 'package:gecko/ui/generateWallets.dart'; +// import 'package:gecko/ui/generateWallets.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:dubp/dubp.dart'; @@ -15,7 +15,7 @@ class MyWalletsScreen extends StatefulWidget { } class MyWalletState extends State { - GlobalKey _keyGenWallet = GlobalKey(); + // GlobalKey _keyGenWallet = GlobalKey(); StreamController errorController; Directory appPath; List _listWallets = []; @@ -25,6 +25,7 @@ class MyWalletState extends State { errorController = StreamController(); initAppDirectory(); DubpRust.setup(); + // getAllWalletsNames(); // initAppDirectory(); // _walletsList = await getAllWalletsNames(); // HistoryScreen( @@ -235,7 +236,7 @@ class MyWalletState extends State { Future readLocalWallet(String _pin) async { // print(pin); try { - final file = await _localWallet('tata'); + final file = await _localWallet('MonWallet'); String _localDewif = await file.readAsString(); String _localPubkey; @@ -263,9 +264,17 @@ class MyWalletState extends State { final appPath = await _localPath; final _walletFile = File('$appPath/wallets/$_name/wallet.dewif'); - await _walletFile.delete(); + _walletFile.delete(); + getAllWalletsNames(); + setState(() { + // getAllWalletsNames(); + }); return 0; } catch (e) { + getAllWalletsNames(); + setState(() { + // getAllWalletsNames(); + }); return 1; } } diff --git a/lib/ui/myWallets/walletsHome.dart b/lib/ui/myWallets/walletsHome.dart new file mode 100644 index 0000000..ee32159 --- /dev/null +++ b/lib/ui/myWallets/walletsHome.dart @@ -0,0 +1,146 @@ +import 'package:gecko/ui/myWallets/generateWalletsScreen.dart'; +import 'package:gecko/ui/myWallets/myWalletsList.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:dubp/dubp.dart'; +import 'dart:io'; +import 'dart:async'; +import 'package:path_provider/path_provider.dart'; + +class WalletsHome extends StatefulWidget { + const WalletsHome({Key keyGenWallet}) : super(key: keyGenWallet); + @override + WalletsHomeState createState() => WalletsHomeState(); +} + +class WalletsHomeState extends State { + GlobalKey _keyWalletsHome = GlobalKey(); + // GlobalKey _keyMyWallets = GlobalKey(); + // GlobalKey _keyValidWallets = GlobalKey(); + void initState() { + super.initState(); + DubpRust.setup(); + getAppDirectory(); + // _keyWalletsHome.currentState.getAllWalletsNames(); + // _keyMyWallets.currentState.getAllWalletsNames(); + } + + String generatedMnemonic; + bool walletIsGenerated = false; + NewWallet actualWallet; + + bool hasError = false; + String validPin = 'NO PIN'; + String currentText = ""; + var pinColor = Colors.grey[300]; + Directory appPath; + + @override + Widget build(BuildContext context) { + // getAppDirectory(); + return Scaffold( + floatingActionButton: Visibility( + visible: (checkIfWalletExist( + 'MonWallet')), //!checkIfWalletExist('MonWallet') && + child: Container( + height: 80.0, + width: 80.0, + child: FittedBox( + child: FloatingActionButton( + heroTag: "buttonGenerateWallet", + onPressed: () { + Navigator.push( + context, + MaterialPageRoute(builder: (context) { + return GenerateWalletsScreen(); + }), + ); + }, + child: Container( + height: 40.0, + width: 40.0, + child: Icon(Icons.person_add_alt_1_rounded)), + backgroundColor: Color(0xffEFEFBF))))), + body: SafeArea( + child: Column(children: [ + Visibility( + visible: (!checkIfWalletExist('MonWallet') && !walletIsGenerated), + child: Column(children: [ + SizedBox(height: 120), + Center( + child: Text("Vous n'avez encore généré aucun portefeuille.", + style: TextStyle( + fontSize: 20, fontWeight: FontWeight.w500), + textAlign: TextAlign.center)), + SizedBox(height: 80), + ElevatedButton( + style: ElevatedButton.styleFrom( + primary: Color(0xffFFD68E), // background + onPrimary: Colors.black, // foreground + ), + onPressed: () => Navigator.push( + context, + MaterialPageRoute(builder: (context) { + return GenerateWalletsScreen(); + }), + ), + child: Text('Générer un portefeuille', + style: TextStyle(fontSize: 20))), + SizedBox(height: 15), + Center( + child: Text("ou", + style: TextStyle( + fontSize: 20, fontWeight: FontWeight.w500), + textAlign: TextAlign.center)), + SizedBox(height: 15), + ElevatedButton( + style: ElevatedButton.styleFrom( + primary: Color(0xffFFD68E), // background + onPrimary: Colors.black, // foreground + ), + onPressed: () => importWallet(), + child: Text('Importer un portefeuille existant', + style: TextStyle(fontSize: 20))), + ])), + Visibility( + visible: checkIfWalletExist('MonWallet'), + child: MyWalletsScreen(keyMyWallets: _keyWalletsHome)) + ]))); + } + + // Future resetWalletState() async { + // final bool _isExist = await checkIfWalletExist('MonWallet'); + // print('The wallet exist in resetWalletState(): ' + _isExist.toString()); + // // initState(); + // // _keyMyWallets.currentState.setState(() {}); + // // _keyMyWallets.currentState.initAppDirectory(); + // setState(() { + // // getAllWalletsNames(); + // // this.walletIsGenerated = true; + // }); + // } + + bool checkIfWalletExist(_name) { + if (this.appPath == null) { + return false; + } + final bool isExist = + File('${this.appPath.path}/wallets/$_name/wallet.dewif').existsSync(); + print(this.appPath.path); + print('Wallet existe ? : ' + isExist.toString()); + print('Is wallet generated ? : ' + walletIsGenerated.toString()); + if (isExist) { + print('Un wallet existe !'); + return true; + } else { + return false; + } + } + + Future getAppDirectory() async { + this.appPath = await getApplicationDocumentsDirectory(); + setState(() {}); + } + + Future importWallet() async {} +} diff --git a/pubspec.lock b/pubspec.lock index a6dbebd..c2a9048 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -576,4 +576,4 @@ packages: version: "2.2.1" sdks: dart: ">=2.12.0-0.0 <3.0.0" - flutter: ">=1.22.0 <2.0.0" + flutter: ">=1.22.0"