@aeternity/aepp-sdk 10.0.0-beta.1 → 11.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/README.md +15 -14
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -27
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/account/{base.js → base.mjs} +23 -11
  10. package/es/account/base.mjs.map +1 -0
  11. package/es/account/{memory.js → memory.mjs} +29 -14
  12. package/es/account/memory.mjs.map +1 -0
  13. package/es/account/{multiple.js → multiple.mjs} +14 -11
  14. package/es/account/multiple.mjs.map +1 -0
  15. package/es/ae/{aens.js → aens.mjs} +69 -59
  16. package/es/ae/aens.mjs.map +1 -0
  17. package/es/ae/{aepp.js → aepp.mjs} +9 -9
  18. package/es/ae/aepp.mjs.map +1 -0
  19. package/es/ae/contract.mjs +159 -0
  20. package/es/ae/contract.mjs.map +1 -0
  21. package/es/ae/{index.js → index.mjs} +12 -10
  22. package/es/ae/index.mjs.map +1 -0
  23. package/es/ae/{oracle.js → oracle.mjs} +37 -20
  24. package/es/ae/oracle.mjs.map +1 -0
  25. package/es/ae/{universal.js → universal.mjs} +10 -10
  26. package/es/ae/universal.mjs.map +1 -0
  27. package/es/ae/{wallet.js → wallet.mjs} +10 -10
  28. package/es/ae/wallet.mjs.map +1 -0
  29. package/es/aepp-wallet-communication/schema.d.ts +74 -0
  30. package/es/bignumber.d.ts +1 -7
  31. package/es/chain/{index.js → index.mjs} +28 -8
  32. package/es/chain/index.mjs.map +1 -0
  33. package/es/chain/{node.js → node.mjs} +133 -90
  34. package/es/chain/node.mjs.map +1 -0
  35. package/es/channel/{handlers.js → handlers.mjs} +18 -20
  36. package/es/channel/handlers.mjs.map +1 -0
  37. package/es/channel/{index.js → index.mjs} +43 -22
  38. package/es/channel/index.mjs.map +1 -0
  39. package/es/channel/{internal.js → internal.mjs} +51 -35
  40. package/es/channel/internal.mjs.map +1 -0
  41. package/es/contract/aci.mjs +477 -0
  42. package/es/contract/aci.mjs.map +1 -0
  43. package/es/contract/compiler.mjs +115 -0
  44. package/es/contract/compiler.mjs.map +1 -0
  45. package/es/contract/ga/helpers.mjs +30 -0
  46. package/es/contract/ga/helpers.mjs.map +1 -0
  47. package/es/contract/ga/{index.js → index.mjs} +40 -28
  48. package/es/contract/ga/index.mjs.map +1 -0
  49. package/es/encoder.d.ts +26 -0
  50. package/es/errors.d.ts +284 -0
  51. package/es/index.mjs +57 -0
  52. package/es/index.mjs.map +1 -0
  53. package/es/node-pool/{index.js → index.mjs} +15 -12
  54. package/es/node-pool/index.mjs.map +1 -0
  55. package/es/{node.js → node.mjs} +17 -11
  56. package/es/node.mjs.map +1 -0
  57. package/es/tx/builder/constants.mjs +108 -0
  58. package/es/tx/builder/constants.mjs.map +1 -0
  59. package/es/tx/builder/field-types.mjs +74 -0
  60. package/es/tx/builder/field-types.mjs.map +1 -0
  61. package/es/tx/builder/helpers.mjs +321 -0
  62. package/es/tx/builder/helpers.mjs.map +1 -0
  63. package/es/tx/builder/{index.js → index.mjs} +92 -74
  64. package/es/tx/builder/index.mjs.map +1 -0
  65. package/es/tx/builder/{schema.js → schema.mjs} +16 -172
  66. package/es/tx/builder/schema.mjs.map +1 -0
  67. package/es/tx/{index.js → index.mjs} +106 -14
  68. package/es/tx/index.mjs.map +1 -0
  69. package/es/tx/{tx-object.js → tx-object.mjs} +31 -18
  70. package/es/tx/tx-object.mjs.map +1 -0
  71. package/es/tx/{tx.js → tx.mjs} +15 -14
  72. package/es/tx/tx.mjs.map +1 -0
  73. package/es/tx/{validator.js → validator.mjs} +72 -33
  74. package/es/tx/validator.mjs.map +1 -0
  75. package/es/typings/blakejs/index.d.mjs +2 -0
  76. package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
  77. package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +10 -8
  78. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
  79. package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +25 -17
  80. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
  81. package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
  82. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
  83. package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +15 -8
  84. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
  85. package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
  86. package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
  87. package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +126 -72
  88. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
  89. package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +23 -14
  90. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
  91. package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +116 -91
  92. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
  93. package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
  94. package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
  95. package/es/utils/aepp-wallet-communication/{wallet-detector.js → wallet-detector.mjs} +6 -5
  96. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  97. package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
  98. package/es/utils/amount-formatter.mjs.map +1 -0
  99. package/es/utils/{async-init.js → async-init.mjs} +8 -11
  100. package/es/utils/async-init.mjs.map +1 -0
  101. package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
  102. package/es/utils/bignumber.mjs.map +1 -0
  103. package/es/utils/{bytes.js → bytes.mjs} +16 -8
  104. package/es/utils/bytes.mjs.map +1 -0
  105. package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
  106. package/es/utils/crypto-ts.mjs.map +1 -0
  107. package/es/utils/{crypto.js → crypto.mjs} +42 -156
  108. package/es/utils/crypto.mjs.map +1 -0
  109. package/es/utils/encoder.mjs +106 -0
  110. package/es/utils/encoder.mjs.map +1 -0
  111. package/es/utils/errors.mjs +689 -0
  112. package/es/utils/errors.mjs.map +1 -0
  113. package/es/utils/{hd-wallet.js → hd-wallet.mjs} +33 -34
  114. package/es/utils/hd-wallet.mjs.map +1 -0
  115. package/es/utils/{json-big.js → json-big.mjs} +1 -1
  116. package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
  117. package/es/utils/{keystore.js → keystore.mjs} +63 -51
  118. package/es/utils/keystore.mjs.map +1 -0
  119. package/es/utils/{mptree.js → mptree.mjs} +21 -12
  120. package/es/utils/mptree.mjs.map +1 -0
  121. package/es/utils/{other.js → other.mjs} +2 -2
  122. package/es/utils/other.mjs.map +1 -0
  123. package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
  124. package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
  125. package/es/utils/{string.js → string.mjs} +1 -1
  126. package/es/utils/{string.js.map → string.mjs.map} +1 -1
  127. package/es/utils/{swagger.js → swagger.mjs} +49 -15
  128. package/es/utils/swagger.mjs.map +1 -0
  129. package/package.json +49 -58
  130. package/src/account/base.js +3 -1
  131. package/src/account/memory.js +13 -8
  132. package/src/account/multiple.js +16 -6
  133. package/src/ae/aens.js +52 -58
  134. package/src/ae/aepp.js +2 -2
  135. package/src/ae/contract.js +21 -154
  136. package/src/ae/index.js +6 -4
  137. package/src/ae/oracle.js +33 -14
  138. package/src/ae/universal.js +4 -2
  139. package/src/ae/wallet.js +2 -2
  140. package/src/chain/index.js +29 -6
  141. package/src/chain/node.js +95 -65
  142. package/src/channel/handlers.js +30 -20
  143. package/src/channel/index.js +46 -16
  144. package/src/channel/internal.js +35 -30
  145. package/src/contract/aci.js +376 -0
  146. package/src/contract/compiler.js +22 -113
  147. package/src/contract/ga/helpers.js +10 -6
  148. package/src/contract/ga/index.js +31 -24
  149. package/src/index.js +9 -8
  150. package/src/node-pool/index.js +13 -10
  151. package/src/node.js +11 -10
  152. package/src/tx/builder/constants.js +100 -0
  153. package/src/tx/builder/field-types.js +61 -0
  154. package/src/tx/builder/helpers.js +99 -103
  155. package/src/tx/builder/index.js +81 -40
  156. package/src/tx/builder/schema.js +44 -132
  157. package/src/tx/index.js +101 -12
  158. package/src/tx/tx-object.js +35 -15
  159. package/src/tx/tx.js +110 -35
  160. package/src/tx/validator.js +25 -5
  161. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
  162. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
  163. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  164. package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
  165. package/src/utils/aepp-wallet-communication/helpers.js +6 -14
  166. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +106 -57
  167. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
  168. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +95 -53
  169. package/src/utils/aepp-wallet-communication/schema.ts +80 -0
  170. package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
  171. package/src/utils/amount-formatter.ts +4 -3
  172. package/src/utils/async-init.js +10 -6
  173. package/src/utils/bignumber.ts +1 -9
  174. package/src/utils/bytes.ts +3 -2
  175. package/src/utils/crypto-ts.ts +2 -2
  176. package/src/utils/crypto.js +11 -155
  177. package/src/utils/encoder.ts +101 -0
  178. package/src/utils/errors.ts +683 -0
  179. package/src/utils/hd-wallet.js +11 -26
  180. package/src/utils/keystore.js +57 -53
  181. package/src/utils/mptree.ts +12 -6
  182. package/src/utils/swagger.js +68 -45
  183. package/es/account/base.js.map +0 -1
  184. package/es/account/memory.js.map +0 -1
  185. package/es/account/multiple.js.map +0 -1
  186. package/es/ae/aens.js.map +0 -1
  187. package/es/ae/aepp.js.map +0 -1
  188. package/es/ae/contract.js +0 -308
  189. package/es/ae/contract.js.map +0 -1
  190. package/es/ae/index.js.map +0 -1
  191. package/es/ae/oracle.js.map +0 -1
  192. package/es/ae/universal.js.map +0 -1
  193. package/es/ae/wallet.js.map +0 -1
  194. package/es/chain/index.js.map +0 -1
  195. package/es/chain/node.js.map +0 -1
  196. package/es/channel/handlers.js.map +0 -1
  197. package/es/channel/index.js.map +0 -1
  198. package/es/channel/internal.js.map +0 -1
  199. package/es/contract/aci/helpers.js +0 -298
  200. package/es/contract/aci/helpers.js.map +0 -1
  201. package/es/contract/aci/index.js +0 -356
  202. package/es/contract/aci/index.js.map +0 -1
  203. package/es/contract/aci/transformation.js +0 -66
  204. package/es/contract/aci/transformation.js.map +0 -1
  205. package/es/contract/compiler.js +0 -242
  206. package/es/contract/compiler.js.map +0 -1
  207. package/es/contract/ga/helpers.js +0 -22
  208. package/es/contract/ga/helpers.js.map +0 -1
  209. package/es/contract/ga/index.js.map +0 -1
  210. package/es/contract/index.js +0 -158
  211. package/es/contract/index.js.map +0 -1
  212. package/es/index.js +0 -56
  213. package/es/index.js.map +0 -1
  214. package/es/node-pool/index.js.map +0 -1
  215. package/es/node.js.map +0 -1
  216. package/es/tx/builder/helpers.js +0 -318
  217. package/es/tx/builder/helpers.js.map +0 -1
  218. package/es/tx/builder/index.js.map +0 -1
  219. package/es/tx/builder/schema.js.map +0 -1
  220. package/es/tx/index.js.map +0 -1
  221. package/es/tx/tx-object.js.map +0 -1
  222. package/es/tx/tx.js.map +0 -1
  223. package/es/tx/validator.js.map +0 -1
  224. package/es/typings/blakejs/index.d.js +0 -2
  225. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  226. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  227. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  228. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  229. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  230. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  231. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  232. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  233. package/es/utils/aepp-wallet-communication/schema.js +0 -107
  234. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  235. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  236. package/es/utils/amount-formatter.js.map +0 -1
  237. package/es/utils/async-init.js.map +0 -1
  238. package/es/utils/bignumber.js.map +0 -1
  239. package/es/utils/bytes.js.map +0 -1
  240. package/es/utils/crypto-ts.js.map +0 -1
  241. package/es/utils/crypto.js.map +0 -1
  242. package/es/utils/hd-wallet.js.map +0 -1
  243. package/es/utils/keystore.js.map +0 -1
  244. package/es/utils/mptree.js.map +0 -1
  245. package/es/utils/other.js.map +0 -1
  246. package/es/utils/swagger.js.map +0 -1
  247. package/src/contract/aci/index.js +0 -310
  248. package/src/contract/aci/transformation.js +0 -72
  249. package/src/contract/index.js +0 -159
  250. package/src/utils/aepp-wallet-communication/schema.js +0 -108
@@ -0,0 +1,321 @@
1
+ import _buffer from "buffer";
2
+ const {
3
+ Buffer: _Buffer
4
+ } = _buffer;
5
+ import BigNumber from 'bignumber.js';
6
+ import _bs from 'bs58';
7
+ const {
8
+ encode: bs58Encode,
9
+ decode: bs58Decode
10
+ } = _bs;
11
+ import { hash, salt } from "../../utils/crypto.mjs";
12
+ import { encode, decode } from "../../utils/encoder.mjs";
13
+ import { toBytes } from "../../utils/bytes.mjs";
14
+ 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 "./constants.mjs";
15
+ import { ceil } from "../../utils/bignumber.mjs";
16
+ import { TagNotFoundError, PrefixNotFoundError, InvalidNameError, IllegalBidFeeError, NoDefaultAensPointerError, ArgumentError } from "../../utils/errors.mjs";
17
+ /**
18
+ * JavaScript-based Transaction builder helper function's
19
+ * @module @aeternity/aepp-sdk/es/tx/builder/helpers
20
+ * @export TxBuilderHelper
21
+ * @example import { TxBuilderHelper } from '@aeternity/aepp-sdk'
22
+ */
23
+
24
+ export const createSalt = salt;
25
+ export { encode, decode };
26
+ /**
27
+ * Build a contract public key
28
+ * @function
29
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
30
+ * @param {string} ownerId The public key of the owner account
31
+ * @param {number} nonce the nonce of the transaction
32
+ * @return {string} Contract public key
33
+ */
34
+
35
+ export function buildContractId(ownerId, nonce) {
36
+ const ownerIdAndNonce = _Buffer.from([...decode(ownerId, 'ak'), ...toBytes(nonce)]);
37
+
38
+ const b2bHash = hash(ownerIdAndNonce);
39
+ return encode(b2bHash, 'ct');
40
+ }
41
+ /**
42
+ * Build a oracle query id
43
+ * @function
44
+ * @function* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
45
+ * @param {String} senderId The public key of the sender account
46
+ * @param {Number} nonce the nonce of the transaction
47
+ * @param {Number} oracleId The oracle public key
48
+ * @return {string} Contract public key
49
+ */
50
+
51
+ export function oracleQueryId(senderId, nonce, oracleId) {
52
+ function _int32(val) {
53
+ const nonceBE = toBytes(val, true);
54
+ return _Buffer.concat([_Buffer.alloc(32 - nonceBE.length), nonceBE]);
55
+ }
56
+
57
+ const b2bHash = hash(_Buffer.from([...decode(senderId, 'ak'), ..._int32(nonce), ...decode(oracleId, 'ok')]));
58
+ return encode(b2bHash, 'oq');
59
+ }
60
+ /**
61
+ * Format the salt into a 64-byte hex string
62
+ * @function
63
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
64
+ * @param {number} salt
65
+ * @return {string} Zero-padded hex string of salt
66
+ */
67
+
68
+ export function formatSalt(salt) {
69
+ return _Buffer.from(salt.toString(16).padStart(64, '0'), 'hex');
70
+ }
71
+ /**
72
+ * Encode an AENS name
73
+ * @function
74
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
75
+ * @param {String} name Name to encode
76
+ * @return {String} `nm_` prefixed encoded AENS name
77
+ */
78
+
79
+ export function produceNameId(name) {
80
+ ensureNameValid(name);
81
+ return encode(hash(name.toLowerCase()), 'nm');
82
+ }
83
+ /**
84
+ * Generate the commitment hash by hashing the formatted salt and
85
+ * name, base 58 encoding the result and prepending 'cm_'
86
+ *
87
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
88
+ * @function commitmentHash
89
+ * @category async
90
+ * @rtype (name: String, salt?: String) => hash: Promise[String]
91
+ * @param {String} name - Name to be registered
92
+ * @param {Number} salt Random salt
93
+ * @return {String} Commitment hash
94
+ */
95
+
96
+ export function commitmentHash(name) {
97
+ let salt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : createSalt();
98
+ ensureNameValid(name);
99
+ return encode(hash(_Buffer.concat([_Buffer.from(name.toLowerCase()), formatSalt(salt)])), 'cm');
100
+ } // based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
101
+
102
+ const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx'];
103
+ 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
104
+
105
+ const typesLength = {
106
+ ak: 32,
107
+ ct: 32,
108
+ ok: 32
109
+ };
110
+
111
+ function ensureValidLength(data, type) {
112
+ if (!typesLength[type]) return;
113
+ if (data.length === typesLength[type]) return;
114
+ throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`);
115
+ }
116
+
117
+ const getChecksum = payload => sha256hash(sha256hash(payload)).slice(0, 4);
118
+
119
+ const addChecksum = input => {
120
+ const payload = _Buffer.from(input);
121
+
122
+ return _Buffer.concat([payload, getChecksum(payload)]);
123
+ };
124
+
125
+ function getPayload(buffer) {
126
+ const payload = buffer.slice(0, -4);
127
+ if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError();
128
+ return payload;
129
+ }
130
+
131
+ const base64 = {
132
+ encode: buffer => addChecksum(buffer).toString('base64'),
133
+ decode: string => getPayload(_Buffer.from(string, 'base64'))
134
+ };
135
+ const base58 = {
136
+ encode: buffer => bs58.encode(addChecksum(buffer)),
137
+ decode: string => getPayload(bs58.decode(string))
138
+ };
139
+ /**
140
+ * Utility function to create and _id type
141
+ * @function
142
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
143
+ * @param {string} hashId Encoded hash
144
+ * @return {Buffer} Buffer Buffer with ID tag and decoded HASh
145
+ */
146
+
147
+ export function writeId(hashId) {
148
+ if (typeof hashId !== 'string') throw new ArgumentError('hashId', 'a string', hashId);
149
+ const prefix = hashId.slice(0, 2);
150
+ const idTag = PREFIX_ID_TAG[prefix];
151
+ if (!idTag) throw new TagNotFoundError(prefix);
152
+ return _Buffer.from([...toBytes(idTag), ...decode(hashId, prefix)]);
153
+ }
154
+ /**
155
+ * Utility function to read and _id type
156
+ * @function
157
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
158
+ * @param {Buffer} buf Data
159
+ * @return {String} Encoided hash string with prefix
160
+ */
161
+
162
+ export function readId(buf) {
163
+ const tag = _Buffer.from(buf).readUIntBE(0, 1);
164
+
165
+ const prefix = ID_TAG_PREFIX[tag];
166
+ if (!prefix) throw new PrefixNotFoundError(tag);
167
+ return encode(buf.slice(1, buf.length), prefix);
168
+ }
169
+ /**
170
+ * Utility function to convert int to bytes
171
+ * @function
172
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
173
+ * @param {Number|String|BigNumber} val Value
174
+ * @return {Buffer} Buffer Buffer from number(BigEndian)
175
+ */
176
+
177
+ export function writeInt(val) {
178
+ return toBytes(val, true);
179
+ }
180
+ /**
181
+ * Utility function to convert bytes to int
182
+ * @function
183
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
184
+ * @param {Buffer} buf Value
185
+ * @return {String} Buffer Buffer from number(BigEndian)
186
+ */
187
+
188
+ export function readInt() {
189
+ let buf = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _Buffer.from([]);
190
+ return new BigNumber(_Buffer.from(buf).toString('hex'), 16).toString(10);
191
+ }
192
+ /**
193
+ * Helper function to build pointers for name update TX
194
+ * @function
195
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
196
+ * @param {Array} pointers - Array of pointers
197
+ * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
198
+ * @return {Array} Serialized pointers array
199
+ */
200
+
201
+ export function buildPointers(pointers) {
202
+ return pointers.map(p => [toBytes(p.key), writeId(p.id)]);
203
+ }
204
+ /**
205
+ * Helper function to read pointers from name update TX
206
+ * @function
207
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
208
+ * @param {Array} pointers - Array of pointers
209
+ * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
210
+ * @return {Array} Deserialize pointer array
211
+ */
212
+
213
+ export function readPointers(pointers) {
214
+ return pointers.map(_ref => {
215
+ let [key, id] = _ref;
216
+ return Object.assign({
217
+ key: key.toString(),
218
+ id: readId(id)
219
+ });
220
+ });
221
+ }
222
+ const AENS_SUFFIX = '.chain';
223
+ /**
224
+ * Ensure that AENS name is valid
225
+ * @function
226
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
227
+ * @param {string} name
228
+ * @return void
229
+ * @throws Error
230
+ */
231
+
232
+ export function ensureNameValid(name) {
233
+ if (!name || typeof name !== 'string') throw new InvalidNameError('Name must be a string');
234
+ if (!name.endsWith(AENS_SUFFIX)) throw new InvalidNameError(`Name should end with ${AENS_SUFFIX}: ${name}`);
235
+ }
236
+ /**
237
+ * Is AENS name valid
238
+ * @function
239
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
240
+ * @param {string} name
241
+ * @return Boolean
242
+ */
243
+
244
+ export function isNameValid(name) {
245
+ try {
246
+ ensureNameValid(name);
247
+ return true;
248
+ } catch (error) {
249
+ return false;
250
+ }
251
+ }
252
+ /**
253
+ * @param identifier - account/oracle/contract address, or channel
254
+ * @returns {String} default AENS pointer key
255
+ * @throws exception when default key not defined
256
+ */
257
+
258
+ export function getDefaultPointerKey(identifier) {
259
+ decode(identifier);
260
+ const prefix = identifier.substr(0, 2);
261
+ return POINTER_KEY_BY_PREFIX[prefix] || (() => {
262
+ throw new NoDefaultAensPointerError(prefix);
263
+ })();
264
+ }
265
+ /**
266
+ * Get the minimum AENS name fee
267
+ * @function
268
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
269
+ * @param {String} name the AENS name to get the fee for
270
+ * @return {String} the minimum fee for the AENS name auction
271
+ */
272
+
273
+ export function getMinimumNameFee(name) {
274
+ ensureNameValid(name);
275
+ const nameLength = name.length - AENS_SUFFIX.length;
276
+ return NAME_BID_RANGES[Math.min(nameLength, NAME_MAX_LENGTH_FEE)];
277
+ }
278
+ /**
279
+ * Compute bid fee for AENS auction
280
+ * @function
281
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
282
+ * @param {String} name the AENS name to get the fee for
283
+ * @param {Number|String} startFee Auction start fee
284
+ * @param {Number} [increment=0.5] Bid multiplier(In percentage, must be between 0 and 1)
285
+ * @return {String} Bid fee
286
+ */
287
+
288
+ export function computeBidFee(name, startFee) {
289
+ let increment = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NAME_FEE_BID_INCREMENT;
290
+ if (!(Number(increment) === increment && increment % 1 !== 0)) throw new IllegalBidFeeError(`Increment must be float. Current increment ${increment}`);
291
+ if (increment < NAME_FEE_BID_INCREMENT) throw new IllegalBidFeeError(`minimum increment percentage is ${NAME_FEE_BID_INCREMENT}`);
292
+ return ceil(BigNumber(startFee !== null && startFee !== void 0 ? startFee : getMinimumNameFee(name)).times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1)));
293
+ }
294
+ /**
295
+ * Compute auction end height
296
+ * @function
297
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
298
+ * @param {String} name
299
+ * @param {Number|String} claimHeight Auction starting height
300
+ * @return {String} Auction end height
301
+ */
302
+
303
+ export function computeAuctionEndBlock(name, claimHeight) {
304
+ ensureNameValid(name);
305
+ const length = name.length - AENS_SUFFIX.length;
306
+ const h = length <= 4 && NAME_BID_TIMEOUTS[4] || length <= 8 && NAME_BID_TIMEOUTS[8] || length <= 12 && NAME_BID_TIMEOUTS[12] || NAME_BID_TIMEOUTS[13];
307
+ return h.plus(claimHeight).toString(10);
308
+ }
309
+ /**
310
+ * Is name accept going to auction
311
+ * @function
312
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
313
+ * @param {String} name
314
+ * @return {Boolean}
315
+ */
316
+
317
+ export function isAuctionName(name) {
318
+ ensureNameValid(name);
319
+ return name.length < 13 + AENS_SUFFIX.length;
320
+ }
321
+ //# sourceMappingURL=helpers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/tx/builder/helpers.js"],"names":["BigNumber","hash","salt","encode","decode","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","TagNotFoundError","PrefixNotFoundError","InvalidNameError","IllegalBidFeeError","NoDefaultAensPointerError","ArgumentError","createSalt","buildContractId","ownerId","nonce","ownerIdAndNonce","from","b2bHash","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","PayloadLengthError","getChecksum","payload","sha256hash","slice","addChecksum","input","getPayload","buffer","equals","InvalidChecksumError","base64","string","base58","bs58","writeId","hashId","prefix","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,gBAA2D,MAA3D;;;;;SACSC,I,EAAMC,I;SACNC,M,EAAQC,M;SACRC,O;SAEPC,a,EACAC,a,EACAC,e,EACAC,sB,EACAC,iB,EACAC,mB,EACAC,qB;SAEOC,I;SAEPC,gB,EACAC,mB,EACAC,gB,EACAC,kB,EACAC,yB,EACAC,a;AAGF;AACA;AACA;AACA;AACA;AACA;;AAEA,OAAO,MAAMC,UAAU,GAAGlB,IAAnB;AAEP,SAASC,MAAT,EAAiBC,MAAjB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASiB,eAAT,CAA0BC,OAA1B,EAAmCC,KAAnC,EAA0C;AAC/C,QAAMC,eAAe,GAAG,QAAOC,IAAP,CAAY,CAAC,GAAGrB,MAAM,CAACkB,OAAD,EAAU,IAAV,CAAV,EAA2B,GAAGjB,OAAO,CAACkB,KAAD,CAArC,CAAZ,CAAxB;;AACA,QAAMG,OAAO,GAAGzB,IAAI,CAACuB,eAAD,CAApB;AACA,SAAOrB,MAAM,CAACuB,OAAD,EAAU,IAAV,CAAb;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,aAAT,CAAwBC,QAAxB,EAAkCL,KAAlC,EAAyCM,QAAzC,EAAmD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uE,CAAC,GAAIvE,MAAM,IAAI,CAAV,IAAezB,iBAAiB,CAAC,CAAD,CAAjC,IACPyB,MAAM,IAAI,CAAV,IAAezB,iBAAiB,CAAC,CAAD,CADzB,IAEPyB,MAAM,IAAI,EAAV,IAAgBzB,iBAAiB,CAAC,EAAD,CAF1B,IAGRA,iBAAiB,CAAC,EAAD,CAHnB;AAIA,SAAOgG,CAAC,CAACH,IAAF,CAAOE,WAAP,EAAoBpE,QAApB,CAA6B,EAA7B,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASsE,aAAT,CAAwBnE,IAAxB,EAA8B;AACnCC,EAAAA,eAAe,CAACD,IAAD,CAAf;AACA,SAAOA,IAAI,CAACL,MAAL,GAAc,KAAKoD,WAAW,CAACpD,MAAtC;AACD","sourcesContent":["import BigNumber from 'bignumber.js'\nimport { encode as bs58Encode, decode as bs58Decode } from 'bs58'\nimport { hash, salt } from '../../utils/crypto'\nimport { encode, decode } from '../../utils/encoder'\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 './constants'\nimport { ceil } from '../../utils/bignumber'\nimport {\n TagNotFoundError,\n PrefixNotFoundError,\n InvalidNameError,\n IllegalBidFeeError,\n NoDefaultAensPointerError,\n ArgumentError\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\nexport { encode, decode }\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 * 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"}