@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
@@ -1,26 +1,30 @@
1
- import * as R from 'ramda'
2
1
  import BigNumber from 'bignumber.js'
3
-
4
- import {
5
- decodeBase58Check,
6
- decodeBase64Check,
7
- encodeBase58Check, encodeBase64Check,
8
- hash,
9
- salt
10
- } from '../../utils/crypto'
2
+ import bs58 from 'bs58'
3
+ import { hash, salt, sha256hash } from '../../utils/crypto'
11
4
  import { toBytes } from '../../utils/bytes'
12
5
  import {
13
6
  ID_TAG_PREFIX,
14
7
  PREFIX_ID_TAG,
15
8
  NAME_BID_RANGES,
16
- NAME_BID_MAX_LENGTH,
17
- NAME_FEE,
18
9
  NAME_FEE_BID_INCREMENT,
19
10
  NAME_BID_TIMEOUTS,
20
11
  NAME_MAX_LENGTH_FEE,
21
12
  POINTER_KEY_BY_PREFIX
22
13
  } from './schema'
23
14
  import { ceil } from '../../utils/bignumber'
15
+ import {
16
+ PrefixMismatchError,
17
+ DecodeError,
18
+ EncodeError,
19
+ PayloadLengthError,
20
+ TagNotFoundError,
21
+ PrefixNotFoundError,
22
+ InvalidNameError,
23
+ IllegalBidFeeError,
24
+ NoDefaultAensPointerError,
25
+ ArgumentError,
26
+ InvalidChecksumError
27
+ } from '../../utils/errors'
24
28
 
25
29
  /**
26
30
  * JavaScript-based Transaction builder helper function's
@@ -31,8 +35,6 @@ import { ceil } from '../../utils/bignumber'
31
35
 
32
36
  export const createSalt = salt
33
37
 
34
- const base64Types = ['tx', 'st', 'ss', 'pi', 'ov', 'or', 'cb', 'cs', 'ba']
35
-
36
38
  /**
37
39
  * Build a contract public key
38
40
  * @function
@@ -78,11 +80,11 @@ export function formatSalt (salt) {
78
80
  }
79
81
 
80
82
  /**
81
- * Encode a domain name
83
+ * Encode an AENS name
82
84
  * @function
83
85
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
84
86
  * @param {String} name Name to encode
85
- * @return {String} `nm_` prefixed encoded domain name
87
+ * @return {String} `nm_` prefixed encoded AENS name
86
88
  */
87
89
  export function produceNameId (name) {
88
90
  ensureNameValid(name)
@@ -103,7 +105,46 @@ export function produceNameId (name) {
103
105
  */
104
106
  export function commitmentHash (name, salt = createSalt()) {
105
107
  ensureNameValid(name)
106
- return `cm_${encodeBase58Check(hash(Buffer.concat([Buffer.from(name.toLowerCase()), formatSalt(salt)])))}`
108
+ return encode(hash(Buffer.concat([Buffer.from(name.toLowerCase()), formatSalt(salt)])), 'cm')
109
+ }
110
+
111
+ // based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
112
+ const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx']
113
+ const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']
114
+ // TODO: add all types with a fixed length
115
+ const typesLength = {
116
+ ak: 32,
117
+ ct: 32,
118
+ ok: 32
119
+ }
120
+
121
+ function ensureValidLength (data, type) {
122
+ if (!typesLength[type]) return
123
+ if (data.length === typesLength[type]) return
124
+ throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`)
125
+ }
126
+
127
+ const getChecksum = payload => sha256hash(sha256hash(payload)).slice(0, 4)
128
+
129
+ const addChecksum = (input) => {
130
+ const payload = Buffer.from(input)
131
+ return Buffer.concat([payload, getChecksum(payload)])
132
+ }
133
+
134
+ function getPayload (buffer) {
135
+ const payload = buffer.slice(0, -4)
136
+ if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError()
137
+ return payload
138
+ }
139
+
140
+ const base64 = {
141
+ encode: buffer => addChecksum(buffer).toString('base64'),
142
+ decode: string => getPayload(Buffer.from(string, 'base64'))
143
+ }
144
+
145
+ const base58 = {
146
+ encode: buffer => bs58.encode(addChecksum(buffer)),
147
+ decode: string => getPayload(bs58.decode(string))
107
148
  }
108
149
 
109
150
  /**
@@ -115,13 +156,21 @@ export function commitmentHash (name, salt = createSalt()) {
115
156
  * @return {Buffer} Decoded data
116
157
  */
117
158
  export function decode (data, requiredPrefix) {
118
- const [prefix, payload, extra] = data.split('_')
119
- if (!payload) throw new Error(`Encoded string missing payload: ${data}`)
120
- if (extra) throw new Error(`Encoded string have extra parts: ${data}`)
159
+ if (typeof data !== 'string') throw new DecodeError(`Encoded should be a string, got ${data} instead`)
160
+ const [prefix, encodedPayload, extra] = data.split('_')
161
+ if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`)
162
+ if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`)
121
163
  if (requiredPrefix && requiredPrefix !== prefix) {
122
- throw new Error(`Encoded string have a wrong type: ${prefix} (expected: ${requiredPrefix})`)
164
+ throw new PrefixMismatchError(prefix, requiredPrefix)
165
+ }
166
+ const decoder = (base64Types.includes(prefix) && base64.decode) ||
167
+ (base58Types.includes(prefix) && base58.decode)
168
+ if (!decoder) {
169
+ throw new DecodeError(`Encoded string have unknown type: ${prefix}`)
123
170
  }
124
- return (base64Types.includes(prefix) ? decodeBase64Check : decodeBase58Check)(payload)
171
+ const payload = decoder(encodedPayload)
172
+ ensureValidLength(payload, prefix)
173
+ return payload
125
174
  }
126
175
 
127
176
  /**
@@ -133,9 +182,13 @@ export function decode (data, requiredPrefix) {
133
182
  * @return {String} Encoded string Base58check or Base64check data
134
183
  */
135
184
  export function encode (data, type) {
136
- return `${type}_${base64Types.includes(type)
137
- ? encodeBase64Check(data)
138
- : encodeBase58Check(data)}`
185
+ const encoder = (base64Types.includes(type) && base64.encode) ||
186
+ (base58Types.includes(type) && base58.encode)
187
+ if (!encoder) {
188
+ throw new EncodeError(`Unknown type: ${type}`)
189
+ }
190
+ ensureValidLength(data, type)
191
+ return `${type}_${encoder(data)}`
139
192
  }
140
193
 
141
194
  /**
@@ -146,12 +199,10 @@ export function encode (data, type) {
146
199
  * @return {Buffer} Buffer Buffer with ID tag and decoded HASh
147
200
  */
148
201
  export function writeId (hashId) {
149
- if (typeof hashId !== 'string') {
150
- throw new Error(`Address should be a string, got ${hashId} instead`)
151
- }
202
+ if (typeof hashId !== 'string') throw new ArgumentError('hashId', 'a string', hashId)
152
203
  const prefix = hashId.slice(0, 2)
153
204
  const idTag = PREFIX_ID_TAG[prefix]
154
- if (!idTag) throw new Error(`Id tag for prefix ${prefix} not found.`)
205
+ if (!idTag) throw new TagNotFoundError(prefix)
155
206
  return Buffer.from([...toBytes(idTag), ...decode(hashId, prefix)])
156
207
  }
157
208
 
@@ -163,9 +214,9 @@ export function writeId (hashId) {
163
214
  * @return {String} Encoided hash string with prefix
164
215
  */
165
216
  export function readId (buf) {
166
- const tag = buf.readUIntBE(0, 1)
217
+ const tag = Buffer.from(buf).readUIntBE(0, 1)
167
218
  const prefix = ID_TAG_PREFIX[tag]
168
- if (!prefix) throw new Error(`Prefix for id-tag ${tag} not found.`)
219
+ if (!prefix) throw new PrefixNotFoundError(tag)
169
220
  return encode(buf.slice(1, buf.length), prefix)
170
221
  }
171
222
 
@@ -188,7 +239,7 @@ export function writeInt (val) {
188
239
  * @return {String} Buffer Buffer from number(BigEndian)
189
240
  */
190
241
  export function readInt (buf = Buffer.from([])) {
191
- return BigNumber(buf.toString('hex'), 16).toString(10)
242
+ return new BigNumber(Buffer.from(buf).toString('hex'), 16).toString(10)
192
243
  }
193
244
 
194
245
  /**
@@ -225,8 +276,10 @@ export function readPointers (pointers) {
225
276
  )
226
277
  }
227
278
 
279
+ const AENS_SUFFIX = '.chain'
280
+
228
281
  /**
229
- * Ensure that name is valid
282
+ * Ensure that AENS name is valid
230
283
  * @function
231
284
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
232
285
  * @param {string} name
@@ -234,12 +287,12 @@ export function readPointers (pointers) {
234
287
  * @throws Error
235
288
  */
236
289
  export function ensureNameValid (name) {
237
- if (!name || typeof name !== 'string') throw new Error('Name must be a string')
238
- if (!name.endsWith('.chain')) throw new Error(`Name should end with .chain: ${name}`)
290
+ if (!name || typeof name !== 'string') throw new InvalidNameError('Name must be a string')
291
+ if (!name.endsWith(AENS_SUFFIX)) throw new InvalidNameError(`Name should end with ${AENS_SUFFIX}: ${name}`)
239
292
  }
240
293
 
241
294
  /**
242
- * Is name valid
295
+ * Is AENS name valid
243
296
  * @function
244
297
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
245
298
  * @param {string} name
@@ -263,36 +316,36 @@ export function getDefaultPointerKey (identifier) {
263
316
  decode(identifier)
264
317
  const prefix = identifier.substr(0, 2)
265
318
  return POINTER_KEY_BY_PREFIX[prefix] ||
266
- (() => { throw new Error(`Default AENS pointer key is not defined for ${prefix} prefix`) })()
319
+ (() => { throw new NoDefaultAensPointerError(prefix) })()
267
320
  }
268
321
 
269
322
  /**
270
- * Get the minimum name fee for a domain
323
+ * Get the minimum AENS name fee
271
324
  * @function
272
325
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
273
- * @param {String} domain the domain name to get the fee for
274
- * @return {String} the minimum fee for the domain auction
326
+ * @param {String} name the AENS name to get the fee for
327
+ * @return {String} the minimum fee for the AENS name auction
275
328
  */
276
- export function getMinimumNameFee (domain) {
277
- const nameLength = domain.replace('.chain', '').length
278
- return NAME_BID_RANGES[nameLength >= NAME_MAX_LENGTH_FEE ? NAME_MAX_LENGTH_FEE : nameLength]
329
+ export function getMinimumNameFee (name) {
330
+ ensureNameValid(name)
331
+ const nameLength = name.length - AENS_SUFFIX.length
332
+ return NAME_BID_RANGES[Math.min(nameLength, NAME_MAX_LENGTH_FEE)]
279
333
  }
280
334
 
281
335
  /**
282
336
  * Compute bid fee for AENS auction
283
337
  * @function
284
338
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
285
- * @param {String} domain the domain name to get the fee for
339
+ * @param {String} name the AENS name to get the fee for
286
340
  * @param {Number|String} startFee Auction start fee
287
341
  * @param {Number} [increment=0.5] Bid multiplier(In percentage, must be between 0 and 1)
288
342
  * @return {String} Bid fee
289
343
  */
290
- export function computeBidFee (domain, startFee = NAME_FEE, increment = NAME_FEE_BID_INCREMENT) {
291
- if (!(Number(increment) === increment && increment % 1 !== 0)) throw new Error(`Increment must be float. Current increment ${increment}`)
292
- if (increment < NAME_FEE_BID_INCREMENT) throw new Error(`minimum increment percentage is ${NAME_FEE_BID_INCREMENT}`)
344
+ export function computeBidFee (name, startFee, increment = NAME_FEE_BID_INCREMENT) {
345
+ if (!(Number(increment) === increment && increment % 1 !== 0)) throw new IllegalBidFeeError(`Increment must be float. Current increment ${increment}`)
346
+ if (increment < NAME_FEE_BID_INCREMENT) throw new IllegalBidFeeError(`minimum increment percentage is ${NAME_FEE_BID_INCREMENT}`)
293
347
  return ceil(
294
- BigNumber(BigNumber(startFee).eq(NAME_FEE) ? getMinimumNameFee(domain) : startFee)
295
- .times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1))
348
+ BigNumber(startFee ?? getMinimumNameFee(name)).times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1))
296
349
  )
297
350
  }
298
351
 
@@ -300,26 +353,28 @@ export function computeBidFee (domain, startFee = NAME_FEE, increment = NAME_FEE
300
353
  * Compute auction end height
301
354
  * @function
302
355
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
303
- * @param {String} domain the domain name to get the fee for
356
+ * @param {String} name
304
357
  * @param {Number|String} claimHeight Auction starting height
305
358
  * @return {String} Auction end height
306
359
  */
307
- export function computeAuctionEndBlock (domain, claimHeight) {
308
- return R.cond([
309
- [R.lt(5), R.always(NAME_BID_TIMEOUTS[4].plus(claimHeight))],
310
- [R.lt(9), R.always(NAME_BID_TIMEOUTS[8].plus(claimHeight))],
311
- [R.lte(NAME_BID_MAX_LENGTH), R.always(NAME_BID_TIMEOUTS[12].plus(claimHeight))],
312
- [R.T, R.always(BigNumber(claimHeight))]
313
- ])(domain.replace('.chain', '').length).toString(10)
360
+ export function computeAuctionEndBlock (name, claimHeight) {
361
+ ensureNameValid(name)
362
+ const length = name.length - AENS_SUFFIX.length
363
+ const h = (length <= 4 && NAME_BID_TIMEOUTS[4]) ||
364
+ (length <= 8 && NAME_BID_TIMEOUTS[8]) ||
365
+ (length <= 12 && NAME_BID_TIMEOUTS[12]) ||
366
+ NAME_BID_TIMEOUTS[13]
367
+ return h.plus(claimHeight).toString(10)
314
368
  }
315
369
 
316
370
  /**
317
371
  * Is name accept going to auction
318
372
  * @function
319
373
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
320
- * @param {String} name Transaction abiVersion
374
+ * @param {String} name
321
375
  * @return {Boolean}
322
376
  */
323
377
  export function isAuctionName (name) {
324
- return name.replace('.chain', '').length < 13
378
+ ensureNameValid(name)
379
+ return name.length < 13 + AENS_SUFFIX.length
325
380
  }
@@ -2,18 +2,17 @@ import BigNumber from 'bignumber.js'
2
2
  import { decode as rlpDecode, encode as rlpEncode } from 'rlp'
3
3
  import { AE_AMOUNT_FORMATS, formatAmount } from '../../utils/amount-formatter'
4
4
  import { hash } from '../../utils/crypto'
5
+ import { Field } from './field-types'
5
6
 
6
7
  import {
7
8
  DEFAULT_FEE,
8
9
  FIELD_TYPES,
9
10
  OBJECT_ID_TX_TYPE,
10
- PREFIX_ID_TAG,
11
11
  TX_DESERIALIZATION_SCHEMA,
12
12
  TX_FEE_BASE_GAS,
13
13
  TX_FEE_OTHER_GAS,
14
14
  TX_SERIALIZATION_SCHEMA,
15
15
  TX_TYPE,
16
- VALIDATION_MESSAGE,
17
16
  VSN
18
17
  } from './schema'
19
18
  import {
@@ -28,6 +27,7 @@ import {
28
27
  } from './helpers'
29
28
  import { toBytes } from '../../utils/bytes'
30
29
  import MPTree from '../../utils/mptree'
30
+ import { InvalidTxParamsError, SchemaNotFoundError } from '../../utils/errors'
31
31
 
32
32
  /**
33
33
  * JavaScript-based Transaction builder
@@ -50,7 +50,6 @@ function deserializeField (value, type, prefix) {
50
50
  return { vmVersion: readInt(Buffer.from([vm])), abiVersion: readInt(Buffer.from([abi])) }
51
51
  }
52
52
  case FIELD_TYPES.amount:
53
- return readInt(value)
54
53
  case FIELD_TYPES.int:
55
54
  return readInt(value)
56
55
  case FIELD_TYPES.id:
@@ -99,11 +98,12 @@ function deserializeField (value, type, prefix) {
99
98
  {}
100
99
  )
101
100
  default:
101
+ if (type.prototype instanceof Field) return type.deserialize(value)
102
102
  return value
103
103
  }
104
104
  }
105
105
 
106
- function serializeField (value, type, prefix) {
106
+ function serializeField (value, type, prefix, params) {
107
107
  switch (type) {
108
108
  case FIELD_TYPES.amount:
109
109
  case FIELD_TYPES.int:
@@ -144,40 +144,45 @@ function serializeField (value, type, prefix) {
144
144
  default: return value
145
145
  }
146
146
  default:
147
+ if (type.prototype instanceof Field) return type.serialize(value, params)
147
148
  return value
148
149
  }
149
150
  }
150
151
 
151
- function validateField (value, key, type, prefix) {
152
- const assert = (valid, params) => valid ? {} : { [key]: VALIDATION_MESSAGE[type](params) }
152
+ function validateField (value, type, prefix) {
153
153
  // All fields are required
154
- if (value === undefined || value === null) return { [key]: 'Field is required' }
154
+ if (value === undefined || value === null) return 'Field is required'
155
155
 
156
156
  // Validate type of value
157
157
  switch (type) {
158
158
  case FIELD_TYPES.amount:
159
159
  case FIELD_TYPES.int: {
160
- const isMinusValue = (!isNaN(value) || BigNumber.isBigNumber(value)) && BigNumber(value).lt(0)
161
- return assert(
162
- (!isNaN(value) || BigNumber.isBigNumber(value)) && BigNumber(value).gte(0),
163
- { value, isMinusValue }
164
- )
160
+ if (isNaN(value) && !BigNumber.isBigNumber(value)) {
161
+ return `${value} is not of type Number or BigNumber`
162
+ }
163
+ if (new BigNumber(value).lt(0)) return `${value} must be >= 0`
164
+ return
165
165
  }
166
166
  case FIELD_TYPES.id: {
167
167
  const prefixes = Array.isArray(prefix) ? prefix : [prefix]
168
- const p = prefixes.find(p => p === value.split('_')[0])
169
- return assert(p && PREFIX_ID_TAG[value.split('_')[0]], { value, prefix })
168
+ if (!prefixes.includes(value.split('_')[0])) {
169
+ return `'${value}' prefix doesn't match expected prefix '${prefix}'`
170
+ }
171
+ return
170
172
  }
171
- case FIELD_TYPES.binary:
172
- return assert(value.split('_')[0] === prefix, { prefix, value })
173
- case FIELD_TYPES.string:
174
- return assert(true)
175
173
  case FIELD_TYPES.ctVersion:
176
- return assert(typeof value === 'object' && Object.prototype.hasOwnProperty.call(value, 'abiVersion') && Object.prototype.hasOwnProperty.call(value, 'vmVersion'))
174
+ if (!value?.abiVersion || !value?.vmVersion) {
175
+ return 'Value must be an object with "vmVersion" and "abiVersion" fields'
176
+ }
177
+ return
177
178
  case FIELD_TYPES.pointers:
178
- return assert(Array.isArray(value) && !value.find(e => e !== Object(e)), { value })
179
- default:
180
- return {}
179
+ if (!Array.isArray(value)) return 'Value must be of type Array'
180
+ if (value.some(p => !p?.key || !p?.id)) {
181
+ return 'Value must contains only object\'s like \'{key: "account_pubkey", id: "ak_lkamsflkalsdalksdlasdlasdlamd"}\''
182
+ }
183
+ if (value.length > 32) {
184
+ return `Expected 32 pointers or less, got ${value.length} instead`
185
+ }
181
186
  }
182
187
  }
183
188
 
@@ -297,14 +302,14 @@ export function calculateFee (fee = 0, txType, { gas = 0, params, showWarning =
297
302
  * @return {Object} Object with validation errors
298
303
  */
299
304
  export function validateParams (params, schema, { excludeKeys = [] }) {
300
- return schema
301
- .filter(([key]) => !excludeKeys.includes(key) && key !== 'payload')
302
- .reduce(
303
- (acc, [key, type, prefix]) => Object.assign(
304
- acc, validateField(params[key], key, type, prefix)
305
- ),
306
- {}
307
- )
305
+ return Object.fromEntries(
306
+ schema
307
+ // TODO: allow optional keys in schema
308
+ .filter(([key]) => !excludeKeys.includes(key) &&
309
+ !['payload', 'nameFee', 'deposit'].includes(key))
310
+ .map(([key, type, prefix]) => [key, validateField(params[key], type, prefix)])
311
+ .filter(([, message]) => message)
312
+ )
308
313
  }
309
314
 
310
315
  /**
@@ -331,11 +336,11 @@ export function buildRawTx (
331
336
  // Validation
332
337
  const valid = validateParams(params, schema, { excludeKeys })
333
338
  if (Object.keys(valid).length) {
334
- throw new Error('Transaction build error. ' + JSON.stringify(valid))
339
+ throw new InvalidTxParamsError('Transaction build error. ' + JSON.stringify(valid))
335
340
  }
336
341
 
337
342
  return filteredSchema
338
- .map(([key, fieldType, prefix]) => serializeField(params[key], fieldType, prefix))
343
+ .map(([key, fieldType, prefix]) => serializeField(params[key], fieldType, prefix, params))
339
344
  }
340
345
 
341
346
  /**
@@ -371,10 +376,10 @@ const getSchema = ({ vsn, objId, type }) => {
371
376
  const schema = isDeserialize ? TX_DESERIALIZATION_SCHEMA : TX_SERIALIZATION_SCHEMA
372
377
 
373
378
  if (!schema[firstKey]) {
374
- throw new Error(`Transaction ${isDeserialize ? 'deserialization' : 'serialization'} not implemented for ${isDeserialize ? 'tag ' + objId : type}`)
379
+ throw new SchemaNotFoundError(`Transaction ${isDeserialize ? 'deserialization' : 'serialization'} not implemented for ${isDeserialize ? 'tag ' + objId : type}`)
375
380
  }
376
381
  if (!schema[firstKey][vsn]) {
377
- throw new Error(`Transaction ${isDeserialize ? 'deserialization' : 'serialization'} not implemented for ${isDeserialize ? 'tag ' + objId : type} version ${vsn}`)
382
+ throw new SchemaNotFoundError(`Transaction ${isDeserialize ? 'deserialization' : 'serialization'} not implemented for ${isDeserialize ? 'tag ' + objId : type} version ${vsn}`)
378
383
  }
379
384
  return schema[firstKey][vsn]
380
385
  }
@@ -8,6 +8,7 @@
8
8
  // # https://github.com/aeternity/protocol/blob/master/serializations.md#binary-serialization
9
9
 
10
10
  import BigNumber from 'bignumber.js'
11
+ import { Name, NameId, NameFee, Deposit } from './field-types'
11
12
 
12
13
  export const VSN = 1
13
14
  export const VSN_2 = 2
@@ -20,9 +21,8 @@ export const ORACLE_TTL = { type: 'delta', value: 500 }
20
21
  export const QUERY_TTL = { type: 'delta', value: 10 }
21
22
  export const RESPONSE_TTL = { type: 'delta', value: 10 }
22
23
  // # CONTRACT
23
- export const DEPOSIT = 0
24
24
  export const AMOUNT = 0
25
- export const GAS = 25000
25
+ export const GAS_MAX = 1600000 - 21000
26
26
  export const MIN_GAS_PRICE = 1e9
27
27
  export const MAX_AUTH_FUN_GAS = 50000
28
28
  export const DRY_RUN_ACCOUNT = { pub: 'ak_11111111111111111111111111111111273Yts', amount: '100000000000000000000000000000000000' }
@@ -34,7 +34,6 @@ export const NAME_TTL = 180000
34
34
  export const NAME_MAX_TTL = 36000
35
35
  export const NAME_MAX_CLIENT_TTL = 84600
36
36
  export const CLIENT_TTL = NAME_MAX_CLIENT_TTL
37
- export const NAME_FEE = 0
38
37
  // # see https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L67
39
38
  export const NAME_FEE_MULTIPLIER = 1e14 // 100000000000000
40
39
  export const NAME_FEE_BID_INCREMENT = 0.05 // # the increment is in percentage
@@ -227,7 +226,6 @@ export const TX_TYPE = {
227
226
  nameserviceTree: 'nameserviceTree',
228
227
  oraclesTree: 'oraclesTree',
229
228
  accountsTree: 'accountsTree',
230
- // GA ACCOUNTS
231
229
  gaAttach: 'gaAttach',
232
230
  gaMeta: 'gaMeta',
233
231
  payingFor: 'payingFor',
@@ -327,7 +325,6 @@ export const OBJECT_ID_TX_TYPE = {
327
325
  [OBJECT_TAG_NAMESERVICE_TREE]: TX_TYPE.nameserviceTree,
328
326
  [OBJECT_TAG_ORACLES_TREE]: TX_TYPE.oraclesTree,
329
327
  [OBJECT_TAG_ACCOUNTS_TREE]: TX_TYPE.accountsTree,
330
- // GA Accounts
331
328
  [OBJECT_TAG_GA_ATTACH]: TX_TYPE.gaAttach,
332
329
  [OBJECT_TAG_GA_META]: TX_TYPE.gaMeta,
333
330
  [OBJECT_TAG_PAYING_FOR]: TX_TYPE.payingFor,
@@ -421,17 +418,6 @@ export const PREFIX_ID_TAG = {
421
418
  ch: ID_TAG.channel
422
419
  }
423
420
  export const ID_TAG_PREFIX = revertObject(PREFIX_ID_TAG)
424
- const VALIDATION_ERROR = (msg) => msg
425
-
426
- export const VALIDATION_MESSAGE = {
427
- [FIELD_TYPES.int]: ({ value, isMinusValue }) => isMinusValue ? VALIDATION_ERROR(`${value} must be >= 0`) : VALIDATION_ERROR(`${value} is not of type Number or BigNumber`),
428
- [FIELD_TYPES.amount]: ({ value, isMinusValue }) => isMinusValue ? VALIDATION_ERROR(`${value} must be >= 0`) : VALIDATION_ERROR(`${value} is not of type Number or BigNumber`),
429
- [FIELD_TYPES.id]: ({ value, prefix }) => VALIDATION_ERROR(`'${value}' prefix doesn't match expected prefix '${prefix}' or ID_TAG for prefix not found`),
430
- [FIELD_TYPES.binary]: ({ prefix, value }) => VALIDATION_ERROR(`'${value}' prefix doesn't match expected prefix '${prefix}'`),
431
- [FIELD_TYPES.string]: ({ value }) => VALIDATION_ERROR('Not a string'),
432
- [FIELD_TYPES.pointers]: ({ value }) => VALIDATION_ERROR('Value must be of type Array and contains only object\'s like \'{key: "account_pubkey", id: "ak_lkamsflkalsdalksdlasdlasdlamd"}\''),
433
- [FIELD_TYPES.ctVersion]: ({ value }) => VALIDATION_ERROR('Value must be an object with "vmVersion" and "abiVersion" fields')
434
- }
435
421
 
436
422
  const BASE_TX = [
437
423
  TX_FIELD('tag', FIELD_TYPES.int),
@@ -486,9 +472,9 @@ const NAME_CLAIM_TX_2 = [
486
472
  ...BASE_TX,
487
473
  TX_FIELD('accountId', FIELD_TYPES.id, 'ak'),
488
474
  TX_FIELD('nonce', FIELD_TYPES.int),
489
- TX_FIELD('name', FIELD_TYPES.binary, 'nm'),
475
+ TX_FIELD('name', Name),
490
476
  TX_FIELD('nameSalt', FIELD_TYPES.int),
491
- TX_FIELD('nameFee', FIELD_TYPES.amount),
477
+ TX_FIELD('nameFee', NameFee),
492
478
  TX_FIELD('fee', FIELD_TYPES.int),
493
479
  TX_FIELD('ttl', FIELD_TYPES.int)
494
480
  ]
@@ -497,7 +483,7 @@ const NAME_UPDATE_TX = [
497
483
  ...BASE_TX,
498
484
  TX_FIELD('accountId', FIELD_TYPES.id, 'ak'),
499
485
  TX_FIELD('nonce', FIELD_TYPES.int),
500
- TX_FIELD('nameId', FIELD_TYPES.id, 'nm'),
486
+ TX_FIELD('nameId', NameId),
501
487
  TX_FIELD('nameTtl', FIELD_TYPES.int),
502
488
  TX_FIELD('pointers', FIELD_TYPES.pointers),
503
489
  TX_FIELD('clientTtl', FIELD_TYPES.int),
@@ -509,7 +495,7 @@ const NAME_TRANSFER_TX = [
509
495
  ...BASE_TX,
510
496
  TX_FIELD('accountId', FIELD_TYPES.id, 'ak'),
511
497
  TX_FIELD('nonce', FIELD_TYPES.int),
512
- TX_FIELD('nameId', FIELD_TYPES.id, 'nm'),
498
+ TX_FIELD('nameId', NameId),
513
499
  TX_FIELD('recipientId', FIELD_TYPES.id, ['ak', 'nm']),
514
500
  TX_FIELD('fee', FIELD_TYPES.int),
515
501
  TX_FIELD('ttl', FIELD_TYPES.int)
@@ -519,7 +505,7 @@ const NAME_REVOKE_TX = [
519
505
  ...BASE_TX,
520
506
  TX_FIELD('accountId', FIELD_TYPES.id, 'ak'),
521
507
  TX_FIELD('nonce', FIELD_TYPES.int),
522
- TX_FIELD('nameId', FIELD_TYPES.id, 'nm'),
508
+ TX_FIELD('nameId', NameId),
523
509
  TX_FIELD('fee', FIELD_TYPES.int),
524
510
  TX_FIELD('ttl', FIELD_TYPES.int)
525
511
  ]
@@ -532,7 +518,7 @@ const CONTRACT_TX = [
532
518
  TX_FIELD('log', FIELD_TYPES.binary, 'cb'),
533
519
  TX_FIELD('active', FIELD_TYPES.bool),
534
520
  TX_FIELD('referers', FIELD_TYPES.ids, 'ak'),
535
- TX_FIELD('deposit', FIELD_TYPES.amount)
521
+ TX_FIELD('deposit', Deposit)
536
522
  ]
537
523
 
538
524
  const GA_ATTACH_TX = [
@@ -576,7 +562,7 @@ const CONTRACT_CREATE_TX = [
576
562
  TX_FIELD('ctVersion', FIELD_TYPES.ctVersion),
577
563
  TX_FIELD('fee', FIELD_TYPES.int),
578
564
  TX_FIELD('ttl', FIELD_TYPES.int),
579
- TX_FIELD('deposit', FIELD_TYPES.amount),
565
+ TX_FIELD('deposit', Deposit),
580
566
  TX_FIELD('amount', FIELD_TYPES.amount),
581
567
  TX_FIELD('gas', FIELD_TYPES.int),
582
568
  TX_FIELD('gasPrice', FIELD_TYPES.int),
@@ -752,8 +738,8 @@ const CHANNEL_FORCE_PROGRESS_TX = [
752
738
  TX_FIELD('fromId', FIELD_TYPES.id, 'ak'),
753
739
  TX_FIELD('payload', FIELD_TYPES.binary, 'tx'),
754
740
  TX_FIELD('round', FIELD_TYPES.int),
755
- TX_FIELD('update', FIELD_TYPES.binary),
756
- TX_FIELD('stateHash', FIELD_TYPES.binary),
741
+ TX_FIELD('update', FIELD_TYPES.binary, 'cb'),
742
+ TX_FIELD('stateHash', FIELD_TYPES.binary, 'st'),
757
743
  TX_FIELD('offChainTrees', FIELD_TYPES.stateTree),
758
744
  TX_FIELD('ttl', FIELD_TYPES.int),
759
745
  TX_FIELD('fee', FIELD_TYPES.int),