From 5ec6b715b0c6fc3fc271c7f147b45dfbf12f5ee0 Mon Sep 17 00:00:00 2001 From: poka Date: Sun, 14 Feb 2021 23:17:03 +0100 Subject: [PATCH] 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]); })); }, )