@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.
- package/CHANGELOG.md +216 -337
- package/README.md +16 -9
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -9
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +1 -1
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +1 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/account/base.js +36 -120
- package/es/account/base.js.map +1 -1
- package/es/account/memory.js +17 -19
- package/es/account/memory.js.map +1 -1
- package/es/account/multiple.js +79 -156
- package/es/account/multiple.js.map +1 -1
- package/es/ae/aens.js +205 -626
- package/es/ae/aens.js.map +1 -1
- package/es/ae/contract.js +114 -918
- package/es/ae/contract.js.map +1 -1
- package/es/ae/index.js +90 -250
- package/es/ae/index.js.map +1 -1
- package/es/ae/oracle.js +136 -504
- package/es/ae/oracle.js.map +1 -1
- package/es/ae/universal.js.map +1 -1
- package/es/chain/index.js +12 -27
- package/es/chain/index.js.map +1 -1
- package/es/chain/node.js +289 -1020
- package/es/chain/node.js.map +1 -1
- package/es/channel/handlers.js +597 -1086
- package/es/channel/handlers.js.map +1 -1
- package/es/channel/index.js +238 -465
- package/es/channel/index.js.map +1 -1
- package/es/channel/internal.js +129 -301
- package/es/channel/internal.js.map +1 -1
- package/es/contract/aci/index.js +328 -495
- package/es/contract/aci/index.js.map +1 -1
- package/es/contract/aci/transformation.js +35 -711
- package/es/contract/aci/transformation.js.map +1 -1
- package/es/contract/compiler.js +131 -203
- package/es/contract/compiler.js.map +1 -1
- package/es/contract/ga/helpers.js +17 -122
- package/es/contract/ga/helpers.js.map +1 -1
- package/es/contract/ga/index.js +108 -249
- package/es/contract/ga/index.js.map +1 -1
- package/es/contract/index.js +7 -3
- package/es/contract/index.js.map +1 -1
- package/es/index.js +3 -3
- package/es/index.js.map +1 -1
- package/es/node-pool/index.js +57 -74
- package/es/node-pool/index.js.map +1 -1
- package/es/node.js +65 -161
- package/es/node.js.map +1 -1
- package/es/other.d.ts +1 -1
- package/es/tx/builder/helpers.js +62 -115
- package/es/tx/builder/helpers.js.map +1 -1
- package/es/tx/builder/index.js +218 -309
- package/es/tx/builder/index.js.map +1 -1
- package/es/tx/builder/schema.js +586 -573
- package/es/tx/builder/schema.js.map +1 -1
- package/es/tx/index.js +102 -13
- package/es/tx/index.js.map +1 -1
- package/es/tx/tx-object.js +114 -138
- package/es/tx/tx-object.js.map +1 -1
- package/es/tx/tx.js +810 -1586
- package/es/tx/tx.js.map +1 -1
- package/es/tx/validator.js +191 -426
- package/es/tx/validator.js.map +1 -1
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +20 -23
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +1 -1
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +53 -80
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +1 -1
- package/es/utils/aepp-wallet-communication/connection/index.js +2 -1
- package/es/utils/aepp-wallet-communication/connection/index.js.map +1 -1
- package/es/utils/aepp-wallet-communication/content-script-bridge.js +24 -19
- package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +1 -1
- package/es/utils/aepp-wallet-communication/helpers.js +60 -105
- package/es/utils/aepp-wallet-communication/helpers.js.map +1 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +191 -514
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +1 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +78 -93
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +1 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +381 -613
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +1 -1
- package/es/utils/aepp-wallet-communication/schema.js +43 -39
- package/es/utils/aepp-wallet-communication/schema.js.map +1 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js +32 -58
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +1 -1
- package/es/utils/amount-formatter.js +41 -61
- package/es/utils/amount-formatter.js.map +1 -1
- package/es/utils/async-init.js +28 -64
- package/es/utils/async-init.js.map +1 -1
- package/es/utils/bignumber.js +3 -11
- package/es/utils/bignumber.js.map +1 -1
- package/es/utils/bytes.js +5 -9
- package/es/utils/bytes.js.map +1 -1
- package/es/utils/crypto.js +41 -106
- package/es/utils/crypto.js.map +1 -1
- package/es/utils/hd-wallet.js +45 -81
- package/es/utils/hd-wallet.js.map +1 -1
- package/es/utils/json-big.js +16 -49
- package/es/utils/json-big.js.map +1 -1
- package/es/utils/keystore.js +81 -246
- package/es/utils/keystore.js.map +1 -1
- package/es/utils/mptree.js +88 -134
- package/es/utils/mptree.js.map +1 -1
- package/es/utils/other.js +10 -60
- package/es/utils/other.js.map +1 -1
- package/es/utils/semver-satisfies.js +9 -28
- package/es/utils/semver-satisfies.js.map +1 -1
- package/es/utils/string.js +4 -13
- package/es/utils/string.js.map +1 -1
- package/es/utils/swagger.js +87 -224
- package/es/utils/swagger.js.map +1 -1
- package/package.json +51 -44
- package/src/account/base.js +11 -10
- package/src/account/multiple.js +24 -18
- package/src/ae/aens.js +64 -61
- package/src/ae/contract.js +71 -248
- package/src/ae/index.js +34 -9
- package/src/ae/oracle.js +30 -22
- package/src/ae/universal.js +3 -1
- package/src/chain/index.js +10 -16
- package/src/chain/node.js +38 -38
- package/src/channel/handlers.js +10 -4
- package/src/channel/index.js +44 -20
- package/src/contract/aci/index.js +234 -112
- package/src/contract/aci/transformation.js +30 -422
- package/src/contract/compiler.js +50 -5
- package/src/contract/ga/helpers.js +6 -14
- package/src/contract/ga/index.js +28 -28
- package/src/contract/index.js +6 -2
- package/src/index.js +4 -4
- package/src/node-pool/index.js +24 -13
- package/src/node.js +29 -30
- package/src/tx/builder/helpers.js +31 -56
- package/src/tx/builder/index.js +67 -28
- package/src/tx/builder/schema.js +79 -121
- package/src/tx/index.js +101 -12
- package/src/tx/tx-object.js +25 -8
- package/src/tx/tx.js +229 -150
- package/src/tx/validator.js +134 -202
- package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +3 -2
- package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +25 -7
- package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
- package/src/utils/aepp-wallet-communication/content-script-bridge.js +10 -4
- package/src/utils/aepp-wallet-communication/helpers.js +4 -2
- package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +33 -12
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +9 -5
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +59 -29
- package/src/utils/aepp-wallet-communication/schema.js +1 -3
- package/src/utils/aepp-wallet-communication/wallet-detector.js +4 -1
- package/src/utils/amount-formatter.ts +1 -1
- package/src/utils/bignumber.ts +2 -1
- package/src/utils/crypto.js +4 -68
- package/src/utils/keystore.js +10 -2
- package/src/utils/other.ts +1 -1
- package/src/utils/semver-satisfies.ts +1 -1
- package/src/utils/swagger.js +22 -10
- package/src/contract/aci/helpers.js +0 -140
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import Joi from 'joi-browser'
|
|
2
|
-
import { isHex } from '../../utils/string'
|
|
3
1
|
import { toBytes } from '../../utils/bytes'
|
|
4
2
|
import { decode } from '../../tx/builder/helpers'
|
|
5
3
|
import { parseBigNumber } from '../../utils/bignumber'
|
|
6
|
-
import {
|
|
4
|
+
import { addressFromDecimal, hash } from '../../utils/crypto'
|
|
7
5
|
|
|
8
6
|
export const SOPHIA_TYPES = [
|
|
9
7
|
'int',
|
|
@@ -24,441 +22,51 @@ export const SOPHIA_TYPES = [
|
|
|
24
22
|
].reduce((acc, type) => ({ ...acc, [type]: type }), { ChainTtl: 'Chain.ttl' })
|
|
25
23
|
|
|
26
24
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @param {Object[]} events Array of events
|
|
29
|
-
* @param {
|
|
30
|
-
* @param {Object} [options.schema=[]] SC function ACI schema
|
|
25
|
+
* Decode events fields
|
|
26
|
+
* @param {Object[]} events Array of events with encoded fields
|
|
27
|
+
* @param {Array} schemas Smart contract event ACI schemas
|
|
31
28
|
* @return {Object}
|
|
32
29
|
*/
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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 })
|
|
30
|
+
export const decodeEvents = (events, schemas = []) => events.reduce((acc, event) => {
|
|
31
|
+
const [nameHash, ...params] = event.topics
|
|
32
|
+
const schema = schemas.find((s) => hash(s.name).equals(toBytes(nameHash, true)))
|
|
33
|
+
if (!schema) return acc
|
|
34
|
+
const stringCount = schema.types.filter(t => t === SOPHIA_TYPES.string).length
|
|
35
|
+
if (stringCount > 1) throw new Error(`Event schema contains more than one string: ${schema.types}`)
|
|
36
|
+
const topicsCount = schema.types.length - stringCount
|
|
37
|
+
if (topicsCount !== params.length) {
|
|
38
|
+
throw new Error(`Schema defines ${topicsCount} types, but ${params.length} topics present`)
|
|
39
|
+
}
|
|
60
40
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
41
|
+
acc.push({
|
|
42
|
+
...event,
|
|
43
|
+
name: schema.name,
|
|
44
|
+
decoded: schema.types.map((type) =>
|
|
45
|
+
decodeEventField(type === SOPHIA_TYPES.string ? event.data : params.shift(), type))
|
|
65
46
|
})
|
|
66
|
-
|
|
47
|
+
|
|
48
|
+
return acc
|
|
49
|
+
}, [])
|
|
67
50
|
|
|
68
51
|
/**
|
|
69
52
|
* Transform Event based on type
|
|
70
|
-
* @param {String|Number}
|
|
53
|
+
* @param {String|Number} field Event data
|
|
71
54
|
* @param {String} type Event type from schema
|
|
72
55
|
* @return {*}
|
|
73
56
|
*/
|
|
74
|
-
|
|
57
|
+
function decodeEventField (field, type) {
|
|
75
58
|
switch (type) {
|
|
76
59
|
case SOPHIA_TYPES.int:
|
|
77
|
-
return parseBigNumber(
|
|
60
|
+
return parseBigNumber(field)
|
|
78
61
|
case SOPHIA_TYPES.bool:
|
|
79
|
-
return !!
|
|
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
|
-
}
|
|
62
|
+
return !!field
|
|
197
63
|
case SOPHIA_TYPES.hash:
|
|
198
|
-
|
|
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
|
|
64
|
+
return toBytes(field, true).toString('hex')
|
|
269
65
|
case SOPHIA_TYPES.address:
|
|
270
|
-
return
|
|
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)
|
|
66
|
+
return addressFromDecimal(field)
|
|
348
67
|
case SOPHIA_TYPES.string:
|
|
349
|
-
return
|
|
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))))
|
|
68
|
+
return decode(field).toString('utf-8')
|
|
373
69
|
default:
|
|
374
|
-
return
|
|
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
|
|
70
|
+
return toBytes(field, true)
|
|
463
71
|
}
|
|
464
72
|
}
|
package/src/contract/compiler.js
CHANGED
|
@@ -52,12 +52,29 @@ export default AsyncInit.compose(ContractBase, {
|
|
|
52
52
|
compilerUrl = compilerUrl.replace(/\/$/, '')
|
|
53
53
|
const client = await genSwaggerClient(`${compilerUrl}/api`, {
|
|
54
54
|
disableBigNumbers: true,
|
|
55
|
-
disableCaseConversion: true
|
|
55
|
+
disableCaseConversion: true,
|
|
56
|
+
responseInterceptor: response => {
|
|
57
|
+
if (response.ok) return
|
|
58
|
+
let message = `${new URL(response.url).pathname.slice(1)} error`
|
|
59
|
+
if (response.body.reason) {
|
|
60
|
+
message += ': ' + response.body.reason +
|
|
61
|
+
(response.body.parameter ? ` in ${response.body.parameter}` : '') +
|
|
62
|
+
// TODO: revising after improving documentation https://github.com/aeternity/aesophia_http/issues/78
|
|
63
|
+
(response.body.info ? ` (${JSON.stringify(response.body.info)})` : '')
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(response.body)) {
|
|
66
|
+
message += ':\n' + response.body
|
|
67
|
+
.map(e => `${e.type}:${e.pos.line}:${e.pos.col}: ${e.message}${e.context ? `(${e.context})` : ''}`)
|
|
68
|
+
.map(e => e.trim()) // TODO: remove after fixing https://github.com/aeternity/aesophia_http/issues/80
|
|
69
|
+
.join('\n')
|
|
70
|
+
}
|
|
71
|
+
response.statusText = message
|
|
72
|
+
return response
|
|
73
|
+
}
|
|
56
74
|
})
|
|
57
75
|
this.compilerVersion = client.spec.info.version
|
|
58
76
|
this._compilerApi = client.api
|
|
59
77
|
|
|
60
|
-
this._isCompiler6 = semverSatisfies(this.compilerVersion, COMPILER_6_GE_VERSION, COMPILER_LT_VERSION)
|
|
61
78
|
if (ignoreVersion) return
|
|
62
79
|
if (!semverSatisfies(this.compilerVersion, COMPILER_GE_VERSION, COMPILER_LT_VERSION)) {
|
|
63
80
|
throw new Error(`Unsupported compiler version ${this.compilerVersion}. ` +
|
|
@@ -74,6 +91,17 @@ export default AsyncInit.compose(ContractBase, {
|
|
|
74
91
|
this._ensureCompilerReady()
|
|
75
92
|
return Promise.resolve(this.compilerVersion)
|
|
76
93
|
},
|
|
94
|
+
/**
|
|
95
|
+
* Encode call data for contract call
|
|
96
|
+
* @function
|
|
97
|
+
* @category async
|
|
98
|
+
* @param {String} source Contract source code
|
|
99
|
+
* @param {String} name Name of function to call
|
|
100
|
+
* @param {Array} args Argument's for call
|
|
101
|
+
* @param {Object} [options={}] Options
|
|
102
|
+
* @param {Object} [options.filesystem={}] Contract external namespaces map
|
|
103
|
+
* @return {Promise<String>}
|
|
104
|
+
*/
|
|
77
105
|
async contractEncodeCallDataAPI (source, name, args = [], options) {
|
|
78
106
|
this._ensureCompilerReady()
|
|
79
107
|
const { calldata } = await this._compilerApi.encodeCalldata({
|
|
@@ -109,6 +137,22 @@ export default AsyncInit.compose(ContractBase, {
|
|
|
109
137
|
options: this._prepareCompilerOptions(options)
|
|
110
138
|
})
|
|
111
139
|
},
|
|
140
|
+
/**
|
|
141
|
+
* Decode contract call result data
|
|
142
|
+
* @function
|
|
143
|
+
* @category async
|
|
144
|
+
* @param {String} source - source code
|
|
145
|
+
* @param {String } fn - function name
|
|
146
|
+
* @param {String} callValue - result call data
|
|
147
|
+
* @param {String} callResult - result status
|
|
148
|
+
* @param {Object} [options={}] Options
|
|
149
|
+
* @param {Object} [options.filesystem={}] Contract external namespaces map
|
|
150
|
+
* @return {Promise<String>} Result object
|
|
151
|
+
* @example
|
|
152
|
+
* const decodedData = await sdk.contractDecodeCallResultAPI(
|
|
153
|
+
* SourceCode ,'functionName', 'cb_asdasdasd...', 'ok|revert'
|
|
154
|
+
* )
|
|
155
|
+
*/
|
|
112
156
|
contractDecodeCallResultAPI (source, fn, callValue, callResult, options) {
|
|
113
157
|
this._ensureCompilerReady()
|
|
114
158
|
return this._compilerApi.decodeCallResult({
|
|
@@ -130,7 +174,9 @@ export default AsyncInit.compose(ContractBase, {
|
|
|
130
174
|
},
|
|
131
175
|
getFateAssembler (bytecode, options) {
|
|
132
176
|
this._ensureCompilerReady()
|
|
133
|
-
return this._compilerApi.getFateAssemblerCode(
|
|
177
|
+
return this._compilerApi.getFateAssemblerCode(
|
|
178
|
+
{ bytecode, options: this._prepareCompilerOptions(options) }
|
|
179
|
+
)
|
|
134
180
|
},
|
|
135
181
|
getBytecodeCompilerVersion (bytecode, options) {
|
|
136
182
|
this._ensureCompilerReady()
|
|
@@ -145,6 +191,5 @@ export default AsyncInit.compose(ContractBase, {
|
|
|
145
191
|
}
|
|
146
192
|
})
|
|
147
193
|
|
|
148
|
-
const COMPILER_GE_VERSION = '
|
|
149
|
-
const COMPILER_6_GE_VERSION = '6.0.0'
|
|
194
|
+
const COMPILER_GE_VERSION = '6.0.0'
|
|
150
195
|
const COMPILER_LT_VERSION = '7.0.0'
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import BigNumber from 'bignumber.js'
|
|
2
|
-
import { MAX_AUTH_FUN_GAS
|
|
3
|
-
import { buildTx } from '../../tx/builder'
|
|
4
|
-
import { hash } from '../../utils/crypto'
|
|
2
|
+
import { MAX_AUTH_FUN_GAS } from '../../tx/builder/schema'
|
|
5
3
|
|
|
6
4
|
export const prepareGaParams = (ins) => async (authData, authFnName) => {
|
|
7
5
|
if (typeof authData !== 'object') throw new Error('AuthData must be an object')
|
|
@@ -10,16 +8,10 @@ export const prepareGaParams = (ins) => async (authData, authFnName) => {
|
|
|
10
8
|
if (authData.callData) {
|
|
11
9
|
if (authData.callData.split('_')[0] !== 'cb') throw new Error('Auth data must be a string with "cb" prefix.')
|
|
12
10
|
return { authCallData: authData.callData, gas }
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
}
|
|
12
|
+
if (!authData.source || !authData.args) throw new Error('Auth data must contain source code and arguments.')
|
|
13
|
+
return {
|
|
14
|
+
authCallData: await ins.contractEncodeCallDataAPI(authData.source, authFnName, authData.args),
|
|
15
|
+
gas
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
export const getContractAuthFan = (ins) => async (source, fnName) => {
|
|
20
|
-
const { bytecode } = await ins.contractCompile(source)
|
|
21
|
-
|
|
22
|
-
return { bytecode, authFun: hash(fnName) }
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const wrapInEmptySignedTx = (rlp) => buildTx({ encodedTx: rlp, signatures: [] }, TX_TYPE.signed)
|