@aeternity/aepp-sdk 8.2.1 → 10.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 (159) hide show
  1. package/CHANGELOG.md +216 -337
  2. package/README.md +16 -9
  3. package/dist/aepp-sdk.browser-script.js +1 -1
  4. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -9
  5. package/dist/aepp-sdk.browser-script.js.map +1 -1
  6. package/dist/aepp-sdk.browser.js +1 -1
  7. package/dist/aepp-sdk.browser.js.map +1 -1
  8. package/dist/aepp-sdk.js +1 -1
  9. package/dist/aepp-sdk.js.map +1 -1
  10. package/es/account/base.js +36 -120
  11. package/es/account/base.js.map +1 -1
  12. package/es/account/memory.js +17 -19
  13. package/es/account/memory.js.map +1 -1
  14. package/es/account/multiple.js +79 -156
  15. package/es/account/multiple.js.map +1 -1
  16. package/es/ae/aens.js +205 -626
  17. package/es/ae/aens.js.map +1 -1
  18. package/es/ae/contract.js +114 -918
  19. package/es/ae/contract.js.map +1 -1
  20. package/es/ae/index.js +90 -250
  21. package/es/ae/index.js.map +1 -1
  22. package/es/ae/oracle.js +136 -504
  23. package/es/ae/oracle.js.map +1 -1
  24. package/es/ae/universal.js.map +1 -1
  25. package/es/chain/index.js +12 -27
  26. package/es/chain/index.js.map +1 -1
  27. package/es/chain/node.js +289 -1020
  28. package/es/chain/node.js.map +1 -1
  29. package/es/channel/handlers.js +597 -1086
  30. package/es/channel/handlers.js.map +1 -1
  31. package/es/channel/index.js +238 -465
  32. package/es/channel/index.js.map +1 -1
  33. package/es/channel/internal.js +129 -301
  34. package/es/channel/internal.js.map +1 -1
  35. package/es/contract/aci/index.js +328 -495
  36. package/es/contract/aci/index.js.map +1 -1
  37. package/es/contract/aci/transformation.js +35 -711
  38. package/es/contract/aci/transformation.js.map +1 -1
  39. package/es/contract/compiler.js +131 -203
  40. package/es/contract/compiler.js.map +1 -1
  41. package/es/contract/ga/helpers.js +17 -122
  42. package/es/contract/ga/helpers.js.map +1 -1
  43. package/es/contract/ga/index.js +108 -249
  44. package/es/contract/ga/index.js.map +1 -1
  45. package/es/contract/index.js +7 -3
  46. package/es/contract/index.js.map +1 -1
  47. package/es/index.js +3 -3
  48. package/es/index.js.map +1 -1
  49. package/es/node-pool/index.js +57 -74
  50. package/es/node-pool/index.js.map +1 -1
  51. package/es/node.js +65 -161
  52. package/es/node.js.map +1 -1
  53. package/es/other.d.ts +1 -1
  54. package/es/tx/builder/helpers.js +62 -115
  55. package/es/tx/builder/helpers.js.map +1 -1
  56. package/es/tx/builder/index.js +218 -309
  57. package/es/tx/builder/index.js.map +1 -1
  58. package/es/tx/builder/schema.js +586 -573
  59. package/es/tx/builder/schema.js.map +1 -1
  60. package/es/tx/index.js +102 -13
  61. package/es/tx/index.js.map +1 -1
  62. package/es/tx/tx-object.js +114 -138
  63. package/es/tx/tx-object.js.map +1 -1
  64. package/es/tx/tx.js +810 -1586
  65. package/es/tx/tx.js.map +1 -1
  66. package/es/tx/validator.js +191 -426
  67. package/es/tx/validator.js.map +1 -1
  68. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +20 -23
  69. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +1 -1
  70. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +53 -80
  71. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +1 -1
  72. package/es/utils/aepp-wallet-communication/connection/index.js +2 -1
  73. package/es/utils/aepp-wallet-communication/connection/index.js.map +1 -1
  74. package/es/utils/aepp-wallet-communication/content-script-bridge.js +24 -19
  75. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +1 -1
  76. package/es/utils/aepp-wallet-communication/helpers.js +60 -105
  77. package/es/utils/aepp-wallet-communication/helpers.js.map +1 -1
  78. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +191 -514
  79. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +1 -1
  80. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +78 -93
  81. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +1 -1
  82. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +381 -613
  83. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +1 -1
  84. package/es/utils/aepp-wallet-communication/schema.js +43 -39
  85. package/es/utils/aepp-wallet-communication/schema.js.map +1 -1
  86. package/es/utils/aepp-wallet-communication/wallet-detector.js +32 -58
  87. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +1 -1
  88. package/es/utils/amount-formatter.js +41 -61
  89. package/es/utils/amount-formatter.js.map +1 -1
  90. package/es/utils/async-init.js +28 -64
  91. package/es/utils/async-init.js.map +1 -1
  92. package/es/utils/bignumber.js +3 -11
  93. package/es/utils/bignumber.js.map +1 -1
  94. package/es/utils/bytes.js +5 -9
  95. package/es/utils/bytes.js.map +1 -1
  96. package/es/utils/crypto.js +41 -106
  97. package/es/utils/crypto.js.map +1 -1
  98. package/es/utils/hd-wallet.js +45 -81
  99. package/es/utils/hd-wallet.js.map +1 -1
  100. package/es/utils/json-big.js +16 -49
  101. package/es/utils/json-big.js.map +1 -1
  102. package/es/utils/keystore.js +81 -246
  103. package/es/utils/keystore.js.map +1 -1
  104. package/es/utils/mptree.js +88 -134
  105. package/es/utils/mptree.js.map +1 -1
  106. package/es/utils/other.js +10 -60
  107. package/es/utils/other.js.map +1 -1
  108. package/es/utils/semver-satisfies.js +9 -28
  109. package/es/utils/semver-satisfies.js.map +1 -1
  110. package/es/utils/string.js +4 -13
  111. package/es/utils/string.js.map +1 -1
  112. package/es/utils/swagger.js +87 -224
  113. package/es/utils/swagger.js.map +1 -1
  114. package/package.json +51 -44
  115. package/src/account/base.js +11 -10
  116. package/src/account/multiple.js +24 -18
  117. package/src/ae/aens.js +64 -61
  118. package/src/ae/contract.js +71 -248
  119. package/src/ae/index.js +34 -9
  120. package/src/ae/oracle.js +30 -22
  121. package/src/ae/universal.js +3 -1
  122. package/src/chain/index.js +10 -16
  123. package/src/chain/node.js +38 -38
  124. package/src/channel/handlers.js +10 -4
  125. package/src/channel/index.js +44 -20
  126. package/src/contract/aci/index.js +234 -112
  127. package/src/contract/aci/transformation.js +30 -422
  128. package/src/contract/compiler.js +50 -5
  129. package/src/contract/ga/helpers.js +6 -14
  130. package/src/contract/ga/index.js +28 -28
  131. package/src/contract/index.js +6 -2
  132. package/src/index.js +4 -4
  133. package/src/node-pool/index.js +24 -13
  134. package/src/node.js +29 -30
  135. package/src/tx/builder/helpers.js +31 -56
  136. package/src/tx/builder/index.js +67 -28
  137. package/src/tx/builder/schema.js +79 -121
  138. package/src/tx/index.js +101 -12
  139. package/src/tx/tx-object.js +25 -8
  140. package/src/tx/tx.js +229 -150
  141. package/src/tx/validator.js +134 -202
  142. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +3 -2
  143. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +25 -7
  144. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  145. package/src/utils/aepp-wallet-communication/content-script-bridge.js +10 -4
  146. package/src/utils/aepp-wallet-communication/helpers.js +4 -2
  147. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +33 -12
  148. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +9 -5
  149. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +59 -29
  150. package/src/utils/aepp-wallet-communication/schema.js +1 -3
  151. package/src/utils/aepp-wallet-communication/wallet-detector.js +4 -1
  152. package/src/utils/amount-formatter.ts +1 -1
  153. package/src/utils/bignumber.ts +2 -1
  154. package/src/utils/crypto.js +4 -68
  155. package/src/utils/keystore.js +10 -2
  156. package/src/utils/other.ts +1 -1
  157. package/src/utils/semver-satisfies.ts +1 -1
  158. package/src/utils/swagger.js +22 -10
  159. package/src/contract/aci/helpers.js +0 -140
@@ -1,742 +1,66 @@
1
- import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
2
- import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
3
- import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
4
- import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
5
- import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
6
- import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
7
- import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
8
- import _typeof from "@babel/runtime-corejs3/helpers/typeof";
9
- import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
10
- import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray";
11
- import _toArray from "@babel/runtime-corejs3/helpers/toArray";
12
- import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
13
-
14
- var _context;
15
-
16
- function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
17
-
18
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context29; _forEachInstanceProperty(_context29 = ownKeys(Object(source), true)).call(_context29, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context30; _forEachInstanceProperty(_context30 = ownKeys(Object(source))).call(_context30, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
19
-
20
- import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
21
- import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
22
- import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
23
- import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
24
- import _Object$entries3 from "@babel/runtime-corejs3/core-js-stable/object/entries";
25
- import _indexOfInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/index-of";
26
- import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
27
- import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
28
- import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
29
- import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values";
30
- import _Array$isArray from "@babel/runtime-corejs3/core-js-stable/array/is-array";
31
- import _entriesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/entries";
32
- import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
33
- import _JSON$stringify from "@babel/runtime-corejs3/core-js-stable/json/stringify";
34
- import Joi from 'joi-browser';
35
- import { isHex } from '../../utils/string';
36
1
  import { toBytes } from '../../utils/bytes';
37
2
  import { decode } from '../../tx/builder/helpers';
38
3
  import { parseBigNumber } from '../../utils/bignumber';
39
- import { isAddressValid, addressFromDecimal, hash } from '../../utils/crypto';
40
- export var SOPHIA_TYPES = _reduceInstanceProperty(_context = ['int', 'string', 'tuple', 'address', 'bool', 'list', 'map', 'record', 'option', 'oracle', 'oracleQuery', 'hash', 'signature', 'bytes', 'variant']).call(_context, function (acc, type) {
41
- return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, type, type));
42
- }, {
4
+ import { addressFromDecimal, hash } from '../../utils/crypto';
5
+ export const SOPHIA_TYPES = ['int', 'string', 'tuple', 'address', 'bool', 'list', 'map', 'record', 'option', 'oracle', 'oracleQuery', 'hash', 'signature', 'bytes', 'variant'].reduce((acc, type) => ({ ...acc,
6
+ [type]: type
7
+ }), {
43
8
  ChainTtl: 'Chain.ttl'
44
9
  });
45
10
  /**
46
- * Transform decoded event to JS type
47
- * @param {Object[]} events Array of events
48
- * @param {Object} [options={}] Options
49
- * @param {Object} [options.schema=[]] SC function ACI schema
11
+ * Decode events fields
12
+ * @param {Object[]} events Array of events with encoded fields
13
+ * @param {Array} schemas Smart contract event ACI schemas
50
14
  * @return {Object}
51
15
  */
52
16
 
53
- export function decodeEvents(events) {
54
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
55
- schema: []
56
- };
57
- if (!events.length) return [];
58
- return _mapInstanceProperty(events).call(events, function (l) {
59
- var _context2;
60
-
61
- var _l$topics = _toArray(l.topics),
62
- eName = _l$topics[0],
63
- eParams = _sliceInstanceProperty(_l$topics).call(_l$topics, 1);
64
-
65
- var hexHash = toBytes(eName, true).toString('hex');
66
-
67
- var _options$schema$reduc = _reduceInstanceProperty(_context2 = options.schema).call(_context2, function (acc, el) {
68
- if (hash(el.name).toString('hex') === hexHash) {
69
- l.name = el.name;
70
- return {
71
- schema: el.types,
72
- name: el.name
73
- };
74
- }
75
-
76
- return acc;
77
- }, {
78
- schema: []
79
- }),
80
- schema = _options$schema$reduc.schema;
81
-
82
- var _schema$reduce = _reduceInstanceProperty(schema).call(schema, function (acc, el) {
83
- var _context4;
84
-
85
- if (el === SOPHIA_TYPES.string) {
86
- var _context3;
87
-
88
- return {
89
- decoded: _concatInstanceProperty(_context3 = []).call(_context3, _toConsumableArray(acc.decoded), [transformEvent(l.data, el)]),
90
- params: acc.params
91
- };
92
- }
93
-
94
- var _acc$params = _toArray(acc.params),
95
- event = _acc$params[0],
96
- tail = _sliceInstanceProperty(_acc$params).call(_acc$params, 1);
97
-
98
- return {
99
- decoded: _concatInstanceProperty(_context4 = []).call(_context4, _toConsumableArray(acc.decoded), [transformEvent(event, el)]),
100
- params: tail
101
- };
102
- }, {
103
- decoded: [],
104
- params: eParams
105
- }),
106
- decoded = _schema$reduce.decoded;
17
+ export const decodeEvents = function (events) {
18
+ let schemas = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
19
+ return events.reduce((acc, event) => {
20
+ const [nameHash, ...params] = event.topics;
21
+ const schema = schemas.find(s => hash(s.name).equals(toBytes(nameHash, true)));
22
+ if (!schema) return acc;
23
+ const stringCount = schema.types.filter(t => t === SOPHIA_TYPES.string).length;
24
+ if (stringCount > 1) throw new Error(`Event schema contains more than one string: ${schema.types}`);
25
+ const topicsCount = schema.types.length - stringCount;
26
+
27
+ if (topicsCount !== params.length) {
28
+ throw new Error(`Schema defines ${topicsCount} types, but ${params.length} topics present`);
29
+ }
107
30
 
108
- return _objectSpread(_objectSpread({}, l), {}, {
109
- decoded: decoded
31
+ acc.push({ ...event,
32
+ name: schema.name,
33
+ decoded: schema.types.map(type => decodeEventField(type === SOPHIA_TYPES.string ? event.data : params.shift(), type))
110
34
  });
111
- });
112
- }
35
+ return acc;
36
+ }, []);
37
+ };
113
38
  /**
114
39
  * Transform Event based on type
115
- * @param {String|Number} event Event data
40
+ * @param {String|Number} field Event data
116
41
  * @param {String} type Event type from schema
117
42
  * @return {*}
118
43
  */
119
44
 
120
- export function transformEvent(event, type) {
45
+ function decodeEventField(field, type) {
121
46
  switch (type) {
122
- case SOPHIA_TYPES["int"]:
123
- return parseBigNumber(event);
47
+ case SOPHIA_TYPES.int:
48
+ return parseBigNumber(field);
124
49
 
125
50
  case SOPHIA_TYPES.bool:
126
- return !!event;
51
+ return !!field;
127
52
 
128
53
  case SOPHIA_TYPES.hash:
129
- return toBytes(event, true).toString('hex');
54
+ return toBytes(field, true).toString('hex');
130
55
 
131
56
  case SOPHIA_TYPES.address:
132
- return addressFromDecimal(event).split('_')[1];
57
+ return addressFromDecimal(field);
133
58
 
134
59
  case SOPHIA_TYPES.string:
135
- return decode(event).toString('utf-8');
60
+ return decode(field).toString('utf-8');
136
61
 
137
62
  default:
138
- return toBytes(event, true);
139
- }
140
- }
141
- export function injectVars(t, aciType) {
142
- var _Object$entries = _Object$entries3(aciType.typedef),
143
- _Object$entries2 = _slicedToArray(_Object$entries, 1),
144
- _Object$entries2$ = _slicedToArray(_Object$entries2[0], 2),
145
- baseType = _Object$entries2$[0],
146
- generic = _Object$entries2$[1];
147
-
148
- var _Object$entries4 = _Object$entries3(t),
149
- _Object$entries5 = _slicedToArray(_Object$entries4, 1),
150
- _Object$entries5$ = _slicedToArray(_Object$entries5[0], 2),
151
- varianValue = _Object$entries5$[1];
152
-
153
- switch (baseType) {
154
- case SOPHIA_TYPES.variant:
155
- return _defineProperty({}, baseType, _mapInstanceProperty(generic).call(generic, function (el) {
156
- var _Object$entries$ = _slicedToArray(_Object$entries3(el)[0], 2),
157
- tag = _Object$entries$[0],
158
- gen = _Object$entries$[1];
159
-
160
- return _defineProperty({}, tag, _mapInstanceProperty(gen).call(gen, function (type) {
161
- var _context5, _context6;
162
-
163
- var index = _indexOfInstanceProperty(_context5 = _mapInstanceProperty(_context6 = aciType.vars).call(_context6, function (e) {
164
- return e.name;
165
- })).call(_context5, type);
166
-
167
- return index === -1 ? type : varianValue[index];
168
- }));
169
- }));
170
- }
171
- }
172
- /**
173
- * Ling Type Defs
174
- * @param t
175
- * @param bindings
176
- * @return {Object}
177
- */
178
-
179
- export function linkTypeDefs(t, bindings) {
180
- var _context7, _context8;
181
-
182
- var _ref3 = _typeof(t) === 'object' ? _Object$keys(t)[0].split('.') : t.split('.'),
183
- _ref4 = _slicedToArray(_ref3, 2),
184
- root = _ref4[0],
185
- typeDef = _ref4[1];
186
-
187
- var contractTypeDefs = _findInstanceProperty(bindings).call(bindings, function (c) {
188
- return c.name === root;
189
- });
190
-
191
- var aciType = _findInstanceProperty(_context7 = _concatInstanceProperty(_context8 = []).call(_context8, _toConsumableArray(contractTypeDefs.type_defs), [{
192
- name: 'state',
193
- typedef: contractTypeDefs.state,
194
- vars: []
195
- }])).call(_context7, function (_ref5) {
196
- var name = _ref5.name;
197
- return name === typeDef;
198
- });
199
-
200
- if (aciType.vars.length) {
201
- aciType.typedef = injectVars(t, aciType);
202
- }
203
-
204
- return isTypedDefOrState(aciType.typedef, bindings) ? linkTypeDefs(aciType.typedef, bindings) : aciType.typedef;
205
- }
206
-
207
- var isTypedDefOrState = function isTypedDefOrState(t, bindings) {
208
- var _context9, _context10;
209
-
210
- if (!_includesInstanceProperty(_context9 = ['string', 'object']).call(_context9, _typeof(t))) return false;
211
- t = _typeof(t) === 'object' ? _Object$keys(t)[0] : t;
212
-
213
- var _t$split = t.split('.'),
214
- _t$split2 = _toArray(_t$split),
215
- root = _t$split2[0],
216
- path = _sliceInstanceProperty(_t$split2).call(_t$split2, 1); // Remote Contract Address
217
-
218
-
219
- if (!path.length) return false;
220
- return _includesInstanceProperty(_context10 = _mapInstanceProperty(bindings).call(bindings, function (c) {
221
- return c.name;
222
- })).call(_context10, root);
223
- };
224
-
225
- var isRemoteAddress = function isRemoteAddress(t) {
226
- var _context11;
227
-
228
- if (typeof t !== 'string') return false;
229
-
230
- var _t$split3 = t.split('.'),
231
- _t$split4 = _toArray(_t$split3),
232
- root = _t$split4[0],
233
- path = _sliceInstanceProperty(_t$split4).call(_t$split4, 1);
234
-
235
- return !path.length && !_includesInstanceProperty(_context11 = _Object$values(SOPHIA_TYPES)).call(_context11, root);
236
- };
237
- /**
238
- * Parse sophia type
239
- * @param type
240
- * @param returnType
241
- * @return {Object}
242
- */
243
-
244
-
245
- export function readType(type) {
246
- var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
247
- bindings = _ref6.bindings;
248
-
249
- var _ref7 = _Array$isArray(type) ? type : [type],
250
- _ref8 = _slicedToArray(_ref7, 1),
251
- t = _ref8[0]; // If remote address
252
-
253
-
254
- if (isRemoteAddress(t)) return {
255
- t: SOPHIA_TYPES.address
256
- }; // Link State and typeDef
257
-
258
- if (isTypedDefOrState(t, bindings)) t = linkTypeDefs(t, bindings); // Map, Tuple, List, Record, Bytes
259
-
260
- if (_typeof(t) === 'object') {
261
- var _Object$entries6 = _Object$entries3(t),
262
- _Object$entries7 = _slicedToArray(_Object$entries6, 1),
263
- _Object$entries7$ = _slicedToArray(_Object$entries7[0], 2),
264
- baseType = _Object$entries7$[0],
265
- generic = _Object$entries7$[1];
266
-
267
- return {
268
- t: baseType,
269
- generic: generic
270
- };
271
- } // Base types
272
-
273
-
274
- if (typeof t === 'string') return {
275
- t: t
276
- };
277
- } // FUNCTION ARGUMENTS TRANSFORMATION ↓↓↓
278
-
279
- /**
280
- * Transform JS type to Sophia-type
281
- * @param type
282
- * @param value
283
- * @param bindings
284
- * @return {string}
285
- */
286
-
287
- export function transform(type, value) {
288
- var _ref9 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
289
- bindings = _ref9.bindings;
290
-
291
- var _readType = readType(type, {
292
- bindings: bindings
293
- }),
294
- t = _readType.t,
295
- generic = _readType.generic;
296
-
297
- switch (t) {
298
- case SOPHIA_TYPES.ChainTtl:
299
- return "".concat(value);
300
-
301
- case SOPHIA_TYPES.string:
302
- return "\"".concat(value, "\"");
303
-
304
- case SOPHIA_TYPES.list:
305
- return "[".concat(_mapInstanceProperty(value).call(value, function (el) {
306
- return transform(generic, el, {
307
- bindings: bindings
308
- });
309
- }), "]");
310
-
311
- case SOPHIA_TYPES.tuple:
312
- return "(".concat(_mapInstanceProperty(value).call(value, function (el, i) {
313
- return transform(generic[i], el, {
314
- bindings: bindings
315
- });
316
- }), ")");
317
-
318
- case SOPHIA_TYPES.option:
319
- {
320
- return value === undefined ? 'None' : "Some(".concat(transform(generic, value, {
321
- bindings: bindings
322
- }), ")");
323
- }
324
-
325
- case SOPHIA_TYPES.hash:
326
- case SOPHIA_TYPES.bytes:
327
- case SOPHIA_TYPES.signature:
328
- if (typeof value === 'string') {
329
- if (isHex(value)) return "#".concat(value);
330
- if (isAddressValid(value)) return "#".concat(decode(value).toString('hex'));
331
- }
332
-
333
- return "#".concat(Buffer.from(value).toString('hex'));
334
-
335
- case SOPHIA_TYPES.record:
336
- return "{".concat(_reduceInstanceProperty(generic).call(generic, function (acc, _ref10, i) {
337
- var _context12, _context13;
338
-
339
- var name = _ref10.name,
340
- type = _ref10.type;
341
- acc += _concatInstanceProperty(_context12 = _concatInstanceProperty(_context13 = "".concat(i !== 0 ? ',' : '')).call(_context13, name, " = ")).call(_context12, transform(type, value[name], {
342
- bindings: bindings
343
- }));
344
- return acc;
345
- }, ''), "}");
346
-
347
- case _mapInstanceProperty(SOPHIA_TYPES):
348
- return transformMap(value, generic, {
349
- bindings: bindings
350
- });
351
-
352
- case SOPHIA_TYPES.variant:
353
- return transformVariant(value, generic, {
354
- bindings: bindings
355
- });
356
- }
357
-
358
- return "".concat(value);
359
- }
360
- export function transformVariant(value, generic, _ref11) {
361
- var _context14, _context15;
362
-
363
- var bindings = _ref11.bindings;
364
-
365
- var _ref12 = typeof value === 'string' ? [[value, []]] : _Object$entries3(value),
366
- _ref13 = _slicedToArray(_ref12, 1),
367
- _ref13$ = _slicedToArray(_ref13[0], 2),
368
- variant = _ref13$[0],
369
- variantArgs = _ref13$[1];
370
-
371
- var _Object$entries8 = _Object$entries3(_findInstanceProperty(generic).call(generic, function (o) {
372
- return _Object$keys(o)[0].toLowerCase() === variant.toLowerCase();
373
- })),
374
- _Object$entries9 = _slicedToArray(_Object$entries8, 1),
375
- _Object$entries9$ = _slicedToArray(_Object$entries9[0], 2),
376
- v = _Object$entries9$[0],
377
- type = _Object$entries9$[1];
378
-
379
- return _concatInstanceProperty(_context14 = "".concat(v)).call(_context14, !type.length ? '' : "(".concat(_mapInstanceProperty(_context15 = _sliceInstanceProperty(variantArgs).call(variantArgs, 0, type.length)).call(_context15, function (el, i) {
380
- return transform(type[i], el, {
381
- bindings: bindings
382
- });
383
- }), ")"));
384
- }
385
- export function transformMap(value, generic, _ref14) {
386
- var bindings = _ref14.bindings;
387
-
388
- if (!_Array$isArray(value)) {
389
- if (_entriesInstanceProperty(value)) value = _Array$from(_entriesInstanceProperty(value).call(value));else if (value instanceof Object) value = _Object$entries3(value);
390
- }
391
-
392
- return ['{', _mapInstanceProperty(value).call(value, function (_ref15) {
393
- var _ref16 = _slicedToArray(_ref15, 2),
394
- key = _ref16[0],
395
- value = _ref16[1];
396
-
397
- return ["[".concat(transform(generic[0], key, {
398
- bindings: bindings
399
- }), "]"), transform(generic[1], value, {
400
- bindings: bindings
401
- })].join(' = ');
402
- }).join(), '}'].join('');
403
- } // FUNCTION RETURN VALUE TRANSFORMATION ↓↓↓
404
-
405
- /**
406
- * Transform decoded data to JS type
407
- * @param aci
408
- * @param result
409
- * @param transformDecodedData
410
- * @return {*}
411
- */
412
-
413
- export function transformDecodedData(aci, result) {
414
- var _ref17 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
415
- _ref17$skipTransformD = _ref17.skipTransformDecoded,
416
- skipTransformDecoded = _ref17$skipTransformD === void 0 ? false : _ref17$skipTransformD,
417
- _ref17$addressPrefix = _ref17.addressPrefix,
418
- addressPrefix = _ref17$addressPrefix === void 0 ? 'ak' : _ref17$addressPrefix,
419
- bindings = _ref17.bindings;
420
-
421
- if (skipTransformDecoded) return result;
422
-
423
- var _readType2 = readType(aci, {
424
- bindings: bindings
425
- }),
426
- t = _readType2.t,
427
- generic = _readType2.generic;
428
-
429
- switch (t) {
430
- case SOPHIA_TYPES.bool:
431
- return !!result;
432
-
433
- case SOPHIA_TYPES.address:
434
- return result === 0 ? 0 : result;
435
-
436
- case SOPHIA_TYPES.hash:
437
- case SOPHIA_TYPES.bytes:
438
- case SOPHIA_TYPES.signature:
439
- return result.split('#')[1];
440
-
441
- case _mapInstanceProperty(SOPHIA_TYPES):
442
- {
443
- var _generic = _slicedToArray(generic, 2),
444
- keyT = _generic[0],
445
- valueT = _generic[1];
446
-
447
- return _reduceInstanceProperty(result).call(result, function (acc, _ref18) {
448
- var _ref19 = _slicedToArray(_ref18, 2),
449
- key = _ref19[0],
450
- val = _ref19[1];
451
-
452
- key = transformDecodedData(keyT, key, {
453
- bindings: bindings
454
- });
455
- val = transformDecodedData(valueT, val, {
456
- bindings: bindings
457
- });
458
- acc.push([key, val]);
459
- return acc;
460
- }, []);
461
- }
462
-
463
- case SOPHIA_TYPES.option:
464
- {
465
- if (result === 'None') return undefined;
466
-
467
- var _Object$entries10 = _Object$entries3(result),
468
- _Object$entries11 = _slicedToArray(_Object$entries10, 1),
469
- _Object$entries11$ = _slicedToArray(_Object$entries11[0], 2),
470
- variantType = _Object$entries11$[0],
471
- _Object$entries11$$ = _slicedToArray(_Object$entries11$[1], 1),
472
- value = _Object$entries11$$[0];
473
-
474
- return variantType === 'Some' ? transformDecodedData(generic, value, {
475
- bindings: bindings
476
- }) : undefined;
477
- }
478
-
479
- case SOPHIA_TYPES.list:
480
- return _mapInstanceProperty(result).call(result, function (value) {
481
- return transformDecodedData(generic, value, {
482
- bindings: bindings
483
- });
484
- });
485
-
486
- case SOPHIA_TYPES.tuple:
487
- return _mapInstanceProperty(result).call(result, function (value, i) {
488
- return transformDecodedData(generic[i], value, {
489
- bindings: bindings
490
- });
491
- });
492
-
493
- case SOPHIA_TYPES.record:
494
- {
495
- var _context16;
496
-
497
- var genericMap = _reduceInstanceProperty(generic).call(generic, function (acc, val) {
498
- return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, val.name, {
499
- type: val.type
500
- }));
501
- }, {});
502
-
503
- return _reduceInstanceProperty(_context16 = _Object$entries3(result)).call(_context16, function (acc, _ref20) {
504
- var _ref21 = _slicedToArray(_ref20, 2),
505
- name = _ref21[0],
506
- value = _ref21[1];
507
-
508
- return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, name, transformDecodedData(genericMap[name].type, value, {
509
- bindings: bindings
510
- })));
511
- }, {});
512
- }
513
- }
514
-
515
- return result;
516
- } // FUNCTION ARGUMENTS VALIDATION ↓↓↓
517
-
518
- /**
519
- * Prepare Joi validation schema for sophia types
520
- * @param type
521
- * @param bindings
522
- * @return {Object} JoiSchema
523
- */
524
-
525
- export function prepareSchema(type) {
526
- var _Joi$string, _Joi$object;
527
-
528
- var _ref22 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
529
- bindings = _ref22.bindings;
530
-
531
- var _readType3 = readType(type, {
532
- bindings: bindings
533
- }),
534
- t = _readType3.t,
535
- generic = _readType3.generic;
536
-
537
- switch (t) {
538
- case SOPHIA_TYPES["int"]:
539
- return Joi.number().error(getJoiErrorMsg);
540
-
541
- case SOPHIA_TYPES.variant:
542
- return Joi.alternatives()["try"]([(_Joi$string = Joi.string()).valid.apply(_Joi$string, _toConsumableArray(_reduceInstanceProperty(generic).call(generic, function (acc, el) {
543
- var _Object$entries12 = _Object$entries3(el),
544
- _Object$entries13 = _slicedToArray(_Object$entries12, 1),
545
- _Object$entries13$ = _slicedToArray(_Object$entries13[0], 2),
546
- t = _Object$entries13$[0],
547
- g = _Object$entries13$[1];
548
-
549
- if (!g || !g.length) acc.push(t);
550
- return acc;
551
- }, []))), (_Joi$object = Joi.object(_reduceInstanceProperty(generic).call(generic, function (acc, el) {
552
- var variant = _Object$keys(el)[0];
553
-
554
- return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, variant, Joi.array()));
555
- }, {}))).or.apply(_Joi$object, _toConsumableArray(_mapInstanceProperty(generic).call(generic, function (e) {
556
- return _Object$keys(e)[0];
557
- })))]);
558
-
559
- case SOPHIA_TYPES.ChainTtl:
560
- return Joi.string().error(getJoiErrorMsg);
561
-
562
- case SOPHIA_TYPES.string:
563
- return Joi.string().error(getJoiErrorMsg);
564
-
565
- case SOPHIA_TYPES.address:
566
- return Joi.string().regex(/^(ak_|ct_|ok_|oq_)/).error(getJoiErrorMsg);
567
-
568
- case SOPHIA_TYPES.bool:
569
- return Joi["boolean"]().error(getJoiErrorMsg);
570
-
571
- case SOPHIA_TYPES.list:
572
- return Joi.array().items(prepareSchema(generic, {
573
- bindings: bindings
574
- })).error(getJoiErrorMsg);
575
-
576
- case SOPHIA_TYPES.tuple:
577
- return Joi.array().ordered(_mapInstanceProperty(generic).call(generic, function (type) {
578
- return prepareSchema(type, {
579
- bindings: bindings
580
- }).required();
581
- })).label('Tuple argument').error(getJoiErrorMsg);
582
-
583
- case SOPHIA_TYPES.record:
584
- return Joi.object(_reduceInstanceProperty(generic).call(generic, function (acc, _ref23) {
585
- var name = _ref23.name,
586
- type = _ref23.type;
587
- return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, name, prepareSchema(type, {
588
- bindings: bindings
589
- })));
590
- }, {})).error(getJoiErrorMsg);
591
-
592
- case SOPHIA_TYPES.hash:
593
- return JoiBinary.binary().bufferCheck(32).error(getJoiErrorMsg);
594
-
595
- case SOPHIA_TYPES.bytes:
596
- return JoiBinary.binary().bufferCheck(generic).error(getJoiErrorMsg);
597
-
598
- case SOPHIA_TYPES.signature:
599
- return JoiBinary.binary().bufferCheck(64).error(getJoiErrorMsg);
600
-
601
- case SOPHIA_TYPES.option:
602
- return prepareSchema(generic, {
603
- bindings: bindings
604
- }).optional().error(getJoiErrorMsg);
605
- // @Todo Need to transform Map to Array of arrays before validating it
606
- // case SOPHIA_TYPES.map:
607
- // return Joi.array().items(Joi.array().ordered(generic.map(type => prepareSchema(type))))
608
-
609
- default:
610
- return Joi.any();
611
- }
612
- }
613
- /**
614
- * Parse Joi validation error message
615
- * @param errors
616
- * @return {Object} JoiError
617
- */
618
-
619
- export function getJoiErrorMsg(errors) {
620
- return _mapInstanceProperty(errors).call(errors, function (err) {
621
- var _context17, _context18, _context19, _context20, _context21, _context22, _context23, _context24, _context25, _context26, _context27;
622
-
623
- var path = err.path,
624
- type = err.type,
625
- context = err.context;
626
- var value = Object.prototype.hasOwnProperty.call(context, 'value') ? context.value : context.label;
627
- value = _typeof(value) === 'object' ? _sliceInstanceProperty(_context17 = _sliceInstanceProperty(_context18 = _JSON$stringify(value)).call(_context18, 1)).call(_context17, 0, -1) : value;
628
-
629
- switch (type) {
630
- case 'string.base':
631
- return _objectSpread(_objectSpread({}, err), {}, {
632
- message: _concatInstanceProperty(_context19 = "Value \"".concat(value, "\" at path: [")).call(_context19, path, "] not a string")
633
- });
634
-
635
- case 'number.base':
636
- return _objectSpread(_objectSpread({}, err), {}, {
637
- message: _concatInstanceProperty(_context20 = "Value \"".concat(value, "\" at path: [")).call(_context20, path, "] not a number")
638
- });
639
-
640
- case 'boolean.base':
641
- return _objectSpread(_objectSpread({}, err), {}, {
642
- message: _concatInstanceProperty(_context21 = "Value \"".concat(value, "\" at path: [")).call(_context21, path, "] not a boolean")
643
- });
644
-
645
- case 'array.base':
646
- return _objectSpread(_objectSpread({}, err), {}, {
647
- message: _concatInstanceProperty(_context22 = "Value \"".concat(value, "\" at path: [")).call(_context22, path, "] not a array")
648
- });
649
-
650
- case 'object.base':
651
- return _objectSpread(_objectSpread({}, err), {}, {
652
- message: _concatInstanceProperty(_context23 = "Value '".concat(value, "' at path: [")).call(_context23, path, "] not a object")
653
- });
654
-
655
- case 'object.type':
656
- return _objectSpread(_objectSpread({}, err), {}, {
657
- message: _concatInstanceProperty(_context24 = _concatInstanceProperty(_context25 = "Value '".concat(value, "' at path: [")).call(_context25, path, "] not a ")).call(_context24, context.type)
658
- });
659
-
660
- case 'binary.bufferCheck':
661
- return _objectSpread(_objectSpread({}, err), {}, {
662
- message: _concatInstanceProperty(_context26 = _concatInstanceProperty(_context27 = "Value '".concat(Buffer.from(value).toString('hex'), "' at path: [")).call(_context27, path, "] not a ")).call(_context26, context.size, " bytes")
663
- });
664
-
665
- default:
666
- return err;
667
- }
668
- });
669
- }
670
- /**
671
- * Custom Joi Validator for binary type
672
- */
673
-
674
- var JoiBinary = Joi.extend(function (joi) {
675
- return {
676
- name: 'binary',
677
- base: joi.any(),
678
- pre: function pre(value, state, options) {
679
- if (options.convert && typeof value === 'string') {
680
- if (isAddressValid(value)) {
681
- return decode(value);
682
- }
683
-
684
- try {
685
- return Buffer.from(value, 'hex');
686
- } catch (e) {
687
- return undefined;
688
- }
689
- }
690
-
691
- return Buffer.from(value);
692
- },
693
- rules: [{
694
- name: 'bufferCheck',
695
- params: {
696
- size: joi.number().required()
697
- },
698
- validate: function validate(params, value, state, options) {
699
- if (!Buffer.isBuffer(value)) {
700
- return this.createError('binary.base', {
701
- value: value
702
- }, state, options);
703
- }
704
-
705
- if (value.length !== params.size) {
706
- return this.createError('binary.bufferCheck', {
707
- value: value,
708
- size: params.size
709
- }, state, options);
710
- }
711
-
712
- return value;
713
- }
714
- }]
715
- };
716
- });
717
- /**
718
- * Validation contract function arguments
719
- * @param aci
720
- * @param params
721
- */
722
-
723
- export function validateArguments(aci, params) {
724
- var _context28;
725
-
726
- var validationSchema = Joi.array().ordered(_mapInstanceProperty(_context28 = aci.arguments).call(_context28, function (_ref24, i) {
727
- var type = _ref24.type;
728
- return prepareSchema(type, {
729
- bindings: aci.bindings
730
- }).label("[".concat(params[i], "]"));
731
- })).sparse(true).label('Argument');
732
-
733
- var _Joi$validate = Joi.validate(params, validationSchema, {
734
- abortEarly: false
735
- }),
736
- error = _Joi$validate.error;
737
-
738
- if (error) {
739
- throw error;
63
+ return toBytes(field, true);
740
64
  }
741
65
  }
742
66
  //# sourceMappingURL=transformation.js.map