Add pincode form; Add tab My wallets

This commit is contained in:
poka 2021-01-08 05:02:27 +01:00
parent 707613115c
commit ce85df7227
3 changed files with 212 additions and 101 deletions

View File

@ -3,15 +3,15 @@ import 'package:flutter/material.dart';
import 'package:dubp/dubp.dart';
import 'package:sentry/sentry.dart' as sentry;
import 'dart:io';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
class GenerateWalletScreen extends StatefulWidget {
@override
_GenerateWalletScreen createState() => _GenerateWalletScreen();
_GenerateWalletState createState() => _GenerateWalletState();
}
class _GenerateWalletScreen extends State<GenerateWalletScreen> {
class _GenerateWalletState extends State<GenerateWalletScreen> {
void initState() {
super.initState();
DubpRust.setup();
@ -19,10 +19,8 @@ class _GenerateWalletScreen extends State<GenerateWalletScreen> {
TextEditingController _mnemonic = new TextEditingController();
TextEditingController _pubkey = new TextEditingController();
TextEditingController _dewif = new TextEditingController();
TextEditingController _pin = new TextEditingController();
TextEditingController _enterPin = new TextEditingController();
String validPin = 'NO PIN';
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
@ -48,13 +46,6 @@ class _GenerateWalletScreen extends State<GenerateWalletScreen> {
fontSize: 14.0,
color: Colors.black,
fontWeight: FontWeight.bold)),
TextField(
enabled: false,
controller: this._dewif,
maxLines: 3,
textAlign: TextAlign.center,
decoration: InputDecoration(),
style: TextStyle(fontSize: 12.0, color: Colors.red)),
TextField(
enabled: false,
controller: this._pin,
@ -69,28 +60,7 @@ class _GenerateWalletScreen extends State<GenerateWalletScreen> {
new RaisedButton(
onPressed: () => generateMnemonic(),
child: Text('Générer un wallet', style: TextStyle(fontSize: 20))),
SizedBox(height: 20),
TextField(
controller: this._enterPin,
onChanged: (validPin) {
this.validPin = validPin ?? 'NO PIN';
print('PIN: ' + this.validPin);
},
maxLines: 1,
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: 'Tappez votre code PIN',
hintStyle: TextStyle(fontSize: 15),
contentPadding: EdgeInsets.symmetric(horizontal: 7, vertical: 15),
),
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
fontWeight: FontWeight.bold)),
SizedBox(height: 12),
new RaisedButton(
onPressed: () => readLocalWallet(this.validPin.toUpperCase()),
child: Text('Lire le wallet local', style: TextStyle(fontSize: 20))),
SizedBox(height: 20)
]));
}
@ -130,75 +100,12 @@ class _GenerateWalletScreen extends State<GenerateWalletScreen> {
setState(() {
this._mnemonic.text = generatedMnemonic;
this._pubkey.text = newWallet.publicKey;
this._dewif.text = newWallet.dewif;
this._pin.text = newWallet.pin;
});
return walletFile.writeAsString('${newWallet.dewif}');
}
Future getPubkeyFromDewif(_dewif, _pin) async {
String _pubkey;
RegExp regExp = new RegExp(
r'^[A-Z0-9]+$',
caseSensitive: false,
multiLine: false,
);
if (regExp.hasMatch(_pin) == true && _pin.length == 6) {
print("Le format du code PIN est correct.");
} else {
print('Format de code PIN invalide');
return 'false';
}
try {
_pubkey = await DubpRust.getDewifPublicKey(dewif: _dewif, pin: _pin);
setState(() {
this._pubkey.text = _pubkey;
});
return _pubkey;
} catch (e, stack) {
print('Bad PIN code !');
print(e);
if (kReleaseMode) {
await sentry.Sentry.captureException(
e,
stackTrace: stack,
);
}
return 'false';
}
}
Future readLocalWallet(String _pin) async {
// print(pin);
try {
final file = await _localWallet;
String _localDewif = await file.readAsString();
String _localPubkey;
// _localPubkey = await getPubkeyFromDewif(_localDewif, _pin)?
if ((_localPubkey = await getPubkeyFromDewif(_localDewif, _pin)) !=
'false') {
setState(() {
this._mnemonic.text = '';
this._pubkey.text = _localPubkey;
this._dewif.text = _localDewif;
this._pin.text = _pin;
});
return _localDewif;
} else {
throw 'Bad pubkey';
}
} catch (e) {
print('ERROR READING FILE: $e');
return 0;
}
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();

View File

@ -4,6 +4,8 @@ import 'package:flutter/material.dart';
import 'dart:typed_data';
import 'dart:ui';
import 'myWallets.dart';
//ignore: must_be_immutable
class HomeScreen extends StatefulWidget {
HomeScreen({this.screens});
@ -40,6 +42,7 @@ class _HomeScreenState extends State<HomeScreen> {
keyHistory: _keyHistory,
),
GenerateWalletScreen(),
MyWalletsScreen(),
],
),
),
@ -65,11 +68,15 @@ class _HomeScreenState extends State<HomeScreen> {
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.format_list_bulleted),
label: 'HOME',
label: 'Accueil',
),
BottomNavigationBarItem(
icon: new Icon(Icons.settings),
label: 'GENERATE WALLET',
icon: new Icon(Icons.person_add_alt_1_rounded),
label: 'Générer un wallet',
),
BottomNavigationBarItem(
icon: new Icon(Icons.lock),
label: 'Mes wallets',
)
],
),

197
lib/ui/myWallets.dart Normal file
View File

@ -0,0 +1,197 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:dubp/dubp.dart';
import 'package:sentry/sentry.dart' as sentry;
import 'dart:io';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
class MyWalletsScreen extends StatefulWidget {
@override
_MyWalletState createState() => _MyWalletState();
}
class _MyWalletState extends State<MyWalletsScreen> {
StreamController<ErrorAnimationType> errorController;
void initState() {
super.initState();
errorController = StreamController<ErrorAnimationType>();
DubpRust.setup();
// HistoryScreen(
// keyHistory: _keyHistory,
// );
}
TextEditingController _pubkey = new TextEditingController();
TextEditingController _enterPin = new TextEditingController();
final formKey = GlobalKey<FormState>();
bool hasError = false;
String validPin = 'NO PIN';
String currentText = "";
var pinColor = Colors.grey[300];
@override
Widget build(BuildContext context) {
return SafeArea(
child: Column(children: <Widget>[
InkWell(
child: TextField(
enabled: false,
controller: this._pubkey,
maxLines: 1,
textAlign: TextAlign.center,
decoration: InputDecoration(),
style: TextStyle(
fontSize: 14.0,
color: Colors.black,
fontWeight: FontWeight.bold)),
onTap: () {
print("Ma pubkey click");
// _keyHistory.currentState.scan();
},
),
Form(
key: formKey,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 30),
child: PinCodeTextField(
appContext: context,
pastedTextStyle: TextStyle(
color: Colors.green.shade600,
fontWeight: FontWeight.bold,
),
length: 6,
obscureText: false,
obscuringCharacter: '*',
animationType: AnimationType.fade,
validator: (v) {
if (v.length < 6) {
return "Votre code PIN fait 6 caractères";
} else {
return null;
}
},
pinTheme: PinTheme(
shape: PinCodeFieldShape.box,
borderRadius: BorderRadius.circular(5),
fieldHeight: 60,
fieldWidth: 50,
activeFillColor: hasError ? Colors.orange : Colors.white,
),
cursorColor: Colors.black,
animationDuration: Duration(milliseconds: 300),
textStyle: TextStyle(fontSize: 20, height: 1.6),
backgroundColor: pinColor,
enableActiveFill: false,
errorAnimationController: errorController,
controller: _enterPin,
keyboardType: TextInputType.text,
boxShadows: [
BoxShadow(
offset: Offset(0, 1),
color: Colors.black12,
blurRadius: 10,
)
],
onCompleted: (v) async {
print("Completed");
final resultWallet = await readLocalWallet(v.toUpperCase());
if (resultWallet == 'bad') {
errorController.add(ErrorAnimationType
.shake); // Triggering error shake animation
setState(() {
hasError = true;
pinColor = Colors.red[200];
});
} else {
setState(() {
pinColor = Colors.green[200];
});
}
},
onChanged: (value) {
if (pinColor != Colors.grey[300]) {
setState(() {
pinColor = Colors.grey[300];
});
}
print(value);
},
)),
)
]));
}
Future getPubkeyFromDewif(_dewif, _pin) async {
String _pubkey;
RegExp regExp = new RegExp(
r'^[A-Z0-9]+$',
caseSensitive: false,
multiLine: false,
);
if (regExp.hasMatch(_pin) == true && _pin.length == 6) {
print("Le format du code PIN est correct.");
} else {
print('Format de code PIN invalide');
return 'false';
}
try {
_pubkey = await DubpRust.getDewifPublicKey(dewif: _dewif, pin: _pin);
setState(() {
this._pubkey.text = _pubkey;
});
return _pubkey;
} catch (e, stack) {
print('Bad PIN code !');
print(e);
if (kReleaseMode) {
await sentry.Sentry.captureException(
e,
stackTrace: stack,
);
}
return 'false';
}
}
Future readLocalWallet(String _pin) async {
// print(pin);
try {
final file = await _localWallet;
String _localDewif = await file.readAsString();
String _localPubkey;
if ((_localPubkey = await getPubkeyFromDewif(_localDewif, _pin)) !=
'false') {
setState(() {
this._pubkey.text = _localPubkey;
});
return _localDewif;
} else {
throw 'Bad pubkey';
}
} catch (e) {
print('ERROR READING FILE: $e');
setState(() {
this._pubkey.clear();
});
return 'bad';
}
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localWallet async {
final path = await _localPath;
return File('$path/wallet.dewif');
}
}