diff --git a/lib/models/generateWallets.dart b/lib/models/generateWallets.dart index ea67e0f186326d32a46678b2008b75b4fb5c607b..cc9dd25b5e2c4c6c6eba499edab7da89ac5b7d8d 100644 --- a/lib/models/generateWallets.dart +++ b/lib/models/generateWallets.dart @@ -26,22 +26,30 @@ 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'); - await walletNameDirectory.create(); - await walletFile.writeAsString('${wallet.dewif}'); + if (isHD) { + final int _derivationNbr = 3; + await configFile.writeAsString('$nbrWallet:$_name:$_derivationNbr'); + } else { + await configFile.writeAsString('$nbrWallet:$_name'); + } Navigator.pop(context, true); Navigator.pop(context, true); @@ -105,35 +113,6 @@ class GenerateWalletsProvider with ChangeNotifier { notifyListeners(); } - Future<void> _showWalletExistDialog(BuildContext context) async { - return showDialog<void>( - 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: <Widget>[ - Text('Veuillez choisir un autre nom pour votre portefeuille.'), - ], - ), - ), - actions: <Widget>[ - TextButton( - child: Text("J'ai compris"), - onPressed: () { - Navigator.of(context).pop(); - askedWordColor = Colors.green[500]; - isAskedWordValid = true; - }, - ), - ], - ); - }, - ); - } - Future<String> generateMnemonic() async { try { generatedMnemonic = await DubpRust.genMnemonic(language: Language.french); diff --git a/lib/models/myWallets.dart b/lib/models/myWallets.dart index 7fefaac17d487b495c7c469fcdaa2f63c62091c2..c4bc26d9cef1dc6bbf5dcfab6ee6a295769f47bd 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<MyWalletsProvider>(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: <Widget>[ TextButton( diff --git a/lib/models/walletOptions.dart b/lib/models/walletOptions.dart index 58abd1149c6f0ab490647909153976121e5d1b64..84764efbfdb037ef845238fd375e6162993cc8c5 100644 --- a/lib/models/walletOptions.dart +++ b/lib/models/walletOptions.dart @@ -15,7 +15,7 @@ class WalletOptionsProvider with ChangeNotifier { Future<NewWallet> 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 8ff63200914980979ddce4f8b1769ebcb434046c..6a8a775e7e7e7fc18c47c1ca1f898f40676559ce 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<ErrorAnimationType> errorController; @@ -34,7 +36,7 @@ class WalletOptions extends StatelessWidget with ChangeNotifier { errorController = StreamController<ErrorAnimationType>(); // _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 c88cd85c3093b5e17bfa60ef2468452434b16f75..cec0c8e6989fcc55fc6664e78211a2403b1f919e 100644 --- a/lib/screens/myWallets/walletsHome.dart +++ b/lib/screens/myWallets/walletsHome.dart @@ -90,8 +90,8 @@ class WalletsHome extends StatelessWidget { Provider.of<MyWalletsProvider>(context); List _listWallets = []; - myWalletProvider.listWallets.forEach((_name, _pubkey) { - _listWallets.add(_name); + myWalletProvider.listWallets.forEach((_nbr, _name) { + _listWallets.add('$_nbr:$_name'); }); return Column(children: <Widget>[ @@ -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]); })); }, )