@aeternity/aepp-sdk 10.0.0-beta.1 → 11.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/README.md +15 -14
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -27
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/account/{base.js → base.mjs} +23 -11
  10. package/es/account/base.mjs.map +1 -0
  11. package/es/account/{memory.js → memory.mjs} +29 -14
  12. package/es/account/memory.mjs.map +1 -0
  13. package/es/account/{multiple.js → multiple.mjs} +14 -11
  14. package/es/account/multiple.mjs.map +1 -0
  15. package/es/ae/{aens.js → aens.mjs} +69 -59
  16. package/es/ae/aens.mjs.map +1 -0
  17. package/es/ae/{aepp.js → aepp.mjs} +9 -9
  18. package/es/ae/aepp.mjs.map +1 -0
  19. package/es/ae/contract.mjs +159 -0
  20. package/es/ae/contract.mjs.map +1 -0
  21. package/es/ae/{index.js → index.mjs} +12 -10
  22. package/es/ae/index.mjs.map +1 -0
  23. package/es/ae/{oracle.js → oracle.mjs} +37 -20
  24. package/es/ae/oracle.mjs.map +1 -0
  25. package/es/ae/{universal.js → universal.mjs} +10 -10
  26. package/es/ae/universal.mjs.map +1 -0
  27. package/es/ae/{wallet.js → wallet.mjs} +10 -10
  28. package/es/ae/wallet.mjs.map +1 -0
  29. package/es/aepp-wallet-communication/schema.d.ts +74 -0
  30. package/es/bignumber.d.ts +1 -7
  31. package/es/chain/{index.js → index.mjs} +28 -8
  32. package/es/chain/index.mjs.map +1 -0
  33. package/es/chain/{node.js → node.mjs} +133 -90
  34. package/es/chain/node.mjs.map +1 -0
  35. package/es/channel/{handlers.js → handlers.mjs} +18 -20
  36. package/es/channel/handlers.mjs.map +1 -0
  37. package/es/channel/{index.js → index.mjs} +43 -22
  38. package/es/channel/index.mjs.map +1 -0
  39. package/es/channel/{internal.js → internal.mjs} +51 -35
  40. package/es/channel/internal.mjs.map +1 -0
  41. package/es/contract/aci.mjs +477 -0
  42. package/es/contract/aci.mjs.map +1 -0
  43. package/es/contract/compiler.mjs +115 -0
  44. package/es/contract/compiler.mjs.map +1 -0
  45. package/es/contract/ga/helpers.mjs +30 -0
  46. package/es/contract/ga/helpers.mjs.map +1 -0
  47. package/es/contract/ga/{index.js → index.mjs} +40 -28
  48. package/es/contract/ga/index.mjs.map +1 -0
  49. package/es/encoder.d.ts +26 -0
  50. package/es/errors.d.ts +284 -0
  51. package/es/index.mjs +57 -0
  52. package/es/index.mjs.map +1 -0
  53. package/es/node-pool/{index.js → index.mjs} +15 -12
  54. package/es/node-pool/index.mjs.map +1 -0
  55. package/es/{node.js → node.mjs} +17 -11
  56. package/es/node.mjs.map +1 -0
  57. package/es/tx/builder/constants.mjs +108 -0
  58. package/es/tx/builder/constants.mjs.map +1 -0
  59. package/es/tx/builder/field-types.mjs +74 -0
  60. package/es/tx/builder/field-types.mjs.map +1 -0
  61. package/es/tx/builder/helpers.mjs +321 -0
  62. package/es/tx/builder/helpers.mjs.map +1 -0
  63. package/es/tx/builder/{index.js → index.mjs} +92 -74
  64. package/es/tx/builder/index.mjs.map +1 -0
  65. package/es/tx/builder/{schema.js → schema.mjs} +16 -172
  66. package/es/tx/builder/schema.mjs.map +1 -0
  67. package/es/tx/{index.js → index.mjs} +106 -14
  68. package/es/tx/index.mjs.map +1 -0
  69. package/es/tx/{tx-object.js → tx-object.mjs} +31 -18
  70. package/es/tx/tx-object.mjs.map +1 -0
  71. package/es/tx/{tx.js → tx.mjs} +15 -14
  72. package/es/tx/tx.mjs.map +1 -0
  73. package/es/tx/{validator.js → validator.mjs} +72 -33
  74. package/es/tx/validator.mjs.map +1 -0
  75. package/es/typings/blakejs/index.d.mjs +2 -0
  76. package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
  77. package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +10 -8
  78. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
  79. package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +25 -17
  80. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
  81. package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
  82. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
  83. package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +15 -8
  84. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
  85. package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
  86. package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
  87. package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +126 -72
  88. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
  89. package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +23 -14
  90. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
  91. package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +116 -91
  92. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
  93. package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
  94. package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
  95. package/es/utils/aepp-wallet-communication/{wallet-detector.js → wallet-detector.mjs} +6 -5
  96. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  97. package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
  98. package/es/utils/amount-formatter.mjs.map +1 -0
  99. package/es/utils/{async-init.js → async-init.mjs} +8 -11
  100. package/es/utils/async-init.mjs.map +1 -0
  101. package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
  102. package/es/utils/bignumber.mjs.map +1 -0
  103. package/es/utils/{bytes.js → bytes.mjs} +16 -8
  104. package/es/utils/bytes.mjs.map +1 -0
  105. package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
  106. package/es/utils/crypto-ts.mjs.map +1 -0
  107. package/es/utils/{crypto.js → crypto.mjs} +42 -156
  108. package/es/utils/crypto.mjs.map +1 -0
  109. package/es/utils/encoder.mjs +106 -0
  110. package/es/utils/encoder.mjs.map +1 -0
  111. package/es/utils/errors.mjs +689 -0
  112. package/es/utils/errors.mjs.map +1 -0
  113. package/es/utils/{hd-wallet.js → hd-wallet.mjs} +33 -34
  114. package/es/utils/hd-wallet.mjs.map +1 -0
  115. package/es/utils/{json-big.js → json-big.mjs} +1 -1
  116. package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
  117. package/es/utils/{keystore.js → keystore.mjs} +63 -51
  118. package/es/utils/keystore.mjs.map +1 -0
  119. package/es/utils/{mptree.js → mptree.mjs} +21 -12
  120. package/es/utils/mptree.mjs.map +1 -0
  121. package/es/utils/{other.js → other.mjs} +2 -2
  122. package/es/utils/other.mjs.map +1 -0
  123. package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
  124. package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
  125. package/es/utils/{string.js → string.mjs} +1 -1
  126. package/es/utils/{string.js.map → string.mjs.map} +1 -1
  127. package/es/utils/{swagger.js → swagger.mjs} +49 -15
  128. package/es/utils/swagger.mjs.map +1 -0
  129. package/package.json +49 -58
  130. package/src/account/base.js +3 -1
  131. package/src/account/memory.js +13 -8
  132. package/src/account/multiple.js +16 -6
  133. package/src/ae/aens.js +52 -58
  134. package/src/ae/aepp.js +2 -2
  135. package/src/ae/contract.js +21 -154
  136. package/src/ae/index.js +6 -4
  137. package/src/ae/oracle.js +33 -14
  138. package/src/ae/universal.js +4 -2
  139. package/src/ae/wallet.js +2 -2
  140. package/src/chain/index.js +29 -6
  141. package/src/chain/node.js +95 -65
  142. package/src/channel/handlers.js +30 -20
  143. package/src/channel/index.js +46 -16
  144. package/src/channel/internal.js +35 -30
  145. package/src/contract/aci.js +376 -0
  146. package/src/contract/compiler.js +22 -113
  147. package/src/contract/ga/helpers.js +10 -6
  148. package/src/contract/ga/index.js +31 -24
  149. package/src/index.js +9 -8
  150. package/src/node-pool/index.js +13 -10
  151. package/src/node.js +11 -10
  152. package/src/tx/builder/constants.js +100 -0
  153. package/src/tx/builder/field-types.js +61 -0
  154. package/src/tx/builder/helpers.js +99 -103
  155. package/src/tx/builder/index.js +81 -40
  156. package/src/tx/builder/schema.js +44 -132
  157. package/src/tx/index.js +101 -12
  158. package/src/tx/tx-object.js +35 -15
  159. package/src/tx/tx.js +110 -35
  160. package/src/tx/validator.js +25 -5
  161. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
  162. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
  163. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  164. package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
  165. package/src/utils/aepp-wallet-communication/helpers.js +6 -14
  166. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +106 -57
  167. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
  168. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +95 -53
  169. package/src/utils/aepp-wallet-communication/schema.ts +80 -0
  170. package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
  171. package/src/utils/amount-formatter.ts +4 -3
  172. package/src/utils/async-init.js +10 -6
  173. package/src/utils/bignumber.ts +1 -9
  174. package/src/utils/bytes.ts +3 -2
  175. package/src/utils/crypto-ts.ts +2 -2
  176. package/src/utils/crypto.js +11 -155
  177. package/src/utils/encoder.ts +101 -0
  178. package/src/utils/errors.ts +683 -0
  179. package/src/utils/hd-wallet.js +11 -26
  180. package/src/utils/keystore.js +57 -53
  181. package/src/utils/mptree.ts +12 -6
  182. package/src/utils/swagger.js +68 -45
  183. package/es/account/base.js.map +0 -1
  184. package/es/account/memory.js.map +0 -1
  185. package/es/account/multiple.js.map +0 -1
  186. package/es/ae/aens.js.map +0 -1
  187. package/es/ae/aepp.js.map +0 -1
  188. package/es/ae/contract.js +0 -308
  189. package/es/ae/contract.js.map +0 -1
  190. package/es/ae/index.js.map +0 -1
  191. package/es/ae/oracle.js.map +0 -1
  192. package/es/ae/universal.js.map +0 -1
  193. package/es/ae/wallet.js.map +0 -1
  194. package/es/chain/index.js.map +0 -1
  195. package/es/chain/node.js.map +0 -1
  196. package/es/channel/handlers.js.map +0 -1
  197. package/es/channel/index.js.map +0 -1
  198. package/es/channel/internal.js.map +0 -1
  199. package/es/contract/aci/helpers.js +0 -298
  200. package/es/contract/aci/helpers.js.map +0 -1
  201. package/es/contract/aci/index.js +0 -356
  202. package/es/contract/aci/index.js.map +0 -1
  203. package/es/contract/aci/transformation.js +0 -66
  204. package/es/contract/aci/transformation.js.map +0 -1
  205. package/es/contract/compiler.js +0 -242
  206. package/es/contract/compiler.js.map +0 -1
  207. package/es/contract/ga/helpers.js +0 -22
  208. package/es/contract/ga/helpers.js.map +0 -1
  209. package/es/contract/ga/index.js.map +0 -1
  210. package/es/contract/index.js +0 -158
  211. package/es/contract/index.js.map +0 -1
  212. package/es/index.js +0 -56
  213. package/es/index.js.map +0 -1
  214. package/es/node-pool/index.js.map +0 -1
  215. package/es/node.js.map +0 -1
  216. package/es/tx/builder/helpers.js +0 -318
  217. package/es/tx/builder/helpers.js.map +0 -1
  218. package/es/tx/builder/index.js.map +0 -1
  219. package/es/tx/builder/schema.js.map +0 -1
  220. package/es/tx/index.js.map +0 -1
  221. package/es/tx/tx-object.js.map +0 -1
  222. package/es/tx/tx.js.map +0 -1
  223. package/es/tx/validator.js.map +0 -1
  224. package/es/typings/blakejs/index.d.js +0 -2
  225. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  226. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  227. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  228. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  229. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  230. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  231. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  232. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  233. package/es/utils/aepp-wallet-communication/schema.js +0 -107
  234. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  235. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  236. package/es/utils/amount-formatter.js.map +0 -1
  237. package/es/utils/async-init.js.map +0 -1
  238. package/es/utils/bignumber.js.map +0 -1
  239. package/es/utils/bytes.js.map +0 -1
  240. package/es/utils/crypto-ts.js.map +0 -1
  241. package/es/utils/crypto.js.map +0 -1
  242. package/es/utils/hd-wallet.js.map +0 -1
  243. package/es/utils/keystore.js.map +0 -1
  244. package/es/utils/mptree.js.map +0 -1
  245. package/es/utils/other.js.map +0 -1
  246. package/es/utils/swagger.js.map +0 -1
  247. package/src/contract/aci/index.js +0 -310
  248. package/src/contract/aci/transformation.js +0 -72
  249. package/src/contract/index.js +0 -159
  250. package/src/utils/aepp-wallet-communication/schema.js +0 -108
@@ -3,24 +3,36 @@
3
3
  *
4
4
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc
5
5
  * @export AeppRpc
6
- * @example import ContentScriptBridge from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc'
6
+ * @example
7
+ * import ContentScriptBridge
8
+ * from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc'
7
9
  */
8
- import { v4 as uuid } from 'uuid';
9
- import Ae from '../../../ae';
10
- import RpcClient from './rpc-client';
11
- import { getHandler, message, voidFn } from '../helpers';
12
- import { METHODS, RPC_STATUS, VERSION } from '../schema';
10
+ import { v4 as uuid } from '@aeternity/uuid';
11
+ import Ae from "../../../ae/index.mjs";
12
+ import RpcClient from "./rpc-client.mjs";
13
+ import { getHandler, message } from "../helpers.mjs";
14
+ import { METHODS, RPC_STATUS, VERSION } from "../schema.mjs";
15
+ import { AlreadyConnectedError, NoWalletConnectedError, UnsubscribedAccountError, UnAuthorizedAccountError, ArgumentError, RpcConnectionError } from "../../errors.mjs";
16
+ import Node from "../../../node.mjs";
13
17
  const NOTIFICATIONS = {
14
- [METHODS.wallet.updateAddress]: instance => _ref => {
18
+ [METHODS.updateAddress]: instance => _ref => {
15
19
  let {
16
20
  params
17
21
  } = _ref;
18
22
  instance.rpcClient.accounts = params;
19
23
  instance.onAddressChange(params);
20
24
  },
21
- [METHODS.updateNetwork]: instance => msg => {
22
- instance.rpcClient.info.networkId = msg.params.networkId;
23
- instance.onNetworkChange(msg.params);
25
+ [METHODS.updateNetwork]: instance => async _ref2 => {
26
+ let {
27
+ params
28
+ } = _ref2;
29
+ const {
30
+ networkId,
31
+ node
32
+ } = params;
33
+ instance.rpcClient.info.networkId = networkId;
34
+ node && instance.addNode(node.name, await Node(node), true);
35
+ instance.onNetworkChange(params);
24
36
  },
25
37
  [METHODS.closeConnection]: instance => msg => {
26
38
  instance.disconnectWallet();
@@ -28,40 +40,45 @@ const NOTIFICATIONS = {
28
40
  }
29
41
  };
30
42
  const RESPONSES = {
31
- [METHODS.aepp.address]: instance => msg => instance.rpcClient.processResponse(msg),
32
- [METHODS.aepp.connect]: instance => msg => {
43
+ [METHODS.address]: instance => msg => instance.rpcClient.processResponse(msg),
44
+ [METHODS.connect]: instance => msg => {
33
45
  if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED;
34
46
  instance.rpcClient.processResponse(msg);
35
47
  },
36
- [METHODS.aepp.subscribeAddress]: instance => msg => {
48
+ [METHODS.subscribeAddress]: instance => msg => {
37
49
  if (msg.result) {
38
- msg.result.address && (instance.rpcClient.accounts = msg.result.address);
39
- msg.result.subscription && (instance.rpcClient.addressSubscription = msg.result.subscription);
50
+ if (msg.result.address) {
51
+ instance.rpcClient.accounts = msg.result.address;
52
+ }
53
+
54
+ if (msg.result.subscription) {
55
+ instance.rpcClient.addressSubscription = msg.result.subscription;
56
+ }
40
57
  }
41
58
 
42
- instance.rpcClient.processResponse(msg, _ref2 => {
59
+ instance.rpcClient.processResponse(msg, _ref3 => {
43
60
  let {
44
61
  id,
45
62
  result
46
- } = _ref2;
63
+ } = _ref3;
47
64
  return [result];
48
65
  });
49
66
  },
50
- [METHODS.aepp.sign]: instance => msg => {
51
- instance.rpcClient.processResponse(msg, _ref3 => {
67
+ [METHODS.sign]: instance => msg => {
68
+ instance.rpcClient.processResponse(msg, _ref4 => {
52
69
  let {
53
70
  id,
54
71
  result
55
- } = _ref3;
72
+ } = _ref4;
56
73
  return [result.signedTransaction || result.transactionHash];
57
74
  });
58
75
  },
59
- [METHODS.aepp.signMessage]: instance => msg => {
60
- instance.rpcClient.processResponse(msg, _ref4 => {
76
+ [METHODS.signMessage]: instance => msg => {
77
+ instance.rpcClient.processResponse(msg, _ref5 => {
61
78
  let {
62
79
  id,
63
80
  result
64
- } = _ref4;
81
+ } = _ref5;
65
82
  return [result.signature];
66
83
  });
67
84
  }
@@ -99,17 +116,20 @@ const handleMessage = instance => async msg => {
99
116
 
100
117
 
101
118
  export default Ae.compose({
102
- async init(_ref5) {
119
+ async init(_ref6) {
103
120
  let {
104
121
  name,
105
- onAddressChange = voidFn,
106
- onDisconnect = voidFn,
107
- onNetworkChange = voidFn,
108
122
  connection,
109
- forceValidation = false,
110
- debug = false
111
- } = _ref5;
112
- const eventsHandlers = ['onDisconnect', 'onAddressChange', 'onNetworkChange'];
123
+ debug = false,
124
+ ...other
125
+ } = _ref6;
126
+ ['onAddressChange', 'onDisconnect', 'onNetworkChange'].forEach(event => {
127
+ var _other$event;
128
+
129
+ const handler = (_other$event = other[event]) !== null && _other$event !== void 0 ? _other$event : () => {};
130
+ if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler);
131
+ this[event] = handler;
132
+ });
113
133
  this.connection = connection;
114
134
  this.name = name;
115
135
  this.debug = debug;
@@ -117,16 +137,7 @@ export default Ae.compose({
117
137
  if (connection) {
118
138
  // Init RPCClient
119
139
  await this.connectToWallet(connection);
120
- } // Event callbacks
121
-
122
-
123
- this.onDisconnect = onDisconnect;
124
- this.onAddressChange = onAddressChange;
125
- this.onNetworkChange = onNetworkChange; // validation
126
-
127
- eventsHandlers.forEach(event => {
128
- if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`);
129
- });
140
+ }
130
141
  },
131
142
 
132
143
  deepProps: {
@@ -140,7 +151,7 @@ export default Ae.compose({
140
151
  sign() {},
141
152
 
142
153
  addresses() {
143
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.');
154
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
144
155
  return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)];
145
156
  },
146
157
 
@@ -150,10 +161,19 @@ export default Ae.compose({
150
161
  * @instance
151
162
  * @rtype (connection: Object) => void
152
163
  * @param {Object} connection Wallet connection object
153
- * @return {void}
164
+ * @param {Object} [options={}]
165
+ * @param {Boolean} [options.connectNode=true] - Request wallet to bind node
166
+ * @param {String} [options.name=wallet-node] - Node name
167
+ * @param {Boolean} [options.select=false] - Select this node as current
168
+ * @return {Object}
154
169
  */
155
170
  async connectToWallet(connection) {
156
- if (this.rpcClient && this.rpcClient.isConnected()) throw new Error('You are already connected to wallet ' + this.rpcClient);
171
+ let {
172
+ connectNode = false,
173
+ name = 'wallet-node',
174
+ select = false
175
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
176
+ if (this.rpcClient && this.rpcClient.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient);
157
177
  this.rpcClient = RpcClient({
158
178
  connection,
159
179
  networkId: this.getNetworkId({
@@ -163,7 +183,14 @@ export default Ae.compose({
163
183
  id: uuid(),
164
184
  handlers: [handleMessage(this), this.onDisconnect]
165
185
  });
166
- return this.sendConnectRequest();
186
+ const walletInfo = await this.sendConnectRequest(connectNode);
187
+
188
+ if (connectNode && !Object.prototype.hasOwnProperty.call(walletInfo, 'node')) {
189
+ throw new RpcConnectionError('Missing URLs of the Node');
190
+ }
191
+
192
+ connectNode && this.addNode(name, await Node(walletInfo.node), select);
193
+ return walletInfo;
167
194
  },
168
195
 
169
196
  /**
@@ -176,7 +203,7 @@ export default Ae.compose({
176
203
  */
177
204
  async disconnectWallet() {
178
205
  let sendDisconnect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
179
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
206
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
180
207
  sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, {
181
208
  reason: 'bye'
182
209
  }), true);
@@ -188,10 +215,14 @@ export default Ae.compose({
188
215
  let {
189
216
  onAccount
190
217
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
191
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
218
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
192
219
  const current = this.rpcClient.currentAccount;
193
- if (!current) throw new Error('You are not subscribed for an account.');
194
- if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) throw new Error(`You do not have access to account ${onAccount}`);
220
+ if (!current) throw new UnsubscribedAccountError();
221
+
222
+ if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) {
223
+ throw new UnAuthorizedAccountError(onAccount);
224
+ }
225
+
195
226
  return onAccount || current;
196
227
  },
197
228
 
@@ -203,9 +234,9 @@ export default Ae.compose({
203
234
  * @return {Promise} Address from wallet
204
235
  */
205
236
  async askAddresses() {
206
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
207
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.');
208
- return this.rpcClient.request(METHODS.aepp.address);
237
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
238
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
239
+ return this.rpcClient.request(METHODS.address);
209
240
  },
210
241
 
211
242
  /**
@@ -213,13 +244,13 @@ export default Ae.compose({
213
244
  * @function subscribeAddress
214
245
  * @instance
215
246
  * @rtype (type: String, value: String) => Promise
216
- * @param {String} type Type of subscription can be one of ['current'(just for selected account updates), 'connected(all accounts)']
247
+ * @param {String} type Should be one of 'current' (the selected account), 'connected' (all)
217
248
  * @param {String} value Subscription action('subscribe'|'unsubscribe')
218
249
  * @return {Promise} Address from wallet
219
250
  */
220
251
  async subscribeAddress(type, value) {
221
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
222
- return this.rpcClient.request(METHODS.aepp.subscribeAddress, {
252
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
253
+ return this.rpcClient.request(METHODS.subscribeAddress, {
223
254
  type,
224
255
  value
225
256
  });
@@ -235,10 +266,14 @@ export default Ae.compose({
235
266
  */
236
267
  async signTransaction(tx) {
237
268
  let opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
238
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
239
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.');
240
- if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new Error(`You do not have access to account ${opt.onAccount}`);
241
- return this.rpcClient.request(METHODS.aepp.sign, { ...opt,
269
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
270
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
271
+
272
+ if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
273
+ throw new UnAuthorizedAccountError(opt.onAccount);
274
+ }
275
+
276
+ return this.rpcClient.request(METHODS.sign, { ...opt,
242
277
  tx,
243
278
  returnSigned: true,
244
279
  networkId: this.getNetworkId()
@@ -255,10 +290,14 @@ export default Ae.compose({
255
290
  */
256
291
  async signMessage(msg) {
257
292
  let opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
258
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
259
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.');
260
- if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new Error(`You do not have access to account ${opt.onAccount}`);
261
- return this.rpcClient.request(METHODS.aepp.signMessage, { ...opt,
293
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
294
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
295
+
296
+ if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
297
+ throw new UnAuthorizedAccountError(opt.onAccount);
298
+ }
299
+
300
+ return this.rpcClient.request(METHODS.signMessage, { ...opt,
262
301
  message: msg
263
302
  });
264
303
  },
@@ -267,23 +306,26 @@ export default Ae.compose({
267
306
  * Send connection request to wallet
268
307
  * @function sendConnectRequest
269
308
  * @instance
309
+ * @param {Boolean} connectNode - Request wallet to bind node
270
310
  * @rtype () => Promise
271
311
  * @return {Promise} Connection response
272
312
  */
273
- async sendConnectRequest() {
274
- return this.rpcClient.request(METHODS.aepp.connect, {
313
+ async sendConnectRequest(connectNode) {
314
+ return this.rpcClient.request(METHODS.connect, {
275
315
  name: this.name,
276
316
  version: VERSION,
277
317
  networkId: this.getNetworkId({
278
318
  force: true
279
- })
319
+ }),
320
+ connectNode
280
321
  });
281
322
  },
282
323
 
283
324
  /**
284
325
  * Overwriting of `send` AE method
285
326
  * All sdk API which use it will be send notification to wallet and wait for callBack
286
- * This method will sign, broadcast and wait until transaction will be accepted using rpc communication with wallet
327
+ * This method will sign, broadcast and wait until transaction will be accepted using rpc
328
+ * communication with wallet
287
329
  * @function send
288
330
  * @instance
289
331
  * @rtype (tx: String, options = {}) => Promise
@@ -294,9 +336,13 @@ export default Ae.compose({
294
336
  */
295
337
  async send(tx) {
296
338
  let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
297
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet');
298
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.');
299
- if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) throw new Error(`You do not have access to account ${options.onAccount}`);
339
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
340
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
341
+
342
+ if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) {
343
+ throw new UnAuthorizedAccountError(options.onAccount);
344
+ }
345
+
300
346
  const opt = { ...this.Ae.defaults,
301
347
  ...options
302
348
  };
@@ -308,14 +354,22 @@ export default Ae.compose({
308
354
  return this.sendTransaction(signed, opt);
309
355
  }
310
356
 
311
- return this.rpcClient.request(METHODS.aepp.sign, {
357
+ return this.rpcClient.request(METHODS.sign, {
312
358
  onAccount: opt.onAccount,
313
359
  tx,
314
360
  returnSigned: false,
315
361
  networkId: this.getNetworkId()
316
362
  });
363
+ },
364
+
365
+ isConnected() {
366
+ return this.rpcClient && this.rpcClient.isConnected();
367
+ },
368
+
369
+ isSubscribedAccount() {
370
+ return typeof this.rpcClient.currentAccount !== 'undefined';
317
371
  }
318
372
 
319
373
  }
320
374
  });
321
- //# sourceMappingURL=aepp-rpc.js.map
375
+ //# sourceMappingURL=aepp-rpc.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/utils/aepp-wallet-communication/rpc/aepp-rpc.js"],"names":["v4","uuid","Ae","RpcClient","getHandler","message","METHODS","RPC_STATUS","VERSION","AlreadyConnectedError","NoWalletConnectedError","UnsubscribedAccountError","UnAuthorizedAccountError","ArgumentError","RpcConnectionError","Node","NOTIFICATIONS","updateAddress","instance","params","rpcClient","accounts","onAddressChange","updateNetwork","networkId","node","info","addNode","name","onNetworkChange","closeConnection","msg","disconnectWallet","onDisconnect","RESPONSES","address","processResponse","connect","result","status","CONNECTED","subscribeAddress","subscription","addressSubscription","id","sign","signedTransaction","transactionHash","signMessage","signature","REQUESTS","handleMessage","debug","Object","prototype","hasOwnProperty","call","callbacks","compose","init","connection","other","forEach","event","handler","connectToWallet","deepProps","defaults","walletBroadcast","methods","addresses","isSubscribedAccount","currentAccount","keys","connected","connectNode","select","isConnected","getNetworkId","force","connectionInfo","handlers","walletInfo","sendConnectRequest","sendDisconnect","sendMessage","reason","disconnect","onAccount","current","hasAccessToAccount","askAddresses","request","type","value","signTransaction","tx","opt","returnSigned","version","send","options","signed","sendTransaction"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,EAAE,IAAIC,IAAf,QAA2B,iBAA3B;OACOC,E;OACAC,S;SACEC,U,EAAYC,O;SACZC,O,EAASC,U,EAAYC,O;SAE5BC,qB,EACAC,sB,EACAC,wB,EACAC,wB,EACAC,a,EACAC,kB;OAEKC,I;AAEP,MAAMC,aAAa,GAAG;AACpB,GAACV,OAAO,CAACW,aAAT,GAA0BC,QAAD,IACvB,QAAgB;AAAA,QAAf;AAAEC,MAAAA;AAAF,KAAe;AACdD,IAAAA,QAAQ,CAACE,SAAT,CAAmBC,QAAnB,GAA8BF,MAA9B;AACAD,IAAAA,QAAQ,CAACI,eAAT,CAAyBH,MAAzB;AACD,GALiB;AAMpB,GAACb,OAAO,CAACiB,aAAT,GAA0BL,QAAD,IACvB,eAAsB;AAAA,QAAf;AAAEC,MAAAA;AAAF,KAAe;AACpB,UAAM;AAAEK,MAAAA,SAAF;AAAaC,MA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n * RPC handler for AEPP side\n *\n * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc\n * @export AeppRpc\n * @example\n * import ContentScriptBridge\n * from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc'\n */\nimport { v4 as uuid } from '@aeternity/uuid'\nimport Ae from '../../../ae'\nimport RpcClient from './rpc-client'\nimport { getHandler, message } from '../helpers'\nimport { METHODS, RPC_STATUS, VERSION } from '../schema'\nimport {\n AlreadyConnectedError,\n NoWalletConnectedError,\n UnsubscribedAccountError,\n UnAuthorizedAccountError,\n ArgumentError,\n RpcConnectionError\n} from '../../errors'\nimport Node from '../../../node'\n\nconst NOTIFICATIONS = {\n [METHODS.updateAddress]: (instance) =>\n ({ params }) => {\n instance.rpcClient.accounts = params\n instance.onAddressChange(params)\n },\n [METHODS.updateNetwork]: (instance) =>\n async ({ params }) => {\n const { networkId, node } = params\n instance.rpcClient.info.networkId = networkId\n node && instance.addNode(node.name, await Node(node), true)\n instance.onNetworkChange(params)\n },\n [METHODS.closeConnection]: (instance) =>\n (msg) => {\n instance.disconnectWallet()\n instance.onDisconnect(msg.params)\n }\n}\n\nconst RESPONSES = {\n [METHODS.address]: (instance) =>\n (msg) => instance.rpcClient.processResponse(msg),\n [METHODS.connect]: (instance) =>\n (msg) => {\n if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED\n instance.rpcClient.processResponse(msg)\n },\n [METHODS.subscribeAddress]: (instance) =>\n (msg) => {\n if (msg.result) {\n if (msg.result.address) {\n instance.rpcClient.accounts = msg.result.address\n }\n if (msg.result.subscription) {\n instance.rpcClient.addressSubscription = msg.result.subscription\n }\n }\n\n instance.rpcClient.processResponse(msg, ({ id, result }) => [result])\n },\n [METHODS.sign]: (instance) =>\n (msg) => {\n instance.rpcClient.processResponse(\n msg, ({ id, result }) => [result.signedTransaction || result.transactionHash]\n )\n },\n [METHODS.signMessage]: (instance) =>\n (msg) => {\n instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signature])\n }\n}\n\nconst REQUESTS = {}\n\nconst handleMessage = (instance) => async (msg) => {\n if (!msg.id) {\n return getHandler(NOTIFICATIONS, msg, { debug: instance.debug })(instance)(msg)\n } else if (Object.prototype.hasOwnProperty.call(instance.rpcClient.callbacks, msg.id)) {\n return getHandler(RESPONSES, msg, { debug: instance.debug })(instance)(msg)\n } else {\n return getHandler(REQUESTS, msg, { debug: instance.debug })(instance)(msg)\n }\n}\n\n/**\n * Contain functionality for wallet interaction and connect it to sdk\n * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc\n * @function\n * @rtype Stamp\n * @param {Object} param Init params object\n * @param {String=} [param.name] Aepp name\n * @param {Function} onAddressChange Call-back function for update address event\n * @param {Function} onDisconnect Call-back function for disconnect event\n * @param {Function} onNetworkChange Call-back function for update network event\n * @param {Object} connection Wallet connection object\n * @return {Object}\n */\nexport default Ae.compose({\n async init ({\n name,\n connection,\n debug = false,\n ...other\n }) {\n ['onAddressChange', 'onDisconnect', 'onNetworkChange'].forEach(event => {\n const handler = other[event] ?? (() => {})\n if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler)\n this[event] = handler\n })\n\n this.connection = connection\n this.name = name\n this.debug = debug\n\n if (connection) {\n // Init RPCClient\n await this.connectToWallet(connection)\n }\n },\n deepProps: { Ae: { defaults: { walletBroadcast: true } } },\n methods: {\n sign () {\n },\n addresses () {\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)]\n },\n /**\n * Connect to wallet\n * @function connectToWallet\n * @instance\n * @rtype (connection: Object) => void\n * @param {Object} connection Wallet connection object\n * @param {Object} [options={}]\n * @param {Boolean} [options.connectNode=true] - Request wallet to bind node\n * @param {String} [options.name=wallet-node] - Node name\n * @param {Boolean} [options.select=false] - Select this node as current\n * @return {Object}\n */\n async connectToWallet (connection, { connectNode = false, name = 'wallet-node', select = false } = {}) {\n if (this.rpcClient && this.rpcClient.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient)\n this.rpcClient = RpcClient({\n connection,\n networkId: this.getNetworkId({ force: true }),\n ...connection.connectionInfo,\n id: uuid(),\n handlers: [handleMessage(this), this.onDisconnect]\n })\n const walletInfo = await this.sendConnectRequest(connectNode)\n if (connectNode && !Object.prototype.hasOwnProperty.call(walletInfo, 'node')) {\n throw new RpcConnectionError('Missing URLs of the Node')\n }\n connectNode && this.addNode(name, await Node(walletInfo.node), select)\n return walletInfo\n },\n /**\n * Disconnect from wallet\n * @function disconnectWallet\n * @instance\n * @rtype (force: Boolean = false) => void\n * @param {Boolean} sendDisconnect=false Force sending close connection message\n * @return {void}\n */\n async disconnectWallet (sendDisconnect = true) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, { reason: 'bye' }), true)\n this.rpcClient.disconnect()\n this.rpcClient = null\n },\n async address ({ onAccount } = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n const current = this.rpcClient.currentAccount\n if (!current) throw new UnsubscribedAccountError()\n if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) {\n throw new UnAuthorizedAccountError(onAccount)\n }\n return onAccount || current\n },\n /**\n * Ask address from wallet\n * @function askAddresses\n * @instance\n * @rtype () => Promise\n * @return {Promise} Address from wallet\n */\n async askAddresses () {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n return this.rpcClient.request(METHODS.address)\n },\n /**\n * Subscribe for addresses from wallet\n * @function subscribeAddress\n * @instance\n * @rtype (type: String, value: String) => Promise\n * @param {String} type Should be one of 'current' (the selected account), 'connected' (all)\n * @param {String} value Subscription action('subscribe'|'unsubscribe')\n * @return {Promise} Address from wallet\n */\n async subscribeAddress (type, value) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n return this.rpcClient.request(METHODS.subscribeAddress, { type, value })\n },\n /**\n * Overwriting of `signTransaction` AE method\n * All sdk API which use it will be send notification to wallet and wait for callBack\n * @function signTransaction\n * @instance\n * @rtype (tx: String, options = {}) => Promise\n * @return {Promise<String>} Signed transaction\n */\n async signTransaction (tx, opt = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {\n throw new UnAuthorizedAccountError(opt.onAccount)\n }\n return this.rpcClient.request(\n METHODS.sign,\n { ...opt, tx, returnSigned: true, networkId: this.getNetworkId() }\n )\n },\n /**\n * Overwriting of `signMessage` AE method\n * All sdk API which use it will be send notification to wallet and wait for callBack\n * @function signMessage\n * @instance\n * @rtype (msg: String, options = {}) => Promise\n * @return {Promise<String>} Signed transaction\n */\n async signMessage (msg, opt = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {\n throw new UnAuthorizedAccountError(opt.onAccount)\n }\n return this.rpcClient.request(METHODS.signMessage, { ...opt, message: msg })\n },\n /**\n * Send connection request to wallet\n * @function sendConnectRequest\n * @instance\n * @param {Boolean} connectNode - Request wallet to bind node\n * @rtype () => Promise\n * @return {Promise} Connection response\n */\n async sendConnectRequest (connectNode) {\n return this.rpcClient.request(\n METHODS.connect, {\n name: this.name,\n version: VERSION,\n networkId: this.getNetworkId({ force: true }),\n connectNode\n }\n )\n },\n /**\n * Overwriting of `send` AE method\n * All sdk API which use it will be send notification to wallet and wait for callBack\n * This method will sign, broadcast and wait until transaction will be accepted using rpc\n * communication with wallet\n * @function send\n * @instance\n * @rtype (tx: String, options = {}) => Promise\n * @param {String} tx\n * @param {Object} [options={}]\n * @param {Object} [options.walletBroadcast=true]\n * @return {Promise<Object>} Transaction broadcast result\n */\n async send (tx, options = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) {\n throw new UnAuthorizedAccountError(options.onAccount)\n }\n const opt = { ...this.Ae.defaults, ...options }\n if (!opt.walletBroadcast) {\n const signed = await this.signTransaction(tx, { onAccount: opt.onAccount })\n return this.sendTransaction(signed, opt)\n }\n return this.rpcClient.request(\n METHODS.sign,\n { onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() }\n )\n },\n isConnected () { return (this.rpcClient && this.rpcClient.isConnected()) },\n isSubscribedAccount () { return typeof this.rpcClient.currentAccount !== 'undefined' }\n }\n})\n"],"file":"aepp-rpc.mjs"}
@@ -3,11 +3,13 @@
3
3
  *
4
4
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client
5
5
  * @export { RpcClient, RpcClients }
6
- * @example import RpcClient from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client'
6
+ * @example
7
+ * import RpcClient from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client'
7
8
  */
8
9
  import stampit from '@stamp/it';
9
- import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema';
10
- import { sendMessage, message, isValidAccounts } from '../helpers';
10
+ import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from "../schema.mjs";
11
+ import { sendMessage, message, isValidAccounts } from "../helpers.mjs";
12
+ import { InvalidRpcMessageError, TypeError, DuplicateCallbackError, MissingCallbackError } from "../../errors.mjs";
11
13
  /**
12
14
  * Contain functionality for using RPC conection
13
15
  * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client
@@ -16,7 +18,9 @@ import { sendMessage, message, isValidAccounts } from '../helpers';
16
18
  * @param {Object} param Init params object
17
19
  * @param {String} param.name Client name
18
20
  * @param {Object} param.connection Connection object
19
- * @param {Function[]} param.handlers Arrays with two function for handling messages ([ onMessage: Function, onDisconnect: Function])
21
+ * @param {Function[]} param.handlers Array with two function for message handling
22
+ * @param {Function} param.handlers[0] Message handler
23
+ * @param {Function} param.handlers[1] Disconnect callback
20
24
  * @return {Object}
21
25
  */
22
26
 
@@ -52,7 +56,7 @@ export default stampit({
52
56
 
53
57
  const handleMessage = (msg, origin) => {
54
58
  if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {
55
- throw new Error(`Received invalid message: ${msg}`);
59
+ throw new InvalidRpcMessageError(msg);
56
60
  }
57
61
 
58
62
  onMessage(msg, origin);
@@ -136,7 +140,7 @@ export default stampit({
136
140
  * @return {Boolean} is connected
137
141
  */
138
142
  isConnected() {
139
- return this.connection.isConnected() && this.info.status === RPC_STATUS.CONNECTED;
143
+ return this.connection.isConnected() && (this.info.status === RPC_STATUS.CONNECTED || this.info.status === RPC_STATUS.NODE_BINDED);
140
144
  },
141
145
 
142
146
  /**
@@ -171,23 +175,24 @@ export default stampit({
171
175
 
172
176
  /**
173
177
  * Update accounts and sent `update.address` notification to AEPP
174
- * @param {{ current: Object, connected: Object }} accounts Object with current and connected accounts
175
- * @param {{ forceNotification: Boolean }} [options={ forceNotification: false }] Don not sent update notification to AEPP
178
+ * @param {{ current: Object, connected: Object }} accounts Current and connected accounts
179
+ * @param {Object} [options]
180
+ * @param {Boolean} [options.forceNotification] Don't sent update notification to AEPP
176
181
  */
177
182
  setAccounts(accounts) {
178
183
  let {
179
- forceNotification = false
184
+ forceNotification
180
185
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
181
186
 
182
187
  if (!isValidAccounts(accounts)) {
183
- throw new Error('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`');
188
+ throw new TypeError('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`');
184
189
  }
185
190
 
186
191
  this.accounts = accounts;
187
192
 
188
193
  if (!forceNotification) {
189
194
  // Sent notification about account updates
190
- this.sendMessage(message(METHODS.wallet.updateAddress, this.accounts), true);
195
+ this.sendMessage(message(METHODS.updateAddress, this.accounts), true);
191
196
  }
192
197
  },
193
198
 
@@ -223,7 +228,11 @@ export default stampit({
223
228
  */
224
229
  request(name, params) {
225
230
  const msgId = this.sendMessage(message(name, params));
226
- if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) throw new Error('Callback Already exist');
231
+
232
+ if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) {
233
+ throw new DuplicateCallbackError();
234
+ }
235
+
227
236
  return new Promise((resolve, reject) => {
228
237
  this.callbacks[msgId] = {
229
238
  resolve,
@@ -247,7 +256,7 @@ export default stampit({
247
256
  error,
248
257
  result
249
258
  } = _ref2;
250
- if (!this.callbacks[id]) throw new Error(`Can't find callback for this messageId ${id}`);
259
+ if (!this.callbacks[id]) throw new MissingCallbackError(id);
251
260
 
252
261
  if (result) {
253
262
  this.callbacks[id].resolve(...(typeof transformResult === 'function' ? transformResult({
@@ -263,4 +272,4 @@ export default stampit({
263
272
 
264
273
  }
265
274
  });
266
- //# sourceMappingURL=rpc-client.js.map
275
+ //# sourceMappingURL=rpc-client.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/utils/aepp-wallet-communication/rpc/rpc-client.js"],"names":["stampit","METHODS","RPC_STATUS","SUBSCRIPTION_TYPES","sendMessage","message","isValidAccounts","InvalidRpcMessageError","TypeError","DuplicateCallbackError","MissingCallbackError","init","id","name","networkId","icons","connection","handlers","onMessage","onDisconnect","info","callbacks","addressSubscription","accounts","handleMessage","msg","origin","jsonrpc","method","disconnect","aepp","connect","propertyDescriptors","currentAccount","enumerable","configurable","get","isHasAccounts","Object","keys","current","undefined","addresses","connected","methods","updateInfo","assign","isSubscribed","length","hasAccessToAccount","address","find","a","isConnected","status","CONNECTED","NODE_BINDED","getCurrentAccount","onAccount","forceConnectionClose","DISCONNECTED","setAccounts","forceNotification","updateAddress","updateSubscription","type","value","subscribe","includes","push","unsubscribe","filter","s","request","params","msgId","prototype","hasOwnProperty","call","Promise","resolve","reject","processResponse","transformResult","error","result"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,OAAP,MAAoB,WAApB;SAESC,O,EAASC,U,EAAYC,kB;SACrBC,W,EAAaC,O,EAASC,e;SAE7BC,sB,EACAC,S,EACAC,sB,EACAC,oB;AAGF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAeV,OAAO,CAAC;AACrBW,EAAAA,IAAI,OAAmF;AAAA,QAAjF;AAAEC,MAAAA,EAAF;AAAMC,MAAAA,IAAN;AAAYC,MAAAA,SAAZ;AAAuBC,MAAAA,KAAvB;AAA8BC,MAAAA,UAA9B;AAA0CC,MAAAA,QAAQ,EAAE,CAACC,SAAD,EAAYC,YAAZ;AAApD,KAAiF;AACrF,SAAKP,EAAL,GAAUA,EAAV;AACA,SAAKI,UAAL,GAAkBA,UAAlB;AACA,SAAKI,IAAL,GAAY;AAAEP,MAAAA,IAAF;AAAQC,MAAAA,SAAR;AAAmBC,MAAAA;AAAnB,KAAZ,CAHqF,CAIrF;AACA;AACA;;AACA,SAAKM,SAAL,GAAiB,EAAjB,CAPqF,CAQrF;;AACA,SAAKC,mBAAL,GAA2B,EAA3B,CATqF,CAUrF;AACA;AACA;AACA;;AACA,SAAKC,QAAL,GAAgB,EAAhB;AAEA,SAAKnB,WAAL,GAAmBA,WAAW,CAAC,KAAKY,UAAN,CAA9B;;AAEA,UAAMQ,aAAa,GAAG,CAACC,GAAD,EAAMC,MAAN,KAAiB;AACrC,UAAI,CAACD,GAAD,IAAQ,CAACA,GAAG,CAACE,OAAb,IAAwBF,GAAG,CAACE,OAAJ,KAAgB,KAAxC,IAAiD,CAACF,GAAG,CAACG,MAA1D,EAAkE;AAChE,cAAM,IAAIrB,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/aepp-wallet-communication/rpc/rpc-client\n * @export { RpcClient, RpcClients }\n * @example\n * import RpcClient from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client'\n */\nimport stampit from '@stamp/it'\n\nimport { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema'\nimport { sendMessage, message, isValidAccounts } from '../helpers'\nimport {\n InvalidRpcMessageError,\n TypeError,\n DuplicateCallbackError,\n MissingCallbackError\n} from '../../errors'\n\n/**\n * Contain functionality for using RPC conection\n * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client\n * @function\n * @rtype Stamp\n * @param {Object} param Init params object\n * @param {String} param.name Client name\n * @param {Object} param.connection Connection object\n * @param {Function[]} param.handlers Array with two function for message handling\n * @param {Function} param.handlers[0] Message handler\n * @param {Function} param.handlers[1] Disconnect callback\n * @return {Object}\n */\nexport default stampit({\n init ({ id, name, networkId, icons, connection, handlers: [onMessage, onDisconnect] }) {\n this.id = id\n this.connection = connection\n this.info = { name, networkId, icons }\n // {\n // [msg.id]: { resolve, reject }\n // }\n this.callbacks = {}\n // ['connected', 'current']\n this.addressSubscription = []\n // {\n // connected: { [pub]: {...meta} },\n // current: { [pub]: {...meta} }\n // }\n this.accounts = {}\n\n this.sendMessage = sendMessage(this.connection)\n\n const handleMessage = (msg, origin) => {\n if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {\n throw new InvalidRpcMessageError(msg)\n }\n onMessage(msg, origin)\n }\n\n const disconnect = (aepp, connection) => {\n this.disconnect(true)\n typeof onDisconnect === 'function' && onDisconnect(connection, this)\n }\n\n connection.connect(handleMessage, disconnect)\n },\n propertyDescriptors: {\n currentAccount: {\n enumerable: true,\n configurable: false,\n get () {\n return this.isHasAccounts()\n ? Object.keys(this.accounts.current)[0]\n : undefined\n }\n },\n addresses: {\n enumerable: true,\n configurable: false,\n get () {\n return this.isHasAccounts()\n ? [...Object.keys(this.accounts.current), ...Object.keys(this.accounts.connected)]\n : []\n }\n },\n origin: {\n enumerable: true,\n configurable: false,\n get () {\n return this.connection\n }\n }\n },\n methods: {\n /**\n * Update info\n * @function updateInfo\n * @instance\n * @rtype (info: Object) => void\n * @param {Object} info Info to update (will be merged with current info object)\n * @return {void}\n */\n updateInfo (info) {\n Object.assign(this.info, info)\n },\n isHasAccounts () {\n return typeof this.accounts === 'object' &&\n typeof this.accounts.connected === 'object' &&\n typeof this.accounts.current === 'object'\n },\n isSubscribed () {\n return this.addressSubscription.length && this.isHasAccounts()\n },\n /**\n * Check if aepp has access to account\n * @function hasAccessToAccount\n * @instance\n * @rtype (address: String) => Boolean\n * @param {String} address Account address\n * @return {Boolean} is connected\n */\n hasAccessToAccount (address) {\n return !!address && this.addresses.find(a => a === address)\n },\n /**\n * Check if is connected\n * @function isConnected\n * @instance\n * @rtype () => Boolean\n * @return {Boolean} is connected\n */\n isConnected () {\n return this.connection.isConnected() &&\n (this.info.status === RPC_STATUS.CONNECTED || this.info.status === RPC_STATUS.NODE_BINDED)\n },\n /**\n * Get selected account\n * @function getCurrentAccount\n * @instance\n * @rtype ({ onAccount } = {}) => String\n * @param {Object} options Options\n * @return {String}\n */\n getCurrentAccount ({ onAccount } = {}) {\n return onAccount || Object.keys(this.accounts.current)[0]\n },\n /**\n * Disconnect\n * @function disconnect\n * @instance\n * @rtype () => void\n * @return {void}\n */\n disconnect (forceConnectionClose = false) {\n this.info.status = RPC_STATUS.DISCONNECTED\n this.addressSubscription = []\n this.accounts = {}\n forceConnectionClose || this.connection.disconnect()\n },\n /**\n * Update accounts and sent `update.address` notification to AEPP\n * @param {{ current: Object, connected: Object }} accounts Current and connected accounts\n * @param {Object} [options]\n * @param {Boolean} [options.forceNotification] Don't sent update notification to AEPP\n */\n setAccounts (accounts, { forceNotification } = {}) {\n if (!isValidAccounts(accounts)) {\n throw new TypeError('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`')\n }\n this.accounts = accounts\n if (!forceNotification) {\n // Sent notification about account updates\n this.sendMessage(message(METHODS.updateAddress, this.accounts), true)\n }\n },\n /**\n * Update subscription\n * @function updateSubscription\n * @instance\n * @rtype (type: String, value: String) => void\n * @param {String} type Subscription type\n * @param {String} value Subscription value\n * @return {String[]}\n */\n updateSubscription (type, value) {\n if (type === SUBSCRIPTION_TYPES.subscribe && !this.addressSubscription.includes(value)) {\n this.addressSubscription.push(value)\n }\n if (type === SUBSCRIPTION_TYPES.unsubscribe && this.addressSubscription.includes(value)) {\n this.addressSubscription = this.addressSubscription.filter(s => s !== value)\n }\n return this.addressSubscription\n },\n /**\n * Make a request\n * @function request\n * @instance\n * @rtype (name: String, params: Object) => Promise\n * @param {String} name Method name\n * @param {Object} params Method params\n * @return {Promise} Promise which will be resolved after receiving response message\n */\n request (name, params) {\n const msgId = this.sendMessage(message(name, params))\n if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) {\n throw new DuplicateCallbackError()\n }\n return new Promise((resolve, reject) => {\n this.callbacks[msgId] = { resolve, reject }\n })\n },\n /**\n * Process response message\n * @function processResponse\n * @instance\n * @rtype (msg: Object, transformResult: Function) => void\n * @param {Object} msg Message object\n * @param {Function=} transformResult Optional parser function for message\n * @return {void}\n */\n processResponse ({ id, error, result }, transformResult) {\n if (!this.callbacks[id]) throw new MissingCallbackError(id)\n if (result) {\n this.callbacks[id].resolve(...typeof transformResult === 'function'\n ? transformResult({ id, result })\n : [result])\n } else {\n this.callbacks[id].reject(error)\n }\n delete this.callbacks[id]\n }\n }\n})\n"],"file":"rpc-client.mjs"}