polkawallet_sdk.dart 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. library polkawallet_sdk;
  2. import 'dart:async';
  3. import 'dart:convert';
  4. import 'package:http/http.dart';
  5. import 'package:polkawallet_sdk/api/api.dart';
  6. import 'package:polkawallet_sdk/ethers/apiEthers.dart';
  7. import 'package:polkawallet_sdk/service/index.dart';
  8. import 'package:polkawallet_sdk/service/webViewRunner.dart';
  9. import 'package:polkawallet_sdk/storage/keyring.dart';
  10. import 'package:polkawallet_sdk/storage/keyringEVM.dart';
  11. /// SDK launchs a hidden webView to run polkadot.js/api for interacting
  12. /// with the substrate-based block-chain network.
  13. class WalletSDK {
  14. late PolkawalletApi api;
  15. late ApiEthers ethers;
  16. List<String> _blackList = [];
  17. get blackList => _blackList;
  18. final _service = SubstrateService();
  19. /// webView instance, this is the only instance of FlutterWebViewPlugin
  20. /// in App, we need to get it and reuse in other sdk.
  21. WebViewRunner? get webView => _service.webView;
  22. /// param [jsCode] is customized js code of parachain,
  23. /// the api works without [jsCode] param in Kusama/Polkadot.
  24. Future<void> init(
  25. Keyring keyring, {
  26. KeyringEVM? keyringEVM,
  27. WebViewRunner? webView,
  28. String? jsCode,
  29. Function? socketDisconnectedAction,
  30. bool isEVM = false,
  31. }) async {
  32. final c = Completer();
  33. await _service.init(
  34. keyring,
  35. webViewParam: webView,
  36. jsCode: jsCode,
  37. socketDisconnectedAction: socketDisconnectedAction,
  38. onInitiated: () {
  39. // inject keyPairs after webView launched
  40. _service.keyring.injectKeyPairsToWebView(keyring);
  41. // and initiate pubKeyIconsMap
  42. api.keyring.updatePubKeyIconsMap(keyring);
  43. if (keyringEVM != null) {
  44. _service.eth.keyring.injectKeyPairsToWebView(keyringEVM);
  45. api.eth.account.updateAddressIconsMap(keyringEVM);
  46. }
  47. _updateBlackList();
  48. if (!c.isCompleted) {
  49. c.complete();
  50. }
  51. },
  52. );
  53. api = PolkawalletApi(_service);
  54. ethers = ApiEthers(_service);
  55. return c.future;
  56. }
  57. Future<void> _updateBlackList() async {
  58. try {
  59. Response res =
  60. await get(Uri.parse('https://polkadot.js.org/phishing/address.json'));
  61. if (res.body.isNotEmpty) {
  62. final data = jsonDecode(res.body) as Map;
  63. final List<String> list = [];
  64. data.values.forEach((e) {
  65. list.addAll(List<String>.from(e));
  66. });
  67. final pubKeys = await api.account.decodeAddress(list);
  68. if (pubKeys != null) {
  69. _blackList = List<String>.from(pubKeys.keys.toList());
  70. }
  71. }
  72. } catch (err) {
  73. print(err);
  74. }
  75. }
  76. }