diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart index 7395d2b..c94baf6 100644 --- a/lib/providers/substrate_sdk.dart +++ b/lib/providers/substrate_sdk.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:gecko/globals.dart'; @@ -14,9 +13,11 @@ class SubstrateSdk with ChangeNotifier { final WalletSDK sdk = WalletSDK(); final Keyring keyring = Keyring(); + String generatedMnemonic = ''; bool sdkReady = false; bool sdkLoading = false; bool nodeConnected = false; + bool importIsLoading = false; int blocNumber = 0; TextEditingController jsonKeystore = TextEditingController(); @@ -44,7 +45,6 @@ class SubstrateSdk with ChangeNotifier { ); if (res != null) { nodeConnected = true; - print("Connecté au noeud ${sdk.api.connectedNode!.name}"); notifyListeners(); } @@ -56,23 +56,38 @@ class SubstrateSdk with ChangeNotifier { } Future 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, keyType: KeyType.keystore, key: jsonKeystore.text.replaceAll("'", "\\'"), name: 'testKey', password: keystorePassword.text, - ); - final acc = await sdk.api.keyring.addAccount( + ) + .catchError((e) { + importIsLoading = false; + notifyListeners(); + }); + final acc = await sdk.api.keyring + .addAccount( keyring, keyType: KeyType.mnemonic, acc: json!, password: keystorePassword.text, - ); + ) + .catchError((e) { + importIsLoading = false; + notifyListeners(); + }); // await keystoreBox.clear(); await keystoreBox.add(acc.toJson()); Clipboard.setData(ClipboardData(text: jsonEncode(acc.toJson()))); + importIsLoading = false; notifyListeners(); } @@ -93,6 +108,8 @@ class SubstrateSdk with ChangeNotifier { Future generateMnemonic() async { final gen = await sdk.api.keyring.generateMnemonic(42); + generatedMnemonic = gen.mnemonic!; + notifyListeners(); return gen.mnemonic!; } } diff --git a/lib/screens/substrate_sandbox.dart b/lib/screens/substrate_sandbox.dart index 16dd38e..9dac1f9 100644 --- a/lib/screens/substrate_sandbox.dart +++ b/lib/screens/substrate_sandbox.dart @@ -43,56 +43,87 @@ class SubstrateSandBox extends StatelessWidget { Text( 'Noeud "${_sub.sdk.api.connectedNode!.name}", bloc N°${_sub.blocNumber}'), 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 ? '-' : _sub.getKeyStoreAddress().toString()), - const SizedBox(height: 40), - const Text('Trousseau:'), - TextField( - controller: _sub.jsonKeystore, - onChanged: (_) => _sub.reload(), - minLines: 5, - maxLines: 5, - ), + // const SizedBox(height: 40), + // const Text('Trousseau:'), + // TextField( + // controller: _sub.jsonKeystore, + // onChanged: (_) => _sub.reload(), + // minLines: 5, + // maxLines: 5, + // ), const SizedBox(height: 20), - const Text('Mot de passe:'), + const Text('Mot de passe du trousseau:'), TextField( controller: _sub.keystorePassword, obscureText: true, obscuringCharacter: '•', onChanged: (_) => _sub.reload(), ), - const SizedBox(height: 20), - Row(mainAxisAlignment: MainAxisAlignment.center, children: [ - ElevatedButton( - style: ElevatedButton.styleFrom( - primary: yellowC, // background - onPrimary: Colors.black, // foreground - ), - onPressed: _sub.jsonKeystore.text.isNotEmpty && - _sub.keystorePassword.text.isNotEmpty - ? () async => await _sub.importFromKeystore() - : null, - child: const Text( - 'Importer la trousseau', - style: TextStyle(fontSize: 20), - ), - ), - const SizedBox(height: 40), - ElevatedButton( - style: ElevatedButton.styleFrom( - primary: yellowC, // background - onPrimary: Colors.black, // foreground - ), - onPressed: () async => - print(await _sub.generateMnemonic()), - child: const Text( - 'Générer un mnemonic', - style: TextStyle(fontSize: 20), - ), - ), - ]), + Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const SizedBox(height: 20, width: double.infinity), + ElevatedButton( + style: ElevatedButton.styleFrom( + primary: yellowC, // background + onPrimary: Colors.black, // foreground + ), + onPressed: _sub.keystorePassword.text.isNotEmpty + ? () async { + await _sub.importFromKeystore(); + _sub.importIsLoading = false; + _sub.reload(); + } + : null, + child: const Text( + 'Importer le trousseau depuis le presse-papier', + style: TextStyle(fontSize: 20), + ), + ), + if (_sub.importIsLoading) + const CircularProgressIndicator(), + const SizedBox(height: 20), + ElevatedButton( + style: ElevatedButton.styleFrom( + 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, + ), + ) + ]) ]), ); }),