WIP: Work about derivation: Can't get seed from stored keypair

This commit is contained in:
poka 2022-02-20 20:49:05 +01:00
parent 90c0765c91
commit 981094ab0e
2 changed files with 92 additions and 35 deletions

View File

@ -1,4 +1,7 @@
// ignore_for_file: avoid_print
import 'dart:convert'; import 'dart:convert';
import "package:hex/hex.dart";
import 'dart:io'; import 'dart:io';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:fast_base58/fast_base58.dart'; import 'package:fast_base58/fast_base58.dart';
@ -9,7 +12,12 @@ import 'package:polkawallet_sdk/api/types/networkParams.dart';
import 'package:polkawallet_sdk/api/types/txInfoData.dart'; import 'package:polkawallet_sdk/api/types/txInfoData.dart';
import 'package:polkawallet_sdk/polkawallet_sdk.dart'; import 'package:polkawallet_sdk/polkawallet_sdk.dart';
import 'package:polkawallet_sdk/storage/keyring.dart'; import 'package:polkawallet_sdk/storage/keyring.dart';
import 'package:polkawallet_sdk/storage/localStorage.dart';
import 'package:polkawallet_sdk/utils/index.dart';
import 'package:polkawallet_sdk/utils/localStorage.dart';
import 'package:truncate/truncate.dart'; import 'package:truncate/truncate.dart';
import 'package:get_storage/get_storage.dart';
import 'package:aes_ecb_pkcs5_flutter/aes_ecb_pkcs5_flutter.dart';
class SubstrateSdk with ChangeNotifier { class SubstrateSdk with ChangeNotifier {
final List subNode = ['127.0.0.1:9944', '192.168.1.85:9944']; final List subNode = ['127.0.0.1:9944', '192.168.1.85:9944'];
@ -65,7 +73,8 @@ class SubstrateSdk with ChangeNotifier {
}); });
} }
Future<bool> importAccount({bool fromMnemonic = false}) async { Future<bool> importAccount(
{bool fromMnemonic = false, String derivePath = ''}) async {
final KeyType keytype; final KeyType keytype;
final String keyToImport; final String keyToImport;
if (fromMnemonic) { if (fromMnemonic) {
@ -80,19 +89,19 @@ class SubstrateSdk with ChangeNotifier {
notifyListeners(); notifyListeners();
final clipboardData = await Clipboard.getData(Clipboard.kTextPlain); final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
if (clipboardData?.text != null) jsonKeystore.text = clipboardData!.text!; if (clipboardData?.text != null) jsonKeystore.text = clipboardData!.text!;
final json = await sdk.api.keyring var json = await sdk.api.keyring
.importAccount( .importAccount(keyring,
keyring, keyType: keytype,
keyType: keytype, key: keyToImport,
key: keyToImport, name: 'testKey',
name: 'testKey', password: keystorePassword.text,
password: keystorePassword.text, derivePath: derivePath)
)
.catchError((e) { .catchError((e) {
importIsLoading = false; importIsLoading = false;
notifyListeners(); notifyListeners();
}); });
if (json == null) return false; if (json == null) return false;
print(json);
try { try {
final acc = await sdk.api.keyring.addAccount( final acc = await sdk.api.keyring.addAccount(
keyring, keyring,
@ -178,6 +187,46 @@ class SubstrateSdk with ChangeNotifier {
print(err.toString()); print(err.toString());
} }
} }
derive(BuildContext context, String address, double amount,
String password) async {
final keypair =
keyring.keyPairs.firstWhere((element) => element.address == address);
// KeyringStorage _storage = KeyringStorage();
// print(_storage.encryptedMnemonics.val);
// final keyPairs = [].val('keyPairs', getBox: _storage);
// final encryptedRawSeeds = {}.val('encryptedRawSeeds', getBox: _storage);
print(await sdk.api.keyring.getDecryptedSeed(keyring, password));
print(await keyring.store.getDecryptedSeed(keypair.pubKey, password));
print(
await keyring.store.checkSeedExist(KeyType.keystore, keypair.pubKey!));
print(
await keyring.store.checkSeedExist(KeyType.mnemonic, keypair.pubKey!));
_storage() => GetStorage(sdk_storage_key);
final encryptedMnemonics = {}.val('encryptedMnemonics', getBox: _storage);
// await keyring.store.updateEncryptedSeed(keypair.pubKey, password, '012');
final enc = encryptedMnemonics.val[keypair.pubKey!];
// final key = Encrypt.passwordToEncryptKey(password);
// const enc =
// '69E16A6F8CD15799FC036B3D71FC3C53EFCEDB6A2D4F2A743555809DC5B48D6FC25E96E5CFF8E7DF4FCE1AE9AFC61A0D85CDCAD945C5371F11DEBA1BF362B8124A4C8D264A05AC1D72F5A9566D0D3B35';
final LocalStorage _storageOld = LocalStorage();
print(enc);
print(await _storageOld.getAccountList());
print(await _storageOld.getSeeds('keystore'));
// print(await FlutterAesEcbPkcs5.decryptString(enc, key));
// generatedMnemonic = keypair.encoded;
// importAccount();
}
} }
void snack(BuildContext context, String message, {int duration = 2}) { void snack(BuildContext context, String message, {int duration = 2}) {

View File

@ -32,11 +32,9 @@ class SubstrateSandBox extends StatelessWidget {
children: <Widget>[ children: <Widget>[
Text('js-api chargé ?: ${_sub.sdkReady}'), Text('js-api chargé ?: ${_sub.sdkReady}'),
InkWell( InkWell(
onTap: !_sub.nodeConnected onTap: () async {
? () async {
await _sub.connectNode(); await _sub.connectNode();
} },
: null,
child: Text( child: Text(
'Noeud connecté ?: ${_sub.nodeConnected} (${_sub.subNode})')), 'Noeud connecté ?: ${_sub.nodeConnected} (${_sub.subNode})')),
if (_sub.nodeConnected) if (_sub.nodeConnected)
@ -63,29 +61,39 @@ class SubstrateSandBox extends StatelessWidget {
builder: (BuildContext context, builder: (BuildContext context,
AsyncSnapshot<List<AddressInfo>> _data) { AsyncSnapshot<List<AddressInfo>> _data) {
return Column(children: [ return Column(children: [
for (final AddressInfo e in _data.data!) if (_data.data != null)
Row(children: [ for (final AddressInfo e in _data.data!)
InkWell( Row(children: [
onTap: () => _sub.keyring.setCurrent(_sub InkWell(
.keyring.keyPairs onTap: () => _sub.keyring.setCurrent(_sub
.firstWhere((element) => .keyring.keyPairs
element.address == e.address!)), .firstWhere((element) =>
child: Text( element.address == e.address!)),
getShortPubkey(e.address!), child: Text(
style: const TextStyle( getShortPubkey(e.address!),
fontFamily: 'Monospace'), style: const TextStyle(
fontFamily: 'Monospace'),
),
), ),
), const SizedBox(width: 20),
const SizedBox(width: 20), InkWell(
InkWell( onTap: () async => await _sub.pay(
onTap: () async => await _sub.pay( context,
context, e.address!,
e.address!, 10,
10, _sub.keystorePassword.text),
_sub.keystorePassword.text), child: Text("${e.balance.toString()} ğdev"),
child: Text("${e.balance.toString()} ğdev"), ),
) const SizedBox(width: 20),
]) InkWell(
onTap: () async => await _sub.derive(
context,
e.address!,
3,
_sub.keystorePassword.text),
child: const Text("Dériver"),
)
])
]); ]);
}), }),
const SizedBox(height: 20), const SizedBox(height: 20),