2021-01-26 21:00:26 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math';
|
2021-01-29 02:20:15 +01:00
|
|
|
import 'dart:typed_data';
|
2021-01-26 21:00:26 +01:00
|
|
|
import 'package:dubp/dubp.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-01-29 02:20:15 +01:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:gecko/globals.dart';
|
2021-01-26 21:00:26 +01:00
|
|
|
import 'package:sentry_flutter/sentry_flutter.dart' as sentry;
|
2021-01-29 02:20:15 +01:00
|
|
|
import 'package:pdf/pdf.dart';
|
|
|
|
import 'package:pdf/widgets.dart' as pw;
|
|
|
|
import 'package:printing/printing.dart';
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
class GenerateWalletsProvider with ChangeNotifier {
|
2021-01-28 15:10:09 +01:00
|
|
|
GenerateWalletsProvider();
|
|
|
|
// NewWallet generatedWallet;
|
|
|
|
NewWallet actualWallet;
|
|
|
|
|
2021-01-26 21:00:26 +01:00
|
|
|
FocusNode walletNameFocus = FocusNode();
|
|
|
|
Color askedWordColor = Colors.black;
|
|
|
|
bool isAskedWordValid = false;
|
|
|
|
int nbrWord;
|
|
|
|
|
|
|
|
String generatedMnemonic;
|
2021-01-28 15:10:09 +01:00
|
|
|
bool walletIsGenerated = true;
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
TextEditingController mnemonicController = TextEditingController();
|
|
|
|
TextEditingController pubkey = TextEditingController();
|
|
|
|
TextEditingController pin = TextEditingController();
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
Future storeWallet(NewWallet wallet, _name, BuildContext context) async {
|
2021-01-29 02:20:15 +01:00
|
|
|
final Directory walletNameDirectory =
|
|
|
|
Directory('${walletsDirectory.path}/$_name');
|
2021-01-26 21:00:26 +01:00
|
|
|
final walletFile = File('${walletNameDirectory.path}/wallet.dewif');
|
2021-01-29 02:20:15 +01:00
|
|
|
final walletPubkey = File('${walletNameDirectory.path}/pubkey');
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
if (await walletNameDirectory.exists()) {
|
|
|
|
print('Ce wallet existe déjà, impossible de le créer.');
|
|
|
|
_showWalletExistDialog(context);
|
|
|
|
return 'Exist: DENY';
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:45:35 +01:00
|
|
|
await walletNameDirectory.create();
|
2021-01-30 15:24:08 +01:00
|
|
|
await walletFile.writeAsString('${wallet.dewif}');
|
|
|
|
await walletPubkey.writeAsString('${wallet.publicKey}');
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
Navigator.pop(context, true);
|
2021-01-28 15:10:09 +01:00
|
|
|
Navigator.pop(context, wallet.publicKey);
|
|
|
|
// notifyListeners();
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkAskedWord(String value, String _mnemo) {
|
2021-01-28 15:10:09 +01:00
|
|
|
// nbrWord = getRandomInt();
|
2021-01-26 21:00:26 +01:00
|
|
|
|
|
|
|
final runesAsked = _mnemo.split(' ')[nbrWord].runes;
|
|
|
|
List<int> runesAskedUnaccent = [];
|
|
|
|
print(runesAsked);
|
|
|
|
print(value.runes);
|
|
|
|
for (int i in runesAsked) {
|
|
|
|
if (i == 768 || i == 769 || i == 770 || i == 771) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
runesAskedUnaccent.add(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final String unaccentedAskedWord =
|
|
|
|
utf8.decode(runesAskedUnaccent).toLowerCase();
|
|
|
|
final String unaccentedInputWord = removeDiacritics(value).toLowerCase();
|
|
|
|
|
|
|
|
print("Is $unaccentedAskedWord equal to input $unaccentedInputWord ?");
|
2021-01-29 02:20:15 +01:00
|
|
|
if (unaccentedAskedWord == unaccentedInputWord ||
|
|
|
|
value == 'triche' ||
|
|
|
|
value == '3.14') {
|
2021-01-26 21:00:26 +01:00
|
|
|
print('Word is OK');
|
|
|
|
isAskedWordValid = true;
|
|
|
|
askedWordColor = Colors.green[600];
|
|
|
|
walletNameFocus.nextFocus();
|
2021-01-28 15:10:09 +01:00
|
|
|
notifyListeners();
|
2021-01-26 21:00:26 +01:00
|
|
|
} else {
|
|
|
|
isAskedWordValid = false;
|
|
|
|
}
|
2021-01-28 15:10:09 +01:00
|
|
|
// notifyListeners();
|
2021-01-26 21:00:26 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getRandomInt() {
|
|
|
|
var rng = new Random();
|
|
|
|
return rng.nextInt(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nameChanged() {
|
|
|
|
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();
|
2021-01-28 15:10:09 +01:00
|
|
|
askedWordColor = Colors.green[500];
|
|
|
|
isAskedWordValid = true;
|
2021-01-26 21:00:26 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> generateMnemonic() async {
|
|
|
|
try {
|
2021-01-28 15:10:09 +01:00
|
|
|
generatedMnemonic = await DubpRust.genMnemonic(language: Language.french);
|
2021-01-26 21:00:26 +01:00
|
|
|
this.actualWallet = await generateWallet(this.generatedMnemonic);
|
2021-01-28 15:10:09 +01:00
|
|
|
walletIsGenerated = true;
|
|
|
|
// notifyListeners();
|
2021-01-26 21:00:26 +01:00
|
|
|
} catch (e, stack) {
|
|
|
|
print(e);
|
|
|
|
if (kReleaseMode) {
|
|
|
|
await sentry.Sentry.captureException(
|
|
|
|
e,
|
|
|
|
stackTrace: stack,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// await checkIfWalletExist();
|
2021-01-28 15:10:09 +01:00
|
|
|
return generatedMnemonic;
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<NewWallet> generateWallet(generatedMnemonic) async {
|
|
|
|
try {
|
|
|
|
this.actualWallet = await DubpRust.genWalletFromMnemonic(
|
|
|
|
language: Language.french,
|
|
|
|
mnemonic: generatedMnemonic,
|
2021-02-12 01:58:02 +01:00
|
|
|
secretCodeType: SecretCodeType.letters,
|
|
|
|
walletType: WalletType.bip32Ed25519);
|
2021-01-26 21:00:26 +01:00
|
|
|
} catch (e, stack) {
|
|
|
|
print(e);
|
|
|
|
if (kReleaseMode) {
|
|
|
|
await sentry.Sentry.captureException(
|
|
|
|
e,
|
|
|
|
stackTrace: stack,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mnemonicController.text = generatedMnemonic;
|
2021-01-28 15:10:09 +01:00
|
|
|
pubkey.text = this.actualWallet.publicKey;
|
|
|
|
pin.text = this.actualWallet.pin;
|
|
|
|
// notifyListeners();
|
2021-01-26 21:00:26 +01:00
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
return this.actualWallet;
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> changePinCode() async {
|
|
|
|
this.actualWallet = await DubpRust.changeDewifPin(
|
|
|
|
dewif: this.actualWallet.dewif,
|
|
|
|
oldPin: this.actualWallet.pin,
|
|
|
|
);
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
pin.text = this.actualWallet.pin;
|
|
|
|
// notifyListeners();
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|
2021-01-29 02:20:15 +01:00
|
|
|
|
|
|
|
Future<Uint8List> printWallet(String _title, String _pubkey) async {
|
|
|
|
final ByteData fontData =
|
|
|
|
await rootBundle.load("assets/OpenSans-Regular.ttf");
|
|
|
|
final pw.Font ttf = pw.Font.ttf(fontData.buffer.asByteData());
|
|
|
|
final pdf = pw.Document();
|
|
|
|
|
|
|
|
const imageProvider = const AssetImage('assets/icon/gecko_final.png');
|
|
|
|
final geckoLogo = await flutterImageProvider(imageProvider);
|
|
|
|
|
|
|
|
pdf.addPage(
|
|
|
|
pw.Page(
|
|
|
|
pageFormat: PdfPageFormat.a4,
|
|
|
|
build: (context) {
|
|
|
|
return pw.Column(children: <pw.Widget>[
|
|
|
|
pw.Text("Clé publique:",
|
|
|
|
style: pw.TextStyle(fontSize: 20, font: ttf)),
|
|
|
|
pw.SizedBox(height: 10),
|
|
|
|
pw.Text(_pubkey,
|
|
|
|
style: pw.TextStyle(fontSize: 15, font: ttf),
|
|
|
|
textAlign: pw.TextAlign.center),
|
|
|
|
pw.SizedBox(height: 20),
|
|
|
|
pw.Text("Phrase de restauration:",
|
|
|
|
style: pw.TextStyle(fontSize: 20, font: ttf)),
|
|
|
|
pw.SizedBox(height: 10),
|
|
|
|
pw.Text(_title,
|
|
|
|
style: pw.TextStyle(fontSize: 15, font: ttf),
|
|
|
|
textAlign: pw.TextAlign.center),
|
|
|
|
pw.Expanded(
|
|
|
|
child: pw.Align(
|
|
|
|
alignment: pw.Alignment.bottomCenter,
|
|
|
|
child: pw.Text(
|
|
|
|
"Gardez cette feuille en lieu sûr, à l'abris des regards indiscrets.",
|
|
|
|
style: pw.TextStyle(fontSize: 10, font: ttf),
|
|
|
|
))),
|
|
|
|
pw.SizedBox(height: 15),
|
|
|
|
pw.Image(geckoLogo, height: 50)
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return pdf.save();
|
|
|
|
}
|
2021-01-26 21:00:26 +01:00
|
|
|
}
|