Can import and use Cesium wallet (salut/password)

This commit is contained in:
poka 2021-02-15 23:57:38 +01:00
parent 2e484d4373
commit 533f19dabc
5 changed files with 348 additions and 41 deletions

View File

@ -11,6 +11,7 @@ import 'package:sentry_flutter/sentry_flutter.dart' as sentry;
import 'package:pdf/pdf.dart'; import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw; import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart'; import 'package:printing/printing.dart';
import 'package:truncate/truncate.dart';
class GenerateWalletsProvider with ChangeNotifier { class GenerateWalletsProvider with ChangeNotifier {
GenerateWalletsProvider(); GenerateWalletsProvider();
@ -20,6 +21,7 @@ class GenerateWalletsProvider with ChangeNotifier {
FocusNode walletNameFocus = FocusNode(); FocusNode walletNameFocus = FocusNode();
Color askedWordColor = Colors.black; Color askedWordColor = Colors.black;
bool isAskedWordValid = false; bool isAskedWordValid = false;
int nbrWord; int nbrWord;
String generatedMnemonic; String generatedMnemonic;
@ -28,6 +30,15 @@ class GenerateWalletsProvider with ChangeNotifier {
TextEditingController mnemonicController = TextEditingController(); TextEditingController mnemonicController = TextEditingController();
TextEditingController pin = TextEditingController(); TextEditingController pin = TextEditingController();
// Import wallet
TextEditingController cesiumID = TextEditingController();
TextEditingController cesiumPWD = TextEditingController();
TextEditingController cesiumPubkey = TextEditingController();
bool isCesiumIDVisible = false;
bool isCesiumPWDVisible = false;
bool canImport = false;
bool isPinChanged = false;
Future storeWallet(NewWallet wallet, String _name, BuildContext context, Future storeWallet(NewWallet wallet, String _name, BuildContext context,
{bool isHD = false}) async { {bool isHD = false}) async {
int nbrWallet = 0; int nbrWallet = 0;
@ -55,11 +66,19 @@ class GenerateWalletsProvider with ChangeNotifier {
await configFile await configFile
.writeAsString('$nbrWallet:$_name:$_derivationNbr:$_pubkey'); .writeAsString('$nbrWallet:$_name:$_derivationNbr:$_pubkey');
} else { } else {
await configFile.writeAsString('$nbrWallet:$_name'); final int _derivationNbr = -1;
String _pubkey = await DubpRust.getDewifPublicKey(
dewif: wallet.dewif,
pin: wallet.pin,
);
await configFile
.writeAsString('$nbrWallet:$_name:$_derivationNbr:$_pubkey');
} }
Navigator.pop(context, true); Navigator.pop(context, true);
Navigator.pop(context, true); if (isHD) {
Navigator.pop(context, true);
}
// notifyListeners(); // notifyListeners();
return _name; return _name;
@ -164,13 +183,13 @@ class GenerateWalletsProvider with ChangeNotifier {
} }
Future<void> changePinCode() async { Future<void> changePinCode() async {
this.actualWallet = await DubpRust.changeDewifPin( actualWallet = await DubpRust.changeDewifPin(
dewif: this.actualWallet.dewif, dewif: actualWallet.dewif,
oldPin: this.actualWallet.pin, oldPin: actualWallet.pin,
); );
pin.text = this.actualWallet.pin; pin.text = actualWallet.pin;
// notifyListeners(); notifyListeners();
} }
Future<Uint8List> printWallet(String _title) async { Future<Uint8List> printWallet(String _title) async {
@ -210,4 +229,49 @@ class GenerateWalletsProvider with ChangeNotifier {
return pdf.save(); return pdf.save();
} }
Future<void> generateCesiumWalletPubkey(
String _cesiumID, String _cesiumPWD) async {
actualWallet = await DubpRust.genWalletFromDeprecatedSaltPassword(
salt: _cesiumID, password: _cesiumPWD);
String _walletPubkey = await DubpRust.getLegacyPublicKey(
salt: _cesiumID, password: _cesiumPWD);
cesiumPubkey.text = _walletPubkey;
// changePinCode();
// notifyListeners();
print(_walletPubkey);
}
Future importWallet(context, _cesiumID, _cesiumPWD) async {
String _walletPubkey = await DubpRust.getLegacyPublicKey(
salt: _cesiumID, password: _cesiumPWD);
String shortPubkey = truncate(_walletPubkey, 9,
omission: "...", position: TruncatePosition.end);
await storeWallet(
actualWallet, 'Portefeuille Cesium - $shortPubkey', context);
print('taaaaaaaaaaaaaaaaa');
print(actualWallet.pin);
cesiumID.text = '';
cesiumPWD.text = '';
cesiumPubkey.text = '';
canImport = false;
isPinChanged = false;
pin.text = '';
notifyListeners();
}
void cesiumIDisVisible() {
isCesiumIDVisible = !isCesiumIDVisible;
notifyListeners();
}
void cesiumPWDisVisible() {
isCesiumPWDVisible = !isCesiumPWDVisible;
notifyListeners();
}
void reloadBuild() {
notifyListeners();
}
} }

View File

@ -23,8 +23,6 @@ class MyWalletsProvider with ChangeNotifier {
} }
} }
Future importWallet() async {}
String getAllWalletsNames() { String getAllWalletsNames() {
final bool _isWalletsExists = checkIfWalletExist(); final bool _isWalletsExists = checkIfWalletExist();
if (!_isWalletsExists) { if (!_isWalletsExists) {

View File

@ -3,7 +3,6 @@ import 'package:dubp/dubp.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:async'; import 'dart:async';
import 'package:sentry/sentry.dart' as sentry;
import 'package:gecko/globals.dart'; import 'package:gecko/globals.dart';
class WalletOptionsProvider with ChangeNotifier { class WalletOptionsProvider with ChangeNotifier {
@ -30,26 +29,35 @@ class WalletOptionsProvider with ChangeNotifier {
print('Format de code PIN invalide'); print('Format de code PIN invalide');
return 'false'; return 'false';
} }
try { if (derivation != -1) {
List _pubkeysTmp = await DubpRust.getBip32DewifAccountsPublicKeys( try {
dewif: _dewif, secretCode: _pin, accountsIndex: [derivation]); List _pubkeysTmp = await DubpRust.getBip32DewifAccountsPublicKeys(
_pubkey = _pubkeysTmp[0]; dewif: _dewif, secretCode: _pin, accountsIndex: [derivation]);
this.pubkey.text = _pubkey; _pubkey = _pubkeysTmp[0];
notifyListeners(); this.pubkey.text = _pubkey;
notifyListeners();
return _pubkey; return _pubkey;
} catch (e, stack) { } catch (e) {
print('Bad PIN code !'); print('Bad PIN code !');
print(e); print(e);
if (kReleaseMode) { notifyListeners();
await sentry.Sentry.captureException(
e, return 'false';
stackTrace: stack,
);
} }
notifyListeners(); } else {
try {
_pubkey = await DubpRust.getDewifPublicKey(dewif: _dewif, pin: _pin);
this.pubkey.text = _pubkey;
notifyListeners();
return _pubkey;
} catch (e) {
print('Bad PIN code !');
print(e);
notifyListeners();
return 'false'; return 'false';
}
} }
} }
@ -97,12 +105,17 @@ class WalletOptionsProvider with ChangeNotifier {
String newConfig = String newConfig =
await _walletConfig.readAsLines().then((List<String> lines) { await _walletConfig.readAsLines().then((List<String> lines) {
int nbrLines = lines.length;
int _index = lines.indexOf('$_walletNbr:$_walletName:$_derivation'); int _index = lines.indexOf('$_walletNbr:$_walletName:$_derivation');
lines.removeWhere( print(nbrLines);
(element) => element == '$_walletNbr:$_walletName:$_derivation'); if (nbrLines != 1) {
lines.insert(_index, '$_walletNbr:$_newName:$_derivation'); lines.removeWhere((element) =>
element.contains('$_walletNbr:$_walletName:$_derivation'));
return lines.join('\n'); lines.insert(_index, '$_walletNbr:$_newName:$_derivation');
return lines.join('\n');
} else {
return '$_walletNbr:$_newName:$_derivation';
}
}); });
await _walletConfig.delete(); await _walletConfig.delete();
@ -158,16 +171,21 @@ class WalletOptionsProvider with ChangeNotifier {
final _walletConfig = final _walletConfig =
File('${walletsDirectory.path}/$_walletNbr/config.txt'); File('${walletsDirectory.path}/$_walletNbr/config.txt');
String newConfig = if (_derivation != -1) {
await _walletConfig.readAsLines().then((List<String> lines) { String newConfig =
lines.removeWhere( await _walletConfig.readAsLines().then((List<String> lines) {
(element) => element.contains('$_walletNbr:$_name:$_derivation')); lines.removeWhere(
(element) => element.contains('$_walletNbr:$_name:$_derivation'));
return lines.join('\n'); return lines.join('\n');
}); });
await _walletConfig.delete(); await _walletConfig.delete();
await _walletConfig.writeAsString(newConfig); await _walletConfig.writeAsString(newConfig);
} else {
final _walletFile = Directory('${walletsDirectory.path}/$_walletNbr');
await _walletFile.delete(recursive: true);
}
Navigator.pop(context); Navigator.pop(context);
} }
return 0; return 0;

View File

@ -0,0 +1,221 @@
import 'dart:async';
import 'package:gecko/models/generateWallets.dart';
import 'package:flutter/material.dart';
import 'package:gecko/models/myWallets.dart';
import 'package:provider/provider.dart';
class ImportWalletScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
GlobalKey _toolTipSecret = GlobalKey();
Timer _debounce;
GenerateWalletsProvider _generateWalletProvider =
Provider.of<GenerateWalletsProvider>(context);
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(context);
return WillPopScope(
onWillPop: () {
_generateWalletProvider.cesiumID.text = '';
_generateWalletProvider.cesiumPWD.text = '';
_generateWalletProvider.cesiumPubkey.text = '';
_generateWalletProvider.pin.text = '';
_generateWalletProvider.canImport = false;
_generateWalletProvider.isPinChanged = false;
return Future<bool>.value(true);
},
child: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () {
_generateWalletProvider.cesiumID.text = '';
_generateWalletProvider.cesiumPWD.text = '';
_generateWalletProvider.cesiumPubkey.text = '';
_generateWalletProvider.pin.text = '';
_generateWalletProvider.canImport = false;
_generateWalletProvider.isPinChanged = false;
Navigator.of(context).pop();
}),
title: SizedBox(
height: 22,
child: Text('Importer un portefeuille'),
)),
body: Builder(
builder: (ctx) => SafeArea(
child: Column(children: <Widget>[
SizedBox(height: 20),
TextFormField(
onChanged: (text) {
if (_debounce?.isActive ?? false)
// _generateWalletProvider.canImport = false;
// _generateWalletProvider.reloadBuild();
_debounce.cancel();
_debounce =
Timer(const Duration(milliseconds: 200), () {
print("ID Cesium tappé: $text");
_generateWalletProvider
.generateCesiumWalletPubkey(text,
_generateWalletProvider.cesiumPWD.text)
.then((value) {
_generateWalletProvider.canImport = true;
_generateWalletProvider.reloadBuild();
});
});
},
keyboardType: TextInputType.text,
controller: _generateWalletProvider.cesiumID,
obscureText: !_generateWalletProvider
.isCesiumIDVisible, //This will obscure text dynamically
decoration: InputDecoration(
hintText: 'Entrez votre identifiant Cesium',
suffixIcon: IconButton(
icon: Icon(
_generateWalletProvider.isCesiumIDVisible
? Icons.visibility
: Icons.visibility_off,
color: Colors.black,
),
onPressed: () {
_generateWalletProvider.cesiumIDisVisible();
},
),
),
),
SizedBox(height: 15),
TextFormField(
onChanged: (text) {
if (_debounce?.isActive ?? false)
// _generateWalletProvider.canImport = false;
// _generateWalletProvider.reloadBuild();
_debounce.cancel();
_debounce =
Timer(const Duration(milliseconds: 200), () {
print("ID Cesium tappé: $text");
_generateWalletProvider
.generateCesiumWalletPubkey(
_generateWalletProvider.cesiumID.text,
text)
.then((value) {
_generateWalletProvider.canImport = true;
_generateWalletProvider.reloadBuild();
});
});
},
keyboardType: TextInputType.text,
controller: _generateWalletProvider.cesiumPWD,
obscureText: !_generateWalletProvider
.isCesiumPWDVisible, //This will obscure text dynamically
decoration: InputDecoration(
hintText: 'Entrez votre mot de passe Cesium',
suffixIcon: IconButton(
icon: Icon(
_generateWalletProvider.isCesiumPWDVisible
? Icons.visibility
: Icons.visibility_off,
color: Colors.black,
),
onPressed: () {
_generateWalletProvider.cesiumPWDisVisible();
},
),
),
),
SizedBox(height: 15),
Text(
_generateWalletProvider.cesiumPubkey.text,
style: TextStyle(
fontSize: 14.0,
color: Colors.black,
fontWeight: FontWeight.bold,
fontFamily: 'Monospace'),
),
SizedBox(height: 20),
toolTips(_toolTipSecret, 'Code secret:',
"Retenez bien votre code secret, il vous sera demandé à chaque paiement, ainsi que pour configurer votre portefeuille"),
Container(
child: Stack(
alignment: Alignment.centerRight,
children: <Widget>[
TextField(
enabled: false,
controller: _generateWalletProvider.pin,
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: () {
_generateWalletProvider.changePinCode();
_generateWalletProvider.isPinChanged = true;
},
),
],
),
),
SizedBox(height: 30),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Color(0xffFFD68E), // background
onPrimary: Colors.black, // foreground
),
onPressed: _generateWalletProvider.canImport &&
_generateWalletProvider.isPinChanged
? () {
_generateWalletProvider
.importWallet(
context,
_generateWalletProvider
.cesiumID.text,
_generateWalletProvider
.cesiumPWD.text)
.then((value) {
_myWalletProvider.rebuildWidget();
});
}
: null,
child: Text('Importer ce portefeuille Cesium',
style: TextStyle(fontSize: 20))),
]),
))));
}
Widget toolTips(_key, _text, _message) {
return GestureDetector(
onTap: () {
final dynamic _toolTip = _key.currentState;
_toolTip.ensureTooltipVisible();
},
child: Tooltip(
padding: EdgeInsets.all(10),
key: _key,
showDuration: Duration(seconds: 5),
message: _message,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(width: 20),
Column(children: <Widget>[
SizedBox(
width: 30,
height: 25,
child: Icon(Icons.info_outline,
size: 22, color: Color(0xffD28928))),
SizedBox(height: 1)
]),
Text(
_text,
style: TextStyle(
fontSize: 15.0,
color: Colors.grey[600],
fontWeight: FontWeight.w400),
),
SizedBox(width: 45)
])));
}
}

View File

@ -2,6 +2,7 @@ import 'package:gecko/models/myWallets.dart';
import 'package:gecko/models/walletOptions.dart'; import 'package:gecko/models/walletOptions.dart';
import 'package:gecko/screens/myWallets/generateWallets.dart'; import 'package:gecko/screens/myWallets/generateWallets.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gecko/screens/myWallets/importWallet.dart';
import 'package:gecko/screens/myWallets/walletOptions.dart'; import 'package:gecko/screens/myWallets/walletOptions.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -78,7 +79,12 @@ class WalletsHome extends StatelessWidget {
primary: Color(0xffFFD68E), // background primary: Color(0xffFFD68E), // background
onPrimary: Colors.black, // foreground onPrimary: Colors.black, // foreground
), ),
onPressed: () => myWalletProvider.importWallet(), onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return ImportWalletScreen();
}),
),
child: Text('Importer un portefeuille existant', child: Text('Importer un portefeuille existant',
style: TextStyle(fontSize: 20))), style: TextStyle(fontSize: 20))),
])), ])),