123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:polkawallet_sdk/api/apiKeyring.dart';
- import 'package:polkawallet_sdk/api/types/addressIconData.dart';
- import 'package:polkawallet_sdk/service/index.dart';
- import 'package:polkawallet_sdk/storage/keyring.dart';
- class ServiceKeyring {
- ServiceKeyring(this.serviceRoot);
- final SubstrateService serviceRoot;
- Future<Map?> getPubKeyAddressMap(List keyPairs, List<int> ss58) async {
- final List<String> pubKeys =
- keyPairs.map((e) => e['pubKey'].toString()).toList();
- return await serviceRoot.account.encodeAddress(pubKeys, ss58);
- }
- Future<List?> getPubKeyIconsMap(List<String?> pubKeys) async {
- return await serviceRoot.account.getPubKeyIcons(pubKeys);
- }
- Future<Map?> injectKeyPairsToWebView(Keyring keyring) async {
- final resMap = Map<String, Map>();
- if (keyring.store.list.length > 0) {
- final String pairs = jsonEncode(keyring.store.list);
- final ss58 = keyring.store.ss58List;
- final res = Map<String, Map>.from(await serviceRoot.webView!
- .evalJavascript('keyring.initKeys($pairs, ${jsonEncode(ss58)})'));
- resMap.addAll(res);
- }
- if (keyring.store.contacts.length > 0) {
- final ss58 = keyring.store.ss58List;
- final contacts = await getPubKeyAddressMap(keyring.store.contacts, ss58);
- if (resMap.length > 0) {
- resMap.forEach((key, value) {
- resMap[key]!.addAll(contacts![key]);
- });
- } else {
- contacts!.forEach((key, value) {
- resMap[key] = value;
- });
- }
- }
- if (resMap.length > 0) {
- keyring.store.updatePubKeyAddressMap(resMap);
- return resMap;
- }
- return null;
- }
- Map updateKeyPairMetaData(Map acc, String? name) {
- acc['name'] = name;
- acc['meta']['name'] = name;
- if (acc['meta']['whenCreated'] == null) {
- acc['meta']['whenCreated'] = DateTime.now().millisecondsSinceEpoch;
- }
- acc['meta']['whenEdited'] = DateTime.now().millisecondsSinceEpoch;
- return acc;
- }
- /// Generate a set of new mnemonic.
- Future<AddressIconDataWithMnemonic> generateMnemonic(int ss58,
- {CryptoType cryptoType = CryptoType.sr25519,
- String derivePath = '',
- String key = ''}) async {
- final String crypto = cryptoType.toString().split('.')[1];
- final isAvatarSupport = (await serviceRoot.webView!.evalJavascript(
- 'keyring.addressFromMnemonic ? {}:null',
- wrapPromise: false)) !=
- null;
- final dynamic acc = await serviceRoot.webView!.evalJavascript(
- isAvatarSupport
- ? 'keyring.gen("$key",$ss58,"$crypto","$derivePath")'
- : 'keyring.gen()');
- return AddressIconDataWithMnemonic.fromJson(acc);
- }
- /// get address and avatar from mnemonic.
- Future<AddressIconData> addressFromMnemonic(int ss58,
- {CryptoType cryptoType = CryptoType.sr25519,
- String derivePath = '',
- required String mnemonic}) async {
- final String crypto = cryptoType.toString().split('.')[1];
- final isAvatarSupport = (await serviceRoot.webView!.evalJavascript(
- 'keyring.addressFromMnemonic ? {}:null',
- wrapPromise: false)) !=
- null;
- final dynamic acc = isAvatarSupport
- ? (await serviceRoot.webView!.evalJavascript(
- 'keyring.addressFromMnemonic("$mnemonic",$ss58,"$crypto","$derivePath")'))
- : {};
- return AddressIconData.fromJson(Map<String, dynamic>.from(acc));
- }
- /// get address and avatar from rawSeed.
- Future<AddressIconData> addressFromRawSeed(int ss58,
- {CryptoType cryptoType = CryptoType.sr25519,
- String derivePath = '',
- required String rawSeed}) async {
- final String crypto = cryptoType.toString().split('.')[1];
- final isAvatarSupport = (await serviceRoot.webView!.evalJavascript(
- 'keyring.addressFromMnemonic ? {}:null',
- wrapPromise: false)) !=
- null;
- final dynamic acc = isAvatarSupport
- ? (await serviceRoot.webView!.evalJavascript(
- 'keyring.addressFromRawSeed("$rawSeed",$ss58,"$crypto","$derivePath")'))
- : {};
- return AddressIconData.fromJson(Map<String, dynamic>.from(acc));
- }
- /// get address and avatar from KeyStore.
- Future<dynamic> addressFromKeyStore(int ss58, {required Map keyStore}) async {
- final String addressOld = keyStore['address'];
- final dynamic acc = await serviceRoot.webView!
- .evalJavascript('account.decodeAddress(["$addressOld"])'
- '.then(res => account.encodeAddress(Object.keys(res), [$ss58]))'
- '.then(res => account.genIcons(Object.values(res[$ss58])))');
- return acc;
- }
- /// check mnemonic valid.
- Future<bool> checkMnemonicValid(String mnemonic) async {
- final isApiSupport = (await serviceRoot.webView!.evalJavascript(
- 'keyring.checkMnemonicValid ? {}:null',
- wrapPromise: false)) !=
- null;
- final bool res = isApiSupport
- ? (await serviceRoot.webView!
- .evalJavascript('keyring.checkMnemonicValid("$mnemonic")'))
- : true;
- return res;
- }
- /// Import account from mnemonic/rawSeed/keystore.
- /// param [cryptoType] can be `sr25519`(default) or `ed25519`.
- /// return [null] if import failed.
- Future<dynamic> importAccount({
- required KeyType keyType,
- required String key,
- required name,
- required password,
- CryptoType cryptoType = CryptoType.sr25519,
- String derivePath = '',
- }) async {
- // generate json from js-api
- final String type = keyType.toString().split('.')[1];
- final String crypto = cryptoType.toString().split('.')[1];
- String code =
- 'keyring.recover("$type", "$crypto", \'$key$derivePath\', "$password")';
- code = code.replaceAll(RegExp(r'\t|\n|\r'), '');
- final dynamic acc = await serviceRoot.webView!.evalJavascript(code);
- if (acc == null || acc['error'] != null) {
- return acc;
- }
- // add metadata to json
- updateKeyPairMetaData(acc, name);
- return acc;
- }
- /// check password of account
- Future<bool> checkPassword(String? pubKey, pass) async {
- final res = await serviceRoot.webView!
- .evalJavascript('keyring.checkPassword("$pubKey", "$pass")');
- if (res == null) {
- return false;
- }
- return true;
- }
- /// change password of account
- Future<Map?> changePassword(String? pubKey, passOld, passNew) async {
- final res = await serviceRoot.webView!.evalJavascript(
- 'keyring.changePassword("$pubKey", "$passOld", "$passNew")');
- return res;
- }
- Future<String?> checkDerivePath(
- String seed, path, CryptoType cryptoType) async {
- final String crypto = cryptoType.toString().split('.')[1];
- dynamic res = await serviceRoot.webView!
- .evalJavascript('keyring.checkDerivePath("$seed", "$path", "$crypto")');
- return res;
- }
- Future<Map?> signAsExtension(String password, Map args) async {
- final String call = args['msgType'] == 'pub(bytes.sign)'
- ? 'signBytesAsExtension'
- : 'signTxAsExtension';
- final res = await serviceRoot.webView!.evalJavascript(
- 'keyring.$call("$password", ${jsonEncode(args['request'])})',
- allowRepeat: true,
- );
- return res;
- }
- Future<Map?> signatureVerify(String message, signature, address) async {
- final res = await serviceRoot.webView!.evalJavascript(
- 'keyring.verifySignature("$message", "$signature", "$address")',
- allowRepeat: true,
- );
- if (res == null) {
- return null;
- }
- return res;
- }
- }
|