@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,16 +3,27 @@
3
3
  *
4
4
  * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/wallet-rpc
5
5
  * @export WalletRpc
6
- * @example import WalletRpc from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/wallet-rpc'
6
+ * @example
7
+ * import WalletRpc from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/wallet-rpc'
7
8
  */
8
- import { v4 as uuid } from 'uuid'
9
+ import { v4 as uuid } from '@aeternity/uuid'
9
10
  import Ae from '../../../ae'
10
11
  import verifyTransaction from '../../../tx/validator'
11
12
  import AccountMultiple from '../../../account/multiple'
12
13
  import TxObject from '../../../tx/tx-object'
13
14
  import RpcClient from './rpc-client'
14
- import { getBrowserAPI, getHandler, isValidAccounts, message, resolveOnAccount, sendResponseMessage } from '../helpers'
15
+ import { getBrowserAPI, getHandler, isValidAccounts, message, sendResponseMessage } from '../helpers'
15
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
+ }
16
27
 
17
28
  const NOTIFICATIONS = {
18
29
  [METHODS.closeConnection]: (instance, { client }) =>
@@ -27,26 +38,36 @@ const RESPONSES = {}
27
38
  const REQUESTS = {
28
39
  // Store client info and prepare two fn for each client `connect` and `denyConnection`
29
40
  // which automatically prepare and send response for that client
30
- [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 }) {
31
46
  // Check if protocol and network is compatible with wallet
32
47
  if (version !== VERSION) return { error: ERRORS.unsupportedProtocol() }
33
-
34
48
  // Store new AEPP and wait for connection approve
35
49
  client.updateInfo({
36
50
  status: RPC_STATUS.WAITING_FOR_CONNECTION_APPROVE,
37
51
  name,
38
52
  networkId,
39
53
  icons,
40
- version
54
+ version,
55
+ origin: window.location.origin,
56
+ connectNode
41
57
  })
42
58
 
43
59
  // Call onConnection callBack to notice Wallet about new AEPP
44
60
  return callInstance(
45
61
  'onConnection',
46
62
  { name, networkId, version },
47
- () => {
48
- client.updateInfo({ status: RPC_STATUS.CONNECTED })
49
- 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
+ }
50
71
  },
51
72
  (error) => {
52
73
  client.updateInfo({ status: RPC_STATUS.CONNECTION_REJECTED })
@@ -54,7 +75,7 @@ const REQUESTS = {
54
75
  }
55
76
  )
56
77
  },
57
- [METHODS.aepp.subscribeAddress] (callInstance, instance, client, { type, value }) {
78
+ [METHODS.subscribeAddress] (callInstance, instance, client, { type, value }) {
58
79
  // Authorization check
59
80
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
60
81
 
@@ -64,7 +85,9 @@ const REQUESTS = {
64
85
  async ({ accounts } = {}) => {
65
86
  try {
66
87
  const clientAccounts = accounts || instance.getAccounts()
67
- if (!isValidAccounts(clientAccounts)) throw new Error('Invalid provided accounts object')
88
+ if (!isValidAccounts(clientAccounts)) {
89
+ throw new TypeError('Invalid provided accounts object')
90
+ }
68
91
  const subscription = client.updateSubscription(type, value)
69
92
  client.setAccounts(clientAccounts, { forceNotification: true })
70
93
  return {
@@ -75,13 +98,13 @@ const REQUESTS = {
75
98
  }
76
99
  } catch (e) {
77
100
  if (instance.debug) console.error(e)
78
- return { error: ERRORS.internalError({ msg: e.message }) }
101
+ return { error: ERRORS.internalError(e.message) }
79
102
  }
80
103
  },
81
104
  (error) => ({ error: ERRORS.rejectedByUser(error) })
82
105
  )
83
106
  },
84
- [METHODS.aepp.address] (callInstance, instance, client) {
107
+ [METHODS.address] (callInstance, instance, client) {
85
108
  // Authorization check
86
109
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
87
110
  if (!client.isSubscribed()) return { error: ERRORS.notAuthorize() }
@@ -89,20 +112,28 @@ const REQUESTS = {
89
112
  return callInstance(
90
113
  'onAskAccounts',
91
114
  {},
92
- ({ accounts } = {}) => ({ result: accounts || [...Object.keys(client.accounts.current), ...Object.keys(client.accounts.connected)] }),
115
+ ({ accounts } = {}) => ({
116
+ result: accounts ||
117
+ [...Object.keys(client.accounts.current), ...Object.keys(client.accounts.connected)]
118
+ }),
93
119
  (error) => ({ error: ERRORS.rejectedByUser(error) })
94
120
  )
95
121
  },
96
- [METHODS.aepp.sign] (callInstance, instance, client, { tx, onAccount, networkId, returnSigned = false }) {
122
+ [METHODS.sign] (callInstance, instance, client, options) {
123
+ const { tx, onAccount, networkId, returnSigned = false } = options
97
124
  const address = onAccount || client.currentAccount
98
125
  // Update client with new networkId
99
126
  networkId && client.updateInfo({ networkId })
100
127
  // Authorization check
101
128
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
102
129
  // Account permission check
103
- if (!client.hasAccessToAccount(address)) return { error: ERRORS.permissionDeny({ account: address }) }
130
+ if (!client.hasAccessToAccount(address)) {
131
+ return { error: ERRORS.permissionDeny(address) }
132
+ }
104
133
  // NetworkId check
105
- if (!networkId || networkId !== instance.getNetworkId()) return { error: ERRORS.unsupportedNetwork() }
134
+ if (!networkId || networkId !== instance.getNetworkId()) {
135
+ return { error: ERRORS.unsupportedNetwork() }
136
+ }
106
137
 
107
138
  return callInstance(
108
139
  'onSign',
@@ -113,14 +144,14 @@ const REQUESTS = {
113
144
  onAcc = resolveOnAccount(instance.addresses(), address, opt)
114
145
  } catch (e) {
115
146
  if (instance.debug) console.error(e)
116
- return { error: ERRORS.internalError({ msg: e.message }) }
147
+ return { error: ERRORS.internalError(e.message) }
117
148
  }
118
149
  try {
119
- return {
120
- result: returnSigned
121
- ? { signedTransaction: await instance.signTransaction(rawTx || tx, { onAccount: onAcc }) }
122
- : { transactionHash: await instance.send(rawTx || tx, { onAccount: onAcc, verify: false }) }
123
- }
150
+ const t = rawTx || tx
151
+ const result = returnSigned
152
+ ? { signedTransaction: await instance.signTransaction(t, { onAccount: onAcc }) }
153
+ : { transactionHash: await instance.send(t, { onAccount: onAcc, verify: false }) }
154
+ return { result }
124
155
  } catch (e) {
125
156
  if (!returnSigned) {
126
157
  // Validate transaction
@@ -135,11 +166,13 @@ const REQUESTS = {
135
166
  (error) => ({ error: ERRORS.rejectedByUser(error) })
136
167
  )
137
168
  },
138
- [METHODS.aepp.signMessage] (callInstance, instance, client, { message, onAccount }) {
169
+ [METHODS.signMessage] (callInstance, instance, client, { message, onAccount }) {
139
170
  // Authorization check
140
171
  if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
141
172
  const address = onAccount || client.currentAccount
142
- if (!client.hasAccessToAccount(address)) return { error: ERRORS.permissionDeny({ account: address }) }
173
+ if (!client.hasAccessToAccount(address)) {
174
+ return { error: ERRORS.permissionDeny(address) }
175
+ }
143
176
 
144
177
  return callInstance(
145
178
  'onMessageSign',
@@ -157,7 +190,7 @@ const REQUESTS = {
157
190
  }
158
191
  } catch (e) {
159
192
  if (instance.debug) console.error(e)
160
- return { error: ERRORS.internalError({ msg: e.message }) }
193
+ return { error: ERRORS.internalError(e.message) }
161
194
  }
162
195
  },
163
196
  (error) => ({ error: ERRORS.rejectedByUser(error) })
@@ -168,7 +201,9 @@ const REQUESTS = {
168
201
  const handleMessage = (instance, id) => async (msg, origin) => {
169
202
  const client = instance.rpcClients[id]
170
203
  if (!msg.id) {
171
- return getHandler(NOTIFICATIONS, msg, { debug: instance.debug })(instance, { client })(msg, origin)
204
+ return getHandler(
205
+ NOTIFICATIONS, msg, { debug: instance.debug }
206
+ )(instance, { client })(msg, origin)
172
207
  }
173
208
  if (Object.prototype.hasOwnProperty.call(client.callbacks, msg.id)) {
174
209
  return getHandler(RESPONSES, msg, { debug: instance.debug })(instance, { client })(msg, origin)
@@ -188,7 +223,9 @@ const handleMessage = (instance, id) => async (msg, origin) => {
188
223
  )
189
224
  })
190
225
  // TODO make one structure for handler functions
191
- const errorObjectOrHandler = getHandler(REQUESTS, msg, { debug: instance.debug })(callInstance, instance, client, msg.params)
226
+ const errorObjectOrHandler = getHandler(REQUESTS, msg, { debug: instance.debug })(
227
+ callInstance, instance, client, msg.params
228
+ )
192
229
  const response = typeof errorObjectOrHandler === 'function' ? await errorObjectOrHandler() : errorObjectOrHandler
193
230
  sendResponseMessage(client)(id, method, response)
194
231
  }
@@ -201,31 +238,31 @@ const handleMessage = (instance, id) => async (msg, origin) => {
201
238
  * @rtype Stamp
202
239
  * @param {Object} param Init params object
203
240
  * @param {String=} [param.name] Wallet name
204
- * @param {Function} onConnection Call-back function for incoming AEPP connection (Second argument contain function for accept/deny request)
205
- * @param {Function} onSubscription Call-back function for incoming AEPP account subscription (Second argument contain function for accept/deny request)
206
- * @param {Function} onSign Call-back function for incoming AEPP sign request (Second argument contain function for accept/deny request)
207
- * @param {Function} onAskAccounts Call-back function for incoming AEPP get address request (Second argument contain function for accept/deny request)
208
- * @param {Function} onMessageSign Call-back function for incoming AEPP sign message request (Second argument contain function for accept/deny request)
241
+ * @param {Function} onConnection Call-back function for incoming AEPP connection
242
+ * @param {Function} onSubscription Call-back function for incoming AEPP account subscription
243
+ * @param {Function} onSign Call-back function for incoming AEPP sign request
244
+ * @param {Function} onAskAccounts Call-back function for incoming AEPP get address request
245
+ * @param {Function} onMessageSign Call-back function for incoming AEPP sign message request
246
+ * Second argument of incoming call-backs contain function for accept/deny request
209
247
  * @param {Function} onDisconnect Call-back function for disconnect event
210
248
  * @return {Object}
211
249
  */
212
250
  export default Ae.compose(AccountMultiple, {
213
- init ({ name, onConnection, onSubscription, onSign, onDisconnect, onAskAccounts, onMessageSign, forceValidation = false, debug = false } = {}) {
251
+ init ({
252
+ name,
253
+ debug = false,
254
+ ...other
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
+
214
264
  this.debug = debug
215
- const eventsHandlers = ['onConnection', 'onSubscription', 'onSign', 'onDisconnect', 'onMessageSign']
216
- // CallBacks for events
217
- this.onConnection = onConnection
218
- this.onSubscription = onSubscription
219
- this.onSign = onSign
220
- this.onDisconnect = onDisconnect
221
- this.onAskAccounts = onAskAccounts
222
- this.onMessageSign = onMessageSign
223
265
  this.rpcClients = {}
224
-
225
- eventsHandlers.forEach(event => {
226
- if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`)
227
- })
228
- //
229
266
  this.name = name
230
267
  this.id = uuid()
231
268
 
@@ -268,9 +305,13 @@ export default Ae.compose(AccountMultiple, {
268
305
  // Send notification 'update.network' to all Aepp which connected
269
306
  Object.values(this.rpcClients)
270
307
  .filter(client => client.isConnected())
271
- .forEach(client => client.sendMessage(
272
- message(METHODS.updateNetwork, { networkId: this.getNetworkId() }), true
273
- ))
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
+ })
274
315
  }
275
316
  },
276
317
  methods: {
@@ -285,7 +326,7 @@ export default Ae.compose(AccountMultiple, {
285
326
  */
286
327
  removeRpcClient (id, { forceConnectionClose = false } = {}) {
287
328
  const client = this.rpcClients[id]
288
- if (!client) throw new Error(`RpcClient with id ${id} do not exist`)
329
+ if (!client) throw new UnknownRpcClientError(id)
289
330
  client.disconnect(forceConnectionClose)
290
331
  delete this.rpcClients[id]
291
332
  },
@@ -298,7 +339,8 @@ export default Ae.compose(AccountMultiple, {
298
339
  * @return {String} Client ID
299
340
  */
300
341
  addRpcClient (clientConnection) {
301
- // @TODO detect if aepp has some history based on origin????: if yes use this instance for connection
342
+ // @TODO detect if aepp has some history based on origin????
343
+ // if yes use this instance for connection
302
344
  const id = uuid()
303
345
  this.rpcClients[id] = RpcClient({
304
346
  id,
@@ -320,7 +362,7 @@ export default Ae.compose(AccountMultiple, {
320
362
  shareWalletInfo (postFn) {
321
363
  postFn({
322
364
  jsonrpc: '2.0',
323
- ...message(METHODS.wallet.readyToConnect, this.getWalletInfo())
365
+ ...message(METHODS.readyToConnect, this.getWalletInfo())
324
366
  })
325
367
  },
326
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
  },
@@ -52,7 +53,10 @@ export default stampit({
52
53
  scan (onDetected) {
53
54
  const { wallets } = this
54
55
  this.connection.connect(({ method, params }, origin, source) => {
55
- if (!method || !params || method !== METHODS.wallet.readyToConnect || wallets[params.id]) return
56
+ if (
57
+ !method || !params ||
58
+ method !== METHODS.readyToConnect || wallets[params.id]
59
+ ) return
56
60
 
57
61
  const wallet = {
58
62
  ...params,
@@ -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
  }