@aeternity/aepp-sdk 9.0.1 → 11.0.0

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 (284) 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 -36
  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.mjs +179 -0
  10. package/es/account/base.mjs.map +1 -0
  11. package/es/account/memory.mjs +99 -0
  12. package/es/account/memory.mjs.map +1 -0
  13. package/es/account/multiple.mjs +179 -0
  14. package/es/account/multiple.mjs.map +1 -0
  15. package/es/ae/aens.mjs +404 -0
  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.mjs +207 -0
  22. package/es/ae/index.mjs.map +1 -0
  23. package/es/ae/oracle.mjs +339 -0
  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} +29 -9
  32. package/es/chain/index.mjs.map +1 -0
  33. package/es/chain/node.mjs +442 -0
  34. package/es/chain/node.mjs.map +1 -0
  35. package/es/channel/handlers.mjs +1110 -0
  36. package/es/channel/handlers.mjs.map +1 -0
  37. package/es/channel/{index.js → index.mjs} +245 -472
  38. package/es/channel/index.mjs.map +1 -0
  39. package/es/channel/internal.mjs +283 -0
  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.mjs +178 -0
  48. package/es/contract/ga/index.mjs.map +1 -0
  49. package/es/errors.d.ts +284 -0
  50. package/es/index.mjs +57 -0
  51. package/es/index.mjs.map +1 -0
  52. package/es/node-pool/index.mjs +188 -0
  53. package/es/node-pool/index.mjs.map +1 -0
  54. package/es/node.mjs +133 -0
  55. package/es/node.mjs.map +1 -0
  56. package/es/other.d.ts +1 -1
  57. package/es/tx/builder/field-types.mjs +74 -0
  58. package/es/tx/builder/field-types.mjs.map +1 -0
  59. package/es/tx/builder/helpers.mjs +363 -0
  60. package/es/tx/builder/helpers.mjs.map +1 -0
  61. package/es/tx/builder/index.mjs +606 -0
  62. package/es/tx/builder/index.mjs.map +1 -0
  63. package/es/tx/builder/schema.mjs +760 -0
  64. package/es/tx/builder/schema.mjs.map +1 -0
  65. package/es/tx/{index.js → index.mjs} +107 -15
  66. package/es/tx/index.mjs.map +1 -0
  67. package/es/tx/tx-object.mjs +283 -0
  68. package/es/tx/tx-object.mjs.map +1 -0
  69. package/es/tx/tx.mjs +938 -0
  70. package/es/tx/tx.mjs.map +1 -0
  71. package/es/tx/validator.mjs +247 -0
  72. package/es/tx/validator.mjs.map +1 -0
  73. package/es/typings/blakejs/index.d.mjs +2 -0
  74. package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
  75. package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +27 -29
  76. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
  77. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +183 -0
  78. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
  79. package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
  80. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
  81. package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +29 -23
  82. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
  83. package/es/utils/aepp-wallet-communication/helpers.mjs +92 -0
  84. package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
  85. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +375 -0
  86. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
  87. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +275 -0
  88. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
  89. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +541 -0
  90. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
  91. package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
  92. package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
  93. package/es/utils/aepp-wallet-communication/wallet-detector.mjs +110 -0
  94. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  95. package/es/utils/amount-formatter.mjs +139 -0
  96. package/es/utils/amount-formatter.mjs.map +1 -0
  97. package/es/utils/async-init.mjs +64 -0
  98. package/es/utils/async-init.mjs.map +1 -0
  99. package/es/utils/bignumber.mjs +21 -0
  100. package/es/utils/bignumber.mjs.map +1 -0
  101. package/es/utils/{bytes.js → bytes.mjs} +16 -12
  102. package/es/utils/bytes.mjs.map +1 -0
  103. package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
  104. package/es/utils/crypto-ts.mjs.map +1 -0
  105. package/es/utils/crypto.mjs +239 -0
  106. package/es/utils/crypto.mjs.map +1 -0
  107. package/es/utils/errors.mjs +689 -0
  108. package/es/utils/errors.mjs.map +1 -0
  109. package/es/utils/hd-wallet.mjs +111 -0
  110. package/es/utils/hd-wallet.mjs.map +1 -0
  111. package/es/utils/json-big.mjs +45 -0
  112. package/es/utils/json-big.mjs.map +1 -0
  113. package/es/utils/keystore.mjs +240 -0
  114. package/es/utils/keystore.mjs.map +1 -0
  115. package/es/utils/mptree.mjs +161 -0
  116. package/es/utils/mptree.mjs.map +1 -0
  117. package/es/utils/other.mjs +43 -0
  118. package/es/utils/other.mjs.map +1 -0
  119. package/es/utils/semver-satisfies.mjs +16 -0
  120. package/es/utils/semver-satisfies.mjs.map +1 -0
  121. package/es/utils/{string.js → string.mjs} +5 -14
  122. package/es/utils/string.mjs.map +1 -0
  123. package/es/utils/swagger.mjs +164 -0
  124. package/es/utils/swagger.mjs.map +1 -0
  125. package/package.json +55 -62
  126. package/src/account/base.js +3 -1
  127. package/src/account/memory.js +13 -8
  128. package/src/account/multiple.js +31 -22
  129. package/src/ae/aens.js +75 -77
  130. package/src/ae/aepp.js +2 -2
  131. package/src/ae/contract.js +27 -291
  132. package/src/ae/index.js +9 -7
  133. package/src/ae/oracle.js +51 -28
  134. package/src/ae/universal.js +4 -2
  135. package/src/ae/wallet.js +2 -2
  136. package/src/chain/index.js +29 -6
  137. package/src/chain/node.js +95 -67
  138. package/src/channel/handlers.js +30 -21
  139. package/src/channel/index.js +44 -20
  140. package/src/channel/internal.js +30 -24
  141. package/src/contract/aci.js +376 -0
  142. package/src/contract/compiler.js +41 -91
  143. package/src/contract/ga/helpers.js +10 -13
  144. package/src/contract/ga/index.js +42 -33
  145. package/src/index.js +9 -10
  146. package/src/node-pool/index.js +29 -17
  147. package/src/node.js +33 -36
  148. package/src/tx/builder/field-types.js +61 -0
  149. package/src/tx/builder/helpers.js +126 -84
  150. package/src/tx/builder/index.js +83 -41
  151. package/src/tx/builder/schema.js +50 -40
  152. package/src/tx/index.js +101 -12
  153. package/src/tx/tx-object.js +35 -15
  154. package/src/tx/tx.js +232 -153
  155. package/src/tx/validator.js +34 -12
  156. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
  157. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
  158. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  159. package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
  160. package/src/utils/aepp-wallet-communication/helpers.js +6 -14
  161. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +107 -60
  162. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
  163. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +98 -56
  164. package/src/utils/aepp-wallet-communication/schema.ts +80 -0
  165. package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
  166. package/src/utils/amount-formatter.ts +4 -3
  167. package/src/utils/async-init.js +10 -6
  168. package/src/utils/bignumber.ts +3 -10
  169. package/src/utils/bytes.ts +3 -2
  170. package/src/utils/crypto-ts.ts +2 -2
  171. package/src/utils/crypto.js +10 -144
  172. package/src/utils/errors.ts +683 -0
  173. package/src/utils/hd-wallet.js +11 -26
  174. package/src/utils/keystore.js +57 -53
  175. package/src/utils/mptree.ts +12 -6
  176. package/src/utils/other.ts +1 -1
  177. package/src/utils/semver-satisfies.ts +1 -1
  178. package/src/utils/swagger.js +75 -45
  179. package/es/account/base.js +0 -259
  180. package/es/account/base.js.map +0 -1
  181. package/es/account/memory.js +0 -86
  182. package/es/account/memory.js.map +0 -1
  183. package/es/account/multiple.js +0 -267
  184. package/es/account/multiple.js.map +0 -1
  185. package/es/ae/aens.js +0 -829
  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 -985
  189. package/es/ae/contract.js.map +0 -1
  190. package/es/ae/index.js +0 -442
  191. package/es/ae/index.js.map +0 -1
  192. package/es/ae/oracle.js +0 -690
  193. package/es/ae/oracle.js.map +0 -1
  194. package/es/ae/universal.js.map +0 -1
  195. package/es/ae/wallet.js.map +0 -1
  196. package/es/chain/index.js.map +0 -1
  197. package/es/chain/node.js +0 -1119
  198. package/es/chain/node.js.map +0 -1
  199. package/es/channel/handlers.js +0 -1601
  200. package/es/channel/handlers.js.map +0 -1
  201. package/es/channel/index.js.map +0 -1
  202. package/es/channel/internal.js +0 -438
  203. package/es/channel/internal.js.map +0 -1
  204. package/es/contract/aci/helpers.js +0 -298
  205. package/es/contract/aci/helpers.js.map +0 -1
  206. package/es/contract/aci/index.js +0 -517
  207. package/es/contract/aci/index.js.map +0 -1
  208. package/es/contract/aci/transformation.js +0 -742
  209. package/es/contract/aci/transformation.js.map +0 -1
  210. package/es/contract/compiler.js +0 -316
  211. package/es/contract/compiler.js.map +0 -1
  212. package/es/contract/ga/helpers.js +0 -120
  213. package/es/contract/ga/helpers.js.map +0 -1
  214. package/es/contract/ga/index.js +0 -307
  215. package/es/contract/ga/index.js.map +0 -1
  216. package/es/contract/index.js +0 -158
  217. package/es/contract/index.js.map +0 -1
  218. package/es/index.js +0 -57
  219. package/es/index.js.map +0 -1
  220. package/es/node-pool/index.js +0 -204
  221. package/es/node-pool/index.js.map +0 -1
  222. package/es/node.js +0 -228
  223. package/es/node.js.map +0 -1
  224. package/es/tx/builder/helpers.js +0 -355
  225. package/es/tx/builder/helpers.js.map +0 -1
  226. package/es/tx/builder/index.js +0 -686
  227. package/es/tx/builder/index.js.map +0 -1
  228. package/es/tx/builder/schema.js +0 -695
  229. package/es/tx/builder/schema.js.map +0 -1
  230. package/es/tx/index.js.map +0 -1
  231. package/es/tx/tx-object.js +0 -297
  232. package/es/tx/tx-object.js.map +0 -1
  233. package/es/tx/tx.js +0 -1757
  234. package/es/tx/tx.js.map +0 -1
  235. package/es/tx/validator.js +0 -344
  236. package/es/tx/validator.js.map +0 -1
  237. package/es/typings/blakejs/index.d.js +0 -2
  238. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  239. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -211
  240. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  241. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  242. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  243. package/es/utils/aepp-wallet-communication/helpers.js +0 -148
  244. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  245. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -652
  246. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  247. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -285
  248. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  249. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -763
  250. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  251. package/es/utils/aepp-wallet-communication/schema.js +0 -103
  252. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  253. package/es/utils/aepp-wallet-communication/wallet-detector.js +0 -135
  254. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  255. package/es/utils/amount-formatter.js +0 -152
  256. package/es/utils/amount-formatter.js.map +0 -1
  257. package/es/utils/async-init.js +0 -103
  258. package/es/utils/async-init.js.map +0 -1
  259. package/es/utils/bignumber.js +0 -36
  260. package/es/utils/bignumber.js.map +0 -1
  261. package/es/utils/bytes.js.map +0 -1
  262. package/es/utils/crypto-ts.js.map +0 -1
  263. package/es/utils/crypto.js +0 -358
  264. package/es/utils/crypto.js.map +0 -1
  265. package/es/utils/hd-wallet.js +0 -148
  266. package/es/utils/hd-wallet.js.map +0 -1
  267. package/es/utils/json-big.js +0 -78
  268. package/es/utils/json-big.js.map +0 -1
  269. package/es/utils/keystore.js +0 -393
  270. package/es/utils/keystore.js.map +0 -1
  271. package/es/utils/mptree.js +0 -198
  272. package/es/utils/mptree.js.map +0 -1
  273. package/es/utils/other.js +0 -93
  274. package/es/utils/other.js.map +0 -1
  275. package/es/utils/semver-satisfies.js +0 -35
  276. package/es/utils/semver-satisfies.js.map +0 -1
  277. package/es/utils/string.js.map +0 -1
  278. package/es/utils/swagger.js +0 -269
  279. package/es/utils/swagger.js.map +0 -1
  280. package/src/contract/aci/helpers.js +0 -140
  281. package/src/contract/aci/index.js +0 -191
  282. package/src/contract/aci/transformation.js +0 -464
  283. package/src/contract/index.js +0 -159
  284. package/src/utils/aepp-wallet-communication/schema.js +0 -110
@@ -4,6 +4,7 @@ import { encode, decode } from './builder/helpers'
4
4
  import BigNumber from 'bignumber.js'
5
5
  import { MIN_GAS_PRICE, PROTOCOL_VM_ABI, TX_TYPE } from './builder/schema'
6
6
  import { calculateFee, unpackTx } from './builder'
7
+ import { UnsupportedProtocolError } from '../utils/errors'
7
8
 
8
9
  /**
9
10
  * Transaction validator
@@ -13,7 +14,7 @@ import { calculateFee, unpackTx } from './builder'
13
14
  */
14
15
 
15
16
  const validators = [
16
- async ({ encodedTx, signatures }, { account, node, parentTxTypes }) => {
17
+ ({ encodedTx, signatures }, { account, node, parentTxTypes }) => {
17
18
  if ((encodedTx ?? signatures) === undefined) return []
18
19
  if (signatures.length !== 1) return [] // TODO: Support multisignature?
19
20
  const prefix = Buffer.from([
@@ -32,7 +33,7 @@ const validators = [
32
33
  checkedKeys: ['encodedTx', 'signatures']
33
34
  }]
34
35
  },
35
- async ({ encodedTx, tx }, { node, parentTxTypes, txType }) => {
36
+ ({ encodedTx, tx }, { node, parentTxTypes, txType }) => {
36
37
  if ((encodedTx ?? tx) === undefined) return []
37
38
  return verifyTransaction(
38
39
  encode((encodedTx ?? tx).rlpEncoded, 'tx'),
@@ -80,13 +81,15 @@ const validators = [
80
81
  const validNonce = account.nonce + 1
81
82
  if (nonce === validNonce) return []
82
83
  return [{
83
- ...nonce < validNonce ? {
84
- message: `Nonce ${nonce} is already used, valid nonce is ${validNonce}`,
85
- key: 'NonceAlreadyUsed'
86
- } : {
87
- message: `Nonce ${nonce} is too high, valid nonce is ${validNonce}`,
88
- key: 'NonceHigh'
89
- },
84
+ ...nonce < validNonce
85
+ ? {
86
+ message: `Nonce ${nonce} is already used, valid nonce is ${validNonce}`,
87
+ key: 'NonceAlreadyUsed'
88
+ }
89
+ : {
90
+ message: `Nonce ${nonce} is too high, valid nonce is ${validNonce}`,
91
+ key: 'NonceHigh'
92
+ },
90
93
  checkedKeys: ['nonce']
91
94
  }]
92
95
  },
@@ -102,7 +105,7 @@ const validators = [
102
105
  ({ ctVersion, abiVersion }, { txType, node }) => {
103
106
  const { consensusProtocolVersion } = node.getNodeInfo()
104
107
  const protocol = PROTOCOL_VM_ABI[consensusProtocolVersion]
105
- if (!protocol) throw new Error(`Unsupported protocol: ${consensusProtocolVersion}`)
108
+ if (!protocol) throw new UnsupportedProtocolError(`Unsupported protocol: ${consensusProtocolVersion}`)
106
109
  // If not contract create tx
107
110
  if (!ctVersion) ctVersion = { abiVersion }
108
111
  const txProtocol = protocol[txType]
@@ -115,6 +118,25 @@ const validators = [
115
118
  }]
116
119
  }
117
120
  return []
121
+ },
122
+ async ({ contractId }, { txType, node }) => {
123
+ if (TX_TYPE.contractCall !== txType) return []
124
+ try {
125
+ const { active } = await node.api.getContract(contractId)
126
+ if (active) return []
127
+ return [{
128
+ message: `Contract ${contractId} is not active`,
129
+ key: 'ContractNotActive',
130
+ checkedKeys: ['contractId']
131
+ }]
132
+ } catch (error) {
133
+ if (!error.response?.body?.reason) throw error
134
+ return [{
135
+ message: error.response.body.reason,
136
+ key: 'ContractNotFound',
137
+ checkedKeys: ['contractId']
138
+ }]
139
+ }
118
140
  }
119
141
  ]
120
142
 
@@ -144,13 +166,13 @@ export default async function verifyTransaction (transaction, node, parentTxType
144
166
 
145
167
  const address = getSenderAddress(tx) ??
146
168
  (txType === TX_TYPE.signed ? getSenderAddress(tx.encodedTx.tx) : null)
147
- const [account, height] = await Promise.all([
169
+ const [account, { height }] = await Promise.all([
148
170
  address && node.api.getAccountByPubkey(address).catch(() => ({
149
171
  id: address,
150
172
  balance: new BigNumber(0),
151
173
  nonce: 0
152
174
  })),
153
- node.api.getCurrentKeyBlockHeight().then(({ height }) => height)
175
+ node.api.getCurrentKeyBlockHeight()
154
176
  ])
155
177
 
156
178
  return (await Promise.all(
@@ -18,15 +18,21 @@
18
18
  /**
19
19
  * Browser runtime connector module
20
20
  *
21
- * This is the complement to {@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection}.
21
+ * This is the complement to
22
+ * {@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection}.
22
23
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-runtime
23
24
  * @export BrowserRuntimeConnection
24
- * @example import BrowserRuntimeConnection from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-runtime'
25
+ * @example import { BrowserRuntimeConnection } from '@aeternity/aepp-sdk'
25
26
  */
26
27
  import stampit from '@stamp/it'
27
28
 
28
29
  import WalletConnection from '.'
29
30
  import { getBrowserAPI } from '../helpers'
31
+ import {
32
+ RpcConnectionError,
33
+ AlreadyConnectedError,
34
+ NoWalletConnectedError
35
+ } from '../../errors'
30
36
 
31
37
  /**
32
38
  * Disconnect
@@ -53,7 +59,7 @@ function disconnect () {
53
59
  * @return {void}
54
60
  */
55
61
  function connect (onMessage, onDisconnect) {
56
- if (this.isConnected()) throw new Error('You already connected')
62
+ if (this.isConnected()) throw new AlreadyConnectedError('You already connected')
57
63
  this.handler = (msg, source) => {
58
64
  if (this.debug) console.log('Receive message: ', msg)
59
65
  onMessage(msg, source)
@@ -74,7 +80,7 @@ function connect (onMessage, onDisconnect) {
74
80
  * @return {void}
75
81
  */
76
82
  function sendMessage (msg) {
77
- if (!this.port) throw new Error('You dont have connection. Please connect before')
83
+ if (!this.port) throw new NoWalletConnectedError('You dont have connection. Please connect before')
78
84
  if (this.debug) console.log('Send message: ', msg)
79
85
  this.port.postMessage(msg)
80
86
  }
@@ -104,7 +110,7 @@ function isConnected () {
104
110
  */
105
111
  export default stampit({
106
112
  init ({ connectionInfo = {}, port, debug = false }) {
107
- if (!getBrowserAPI().runtime) throw new Error('Runtime is not accessible in your environment')
113
+ if (!getBrowserAPI().runtime) throw new RpcConnectionError('Runtime is not accessible in your environment')
108
114
  this.debug = debug
109
115
  this.connectionInfo = connectionInfo
110
116
  this.port = port || getBrowserAPI().runtime.connect(...[connectionInfo.id || undefined])
@@ -18,16 +18,24 @@
18
18
  /**
19
19
  * Browser window Post Message connector module
20
20
  *
21
- * This is the complement to {@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection}.
21
+ * This is the complement to
22
+ * {@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection}.
22
23
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message
23
24
  * @export BrowserWindowMessageConnection
24
- * @example import BrowserWindowMessageConnection from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message'
25
+ * @example
26
+ * import BrowserWindowMessageConnection
27
+ * from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message'
25
28
  */
26
29
  import stampit from '@stamp/it'
27
30
  import WalletConnection from '.'
28
- import { v4 as uuid } from 'uuid'
31
+ import { v4 as uuid } from '@aeternity/uuid'
29
32
  import { MESSAGE_DIRECTION } from '../schema'
30
33
  import { getBrowserAPI, isInIframe } from '../helpers'
34
+ import {
35
+ AlreadyConnectedError,
36
+ NoWalletConnectedError,
37
+ MessageDirectionError
38
+ } from '../../errors'
31
39
 
32
40
  /**
33
41
  * Check if connected
@@ -53,7 +61,7 @@ function connect (onMessage) {
53
61
  const receiveDirection = this.receiveDirection
54
62
  const debug = this.debug
55
63
  const forceOrigin = this.forceOrigin
56
- if (this.listener) throw new Error('You already connected')
64
+ if (this.listener) throw new AlreadyConnectedError('You already connected')
57
65
 
58
66
  this.listener = (msg, source) => {
59
67
  if (!msg || typeof msg.data !== 'object') return
@@ -77,7 +85,7 @@ function connect (onMessage) {
77
85
  * @return {void}
78
86
  */
79
87
  function disconnect () {
80
- if (!this.listener) throw new Error('You dont have connection. Please connect before')
88
+ if (!this.listener) throw new NoWalletConnectedError('You dont have connection. Please connect before')
81
89
  this.unsubscribeFn(this.listener)
82
90
  this.listener = null
83
91
  }
@@ -102,29 +110,44 @@ const getTarget = () => {
102
110
  const isContentScript = isExtensionContext && isWeb
103
111
  if (isContentScript) return window
104
112
  // When we is the main page we need to decide the target by our self
105
- // Probably can be implemented some algo for checking DOM for Iframes and somehow decide which Iframe to talk
113
+ // Probably can be implemented some algo for checking DOM for Iframes and somehow decide which
114
+ // Iframe to talk
106
115
  return isInIframe() ? window.parent : undefined
107
116
  }
108
117
 
109
118
  /**
110
119
  * BrowserWindowMessageConnection
111
120
  * @function
112
- * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message
121
+ * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication\
122
+ * /connection/browser-window-message
113
123
  * @rtype Stamp
114
124
  * @param {Object} [params={}] - Initializer object
115
125
  * @param {Object} [params.target=window.parent] - Target window for message
116
126
  * @param {Object} [params.self=window] - Host window for message
117
127
  * @param {Object} [params.origin] - Origin of receiver
118
- * @param {Object} [params.sendDirection] - Optional field for wrapping messages in additional structure({ type: 'to_aepp' || 'to_waellet', data }).Used for handling messages netween content script and page
119
- * @param {Object} [params.receiveDirection='to_aepp'] - Optional(default: 'to_aepp') field for unwrapping messages from additional structure({ type: 'to_aepp' || 'to_waellet', data }).Used for handling messages netween content script and page
128
+ * @param {String} [params.sendDirection] Wrapping messages into additional struct
129
+ * ({ type: 'to_aepp' || 'to_waellet', data })
130
+ * Used for handling messages between content script and page
131
+ * @param {String} [params.receiveDirection='to_aepp'] Unwrapping messages from additional struct
132
+ * ({ type: 'to_aepp' || 'to_waellet', data })
133
+ * Used for handling messages between content script and page
120
134
  * @param {Object} [params.connectionInfo={}] - Connection info object
121
135
  * @param {Boolean} [params.debug=false] - Debug flag
122
136
  * @return {Object}
123
137
  */
124
138
  export default stampit({
125
- init ({ connectionInfo = {}, target = getTarget(), self = window, origin, sendDirection, receiveDirection = MESSAGE_DIRECTION.to_aepp, debug = false, forceOrigin = false } = {}) {
126
- if (sendDirection && !Object.keys(MESSAGE_DIRECTION).includes(sendDirection)) throw new Error(`sendDirection must be one of [${Object.keys(MESSAGE_DIRECTION)}]`)
127
- if (!Object.keys(MESSAGE_DIRECTION).includes(receiveDirection)) throw new Error(`receiveDirection must be one of [${Object.keys(MESSAGE_DIRECTION)}]`)
139
+ init ({
140
+ connectionInfo = {},
141
+ target = getTarget(),
142
+ self = window,
143
+ origin,
144
+ sendDirection,
145
+ receiveDirection = MESSAGE_DIRECTION.to_aepp,
146
+ debug = false,
147
+ forceOrigin = false
148
+ } = {}) {
149
+ if (sendDirection && !Object.keys(MESSAGE_DIRECTION).includes(sendDirection)) throw new MessageDirectionError(`sendDirection must be one of [${Object.keys(MESSAGE_DIRECTION)}]`)
150
+ if (!Object.keys(MESSAGE_DIRECTION).includes(receiveDirection)) throw new MessageDirectionError(`receiveDirection must be one of [${Object.keys(MESSAGE_DIRECTION)}]`)
128
151
  this.connectionInfo = { id: uuid(), ...connectionInfo }
129
152
 
130
153
  const selfP = self
@@ -137,7 +160,6 @@ export default stampit({
137
160
  this.subscribeFn = (listener) => selfP.addEventListener('message', listener, false)
138
161
  this.unsubscribeFn = (listener) => selfP.removeEventListener('message', listener, false)
139
162
  this.postFn = (msg) => targetP.postMessage(msg, this.origin || '*')
140
- if (!this.connectionInfo.id) throw new Error('ID required.')
141
163
  },
142
164
  methods: { connect, sendMessage, disconnect, isConnected }
143
165
  }, WalletConnection)
@@ -19,7 +19,8 @@
19
19
  * Wallet Connection base module
20
20
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection
21
21
  * @export WalletConnection
22
- * @example import WalletConnection from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection'
22
+ * @example
23
+ * import WalletConnection from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection'
23
24
  */
24
25
 
25
26
  import { required } from '@stamp/required'
@@ -20,9 +20,12 @@
20
20
  *
21
21
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/content-script-bridge
22
22
  * @export ContentScriptBridge
23
- * @example import ContentScriptBridge from '@aeternity/aepp-sdk/es/utils/wallet-communication/content-script-bridge
23
+ * @example
24
+ * import ContentScriptBridge
25
+ * from '@aeternity/aepp-sdk/es/utils/wallet-communication/content-script-bridge
24
26
  */
25
27
  import stampit from '@stamp/it'
28
+ import { UnsupportedPlatformError, MissingParamError } from '../errors'
26
29
 
27
30
  /**
28
31
  * Start message proxy
@@ -56,21 +59,25 @@ function stop () {
56
59
 
57
60
  /**
58
61
  * ContentScriptBridge stamp
59
- * Provide functionality to easly redirect messages from page to extension and from extension to page through content script
62
+ * Provide functionality to easly redirect messages from page to extension and from extension to
63
+ * page through content script
60
64
  * Using Runtime(Extension) and WindowPostMessage(Web-Page) connections
61
65
  * @function
62
66
  * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/content-script-bridge
63
67
  * @rtype Stamp
64
68
  * @param {Object} params - Initializer object
65
- * @param {Object} params.pageConnection - Page connection object(@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message)
66
- * @param {Object} params.extConnection - Extension connection object(module: @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-runtime)
69
+ * @param {Object} params.pageConnection - Page connection object
70
+ * (@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/
71
+ * browser-window-message)
72
+ * @param {Object} params.extConnection - Extension connection object
73
+ * (@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-runtime)
67
74
  * @return {Object}
68
75
  */
69
76
  export default stampit({
70
77
  init ({ pageConnection, extConnection, allowCrossOrigin = false }) {
71
- if (!window) throw new Error('Window object not found, you can run bridge only in browser')
72
- if (!pageConnection) throw new Error('pageConnection required')
73
- if (!extConnection) throw new Error('extConnection required')
78
+ if (!window) throw new UnsupportedPlatformError('Window object not found, you can run bridge only in browser')
79
+ if (!pageConnection) throw new MissingParamError('pageConnection required')
80
+ if (!extConnection) throw new MissingParamError('extConnection required')
74
81
  this.allowCrossOrigin = allowCrossOrigin
75
82
  this.pageConnection = pageConnection
76
83
  this.extConnection = extConnection
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Browser helper functions
3
3
  */
4
- import { isAccountBase } from '../../account/base'
4
+ import { NoBrowserFoundError } from '../errors'
5
5
 
6
6
  export const getBrowserAPI = (force = false) => {
7
7
  const { chrome, browser } = window
@@ -9,7 +9,7 @@ export const getBrowserAPI = (force = false) => {
9
9
  if (typeof chrome !== 'undefined' && chrome === Object(chrome)) return chrome
10
10
  // Firefox support
11
11
  if (typeof browser !== 'undefined' && browser === Object(browser)) return browser
12
- if (!force) throw new Error('Browser is not detected')
12
+ if (!force) throw new NoBrowserFoundError()
13
13
  return {}
14
14
  }
15
15
 
@@ -51,18 +51,10 @@ export const getHandler = (schema, msg, { debug = false } = {}) => {
51
51
 
52
52
  export const message = (method, params = {}) => ({ method, params })
53
53
 
54
- export const responseMessage = (id, method, { error, result } = {}) => ({ id, method, ...(error ? { error } : { result }) })
54
+ export const responseMessage = (id, method, { error, result } = {}) =>
55
+ ({ id, method, ...(error ? { error } : { result }) })
55
56
 
56
- export const sendResponseMessage = (client) => (id, method, data) => client.sendMessage(responseMessage(id, method, data), true)
57
-
58
- export const voidFn = () => undefined
57
+ export const sendResponseMessage = (client) => (id, method, data) =>
58
+ client.sendMessage(responseMessage(id, method, data), true)
59
59
 
60
60
  export const isValidAccounts = (accounts) => (['', 'connected', 'current'].filter(k => typeof (k ? accounts[k] : accounts) === 'object')).length === 3
61
-
62
- export const resolveOnAccount = (addresses, onAccount, opt = {}) => {
63
- if (!addresses.find(a => a === onAccount)) {
64
- if (typeof opt.onAccount !== 'object' || !isAccountBase(opt.onAccount)) throw new Error('Provided onAccount should be an AccountBase')
65
- onAccount = opt.onAccount
66
- }
67
- return onAccount
68
- }
@@ -3,26 +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 * as R from 'ramda'
9
- import { v4 as uuid } from 'uuid'
10
-
10
+ import { v4 as uuid } from '@aeternity/uuid'
11
11
  import Ae from '../../../ae'
12
12
  import RpcClient from './rpc-client'
13
- import { getHandler, message, voidFn } from '../helpers'
13
+ import { getHandler, message } from '../helpers'
14
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 '../../../index'
15
24
 
16
25
  const NOTIFICATIONS = {
17
- [METHODS.wallet.updateAddress]: (instance) =>
26
+ [METHODS.updateAddress]: (instance) =>
18
27
  ({ params }) => {
19
28
  instance.rpcClient.accounts = params
20
29
  instance.onAddressChange(params)
21
30
  },
22
31
  [METHODS.updateNetwork]: (instance) =>
23
- (msg) => {
24
- instance.rpcClient.info.networkId = msg.params.networkId
25
- 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)
26
37
  },
27
38
  [METHODS.closeConnection]: (instance) =>
28
39
  (msg) => {
@@ -32,27 +43,33 @@ const NOTIFICATIONS = {
32
43
  }
33
44
 
34
45
  const RESPONSES = {
35
- [METHODS.aepp.address]: (instance) =>
46
+ [METHODS.address]: (instance) =>
36
47
  (msg) => instance.rpcClient.processResponse(msg),
37
- [METHODS.aepp.connect]: (instance) =>
48
+ [METHODS.connect]: (instance) =>
38
49
  (msg) => {
39
50
  if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED
40
51
  instance.rpcClient.processResponse(msg)
41
52
  },
42
- [METHODS.aepp.subscribeAddress]: (instance) =>
53
+ [METHODS.subscribeAddress]: (instance) =>
43
54
  (msg) => {
44
55
  if (msg.result) {
45
- msg.result.address && (instance.rpcClient.accounts = msg.result.address)
46
- 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
+ }
47
62
  }
48
63
 
49
64
  instance.rpcClient.processResponse(msg, ({ id, result }) => [result])
50
65
  },
51
- [METHODS.aepp.sign]: (instance) =>
66
+ [METHODS.sign]: (instance) =>
52
67
  (msg) => {
53
- instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signedTransaction || result.transactionHash])
68
+ instance.rpcClient.processResponse(
69
+ msg, ({ id, result }) => [result.signedTransaction || result.transactionHash]
70
+ )
54
71
  },
55
- [METHODS.aepp.signMessage]: (instance) =>
72
+ [METHODS.signMessage]: (instance) =>
56
73
  (msg) => {
57
74
  instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signature])
58
75
  }
@@ -84,8 +101,18 @@ const handleMessage = (instance) => async (msg) => {
84
101
  * @return {Object}
85
102
  */
86
103
  export default Ae.compose({
87
- async init ({ name, onAddressChange = voidFn, onDisconnect = voidFn, onNetworkChange = voidFn, connection, forceValidation = false, debug = false }) {
88
- 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
+
89
116
  this.connection = connection
90
117
  this.name = name
91
118
  this.debug = debug
@@ -94,21 +121,13 @@ export default Ae.compose({
94
121
  // Init RPCClient
95
122
  await this.connectToWallet(connection)
96
123
  }
97
- // Event callbacks
98
- this.onDisconnect = onDisconnect
99
- this.onAddressChange = onAddressChange
100
- this.onNetworkChange = onNetworkChange
101
- // validation
102
- eventsHandlers.forEach(event => {
103
- if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`)
104
- })
105
124
  },
106
125
  deepProps: { Ae: { defaults: { walletBroadcast: true } } },
107
126
  methods: {
108
127
  sign () {
109
128
  },
110
129
  addresses () {
111
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
130
+ if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
112
131
  return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)]
113
132
  },
114
133
  /**
@@ -117,10 +136,14 @@ export default Ae.compose({
117
136
  * @instance
118
137
  * @rtype (connection: Object) => void
119
138
  * @param {Object} connection Wallet connection object
120
- * @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}
121
144
  */
122
- async connectToWallet (connection) {
123
- 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)
124
147
  this.rpcClient = RpcClient({
125
148
  connection,
126
149
  networkId: this.getNetworkId({ force: true }),
@@ -128,7 +151,12 @@ export default Ae.compose({
128
151
  id: uuid(),
129
152
  handlers: [handleMessage(this), this.onDisconnect]
130
153
  })
131
- 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
132
160
  },
133
161
  /**
134
162
  * Disconnect from wallet
@@ -139,16 +167,18 @@ export default Ae.compose({
139
167
  * @return {void}
140
168
  */
141
169
  async disconnectWallet (sendDisconnect = true) {
142
- 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')
143
171
  sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, { reason: 'bye' }), true)
144
172
  this.rpcClient.disconnect()
145
173
  this.rpcClient = null
146
174
  },
147
175
  async address ({ onAccount } = {}) {
148
- 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')
149
177
  const current = this.rpcClient.currentAccount
150
- if (!current) throw new Error('You are not subscribed for an account.')
151
- 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
+ }
152
182
  return onAccount || current
153
183
  },
154
184
  /**
@@ -159,22 +189,22 @@ export default Ae.compose({
159
189
  * @return {Promise} Address from wallet
160
190
  */
161
191
  async askAddresses () {
162
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
163
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
164
- 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)
165
195
  },
166
196
  /**
167
197
  * Subscribe for addresses from wallet
168
198
  * @function subscribeAddress
169
199
  * @instance
170
200
  * @rtype (type: String, value: String) => Promise
171
- * @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)
172
202
  * @param {String} value Subscription action('subscribe'|'unsubscribe')
173
203
  * @return {Promise} Address from wallet
174
204
  */
175
205
  async subscribeAddress (type, value) {
176
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
177
- 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 })
178
208
  },
179
209
  /**
180
210
  * Overwriting of `signTransaction` AE method
@@ -185,10 +215,15 @@ export default Ae.compose({
185
215
  * @return {Promise<String>} Signed transaction
186
216
  */
187
217
  async signTransaction (tx, opt = {}) {
188
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
189
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
190
- if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new Error(`You do not have access to account ${opt.onAccount}`)
191
- 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
+ )
192
227
  },
193
228
  /**
194
229
  * Overwriting of `signMessage` AE method
@@ -199,31 +234,36 @@ export default Ae.compose({
199
234
  * @return {Promise<String>} Signed transaction
200
235
  */
201
236
  async signMessage (msg, opt = {}) {
202
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
203
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
204
- if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new Error(`You do not have access to account ${opt.onAccount}`)
205
- 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 })
206
243
  },
207
244
  /**
208
245
  * Send connection request to wallet
209
246
  * @function sendConnectRequest
210
247
  * @instance
248
+ * @param {Boolean} connectNode - Request wallet to bind node
211
249
  * @rtype () => Promise
212
250
  * @return {Promise} Connection response
213
251
  */
214
- async sendConnectRequest () {
252
+ async sendConnectRequest (connectNode) {
215
253
  return this.rpcClient.request(
216
- METHODS.aepp.connect, {
254
+ METHODS.connect, {
217
255
  name: this.name,
218
256
  version: VERSION,
219
- networkId: this.getNetworkId({ force: true })
257
+ networkId: this.getNetworkId({ force: true }),
258
+ connectNode
220
259
  }
221
260
  )
222
261
  },
223
262
  /**
224
263
  * Overwriting of `send` AE method
225
264
  * All sdk API which use it will be send notification to wallet and wait for callBack
226
- * 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
227
267
  * @function send
228
268
  * @instance
229
269
  * @rtype (tx: String, options = {}) => Promise
@@ -233,15 +273,22 @@ export default Ae.compose({
233
273
  * @return {Promise<Object>} Transaction broadcast result
234
274
  */
235
275
  async send (tx, options = {}) {
236
- if (!this.rpcClient || !this.rpcClient.isConnected()) throw new Error('You are not connected to Wallet')
237
- if (!this.rpcClient.currentAccount) throw new Error('You are not subscribed for an account.')
238
- if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) throw new Error(`You do not have access to account ${options.onAccount}`)
239
- const opt = R.merge(this.Ae.defaults, options)
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
+ }
281
+ const opt = { ...this.Ae.defaults, ...options }
240
282
  if (!opt.walletBroadcast) {
241
283
  const signed = await this.signTransaction(tx, { onAccount: opt.onAccount })
242
284
  return this.sendTransaction(signed, opt)
243
285
  }
244
- return this.rpcClient.request(METHODS.aepp.sign, { onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() })
245
- }
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' }
246
293
  }
247
294
  })