walletConnect.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:polkawallet_sdk/service/index.dart';
  4. class ServiceWalletConnect {
  5. ServiceWalletConnect(this.serviceRoot);
  6. final SubstrateService serviceRoot;
  7. void initClient(Function(Map) onPairing, Function(Map) onPaired,
  8. Function(Map) onPayload) {
  9. serviceRoot.webView!.addMsgHandler("walletConnectPayload", onPayload);
  10. serviceRoot.webView!.addMsgHandler("walletConnectPairing", onPairing);
  11. serviceRoot.webView!.addMsgHandler("walletConnectCreated", onPaired);
  12. serviceRoot.webView!.evalJavascript('walletConnect.initClient()');
  13. }
  14. Future<Map?> connect(String uri) async {
  15. return await serviceRoot.webView!
  16. .evalJavascript('walletConnect.connect("$uri")');
  17. }
  18. Future<Map?> disconnect(Map params) async {
  19. final dynamic res = await serviceRoot.webView!
  20. .evalJavascript('walletConnect.disconnect(${jsonEncode(params)})');
  21. serviceRoot.webView!.removeMsgHandler("walletConnectPayload");
  22. serviceRoot.webView!.removeMsgHandler("walletConnectPairing");
  23. serviceRoot.webView!.removeMsgHandler("walletConnectCreated");
  24. return res;
  25. }
  26. Future<Map?> approvePairing(Map proposal, String address) async {
  27. final dynamic res = await serviceRoot.webView!.evalJavascript(
  28. 'walletConnect.approveProposal(${jsonEncode(proposal)}, "$address")');
  29. return res;
  30. }
  31. Future<Map?> rejectPairing(Map proposal) async {
  32. final dynamic res = await serviceRoot.webView!.evalJavascript(
  33. 'walletConnect.rejectProposal(${jsonEncode(proposal)})');
  34. return res;
  35. }
  36. Future<Map?> signPayload(Map payload, String password) async {
  37. final dynamic res = await serviceRoot.webView!.evalJavascript(
  38. 'walletConnect.signPayload(api, ${jsonEncode(payload)}, "$password")');
  39. return res;
  40. }
  41. Future<Map?> payloadRespond(String topic, Map response) async {
  42. final dynamic res = await serviceRoot.webView!.evalJavascript(
  43. 'walletConnect.payloadRespond("$topic", ${jsonEncode(response)})');
  44. return res;
  45. }
  46. }