2021-01-10 07:49:29 +01:00
import ' package:gecko/ui/myWallets.dart ' ;
2021-01-06 21:17:01 +01:00
import ' package:flutter/foundation.dart ' ;
2021-01-06 05:04:54 +01:00
import ' package:flutter/material.dart ' ;
import ' package:dubp/dubp.dart ' ;
import ' package:sentry/sentry.dart ' as sentry ;
2021-01-06 21:17:01 +01:00
import ' dart:io ' ;
2021-01-08 05:02:27 +01:00
import ' dart:async ' ;
2021-01-06 21:17:01 +01:00
import ' package:path_provider/path_provider.dart ' ;
2021-01-06 05:04:54 +01:00
class GenerateWalletScreen extends StatefulWidget {
2021-01-10 11:49:28 +01:00
const GenerateWalletScreen ( { Key keyGenWallet } ) : super ( key: keyGenWallet ) ;
2021-01-06 05:04:54 +01:00
@ override
2021-01-10 11:49:28 +01:00
GenerateWalletState createState ( ) = > GenerateWalletState ( ) ;
2021-01-06 05:04:54 +01:00
}
2021-01-10 11:49:28 +01:00
class GenerateWalletState extends State < GenerateWalletScreen > {
2021-01-11 19:13:28 +01:00
GlobalKey < MyWalletState > _keyMyWallets = GlobalKey ( ) ;
2021-01-11 04:50:15 +01:00
GlobalKey < ValidStoreWalletState > _keyValidWallets = GlobalKey ( ) ;
2021-01-06 05:04:54 +01:00
void initState ( ) {
super . initState ( ) ;
DubpRust . setup ( ) ;
}
2021-01-09 09:17:01 +01:00
TextEditingController _mnemonicController = new TextEditingController ( ) ;
2021-01-06 05:04:54 +01:00
TextEditingController _pubkey = new TextEditingController ( ) ;
TextEditingController _pin = new TextEditingController ( ) ;
2021-01-09 09:17:01 +01:00
String generatedMnemonic ;
2021-01-09 10:48:06 +01:00
bool walletIsGenerated = false ;
2021-01-09 09:17:01 +01:00
NewWallet actualWallet ;
2021-01-08 05:02:27 +01:00
final formKey = GlobalKey < FormState > ( ) ;
2021-01-06 05:04:54 +01:00
2021-01-09 10:48:06 +01:00
bool hasError = false ;
String validPin = ' NO PIN ' ;
String currentText = " " ;
var pinColor = Colors . grey [ 300 ] ;
2021-01-06 05:04:54 +01:00
@ override
Widget build ( BuildContext context ) {
2021-01-10 07:49:29 +01:00
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),
) ,
2021-01-09 09:17:01 +01:00
) ,
2021-01-10 07:49:29 +01:00
) ,
body: SafeArea (
child: Column ( children: < Widget > [
FutureBuilder < bool > (
2021-01-10 11:49:28 +01:00
future: checkIfWalletExist ( ' tata ' ) ,
2021-01-10 07:49:29 +01:00
builder: ( context , fSnapshot ) {
if ( fSnapshot . hasData )
return Visibility (
visible: ( ! fSnapshot . data & & ! walletIsGenerated ) ,
child: Column ( children: < Widget > [
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 < bool > (
2021-01-10 11:49:28 +01:00
future: checkIfWalletExist ( ' tata ' ) ,
2021-01-10 07:49:29 +01:00
builder: ( context , fSnapshot ) {
if ( fSnapshot . hasData )
return Visibility (
2021-01-10 11:49:28 +01:00
visible: fSnapshot . data ,
2021-01-11 19:13:28 +01:00
child: MyWalletsScreen ( keyMyWallets: _keyMyWallets ) ) ;
2021-01-10 07:49:29 +01:00
return Center ( child: CircularProgressIndicator ( ) ) ;
} ) ,
SizedBox ( height: 8 ) ,
Visibility (
visible: walletIsGenerated ,
child: Column ( children: < Widget > [
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 ) ) ,
2021-01-11 19:13:28 +01:00
SizedBox ( height: 20 ) ,
2021-01-10 07:49:29 +01:00
// Expanded(child: Align(alignment: Alignment.bottomCenter)),
new RaisedButton (
2021-01-09 09:17:01 +01:00
color: Color ( 0xffFFD68E ) ,
2021-01-09 10:48:06 +01:00
onPressed: walletIsGenerated
? ( ) {
Navigator . push (
context ,
MaterialPageRoute ( builder: ( context ) {
return ValidStoreWalletScreen (
2021-01-11 04:50:15 +01:00
validationKey: _keyValidWallets ,
2021-01-09 10:48:06 +01:00
generatedMnemonic: this . generatedMnemonic ,
generatedWallet: this . actualWallet ) ;
} ) ,
) . then ( ( value ) = > setState ( ( ) {
2021-01-11 04:50:15 +01:00
if ( value ! = null ) {
print (
' TODO: Fix resetWalletState() ' ) ; //TODO: Fix resetWalletState()
2021-01-09 10:48:06 +01:00
_pin . clear ( ) ;
_mnemonicController . clear ( ) ;
_pubkey . clear ( ) ;
this . generatedMnemonic = null ;
this . actualWallet = null ;
this . walletIsGenerated = false ;
2021-01-11 04:50:15 +01:00
resetWalletState ( ) ;
// setState(() {});
// getAllWalletsNames();
// checkIfWalletExist('tata');
2021-01-11 19:13:28 +01:00
// _keyMyWallets.currentState.getAllWalletsNames();
2021-01-09 10:48:06 +01:00
}
} ) ) ;
}
: null ,
2021-01-11 19:13:28 +01:00
child: Text ( ' Enregistrer ce portefeuille ' ,
2021-01-10 07:49:29 +01:00
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),
// )
)
] ) ) ) ;
2021-01-06 05:04:54 +01:00
}
2021-01-11 04:50:15 +01:00
Future resetWalletState ( ) async {
final bool _isExist = await checkIfWalletExist ( ' tata ' ) ;
print ( ' The wallet exist in resetWalletState(): ' + _isExist . toString ( ) ) ;
// initState();
2021-01-11 19:13:28 +01:00
// _keyMyWallets.currentState.setState(() {});
// _keyMyWallets.currentState.initAppDirectory();
2021-01-11 04:50:15 +01:00
setState ( ( ) {
// getAllWalletsNames();
// this.walletIsGenerated = true;
} ) ;
}
2021-01-06 05:04:54 +01:00
Future generateMnemonic ( ) async {
try {
2021-01-09 09:17:01 +01:00
this . generatedMnemonic =
await DubpRust . genMnemonic ( language: Language . french ) ;
2021-01-06 05:04:54 +01:00
} catch ( e , stack ) {
print ( e ) ;
2021-01-06 21:17:01 +01:00
if ( kReleaseMode ) {
await sentry . Sentry . captureException (
e ,
stackTrace: stack ,
) ;
}
2021-01-06 05:04:54 +01:00
}
2021-01-09 09:17:01 +01:00
this . actualWallet = await generateWallet ( this . generatedMnemonic ) ;
2021-01-09 10:48:06 +01:00
this . walletIsGenerated = true ;
2021-01-10 07:49:29 +01:00
// await checkIfWalletExist();
2021-01-09 09:17:01 +01:00
return this . generatedMnemonic ;
2021-01-06 05:04:54 +01:00
}
Future generateWallet ( generatedMnemonic ) async {
try {
2021-01-09 09:17:01 +01:00
this . actualWallet = await DubpRust . genWalletFromMnemonic (
2021-01-10 07:49:29 +01:00
language: Language . french ,
mnemonic: generatedMnemonic ,
secretCodeType: SecretCodeType . letters ) ;
2021-01-06 05:04:54 +01:00
} catch ( e , stack ) {
print ( e ) ;
2021-01-06 21:17:01 +01:00
if ( kReleaseMode ) {
await sentry . Sentry . captureException (
e ,
stackTrace: stack ,
) ;
}
2021-01-06 05:04:54 +01:00
}
setState ( ( ) {
2021-01-09 09:17:01 +01:00
this . _mnemonicController . text = generatedMnemonic ;
this . _pubkey . text = actualWallet . publicKey ;
this . _pin . text = actualWallet . pin ;
2021-01-06 05:04:54 +01:00
} ) ;
2021-01-06 21:17:01 +01:00
2021-01-09 09:17:01 +01:00
return actualWallet ;
}
2021-01-10 07:49:29 +01:00
2021-01-10 11:49:28 +01:00
Future < bool > checkIfWalletExist ( _name ) async {
final appPath = await _localPath ;
final _walletFile = File ( ' $ appPath /wallets/ $ _name /wallet.dewif ' ) ;
2021-01-10 07:49:29 +01:00
// deleteWallet();
print ( _walletFile . path ) ;
2021-01-11 04:50:15 +01:00
final bool isExist = await File ( _walletFile . path ) . exists ( ) ;
2021-01-10 07:49:29 +01:00
print ( ' Wallet existe ? : ' + isExist . toString ( ) ) ;
print ( ' Is wallet generated ? : ' + walletIsGenerated . toString ( ) ) ;
2021-01-11 04:50:15 +01:00
if ( isExist ) {
2021-01-10 07:49:29 +01:00
print ( ' Un wallet existe ! ' ) ;
return true ;
} else {
return false ;
}
}
2021-01-11 04:50:15 +01:00
Future < List > 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();
}
2021-01-10 07:49:29 +01:00
Future importWallet ( ) async { }
Future < String > get _localPath async {
final directory = await getApplicationDocumentsDirectory ( ) ;
return directory . path ;
}
2021-01-09 09:17:01 +01:00
}
2021-01-10 07:49:29 +01:00
/* *** ValidStoreWalletScreen Class *** */
2021-01-09 09:17:01 +01:00
class ValidStoreWalletScreen extends StatefulWidget {
final String generatedMnemonic ;
final NewWallet generatedWallet ;
ValidStoreWalletScreen (
{ Key validationKey ,
@ required this . generatedMnemonic ,
@ required this . generatedWallet } )
: super ( key: validationKey ) ;
@ override
2021-01-11 04:50:15 +01:00
ValidStoreWalletState createState ( ) = > ValidStoreWalletState ( ) ;
2021-01-09 09:17:01 +01:00
}
2021-01-11 04:50:15 +01:00
class ValidStoreWalletState extends State < ValidStoreWalletScreen > {
GlobalKey < ValidStoreWalletState > _keyValidWallets = GlobalKey ( ) ;
2021-01-09 09:17:01 +01:00
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 ( ) ;
2021-01-10 11:49:28 +01:00
String walletName = ' tata ' ;
List _listWallets = [ ] ;
2021-01-09 09:17:01 +01:00
@ override
Widget build ( BuildContext context ) {
return Scaffold (
appBar: AppBar ( ) ,
body: Center (
child: Column ( children: < Widget > [
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 ) ,
2021-01-10 07:49:29 +01:00
onPressed: ( ) = > storeWallet ( ) ,
2021-01-09 09:17:01 +01:00
child: Text ( ' Confirmer ' , style: TextStyle ( fontSize: 20 ) ) ) ,
] ) ,
) ,
) ;
}
2021-01-10 07:49:29 +01:00
Future storeWallet ( ) async {
2021-01-10 11:49:28 +01:00
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 ( ) ;
2021-01-09 09:17:01 +01:00
walletFile . writeAsString ( ' ${ widget . generatedWallet . dewif } ' ) ;
_pin . clear ( ) ;
2021-01-10 11:49:28 +01:00
await getAllWalletsNames ( ) ;
2021-01-09 09:17:01 +01:00
Navigator . pop ( context , true ) ;
FocusScope . of ( context ) . unfocus ( ) ;
2021-01-10 11:49:28 +01:00
return this . walletName ;
2021-01-06 21:17:01 +01:00
}
Future < String > get _localPath async {
final directory = await getApplicationDocumentsDirectory ( ) ;
return directory . path ;
}
2021-01-10 11:49:28 +01:00
Future < List > 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();
2021-01-06 05:04:54 +01:00
}
}