Improve keystore import

This commit is contained in:
poka 2022-02-18 19:49:37 +01:00
parent 900c4d2418
commit d2c89b1f14
2 changed files with 94 additions and 46 deletions

View File

@ -1,5 +1,4 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:gecko/globals.dart'; import 'package:gecko/globals.dart';
@ -14,9 +13,11 @@ class SubstrateSdk with ChangeNotifier {
final WalletSDK sdk = WalletSDK(); final WalletSDK sdk = WalletSDK();
final Keyring keyring = Keyring(); final Keyring keyring = Keyring();
String generatedMnemonic = '';
bool sdkReady = false; bool sdkReady = false;
bool sdkLoading = false; bool sdkLoading = false;
bool nodeConnected = false; bool nodeConnected = false;
bool importIsLoading = false;
int blocNumber = 0; int blocNumber = 0;
TextEditingController jsonKeystore = TextEditingController(); TextEditingController jsonKeystore = TextEditingController();
@ -44,7 +45,6 @@ class SubstrateSdk with ChangeNotifier {
); );
if (res != null) { if (res != null) {
nodeConnected = true; nodeConnected = true;
print("Connecté au noeud ${sdk.api.connectedNode!.name}");
notifyListeners(); notifyListeners();
} }
@ -56,23 +56,38 @@ class SubstrateSdk with ChangeNotifier {
} }
Future<void> importFromKeystore() async { Future<void> importFromKeystore() async {
final json = await sdk.api.keyring.importAccount( importIsLoading = true;
notifyListeners();
final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
if (clipboardData?.text != null) jsonKeystore.text = clipboardData!.text!;
final json = await sdk.api.keyring
.importAccount(
keyring, keyring,
keyType: KeyType.keystore, keyType: KeyType.keystore,
key: jsonKeystore.text.replaceAll("'", "\\'"), key: jsonKeystore.text.replaceAll("'", "\\'"),
name: 'testKey', name: 'testKey',
password: keystorePassword.text, password: keystorePassword.text,
); )
final acc = await sdk.api.keyring.addAccount( .catchError((e) {
importIsLoading = false;
notifyListeners();
});
final acc = await sdk.api.keyring
.addAccount(
keyring, keyring,
keyType: KeyType.mnemonic, keyType: KeyType.mnemonic,
acc: json!, acc: json!,
password: keystorePassword.text, password: keystorePassword.text,
); )
.catchError((e) {
importIsLoading = false;
notifyListeners();
});
// await keystoreBox.clear(); // await keystoreBox.clear();
await keystoreBox.add(acc.toJson()); await keystoreBox.add(acc.toJson());
Clipboard.setData(ClipboardData(text: jsonEncode(acc.toJson()))); Clipboard.setData(ClipboardData(text: jsonEncode(acc.toJson())));
importIsLoading = false;
notifyListeners(); notifyListeners();
} }
@ -93,6 +108,8 @@ class SubstrateSdk with ChangeNotifier {
Future<String> generateMnemonic() async { Future<String> generateMnemonic() async {
final gen = await sdk.api.keyring.generateMnemonic(42); final gen = await sdk.api.keyring.generateMnemonic(42);
generatedMnemonic = gen.mnemonic!;
notifyListeners();
return gen.mnemonic!; return gen.mnemonic!;
} }
} }

View File

@ -43,56 +43,87 @@ class SubstrateSandBox extends StatelessWidget {
Text( Text(
'Noeud "${_sub.sdk.api.connectedNode!.name}", bloc N°${_sub.blocNumber}'), 'Noeud "${_sub.sdk.api.connectedNode!.name}", bloc N°${_sub.blocNumber}'),
const SizedBox(height: 20), const SizedBox(height: 20),
const Text('Liste des trousseaux:'), Row(children: [
const Text('Liste des trousseaux:'),
const Spacer(),
InkWell(
child: Image.asset(
'assets/walletOptions/trash.png',
height: 35,
),
onTap: () async {
await keystoreBox.clear();
_sub.reload();
},
),
const SizedBox(width: 10),
]),
Text(keystoreBox.isEmpty Text(keystoreBox.isEmpty
? '-' ? '-'
: _sub.getKeyStoreAddress().toString()), : _sub.getKeyStoreAddress().toString()),
const SizedBox(height: 40), // const SizedBox(height: 40),
const Text('Trousseau:'), // const Text('Trousseau:'),
TextField( // TextField(
controller: _sub.jsonKeystore, // controller: _sub.jsonKeystore,
onChanged: (_) => _sub.reload(), // onChanged: (_) => _sub.reload(),
minLines: 5, // minLines: 5,
maxLines: 5, // maxLines: 5,
), // ),
const SizedBox(height: 20), const SizedBox(height: 20),
const Text('Mot de passe:'), const Text('Mot de passe du trousseau:'),
TextField( TextField(
controller: _sub.keystorePassword, controller: _sub.keystorePassword,
obscureText: true, obscureText: true,
obscuringCharacter: '', obscuringCharacter: '',
onChanged: (_) => _sub.reload(), onChanged: (_) => _sub.reload(),
), ),
const SizedBox(height: 20), Column(
Row(mainAxisAlignment: MainAxisAlignment.center, children: [ mainAxisAlignment: MainAxisAlignment.center,
ElevatedButton( children: [
style: ElevatedButton.styleFrom( const SizedBox(height: 20, width: double.infinity),
primary: yellowC, // background ElevatedButton(
onPrimary: Colors.black, // foreground style: ElevatedButton.styleFrom(
), primary: yellowC, // background
onPressed: _sub.jsonKeystore.text.isNotEmpty && onPrimary: Colors.black, // foreground
_sub.keystorePassword.text.isNotEmpty ),
? () async => await _sub.importFromKeystore() onPressed: _sub.keystorePassword.text.isNotEmpty
: null, ? () async {
child: const Text( await _sub.importFromKeystore();
'Importer la trousseau', _sub.importIsLoading = false;
style: TextStyle(fontSize: 20), _sub.reload();
), }
), : null,
const SizedBox(height: 40), child: const Text(
ElevatedButton( 'Importer le trousseau depuis le presse-papier',
style: ElevatedButton.styleFrom( style: TextStyle(fontSize: 20),
primary: yellowC, // background ),
onPrimary: Colors.black, // foreground ),
), if (_sub.importIsLoading)
onPressed: () async => const CircularProgressIndicator(),
print(await _sub.generateMnemonic()), const SizedBox(height: 20),
child: const Text( ElevatedButton(
'Générer un mnemonic', style: ElevatedButton.styleFrom(
style: TextStyle(fontSize: 20), primary: yellowC, // background
), onPrimary: Colors.black, // foreground
), ),
]), onPressed: () async {
await _sub.generateMnemonic();
},
child: const Text(
'Générer un mnemonic',
style: TextStyle(fontSize: 20),
),
),
const SizedBox(height: 10),
SizedBox(
width: 400,
child: Text(
_sub.generatedMnemonic,
textAlign: TextAlign.center,
),
)
])
]), ]),
); );
}), }),