@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
@@ -0,0 +1,363 @@
1
+ import _buffer from "buffer";
2
+ const {
3
+ Buffer: _Buffer
4
+ } = _buffer;
5
+ import BigNumber from 'bignumber.js';
6
+ import bs58 from 'bs58';
7
+ import { hash, salt, sha256hash } from "../../utils/crypto.mjs";
8
+ import { toBytes } from "../../utils/bytes.mjs";
9
+ import { ID_TAG_PREFIX, PREFIX_ID_TAG, NAME_BID_RANGES, NAME_FEE_BID_INCREMENT, NAME_BID_TIMEOUTS, NAME_MAX_LENGTH_FEE, POINTER_KEY_BY_PREFIX } from "./schema.mjs";
10
+ import { ceil } from "../../utils/bignumber.mjs";
11
+ import { PrefixMismatchError, DecodeError, EncodeError, PayloadLengthError, TagNotFoundError, PrefixNotFoundError, InvalidNameError, IllegalBidFeeError, NoDefaultAensPointerError, ArgumentError, InvalidChecksumError } from "../../utils/errors.mjs";
12
+ /**
13
+ * JavaScript-based Transaction builder helper function's
14
+ * @module @aeternity/aepp-sdk/es/tx/builder/helpers
15
+ * @export TxBuilderHelper
16
+ * @example import { TxBuilderHelper } from '@aeternity/aepp-sdk'
17
+ */
18
+
19
+ export const createSalt = salt;
20
+ /**
21
+ * Build a contract public key
22
+ * @function
23
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
24
+ * @param {string} ownerId The public key of the owner account
25
+ * @param {number} nonce the nonce of the transaction
26
+ * @return {string} Contract public key
27
+ */
28
+
29
+ export function buildContractId(ownerId, nonce) {
30
+ const ownerIdAndNonce = _Buffer.from([...decode(ownerId, 'ak'), ...toBytes(nonce)]);
31
+
32
+ const b2bHash = hash(ownerIdAndNonce);
33
+ return encode(b2bHash, 'ct');
34
+ }
35
+ /**
36
+ * Build a oracle query id
37
+ * @function
38
+ * @function* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
39
+ * @param {String} senderId The public key of the sender account
40
+ * @param {Number} nonce the nonce of the transaction
41
+ * @param {Number} oracleId The oracle public key
42
+ * @return {string} Contract public key
43
+ */
44
+
45
+ export function oracleQueryId(senderId, nonce, oracleId) {
46
+ function _int32(val) {
47
+ const nonceBE = toBytes(val, true);
48
+ return _Buffer.concat([_Buffer.alloc(32 - nonceBE.length), nonceBE]);
49
+ }
50
+
51
+ const b2bHash = hash(_Buffer.from([...decode(senderId, 'ak'), ..._int32(nonce), ...decode(oracleId, 'ok')]));
52
+ return encode(b2bHash, 'oq');
53
+ }
54
+ /**
55
+ * Format the salt into a 64-byte hex string
56
+ * @function
57
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
58
+ * @param {number} salt
59
+ * @return {string} Zero-padded hex string of salt
60
+ */
61
+
62
+ export function formatSalt(salt) {
63
+ return _Buffer.from(salt.toString(16).padStart(64, '0'), 'hex');
64
+ }
65
+ /**
66
+ * Encode an AENS name
67
+ * @function
68
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
69
+ * @param {String} name Name to encode
70
+ * @return {String} `nm_` prefixed encoded AENS name
71
+ */
72
+
73
+ export function produceNameId(name) {
74
+ ensureNameValid(name);
75
+ return encode(hash(name.toLowerCase()), 'nm');
76
+ }
77
+ /**
78
+ * Generate the commitment hash by hashing the formatted salt and
79
+ * name, base 58 encoding the result and prepending 'cm_'
80
+ *
81
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
82
+ * @function commitmentHash
83
+ * @category async
84
+ * @rtype (name: String, salt?: String) => hash: Promise[String]
85
+ * @param {String} name - Name to be registered
86
+ * @param {Number} salt Random salt
87
+ * @return {String} Commitment hash
88
+ */
89
+
90
+ export function commitmentHash(name) {
91
+ let salt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : createSalt();
92
+ ensureNameValid(name);
93
+ return encode(hash(_Buffer.concat([_Buffer.from(name.toLowerCase()), formatSalt(salt)])), 'cm');
94
+ } // based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
95
+
96
+ const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx'];
97
+ const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']; // TODO: add all types with a fixed length
98
+
99
+ const typesLength = {
100
+ ak: 32,
101
+ ct: 32,
102
+ ok: 32
103
+ };
104
+
105
+ function ensureValidLength(data, type) {
106
+ if (!typesLength[type]) return;
107
+ if (data.length === typesLength[type]) return;
108
+ throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`);
109
+ }
110
+
111
+ const getChecksum = payload => sha256hash(sha256hash(payload)).slice(0, 4);
112
+
113
+ const addChecksum = input => {
114
+ const payload = _Buffer.from(input);
115
+
116
+ return _Buffer.concat([payload, getChecksum(payload)]);
117
+ };
118
+
119
+ function getPayload(buffer) {
120
+ const payload = buffer.slice(0, -4);
121
+ if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError();
122
+ return payload;
123
+ }
124
+
125
+ const base64 = {
126
+ encode: buffer => addChecksum(buffer).toString('base64'),
127
+ decode: string => getPayload(_Buffer.from(string, 'base64'))
128
+ };
129
+ const base58 = {
130
+ encode: buffer => bs58.encode(addChecksum(buffer)),
131
+ decode: string => getPayload(bs58.decode(string))
132
+ };
133
+ /**
134
+ * Decode data using the default encoding/decoding algorithm
135
+ * @function
136
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
137
+ * @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)
138
+ * @param {string} [requiredPrefix] Ensure that data have this prefix
139
+ * @return {Buffer} Decoded data
140
+ */
141
+
142
+ export function decode(data, requiredPrefix) {
143
+ if (typeof data !== 'string') throw new DecodeError(`Encoded should be a string, got ${data} instead`);
144
+ const [prefix, encodedPayload, extra] = data.split('_');
145
+ if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`);
146
+ if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`);
147
+
148
+ if (requiredPrefix && requiredPrefix !== prefix) {
149
+ throw new PrefixMismatchError(prefix, requiredPrefix);
150
+ }
151
+
152
+ const decoder = base64Types.includes(prefix) && base64.decode || base58Types.includes(prefix) && base58.decode;
153
+
154
+ if (!decoder) {
155
+ throw new DecodeError(`Encoded string have unknown type: ${prefix}`);
156
+ }
157
+
158
+ const payload = decoder(encodedPayload);
159
+ ensureValidLength(payload, prefix);
160
+ return payload;
161
+ }
162
+ /**
163
+ * Encode data using the default encoding/decoding algorithm
164
+ * @function
165
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
166
+ * @param {Buffer|String} data An decoded data
167
+ * @param {string} type Prefix of Transaction
168
+ * @return {String} Encoded string Base58check or Base64check data
169
+ */
170
+
171
+ export function encode(data, type) {
172
+ const encoder = base64Types.includes(type) && base64.encode || base58Types.includes(type) && base58.encode;
173
+
174
+ if (!encoder) {
175
+ throw new EncodeError(`Unknown type: ${type}`);
176
+ }
177
+
178
+ ensureValidLength(data, type);
179
+ return `${type}_${encoder(data)}`;
180
+ }
181
+ /**
182
+ * Utility function to create and _id type
183
+ * @function
184
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
185
+ * @param {string} hashId Encoded hash
186
+ * @return {Buffer} Buffer Buffer with ID tag and decoded HASh
187
+ */
188
+
189
+ export function writeId(hashId) {
190
+ if (typeof hashId !== 'string') throw new ArgumentError('hashId', 'a string', hashId);
191
+ const prefix = hashId.slice(0, 2);
192
+ const idTag = PREFIX_ID_TAG[prefix];
193
+ if (!idTag) throw new TagNotFoundError(prefix);
194
+ return _Buffer.from([...toBytes(idTag), ...decode(hashId, prefix)]);
195
+ }
196
+ /**
197
+ * Utility function to read and _id type
198
+ * @function
199
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
200
+ * @param {Buffer} buf Data
201
+ * @return {String} Encoided hash string with prefix
202
+ */
203
+
204
+ export function readId(buf) {
205
+ const tag = _Buffer.from(buf).readUIntBE(0, 1);
206
+
207
+ const prefix = ID_TAG_PREFIX[tag];
208
+ if (!prefix) throw new PrefixNotFoundError(tag);
209
+ return encode(buf.slice(1, buf.length), prefix);
210
+ }
211
+ /**
212
+ * Utility function to convert int to bytes
213
+ * @function
214
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
215
+ * @param {Number|String|BigNumber} val Value
216
+ * @return {Buffer} Buffer Buffer from number(BigEndian)
217
+ */
218
+
219
+ export function writeInt(val) {
220
+ return toBytes(val, true);
221
+ }
222
+ /**
223
+ * Utility function to convert bytes to int
224
+ * @function
225
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
226
+ * @param {Buffer} buf Value
227
+ * @return {String} Buffer Buffer from number(BigEndian)
228
+ */
229
+
230
+ export function readInt() {
231
+ let buf = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _Buffer.from([]);
232
+ return new BigNumber(_Buffer.from(buf).toString('hex'), 16).toString(10);
233
+ }
234
+ /**
235
+ * Helper function to build pointers for name update TX
236
+ * @function
237
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
238
+ * @param {Array} pointers - Array of pointers
239
+ * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
240
+ * @return {Array} Serialized pointers array
241
+ */
242
+
243
+ export function buildPointers(pointers) {
244
+ return pointers.map(p => [toBytes(p.key), writeId(p.id)]);
245
+ }
246
+ /**
247
+ * Helper function to read pointers from name update TX
248
+ * @function
249
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
250
+ * @param {Array} pointers - Array of pointers
251
+ * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
252
+ * @return {Array} Deserialize pointer array
253
+ */
254
+
255
+ export function readPointers(pointers) {
256
+ return pointers.map(_ref => {
257
+ let [key, id] = _ref;
258
+ return Object.assign({
259
+ key: key.toString(),
260
+ id: readId(id)
261
+ });
262
+ });
263
+ }
264
+ const AENS_SUFFIX = '.chain';
265
+ /**
266
+ * Ensure that AENS name is valid
267
+ * @function
268
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
269
+ * @param {string} name
270
+ * @return void
271
+ * @throws Error
272
+ */
273
+
274
+ export function ensureNameValid(name) {
275
+ if (!name || typeof name !== 'string') throw new InvalidNameError('Name must be a string');
276
+ if (!name.endsWith(AENS_SUFFIX)) throw new InvalidNameError(`Name should end with ${AENS_SUFFIX}: ${name}`);
277
+ }
278
+ /**
279
+ * Is AENS name valid
280
+ * @function
281
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
282
+ * @param {string} name
283
+ * @return Boolean
284
+ */
285
+
286
+ export function isNameValid(name) {
287
+ try {
288
+ ensureNameValid(name);
289
+ return true;
290
+ } catch (error) {
291
+ return false;
292
+ }
293
+ }
294
+ /**
295
+ * @param identifier - account/oracle/contract address, or channel
296
+ * @returns {String} default AENS pointer key
297
+ * @throws exception when default key not defined
298
+ */
299
+
300
+ export function getDefaultPointerKey(identifier) {
301
+ decode(identifier);
302
+ const prefix = identifier.substr(0, 2);
303
+ return POINTER_KEY_BY_PREFIX[prefix] || (() => {
304
+ throw new NoDefaultAensPointerError(prefix);
305
+ })();
306
+ }
307
+ /**
308
+ * Get the minimum AENS name fee
309
+ * @function
310
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
311
+ * @param {String} name the AENS name to get the fee for
312
+ * @return {String} the minimum fee for the AENS name auction
313
+ */
314
+
315
+ export function getMinimumNameFee(name) {
316
+ ensureNameValid(name);
317
+ const nameLength = name.length - AENS_SUFFIX.length;
318
+ return NAME_BID_RANGES[Math.min(nameLength, NAME_MAX_LENGTH_FEE)];
319
+ }
320
+ /**
321
+ * Compute bid fee for AENS auction
322
+ * @function
323
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
324
+ * @param {String} name the AENS name to get the fee for
325
+ * @param {Number|String} startFee Auction start fee
326
+ * @param {Number} [increment=0.5] Bid multiplier(In percentage, must be between 0 and 1)
327
+ * @return {String} Bid fee
328
+ */
329
+
330
+ export function computeBidFee(name, startFee) {
331
+ let increment = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NAME_FEE_BID_INCREMENT;
332
+ if (!(Number(increment) === increment && increment % 1 !== 0)) throw new IllegalBidFeeError(`Increment must be float. Current increment ${increment}`);
333
+ if (increment < NAME_FEE_BID_INCREMENT) throw new IllegalBidFeeError(`minimum increment percentage is ${NAME_FEE_BID_INCREMENT}`);
334
+ return ceil(BigNumber(startFee !== null && startFee !== void 0 ? startFee : getMinimumNameFee(name)).times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1)));
335
+ }
336
+ /**
337
+ * Compute auction end height
338
+ * @function
339
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
340
+ * @param {String} name
341
+ * @param {Number|String} claimHeight Auction starting height
342
+ * @return {String} Auction end height
343
+ */
344
+
345
+ export function computeAuctionEndBlock(name, claimHeight) {
346
+ ensureNameValid(name);
347
+ const length = name.length - AENS_SUFFIX.length;
348
+ const h = length <= 4 && NAME_BID_TIMEOUTS[4] || length <= 8 && NAME_BID_TIMEOUTS[8] || length <= 12 && NAME_BID_TIMEOUTS[12] || NAME_BID_TIMEOUTS[13];
349
+ return h.plus(claimHeight).toString(10);
350
+ }
351
+ /**
352
+ * Is name accept going to auction
353
+ * @function
354
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
355
+ * @param {String} name
356
+ * @return {Boolean}
357
+ */
358
+
359
+ export function isAuctionName(name) {
360
+ ensureNameValid(name);
361
+ return name.length < 13 + AENS_SUFFIX.length;
362
+ }
363
+ //# sourceMappingURL=helpers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/tx/builder/helpers.js"],"names":["BigNumber","bs58","hash","salt","sha256hash","toBytes","ID_TAG_PREFIX","PREFIX_ID_TAG","NAME_BID_RANGES","NAME_FEE_BID_INCREMENT","NAME_BID_TIMEOUTS","NAME_MAX_LENGTH_FEE","POINTER_KEY_BY_PREFIX","ceil","PrefixMismatchError","DecodeError","EncodeError","PayloadLengthError","TagNotFoundError","PrefixNotFoundError","InvalidNameError","IllegalBidFeeError","NoDefaultAensPointerError","ArgumentError","InvalidChecksumError","createSalt","buildContractId","ownerId","nonce","ownerIdAndNonce","from","decode","b2bHash","encode","oracleQueryId","senderId","oracleId","_int32","val","nonceBE","concat","alloc","length","formatSalt","toString","padStart","produceNameId","name","ensureNameValid","toLowerCase","commitmentHash","base64Types","base58Types","typesLength","ak","ct","ok","ensureValidLength","data","type","getChecksum","payload","slice","addChecksum","input","getPayload","buffer","equals","base64","string","base58","requiredPrefix","prefix","encodedPayload","extra","split","decoder","includes","encoder","writeId","hashId","idTag","readId","buf","tag","readUIntBE","writeInt","readInt","buildPointers","pointers","map","p","key","id","readPointers","Object","assign","AENS_SUFFIX","endsWith","isNameValid","error","getDefaultPointerKey","identifier","substr","getMinimumNameFee","nameLength","Math","min","computeBidFee","startFee","increment","Number","times","plus","computeAuctionEndBlock","claimHeight","h","isAuctionName"],"mappings":";;;;AAAA,OAAOA,SAAP,MAAsB,cAAtB;AACA,OAAOC,IAAP,MAAiB,MAAjB;SACSC,I,EAAMC,I,EAAMC,U;SACZC,O;SAEPC,a,EACAC,a,EACAC,e,EACAC,sB,EACAC,iB,EACAC,mB,EACAC,qB;SAEOC,I;SAEPC,mB,EACAC,W,EACAC,W,EACAC,kB,EACAC,gB,EACAC,mB,EACAC,gB,EACAC,kB,EACAC,yB,EACAC,a,EACAC,oB;AAGF;AACA;AACA;AACA;AACA;AACA;;AAEA,OAAO,MAAMC,UAAU,GAAGtB,IAAnB;AAEP;AACA;AACA;AAC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uD,WAAW,CAACvD,MAAtC;AACD","sourcesContent":["import BigNumber from 'bignumber.js'\nimport bs58 from 'bs58'\nimport { hash, salt, sha256hash } from '../../utils/crypto'\nimport { toBytes } from '../../utils/bytes'\nimport {\n ID_TAG_PREFIX,\n PREFIX_ID_TAG,\n NAME_BID_RANGES,\n NAME_FEE_BID_INCREMENT,\n NAME_BID_TIMEOUTS,\n NAME_MAX_LENGTH_FEE,\n POINTER_KEY_BY_PREFIX\n} from './schema'\nimport { ceil } from '../../utils/bignumber'\nimport {\n PrefixMismatchError,\n DecodeError,\n EncodeError,\n PayloadLengthError,\n TagNotFoundError,\n PrefixNotFoundError,\n InvalidNameError,\n IllegalBidFeeError,\n NoDefaultAensPointerError,\n ArgumentError,\n InvalidChecksumError\n} from '../../utils/errors'\n\n/**\n * JavaScript-based Transaction builder helper function's\n * @module @aeternity/aepp-sdk/es/tx/builder/helpers\n * @export TxBuilderHelper\n * @example import { TxBuilderHelper } from '@aeternity/aepp-sdk'\n */\n\nexport const createSalt = salt\n\n/**\n * Build a contract public key\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} ownerId The public key of the owner account\n * @param {number} nonce the nonce of the transaction\n * @return {string} Contract public key\n */\nexport function buildContractId (ownerId, nonce) {\n const ownerIdAndNonce = Buffer.from([...decode(ownerId, 'ak'), ...toBytes(nonce)])\n const b2bHash = hash(ownerIdAndNonce)\n return encode(b2bHash, 'ct')\n}\n\n/**\n * Build a oracle query id\n * @function\n * @function* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {String} senderId The public key of the sender account\n * @param {Number} nonce the nonce of the transaction\n * @param {Number} oracleId The oracle public key\n * @return {string} Contract public key\n */\nexport function oracleQueryId (senderId, nonce, oracleId) {\n function _int32 (val) {\n const nonceBE = toBytes(val, true)\n return Buffer.concat([Buffer.alloc(32 - nonceBE.length), nonceBE])\n }\n\n const b2bHash = hash(Buffer.from([...decode(senderId, 'ak'), ..._int32(nonce), ...decode(oracleId, 'ok')]))\n return encode(b2bHash, 'oq')\n}\n\n/**\n * Format the salt into a 64-byte hex string\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {number} salt\n * @return {string} Zero-padded hex string of salt\n */\nexport function formatSalt (salt) {\n return Buffer.from(salt.toString(16).padStart(64, '0'), 'hex')\n}\n\n/**\n * Encode an AENS name\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {String} name Name to encode\n * @return {String} `nm_` prefixed encoded AENS name\n */\nexport function produceNameId (name) {\n ensureNameValid(name)\n return encode(hash(name.toLowerCase()), 'nm')\n}\n\n/**\n * Generate the commitment hash by hashing the formatted salt and\n * name, base 58 encoding the result and prepending 'cm_'\n *\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @function commitmentHash\n * @category async\n * @rtype (name: String, salt?: String) => hash: Promise[String]\n * @param {String} name - Name to be registered\n * @param {Number} salt Random salt\n * @return {String} Commitment hash\n */\nexport function commitmentHash (name, salt = createSalt()) {\n ensureNameValid(name)\n return encode(hash(Buffer.concat([Buffer.from(name.toLowerCase()), formatSalt(salt)])), 'cm')\n}\n\n// based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md\nconst base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx']\nconst base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']\n// TODO: add all types with a fixed length\nconst typesLength = {\n ak: 32,\n ct: 32,\n ok: 32\n}\n\nfunction ensureValidLength (data, type) {\n if (!typesLength[type]) return\n if (data.length === typesLength[type]) return\n throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`)\n}\n\nconst getChecksum = payload => sha256hash(sha256hash(payload)).slice(0, 4)\n\nconst addChecksum = (input) => {\n const payload = Buffer.from(input)\n return Buffer.concat([payload, getChecksum(payload)])\n}\n\nfunction getPayload (buffer) {\n const payload = buffer.slice(0, -4)\n if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError()\n return payload\n}\n\nconst base64 = {\n encode: buffer => addChecksum(buffer).toString('base64'),\n decode: string => getPayload(Buffer.from(string, 'base64'))\n}\n\nconst base58 = {\n encode: buffer => bs58.encode(addChecksum(buffer)),\n decode: string => getPayload(bs58.decode(string))\n}\n\n/**\n * Decode data using the default encoding/decoding algorithm\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)\n * @param {string} [requiredPrefix] Ensure that data have this prefix\n * @return {Buffer} Decoded data\n */\nexport function decode (data, requiredPrefix) {\n if (typeof data !== 'string') throw new DecodeError(`Encoded should be a string, got ${data} instead`)\n const [prefix, encodedPayload, extra] = data.split('_')\n if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`)\n if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`)\n if (requiredPrefix && requiredPrefix !== prefix) {\n throw new PrefixMismatchError(prefix, requiredPrefix)\n }\n const decoder = (base64Types.includes(prefix) && base64.decode) ||\n (base58Types.includes(prefix) && base58.decode)\n if (!decoder) {\n throw new DecodeError(`Encoded string have unknown type: ${prefix}`)\n }\n const payload = decoder(encodedPayload)\n ensureValidLength(payload, prefix)\n return payload\n}\n\n/**\n * Encode data using the default encoding/decoding algorithm\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Buffer|String} data An decoded data\n * @param {string} type Prefix of Transaction\n * @return {String} Encoded string Base58check or Base64check data\n */\nexport function encode (data, type) {\n const encoder = (base64Types.includes(type) && base64.encode) ||\n (base58Types.includes(type) && base58.encode)\n if (!encoder) {\n throw new EncodeError(`Unknown type: ${type}`)\n }\n ensureValidLength(data, type)\n return `${type}_${encoder(data)}`\n}\n\n/**\n * Utility function to create and _id type\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} hashId Encoded hash\n * @return {Buffer} Buffer Buffer with ID tag and decoded HASh\n */\nexport function writeId (hashId) {\n if (typeof hashId !== 'string') throw new ArgumentError('hashId', 'a string', hashId)\n const prefix = hashId.slice(0, 2)\n const idTag = PREFIX_ID_TAG[prefix]\n if (!idTag) throw new TagNotFoundError(prefix)\n return Buffer.from([...toBytes(idTag), ...decode(hashId, prefix)])\n}\n\n/**\n * Utility function to read and _id type\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Buffer} buf Data\n * @return {String} Encoided hash string with prefix\n */\nexport function readId (buf) {\n const tag = Buffer.from(buf).readUIntBE(0, 1)\n const prefix = ID_TAG_PREFIX[tag]\n if (!prefix) throw new PrefixNotFoundError(tag)\n return encode(buf.slice(1, buf.length), prefix)\n}\n\n/**\n * Utility function to convert int to bytes\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Number|String|BigNumber} val Value\n * @return {Buffer} Buffer Buffer from number(BigEndian)\n */\nexport function writeInt (val) {\n return toBytes(val, true)\n}\n\n/**\n * Utility function to convert bytes to int\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Buffer} buf Value\n * @return {String} Buffer Buffer from number(BigEndian)\n */\nexport function readInt (buf = Buffer.from([])) {\n return new BigNumber(Buffer.from(buf).toString('hex'), 16).toString(10)\n}\n\n/**\n * Helper function to build pointers for name update TX\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Array} pointers - Array of pointers\n * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])\n * @return {Array} Serialized pointers array\n */\nexport function buildPointers (pointers) {\n return pointers.map(\n p => [\n toBytes(p.key),\n writeId(p.id)\n ]\n )\n}\n\n/**\n * Helper function to read pointers from name update TX\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Array} pointers - Array of pointers\n * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])\n * @return {Array} Deserialize pointer array\n */\nexport function readPointers (pointers) {\n return pointers.map(\n ([key, id]) => Object.assign({\n key: key.toString(),\n id: readId(id)\n })\n )\n}\n\nconst AENS_SUFFIX = '.chain'\n\n/**\n * Ensure that AENS name is valid\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} name\n * @return void\n * @throws Error\n */\nexport function ensureNameValid (name) {\n if (!name || typeof name !== 'string') throw new InvalidNameError('Name must be a string')\n if (!name.endsWith(AENS_SUFFIX)) throw new InvalidNameError(`Name should end with ${AENS_SUFFIX}: ${name}`)\n}\n\n/**\n * Is AENS name valid\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} name\n * @return Boolean\n */\nexport function isNameValid (name) {\n try {\n ensureNameValid(name)\n return true\n } catch (error) {\n return false\n }\n}\n\n/**\n * @param identifier - account/oracle/contract address, or channel\n * @returns {String} default AENS pointer key\n * @throws exception when default key not defined\n */\nexport function getDefaultPointerKey (identifier) {\n decode(identifier)\n const prefix = identifier.substr(0, 2)\n return POINTER_KEY_BY_PREFIX[prefix] ||\n (() => { throw new NoDefaultAensPointerError(prefix) })()\n}\n\n/**\n * Get the minimum AENS name fee\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {String} name the AENS name to get the fee for\n * @return {String} the minimum fee for the AENS name auction\n */\nexport function getMinimumNameFee (name) {\n ensureNameValid(name)\n const nameLength = name.length - AENS_SUFFIX.length\n return NAME_BID_RANGES[Math.min(nameLength, NAME_MAX_LENGTH_FEE)]\n}\n\n/**\n * Compute bid fee for AENS auction\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {String} name the AENS name to get the fee for\n * @param {Number|String} startFee Auction start fee\n * @param {Number} [increment=0.5] Bid multiplier(In percentage, must be between 0 and 1)\n * @return {String} Bid fee\n */\nexport function computeBidFee (name, startFee, increment = NAME_FEE_BID_INCREMENT) {\n if (!(Number(increment) === increment && increment % 1 !== 0)) throw new IllegalBidFeeError(`Increment must be float. Current increment ${increment}`)\n if (increment < NAME_FEE_BID_INCREMENT) throw new IllegalBidFeeError(`minimum increment percentage is ${NAME_FEE_BID_INCREMENT}`)\n return ceil(\n BigNumber(startFee ?? getMinimumNameFee(name)).times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1))\n )\n}\n\n/**\n * Compute auction end height\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {String} name\n * @param {Number|String} claimHeight Auction starting height\n * @return {String} Auction end height\n */\nexport function computeAuctionEndBlock (name, claimHeight) {\n ensureNameValid(name)\n const length = name.length - AENS_SUFFIX.length\n const h = (length <= 4 && NAME_BID_TIMEOUTS[4]) ||\n (length <= 8 && NAME_BID_TIMEOUTS[8]) ||\n (length <= 12 && NAME_BID_TIMEOUTS[12]) ||\n NAME_BID_TIMEOUTS[13]\n return h.plus(claimHeight).toString(10)\n}\n\n/**\n * Is name accept going to auction\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {String} name\n * @return {Boolean}\n */\nexport function isAuctionName (name) {\n ensureNameValid(name)\n return name.length < 13 + AENS_SUFFIX.length\n}\n"],"file":"helpers.mjs"}