keyringEth.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'dart:convert';
  2. import 'package:polkawallet_sdk/api/types/addressIconData.dart';
  3. import 'package:polkawallet_sdk/ethers/apiEthers.dart';
  4. import 'package:polkawallet_sdk/service/index.dart';
  5. const default_derive_path = "m/44'/60'/0'/0/0";
  6. class ServiceKeyringEth {
  7. ServiceKeyringEth(this.serviceRoot);
  8. final SubstrateService serviceRoot;
  9. /// Generate a set of new mnemonic.
  10. Future<AddressIconDataWithMnemonic> generateMnemonic(
  11. {int? index, String? mnemonic}) async {
  12. final dynamic acc = await serviceRoot.webView!
  13. .evalJavascript('eth.keyring.gen("$mnemonic",$index)');
  14. return AddressIconDataWithMnemonic.fromJson(acc);
  15. }
  16. /// get address and avatar from mnemonic.
  17. Future<dynamic> addressFromMnemonic(
  18. {required String derivePath, required String mnemonic}) async {
  19. print('eth.keyring.addressFromMnemonic("$mnemonic","$derivePath")');
  20. final dynamic acc = await serviceRoot.webView!.evalJavascript(
  21. 'eth.keyring.addressFromMnemonic("$mnemonic","$derivePath")');
  22. return acc;
  23. }
  24. /// get address and avatar from privateKey.privateKey: string
  25. Future<dynamic> addressFromPrivateKey({required String privateKey}) async {
  26. final dynamic acc = await serviceRoot.webView!
  27. .evalJavascript('eth.keyring.addressFromPrivateKey("$privateKey")');
  28. return acc;
  29. }
  30. /// Import keyPair from mnemonic, privateKey or keystore.
  31. /// keyType: string, key: string, derivePath: string, password: string
  32. Future<Map> importAccount({
  33. required EVMKeyType keyType,
  34. required String key,
  35. required String name,
  36. required String password,
  37. String derivePath = default_derive_path,
  38. }) async {
  39. // generate json from js-api
  40. final String type = keyType.toString().split('.')[1];
  41. if (type == "keystore") {
  42. key = key.replaceAll("\"", "\\\"");
  43. }
  44. String code =
  45. 'eth.keyring.recover("$type", "$key", "$derivePath", "$password")';
  46. code = code.replaceAll(RegExp(r'\t|\n|\r'), '');
  47. final Map acc = _formatAccountData(
  48. (await serviceRoot.webView!.evalJavascript(code)) ?? {});
  49. return {...acc, type: key, 'name': name};
  50. }
  51. /// check password of account
  52. Future<bool> checkPassword(
  53. {required String keystore, required String pass}) async {
  54. final res = await serviceRoot.webView!
  55. .evalJavascript('eth.keyring.checkPassword($keystore, "$pass")');
  56. //An error Message is displayed if it fails :{ success: false, error: err.message }
  57. return res["success"];
  58. }
  59. /// change password of account
  60. Future<Map> changePassword(
  61. {required String keystore,
  62. required String passOld,
  63. required String passNew}) async {
  64. final res = await serviceRoot.webView!.evalJavascript(
  65. 'eth.keyring.changePassword($keystore, "$passOld", "$passNew")');
  66. return _formatAccountData(res ?? {});
  67. }
  68. /// sign message with private key of an account.
  69. Future<dynamic> signMessage(
  70. {required String message,
  71. required String keystore,
  72. required String pass}) async {
  73. final res = await serviceRoot.webView!.evalJavascript(
  74. 'eth.keyring.signMessage("$message", $keystore, "$pass")');
  75. return res;
  76. }
  77. /// get signer of a signature. so we can verify the signer.
  78. Future<Map> verifySignature(
  79. {required String message, required String signature}) async {
  80. final res = await serviceRoot.webView!.evalJavascript(
  81. 'eth.keyring.verifySignature("$message", "$signature")');
  82. return res;
  83. }
  84. /// Get icons of addresses
  85. /// return svg strings
  86. Future<List?> getAddressIcons(List addresses) async {
  87. final dynamic res = await serviceRoot.webView!
  88. .evalJavascript('eth.account.genIcons(${jsonEncode(addresses)})');
  89. return res;
  90. }
  91. Map _formatAccountData(Map acc) {
  92. final keystore = jsonDecode(acc['keystore'] ?? '{}');
  93. return {
  94. 'error': acc['error'],
  95. 'address': acc['address'],
  96. 'id': keystore['id'],
  97. 'version': keystore['version'],
  98. 'crypto': keystore['Crypto'],
  99. };
  100. }
  101. }