@aeternity/aepp-sdk 10.0.0 → 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 (238) 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} +20 -10
  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} +10 -9
  14. package/es/account/multiple.mjs.map +1 -0
  15. package/es/ae/{aens.js → aens.mjs} +18 -22
  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} +8 -7
  22. package/es/ae/index.mjs.map +1 -0
  23. package/es/ae/{oracle.js → oracle.mjs} +33 -16
  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} +20 -4
  32. package/es/chain/index.mjs.map +1 -0
  33. package/es/chain/{node.js → node.mjs} +121 -73
  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} +7 -7
  38. package/es/channel/index.mjs.map +1 -0
  39. package/es/channel/{internal.js → internal.mjs} +49 -32
  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} +36 -28
  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.js → index.mjs} +11 -10
  53. package/es/node-pool/index.mjs.map +1 -0
  54. package/es/{node.js → node.mjs} +9 -8
  55. package/es/node.mjs.map +1 -0
  56. package/es/tx/builder/field-types.mjs +74 -0
  57. package/es/tx/builder/field-types.mjs.map +1 -0
  58. package/es/tx/builder/{helpers.js → helpers.mjs} +114 -56
  59. package/es/tx/builder/helpers.mjs.map +1 -0
  60. package/es/tx/builder/{index.js → index.mjs} +79 -68
  61. package/es/tx/builder/index.mjs.map +1 -0
  62. package/es/tx/builder/{schema.js → schema.mjs} +11 -65
  63. package/es/tx/builder/schema.mjs.map +1 -0
  64. package/es/tx/{index.js → index.mjs} +5 -2
  65. package/es/tx/index.mjs.map +1 -0
  66. package/es/tx/{tx-object.js → tx-object.mjs} +22 -15
  67. package/es/tx/tx-object.mjs.map +1 -0
  68. package/es/tx/{tx.js → tx.mjs} +15 -14
  69. package/es/tx/tx.mjs.map +1 -0
  70. package/es/tx/{validator.js → validator.mjs} +72 -33
  71. package/es/tx/validator.mjs.map +1 -0
  72. package/es/typings/blakejs/index.d.mjs +2 -0
  73. package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
  74. package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +7 -6
  75. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
  76. package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +10 -11
  77. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
  78. package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +5 -2
  79. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
  80. package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +6 -5
  81. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
  82. package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
  83. package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
  84. package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +113 -67
  85. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
  86. package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +14 -9
  87. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
  88. package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +71 -63
  89. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
  90. package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
  91. package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
  92. package/es/utils/aepp-wallet-communication/{wallet-detector.js → wallet-detector.mjs} +6 -5
  93. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  94. package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
  95. package/es/utils/amount-formatter.mjs.map +1 -0
  96. package/es/utils/{async-init.js → async-init.mjs} +8 -11
  97. package/es/utils/async-init.mjs.map +1 -0
  98. package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
  99. package/es/utils/bignumber.mjs.map +1 -0
  100. package/es/utils/{bytes.js → bytes.mjs} +16 -8
  101. package/es/utils/bytes.mjs.map +1 -0
  102. package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
  103. package/es/utils/crypto-ts.mjs.map +1 -0
  104. package/es/utils/{crypto.js → crypto.mjs} +40 -105
  105. package/es/utils/crypto.mjs.map +1 -0
  106. package/es/utils/errors.mjs +689 -0
  107. package/es/utils/errors.mjs.map +1 -0
  108. package/es/utils/{hd-wallet.js → hd-wallet.mjs} +33 -34
  109. package/es/utils/hd-wallet.mjs.map +1 -0
  110. package/es/utils/{json-big.js → json-big.mjs} +1 -1
  111. package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
  112. package/es/utils/{keystore.js → keystore.mjs} +63 -51
  113. package/es/utils/keystore.mjs.map +1 -0
  114. package/es/utils/{mptree.js → mptree.mjs} +21 -12
  115. package/es/utils/mptree.mjs.map +1 -0
  116. package/es/utils/{other.js → other.mjs} +2 -2
  117. package/es/utils/other.mjs.map +1 -0
  118. package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
  119. package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
  120. package/es/utils/{string.js → string.mjs} +1 -1
  121. package/es/utils/{string.js.map → string.mjs.map} +1 -1
  122. package/es/utils/{swagger.js → swagger.mjs} +46 -14
  123. package/es/utils/swagger.mjs.map +1 -0
  124. package/package.json +46 -55
  125. package/src/account/memory.js +13 -8
  126. package/src/account/multiple.js +12 -4
  127. package/src/ae/aens.js +12 -17
  128. package/src/ae/aepp.js +2 -2
  129. package/src/ae/contract.js +15 -152
  130. package/src/ae/index.js +2 -1
  131. package/src/ae/oracle.js +25 -10
  132. package/src/ae/universal.js +2 -2
  133. package/src/ae/wallet.js +2 -2
  134. package/src/chain/index.js +21 -2
  135. package/src/chain/node.js +80 -53
  136. package/src/channel/handlers.js +20 -17
  137. package/src/channel/internal.js +30 -24
  138. package/src/contract/{aci/index.js → aci.js} +152 -93
  139. package/src/contract/compiler.js +22 -117
  140. package/src/contract/ga/helpers.js +7 -5
  141. package/src/contract/ga/index.js +27 -24
  142. package/src/index.js +7 -8
  143. package/src/node-pool/index.js +9 -8
  144. package/src/node.js +5 -7
  145. package/src/tx/builder/field-types.js +61 -0
  146. package/src/tx/builder/helpers.js +113 -58
  147. package/src/tx/builder/index.js +39 -34
  148. package/src/tx/builder/schema.js +11 -25
  149. package/src/tx/tx-object.js +10 -9
  150. package/src/tx/tx.js +17 -8
  151. package/src/tx/validator.js +25 -5
  152. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +8 -3
  153. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +10 -6
  154. package/src/utils/aepp-wallet-communication/content-script-bridge.js +5 -4
  155. package/src/utils/aepp-wallet-communication/helpers.js +2 -12
  156. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +81 -55
  157. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +15 -6
  158. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +60 -49
  159. package/src/utils/aepp-wallet-communication/schema.ts +80 -0
  160. package/src/utils/aepp-wallet-communication/wallet-detector.js +3 -2
  161. package/src/utils/amount-formatter.ts +4 -3
  162. package/src/utils/async-init.js +10 -6
  163. package/src/utils/bignumber.ts +1 -9
  164. package/src/utils/bytes.ts +3 -2
  165. package/src/utils/crypto-ts.ts +2 -2
  166. package/src/utils/crypto.js +9 -92
  167. package/src/utils/errors.ts +683 -0
  168. package/src/utils/hd-wallet.js +11 -26
  169. package/src/utils/keystore.js +47 -51
  170. package/src/utils/mptree.ts +12 -6
  171. package/src/utils/swagger.js +67 -44
  172. package/CHANGELOG.md +0 -1603
  173. package/es/account/base.js.map +0 -1
  174. package/es/account/memory.js.map +0 -1
  175. package/es/account/multiple.js.map +0 -1
  176. package/es/ae/aens.js.map +0 -1
  177. package/es/ae/aepp.js.map +0 -1
  178. package/es/ae/contract.js +0 -312
  179. package/es/ae/contract.js.map +0 -1
  180. package/es/ae/index.js.map +0 -1
  181. package/es/ae/oracle.js.map +0 -1
  182. package/es/ae/universal.js.map +0 -1
  183. package/es/ae/wallet.js.map +0 -1
  184. package/es/chain/index.js.map +0 -1
  185. package/es/chain/node.js.map +0 -1
  186. package/es/channel/handlers.js.map +0 -1
  187. package/es/channel/index.js.map +0 -1
  188. package/es/channel/internal.js.map +0 -1
  189. package/es/contract/aci/helpers.js +0 -298
  190. package/es/contract/aci/helpers.js.map +0 -1
  191. package/es/contract/aci/index.js +0 -361
  192. package/es/contract/aci/index.js.map +0 -1
  193. package/es/contract/aci/transformation.js +0 -66
  194. package/es/contract/aci/transformation.js.map +0 -1
  195. package/es/contract/compiler.js +0 -244
  196. package/es/contract/compiler.js.map +0 -1
  197. package/es/contract/ga/helpers.js +0 -22
  198. package/es/contract/ga/helpers.js.map +0 -1
  199. package/es/contract/ga/index.js.map +0 -1
  200. package/es/contract/index.js +0 -162
  201. package/es/contract/index.js.map +0 -1
  202. package/es/index.js +0 -57
  203. package/es/index.js.map +0 -1
  204. package/es/node-pool/index.js.map +0 -1
  205. package/es/node.js.map +0 -1
  206. package/es/tx/builder/helpers.js.map +0 -1
  207. package/es/tx/builder/index.js.map +0 -1
  208. package/es/tx/builder/schema.js.map +0 -1
  209. package/es/tx/index.js.map +0 -1
  210. package/es/tx/tx-object.js.map +0 -1
  211. package/es/tx/tx.js.map +0 -1
  212. package/es/tx/validator.js.map +0 -1
  213. package/es/typings/blakejs/index.d.js +0 -2
  214. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  215. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  216. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  217. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  218. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  219. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  220. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  221. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  222. package/es/utils/aepp-wallet-communication/schema.js +0 -107
  223. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  224. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  225. package/es/utils/amount-formatter.js.map +0 -1
  226. package/es/utils/async-init.js.map +0 -1
  227. package/es/utils/bignumber.js.map +0 -1
  228. package/es/utils/bytes.js.map +0 -1
  229. package/es/utils/crypto-ts.js.map +0 -1
  230. package/es/utils/crypto.js.map +0 -1
  231. package/es/utils/hd-wallet.js.map +0 -1
  232. package/es/utils/keystore.js.map +0 -1
  233. package/es/utils/mptree.js.map +0 -1
  234. package/es/utils/other.js.map +0 -1
  235. package/es/utils/swagger.js.map +0 -1
  236. package/src/contract/aci/transformation.js +0 -72
  237. package/src/contract/index.js +0 -163
  238. package/src/utils/aepp-wallet-communication/schema.js +0 -108
@@ -10,6 +10,12 @@ import stampit from '@stamp/it'
10
10
 
11
11
  import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema'
12
12
  import { sendMessage, message, isValidAccounts } from '../helpers'
13
+ import {
14
+ InvalidRpcMessageError,
15
+ TypeError,
16
+ DuplicateCallbackError,
17
+ MissingCallbackError
18
+ } from '../../errors'
13
19
 
14
20
  /**
15
21
  * Contain functionality for using RPC conection
@@ -45,7 +51,7 @@ export default stampit({
45
51
 
46
52
  const handleMessage = (msg, origin) => {
47
53
  if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {
48
- throw new Error(`Received invalid message: ${msg}`)
54
+ throw new InvalidRpcMessageError(msg)
49
55
  }
50
56
  onMessage(msg, origin)
51
57
  }
@@ -123,7 +129,8 @@ export default stampit({
123
129
  * @return {Boolean} is connected
124
130
  */
125
131
  isConnected () {
126
- 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)
127
134
  },
128
135
  /**
129
136
  * Get selected account
@@ -157,12 +164,12 @@ export default stampit({
157
164
  */
158
165
  setAccounts (accounts, { forceNotification } = {}) {
159
166
  if (!isValidAccounts(accounts)) {
160
- 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: {} }`')
161
168
  }
162
169
  this.accounts = accounts
163
170
  if (!forceNotification) {
164
171
  // Sent notification about account updates
165
- this.sendMessage(message(METHODS.wallet.updateAddress, this.accounts), true)
172
+ this.sendMessage(message(METHODS.updateAddress, this.accounts), true)
166
173
  }
167
174
  },
168
175
  /**
@@ -194,7 +201,9 @@ export default stampit({
194
201
  */
195
202
  request (name, params) {
196
203
  const msgId = this.sendMessage(message(name, params))
197
- 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
+ }
198
207
  return new Promise((resolve, reject) => {
199
208
  this.callbacks[msgId] = { resolve, reject }
200
209
  })
@@ -209,7 +218,7 @@ export default stampit({
209
218
  * @return {void}
210
219
  */
211
220
  processResponse ({ id, error, result }, transformResult) {
212
- if (!this.callbacks[id]) throw new Error(`Can't find callback for this messageId ${id}`)
221
+ if (!this.callbacks[id]) throw new MissingCallbackError(id)
213
222
  if (result) {
214
223
  this.callbacks[id].resolve(...typeof transformResult === 'function'
215
224
  ? transformResult({ id, result })
@@ -6,14 +6,24 @@
6
6
  * @example
7
7
  * import WalletRpc from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/wallet-rpc'
8
8
  */
9
- import { v4 as uuid } from 'uuid'
9
+ import { v4 as uuid } from '@aeternity/uuid'
10
10
  import Ae from '../../../ae'
11
11
  import verifyTransaction from '../../../tx/validator'
12
12
  import AccountMultiple from '../../../account/multiple'
13
13
  import TxObject from '../../../tx/tx-object'
14
14
  import RpcClient from './rpc-client'
15
- import { getBrowserAPI, getHandler, isValidAccounts, message, resolveOnAccount, sendResponseMessage } from '../helpers'
15
+ import { getBrowserAPI, getHandler, isValidAccounts, message, sendResponseMessage } from '../helpers'
16
16
  import { ERRORS, METHODS, RPC_STATUS, VERSION, WALLET_TYPE } from '../schema'
17
+ import { ArgumentError, TypeError, UnknownRpcClientError } from '../../errors'
18
+ import { isAccountBase } from '../../../account/base'
19
+
20
+ const resolveOnAccount = (addresses, onAccount, opt = {}) => {
21
+ if (!addresses.find(a => a === onAccount)) {
22
+ if (typeof opt.onAccount !== 'object' || !isAccountBase(opt.onAccount)) throw new TypeError('Provided onAccount should be an AccountBase')
23
+ onAccount = opt.onAccount
24
+ }
25
+ return onAccount
26
+ }
17
27
 
18
28
  const NOTIFICATIONS = {
19
29
  [METHODS.closeConnection]: (instance, { client }) =>
@@ -28,26 +38,36 @@ const RESPONSES = {}
28
38
  const REQUESTS = {
29
39
  // Store client info and prepare two fn for each client `connect` and `denyConnection`
30
40
  // which automatically prepare and send response for that client
31
- [METHODS.aepp.connect] (callInstance, instance, client, { name, networkId, version, icons }) {
41
+ [METHODS.connect] (
42
+ callInstance,
43
+ instance,
44
+ client,
45
+ { name, networkId, version, icons, connectNode }) {
32
46
  // Check if protocol and network is compatible with wallet
33
47
  if (version !== VERSION) return { error: ERRORS.unsupportedProtocol() }
34
-
35
48
  // Store new AEPP and wait for connection approve
36
49
  client.updateInfo({
37
50
  status: RPC_STATUS.WAITING_FOR_CONNECTION_APPROVE,
38
51
  name,
39
52
  networkId,
40
53
  icons,
41
- version
54
+ version,
55
+ origin: window.location.origin,
56
+ connectNode
42
57
  })
43
58
 
44
59
  // Call onConnection callBack to notice Wallet about new AEPP
45
60
  return callInstance(
46
61
  'onConnection',
47
62
  { name, networkId, version },
48
- () => {
49
- client.updateInfo({ status: RPC_STATUS.CONNECTED })
50
- return { result: instance.getWalletInfo() }
63
+ ({ shareNode } = {}) => {
64
+ client.updateInfo({ status: shareNode ? RPC_STATUS.NODE_BINDED : RPC_STATUS.CONNECTED })
65
+ return {
66
+ result: {
67
+ ...instance.getWalletInfo(),
68
+ ...(shareNode && { node: instance.selectedNode })
69
+ }
70
+ }
51
71
  },
52
72
  (error) => {
53
73
  client.updateInfo({ status: RPC_STATUS.CONNECTION_REJECTED })
@@ -55,7 +75,7 @@ const REQUESTS = {
55
75
  }
56
76
  )
57
77
  },
58
- [METHODS.aepp.subscribeAddress] (callInstance, instance, client, { type, value }) {
78
+ [METHODS.subscribeAddress] (callInstance, instance, client, { type, value }) {
59
79
  // Authorization check
60
80
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
61
81
 
@@ -65,7 +85,9 @@ const REQUESTS = {
65
85
  async ({ accounts } = {}) => {
66
86
  try {
67
87
  const clientAccounts = accounts || instance.getAccounts()
68
- if (!isValidAccounts(clientAccounts)) throw new Error('Invalid provided accounts object')
88
+ if (!isValidAccounts(clientAccounts)) {
89
+ throw new TypeError('Invalid provided accounts object')
90
+ }
69
91
  const subscription = client.updateSubscription(type, value)
70
92
  client.setAccounts(clientAccounts, { forceNotification: true })
71
93
  return {
@@ -76,13 +98,13 @@ const REQUESTS = {
76
98
  }
77
99
  } catch (e) {
78
100
  if (instance.debug) console.error(e)
79
- return { error: ERRORS.internalError({ msg: e.message }) }
101
+ return { error: ERRORS.internalError(e.message) }
80
102
  }
81
103
  },
82
104
  (error) => ({ error: ERRORS.rejectedByUser(error) })
83
105
  )
84
106
  },
85
- [METHODS.aepp.address] (callInstance, instance, client) {
107
+ [METHODS.address] (callInstance, instance, client) {
86
108
  // Authorization check
87
109
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
88
110
  if (!client.isSubscribed()) return { error: ERRORS.notAuthorize() }
@@ -97,7 +119,7 @@ const REQUESTS = {
97
119
  (error) => ({ error: ERRORS.rejectedByUser(error) })
98
120
  )
99
121
  },
100
- [METHODS.aepp.sign] (callInstance, instance, client, options) {
122
+ [METHODS.sign] (callInstance, instance, client, options) {
101
123
  const { tx, onAccount, networkId, returnSigned = false } = options
102
124
  const address = onAccount || client.currentAccount
103
125
  // Update client with new networkId
@@ -106,7 +128,7 @@ const REQUESTS = {
106
128
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
107
129
  // Account permission check
108
130
  if (!client.hasAccessToAccount(address)) {
109
- return { error: ERRORS.permissionDeny({ account: address }) }
131
+ return { error: ERRORS.permissionDeny(address) }
110
132
  }
111
133
  // NetworkId check
112
134
  if (!networkId || networkId !== instance.getNetworkId()) {
@@ -122,7 +144,7 @@ const REQUESTS = {
122
144
  onAcc = resolveOnAccount(instance.addresses(), address, opt)
123
145
  } catch (e) {
124
146
  if (instance.debug) console.error(e)
125
- return { error: ERRORS.internalError({ msg: e.message }) }
147
+ return { error: ERRORS.internalError(e.message) }
126
148
  }
127
149
  try {
128
150
  const t = rawTx || tx
@@ -144,12 +166,12 @@ const REQUESTS = {
144
166
  (error) => ({ error: ERRORS.rejectedByUser(error) })
145
167
  )
146
168
  },
147
- [METHODS.aepp.signMessage] (callInstance, instance, client, { message, onAccount }) {
169
+ [METHODS.signMessage] (callInstance, instance, client, { message, onAccount }) {
148
170
  // Authorization check
149
171
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
150
172
  const address = onAccount || client.currentAccount
151
173
  if (!client.hasAccessToAccount(address)) {
152
- return { error: ERRORS.permissionDeny({ account: address }) }
174
+ return { error: ERRORS.permissionDeny(address) }
153
175
  }
154
176
 
155
177
  return callInstance(
@@ -168,7 +190,7 @@ const REQUESTS = {
168
190
  }
169
191
  } catch (e) {
170
192
  if (instance.debug) console.error(e)
171
- return { error: ERRORS.internalError({ msg: e.message }) }
193
+ return { error: ERRORS.internalError(e.message) }
172
194
  }
173
195
  },
174
196
  (error) => ({ error: ERRORS.rejectedByUser(error) })
@@ -221,41 +243,26 @@ const handleMessage = (instance, id) => async (msg, origin) => {
221
243
  * @param {Function} onSign Call-back function for incoming AEPP sign request
222
244
  * @param {Function} onAskAccounts Call-back function for incoming AEPP get address request
223
245
  * @param {Function} onMessageSign Call-back function for incoming AEPP sign message request
224
- * Second argument of incoming call-backs contain function for accept/deny request
246
+ * Second argument of incoming call-backs contain function for accept/deny request
225
247
  * @param {Function} onDisconnect Call-back function for disconnect event
226
248
  * @return {Object}
227
249
  */
228
250
  export default Ae.compose(AccountMultiple, {
229
251
  init ({
230
252
  name,
231
- onConnection,
232
- onSubscription,
233
- onSign,
234
- onDisconnect,
235
- onAskAccounts,
236
- onMessageSign,
237
- forceValidation = false,
238
- debug = false
253
+ debug = false,
254
+ ...other
239
255
  } = {}) {
256
+ [
257
+ 'onConnection', 'onSubscription', 'onSign', 'onDisconnect', 'onAskAccounts', 'onMessageSign'
258
+ ].forEach(event => {
259
+ const handler = other[event]
260
+ if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler)
261
+ this[event] = handler
262
+ })
263
+
240
264
  this.debug = debug
241
- const eventsHandlers = [
242
- 'onConnection', 'onSubscription', 'onSign', 'onDisconnect', 'onMessageSign'
243
- ]
244
- // CallBacks for events
245
- this.onConnection = onConnection
246
- this.onSubscription = onSubscription
247
- this.onSign = onSign
248
- this.onDisconnect = onDisconnect
249
- this.onAskAccounts = onAskAccounts
250
- this.onMessageSign = onMessageSign
251
265
  this.rpcClients = {}
252
-
253
- eventsHandlers.forEach(event => {
254
- if (!forceValidation && typeof this[event] !== 'function') {
255
- throw new Error(`Call-back for ${event} must be an function!`)
256
- }
257
- })
258
- //
259
266
  this.name = name
260
267
  this.id = uuid()
261
268
 
@@ -298,9 +305,13 @@ export default Ae.compose(AccountMultiple, {
298
305
  // Send notification 'update.network' to all Aepp which connected
299
306
  Object.values(this.rpcClients)
300
307
  .filter(client => client.isConnected())
301
- .forEach(client => client.sendMessage(
302
- message(METHODS.updateNetwork, { networkId: this.getNetworkId() }), true
303
- ))
308
+ .forEach(client => {
309
+ client.sendMessage(
310
+ message(METHODS.updateNetwork, {
311
+ networkId: this.getNetworkId(),
312
+ ...(client.info.status === RPC_STATUS.NODE_BINDED && { node: this.selectedNode })
313
+ }), true)
314
+ })
304
315
  }
305
316
  },
306
317
  methods: {
@@ -315,7 +326,7 @@ export default Ae.compose(AccountMultiple, {
315
326
  */
316
327
  removeRpcClient (id, { forceConnectionClose = false } = {}) {
317
328
  const client = this.rpcClients[id]
318
- if (!client) throw new Error(`RpcClient with id ${id} do not exist`)
329
+ if (!client) throw new UnknownRpcClientError(id)
319
330
  client.disconnect(forceConnectionClose)
320
331
  delete this.rpcClients[id]
321
332
  },
@@ -351,7 +362,7 @@ export default Ae.compose(AccountMultiple, {
351
362
  shareWalletInfo (postFn) {
352
363
  postFn({
353
364
  jsonrpc: '2.0',
354
- ...message(METHODS.wallet.readyToConnect, this.getWalletInfo())
365
+ ...message(METHODS.readyToConnect, this.getWalletInfo())
355
366
  })
356
367
  },
357
368
  /**
@@ -0,0 +1,80 @@
1
+ export const VERSION = 1
2
+
3
+ export const enum MESSAGE_DIRECTION {
4
+ to_waellet = 'to_waellet',
5
+ to_aepp = 'to_aepp'
6
+ }
7
+
8
+ export const enum WALLET_TYPE {
9
+ window = 'window',
10
+ extension = 'extension'
11
+ }
12
+
13
+ export const enum SUBSCRIPTION_TYPES {
14
+ subscribe = 'subscribe',
15
+ unsubscribe = 'unsubscribe'
16
+ }
17
+
18
+ export const enum METHODS {
19
+ readyToConnect = 'connection.announcePresence',
20
+ updateAddress = 'address.update',
21
+ address = 'address.get',
22
+ connect = 'connection.open',
23
+ sign = 'transaction.sign',
24
+ signMessage = 'message.sign',
25
+ subscribeAddress = 'address.subscribe',
26
+ updateNetwork = 'networkId.update',
27
+ closeConnection = 'connection.close'
28
+ }
29
+
30
+ export const enum RPC_STATUS {
31
+ CONNECTED = 'CONNECTED',
32
+ NODE_BINDED = 'NODE_BINDED',
33
+ DISCONNECTED = 'DISCONNECTED',
34
+ CONNECTION_REJECTED = 'CONNECTION_REJECTED',
35
+ WAITING_FOR_CONNECTION_APPROVE = 'WAITING_FOR_CONNECTION_APPROVE',
36
+ WAITING_FOR_CONNECTION_REQUEST = 'WAITING_FOR_CONNECTION_REQUEST'
37
+ }
38
+
39
+ export const ERRORS = {
40
+ broadcastFailed: (error = {}) => ({
41
+ code: 3,
42
+ data: error,
43
+ message: 'Broadcast failed'
44
+ }),
45
+ invalidTransaction: (error = {}) => ({
46
+ code: 2,
47
+ data: error,
48
+ message: 'Invalid transaction'
49
+ }),
50
+ rejectedByUser: (error = {}) => ({
51
+ code: 4,
52
+ data: error,
53
+ message: 'Operation rejected by user'
54
+ }),
55
+ connectionDeny: (error = {}) => ({
56
+ code: 9,
57
+ data: error,
58
+ message: 'Wallet deny your connection request'
59
+ }),
60
+ permissionDeny: (address: string) => ({
61
+ code: 11,
62
+ message: `You are not subscribed for account ${address}`
63
+ }),
64
+ internalError: (message: string) => ({
65
+ code: 12,
66
+ message
67
+ }),
68
+ notAuthorize: () => ({
69
+ code: 10,
70
+ message: 'You are not connected to the wallet'
71
+ }),
72
+ unsupportedProtocol: () => ({
73
+ code: 5,
74
+ message: 'Unsupported Protocol Version'
75
+ }),
76
+ unsupportedNetwork: () => ({
77
+ code: 8,
78
+ message: 'Unsupported Network'
79
+ })
80
+ }
@@ -25,6 +25,7 @@
25
25
  import stampit from '@stamp/it'
26
26
  import BrowserWindowMessageConnection from './connection/browser-window-message'
27
27
  import { MESSAGE_DIRECTION, METHODS } from './schema'
28
+ import { UnsupportedPlatformError } from '../errors'
28
29
 
29
30
  /**
30
31
  * WalletDetector stamp
@@ -37,7 +38,7 @@ import { MESSAGE_DIRECTION, METHODS } from './schema'
37
38
  */
38
39
  export default stampit({
39
40
  init ({ connection } = {}) {
40
- if (!window) throw new Error('Window object not found, you can run wallet detector only in browser')
41
+ if (!window) throw new UnsupportedPlatformError('Window object not found, you can run wallet detector only in browser')
41
42
  this.connection = connection || BrowserWindowMessageConnection({ connectionInfo: { id: 'spy' } })
42
43
  this.wallets = {}
43
44
  },
@@ -54,7 +55,7 @@ export default stampit({
54
55
  this.connection.connect(({ method, params }, origin, source) => {
55
56
  if (
56
57
  !method || !params ||
57
- method !== METHODS.wallet.readyToConnect || wallets[params.id]
58
+ method !== METHODS.readyToConnect || wallets[params.id]
58
59
  ) return
59
60
 
60
61
  const wallet = {
@@ -22,6 +22,7 @@
22
22
  */
23
23
  import BigNumber from 'bignumber.js'
24
24
  import { isBigNumber } from './bignumber'
25
+ import { ArgumentError, InvalidDenominationError } from './errors'
25
26
 
26
27
  /**
27
28
  * AE amount formats
@@ -86,9 +87,9 @@ export const formatAmount = (
86
87
  { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS } = {}
87
88
  ): string => {
88
89
  const denominations = Object.values(AE_AMOUNT_FORMATS)
89
- if (!denominations.includes(denomination)) throw new Error(`Invalid denomination: ${denomination}`)
90
- if (!denominations.includes(targetDenomination)) throw new Error(`Invalid target denomination: ${targetDenomination}`)
91
- if (!isBigNumber(value)) throw new Error(`Value ${value?.toString()} is not type of number`)
90
+ if (!denominations.includes(denomination)) throw new InvalidDenominationError(`Invalid denomination: ${denomination}`)
91
+ if (!denominations.includes(targetDenomination)) throw new InvalidDenominationError(`Invalid target denomination: ${targetDenomination}`)
92
+ if (!isBigNumber(value)) throw new ArgumentError('value', `a number`, value)
92
93
 
93
94
  return new BigNumber(value)
94
95
  .shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination])
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * ISC License (ISC)
3
- * Copyright (c) 2018 aeternity developers
3
+ * Copyright (c) 2022 aeternity developers
4
4
  *
5
5
  * Permission to use, copy, modify, and/or distribute this software for any
6
6
  * purpose with or without fee is hereby granted, provided that the above
@@ -16,7 +16,6 @@
16
16
  */
17
17
 
18
18
  import stampit from '@stamp/it'
19
- import * as R from 'ramda'
20
19
 
21
20
  function asyncInit (options = {}, { stamp, args, instance }) {
22
21
  return stamp.compose.deepConfiguration.AsyncInit.initializers.reduce(async (instance, init) => {
@@ -29,13 +28,18 @@ function asyncInit (options = {}, { stamp, args, instance }) {
29
28
  }, instance)
30
29
  }
31
30
 
32
- const AsyncInit = stampit({
31
+ export default stampit({
33
32
  deepConf: { AsyncInit: { initializers: [] } },
34
33
  composers ({ stamp, composables }) {
35
34
  const conf = stamp.compose.deepConfiguration.AsyncInit
36
- conf.initializers = R.without([asyncInit], R.uniqWith(R.identical, R.flatten(composables.map(c => R.path(['compose', 'deepConfiguration', 'AsyncInit', 'initializers'], c) || (c.compose || c).initializers || []))))
35
+ conf.initializers = composables
36
+ .map(c =>
37
+ c?.compose?.deepConfiguration?.AsyncInit?.initializers ||
38
+ (c.compose || c).initializers ||
39
+ [])
40
+ .flat(Infinity)
41
+ .filter((f, idx, arr) => !arr.slice(0, idx).includes(f))
42
+ .filter(f => f !== asyncInit)
37
43
  stamp.compose.initializers = [asyncInit]
38
44
  }
39
45
  })
40
-
41
- export default AsyncInit
@@ -1,18 +1,10 @@
1
1
  /**
2
2
  * Big Number Helpers
3
3
  * @module @aeternity/aepp-sdk/es/utils/bignumber
4
- * @example import { parseBigNumber, isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
4
+ * @example import { isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
5
5
  */
6
6
  import BigNumber from 'bignumber.js'
7
7
 
8
- /**
9
- * Convert number to string
10
- * @param {String|Number|BigNumber} number number to convert
11
- * @return {String}
12
- */
13
- export const parseBigNumber = (number: string | number | BigNumber): string =>
14
- new BigNumber(number.toString()).toString(10)
15
-
16
8
  /**
17
9
  * Check if value is BigNumber, Number or number string representation
18
10
  * @param {String|Number|BigNumber} number number to convert
@@ -15,6 +15,7 @@
15
15
  * PERFORMANCE OF THIS SOFTWARE.
16
16
  */
17
17
  import BigNumber from 'bignumber.js'
18
+ import { NoSerializerFoundError, TypeError } from './errors'
18
19
  import { isBase64, isHex } from './string'
19
20
 
20
21
  /**
@@ -29,7 +30,7 @@ import { isBase64, isHex } from './string'
29
30
  * @return Buffer
30
31
  */
31
32
  export function bigNumberToByteArray (x: BigNumber): Buffer {
32
- if (!x.isInteger()) throw new Error(`Unexpected not integer value: ${x.toFixed()}`)
33
+ if (!x.isInteger()) throw new TypeError(`Unexpected not integer value: ${x.toFixed()}`)
33
34
  let hexString = x.toString(16)
34
35
  if (hexString.length % 2 === 1) hexString = '0' + hexString
35
36
  return Buffer.from(hexString, 'hex')
@@ -54,7 +55,7 @@ export function toBytes (val?: null | string | number | BigNumber, big: boolean
54
55
  if (typeof val === 'string') {
55
56
  return Buffer.from(val)
56
57
  }
57
- throw new Error('Byte serialization not supported')
58
+ throw new NoSerializerFoundError()
58
59
  }
59
60
 
60
61
  /**
@@ -15,7 +15,7 @@
15
15
  * PERFORMANCE OF THIS SOFTWARE.
16
16
  */
17
17
 
18
- import { blake2b } from 'blakejs'
18
+ import { blake2b } from '@aeternity/blakejs'
19
19
  import type { Data } from 'blakejs'
20
20
 
21
21
  /**
@@ -25,5 +25,5 @@ import type { Data } from 'blakejs'
25
25
  * @return {Buffer} Hash
26
26
  */
27
27
  export function hash (input: Data): Buffer {
28
- return Buffer.from(blake2b(input, null, 32)) // 256 bits
28
+ return Buffer.from(blake2b(input, undefined, 32)) // 256 bits
29
29
  }