@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,37 @@
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'
10
+ import { v4 as uuid } from '@aeternity/uuid'
9
11
  import Ae from '../../../ae'
10
12
  import RpcClient from './rpc-client'
11
- import { getHandler, message, voidFn } from '../helpers'
13
+ import { getHandler, message } from '../helpers'
12
14
  import { METHODS, RPC_STATUS, VERSION } from '../schema'
15
+ import {
16
+ AlreadyConnectedError,
17
+ NoWalletConnectedError,
18
+ UnsubscribedAccountError,
19
+ UnAuthorizedAccountError,
20
+ ArgumentError,
21
+ RpcConnectionError
22
+ } from '../../errors'
23
+ import Node from '../../../node'
13
24
 
14
25
  const NOTIFICATIONS = {
15
- [METHODS.wallet.updateAddress]: (instance) =>
26
+ [METHODS.updateAddress]: (instance) =>
16
27
  ({ params }) => {
17
28
  instance.rpcClient.accounts = params
18
29
  instance.onAddressChange(params)
19
30
  },
20
31
  [METHODS.updateNetwork]: (instance) =>
21
- (msg) => {
22
- instance.rpcClient.info.networkId = msg.params.networkId
23
- instance.onNetworkChange(msg.params)
32
+ async ({ params }) => {
33
+ const { networkId, node } = params
34
+ instance.rpcClient.info.networkId = networkId
35
+ node && instance.addNode(node.name, await Node(node), true)
36
+ instance.onNetworkChange(params)
24
37
  },
25
38
  [METHODS.closeConnection]: (instance) =>
26
39
  (msg) => {
@@ -30,27 +43,33 @@ const NOTIFICATIONS = {
30
43
  }
31
44
 
32
45
  const RESPONSES = {
33
- [METHODS.aepp.address]: (instance) =>
46
+ [METHODS.address]: (instance) =>
34
47
  (msg) => instance.rpcClient.processResponse(msg),
35
- [METHODS.aepp.connect]: (instance) =>
48
+ [METHODS.connect]: (instance) =>
36
49
  (msg) => {
37
50
  if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED
38
51
  instance.rpcClient.processResponse(msg)
39
52
  },
40
- [METHODS.aepp.subscribeAddress]: (instance) =>
53
+ [METHODS.subscribeAddress]: (instance) =>
41
54
  (msg) => {
42
55
  if (msg.result) {
43
- msg.result.address && (instance.rpcClient.accounts = msg.result.address)
44
- msg.result.subscription && (instance.rpcClient.addressSubscription = msg.result.subscription)
56
+ if (msg.result.address) {
57
+ instance.rpcClient.accounts = msg.result.address
58
+ }
59
+ if (msg.result.subscription) {
60
+ instance.rpcClient.addressSubscription = msg.result.subscription
61
+ }
45
62
  }
46
63
 
47
64
  instance.rpcClient.processResponse(msg, ({ id, result }) => [result])
48
65
  },
49
- [METHODS.aepp.sign]: (instance) =>
66
+ [METHODS.sign]: (instance) =>
50
67
  (msg) => {
51
- instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signedTransaction || result.transactionHash])
68
+ instance.rpcClient.processResponse(
69
+ msg, ({ id, result }) => [result.signedTransaction || result.transactionHash]
70
+ )
52
71
  },
53
- [METHODS.aepp.signMessage]: (instance) =>
72
+ [METHODS.signMessage]: (instance) =>
54
73
  (msg) => {
55
74
  instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signature])
56
75
  }
@@ -82,8 +101,18 @@ const handleMessage = (instance) => async (msg) => {
82
101
  * @return {Object}
83
102
  */
84
103
  export default Ae.compose({
85
- async init ({ name, onAddressChange = voidFn, onDisconnect = voidFn, onNetworkChange = voidFn, connection, forceValidation = false, debug = false }) {
86
- const eventsHandlers = ['onDisconnect', 'onAddressChange', 'onNetworkChange']
104
+ async init ({
105
+ name,
106
+ connection,
107
+ debug = false,
108
+ ...other
109
+ }) {
110
+ ['onAddressChange', 'onDisconnect', 'onNetworkChange'].forEach(event => {
111
+ const handler = other[event] ?? (() => {})
112
+ if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler)
113
+ this[event] = handler
114
+ })
115
+
87
116
  this.connection = connection
88
117
  this.name = name
89
118
  this.debug = debug
@@ -92,21 +121,13 @@ export default Ae.compose({
92
121
  // Init RPCClient
93
122
  await this.connectToWallet(connection)
94
123
  }
95
- // Event callbacks
96
- this.onDisconnect = onDisconnect
97
- this.onAddressChange = onAddressChange
98
- this.onNetworkChange = onNetworkChange
99
- // validation
100
- eventsHandlers.forEach(event => {
101
- if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`)
102
- })
103
124
  },
104
125
  deepProps: { Ae: { defaults: { walletBroadcast: true } } },
105
126
  methods: {
106
127
  sign () {
107
128
  },
108
129
  addresses () {
109
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
130
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
110
131
  return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)]
111
132
  },
112
133
  /**
@@ -115,10 +136,14 @@ export default Ae.compose({
115
136
  * @instance
116
137
  * @rtype (connection: Object) => void
117
138
  * @param {Object} connection Wallet connection object
118
- * @return {void}
139
+ * @param {Object} [options={}]
140
+ * @param {Boolean} [options.connectNode=true] - Request wallet to bind node
141
+ * @param {String} [options.name=wallet-node] - Node name
142
+ * @param {Boolean} [options.select=false] - Select this node as current
143
+ * @return {Object}
119
144
  */
120
- async connectToWallet (connection) {
121
- if (this.rpcClient && this.rpcClient.isConnected()) throw new Error('You are already connected to wallet ' + this.rpcClient)
145
+ async connectToWallet (connection, { connectNode = false, name = 'wallet-node', select = false } = {}) {
146
+ if (this.rpcClient && this.rpcClient.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient)
122
147
  this.rpcClient = RpcClient({
123
148
  connection,
124
149
  networkId: this.getNetworkId({ force: true }),
@@ -126,7 +151,12 @@ export default Ae.compose({
126
151
  id: uuid(),
127
152
  handlers: [handleMessage(this), this.onDisconnect]
128
153
  })
129
- return this.sendConnectRequest()
154
+ const walletInfo = await this.sendConnectRequest(connectNode)
155
+ if (connectNode && !Object.prototype.hasOwnProperty.call(walletInfo, 'node')) {
156
+ throw new RpcConnectionError('Missing URLs of the Node')
157
+ }
158
+ connectNode && this.addNode(name, await Node(walletInfo.node), select)
159
+ return walletInfo
130
160
  },
131
161
  /**
132
162
  * Disconnect from wallet
@@ -137,16 +167,18 @@ export default Ae.compose({
137
167
  * @return {void}
138
168
  */
139
169
  async disconnectWallet (sendDisconnect = true) {
140
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
170
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
141
171
  sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, { reason: 'bye' }), true)
142
172
  this.rpcClient.disconnect()
143
173
  this.rpcClient = null
144
174
  },
145
175
  async address ({ onAccount } = {}) {
146
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
176
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
147
177
  const current = this.rpcClient.currentAccount
148
- if (!current) throw new Error('You are not subscribed for an account.')
149
- if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) throw new Error(`You do not have access to account ${onAccount}`)
178
+ if (!current) throw new UnsubscribedAccountError()
179
+ if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) {
180
+ throw new UnAuthorizedAccountError(onAccount)
181
+ }
150
182
  return onAccount || current
151
183
  },
152
184
  /**
@@ -157,22 +189,22 @@ export default Ae.compose({
157
189
  * @return {Promise} Address from wallet
158
190
  */
159
191
  async askAddresses () {
160
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
161
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
162
- return this.rpcClient.request(METHODS.aepp.address)
192
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
193
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
194
+ return this.rpcClient.request(METHODS.address)
163
195
  },
164
196
  /**
165
197
  * Subscribe for addresses from wallet
166
198
  * @function subscribeAddress
167
199
  * @instance
168
200
  * @rtype (type: String, value: String) => Promise
169
- * @param {String} type Type of subscription can be one of ['current'(just for selected account updates), 'connected(all accounts)']
201
+ * @param {String} type Should be one of 'current' (the selected account), 'connected' (all)
170
202
  * @param {String} value Subscription action('subscribe'|'unsubscribe')
171
203
  * @return {Promise} Address from wallet
172
204
  */
173
205
  async subscribeAddress (type, value) {
174
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
175
- return this.rpcClient.request(METHODS.aepp.subscribeAddress, { type, value })
206
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
207
+ return this.rpcClient.request(METHODS.subscribeAddress, { type, value })
176
208
  },
177
209
  /**
178
210
  * Overwriting of `signTransaction` AE method
@@ -183,10 +215,15 @@ export default Ae.compose({
183
215
  * @return {Promise<String>} Signed transaction
184
216
  */
185
217
  async signTransaction (tx, opt = {}) {
186
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
187
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
188
- if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new Error(`You do not have access to account ${opt.onAccount}`)
189
- return this.rpcClient.request(METHODS.aepp.sign, { ...opt, tx, returnSigned: true, networkId: this.getNetworkId() })
218
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
219
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
220
+ if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
221
+ throw new UnAuthorizedAccountError(opt.onAccount)
222
+ }
223
+ return this.rpcClient.request(
224
+ METHODS.sign,
225
+ { ...opt, tx, returnSigned: true, networkId: this.getNetworkId() }
226
+ )
190
227
  },
191
228
  /**
192
229
  * Overwriting of `signMessage` AE method
@@ -197,31 +234,36 @@ export default Ae.compose({
197
234
  * @return {Promise<String>} Signed transaction
198
235
  */
199
236
  async signMessage (msg, opt = {}) {
200
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
201
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
202
- if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new Error(`You do not have access to account ${opt.onAccount}`)
203
- return this.rpcClient.request(METHODS.aepp.signMessage, { ...opt, message: msg })
237
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
238
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
239
+ if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
240
+ throw new UnAuthorizedAccountError(opt.onAccount)
241
+ }
242
+ return this.rpcClient.request(METHODS.signMessage, { ...opt, message: msg })
204
243
  },
205
244
  /**
206
245
  * Send connection request to wallet
207
246
  * @function sendConnectRequest
208
247
  * @instance
248
+ * @param {Boolean} connectNode - Request wallet to bind node
209
249
  * @rtype () => Promise
210
250
  * @return {Promise} Connection response
211
251
  */
212
- async sendConnectRequest () {
252
+ async sendConnectRequest (connectNode) {
213
253
  return this.rpcClient.request(
214
- METHODS.aepp.connect, {
254
+ METHODS.connect, {
215
255
  name: this.name,
216
256
  version: VERSION,
217
- networkId: this.getNetworkId({ force: true })
257
+ networkId: this.getNetworkId({ force: true }),
258
+ connectNode
218
259
  }
219
260
  )
220
261
  },
221
262
  /**
222
263
  * Overwriting of `send` AE method
223
264
  * All sdk API which use it will be send notification to wallet and wait for callBack
224
- * This method will sign, broadcast and wait until transaction will be accepted using rpc communication with wallet
265
+ * This method will sign, broadcast and wait until transaction will be accepted using rpc
266
+ * communication with wallet
225
267
  * @function send
226
268
  * @instance
227
269
  * @rtype (tx: String, options = {}) => Promise
@@ -231,15 +273,22 @@ export default Ae.compose({
231
273
  * @return {Promise<Object>} Transaction broadcast result
232
274
  */
233
275
  async send (tx, options = {}) {
234
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
235
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
236
- if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) throw new Error(`You do not have access to account ${options.onAccount}`)
276
+ if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
277
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
278
+ if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) {
279
+ throw new UnAuthorizedAccountError(options.onAccount)
280
+ }
237
281
  const opt = { ...this.Ae.defaults, ...options }
238
282
  if (!opt.walletBroadcast) {
239
283
  const signed = await this.signTransaction(tx, { onAccount: opt.onAccount })
240
284
  return this.sendTransaction(signed, opt)
241
285
  }
242
- return this.rpcClient.request(METHODS.aepp.sign, { onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() })
243
- }
286
+ return this.rpcClient.request(
287
+ METHODS.sign,
288
+ { onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() }
289
+ )
290
+ },
291
+ isConnected () { return (this.rpcClient && this.rpcClient.isConnected()) },
292
+ isSubscribedAccount () { return typeof this.rpcClient.currentAccount !== 'undefined' }
244
293
  }
245
294
  })
@@ -3,12 +3,19 @@
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
10
 
10
11
  import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema'
11
12
  import { sendMessage, message, isValidAccounts } from '../helpers'
13
+ import {
14
+ InvalidRpcMessageError,
15
+ TypeError,
16
+ DuplicateCallbackError,
17
+ MissingCallbackError
18
+ } from '../../errors'
12
19
 
13
20
  /**
14
21
  * Contain functionality for using RPC conection
@@ -18,7 +25,9 @@ import { sendMessage, message, isValidAccounts } from '../helpers'
18
25
  * @param {Object} param Init params object
19
26
  * @param {String} param.name Client name
20
27
  * @param {Object} param.connection Connection object
21
- * @param {Function[]} param.handlers Arrays with two function for handling messages ([ onMessage: Function, onDisconnect: Function])
28
+ * @param {Function[]} param.handlers Array with two function for message handling
29
+ * @param {Function} param.handlers[0] Message handler
30
+ * @param {Function} param.handlers[1] Disconnect callback
22
31
  * @return {Object}
23
32
  */
24
33
  export default stampit({
@@ -42,7 +51,7 @@ export default stampit({
42
51
 
43
52
  const handleMessage = (msg, origin) => {
44
53
  if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {
45
- throw new Error(`Received invalid message: ${msg}`)
54
+ throw new InvalidRpcMessageError(msg)
46
55
  }
47
56
  onMessage(msg, origin)
48
57
  }
@@ -120,7 +129,8 @@ export default stampit({
120
129
  * @return {Boolean} is connected
121
130
  */
122
131
  isConnected () {
123
- return this.connection.isConnected() && this.info.status === RPC_STATUS.CONNECTED
132
+ return this.connection.isConnected() &&
133
+ (this.info.status === RPC_STATUS.CONNECTED || this.info.status === RPC_STATUS.NODE_BINDED)
124
134
  },
125
135
  /**
126
136
  * Get selected account
@@ -148,17 +158,18 @@ export default stampit({
148
158
  },
149
159
  /**
150
160
  * Update accounts and sent `update.address` notification to AEPP
151
- * @param {{ current: Object, connected: Object }} accounts Object with current and connected accounts
152
- * @param {{ forceNotification: Boolean }} [options={ forceNotification: false }] Don not sent update notification to AEPP
161
+ * @param {{ current: Object, connected: Object }} accounts Current and connected accounts
162
+ * @param {Object} [options]
163
+ * @param {Boolean} [options.forceNotification] Don't sent update notification to AEPP
153
164
  */
154
- setAccounts (accounts, { forceNotification = false } = {}) {
165
+ setAccounts (accounts, { forceNotification } = {}) {
155
166
  if (!isValidAccounts(accounts)) {
156
- throw new Error('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`')
167
+ throw new TypeError('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`')
157
168
  }
158
169
  this.accounts = accounts
159
170
  if (!forceNotification) {
160
171
  // Sent notification about account updates
161
- this.sendMessage(message(METHODS.wallet.updateAddress, this.accounts), true)
172
+ this.sendMessage(message(METHODS.updateAddress, this.accounts), true)
162
173
  }
163
174
  },
164
175
  /**
@@ -190,7 +201,9 @@ export default stampit({
190
201
  */
191
202
  request (name, params) {
192
203
  const msgId = this.sendMessage(message(name, params))
193
- if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) throw new Error('Callback Already exist')
204
+ if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) {
205
+ throw new DuplicateCallbackError()
206
+ }
194
207
  return new Promise((resolve, reject) => {
195
208
  this.callbacks[msgId] = { resolve, reject }
196
209
  })
@@ -205,7 +218,7 @@ export default stampit({
205
218
  * @return {void}
206
219
  */
207
220
  processResponse ({ id, error, result }, transformResult) {
208
- if (!this.callbacks[id]) throw new Error(`Can't find callback for this messageId ${id}`)
221
+ if (!this.callbacks[id]) throw new MissingCallbackError(id)
209
222
  if (result) {
210
223
  this.callbacks[id].resolve(...typeof transformResult === 'function'
211
224
  ? transformResult({ id, result })