polkawallet_sdk.dart 2.2 KB

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