From 4bbb7e39151c4ff934b9bba91222284f8e05a239 Mon Sep 17 00:00:00 2001 From: poka Date: Sun, 14 Feb 2021 21:04:58 +0100 Subject: [PATCH 1/5] Remove pubkey from HD wallet infos; Rename portefeuille to trousseau --- lib/models/generateWallets.dart | 13 +----- lib/models/myWallets.dart | 2 +- lib/screens/myWallets/confirmWalletStorage.dart | 59 +++++++++---------------- lib/screens/myWallets/generateWallets.dart | 41 +++-------------- lib/screens/myWallets/walletsHome.dart | 2 +- 5 files changed, 31 insertions(+), 86 deletions(-) diff --git a/lib/models/generateWallets.dart b/lib/models/generateWallets.dart index da33f96..ea67e0f 100644 --- a/lib/models/generateWallets.dart +++ b/lib/models/generateWallets.dart @@ -33,7 +33,6 @@ class GenerateWalletsProvider with ChangeNotifier { final Directory walletNameDirectory = Directory('${walletsDirectory.path}/$_name'); final walletFile = File('${walletNameDirectory.path}/wallet.dewif'); - final walletPubkey = File('${walletNameDirectory.path}/pubkey'); if (await walletNameDirectory.exists()) { print('Ce wallet existe déjà, impossible de le créer.'); @@ -43,10 +42,9 @@ class GenerateWalletsProvider with ChangeNotifier { await walletNameDirectory.create(); await walletFile.writeAsString('${wallet.dewif}'); - await walletPubkey.writeAsString('${wallet.publicKey}'); Navigator.pop(context, true); - Navigator.pop(context, wallet.publicKey); + Navigator.pop(context, true); // notifyListeners(); return _name; @@ -173,7 +171,6 @@ class GenerateWalletsProvider with ChangeNotifier { } mnemonicController.text = generatedMnemonic; - pubkey.text = this.actualWallet.publicKey; pin.text = this.actualWallet.pin; // notifyListeners(); @@ -190,7 +187,7 @@ class GenerateWalletsProvider with ChangeNotifier { // notifyListeners(); } - Future printWallet(String _title, String _pubkey) async { + Future printWallet(String _title) async { final ByteData fontData = await rootBundle.load("assets/OpenSans-Regular.ttf"); final pw.Font ttf = pw.Font.ttf(fontData.buffer.asByteData()); @@ -204,12 +201,6 @@ class GenerateWalletsProvider with ChangeNotifier { pageFormat: PdfPageFormat.a4, build: (context) { return pw.Column(children: [ - pw.Text("Clé publique:", - style: pw.TextStyle(fontSize: 20, font: ttf)), - pw.SizedBox(height: 10), - pw.Text(_pubkey, - style: pw.TextStyle(fontSize: 15, font: ttf), - textAlign: pw.TextAlign.center), pw.SizedBox(height: 20), pw.Text("Phrase de restauration:", style: pw.TextStyle(fontSize: 20, font: ttf)), diff --git a/lib/models/myWallets.dart b/lib/models/myWallets.dart index f3a4ff1..7fefaac 100644 --- a/lib/models/myWallets.dart +++ b/lib/models/myWallets.dart @@ -73,7 +73,7 @@ class MyWalletsProvider with ChangeNotifier { Provider.of(context); return AlertDialog( title: Text( - 'Êtes-vous sûr de vouloir supprimer tous vos portefeuilles ?'), + 'Êtes-vous sûr de vouloir supprimer tous vos trousseaux ?'), content: SingleChildScrollView(child: Text('')), actions: [ TextButton( diff --git a/lib/screens/myWallets/confirmWalletStorage.dart b/lib/screens/myWallets/confirmWalletStorage.dart index b4b81bd..0130fa8 100644 --- a/lib/screens/myWallets/confirmWalletStorage.dart +++ b/lib/screens/myWallets/confirmWalletStorage.dart @@ -17,7 +17,6 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier { NewWallet generatedWallet; TextEditingController _mnemonicController = TextEditingController(); - TextEditingController _pubkey = TextEditingController(); TextEditingController _inputRestoreWord = TextEditingController(); TextEditingController walletName = TextEditingController(); FocusNode _wordFocus = FocusNode(); @@ -30,7 +29,6 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier { Provider.of(context); this._mnemonicController.text = generatedMnemonic; - this._pubkey.text = generatedWallet.publicKey; return WillPopScope( onWillPop: () { _generateWalletProvider.isAskedWordValid = false; @@ -49,38 +47,21 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier { }), title: SizedBox( height: 22, - child: Text('Enregistrer ce portefeuille'), + child: Text('Enregistrer ce trousseau'), )), body: Center( child: Column(children: [ SizedBox(height: 15), - Text( - 'Votre clé publique est :', - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 17.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: 12), - Text( - 'Quel est le ${_generateWalletProvider.nbrWord + 1}ème mot de votre phrase de restauration ?', - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 17.0, - color: Colors.grey[600], - fontWeight: FontWeight.w400), - ), + Container( + width: 360, + child: Text( + 'Quel est le ${_generateWalletProvider.nbrWord + 1}ème mot de votre phrase de restauration ?', + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 17.0, + color: Colors.grey[600], + fontWeight: FontWeight.w400), + )), TextFormField( focusNode: _wordFocus, autofocus: true, @@ -99,14 +80,16 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier { color: _generateWalletProvider.askedWordColor, fontWeight: FontWeight.w500)), SizedBox(height: 12), - Text( - 'Choisissez un nom pour votre portefeuille :', - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 17.0, - color: Colors.grey[600], - fontWeight: FontWeight.w400), - ), + Container( + width: 360, + child: Text( + 'Choisissez un nom pour votre premier portefeuille :', + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 17.0, + color: Colors.grey[600], + fontWeight: FontWeight.w400), + )), TextFormField( focusNode: _generateWalletProvider.walletNameFocus, inputFormatters: [ diff --git a/lib/screens/myWallets/generateWallets.dart b/lib/screens/myWallets/generateWallets.dart index 6b0b4a9..6ce90a5 100644 --- a/lib/screens/myWallets/generateWallets.dart +++ b/lib/screens/myWallets/generateWallets.dart @@ -1,6 +1,4 @@ -import 'package:flutter/services.dart'; import 'package:gecko/models/generateWallets.dart'; -import 'package:gecko/models/walletOptions.dart'; import 'package:gecko/screens/myWallets/confirmWalletStorage.dart'; import 'package:flutter/material.dart'; import 'package:printing/printing.dart'; @@ -15,7 +13,6 @@ class GenerateWalletsScreen extends StatelessWidget { String currentText = ""; var pinColor = Colors.grey[300]; - GlobalKey _toolTipPubkey = GlobalKey(); GlobalKey _toolTipSentence = GlobalKey(); GlobalKey _toolTipSecret = GlobalKey(); @@ -23,8 +20,6 @@ class GenerateWalletsScreen extends StatelessWidget { Widget build(BuildContext context) { GenerateWalletsProvider _generateWalletProvider = Provider.of(context); - WalletOptionsProvider _walletOptions = - Provider.of(context); _generateWalletProvider.generateMnemonic(); print('IS GENERATED ? : ' + _generateWalletProvider.walletIsGenerated.toString()); @@ -32,7 +27,7 @@ class GenerateWalletsScreen extends StatelessWidget { appBar: AppBar( title: SizedBox( height: 22, - child: Text('Générer un portefeuille'), + child: Text('Générer un trousseau'), )), floatingActionButton: Container( height: 80.0, @@ -53,26 +48,6 @@ class GenerateWalletsScreen extends StatelessWidget { builder: (ctx) => SafeArea( child: Column(children: [ SizedBox(height: 20), - toolTips(_toolTipPubkey, 'Clé publique:', - "C'est votre RIB en Ğ1, les gens l'utiliseront pour vous payer"), - GestureDetector( - onTap: () { - Clipboard.setData(ClipboardData( - text: _generateWalletProvider.pubkey.text)); - _walletOptions.snackCopyKey(ctx); - }, - child: TextField( - enabled: false, - controller: _generateWalletProvider.pubkey, - maxLines: 1, - textAlign: TextAlign.center, - decoration: InputDecoration(), - style: TextStyle( - fontSize: 14.0, - color: Colors.black, - fontWeight: FontWeight.bold, - fontFamily: 'Monospace'))), - SizedBox(height: 8), toolTips(_toolTipSentence, 'Phrase de restauration:', "Notez et gardez cette phrase précieusement sur un papier, elle vous servira à restaurer votre portefeuille sur un autre appareil"), TextField( @@ -137,7 +112,7 @@ class GenerateWalletsScreen extends StatelessWidget { ); } : null, - child: Text('Enregistrer ce portefeuille', + child: Text('Enregistrer ce trousseau', style: TextStyle(fontSize: 20))), SizedBox(height: 20), GestureDetector( @@ -146,9 +121,7 @@ class GenerateWalletsScreen extends StatelessWidget { context, MaterialPageRoute(builder: (context) { return PrintWallet( - _generateWalletProvider.generatedMnemonic, - _generateWalletProvider - .actualWallet.publicKey); + _generateWalletProvider.generatedMnemonic); }), ); }, @@ -194,10 +167,9 @@ class GenerateWalletsScreen extends StatelessWidget { // ignore: must_be_immutable class PrintWallet extends StatelessWidget { - PrintWallet(this.sentence, this.pubkey); + PrintWallet(this.sentence); final String sentence; - final String pubkey; @override Widget build(BuildContext context) { @@ -205,10 +177,9 @@ class PrintWallet extends StatelessWidget { Provider.of(context); return MaterialApp( home: Scaffold( - appBar: AppBar(title: Text('Imprimer ce portefeuille')), + appBar: AppBar(title: Text('Imprimer ce trousseau')), body: PdfPreview( - build: (format) => - _generateWalletProvider.printWallet(sentence, pubkey), + build: (format) => _generateWalletProvider.printWallet(sentence), ), ), ); diff --git a/lib/screens/myWallets/walletsHome.dart b/lib/screens/myWallets/walletsHome.dart index fa7da97..c88cd85 100644 --- a/lib/screens/myWallets/walletsHome.dart +++ b/lib/screens/myWallets/walletsHome.dart @@ -63,7 +63,7 @@ class WalletsHome extends StatelessWidget { return GenerateWalletsScreen(); }), ), - child: Text('Générer un portefeuille', + child: Text('Générer un trousseau', style: TextStyle(fontSize: 20))), SizedBox(height: 15), Center( From 5ec6b715b0c6fc3fc271c7f147b45dfbf12f5ee0 Mon Sep 17 00:00:00 2001 From: poka Date: Sun, 14 Feb 2021 23:17:03 +0100 Subject: [PATCH 2/5] Workflow OK for 1 HD wallet and first derivation only --- lib/models/generateWallets.dart | 61 +++++++++++--------------------- lib/models/myWallets.dart | 18 ++++------ lib/models/walletOptions.dart | 17 +++++---- lib/screens/myWallets/walletOptions.dart | 7 ++-- lib/screens/myWallets/walletsHome.dart | 13 +++---- 5 files changed, 49 insertions(+), 67 deletions(-) diff --git a/lib/models/generateWallets.dart b/lib/models/generateWallets.dart index ea67e0f..cc9dd25 100644 --- a/lib/models/generateWallets.dart +++ b/lib/models/generateWallets.dart @@ -26,23 +26,31 @@ class GenerateWalletsProvider with ChangeNotifier { bool walletIsGenerated = true; TextEditingController mnemonicController = TextEditingController(); - TextEditingController pubkey = TextEditingController(); TextEditingController pin = TextEditingController(); - Future storeWallet(NewWallet wallet, _name, BuildContext context) async { - final Directory walletNameDirectory = - Directory('${walletsDirectory.path}/$_name'); - final walletFile = File('${walletNameDirectory.path}/wallet.dewif'); + Future storeWallet(NewWallet wallet, String _name, BuildContext context, + {bool isHD = false}) async { + int nbrWallet = 0; + Directory walletNbrDirectory; + do { + nbrWallet++; + walletNbrDirectory = Directory('${walletsDirectory.path}/$nbrWallet'); + } while (await walletNbrDirectory.exists()); - if (await walletNameDirectory.exists()) { - print('Ce wallet existe déjà, impossible de le créer.'); - _showWalletExistDialog(context); - return 'Exist: DENY'; + final walletFile = File('${walletNbrDirectory.path}/wallet.dewif'); + + await walletNbrDirectory.create(); + await walletFile.writeAsString(wallet.dewif); + + final configFile = File('${walletNbrDirectory.path}/config.txt'); + + if (isHD) { + final int _derivationNbr = 3; + await configFile.writeAsString('$nbrWallet:$_name:$_derivationNbr'); + } else { + await configFile.writeAsString('$nbrWallet:$_name'); } - await walletNameDirectory.create(); - await walletFile.writeAsString('${wallet.dewif}'); - Navigator.pop(context, true); Navigator.pop(context, true); // notifyListeners(); @@ -105,35 +113,6 @@ class GenerateWalletsProvider with ChangeNotifier { notifyListeners(); } - Future _showWalletExistDialog(BuildContext context) async { - return showDialog( - context: context, - barrierDismissible: false, // user must tap button! - builder: (BuildContext context) { - return AlertDialog( - title: Text('Ce nom existe déjà'), - content: SingleChildScrollView( - child: ListBody( - children: [ - Text('Veuillez choisir un autre nom pour votre portefeuille.'), - ], - ), - ), - actions: [ - TextButton( - child: Text("J'ai compris"), - onPressed: () { - Navigator.of(context).pop(); - askedWordColor = Colors.green[500]; - isAskedWordValid = true; - }, - ), - ], - ); - }, - ); - } - Future generateMnemonic() async { try { generatedMnemonic = await DubpRust.genMnemonic(language: Language.french); diff --git a/lib/models/myWallets.dart b/lib/models/myWallets.dart index 7fefaac..c4bc26d 100644 --- a/lib/models/myWallets.dart +++ b/lib/models/myWallets.dart @@ -33,16 +33,10 @@ class MyWalletsProvider with ChangeNotifier { // int i = 0; walletsDirectory .listSync(recursive: false, followLinks: false) - .forEach((wallet) { - String _name = wallet.path.split('/').last; - List _pubkeyList = File(wallet.path + '/pubkey').readAsLinesSync(); - String _pubkey = _pubkeyList[0]; - listWallets[_name] = _pubkey; - // i++; - - // for (var _wallets in listWallets) { - // _wallets.pubkey = - // } + .forEach((_wallet) { + File('${_wallet.path}/config.txt').readAsLinesSync().forEach((element) { + listWallets[int.parse(element.split(':')[0])] = element.split(':')[1]; + }); }); return listWallets; } @@ -72,8 +66,8 @@ class MyWalletsProvider with ChangeNotifier { MyWalletsProvider _myWalletProvider = Provider.of(context); return AlertDialog( - title: Text( - 'Êtes-vous sûr de vouloir supprimer tous vos trousseaux ?'), + title: + Text('Êtes-vous sûr de vouloir supprimer tous vos trousseaux ?'), content: SingleChildScrollView(child: Text('')), actions: [ TextButton( diff --git a/lib/models/walletOptions.dart b/lib/models/walletOptions.dart index 58abd11..84764ef 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, _pinLenght) async { + Future _getPubkeyFromDewif(_dewif, _pin, _pinLenght, {derivation}) async { String _pubkey; RegExp regExp = new RegExp( r'^[A-Z0-9]+$', @@ -30,7 +30,9 @@ class WalletOptionsProvider with ChangeNotifier { return 'false'; } try { - _pubkey = await DubpRust.getDewifPublicKey(dewif: _dewif, pin: _pin); + List _pubkeysTmp = await DubpRust.getBip32DewifAccountsPublicKeys( + dewif: _dewif, secretCode: _pin, accountsIndex: [3]); + _pubkey = _pubkeysTmp[0]; this.pubkey.text = _pubkey; notifyListeners(); @@ -50,10 +52,12 @@ class WalletOptionsProvider with ChangeNotifier { } } - Future readLocalWallet(String _name, String _pin, _pinLenght) async { + Future readLocalWallet( + int _walletNbr, String _name, String _pin, _pinLenght) async { isWalletUnlock = false; try { - File _walletFile = File('${walletsDirectory.path}/$_name/wallet.dewif'); + File _walletFile = + File('${walletsDirectory.path}/$_walletNbr/wallet.dewif'); String _localDewif = await _walletFile.readAsString(); String _localPubkey; @@ -79,8 +83,9 @@ class WalletOptionsProvider with ChangeNotifier { } } - int getPinLenght(_name) { - File _walletFile = File('${walletsDirectory.path}/$_name/wallet.dewif'); + int getPinLenght(_walletNbr) { + File _walletFile = + File('${walletsDirectory.path}/$_walletNbr/wallet.dewif'); String _localDewif = _walletFile.readAsStringSync(); final int _pinLenght = DubpRust.getDewifSecretCodeLen( diff --git a/lib/screens/myWallets/walletOptions.dart b/lib/screens/myWallets/walletOptions.dart index 8ff6320..6a8a775 100644 --- a/lib/screens/myWallets/walletOptions.dart +++ b/lib/screens/myWallets/walletOptions.dart @@ -11,8 +11,10 @@ import 'package:flutter/services.dart'; // ignore: must_be_immutable class WalletOptions extends StatelessWidget with ChangeNotifier { - WalletOptions({Key keyMyWallets, @required this.walletName}) + WalletOptions( + {Key keyMyWallets, @required this.walletNbr, @required this.walletName}) : super(key: keyMyWallets); + int walletNbr; String walletName; StreamController errorController; @@ -34,7 +36,7 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { errorController = StreamController(); // _walletOptions.isWalletUnlock = false; - final int _pinLenght = _walletOptions.getPinLenght(this.walletName); + final int _pinLenght = _walletOptions.getPinLenght(this.walletNbr); return WillPopScope( onWillPop: () { @@ -244,6 +246,7 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { print("Completed"); final resultWallet = await _walletOptions.readLocalWallet( + this.walletNbr, this.walletName, _pin.toUpperCase(), _pinLenght); diff --git a/lib/screens/myWallets/walletsHome.dart b/lib/screens/myWallets/walletsHome.dart index c88cd85..cec0c8e 100644 --- a/lib/screens/myWallets/walletsHome.dart +++ b/lib/screens/myWallets/walletsHome.dart @@ -90,8 +90,8 @@ class WalletsHome extends StatelessWidget { Provider.of(context); List _listWallets = []; - myWalletProvider.listWallets.forEach((_name, _pubkey) { - _listWallets.add(_name); + myWalletProvider.listWallets.forEach((_nbr, _name) { + _listWallets.add('$_nbr:$_name'); }); return Column(children: [ @@ -102,13 +102,14 @@ class WalletsHome extends StatelessWidget { leading: Padding( padding: const EdgeInsets.all(15.0), child: Text("0 Ğ1", style: TextStyle(fontSize: 14.0))), - title: Text(_repository, style: TextStyle(fontSize: 16.0)), - subtitle: Text(myWalletProvider.listWallets[_repository], - style: TextStyle(fontSize: 11.0)), + title: + Text(_repository.split(':')[1], style: TextStyle(fontSize: 16.0)), dense: true, onTap: () { Navigator.push(context, MaterialPageRoute(builder: (context) { - return WalletOptions(walletName: _repository); + return WalletOptions( + walletNbr: int.parse(_repository.split(':')[0]), + walletName: _repository.split(':')[1]); })); }, ) From f430facfb5d493e780c8758174513b50f2c6cd3d Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 15 Feb 2021 01:44:25 +0100 Subject: [PATCH 3/5] HQ Wallet workflow is complete --- lib/models/myWallets.dart | 41 +++++++++++-- lib/models/walletOptions.dart | 81 ++++++++++++++----------- lib/screens/myWallets/confirmWalletStorage.dart | 3 +- lib/screens/myWallets/walletOptions.dart | 45 +++++--------- lib/screens/myWallets/walletsHome.dart | 65 ++++++++++++++++---- 5 files changed, 152 insertions(+), 83 deletions(-) diff --git a/lib/models/myWallets.dart b/lib/models/myWallets.dart index c4bc26d..f1d1faa 100644 --- a/lib/models/myWallets.dart +++ b/lib/models/myWallets.dart @@ -6,7 +6,7 @@ import 'package:gecko/globals.dart'; import 'package:provider/provider.dart'; class MyWalletsProvider with ChangeNotifier { - Map listWallets = Map(); + String listWallets; bool checkIfWalletExist() { if (appPath == null) { @@ -25,19 +25,28 @@ class MyWalletsProvider with ChangeNotifier { Future importWallet() async {} - Map getAllWalletsNames() { - if (listWallets.isNotEmpty) { - listWallets.clear(); + String getAllWalletsNames() { + if (listWallets != null && listWallets.isNotEmpty) { + listWallets = ''; + } + if (listWallets == null) { + listWallets = ''; } // int i = 0; walletsDirectory .listSync(recursive: false, followLinks: false) .forEach((_wallet) { - File('${_wallet.path}/config.txt').readAsLinesSync().forEach((element) { - listWallets[int.parse(element.split(':')[0])] = element.split(':')[1]; + File _walletConfig = File('${_wallet.path}/config.txt'); + _walletConfig.readAsLinesSync().forEach((element) { + if (listWallets != '') { + listWallets += '\n'; + } + listWallets += + "${element.split(':')[0]}:${element.split(':')[1]}:${element.split(':')[2]}"; }); }); + return listWallets; } @@ -93,6 +102,26 @@ class MyWalletsProvider with ChangeNotifier { ); } + Future generateNewDerivation( + context, String _name, int _walletNbr) async { + final _walletConfig = + File('${walletsDirectory.path}/$_walletNbr/config.txt'); + + String _lastWallet = + await _walletConfig.readAsLines().then((value) => value.last); + int _lastDerivation = int.parse(_lastWallet.split(':')[2]); + // print(_lastDerivation); + int _newDerivationNbr = _lastDerivation + 3; + + await _walletConfig.writeAsString('\n$_walletNbr:$_name:$_newDerivationNbr', + mode: FileMode.append); + + print(await _walletConfig.readAsString()); + notifyListeners(); + + Navigator.pop(context); + } + void rebuildWidget() { notifyListeners(); } diff --git a/lib/models/walletOptions.dart b/lib/models/walletOptions.dart index 84764ef..65c15a0 100644 --- a/lib/models/walletOptions.dart +++ b/lib/models/walletOptions.dart @@ -15,7 +15,8 @@ class WalletOptionsProvider with ChangeNotifier { Future get badWallet => null; - Future _getPubkeyFromDewif(_dewif, _pin, _pinLenght, {derivation}) async { + Future _getPubkeyFromDewif( + String _dewif, _pin, int _pinLenght, int derivation) async { String _pubkey; RegExp regExp = new RegExp( r'^[A-Z0-9]+$', @@ -31,7 +32,7 @@ class WalletOptionsProvider with ChangeNotifier { } try { List _pubkeysTmp = await DubpRust.getBip32DewifAccountsPublicKeys( - dewif: _dewif, secretCode: _pin, accountsIndex: [3]); + dewif: _dewif, secretCode: _pin, accountsIndex: [derivation]); _pubkey = _pubkeysTmp[0]; this.pubkey.text = _pubkey; notifyListeners(); @@ -52,8 +53,8 @@ class WalletOptionsProvider with ChangeNotifier { } } - Future readLocalWallet( - int _walletNbr, String _name, String _pin, _pinLenght) async { + Future readLocalWallet(int _walletNbr, String _name, String _pin, + int _pinLenght, int derivation) async { isWalletUnlock = false; try { File _walletFile = @@ -61,16 +62,12 @@ class WalletOptionsProvider with ChangeNotifier { String _localDewif = await _walletFile.readAsString(); String _localPubkey; - if ((_localPubkey = - await _getPubkeyFromDewif(_localDewif, _pin, _pinLenght)) != + if ((_localPubkey = await _getPubkeyFromDewif( + _localDewif, _pin, _pinLenght, derivation)) != 'false') { this.pubkey.text = _localPubkey; isWalletUnlock = true; notifyListeners(); - print('GET BIP32 accounts publickeys from this dewif'); - List _hdWallets = await DubpRust.getBip32DewifAccountsPublicKeys( - dewif: _localDewif, secretCode: _pin, accountsIndex: [0, 1, 2]); - print(_hdWallets); return _localDewif; } else { @@ -94,17 +91,27 @@ class WalletOptionsProvider with ChangeNotifier { return _pinLenght; } - Future _renameWallet(_walletName, _newName) async { - final _walletFile = Directory('${walletsDirectory.path}/$_walletName'); + Future _renameWallet(_walletName, _newName, _walletNbr, _derivation) async { + final _walletConfig = + File('${walletsDirectory.path}/$_walletNbr/config.txt'); - try { - _walletFile.rename('${walletsDirectory.path}/$_newName'); - } catch (e) { - print('ERREUR lors du renommage du wallet: $e'); - } + String newConfig = + await _walletConfig.readAsLines().then((List lines) { + int _index = lines.indexOf('$_walletNbr:$_walletName:$_derivation'); + lines.removeWhere( + (element) => element == '$_walletNbr:$_walletName:$_derivation'); + lines.insert(_index, '$_walletNbr:$_newName:$_derivation'); + + return lines.join('\n'); + }); + + await _walletConfig.delete(); + await _walletConfig.writeAsString(newConfig); + _newWalletName.text = ''; } - Future renameWalletAlerte(context, _walletName) async { + Future renameWalletAlerte( + context, _walletName, _walletNbr, _derivation) async { return showDialog( context: context, barrierDismissible: true, // user must tap button! @@ -130,12 +137,12 @@ class WalletOptionsProvider with ChangeNotifier { TextButton( child: Text("Valider"), onPressed: () { - WidgetsBinding.instance.addPostFrameCallback((_) { - _renameWallet(_walletName, this._newWalletName.text); + WidgetsBinding.instance.addPostFrameCallback((_) async { + await _renameWallet(_walletName, this._newWalletName.text, + _walletNbr, _derivation); }); // notifyListeners(); Navigator.pop(context, true); - Navigator.pop(context, true); }, ), ], @@ -144,21 +151,26 @@ class WalletOptionsProvider with ChangeNotifier { ); } - Future deleteWallet(context, _name) async { - try { - final _walletFile = Directory('${walletsDirectory.path}/$_name'); - print('DELETE THAT ?: $_walletFile'); + Future deleteWallet(context, _walletNbr, _name, _derivation) async { + final bool _answer = await _confirmDeletingWallet(context, _name); - final bool _answer = await _confirmDeletingWallet(context, _name); + if (_answer) { + final _walletConfig = + File('${walletsDirectory.path}/$_walletNbr/config.txt'); - if (_answer) { - await _walletFile.delete(recursive: true); - Navigator.pop(context); - } - return 0; - } catch (e) { - return 1; + String newConfig = + await _walletConfig.readAsLines().then((List lines) { + lines.removeWhere( + (element) => element == '$_walletNbr:$_name:$_derivation'); + + return lines.join('\n'); + }); + + await _walletConfig.delete(); + await _walletConfig.writeAsString(newConfig); + Navigator.pop(context); } + return 0; } Future _confirmDeletingWallet(context, _walletName) async { @@ -172,8 +184,7 @@ class WalletOptionsProvider with ChangeNotifier { content: SingleChildScrollView( child: ListBody( children: [ - Text( - 'Vous pourrez restaurer ce portefeuille à tout moment grace à votre phrase de restauration.'), + Text('Vous pourrez restaurer ce portefeuille plus tard.'), ], ), ), diff --git a/lib/screens/myWallets/confirmWalletStorage.dart b/lib/screens/myWallets/confirmWalletStorage.dart index 0130fa8..7bcc095 100644 --- a/lib/screens/myWallets/confirmWalletStorage.dart +++ b/lib/screens/myWallets/confirmWalletStorage.dart @@ -128,7 +128,8 @@ class ConfirmStoreWallet extends StatelessWidget with ChangeNotifier { await _generateWalletProvider.storeWallet( generatedWallet, walletName.text, - context); + context, + isHD: true); _generateWalletProvider.isAskedWordValid = false; _generateWalletProvider.askedWordColor = diff --git a/lib/screens/myWallets/walletOptions.dart b/lib/screens/myWallets/walletOptions.dart index 6a8a775..0f21b6f 100644 --- a/lib/screens/myWallets/walletOptions.dart +++ b/lib/screens/myWallets/walletOptions.dart @@ -3,7 +3,6 @@ import 'package:flutter/material.dart'; import 'package:dubp/dubp.dart'; import 'package:gecko/models/myWallets.dart'; import 'package:gecko/models/walletOptions.dart'; -import 'package:gecko/screens/myWallets/changePin.dart'; import 'dart:async'; import 'package:pin_code_fields/pin_code_fields.dart'; import 'package:provider/provider.dart'; @@ -12,10 +11,14 @@ import 'package:flutter/services.dart'; // ignore: must_be_immutable class WalletOptions extends StatelessWidget with ChangeNotifier { WalletOptions( - {Key keyMyWallets, @required this.walletNbr, @required this.walletName}) + {Key keyMyWallets, + @required this.walletNbr, + @required this.walletName, + @required this.derivation}) : super(key: keyMyWallets); int walletNbr; String walletName; + int derivation; StreamController errorController; TextEditingController _enterPin = TextEditingController(); @@ -102,7 +105,10 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { ), onPressed: () => _walletOptions .renameWalletAlerte( - context, walletName) + context, + walletName, + walletNbr, + derivation) .then((_result) { if (_result == true) { WidgetsBinding.instance @@ -115,6 +121,8 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { _myWalletProvider .rebuildWidget(); }); + Navigator.pop( + context, true); } }), child: Text( @@ -127,38 +135,14 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { width: 300, child: ElevatedButton( style: ElevatedButton.styleFrom( - elevation: 5, - primary: Color( - 0xffFFD68E), //Color(0xffFFD68E), // background - onPrimary: Colors.black, // foreground - ), - onPressed: () { - // changePin(widget.walletName, this.walletPin); - Navigator.push( - context, - MaterialPageRoute(builder: (context) { - return ChangePinScreen( - walletName: walletName, - oldPin: this.walletPin); - }), - ); - }, - child: Text('Changer mon code secret', - style: TextStyle(fontSize: 20)))), - SizedBox(height: 30), - SizedBox( - height: 50, - width: 300, - child: ElevatedButton( - style: ElevatedButton.styleFrom( elevation: 6, primary: Colors .redAccent, //Color(0xffFFD68E), // background onPrimary: Colors.black, // foreground ), onPressed: () async { - await _walletOptions.deleteWallet( - context, walletName); + await _walletOptions.deleteWallet(context, + walletNbr, walletName, derivation); WidgetsBinding.instance .addPostFrameCallback((_) { _myWalletProvider.listWallets = @@ -249,7 +233,8 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { this.walletNbr, this.walletName, _pin.toUpperCase(), - _pinLenght); + _pinLenght, + this.derivation); if (resultWallet == 'bad') { errorController.add(ErrorAnimationType .shake); // Triggering error shake animation diff --git a/lib/screens/myWallets/walletsHome.dart b/lib/screens/myWallets/walletsHome.dart index cec0c8e..b5a2d91 100644 --- a/lib/screens/myWallets/walletsHome.dart +++ b/lib/screens/myWallets/walletsHome.dart @@ -7,6 +7,9 @@ import 'package:provider/provider.dart'; // ignore: must_be_immutable class WalletsHome extends StatelessWidget { + final _derivationKey = GlobalKey(); + final TextEditingController _newDerivationName = TextEditingController(); + @override Widget build(BuildContext context) { MyWalletsProvider myWalletProvider = @@ -27,12 +30,11 @@ class WalletsHome extends StatelessWidget { child: FloatingActionButton( heroTag: "buttonGenerateWallet", onPressed: () { - Navigator.push( - context, - MaterialPageRoute(builder: (context) { - return GenerateWalletsScreen(); - }), - ); + showDialog( + context: context, + builder: (BuildContext context) { + return addNewDerivation(context, 1); + }); }, child: Container( height: 40.0, @@ -89,10 +91,7 @@ class WalletsHome extends StatelessWidget { MyWalletsProvider myWalletProvider = Provider.of(context); - List _listWallets = []; - myWalletProvider.listWallets.forEach((_nbr, _name) { - _listWallets.add('$_nbr:$_name'); - }); + List _listWallets = myWalletProvider.listWallets.split('\n'); return Column(children: [ SizedBox(height: 8), @@ -109,10 +108,54 @@ class WalletsHome extends StatelessWidget { Navigator.push(context, MaterialPageRoute(builder: (context) { return WalletOptions( walletNbr: int.parse(_repository.split(':')[0]), - walletName: _repository.split(':')[1]); + walletName: _repository.split(':')[1], + derivation: int.parse(_repository.split(':')[2])); })); }, ) ]); } + + Widget addNewDerivation(context, int _walletNbr) { + MyWalletsProvider _myWalletProvider = + Provider.of(context); + return AlertDialog( + content: Stack( + overflow: Overflow.visible, + children: [ + Form( + key: _derivationKey, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text('Nom du portefeuille:'), + Padding( + padding: EdgeInsets.all(8.0), + child: TextFormField( + controller: _newDerivationName, + textAlign: TextAlign.center, + autofocus: true, + ), + ), + SizedBox(height: 20), + Padding( + padding: const EdgeInsets.all(8.0), + child: RaisedButton( + child: Text("Créer"), + color: Color(0xffFFD68E), + onPressed: () async { + await _myWalletProvider + .generateNewDerivation( + context, _newDerivationName.text, _walletNbr) + .then((_) => _newDerivationName.text == ''); + }, + ), + ) + ], + ), + ), + ], + ), + ); + } } From 6891df981ec92085e908b152ce6e3e7f24e19479 Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 15 Feb 2021 02:37:00 +0100 Subject: [PATCH 4/5] Fix scrollable wallet list --- lib/screens/myWallets/walletsHome.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/screens/myWallets/walletsHome.dart b/lib/screens/myWallets/walletsHome.dart index b5a2d91..74f42b0 100644 --- a/lib/screens/myWallets/walletsHome.dart +++ b/lib/screens/myWallets/walletsHome.dart @@ -93,7 +93,8 @@ class WalletsHome extends StatelessWidget { List _listWallets = myWalletProvider.listWallets.split('\n'); - return Column(children: [ + return Expanded( + child: ListView(children: [ SizedBox(height: 8), for (String _repository in _listWallets) ListTile( @@ -113,7 +114,7 @@ class WalletsHome extends StatelessWidget { })); }, ) - ]); + ])); } Widget addNewDerivation(context, int _walletNbr) { From c4d774e256c0ae0e0da187660a95d0d322d6c280 Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 15 Feb 2021 03:59:07 +0100 Subject: [PATCH 5/5] All pubkey in monospace everywhere; Fix delete all derivations; Fix delete all pubkey refresh --- lib/models/generateWallets.dart | 9 ++++++++- lib/models/myWallets.dart | 24 +++++++++++++++++------- lib/models/walletOptions.dart | 2 +- lib/screens/history.dart | 7 +++++-- lib/screens/myWallets/walletsHome.dart | 28 +++++++++++++++++++++++----- lib/screens/settings.dart | 4 ++-- 6 files changed, 56 insertions(+), 18 deletions(-) diff --git a/lib/models/generateWallets.dart b/lib/models/generateWallets.dart index cc9dd25..05e27e1 100644 --- a/lib/models/generateWallets.dart +++ b/lib/models/generateWallets.dart @@ -46,7 +46,14 @@ class GenerateWalletsProvider with ChangeNotifier { if (isHD) { final int _derivationNbr = 3; - await configFile.writeAsString('$nbrWallet:$_name:$_derivationNbr'); + List _pubkeysTmp = await DubpRust.getBip32DewifAccountsPublicKeys( + dewif: wallet.dewif, + secretCode: wallet.pin, + accountsIndex: [_derivationNbr]); + String _pubkey = _pubkeysTmp[0]; + + await configFile + .writeAsString('$nbrWallet:$_name:$_derivationNbr:$_pubkey'); } else { await configFile.writeAsString('$nbrWallet:$_name'); } diff --git a/lib/models/myWallets.dart b/lib/models/myWallets.dart index f1d1faa..03d0072 100644 --- a/lib/models/myWallets.dart +++ b/lib/models/myWallets.dart @@ -26,6 +26,11 @@ class MyWalletsProvider with ChangeNotifier { Future importWallet() async {} String getAllWalletsNames() { + final bool _isWalletsExists = checkIfWalletExist(); + if (!_isWalletsExists) { + return ''; + } + if (listWallets != null && listWallets.isNotEmpty) { listWallets = ''; } @@ -42,8 +47,8 @@ class MyWalletsProvider with ChangeNotifier { if (listWallets != '') { listWallets += '\n'; } - listWallets += - "${element.split(':')[0]}:${element.split(':')[1]}:${element.split(':')[2]}"; + listWallets += element; + // listWallets += "${element.split(':')[0]}:${element.split(':')[1]}:${element.split(':')[2]}" }); }); @@ -59,6 +64,7 @@ class MyWalletsProvider with ChangeNotifier { if (_answer) { await walletsDirectory.delete(recursive: true); await walletsDirectory.create(); + notifyListeners(); Navigator.pop(context); } return 0; @@ -104,14 +110,18 @@ class MyWalletsProvider with ChangeNotifier { Future generateNewDerivation( context, String _name, int _walletNbr) async { + int _newDerivationNbr; final _walletConfig = File('${walletsDirectory.path}/$_walletNbr/config.txt'); - String _lastWallet = - await _walletConfig.readAsLines().then((value) => value.last); - int _lastDerivation = int.parse(_lastWallet.split(':')[2]); - // print(_lastDerivation); - int _newDerivationNbr = _lastDerivation + 3; + if (await _walletConfig.readAsString() == '') { + _newDerivationNbr = 3; + } else { + String _lastWallet = + await _walletConfig.readAsLines().then((value) => value.last); + int _lastDerivation = int.parse(_lastWallet.split(':')[2]); + _newDerivationNbr = _lastDerivation + 3; + } await _walletConfig.writeAsString('\n$_walletNbr:$_name:$_newDerivationNbr', mode: FileMode.append); diff --git a/lib/models/walletOptions.dart b/lib/models/walletOptions.dart index 65c15a0..3fd5257 100644 --- a/lib/models/walletOptions.dart +++ b/lib/models/walletOptions.dart @@ -161,7 +161,7 @@ class WalletOptionsProvider with ChangeNotifier { String newConfig = await _walletConfig.readAsLines().then((List lines) { lines.removeWhere( - (element) => element == '$_walletNbr:$_name:$_derivation'); + (element) => element.contains('$_walletNbr:$_name:$_derivation')); return lines.join('\n'); }); diff --git a/lib/screens/history.dart b/lib/screens/history.dart index 883068c..1297086 100644 --- a/lib/screens/history.dart +++ b/lib/screens/history.dart @@ -78,7 +78,10 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { errorBorder: InputBorder.none, disabledBorder: InputBorder.none, ), - style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.bold)), + style: TextStyle( + fontSize: 14.0, + fontWeight: FontWeight.bold, + fontFamily: 'Monospace')), if (_historyProvider.pubkey != '') historyQuery(context, _historyProvider), ])); @@ -273,7 +276,7 @@ class HistoryScreen extends StatelessWidget with ChangeNotifier { subtitle: Text( truncate(repository[2], 20, omission: "...", position: TruncatePosition.end), - style: TextStyle(fontSize: 11.0), + style: TextStyle(fontSize: 11.0, fontFamily: 'Monospace'), textAlign: TextAlign.center), trailing: Text("${repository[3]} Ğ1", style: TextStyle(fontSize: 14.0), diff --git a/lib/screens/myWallets/walletsHome.dart b/lib/screens/myWallets/walletsHome.dart index 74f42b0..c593dd1 100644 --- a/lib/screens/myWallets/walletsHome.dart +++ b/lib/screens/myWallets/walletsHome.dart @@ -8,7 +8,6 @@ import 'package:provider/provider.dart'; // ignore: must_be_immutable class WalletsHome extends StatelessWidget { final _derivationKey = GlobalKey(); - final TextEditingController _newDerivationName = TextEditingController(); @override Widget build(BuildContext context) { @@ -88,20 +87,37 @@ class WalletsHome extends StatelessWidget { } Widget myWalletsList(BuildContext context) { - MyWalletsProvider myWalletProvider = + MyWalletsProvider _myWalletProvider = Provider.of(context); - List _listWallets = myWalletProvider.listWallets.split('\n'); + final bool isWalletsExists = _myWalletProvider.checkIfWalletExist(); + + if (!isWalletsExists) { + return Text(''); + } + + if (_myWalletProvider.listWallets == '') { + return Expanded( + child: Center( + child: Text( + 'Veuillez générer votre premier portefeuille', + style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500), + ))); + } + + List _listWallets = _myWalletProvider.listWallets.split('\n'); return Expanded( child: ListView(children: [ SizedBox(height: 8), for (String _repository in _listWallets) ListTile( - contentPadding: const EdgeInsets.all(5.0), + contentPadding: const EdgeInsets.only(left: 7.0), leading: Padding( - padding: const EdgeInsets.all(15.0), + padding: const EdgeInsets.all(6.0), child: Text("0 Ğ1", style: TextStyle(fontSize: 14.0))), + // subtitle: Text(_repository.split(':')[3], + // style: TextStyle(fontSize: 12.0, fontFamily: 'Monospace')), title: Text(_repository.split(':')[1], style: TextStyle(fontSize: 16.0)), dense: true, @@ -118,8 +134,10 @@ class WalletsHome extends StatelessWidget { } Widget addNewDerivation(context, int _walletNbr) { + final TextEditingController _newDerivationName = TextEditingController(); MyWalletsProvider _myWalletProvider = Provider.of(context); + return AlertDialog( content: Stack( overflow: Overflow.visible, diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 736b6bc..5d59521 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -42,9 +42,9 @@ class SettingsScreen extends StatelessWidget { .redAccent, //Color(0xffFFD68E), // background onPrimary: Colors.black, // foreground ), - onPressed: () => { + onPressed: () async => { print('Suppression de tous les wallets'), - _myWallets.deleteAllWallet(context) + await _myWallets.deleteAllWallet(context) }, child: Text( "EFFACER TOUS MES PORTEFEUILLES, LE TEMPS DE L'ALPHA",