apiAccount.dart 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'package:polkawallet_sdk/api/api.dart';
  2. import 'package:polkawallet_sdk/api/types/balanceData.dart';
  3. import 'package:polkawallet_sdk/service/account.dart';
  4. class ApiAccount {
  5. ApiAccount(this.apiRoot, this.service);
  6. final PolkawalletApi apiRoot;
  7. final ServiceAccount service;
  8. /// encode addresses to publicKeys
  9. Future<Map?> encodeAddress(List<String> pubKeys) async {
  10. final int? ss58 = apiRoot.connectedNode!.ss58;
  11. final Map? res = await service.encodeAddress(pubKeys, [ss58]);
  12. if (res != null) {
  13. return res[ss58.toString()];
  14. }
  15. return null;
  16. }
  17. /// decode addresses to publicKeys
  18. Future<Map?> decodeAddress(List<String> addresses) async {
  19. return service.decodeAddress(addresses);
  20. }
  21. /// check address matches ss58Format
  22. Future<bool?> checkAddressFormat(String address, int ss58) async {
  23. return service.checkAddressFormat(address, ss58);
  24. }
  25. /// query balance
  26. Future<BalanceData?> queryBalance(String? address) async {
  27. final res = await service.queryBalance(address);
  28. return res != null
  29. ? BalanceData.fromJson(res as Map<String, dynamic>)
  30. : null;
  31. }
  32. /// subscribe balance
  33. /// @return [String] msgChannel, call unsubscribeMessage(msgChannel) to unsub.
  34. Future<String> subscribeBalance(
  35. String? address,
  36. Function(BalanceData) onUpdate,
  37. ) async {
  38. final msgChannel = 'Balance';
  39. final code = 'account.getBalance(api, "$address", "$msgChannel")';
  40. await apiRoot.service.webView!.subscribeMessage(
  41. code, msgChannel, (data) => onUpdate(BalanceData.fromJson(data)));
  42. return msgChannel;
  43. }
  44. /// unsubscribe balance
  45. void unsubscribeBalance() {
  46. final msgChannel = 'Balance';
  47. apiRoot.unsubscribeMessage(msgChannel);
  48. }
  49. /// Get on-chain account info of addresses
  50. Future<List?> queryIndexInfo(List addresses) async {
  51. if (addresses.isEmpty) {
  52. return [];
  53. }
  54. return service.queryIndexInfo(addresses);
  55. }
  56. /// query address with account index
  57. Future<String?> queryAddressWithAccountIndex(String index) async {
  58. final res = await service.queryAddressWithAccountIndex(
  59. index, apiRoot.connectedNode!.ss58);
  60. if (res != null) {
  61. return res[0];
  62. }
  63. return null;
  64. }
  65. /// Get icons of pubKeys
  66. /// return svg strings
  67. Future<List?> getPubKeyIcons(List<String> keys) async {
  68. if (keys.length == 0) {
  69. return [];
  70. }
  71. return service.getPubKeyIcons(keys);
  72. }
  73. /// Get icons of addresses
  74. /// return svg strings
  75. Future<List?> getAddressIcons(List addresses) async {
  76. if (addresses.isEmpty) {
  77. return [];
  78. }
  79. return service.getAddressIcons(addresses);
  80. }
  81. }