@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
@@ -1,464 +0,0 @@
1
- import Joi from 'joi-browser'
2
- import { isHex } from '../../utils/string'
3
- import { toBytes } from '../../utils/bytes'
4
- import { decode } from '../../tx/builder/helpers'
5
- import { parseBigNumber } from '../../utils/bignumber'
6
- import { isAddressValid, addressFromDecimal, hash } from '../../utils/crypto'
7
-
8
- export const SOPHIA_TYPES = [
9
- 'int',
10
- 'string',
11
- 'tuple',
12
- 'address',
13
- 'bool',
14
- 'list',
15
- 'map',
16
- 'record',
17
- 'option',
18
- 'oracle',
19
- 'oracleQuery',
20
- 'hash',
21
- 'signature',
22
- 'bytes',
23
- 'variant'
24
- ].reduce((acc, type) => ({ ...acc, [type]: type }), { ChainTtl: 'Chain.ttl' })
25
-
26
- /**
27
- * Transform decoded event to JS type
28
- * @param {Object[]} events Array of events
29
- * @param {Object} [options={}] Options
30
- * @param {Object} [options.schema=[]] SC function ACI schema
31
- * @return {Object}
32
- */
33
- export function decodeEvents (events, options = { schema: [] }) {
34
- if (!events.length) return []
35
-
36
- return events.map(l => {
37
- const [eName, ...eParams] = l.topics
38
- const hexHash = toBytes(eName, true).toString('hex')
39
- const { schema } = options.schema
40
- .reduce(
41
- (acc, el) => {
42
- if (hash(el.name).toString('hex') === hexHash) {
43
- l.name = el.name
44
- return {
45
- schema: el.types,
46
- name: el.name
47
- }
48
- }
49
- return acc
50
- },
51
- { schema: [] }
52
- )
53
- const { decoded } = schema.reduce((acc, el) => {
54
- if (el === SOPHIA_TYPES.string) {
55
- return { decoded: [...acc.decoded, transformEvent(l.data, el)], params: acc.params }
56
- }
57
- const [event, ...tail] = acc.params
58
- return { decoded: [...acc.decoded, transformEvent(event, el)], params: tail }
59
- }, { decoded: [], params: eParams })
60
-
61
- return {
62
- ...l,
63
- decoded
64
- }
65
- })
66
- }
67
-
68
- /**
69
- * Transform Event based on type
70
- * @param {String|Number} event Event data
71
- * @param {String} type Event type from schema
72
- * @return {*}
73
- */
74
- export function transformEvent (event, type) {
75
- switch (type) {
76
- case SOPHIA_TYPES.int:
77
- return parseBigNumber(event)
78
- case SOPHIA_TYPES.bool:
79
- return !!event
80
- case SOPHIA_TYPES.hash:
81
- return toBytes(event, true).toString('hex')
82
- case SOPHIA_TYPES.address:
83
- return addressFromDecimal(event).split('_')[1]
84
- case SOPHIA_TYPES.string:
85
- return decode(event).toString('utf-8')
86
- default:
87
- return toBytes(event, true)
88
- }
89
- }
90
-
91
- export function injectVars (t, aciType) {
92
- const [[baseType, generic]] = Object.entries(aciType.typedef)
93
- const [[, varianValue]] = Object.entries(t)
94
- switch (baseType) {
95
- case SOPHIA_TYPES.variant:
96
- return {
97
- [baseType]: generic.map(el => {
98
- const [tag, gen] = Object.entries(el)[0]
99
- return {
100
- [tag]: gen.map(type => {
101
- const index = aciType.vars.map(e => e.name).indexOf(type)
102
- return index === -1
103
- ? type
104
- : varianValue[index]
105
- })
106
- }
107
- })
108
- }
109
- }
110
- }
111
-
112
- /**
113
- * Ling Type Defs
114
- * @param t
115
- * @param bindings
116
- * @return {Object}
117
- */
118
- export function linkTypeDefs (t, bindings) {
119
- const [root, typeDef] = typeof t === 'object' ? Object.keys(t)[0].split('.') : t.split('.')
120
- const contractTypeDefs = bindings.find(c => c.name === root)
121
- const aciType = [
122
- ...contractTypeDefs.type_defs,
123
- { name: 'state', typedef: contractTypeDefs.state, vars: [] }
124
- ].find(({ name }) => name === typeDef)
125
- if (aciType.vars.length) {
126
- aciType.typedef = injectVars(t, aciType)
127
- }
128
- return isTypedDefOrState(aciType.typedef, bindings) ? linkTypeDefs(aciType.typedef, bindings) : aciType.typedef
129
- }
130
-
131
- const isTypedDefOrState = (t, bindings) => {
132
- if (!['string', 'object'].includes(typeof t)) return false
133
-
134
- t = typeof t === 'object' ? Object.keys(t)[0] : t
135
- const [root, ...path] = t.split('.')
136
- // Remote Contract Address
137
- if (!path.length) return false
138
- return bindings.map(c => c.name).includes(root)
139
- }
140
-
141
- const isRemoteAddress = (t) => {
142
- if (typeof t !== 'string') return false
143
- const [root, ...path] = t.split('.')
144
- return !path.length && !Object.values(SOPHIA_TYPES).includes(root)
145
- }
146
-
147
- /**
148
- * Parse sophia type
149
- * @param type
150
- * @param returnType
151
- * @return {Object}
152
- */
153
- export function readType (type, { bindings } = {}) {
154
- let [t] = Array.isArray(type) ? type : [type]
155
-
156
- // If remote address
157
- if (isRemoteAddress(t)) return { t: SOPHIA_TYPES.address }
158
- // Link State and typeDef
159
- if (isTypedDefOrState(t, bindings)) t = linkTypeDefs(t, bindings)
160
- // Map, Tuple, List, Record, Bytes
161
- if (typeof t === 'object') {
162
- const [[baseType, generic]] = Object.entries(t)
163
- return { t: baseType, generic }
164
- }
165
- // Base types
166
- if (typeof t === 'string') return { t }
167
- }
168
-
169
- // FUNCTION ARGUMENTS TRANSFORMATION ↓↓↓
170
-
171
- /**
172
- * Transform JS type to Sophia-type
173
- * @param type
174
- * @param value
175
- * @param bindings
176
- * @return {string}
177
- */
178
- export function transform (type, value, { bindings } = {}) {
179
- const { t, generic } = readType(type, { bindings })
180
-
181
- switch (t) {
182
- case SOPHIA_TYPES.ChainTtl:
183
- return `${value}`
184
- case SOPHIA_TYPES.string:
185
- return `"${value}"`
186
- case SOPHIA_TYPES.list:
187
- return `[${value.map(el => transform(generic, el, { bindings }))}]`
188
- case SOPHIA_TYPES.tuple:
189
- return `(${value.map((el, i) => transform(generic[i], el, {
190
- bindings
191
- }))})`
192
- case SOPHIA_TYPES.option: {
193
- return value === undefined ? 'None' : `Some(${transform(generic, value, {
194
- bindings
195
- })})`
196
- }
197
- case SOPHIA_TYPES.hash:
198
- case SOPHIA_TYPES.bytes:
199
- case SOPHIA_TYPES.signature:
200
- if (typeof value === 'string') {
201
- if (isHex(value)) return `#${value}`
202
- if (isAddressValid(value)) return `#${decode(value).toString('hex')}`
203
- }
204
- return `#${Buffer.from(value).toString('hex')}`
205
- case SOPHIA_TYPES.record:
206
- return `{${generic.reduce(
207
- (acc, { name, type }, i) => {
208
- acc += `${i !== 0 ? ',' : ''}${name} = ${transform(type, value[name], {
209
- bindings
210
- })}`
211
- return acc
212
- },
213
- ''
214
- )}}`
215
- case SOPHIA_TYPES.map:
216
- return transformMap(value, generic, { bindings })
217
- case SOPHIA_TYPES.variant:
218
- return transformVariant(value, generic, { bindings })
219
- }
220
-
221
- return `${value}`
222
- }
223
-
224
- export function transformVariant (value, generic, { bindings }) {
225
- const [[variant, variantArgs]] = typeof value === 'string' ? [[value, []]] : Object.entries(value)
226
- const [[v, type]] = Object.entries(generic.find(o => Object.keys(o)[0].toLowerCase() === variant.toLowerCase()))
227
- return `${v}${!type.length
228
- ? ''
229
- : `(${variantArgs.slice(0, type.length).map((el, i) => transform(type[i], el, {
230
- bindings
231
- }))})`
232
- }`
233
- }
234
-
235
- export function transformMap (value, generic, { bindings }) {
236
- if (!Array.isArray(value)) {
237
- if (value.entries) value = Array.from(value.entries())
238
- else if (value instanceof Object) value = Object.entries(value)
239
- }
240
-
241
- return [
242
- '{',
243
- value
244
- .map(([key, value]) => [
245
- `[${transform(generic[0], key, { bindings })}]`,
246
- transform(generic[1], value, { bindings })
247
- ].join(' = '))
248
- .join(),
249
- '}'
250
- ].join('')
251
- }
252
-
253
- // FUNCTION RETURN VALUE TRANSFORMATION ↓↓↓
254
-
255
- /**
256
- * Transform decoded data to JS type
257
- * @param aci
258
- * @param result
259
- * @param transformDecodedData
260
- * @return {*}
261
- */
262
- export function transformDecodedData (aci, result, { skipTransformDecoded = false, addressPrefix = 'ak', bindings } = {}) {
263
- if (skipTransformDecoded) return result
264
- const { t, generic } = readType(aci, { bindings })
265
-
266
- switch (t) {
267
- case SOPHIA_TYPES.bool:
268
- return !!result
269
- case SOPHIA_TYPES.address:
270
- return result === 0
271
- ? 0
272
- : result
273
- case SOPHIA_TYPES.hash:
274
- case SOPHIA_TYPES.bytes:
275
- case SOPHIA_TYPES.signature:
276
- return result.split('#')[1]
277
- case SOPHIA_TYPES.map: {
278
- const [keyT, valueT] = generic
279
- return result
280
- .reduce(
281
- (acc, [key, val]) => {
282
- key = transformDecodedData(keyT, key, { bindings })
283
- val = transformDecodedData(valueT, val, { bindings })
284
- acc.push([key, val])
285
- return acc
286
- },
287
- []
288
- )
289
- }
290
- case SOPHIA_TYPES.option: {
291
- if (result === 'None') return undefined
292
- const [[variantType, [value]]] = Object.entries(result)
293
- return variantType === 'Some' ? transformDecodedData(generic, value, { bindings }) : undefined
294
- }
295
- case SOPHIA_TYPES.list:
296
- return result.map((value) => transformDecodedData(generic, value, { bindings }))
297
- case SOPHIA_TYPES.tuple:
298
- return result.map((value, i) => { return transformDecodedData(generic[i], value, { bindings }) })
299
- case SOPHIA_TYPES.record: {
300
- const genericMap = generic.reduce((acc, val) => ({ ...acc, [val.name]: { type: val.type } }), {})
301
- return Object.entries(result).reduce(
302
- (acc, [name, value]) =>
303
- ({
304
- ...acc,
305
- [name]: transformDecodedData(genericMap[name].type, value, { bindings })
306
- }),
307
- {}
308
- )
309
- }
310
- }
311
- return result
312
- }
313
-
314
- // FUNCTION ARGUMENTS VALIDATION ↓↓↓
315
-
316
- /**
317
- * Prepare Joi validation schema for sophia types
318
- * @param type
319
- * @param bindings
320
- * @return {Object} JoiSchema
321
- */
322
- export function prepareSchema (type, { bindings } = {}) {
323
- const { t, generic } = readType(type, { bindings })
324
-
325
- switch (t) {
326
- case SOPHIA_TYPES.int:
327
- return Joi.number().error(getJoiErrorMsg)
328
- case SOPHIA_TYPES.variant:
329
- return Joi.alternatives().try([
330
- Joi.string().valid(
331
- ...generic.reduce((acc, el) => {
332
- const [[t, g]] = Object.entries(el)
333
- if (!g || !g.length) acc.push(t)
334
- return acc
335
- }, [])
336
- ),
337
- Joi.object(generic
338
- .reduce(
339
- (acc, el) => {
340
- const variant = Object.keys(el)[0]
341
- return { ...acc, [variant]: Joi.array() }
342
- },
343
- {})
344
- ).or(...generic.map(e => Object.keys(e)[0]))
345
- ])
346
- case SOPHIA_TYPES.ChainTtl:
347
- return Joi.string().error(getJoiErrorMsg)
348
- case SOPHIA_TYPES.string:
349
- return Joi.string().error(getJoiErrorMsg)
350
- case SOPHIA_TYPES.address:
351
- return Joi.string().regex(/^(ak_|ct_|ok_|oq_)/).error(getJoiErrorMsg)
352
- case SOPHIA_TYPES.bool:
353
- return Joi.boolean().error(getJoiErrorMsg)
354
- case SOPHIA_TYPES.list:
355
- return Joi.array().items(prepareSchema(generic, { bindings })).error(getJoiErrorMsg)
356
- case SOPHIA_TYPES.tuple:
357
- return Joi.array().ordered(generic.map(type => prepareSchema(type, { bindings }).required())).label('Tuple argument').error(getJoiErrorMsg)
358
- case SOPHIA_TYPES.record:
359
- return Joi.object(
360
- generic.reduce((acc, { name, type }) => ({ ...acc, [name]: prepareSchema(type, { bindings }) }), {})
361
- ).error(getJoiErrorMsg)
362
- case SOPHIA_TYPES.hash:
363
- return JoiBinary.binary().bufferCheck(32).error(getJoiErrorMsg)
364
- case SOPHIA_TYPES.bytes:
365
- return JoiBinary.binary().bufferCheck(generic).error(getJoiErrorMsg)
366
- case SOPHIA_TYPES.signature:
367
- return JoiBinary.binary().bufferCheck(64).error(getJoiErrorMsg)
368
- case SOPHIA_TYPES.option:
369
- return prepareSchema(generic, { bindings }).optional().error(getJoiErrorMsg)
370
- // @Todo Need to transform Map to Array of arrays before validating it
371
- // case SOPHIA_TYPES.map:
372
- // return Joi.array().items(Joi.array().ordered(generic.map(type => prepareSchema(type))))
373
- default:
374
- return Joi.any()
375
- }
376
- }
377
-
378
- /**
379
- * Parse Joi validation error message
380
- * @param errors
381
- * @return {Object} JoiError
382
- */
383
- export function getJoiErrorMsg (errors) {
384
- return errors.map(err => {
385
- const { path, type, context } = err
386
- let value = Object.prototype.hasOwnProperty.call(context, 'value') ? context.value : context.label
387
- value = typeof value === 'object' ? JSON.stringify(value).slice(1).slice(0, -1) : value
388
- switch (type) {
389
- case 'string.base':
390
- return ({ ...err, message: `Value "${value}" at path: [${path}] not a string` })
391
- case 'number.base':
392
- return ({ ...err, message: `Value "${value}" at path: [${path}] not a number` })
393
- case 'boolean.base':
394
- return ({ ...err, message: `Value "${value}" at path: [${path}] not a boolean` })
395
- case 'array.base':
396
- return ({ ...err, message: `Value "${value}" at path: [${path}] not a array` })
397
- case 'object.base':
398
- return ({ ...err, message: `Value '${value}' at path: [${path}] not a object` })
399
- case 'object.type':
400
- return ({ ...err, message: `Value '${value}' at path: [${path}] not a ${context.type}` })
401
- case 'binary.bufferCheck':
402
- return ({
403
- ...err,
404
- message: `Value '${Buffer.from(value).toString('hex')}' at path: [${path}] not a ${context.size} bytes`
405
- })
406
- default:
407
- return err
408
- }
409
- })
410
- }
411
-
412
- /**
413
- * Custom Joi Validator for binary type
414
- */
415
- const JoiBinary = Joi.extend((joi) => ({
416
- name: 'binary',
417
- base: joi.any(),
418
- pre (value, state, options) {
419
- if (options.convert && typeof value === 'string') {
420
- if (isAddressValid(value)) {
421
- return decode(value)
422
- }
423
- try {
424
- return Buffer.from(value, 'hex')
425
- } catch (e) { return undefined }
426
- }
427
-
428
- return Buffer.from(value)
429
- },
430
- rules: [
431
- {
432
- name: 'bufferCheck',
433
- params: {
434
- size: joi.number().required()
435
- },
436
- validate (params, value, state, options) {
437
- if (!Buffer.isBuffer(value)) {
438
- return this.createError('binary.base', { value }, state, options)
439
- }
440
- if (value.length !== params.size) {
441
- return this.createError('binary.bufferCheck', { value, size: params.size }, state, options)
442
- }
443
-
444
- return value
445
- }
446
- }
447
- ]
448
- }))
449
-
450
- /**
451
- * Validation contract function arguments
452
- * @param aci
453
- * @param params
454
- */
455
- export function validateArguments (aci, params) {
456
- const validationSchema = Joi.array().ordered(
457
- aci.arguments
458
- .map(({ type }, i) => prepareSchema(type, { bindings: aci.bindings }).label(`[${params[i]}]`))
459
- ).sparse(true).label('Argument')
460
- const { error } = Joi.validate(params, validationSchema, { abortEarly: false })
461
- if (error) {
462
- throw error
463
- }
464
- }
@@ -1,159 +0,0 @@
1
- /*
2
- * ISC License (ISC)
3
- * Copyright (c) 2018 aeternity developers
4
- *
5
- * Permission to use, copy, modify, and/or distribute this software for any
6
- * purpose with or without fee is hereby granted, provided that the above
7
- * copyright notice and this permission notice appear in all copies.
8
- *
9
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
- * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
- * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- * PERFORMANCE OF THIS SOFTWARE.
16
- */
17
-
18
- /**
19
- * Contract Base module
20
- * @module @aeternity/aepp-sdk/es/contract
21
- * @export Contract
22
- * @example import ContractBase from '@aeternity/aepp-sdk/es/contract'
23
- */
24
-
25
- import stampit from '@stamp/it'
26
- import { required } from '@stamp/required'
27
-
28
- /**
29
- * Basic Contract Stamp
30
- *
31
- * This stamp include api call's related to contract functionality.
32
- * Attempting to create instances from the Stamp without overwriting all
33
- * abstract methods using composition will result in an exception.
34
- * @function
35
- * @alias module:@aeternity/aepp-sdk/es/contract
36
- * @rtype Stamp
37
- * @param {Object} [options={}] - Initializer object
38
- * @return {Object} Contract instance
39
- */
40
- const ContractBase = stampit(required({
41
- methods: {
42
- contractEncodeCallDataAPI: required,
43
- compileContractAPI: required,
44
- contractGetACI: required,
45
- setCompilerUrl: required,
46
- getCompilerVersion: required,
47
- contractDecodeCallResultAPI: required,
48
- validateByteCodeAPI: required,
49
- getFateAssembler: required,
50
- getBytecodeCompilerVersion: required
51
- }
52
- }))
53
-
54
- /**
55
- * Get contract ACI
56
- * @function contractGetACI
57
- * @instance
58
- * @abstract
59
- * @category async
60
- * @rtype (source: String, options: Array) => aciObject: Promise[Object]
61
- * @param {String} source - Contract source code
62
- * @param {Object} [options={}] Options
63
- * @param {Object} [options.filesystem] Contract external namespaces map
64
- * @return {Object} - Contract aci object
65
- */
66
-
67
- /**
68
- * Encode contract data
69
- * @function contractEncodeCallDataAPI
70
- * @instance
71
- * @abstract
72
- * @category async
73
- * @rtype (source: String, name: String, args: Array, options: Array) => callData: Promise[String]
74
- * @param {String} source - Contract source code
75
- * @param {String} name - Function name
76
- * @param {Array} args - Function argument's
77
- * @param {Object} [options={}] Options
78
- * @param {Object} [options.filesystem] Contract external namespaces map
79
- * @return {String} - Contract encoded data
80
- */
81
-
82
- /**
83
- * Decode contract call result data
84
- * @function contractDecodeCallResultAPI
85
- * @instance
86
- * @abstract
87
- * @category async
88
- * @rtype (source: String, fn: String, callValue: String, callResult: String, options: Array) => decodedResult: Promise[String]
89
- * @param {String} source - Contract source
90
- * @param {String} fn - Fn name
91
- * @param {String} callValue - result data (cb_das...)
92
- * @param {String} callResult - contract call result status('ok', 'revert', ...)
93
- * @param {Object} [options={}] Options
94
- * @param {Object} [options.filesystem] Contract external namespaces map
95
- * @return {String} - Decoded contract call result
96
- */
97
-
98
- /**
99
- * Decode call data by source
100
- * @function contractDecodeCallDataBySourceAPI
101
- * @instance
102
- * @abstract
103
- * @category async
104
- * @rtype (source: String, function: String, callData: String, options: Array) => decodedResult: Promise[String]
105
- * @param {String} source - contract source
106
- * @param {String} function - function name
107
- * @param {String} callData - Encoded contract call data
108
- * @param {Object} [options={}] Options
109
- * @param {Object} [options.filesystem] Contract external namespaces map
110
- * @return {String} - Decoded contract call data
111
- */
112
-
113
- /**
114
- * Decode call data by bytecode
115
- * @function contractDecodeCallDataByCodeAPI
116
- * @instance
117
- * @abstract
118
- * @category async
119
- * @rtype (code: String, callData: String) => decodedResult: Promise[String]
120
- * @param {String} code - contract byte code
121
- * @param {String} callData - Encoded contract call data
122
- * @return {String} - Decoded contract call data
123
- */
124
-
125
- /**
126
- * Compile contract
127
- * @function compileContractAPI
128
- * @instance
129
- * @abstract
130
- * @category async
131
- * @rtype (code: String, options?: Object) => compiledContract: Object
132
- * @param {String} code - Contract source code
133
- * @param {Object} [options={}] Options
134
- * @param {Object} [options.filesystem] Contract external namespaces map
135
- * @return {Object} Object which contain bytecode of contract
136
- */
137
-
138
- /**
139
- * Set compiler url
140
- * @function setCompilerUrl
141
- * @instance
142
- * @abstract
143
- * @category async
144
- * @rtype (url: String) => void
145
- * @param {String} url - Compiler url
146
- * @return {void}
147
- */
148
-
149
- /**
150
- * Get Compiler Version
151
- * @function getCompilerVersion
152
- * @instance
153
- * @abstract
154
- * @category async
155
- * @rtype () => String
156
- * @return {String} Compiler version
157
- */
158
-
159
- export default ContractBase