@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,283 @@
1
+ import _buffer from "buffer";
2
+ const {
3
+ Buffer: _Buffer
4
+ } = _buffer;
5
+
6
+ /**
7
+ * TxObject module
8
+ * @module @aeternity/aepp-sdk/es/tx/tx-object
9
+ * @export TxObject
10
+ * @example import TxObject from '@aeternity/aepp-sdk/es/tx/tx-object'
11
+ */
12
+ import stampit from '@stamp/it';
13
+ import { buildTx, calculateFee, unpackTx } from "./builder/index.mjs";
14
+ import { TX_TYPE } from "./builder/schema.mjs";
15
+ import { encode } from "./builder/helpers.mjs";
16
+ import { isHex } from "../utils/string.mjs";
17
+ import { InvalidTxError, TypeError, UnknownTxError, InvalidSignatureError } from "../utils/errors.mjs";
18
+ /**
19
+ * Build transaction from object
20
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
21
+ * @param {String} type Transaction type
22
+ * @param {Object} params Transaction params
23
+ * @param {Object} [options={}] Options
24
+ * @throws {Error} Arguments validation error's
25
+ * @return {{
26
+ * encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, params: Object, type: String
27
+ * }}
28
+ */
29
+
30
+ const buildTransaction = function (type, params) {
31
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
32
+ if (typeof params !== 'object') throw new TypeError('tx "params" should be an object');
33
+ if (typeof type !== 'string' || !Object.values(TX_TYPE).includes(type)) throw new UnknownTxError(`Unknown transaction type ${type}`);
34
+ const fee = calculateFee(params.fee, type, {
35
+ gas: params.gas,
36
+ params,
37
+ vsn: params.vsn
38
+ });
39
+ const {
40
+ rlpEncoded,
41
+ binary,
42
+ tx: encodedTx,
43
+ txObject
44
+ } = buildTx({ ...params,
45
+ fee
46
+ }, type, {
47
+ vsn: params.vsn,
48
+ ...options
49
+ });
50
+ return {
51
+ rlpEncoded,
52
+ binary,
53
+ encodedTx,
54
+ params: txObject,
55
+ type
56
+ };
57
+ };
58
+ /**
59
+ * Unpack transaction from RLP encoded binary or base64c string
60
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
61
+ * @param {Uint8Array|String} tx RLP encoded binary or base64c(rlpBinary) string
62
+ * @throws {Error} Arguments validation error's
63
+ * @return {{
64
+ * encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object
65
+ * }}
66
+ */
67
+
68
+
69
+ const unpackTransaction = tx => {
70
+ if (!tx) throw new InvalidTxError(`Invalid transaction: ${tx}`);
71
+
72
+ if (typeof tx === 'string') {
73
+ const {
74
+ txType: type,
75
+ tx: params,
76
+ rlpEncoded,
77
+ binary
78
+ } = unpackTx(tx);
79
+ return {
80
+ encodedTx: tx,
81
+ type,
82
+ params,
83
+ rlpEncoded,
84
+ binary
85
+ };
86
+ }
87
+
88
+ if (tx instanceof Uint8Array) {
89
+ const {
90
+ txType: type,
91
+ tx: params,
92
+ rlpEncoded,
93
+ binary
94
+ } = unpackTx(tx, true);
95
+ return {
96
+ encodedTx: encode(tx, 'tx'),
97
+ type,
98
+ params,
99
+ rlpEncoded,
100
+ binary
101
+ };
102
+ }
103
+
104
+ throw new InvalidTxError(`"tx" should be a string or Uint8Array, got ${tx} instead`);
105
+ };
106
+ /**
107
+ * Helper which build or unpack transaction base on constructor arguments
108
+ * Need to provide one of arguments: [tx] -> unpack flow or [params, type] -> build flow
109
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
110
+ * @param {Buffer|String} [tx] Transaction rlp binary or vase64c string
111
+ * @param {Object} params Transaction params
112
+ * @param {String} type Transaction type
113
+ * @param {Object} [options={}] Options
114
+ * @throws {Error} Arguments validation error's
115
+ * @return {{
116
+ * encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object
117
+ * }}
118
+ */
119
+
120
+
121
+ const initTransaction = function () {
122
+ let {
123
+ tx,
124
+ params,
125
+ type,
126
+ options = {}
127
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
128
+ if (params && type) return buildTransaction(type, params, options);
129
+ if (tx) return unpackTransaction(tx);
130
+ throw new InvalidTxError('Invalid TxObject arguments. Please provide one of { tx: "tx_asdasd23..." } or { type: "spendTx", params: {...} }');
131
+ };
132
+ /**
133
+ * Transaction Validator Stamp
134
+ * This stamp give us possibility to unpack and validate some of transaction properties,
135
+ * to make sure we can post it to the chain
136
+ * @function
137
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
138
+ * @rtype Stamp
139
+ * @param {Object} [options={}] - Initializer object
140
+ * @param {Buffer|String} [options.tx] - Rlp binary or base64c transaction
141
+ * @param {Object} [options.params] - Transaction params
142
+ * @param {String} [options.type] - Transaction type
143
+ * @param {Object} [options.options] - Build options
144
+ * @return {Object} TxObject instance
145
+ * @example TxObject({ params: {...}, type: 'spendTx' })
146
+ */
147
+
148
+
149
+ export const TxObject = stampit({
150
+ init() {
151
+ let {
152
+ tx,
153
+ params,
154
+ type,
155
+ options = {}
156
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
157
+ this.options = options;
158
+ this.signatures = [];
159
+ Object.assign(this, initTransaction({
160
+ tx,
161
+ params,
162
+ type,
163
+ options
164
+ }));
165
+
166
+ if (this.type === TX_TYPE.signed) {
167
+ const {
168
+ signatures,
169
+ encodedTx: {
170
+ txType,
171
+ tx
172
+ }
173
+ } = this.params;
174
+ this.signatures = signatures;
175
+ this.params = tx;
176
+ this.type = txType;
177
+ this.isSigned = true;
178
+ }
179
+ },
180
+
181
+ statics: {
182
+ /**
183
+ * Create txObject from base64c RLP encoded transaction string with 'tx_' prefix
184
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
185
+ * @static
186
+ * @param {String} tx Transaction string (tx_23fsdgsdfg...)
187
+ * @return {TxObject}
188
+ */
189
+ fromString: tx => TxObject({
190
+ tx
191
+ }),
192
+
193
+ /**
194
+ * Create txObject from transaction RLP binary
195
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
196
+ * @static
197
+ * @param {Buffer} tx Transaction RLP binary
198
+ * @return {TxObject}
199
+ */
200
+ fromRlp: tx => TxObject({
201
+ tx
202
+ })
203
+ },
204
+ methods: {
205
+ /**
206
+ * Rebuild transaction with new params and recalculate fee
207
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
208
+ * @param {Object} props Transaction properties for update
209
+ * @param options
210
+ * @return {TxObject}
211
+ */
212
+ setProp() {
213
+ let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
214
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
215
+ if (typeof props !== 'object') throw new TypeError('Props should be an object');
216
+ this.isSigned = false;
217
+ this.signatures = [];
218
+ Object.assign(this, buildTransaction(this.type, { ...this.params,
219
+ ...props,
220
+ fee: null
221
+ }, { ...this.options,
222
+ ...options
223
+ }));
224
+ return this;
225
+ },
226
+
227
+ /**
228
+ * Get signatures
229
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
230
+ * @return {Array} Array of signatures
231
+ */
232
+ getSignatures() {
233
+ return this.signatures;
234
+ },
235
+
236
+ /**
237
+ * Add signature
238
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
239
+ * @param {Buffer|String} signature Signature to add ( Can be: Buffer | Uint8Array | HexString )
240
+ * @return {void}
241
+ */
242
+ addSignature(signature) {
243
+ signature = isHex(signature) ? _Buffer.from(signature, 'hex') : signature;
244
+ if (!_Buffer.isBuffer(signature) && !(signature instanceof Uint8Array)) throw new InvalidSignatureError('Invalid signature, signature must be of type Buffer or Uint8Array');
245
+ Object.assign(this, buildTransaction(TX_TYPE.signed, {
246
+ encodedTx: this.rlpEncoded,
247
+ signatures: [[...this.signatures, signature]]
248
+ }));
249
+ const {
250
+ signatures,
251
+ encodedTx: {
252
+ txType,
253
+ tx
254
+ }
255
+ } = this.params;
256
+ this.signatures = signatures;
257
+ this.params = tx;
258
+ this.type = txType;
259
+ this.isSigned = true;
260
+ },
261
+
262
+ /**
263
+ * Calculate fee
264
+ * @alias module:@aeternity/aepp-sdk/es/tx/tx-object
265
+ * @param {Object} props
266
+ * @return {String} fee
267
+ */
268
+ calculateMinFee() {
269
+ let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
270
+ const params = { ...this.params,
271
+ ...props
272
+ };
273
+ return calculateFee(0, this.type, {
274
+ gas: params.gas,
275
+ params,
276
+ vsn: params.vsn
277
+ });
278
+ }
279
+
280
+ }
281
+ });
282
+ export default TxObject;
283
+ //# sourceMappingURL=tx-object.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/tx/tx-object.js"],"names":["stampit","buildTx","calculateFee","unpackTx","TX_TYPE","encode","isHex","InvalidTxError","TypeError","UnknownTxError","InvalidSignatureError","buildTransaction","type","params","options","Object","values","includes","fee","gas","vsn","rlpEncoded","binary","tx","encodedTx","txObject","unpackTransaction","txType","Uint8Array","initTransaction","TxObject","init","signatures","assign","signed","isSigned","statics","fromString","fromRlp","methods","setProp","props","getSignatures","addSignature","signature","from","isBuffer","calculateMinFee"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,OAAP,MAAoB,WAApB;SACSC,O,EAASC,Y,EAAcC,Q;SACvBC,O;SACAC,M;SACAC,K;SACAC,c,EAAgBC,S,EAAWC,c,EAAgBC,qB;AAEpD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAMC,gBAAgB,GAAG,UAACC,IAAD,EAAOC,MAAP,EAAgC;AAAA,MAAjBC,OAAiB,uEAAP,EAAO;AACvD,MAAI,OAAOD,MAAP,KAAkB,QAAtB,EAAgC,MAAM,IAAIL,SAAJ,CAAc,i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Q,QAAL,GAAgB,IAAhB;AACD,KAlDM;;AAmDP;AACJ;AACA;AACA;AACA;AACA;AACIY,IAAAA,eAAe,GAAc;AAAA,UAAZN,KAAY,uEAAJ,EAAI;AAC3B,YAAM5B,MAAM,GAAG,EAAE,GAAG,KAAKA,MAAV;AAAkB,WAAG4B;AAArB,OAAf;AACA,aAAOvC,YAAY,CAAC,CAAD,EAAI,KAAKU,IAAT,EAAe;AAAEO,QAAAA,GAAG,EAAEN,MAAM,CAACM,GAAd;AAAmBN,QAAAA,MAAnB;AAA2BO,QAAAA,GAAG,EAAEP,MAAM,CAACO;AAAvC,OAAf,CAAnB;AACD;;AA5DM;AAhCqB,CAAD,CAAxB;AAgGP,eAAeU,QAAf","sourcesContent":["/**\n * TxObject module\n * @module @aeternity/aepp-sdk/es/tx/tx-object\n * @export TxObject\n * @example import TxObject from '@aeternity/aepp-sdk/es/tx/tx-object'\n */\nimport stampit from '@stamp/it'\nimport { buildTx, calculateFee, unpackTx } from './builder'\nimport { TX_TYPE } from './builder/schema'\nimport { encode } from './builder/helpers'\nimport { isHex } from '../utils/string'\nimport { InvalidTxError, TypeError, UnknownTxError, InvalidSignatureError } from '../utils/errors'\n\n/**\n * Build transaction from object\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @param {String} type Transaction type\n * @param {Object} params Transaction params\n * @param {Object} [options={}] Options\n * @throws {Error} Arguments validation error's\n * @return {{\n * encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, params: Object, type: String\n * }}\n */\nconst buildTransaction = (type, params, options = {}) => {\n if (typeof params !== 'object') throw new TypeError('tx \"params\" should be an object')\n if (typeof type !== 'string' || !Object.values(TX_TYPE).includes(type)) throw new UnknownTxError(`Unknown transaction type ${type}`)\n const fee = calculateFee(params.fee, type, { gas: params.gas, params, vsn: params.vsn })\n const { rlpEncoded, binary, tx: encodedTx, txObject } = buildTx(\n { ...params, fee }, type, { vsn: params.vsn, ...options }\n )\n return { rlpEncoded, binary, encodedTx, params: txObject, type }\n}\n\n/**\n * Unpack transaction from RLP encoded binary or base64c string\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @param {Uint8Array|String} tx RLP encoded binary or base64c(rlpBinary) string\n * @throws {Error} Arguments validation error's\n * @return {{\n * encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object\n * }}\n */\nconst unpackTransaction = (tx) => {\n if (!tx) throw new InvalidTxError(`Invalid transaction: ${tx}`)\n if (typeof tx === 'string') {\n const { txType: type, tx: params, rlpEncoded, binary } = unpackTx(tx)\n return { encodedTx: tx, type, params, rlpEncoded, binary }\n }\n if (tx instanceof Uint8Array) {\n const { txType: type, tx: params, rlpEncoded, binary } = unpackTx(tx, true)\n return { encodedTx: encode(tx, 'tx'), type, params, rlpEncoded, binary }\n }\n throw new InvalidTxError(`\"tx\" should be a string or Uint8Array, got ${tx} instead`)\n}\n\n/**\n * Helper which build or unpack transaction base on constructor arguments\n * Need to provide one of arguments: [tx] -> unpack flow or [params, type] -> build flow\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @param {Buffer|String} [tx] Transaction rlp binary or vase64c string\n * @param {Object} params Transaction params\n * @param {String} type Transaction type\n * @param {Object} [options={}] Options\n * @throws {Error} Arguments validation error's\n * @return {{\n * encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object\n * }}\n */\nconst initTransaction = ({ tx, params, type, options = {} } = {}) => {\n if (params && type) return buildTransaction(type, params, options)\n if (tx) return unpackTransaction(tx)\n throw new InvalidTxError('Invalid TxObject arguments. Please provide one of { tx: \"tx_asdasd23...\" } or { type: \"spendTx\", params: {...} }')\n}\n\n/**\n * Transaction Validator Stamp\n * This stamp give us possibility to unpack and validate some of transaction properties,\n * to make sure we can post it to the chain\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @rtype Stamp\n * @param {Object} [options={}] - Initializer object\n * @param {Buffer|String} [options.tx] - Rlp binary or base64c transaction\n * @param {Object} [options.params] - Transaction params\n * @param {String} [options.type] - Transaction type\n * @param {Object} [options.options] - Build options\n * @return {Object} TxObject instance\n * @example TxObject({ params: {...}, type: 'spendTx' })\n */\nexport const TxObject = stampit({\n init ({ tx, params, type, options = {} } = {}) {\n this.options = options\n this.signatures = []\n Object.assign(this, initTransaction({ tx, params, type, options }))\n\n if (this.type === TX_TYPE.signed) {\n const { signatures, encodedTx: { txType, tx } } = this.params\n this.signatures = signatures\n this.params = tx\n this.type = txType\n this.isSigned = true\n }\n },\n statics: {\n /**\n * Create txObject from base64c RLP encoded transaction string with 'tx_' prefix\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @static\n * @param {String} tx Transaction string (tx_23fsdgsdfg...)\n * @return {TxObject}\n */\n fromString: (tx) => TxObject({ tx }),\n /**\n * Create txObject from transaction RLP binary\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @static\n * @param {Buffer} tx Transaction RLP binary\n * @return {TxObject}\n */\n fromRlp: (tx) => TxObject({ tx })\n },\n methods: {\n /**\n * Rebuild transaction with new params and recalculate fee\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @param {Object} props Transaction properties for update\n * @param options\n * @return {TxObject}\n */\n setProp (props = {}, options = {}) {\n if (typeof props !== 'object') throw new TypeError('Props should be an object')\n this.isSigned = false\n this.signatures = []\n Object.assign(\n this,\n buildTransaction(\n this.type, { ...this.params, ...props, fee: null }, { ...this.options, ...options }\n )\n )\n return this\n },\n /**\n * Get signatures\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @return {Array} Array of signatures\n */\n getSignatures () {\n return this.signatures\n },\n /**\n * Add signature\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @param {Buffer|String} signature Signature to add ( Can be: Buffer | Uint8Array | HexString )\n * @return {void}\n */\n addSignature (signature) {\n signature = isHex(signature) ? Buffer.from(signature, 'hex') : signature\n if (!Buffer.isBuffer(signature) && !(signature instanceof Uint8Array)) throw new InvalidSignatureError('Invalid signature, signature must be of type Buffer or Uint8Array')\n Object.assign(\n this,\n buildTransaction(\n TX_TYPE.signed,\n { encodedTx: this.rlpEncoded, signatures: [[...this.signatures, signature]] }\n )\n )\n\n const { signatures, encodedTx: { txType, tx } } = this.params\n this.signatures = signatures\n this.params = tx\n this.type = txType\n this.isSigned = true\n },\n /**\n * Calculate fee\n * @alias module:@aeternity/aepp-sdk/es/tx/tx-object\n * @param {Object} props\n * @return {String} fee\n */\n calculateMinFee (props = {}) {\n const params = { ...this.params, ...props }\n return calculateFee(0, this.type, { gas: params.gas, params, vsn: params.vsn })\n }\n }\n})\n\nexport default TxObject\n"],"file":"tx-object.mjs"}