@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
@@ -1,22 +1,19 @@
1
1
  import BigNumber from 'bignumber.js'
2
2
  import { MAX_AUTH_FUN_GAS } from '../../tx/builder/schema'
3
- import { hash } from '../../utils/crypto'
3
+ import { InvalidAuthDataError } from '../../utils/errors'
4
4
 
5
5
  export const prepareGaParams = (ins) => async (authData, authFnName) => {
6
- if (typeof authData !== 'object') throw new Error('AuthData must be an object')
7
- if (authData.gas && BigNumber(authData.gas).gt(MAX_AUTH_FUN_GAS)) throw new Error(`the maximum gas value for ga authFun is ${MAX_AUTH_FUN_GAS}, got ${authData.gas}`)
6
+ if (typeof authData !== 'object') throw new InvalidAuthDataError('AuthData must be an object')
7
+ if (authData.gas && BigNumber(authData.gas).gt(MAX_AUTH_FUN_GAS)) { throw new InvalidAuthDataError(`the maximum gas value for ga authFun is ${MAX_AUTH_FUN_GAS}, got ${authData.gas}`) }
8
8
  const gas = authData.gas || MAX_AUTH_FUN_GAS
9
9
  if (authData.callData) {
10
- if (authData.callData.split('_')[0] !== 'cb') throw new Error('Auth data must be a string with "cb" prefix.')
10
+ if (authData.callData.split('_')[0] !== 'cb') throw new InvalidAuthDataError('Auth data must be a string with "cb" prefix.')
11
11
  return { authCallData: authData.callData, gas }
12
- } else {
13
- if (!authData.source || !authData.args) throw new Error('Auth data must contain source code and arguments.')
14
- return { authCallData: await ins.contractEncodeCall(authData.source, authFnName, authData.args), gas }
15
12
  }
16
- }
17
-
18
- export const getContractAuthFan = (ins) => async (source, fnName) => {
19
- const { bytecode } = await ins.contractCompile(source)
20
-
21
- return { bytecode, authFun: hash(fnName) }
13
+ if (!authData.source || !authData.args) throw new InvalidAuthDataError('Auth data must contain source code and arguments.')
14
+ const contract = await ins.getContractInstance({ source: authData.source })
15
+ return {
16
+ authCallData: contract.calldata.encode(contract._name, authFnName, authData.args),
17
+ gas
18
+ }
22
19
  }
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * ISC License (ISC)
3
- * Copyright (c) 2018 aeternity developers
3
+ * Copyright (c) 2021 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,51 +16,53 @@
16
16
  */
17
17
 
18
18
  /**
19
- * Generalize Account module - routines to use generalize account
19
+ * Generalized Account module - routines to use generalized account
20
20
  *
21
21
  * @module @aeternity/aepp-sdk/es/contract/ga
22
- * @export GeneralizeAccount
23
- * @example import { GeneralizeAccount } from '@aeternity/aepp-sdk'
22
+ * @export GeneralizedAccount
23
+ * @example import { GeneralizedAccount } from '@aeternity/aepp-sdk'
24
24
  */
25
- import * as R from 'ramda'
26
-
27
- import { ContractAPI } from '../../ae/contract'
25
+ import Contract from '../../ae/contract'
28
26
  import { TX_TYPE } from '../../tx/builder/schema'
29
27
  import { buildTx, unpackTx } from '../../tx/builder'
30
- import { getContractAuthFan, prepareGaParams } from './helpers'
28
+ import { prepareGaParams } from './helpers'
29
+ import { hash } from '../../utils/crypto'
30
+ import { IllegalArgumentError, MissingParamError } from '../../utils/errors'
31
31
 
32
32
  /**
33
- * GeneralizeAccount Stamp
33
+ * GeneralizedAccount Stamp
34
34
  *
35
- * Provide Generalize Account implementation
35
+ * Provide Generalized Account implementation
36
36
  * {@link module:@aeternity/aepp-sdk/es/contract/ga} clients.
37
37
  * @function
38
38
  * @alias module:@aeternity/aepp-sdk/es/contract/ga
39
39
  * @rtype Stamp
40
40
  * @param {Object} [options={}] - Initializer object
41
- * @return {Object} GeneralizeAccount instance
41
+ * @return {Object} GeneralizedAccount instance
42
42
  * @example
43
43
  * const authContract = ``
44
- * await client.createGeneralizeAccount(authFnName, authContract, [...authFnArguments]
44
+ * await aeSdk.createGeneralizedAccount(authFnName, authContract, [...authFnArguments]
45
45
  * // Make spend using GA
46
46
  * const callData = 'cb_...' // encoded call data for auth contract
47
- * await client.spend(10000, receiverPub, { authData: { callData } })
47
+ * await aeSdk.spend(10000, receiverPub, { authData: { callData } })
48
48
  * // or
49
- * await client.spend(10000, receiverPub, { authData: { source: authContract, args: [...authContractArgs] } }) // sdk will prepare callData itself
49
+ * await aeSdk.spend(10000, receiverPub, {
50
+ * authData: { source: authContract, args: [...authContractArgs] }
51
+ * }) // sdk will prepare callData itself
50
52
  */
51
- export const GeneralizeAccount = ContractAPI.compose({
53
+ export const GeneralizedAccount = Contract.compose({
52
54
  methods: {
53
- createGeneralizeAccount,
55
+ createGeneralizedAccount,
54
56
  createMetaTx,
55
57
  isGA
56
58
  }
57
59
  })
58
- export default GeneralizeAccount
60
+ export default GeneralizedAccount
59
61
 
60
62
  /**
61
63
  * @alias module:@aeternity/aepp-sdk/es/contract/ga
62
64
  * @function
63
- * Check if account is GA account
65
+ * Check if account is GA
64
66
  * @param {String} address - Account address
65
67
  * @return {Boolean}
66
68
  */
@@ -70,31 +72,38 @@ async function isGA (address) {
70
72
  }
71
73
 
72
74
  /**
73
- * Convert current account to GA account
75
+ * Convert current account to GA
74
76
  * @alias module:@aeternity/aepp-sdk/es/contract/ga
75
77
  * @function
76
78
  * @param {String} authFnName - Authorization function name
77
79
  * @param {String} source - Auth contract source code
78
80
  * @param {Array} [args] - init arguments
79
81
  * @param {Object} [options] - Options
80
- * @return {Promise<Readonly<{result: *, owner: *, createdAt: Date, address, rawTx: *, transaction: *}>>}
82
+ * @return {Promise<Readonly<{
83
+ * result: *, owner: *, address, rawTx: *, transaction: *
84
+ * }>>}
81
85
  */
82
- async function createGeneralizeAccount (authFnName, source, args = [], options = {}) {
83
- const opt = R.merge(this.Ae.defaults, options)
86
+ async function createGeneralizedAccount (authFnName, source, args = [], options = {}) {
87
+ const opt = { ...this.Ae.defaults, ...options }
84
88
  const ownerId = await this.address(opt)
89
+ if (await this.isGA(ownerId)) throw new IllegalArgumentError(`Account ${ownerId} is already GA`)
85
90
 
86
- if (await this.isGA(ownerId)) throw new Error(`Account ${ownerId} is already GA`)
87
-
88
- const { authFun, bytecode } = await getContractAuthFan(this)(source, authFnName)
89
- const callData = await this.contractEncodeCall(source, 'init', args)
90
-
91
- const { tx, contractId } = await this.gaAttachTx(R.merge(opt, { ownerId, code: bytecode, callData, authFun }))
91
+ const contract = await this.getContractInstance({ source })
92
+ await contract.compile()
93
+ const { tx, contractId } = await this.gaAttachTx({
94
+ ...opt,
95
+ gas: opt.gas ?? await contract._estimateGas('init', args, opt),
96
+ ownerId,
97
+ code: contract.bytecode,
98
+ callData: contract.calldata.encode(contract._name, 'init', args),
99
+ authFun: hash(authFnName)
100
+ })
92
101
 
93
- const { hash, rawTx } = await this.send(tx, opt)
102
+ const { hash: transaction, rawTx } = await this.send(tx, opt)
94
103
 
95
104
  return Object.freeze({
96
105
  owner: ownerId,
97
- transaction: hash,
106
+ transaction,
98
107
  rawTx,
99
108
  gaContractId: contractId
100
109
  })
@@ -113,10 +122,10 @@ const wrapInEmptySignedTx = (tx) => buildTx({ encodedTx: tx, signatures: [] }, T
113
122
  * @return {String}
114
123
  */
115
124
  async function createMetaTx (rawTransaction, authData, authFnName, options = {}) {
116
- if (!authData) throw new Error('authData is required')
125
+ if (!authData) throw new MissingParamError('authData is required')
117
126
  // Check if authData is callData or if it's an object prepare a callData from source and args
118
127
  const { authCallData, gas } = await prepareGaParams(this)(authData, authFnName)
119
- const opt = R.merge(this.Ae.defaults, options)
128
+ const opt = { ...this.Ae.defaults, ...options }
120
129
  const { abiVersion } = await this.getVmVersion(TX_TYPE.contractCall)
121
130
  const wrappedTx = wrapInEmptySignedTx(unpackTx(rawTransaction))
122
131
  const params = {
@@ -126,7 +135,7 @@ async function createMetaTx (rawTransaction, authData, authFnName, options = {})
126
135
  tx: wrappedTx.txObject
127
136
  },
128
137
  gaId: await this.address(opt),
129
- abiVersion: abiVersion,
138
+ abiVersion,
130
139
  authData: authCallData,
131
140
  gas,
132
141
  vsn: 2
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * ISC License (ISC)
3
- * Copyright (c) 2018 aeternity developers
3
+ * Copyright (c) 2021 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
@@ -21,10 +21,8 @@ import * as Bytes from './utils/bytes'
21
21
  import * as TxBuilder from './tx/builder'
22
22
  import * as TxBuilderHelper from './tx/builder/helpers'
23
23
  import * as SCHEMA from './tx/builder/schema'
24
- import * as ACIHelpers from './contract/aci/helpers'
25
- import * as ACITransformation from './contract/aci/transformation'
26
24
  import * as AmountFormatter from './utils/amount-formatter'
27
- import HdWallet from './utils/hd-wallet'
25
+ import * as HdWallet from './utils/hd-wallet'
28
26
 
29
27
  import Ae from './ae'
30
28
  import Chain from './chain'
@@ -39,8 +37,8 @@ import AccountMultiple from './account/multiple'
39
37
  import MemoryAccount from './account/memory'
40
38
  import Aens from './ae/aens'
41
39
  import Contract from './ae/contract'
42
- import GeneralizeAccount from './contract/ga'
43
- import ContractCompilerAPI from './contract/compiler'
40
+ import GeneralizedAccount from './contract/ga'
41
+ import ContractCompilerHttp from './contract/compiler'
44
42
  import RpcAepp from './ae/aepp'
45
43
  import RpcWallet from './ae/wallet'
46
44
  import Oracle from './ae/oracle'
@@ -54,6 +52,9 @@ export * as AeppWalletSchema from './utils/aepp-wallet-communication/schema'
54
52
  export { default as WalletDetector } from './utils/aepp-wallet-communication/wallet-detector'
55
53
  export { default as BrowserRuntimeConnection } from './utils/aepp-wallet-communication/connection/browser-runtime'
56
54
  export { default as BrowserWindowMessageConnection } from './utils/aepp-wallet-communication/connection/browser-window-message'
55
+ export * from './utils/errors'
56
+
57
+ export const getDefaultPointerKey = TxBuilderHelper.getDefaultPointerKey
57
58
 
58
59
  export {
59
60
  AmountFormatter,
@@ -63,9 +64,7 @@ export {
63
64
  Aens,
64
65
  Bytes,
65
66
  Contract,
66
- ContractCompilerAPI,
67
- ACIHelpers,
68
- ACITransformation,
67
+ ContractCompilerHttp,
69
68
  ChainNode,
70
69
  RpcAepp,
71
70
  RpcWallet,
@@ -73,7 +72,7 @@ export {
73
72
  Crypto,
74
73
  Keystore,
75
74
  Chain,
76
- GeneralizeAccount,
75
+ GeneralizedAccount,
77
76
  HdWallet,
78
77
  MemoryAccount,
79
78
  Node,
@@ -5,8 +5,8 @@
5
5
  * @example import { NodePool } from '@aeternity/aepp-sdk'
6
6
  */
7
7
  import stampit from '@stamp/it'
8
- import Joi from 'joi-browser'
9
8
  import { getNetworkId } from '../node'
9
+ import { DisconnectedError, DuplicateNodeError, NodeNotFoundError, TypeError } from '../utils/errors'
10
10
 
11
11
  /**
12
12
  * Node Pool Stamp
@@ -35,7 +35,7 @@ export default stampit({
35
35
  configurable: false,
36
36
  get () {
37
37
  if (!this.selectedNode || !this.selectedNode.instance) {
38
- throw new Error('You can\'t use Node API. Node is not connected or not defined!')
38
+ throw new NodeNotFoundError('You can\'t use Node API. Node is not connected or not defined!')
39
39
  }
40
40
  return this.selectedNode.instance.api
41
41
  }
@@ -52,10 +52,11 @@ export default stampit({
52
52
  * @param {Boolean} select - Select this node as current
53
53
  * @return {void}
54
54
  * @example
55
- * nodePool.addNode('testNode', awaitNode({ url, internalUrl }), true) // add and select new node with name 'testNode'
55
+ * // add and select new node with name 'testNode'
56
+ * nodePool.addNode('testNode', awaitNode({ url, internalUrl }), true)
56
57
  */
57
58
  addNode (name, node, select = false) {
58
- if (this.pool.has(name)) throw new Error(`Node with name ${name} already exist`)
59
+ if (this.pool.has(name)) throw new DuplicateNodeError(name)
59
60
 
60
61
  this.validateNodes([{ name, instance: node }])
61
62
 
@@ -83,7 +84,7 @@ export default stampit({
83
84
  * nodePool.selectNode('testNode')
84
85
  */
85
86
  selectNode (name) {
86
- if (!this.pool.has(name)) throw new Error(`Node with name ${name} not in pool`)
87
+ if (!this.pool.has(name)) throw new NodeNotFoundError(`Node with name ${name} not in pool`)
87
88
 
88
89
  this.selectedNode = this.pool.get(name)
89
90
  },
@@ -119,7 +120,7 @@ export default stampit({
119
120
  * nodePool.getNodeInfo() // { name, version, networkId, protocol, ... }
120
121
  */
121
122
  getNodeInfo () {
122
- if (!this.isNodeConnected()) throw new Error('Can not get node info. Node is not connected')
123
+ if (!this.isNodeConnected()) throw new DisconnectedError()
123
124
  return {
124
125
  name: this.selectedNode.name,
125
126
  ...this.selectedNode.instance.getNodeInfo()
@@ -141,17 +142,28 @@ export default stampit({
141
142
  }))
142
143
  },
143
144
  validateNodes (nodes) {
144
- const { error } = Joi.array().items(
145
- Joi.object({
146
- name: Joi.string().required(),
147
- instance: Joi.object({
148
- api: Joi.object().required(),
149
- consensusProtocolVersion: Joi.number().required(),
150
- genesisHash: Joi.string().required()
151
- }).unknown()
152
- })
153
- ).label('node').validate(nodes)
154
- if (error) throw error
145
+ // TODO: validate it on TypeScript level instead
146
+ // (to speedup development, save runtime resources)
147
+ if (!Array.isArray(nodes)) throw new TypeError('"nodes" should be an array')
148
+ const notObject = nodes.map(n => typeof n).find(t => t !== 'object')
149
+ if (notObject) throw new TypeError(`Each node should be an object, got ${notObject} instead`)
150
+ const wrongFields = nodes.find(n => typeof n.name !== 'string' || typeof n.instance !== 'object')
151
+ if (notObject) {
152
+ throw new TypeError(
153
+ 'Each node should have name (string), instance (object) ' +
154
+ `fields, got ${JSON.stringify(wrongFields)} instead`
155
+ )
156
+ }
157
+ const wrongInstanceFields = nodes
158
+ .map(n => n.instance)
159
+ .find(i => typeof i.api !== 'object' || typeof i.genesisHash !== 'string' ||
160
+ typeof i.consensusProtocolVersion !== 'number')
161
+ if (wrongInstanceFields) {
162
+ throw new TypeError(
163
+ 'Each node instance should have api (object), consensusProtocolVersion (number), ' +
164
+ `genesisHash (string) fields, got ${JSON.stringify(wrongInstanceFields)} instead`
165
+ )
166
+ }
155
167
  }
156
168
  },
157
169
  props: {
package/src/node.js CHANGED
@@ -25,6 +25,7 @@
25
25
  import AsyncInit from './utils/async-init'
26
26
  import genSwaggerClient from './utils/swagger'
27
27
  import semverSatisfies from './utils/semver-satisfies'
28
+ import { MissingParamError, UnsupportedVersionError } from './utils/errors'
28
29
 
29
30
  /**
30
31
  * Obtain networkId from account or node
@@ -34,29 +35,10 @@ import semverSatisfies from './utils/semver-satisfies'
34
35
  * @return {String} NetworkId
35
36
  */
36
37
  export function getNetworkId ({ networkId, force = false } = {}) {
37
- if (!force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) throw new Error('networkId is not provided')
38
- if (force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) return null
39
- return networkId || this.networkId || this.selectedNode.networkId
40
- }
41
-
42
- /**
43
- * get consensus protocol version
44
- * @param {Array} protocols Array of protocols
45
- * @param {Number} height Height
46
- * @return {Number} version Protocol version
47
- */
48
- async function getConsensusProtocolVersion (protocols = [], height) {
49
- if (!protocols) throw new Error('Protocols must be an array')
50
- if (!height) height = (await this.api.getCurrentKeyBlock()).height
51
- if (height < 0) throw new Error('height must be a number >= 0')
52
-
53
- return protocols
54
- .filter(({ effectiveAtHeight }) => height >= effectiveAtHeight)
55
- .reduce(
56
- (acc, p) => p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc,
57
- { effectiveAtHeight: -1, version: 0 }
58
- )
59
- .version
38
+ const res = networkId || this.networkId || this.selectedNode?.networkId
39
+ if (!force && !res) throw new MissingParamError('networkId is not provided')
40
+ if (force && !res) return null
41
+ return res
60
42
  }
61
43
 
62
44
  /**
@@ -67,30 +49,46 @@ async function getConsensusProtocolVersion (protocols = [], height) {
67
49
  * @param {Object} [options={}] - Options
68
50
  * @param {String} options.url - Base URL for Node
69
51
  * @param {String} options.internalUrl - Base URL for internal requests
70
- * @param {String} options.axiosConfig - Object with axios configuration. Example { config: {}, errorHandler: (err) => throw err }
52
+ * @param {Object} options.axiosConfig - Object with axios configuration
53
+ * @param {Object} options.axiosConfig.config
54
+ * @param {Function} options.axiosConfig.errorHandler - (err) => throw err
71
55
  * @return {Object} Node client
72
56
  * @example Node({url: 'https://testnet.aeternity.io'})
73
57
  */
74
58
  const Node = AsyncInit.compose({
75
59
  async init ({ url, internalUrl, ignoreVersion }) {
76
- if (!url) throw new Error('"url" required')
60
+ if (!url) throw new MissingParamError('"url" required')
77
61
  this.url = url.replace(/\/$/, '')
78
- this.internalUrl = internalUrl ? internalUrl.replace(/\/$/, '') : this.url
79
- const client = await genSwaggerClient(`${this.url}/api?oas3`, { internalUrl: this.internalUrl })
62
+ this.internalUrl = internalUrl?.replace(/\/$/, '')
63
+ const client = await genSwaggerClient(`${this.url}/api?oas3`, {
64
+ internalUrl: this.internalUrl,
65
+ responseInterceptor: response => {
66
+ if (response.ok) return
67
+ return Object.assign(response, {
68
+ statusText: `${new URL(response.url).pathname.slice(1)} error: ` + response.body.reason
69
+ })
70
+ }
71
+ })
80
72
  this.version = client.spec.info.version
81
73
  if (
82
74
  !semverSatisfies(this.version, NODE_GE_VERSION, NODE_LT_VERSION) &&
83
75
  !ignoreVersion
84
76
  ) {
85
- throw new Error(
86
- `Unsupported node version ${this.version}. ` +
87
- `Supported: >= ${NODE_GE_VERSION} < ${NODE_LT_VERSION}`
88
- )
77
+ throw new UnsupportedVersionError('node', this.version, NODE_GE_VERSION, NODE_LT_VERSION)
89
78
  }
90
79
  this.api = client.api
91
80
 
92
- const { nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId, protocols } = await this.api.getStatus()
93
- this.consensusProtocolVersion = await this.getConsensusProtocolVersion(protocols)
81
+ const {
82
+ nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId,
83
+ protocols, topBlockHeight
84
+ } = await this.api.getStatus()
85
+ this.consensusProtocolVersion = protocols
86
+ .filter(({ effectiveAtHeight }) => topBlockHeight >= effectiveAtHeight)
87
+ .reduce(
88
+ (acc, p) => p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc,
89
+ { effectiveAtHeight: -1, version: 0 }
90
+ )
91
+ .version
94
92
  this.nodeNetworkId = networkId
95
93
  return Object.assign(this, { revision, genesisHash })
96
94
  },
@@ -103,8 +101,7 @@ const Node = AsyncInit.compose({
103
101
  version: this.version,
104
102
  consensusProtocolVersion: this.consensusProtocolVersion
105
103
  }
106
- },
107
- getConsensusProtocolVersion
104
+ }
108
105
  },
109
106
  props: {
110
107
  version: null,
@@ -113,7 +110,7 @@ const Node = AsyncInit.compose({
113
110
  }
114
111
  })
115
112
 
116
- const NODE_GE_VERSION = '6.0.0'
113
+ const NODE_GE_VERSION = '6.2.0'
117
114
  const NODE_LT_VERSION = '7.0.0'
118
115
 
119
116
  export default Node
@@ -0,0 +1,61 @@
1
+ import {
2
+ writeId, readId, isNameValid, produceNameId, ensureNameValid, getMinimumNameFee, readInt, writeInt
3
+ } from './helpers'
4
+ import { InsufficientNameFeeError, IllegalArgumentError } from '../../utils/errors'
5
+
6
+ export class Field {
7
+ static serialize (value) {
8
+ return value
9
+ }
10
+
11
+ static deserialize (value) {
12
+ return value
13
+ }
14
+ }
15
+
16
+ export class Name extends Field {
17
+ static serialize (value) {
18
+ ensureNameValid(value)
19
+ return Buffer.from(value)
20
+ }
21
+
22
+ static deserialize (value) {
23
+ return value.toString()
24
+ }
25
+ }
26
+
27
+ export class NameId extends Field {
28
+ static serialize (value) {
29
+ return writeId(isNameValid(value) ? produceNameId(value) : value)
30
+ }
31
+
32
+ static deserialize (value) {
33
+ return readId(value)
34
+ }
35
+ }
36
+
37
+ export class NameFee extends Field {
38
+ static serialize (value, { name }) {
39
+ const minNameFee = getMinimumNameFee(name)
40
+ value ??= minNameFee
41
+ if (minNameFee.gt(value)) {
42
+ throw new InsufficientNameFeeError(value, minNameFee)
43
+ }
44
+ return writeInt(value)
45
+ }
46
+
47
+ static deserialize (value) {
48
+ return readInt(value)
49
+ }
50
+ }
51
+
52
+ export class Deposit extends Field {
53
+ static serialize (value, { name }) {
54
+ if (+value) throw new IllegalArgumentError(`Contract deposit is not refundable, so it should be equal 0, got ${value} instead`)
55
+ return writeInt(0)
56
+ }
57
+
58
+ static deserialize (value) {
59
+ return readInt(value)
60
+ }
61
+ }