2021-01-13 00:00:11 +01:00
|
|
|
import 'dart:io';
|
2021-01-13 05:36:23 +01:00
|
|
|
import 'dart:math';
|
2021-01-13 00:00:11 +01:00
|
|
|
import 'package:dubp/dubp.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-01-15 02:33:40 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2021-01-13 00:00:11 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2021-01-15 09:55:03 +01:00
|
|
|
import 'dart:convert' show utf8;
|
2021-01-13 00:00:11 +01:00
|
|
|
|
|
|
|
class ConfirmStoreWallet extends StatefulWidget {
|
|
|
|
final String generatedMnemonic;
|
|
|
|
final NewWallet generatedWallet;
|
|
|
|
|
|
|
|
ConfirmStoreWallet(
|
|
|
|
{Key validationKey,
|
|
|
|
@required this.generatedMnemonic,
|
|
|
|
@required this.generatedWallet})
|
|
|
|
: super(key: validationKey);
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConfirmStoreWalletState createState() => ConfirmStoreWalletState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConfirmStoreWalletState extends State<ConfirmStoreWallet> {
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
this._mnemonicController.text = widget.generatedMnemonic;
|
|
|
|
this._pubkey.text = widget.generatedWallet.publicKey;
|
2021-01-13 05:36:23 +01:00
|
|
|
nbrWord = getRandomInt();
|
2021-01-15 02:33:40 +01:00
|
|
|
askedWordColor = Colors.black;
|
2021-01-13 00:00:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 06:28:14 +01:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_wordFocus.dispose();
|
|
|
|
_walletNameFocus.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-01-13 00:00:11 +01:00
|
|
|
TextEditingController _mnemonicController = new TextEditingController();
|
|
|
|
TextEditingController _pubkey = new TextEditingController();
|
|
|
|
TextEditingController _pin = new TextEditingController();
|
2021-01-13 05:36:23 +01:00
|
|
|
TextEditingController _inputRestoreWord = new TextEditingController();
|
|
|
|
TextEditingController walletName = new TextEditingController();
|
2021-01-15 06:28:14 +01:00
|
|
|
FocusNode _wordFocus = FocusNode();
|
|
|
|
FocusNode _walletNameFocus = FocusNode();
|
2021-01-15 02:33:40 +01:00
|
|
|
Color askedWordColor;
|
2021-01-13 05:36:23 +01:00
|
|
|
int nbrWord;
|
|
|
|
bool isAskedWordValid = false;
|
2021-01-13 00:00:11 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2021-01-13 05:36:23 +01:00
|
|
|
resizeToAvoidBottomInset: false,
|
2021-01-15 03:11:58 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: SizedBox(
|
|
|
|
height: 25,
|
|
|
|
child: Text('Confirmez ce portefeuille'),
|
|
|
|
)),
|
2021-01-13 00:00:11 +01:00
|
|
|
body: Center(
|
|
|
|
child: Column(children: <Widget>[
|
2021-01-13 05:36:23 +01:00
|
|
|
SizedBox(height: 15),
|
|
|
|
Text(
|
|
|
|
'Votre clé publique est :',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 17.0,
|
|
|
|
color: Colors.grey[600],
|
|
|
|
fontWeight: FontWeight.w400),
|
|
|
|
),
|
2021-01-13 00:00:11 +01:00
|
|
|
TextField(
|
|
|
|
enabled: false,
|
2021-01-13 05:36:23 +01:00
|
|
|
controller: this._pubkey,
|
|
|
|
maxLines: 1,
|
2021-01-13 00:00:11 +01:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
decoration: InputDecoration(),
|
|
|
|
style: TextStyle(
|
2021-01-13 05:36:23 +01:00
|
|
|
fontSize: 14.0,
|
2021-01-13 00:00:11 +01:00
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.bold)),
|
2021-01-13 05:36:23 +01:00
|
|
|
SizedBox(height: 12),
|
|
|
|
Text(
|
|
|
|
'Quel est le ${nbrWord + 1}ème mot de votre phrase de restauration ?',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 17.0,
|
|
|
|
color: Colors.grey[600],
|
|
|
|
fontWeight: FontWeight.w400),
|
|
|
|
),
|
2021-01-15 06:28:14 +01:00
|
|
|
TextFormField(
|
|
|
|
focusNode: _wordFocus,
|
|
|
|
autofocus: true,
|
2021-01-13 05:36:23 +01:00
|
|
|
enabled: !isAskedWordValid,
|
|
|
|
controller: this._inputRestoreWord,
|
2021-01-15 06:28:14 +01:00
|
|
|
textInputAction: TextInputAction.next,
|
2021-01-13 05:36:23 +01:00
|
|
|
onChanged: (value) {
|
|
|
|
checkAskedWord(value);
|
|
|
|
},
|
2021-01-15 06:28:14 +01:00
|
|
|
maxLines: 1,
|
2021-01-13 00:00:11 +01:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
decoration: InputDecoration(),
|
|
|
|
style: TextStyle(
|
2021-01-13 05:36:23 +01:00
|
|
|
fontSize: 30.0,
|
2021-01-15 02:33:40 +01:00
|
|
|
color: askedWordColor,
|
2021-01-13 05:36:23 +01:00
|
|
|
fontWeight: FontWeight.w500)),
|
|
|
|
SizedBox(height: 12),
|
|
|
|
Text(
|
|
|
|
'Choisissez un nom pour votre portefeuille :',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 17.0,
|
|
|
|
color: Colors.grey[600],
|
|
|
|
fontWeight: FontWeight.w400),
|
|
|
|
),
|
2021-01-15 06:28:14 +01:00
|
|
|
TextFormField(
|
|
|
|
focusNode: _walletNameFocus,
|
|
|
|
// autofocus: true,
|
2021-01-15 02:33:40 +01:00
|
|
|
inputFormatters: [
|
2021-01-15 03:11:58 +01:00
|
|
|
FilteringTextInputFormatter.allow(
|
2021-01-15 10:45:26 +01:00
|
|
|
RegExp('[A-Za-z|0-9|\\-|_| ]')),
|
2021-01-15 02:33:40 +01:00
|
|
|
],
|
2021-01-15 06:28:14 +01:00
|
|
|
// enabled: isAskedWordValid,
|
2021-01-13 05:36:23 +01:00
|
|
|
controller: this.walletName,
|
2021-01-15 06:28:14 +01:00
|
|
|
textInputAction: TextInputAction.next,
|
2021-01-13 05:36:23 +01:00
|
|
|
onChanged: (v) {
|
|
|
|
nameChanged();
|
|
|
|
},
|
2021-01-15 06:28:14 +01:00
|
|
|
maxLines: 1,
|
2021-01-13 05:36:23 +01:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
decoration: InputDecoration(),
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 30.0,
|
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.w500)),
|
|
|
|
Expanded(
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: SizedBox(
|
|
|
|
width: 200,
|
|
|
|
height: 50,
|
|
|
|
child: ElevatedButton(
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
elevation: 12,
|
|
|
|
primary: Colors
|
|
|
|
.green[400], //Color(0xffFFD68E), // background
|
|
|
|
onPrimary: Colors.black, // foreground
|
|
|
|
),
|
|
|
|
onPressed:
|
|
|
|
(isAskedWordValid && this.walletName.text != '')
|
2021-01-15 02:33:40 +01:00
|
|
|
? () => storeWallet(this.walletName.text)
|
2021-01-13 05:36:23 +01:00
|
|
|
: null,
|
|
|
|
child:
|
|
|
|
Text('Confirmer', style: TextStyle(fontSize: 28))),
|
|
|
|
))),
|
|
|
|
SizedBox(height: 70),
|
|
|
|
Text('TRICHE PENDANT ALPHA: ' + this._mnemonicController.text,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 10.0,
|
|
|
|
color: Colors.black,
|
|
|
|
fontWeight: FontWeight.normal)),
|
2021-01-13 00:00:11 +01:00
|
|
|
]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-15 02:33:40 +01:00
|
|
|
Future storeWallet(_name) async {
|
2021-01-13 00:00:11 +01:00
|
|
|
final appPath = await _localPath;
|
2021-01-15 09:55:03 +01:00
|
|
|
final Directory walletNameDirectory = Directory('$appPath/wallets/$_name');
|
|
|
|
final walletFile = File('${walletNameDirectory.path}/wallet.dewif');
|
2021-01-13 00:00:11 +01:00
|
|
|
|
2021-01-15 09:55:03 +01:00
|
|
|
if (await walletNameDirectory.exists()) {
|
2021-01-15 02:33:40 +01:00
|
|
|
print('Ce wallet existe déjà, impossible de le créer.');
|
|
|
|
_showWalletExistDialog();
|
|
|
|
return 'Exist: DENY';
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:55:03 +01:00
|
|
|
walletNameDirectory.createSync();
|
2021-01-13 00:00:11 +01:00
|
|
|
walletFile.writeAsString('${widget.generatedWallet.dewif}');
|
|
|
|
_pin.clear();
|
|
|
|
|
|
|
|
Navigator.pop(context, true);
|
2021-01-13 05:36:23 +01:00
|
|
|
Navigator.pop(context, this._pubkey.text);
|
2021-01-13 00:00:11 +01:00
|
|
|
|
2021-01-15 02:33:40 +01:00
|
|
|
return _name;
|
2021-01-13 00:00:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> get _localPath async {
|
|
|
|
final directory = await getApplicationDocumentsDirectory();
|
|
|
|
return directory.path;
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:55:03 +01:00
|
|
|
void checkAskedWord(String value) {
|
|
|
|
final runesAsked = _mnemonicController.text.split(' ')[nbrWord].runes;
|
|
|
|
List<int> runesAskedUnaccent = [];
|
2021-01-15 10:45:26 +01:00
|
|
|
print(runesAsked);
|
|
|
|
print(value.runes);
|
2021-01-15 09:55:03 +01:00
|
|
|
for (int i in runesAsked) {
|
2021-01-15 10:45:26 +01:00
|
|
|
if (i == 768 || i == 769 || i == 770 || i == 771) {
|
2021-01-15 09:55:03 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
runesAskedUnaccent.add(i);
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 10:45:26 +01:00
|
|
|
final String unaccentedAskedWord =
|
|
|
|
utf8.decode(runesAskedUnaccent).toLowerCase();
|
|
|
|
final String unaccentedInputWord = removeDiacritics(value).toLowerCase();
|
2021-01-15 09:55:03 +01:00
|
|
|
|
|
|
|
print("Is $unaccentedAskedWord equal to input $unaccentedInputWord ?");
|
|
|
|
if (unaccentedAskedWord == unaccentedInputWord || value == 'triche') {
|
2021-01-13 05:36:23 +01:00
|
|
|
print('Word is OK');
|
|
|
|
isAskedWordValid = true;
|
2021-01-15 02:33:40 +01:00
|
|
|
askedWordColor = Colors.green[600];
|
2021-01-15 06:28:14 +01:00
|
|
|
_walletNameFocus.nextFocus();
|
2021-01-13 05:36:23 +01:00
|
|
|
} else {
|
|
|
|
isAskedWordValid = false;
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
2021-01-15 02:33:40 +01:00
|
|
|
Future<void> _showWalletExistDialog() 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(
|
2021-01-15 03:11:58 +01:00
|
|
|
child: Text("J'ai compris"),
|
2021-01-15 02:33:40 +01:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:55:03 +01:00
|
|
|
String removeDiacritics(String str) {
|
|
|
|
var withDia =
|
|
|
|
'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
|
|
|
|
var withoutDia =
|
|
|
|
'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz';
|
|
|
|
|
|
|
|
for (int i = 0; i < withDia.length; i++) {
|
|
|
|
str = str.replaceAll(withDia[i], withoutDia[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2021-01-13 05:36:23 +01:00
|
|
|
int getRandomInt() {
|
|
|
|
var rng = new Random();
|
|
|
|
return rng.nextInt(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nameChanged() {
|
|
|
|
setState(() {});
|
2021-01-13 00:00:11 +01:00
|
|
|
}
|
|
|
|
}
|