diff --git a/lib/ui/myWallets/changePin.dart b/lib/ui/myWallets/changePin.dart new file mode 100644 index 0000000..43d65ef --- /dev/null +++ b/lib/ui/myWallets/changePin.dart @@ -0,0 +1,135 @@ +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 ChangePinScreen extends StatefulWidget { + const ChangePinScreen( + {Key keyMyWallets, @required this.walletName, @required this.oldPin}) + : super(key: keyMyWallets); + + final String walletName; + final oldPin; + @override + ChangePinScreenState createState() => ChangePinScreenState(); +} + +class ChangePinScreenState extends State { + Directory appPath; + TextEditingController _newPin = new TextEditingController(); + bool ischangedPin = false; + NewWallet newWalletFile; + + Future get badWallet => null; + + void initState() { + super.initState(); + DubpRust.setup(); + changePin(widget.walletName, widget.oldPin); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + resizeToAvoidBottomInset: false, + appBar: AppBar( + title: SizedBox( + height: 25, + child: Text(widget.walletName), + )), + body: Center( + child: SafeArea( + child: Column(children: [ + SizedBox(height: 80), + Text( + 'Veuillez tapper votre code secret pour en générer un nouveau :', + 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: this._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: () { + changePin(widget.walletName, widget.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: (ischangedPin) + ? () => storeWallet(widget.walletName) + : null, + child: Text('Confirmer', style: TextStyle(fontSize: 28))), + ) + ])))); + } + + Future changePin(_name, _oldPin) async { + try { + final appPath = await _localPath; + final _walletFile = Directory('$appPath/wallets/$_name'); + final _dewif = + File(_walletFile.path + '/wallet.dewif').readAsLinesSync()[0]; + + newWalletFile = await DubpRust.changeDewifPin( + dewif: _dewif, + oldPin: _oldPin, + ); + + _newPin.text = newWalletFile.pin; + ischangedPin = true; + setState(() {}); + return newWalletFile; + } catch (e) { + print('Impossible de changer le code PIN.'); + return badWallet; + } + } + + Future storeWallet(_name) async { + final appPath = await _localPath; + final Directory walletNameDirectory = Directory('$appPath/wallets/$_name'); + final walletFile = File('${walletNameDirectory.path}/wallet.dewif'); + + walletFile.writeAsString('${this.newWalletFile.dewif}'); + + Navigator.pop(context); + + return _name; + } + + Future get _localPath async { + final directory = await getApplicationDocumentsDirectory(); + return directory.path; + } +} diff --git a/lib/ui/myWallets/walletOptions.dart b/lib/ui/myWallets/walletOptions.dart index b0e43c7..9d2083c 100644 --- a/lib/ui/myWallets/walletOptions.dart +++ b/lib/ui/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/ui/myWallets/changePin.dart'; import 'dart:io'; import 'dart:async'; import 'package:path_provider/path_provider.dart'; @@ -26,6 +27,7 @@ class WalletOptionsState extends State { String validPin = 'NO PIN'; var pinColor = Color(0xffF9F9F1); bool isWalletUnlock = false; + var walletPin = ''; Future get badWallet => null; @@ -82,7 +84,15 @@ class WalletOptionsState extends State { onPrimary: Colors.black, // foreground ), onPressed: () { - changePin(widget.walletName, '_pin'); + // changePin(widget.walletName, this.walletPin); + Navigator.push( + context, + MaterialPageRoute(builder: (context) { + return ChangePinScreen( + walletName: widget.walletName, + oldPin: this.walletPin); + }), + ); }, child: Text('Changer mon code secret', style: TextStyle(fontSize: 20)))))), @@ -187,6 +197,7 @@ class WalletOptionsState extends State { pinColor = Colors.green[200]; // setState(() {}); // await Future.delayed(Duration(milliseconds: 50)); + this.walletPin = _pin.toUpperCase(); isWalletUnlock = true; setState(() {}); } @@ -265,25 +276,6 @@ class WalletOptionsState extends State { } } - Future changePin(_name, _oldPin) async { - try { - final appPath = await _localPath; - final _walletFile = Directory('$appPath/wallets/$_name'); - final _dewif = - File(_walletFile.path + '/wallet.dewif').readAsLinesSync()[0]; - - final NewWallet _newWalletFile = await DubpRust.changeDewifPin( - dewif: _dewif, - oldPin: _oldPin, - ); - - return _newWalletFile; - } catch (e) { - print('Impossible de changer le code PIN.'); - return badWallet; - } - } - Future deleteWallet(_name) async { try { final appPath = await _localPath; diff --git a/lib/ui/myWallets/walletsHome.dart b/lib/ui/myWallets/walletsHome.dart index 62c2b3d..d2feac2 100644 --- a/lib/ui/myWallets/walletsHome.dart +++ b/lib/ui/myWallets/walletsHome.dart @@ -55,7 +55,10 @@ class WalletsHomeState extends State { MaterialPageRoute(builder: (context) { return GenerateWalletsScreen(); }), - ); + ).then((value) => setState(() { + this.newWalletName = value; + checkIfWalletExist(); + })); }, child: Container( height: 40.0,