apiKeyringEth.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import 'package:polkawallet_sdk/api/types/addressIconData.dart';
  2. import 'package:polkawallet_sdk/api/types/walletConnect/payloadData.dart';
  3. import 'package:polkawallet_sdk/ethers/apiEthers.dart';
  4. import 'package:polkawallet_sdk/service/eth/keyringEth.dart';
  5. import 'package:polkawallet_sdk/storage/keyringEVM.dart';
  6. import 'package:polkawallet_sdk/storage/types/ethWalletData.dart';
  7. import 'package:polkawallet_sdk/storage/types/keyPairData.dart';
  8. import 'package:polkawallet_sdk/webviewWithExtension/types/signExtrinsicParam.dart';
  9. import '../api.dart';
  10. class ApiKeyringEth {
  11. ApiKeyringEth(this.apiRoot, this.service);
  12. final PolkawalletApi apiRoot;
  13. final ServiceKeyringEth service;
  14. /// Generate a set of new mnemonic.
  15. Future<AddressIconDataWithMnemonic> generateMnemonic(
  16. {int? index, String? mnemonic}) async {
  17. final mnemonicData = await service.generateMnemonic(
  18. index: index ?? 0, mnemonic: mnemonic ?? "");
  19. return mnemonicData;
  20. }
  21. /// get address and avatar from mnemonic.
  22. Future<AddressIconData> addressFromMnemonic(
  23. {String? derivePath, required String mnemonic}) async {
  24. final acc = await service.addressFromMnemonic(
  25. derivePath: derivePath, mnemonic: mnemonic);
  26. if (acc['error'] != null) {
  27. throw Exception(acc['error']);
  28. }
  29. return AddressIconData.fromJson(acc);
  30. }
  31. /// get address and avatar from privateKey.privateKey: string
  32. Future<AddressIconData> addressFromPrivateKey(
  33. {required String privateKey}) async {
  34. final acc = await service.addressFromPrivateKey(privateKey: privateKey);
  35. if (acc['error'] != null) {
  36. throw Exception(acc['error']);
  37. }
  38. return AddressIconData.fromJson(acc);
  39. }
  40. Future<Map?> importAccount({
  41. required EVMKeyType keyType,
  42. required String key,
  43. required String name,
  44. required String password,
  45. }) async {
  46. final acc = await service.importAccount(
  47. keyType: keyType, key: key, password: password, name: name);
  48. if (acc['error'] != null) {
  49. throw Exception(acc['error']);
  50. }
  51. return acc;
  52. }
  53. /// check password of account
  54. Future<bool> checkPassword(String address, String pass) async {
  55. final res = await service.checkPassword(address: address, pass: pass);
  56. return res;
  57. }
  58. /// change password of account
  59. Future<EthWalletData?> changePassword(
  60. KeyringEVM keyring, String passOld, String passNew) async {
  61. // 1. change password of keyPair in webView
  62. final res = await service.changePassword(
  63. address: keyring.current.address ?? '',
  64. passNew: passNew,
  65. passOld: passOld);
  66. if (res['error'] != null) {
  67. throw Exception(res['error']);
  68. }
  69. res['name'] = keyring.current.name;
  70. // 2. if success in webView, then update encrypted seed in local storage.
  71. keyring.store
  72. .updateEncryptedSeed(keyring.current.address!, passOld, passNew);
  73. // update keyPair data in storage
  74. keyring.store.updateAccount(res);
  75. return EthWalletData.fromJson(res);
  76. }
  77. /// Add account to local storage.
  78. Future<EthWalletData> addAccount(
  79. KeyringEVM keyring, {
  80. required EVMKeyType keyType,
  81. required Map acc,
  82. required String password,
  83. }) async {
  84. // save seed and remove it before add account
  85. final String type = keyType.toString().split('.')[1];
  86. if (keyType == EVMKeyType.mnemonic || keyType == EVMKeyType.privateKey) {
  87. final String? seed = acc[type];
  88. if (seed != null && seed.isNotEmpty) {
  89. //acc['pubKey'], acc[type], type, password
  90. keyring.store
  91. .encryptSeedAndSave(acc['address'], acc[type], type, password);
  92. }
  93. }
  94. acc.remove(type);
  95. // save keystore to storage
  96. await keyring.store.addAccount(acc);
  97. await apiRoot.eth.account.updateAddressIconsMap(keyring, [acc['address']]);
  98. return EthWalletData.fromJson(acc);
  99. }
  100. /// change name of account
  101. Future<EthWalletData> changeName(KeyringEVM keyring, String name) async {
  102. final json = keyring.current.toJson();
  103. json['name'] = name;
  104. // update keyPair date in storage
  105. keyring.store.updateAccount(json);
  106. return EthWalletData.fromJson(json);
  107. }
  108. /// delete account from storage
  109. Future<void> deleteAccount(KeyringEVM keyring, EthWalletData account) async {
  110. await keyring.store.deleteAccount(account.address);
  111. }
  112. /// Open a new webView for a DApp,
  113. /// sign extrinsic or msg for the DApp.
  114. Future<ExtensionSignResult?> signMessage(
  115. String password,
  116. String message,
  117. String address,
  118. ) async {
  119. final signature = await service.signMessage(
  120. address: address, message: message, pass: password);
  121. if (signature == null) {
  122. return null;
  123. }
  124. if (signature['error'] != null) {
  125. throw Exception(signature['error']);
  126. }
  127. final ExtensionSignResult res = ExtensionSignResult();
  128. res.signature = signature['signature'];
  129. return res;
  130. }
  131. /// get signer of a signature. so we can verify the signer.
  132. Future<Map> signatureVerify(String message, String signature) async {
  133. final res =
  134. await service.verifySignature(message: message, signature: signature);
  135. if (res['error'] != null) {
  136. throw Exception(res['error']);
  137. }
  138. return res;
  139. }
  140. /// Decrypt and get the backup of seed.
  141. Future<SeedBackupData?> getDecryptedSeed(KeyringEVM keyring, password) async {
  142. final Map? data = await keyring.store
  143. .getDecryptedSeed(keyring.current.address!, password);
  144. if (data == null) {
  145. return null;
  146. }
  147. if (data['seed'] == null) {
  148. data['error'] = 'wrong password';
  149. }
  150. return SeedBackupData.fromJson(data as Map<String, dynamic>);
  151. }
  152. /// Add a contact.
  153. Future<EthWalletData> addContact(KeyringEVM keyring, Map acc) async {
  154. // save keystore to storage
  155. await keyring.store.addContact(acc);
  156. await apiRoot.eth.account.updateAddressIconsMap(keyring, [acc['address']]);
  157. return EthWalletData.fromJson(acc);
  158. }
  159. Future<Map> transfer(
  160. {required String token,
  161. required double amount,
  162. required String to,
  163. required String sender,
  164. required String pass,
  165. required Map gasOptions,
  166. required Function(Map) onStatusChange}) async {
  167. return service.transfer(
  168. token: token,
  169. amount: amount,
  170. to: to,
  171. sender: sender,
  172. pass: pass,
  173. gasOptions: gasOptions,
  174. onStatusChange: onStatusChange);
  175. }
  176. Future<int> estimateTransferGas(
  177. {required String token,
  178. required double amount,
  179. required String to,
  180. required String from}) async {
  181. return service.estimateTransferGas(
  182. token: token, amount: amount, to: to, from: from);
  183. }
  184. Future<String?> getGasPrice() async {
  185. return service.getGasPrice();
  186. }
  187. Future<List> renderEthRequest(Map payload) async {
  188. return service.renderEthRequest(payload);
  189. }
  190. Future<WCCallRequestResult> signEthRequest(
  191. Map payload, String address, String pass, Map gasOptions) async {
  192. final Map res =
  193. await service.signEthRequest(payload, address, pass, gasOptions);
  194. return WCCallRequestResult.fromJson(res);
  195. }
  196. }