@helios-lang/contract-utils 0.2.82 → 0.3.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/package.json +7 -7
- package/src/cast/Cast.js +149 -140
- package/src/cast/UserFunc.js +43 -50
- package/src/cast/index.js +2 -29
- package/src/codegen/LoadedScriptsWriter.js +55 -55
- package/src/codegen/TypeSchema.js +6 -14
- package/src/codegen/index.js +1 -14
- package/src/compiler/CompilerLib.js +1 -80
- package/src/compiler/CompilerLib_v0_16.js +14 -14
- package/src/compiler/CompilerLib_v0_17.js +25 -23
- package/src/compiler/index.js +0 -5
- package/src/compiler/ops.js +5 -7
- package/src/context/CacheEntry.js +86 -64
- package/src/context/ContractContextBuilder.js +37 -57
- package/src/context/ContractContextCache.js +5 -7
- package/src/context/DagCompiler.js +24 -20
- package/src/context/index.js +1 -7
- package/src/hl2ts.js +3 -6
- package/src/index.js +355 -9
- package/types/cast/Cast.d.ts +17 -103
- package/types/cast/Cast.d.ts.map +1 -1
- package/types/cast/UserFunc.d.ts +11 -94
- package/types/cast/UserFunc.d.ts.map +1 -1
- package/types/cast/index.d.ts +2 -9
- package/types/cast/index.d.ts.map +1 -1
- package/types/codegen/LoadedScriptsWriter.d.ts +8 -83
- package/types/codegen/LoadedScriptsWriter.d.ts.map +1 -1
- package/types/codegen/TypeCheckedValidator.d.ts +1 -1
- package/types/codegen/TypeCheckedValidator.d.ts.map +1 -1
- package/types/codegen/TypeSchema.d.ts +1 -1
- package/types/codegen/TypeSchema.d.ts.map +1 -1
- package/types/codegen/index.d.ts +1 -7
- package/types/codegen/index.d.ts.map +1 -1
- package/types/compiler/CompilerLib.d.ts +3 -138
- package/types/compiler/CompilerLib.d.ts.map +1 -1
- package/types/compiler/CompilerLib_v0_16.d.ts +8 -118
- package/types/compiler/CompilerLib_v0_16.d.ts.map +1 -1
- package/types/compiler/CompilerLib_v0_17.d.ts +12 -52
- package/types/compiler/CompilerLib_v0_17.d.ts.map +1 -1
- package/types/compiler/index.d.ts +0 -2
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/ops.d.ts +2 -2
- package/types/compiler/ops.d.ts.map +1 -1
- package/types/context/CacheEntry.d.ts +9 -22
- package/types/context/CacheEntry.d.ts.map +1 -1
- package/types/context/ContractContext.d.ts +8 -8
- package/types/context/ContractContext.d.ts.map +1 -1
- package/types/context/ContractContextBuilder.d.ts +5 -100
- package/types/context/ContractContextBuilder.d.ts.map +1 -1
- package/types/context/ContractContextCache.d.ts +5 -8
- package/types/context/ContractContextCache.d.ts.map +1 -1
- package/types/context/DagCompiler.d.ts +11 -15
- package/types/context/DagCompiler.d.ts.map +1 -1
- package/types/context/index.d.ts +1 -6
- package/types/context/index.d.ts.map +1 -1
- package/types/hl2ts.d.ts +0 -3
- package/types/hl2ts.d.ts.map +1 -1
- package/types/index.d.ts +284 -9
- package/types/index.d.ts.map +1 -1
- package/src/cast/PermissiveType.js +0 -18
- package/src/cast/StrictType.js +0 -18
- package/src/codegen/LoadedModule.js +0 -23
- package/src/codegen/LoadedValidator.js +0 -52
- package/src/codegen/TypeCheckedModule.js +0 -31
- package/src/codegen/TypeCheckedValidator.js +0 -15
- package/src/context/ContractContext.js +0 -130
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helios-lang/contract-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Convenience and type-safety utilities for using Helios validators from within Typescript",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -37,12 +37,12 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@helios-lang/cli-utils": "^0.1.10",
|
|
40
|
-
"@helios-lang/codec-utils": "^0.
|
|
41
|
-
"@helios-lang/compiler-utils": "^0.2
|
|
42
|
-
"@helios-lang/crypto": "0.
|
|
43
|
-
"@helios-lang/ledger": "^0.
|
|
44
|
-
"@helios-lang/type-utils": "^0.
|
|
45
|
-
"@helios-lang/uplc": "^0.
|
|
40
|
+
"@helios-lang/codec-utils": "^0.3.1",
|
|
41
|
+
"@helios-lang/compiler-utils": "^0.3.2",
|
|
42
|
+
"@helios-lang/crypto": "0.2.0",
|
|
43
|
+
"@helios-lang/ledger": "^0.6.4",
|
|
44
|
+
"@helios-lang/type-utils": "^0.2.8",
|
|
45
|
+
"@helios-lang/uplc": "^0.7.5"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "pnpm run prettify && pnpm run build:types && pnpm run test:suite",
|
package/src/cast/Cast.js
CHANGED
|
@@ -1,82 +1,81 @@
|
|
|
1
1
|
import { decodeUtf8, encodeUtf8 } from "@helios-lang/codec-utils"
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
3
|
+
convertSpendingCredentialToUplcData,
|
|
4
|
+
convertStakingCredentialToUplcData,
|
|
5
|
+
convertUplcDataToAssetClass,
|
|
6
|
+
convertUplcDataToMintingPolicyHash,
|
|
7
|
+
convertUplcDataToPubKey,
|
|
8
|
+
convertUplcDataToPubKeyHash,
|
|
9
|
+
convertUplcDataToShelleyAddress,
|
|
10
|
+
convertUplcDataToSpendingCredential,
|
|
11
|
+
convertUplcDataToStakingCredential,
|
|
12
|
+
convertUplcDataToStakingValidatorHash,
|
|
13
|
+
convertUplcDataToTimeRange,
|
|
14
|
+
convertUplcDataToTxId,
|
|
15
|
+
convertUplcDataToTxInput,
|
|
16
|
+
convertUplcDataToTxOutput,
|
|
17
|
+
convertUplcDataToTxOutputDatum,
|
|
18
|
+
convertUplcDataToTxOutputId,
|
|
19
|
+
convertUplcDataToValidatorHash,
|
|
20
|
+
convertUplcDataToValue,
|
|
21
|
+
makeAddress,
|
|
22
|
+
makeAssetClass,
|
|
23
|
+
makeDatumHash,
|
|
24
|
+
makeMintingPolicyHash,
|
|
25
|
+
makePubKey,
|
|
26
|
+
makePubKeyHash,
|
|
27
|
+
makeStakingValidatorHash,
|
|
28
|
+
makeTimeRange,
|
|
29
|
+
makeTxId,
|
|
30
|
+
makeTxOutputDatum,
|
|
31
|
+
makeTxOutputId,
|
|
32
|
+
makeValidatorHash,
|
|
33
|
+
makeValue
|
|
22
34
|
} from "@helios-lang/ledger"
|
|
23
|
-
import {
|
|
35
|
+
import { expectDefined } from "@helios-lang/type-utils"
|
|
24
36
|
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
uplcDataToBool,
|
|
38
|
+
unwrapUplcDataOption,
|
|
39
|
+
uplcDataToReal,
|
|
40
|
+
boolToUplcData,
|
|
41
|
+
wrapUplcDataOption,
|
|
42
|
+
realToUplcData,
|
|
43
|
+
expectByteArrayData,
|
|
44
|
+
expectConstrData,
|
|
45
|
+
expectIntData,
|
|
46
|
+
expectListData,
|
|
47
|
+
expectMapData,
|
|
48
|
+
makeByteArrayData,
|
|
49
|
+
makeConstrData,
|
|
50
|
+
makeIntData,
|
|
51
|
+
makeListData,
|
|
52
|
+
makeMapData
|
|
36
53
|
} from "@helios-lang/uplc"
|
|
37
54
|
|
|
38
55
|
/**
|
|
39
|
-
* @
|
|
40
|
-
* @
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @typedef {{
|
|
45
|
-
* isMainnet: boolean
|
|
46
|
-
* }} CastConfig
|
|
56
|
+
* @import { TxInput, TxOutput } from "@helios-lang/ledger"
|
|
57
|
+
* @import { TypeSchema } from "@helios-lang/type-utils"
|
|
58
|
+
* @import { UplcData } from "@helios-lang/uplc"
|
|
59
|
+
* @import { Cast, CastConfig, CastLike, SchemaToUplcContext, UplcToSchemaContext } from "../index.js"
|
|
47
60
|
*/
|
|
48
61
|
|
|
49
62
|
/**
|
|
50
63
|
* @template TStrict
|
|
51
|
-
* @template TPermissive
|
|
52
|
-
* @
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* StrictType and PermissiveType work for both Cast and ConfigurableCast
|
|
57
|
-
* @template TStrict
|
|
58
|
-
* @template TPermissive
|
|
59
|
-
* @typedef {Cast<TStrict, TPermissive> | ConfigurableCast<TStrict, TPermissive>} CastLike
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @typedef {Object} SchemaToUplcContext
|
|
64
|
-
* @property {Record<string, TypeSchema>} defs - symbol table permitting recursive schema references
|
|
65
|
-
* @property {string} dataPath - provides developer-facing cues for any parsing errors, showing the deep field path of any error
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @typedef {Object} UplcToSchemaContext
|
|
70
|
-
* @property {Record<string, TypeSchema>} defs - symbol table permitting recursive schema references
|
|
71
|
-
* @property {string} dataPath - provides developer-facing cues for any parsing errors, showing the deep field path of any error
|
|
72
|
-
* @property {CastConfig} config - has isMainnet indicator
|
|
64
|
+
* @template TPermissive=TStrict
|
|
65
|
+
* @param {TypeSchema} schema
|
|
66
|
+
* @param {CastConfig} config
|
|
67
|
+
* @returns {Cast<TStrict, TPermissive>}
|
|
73
68
|
*/
|
|
69
|
+
export function makeCast(schema, config) {
|
|
70
|
+
return new CastImpl(schema, config)
|
|
71
|
+
}
|
|
74
72
|
|
|
75
73
|
/**
|
|
76
74
|
* @template TStrict
|
|
77
75
|
* @template TPermissive=TStrict
|
|
76
|
+
* @implements {Cast<TStrict, TPermissive>}
|
|
78
77
|
*/
|
|
79
|
-
|
|
78
|
+
class CastImpl {
|
|
80
79
|
/**
|
|
81
80
|
* @readonly
|
|
82
81
|
* @type {TypeSchema}
|
|
@@ -98,6 +97,13 @@ export class Cast {
|
|
|
98
97
|
this.config = config
|
|
99
98
|
}
|
|
100
99
|
|
|
100
|
+
/**
|
|
101
|
+
* @type {"Cast"}
|
|
102
|
+
*/
|
|
103
|
+
get kind() {
|
|
104
|
+
return "Cast"
|
|
105
|
+
}
|
|
106
|
+
|
|
101
107
|
/**
|
|
102
108
|
* @param {UplcData} data
|
|
103
109
|
* @param {string} [dataPath] - can be used to indicate the kind or context of data-decoding
|
|
@@ -170,7 +176,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
170
176
|
const kind = schema.kind
|
|
171
177
|
switch (kind) {
|
|
172
178
|
case "reference": {
|
|
173
|
-
const def =
|
|
179
|
+
const def = expectDefined(defs[schema.id])
|
|
174
180
|
return schemaToUplcWithDataPath(def, x, {
|
|
175
181
|
defs,
|
|
176
182
|
dataPath: `${dataPath}::ref{${schema.id}}`
|
|
@@ -181,68 +187,68 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
181
187
|
|
|
182
188
|
switch (name) {
|
|
183
189
|
case "Address":
|
|
184
|
-
return
|
|
190
|
+
return makeAddress(x).toUplcData()
|
|
185
191
|
case "Any":
|
|
186
|
-
return
|
|
192
|
+
return makeIntData(0n)
|
|
187
193
|
case "AssetClass":
|
|
188
|
-
return
|
|
194
|
+
return makeAssetClass(x).toUplcData()
|
|
189
195
|
case "Bool":
|
|
190
|
-
return
|
|
196
|
+
return boolToUplcData(x)
|
|
191
197
|
case "ByteArray":
|
|
192
|
-
return
|
|
198
|
+
return makeByteArrayData(x)
|
|
193
199
|
case "Credential":
|
|
194
200
|
case "SpendingCredential":
|
|
195
|
-
return
|
|
201
|
+
return convertSpendingCredentialToUplcData(x)
|
|
196
202
|
case "Data":
|
|
197
203
|
return x
|
|
198
204
|
case "DatumHash":
|
|
199
|
-
return
|
|
205
|
+
return makeDatumHash(x).toUplcData()
|
|
200
206
|
case "DCert":
|
|
201
207
|
return x.toUplcData()
|
|
202
208
|
case "Duration":
|
|
203
209
|
case "Int":
|
|
204
|
-
return
|
|
210
|
+
return makeIntData(x)
|
|
205
211
|
case "MintingPolicyHash":
|
|
206
|
-
return
|
|
212
|
+
return makeMintingPolicyHash(x).toUplcData()
|
|
207
213
|
case "PubKey":
|
|
208
|
-
return
|
|
214
|
+
return makePubKey(x).toUplcData()
|
|
209
215
|
case "PubKeyHash":
|
|
210
|
-
return
|
|
216
|
+
return makePubKeyHash(x).toUplcData()
|
|
211
217
|
case "Real":
|
|
212
|
-
return
|
|
218
|
+
return realToUplcData(x)
|
|
213
219
|
case "Ratio":
|
|
214
|
-
return
|
|
220
|
+
return makeListData([makeIntData(x[0]), makeIntData(x[1])])
|
|
215
221
|
case "ScriptHash":
|
|
216
|
-
return
|
|
222
|
+
return makeByteArrayData(makeValidatorHash(x).bytes)
|
|
217
223
|
case "StakingCredential":
|
|
218
|
-
return
|
|
224
|
+
return convertStakingCredentialToUplcData(makePubKeyHash(x))
|
|
219
225
|
case "StakingHash":
|
|
220
|
-
|
|
226
|
+
throw new Error("not yet implemented")
|
|
221
227
|
case "StakingValidatorHash":
|
|
222
|
-
return
|
|
228
|
+
return makeStakingValidatorHash(x).toUplcData()
|
|
223
229
|
case "String":
|
|
224
|
-
return
|
|
230
|
+
return makeByteArrayData(encodeUtf8(x))
|
|
225
231
|
case "Time":
|
|
226
|
-
return
|
|
232
|
+
return makeIntData(x)
|
|
227
233
|
case "TimeRange":
|
|
228
|
-
return
|
|
234
|
+
return makeTimeRange(x).toUplcData()
|
|
229
235
|
case "TxId":
|
|
230
|
-
return
|
|
236
|
+
return makeTxId(x).toUplcData()
|
|
231
237
|
case "TxInput":
|
|
232
238
|
return /** @type {TxInput} */ (x).toUplcData()
|
|
233
239
|
case "TxOutput":
|
|
234
240
|
return /** @type {TxOutput} */ (x).toUplcData()
|
|
235
241
|
case "TxOutputDatum":
|
|
236
242
|
return x
|
|
237
|
-
? (
|
|
238
|
-
|
|
239
|
-
:
|
|
243
|
+
? (makeTxOutputDatum(x)?.toUplcData() ??
|
|
244
|
+
makeConstrData(0, []))
|
|
245
|
+
: makeConstrData(0, [])
|
|
240
246
|
case "TxOutputId":
|
|
241
|
-
return
|
|
247
|
+
return makeTxOutputId(x).toUplcData()
|
|
242
248
|
case "ValidatorHash":
|
|
243
|
-
return
|
|
249
|
+
return makeValidatorHash(x).toUplcData()
|
|
244
250
|
case "Value":
|
|
245
|
-
return
|
|
251
|
+
return makeValue(x).toUplcData()
|
|
246
252
|
default:
|
|
247
253
|
throw new Error(
|
|
248
254
|
`schemaToUplc not yet implemented for ${name}`
|
|
@@ -250,7 +256,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
250
256
|
}
|
|
251
257
|
}
|
|
252
258
|
case "list":
|
|
253
|
-
return
|
|
259
|
+
return makeListData(
|
|
254
260
|
x.map((x, i) =>
|
|
255
261
|
schemaToUplcWithDataPath(schema.itemType, x, {
|
|
256
262
|
defs,
|
|
@@ -265,7 +271,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
265
271
|
: Array.isArray(x)
|
|
266
272
|
? x
|
|
267
273
|
: Object.entries(x)
|
|
268
|
-
return
|
|
274
|
+
return makeMapData(
|
|
269
275
|
entries.map(([k, v], i) => {
|
|
270
276
|
const displayKey = "string" == typeof k ? `'${k}'` : `@{i}`
|
|
271
277
|
return [
|
|
@@ -282,7 +288,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
282
288
|
)
|
|
283
289
|
}
|
|
284
290
|
case "tuple":
|
|
285
|
-
return
|
|
291
|
+
return makeListData(
|
|
286
292
|
x.map((x, i) =>
|
|
287
293
|
schemaToUplcWithDataPath(schema.itemTypes[i], x, {
|
|
288
294
|
defs,
|
|
@@ -291,13 +297,13 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
291
297
|
)
|
|
292
298
|
)
|
|
293
299
|
case "option":
|
|
294
|
-
return
|
|
300
|
+
return wrapUplcDataOption(
|
|
295
301
|
x
|
|
296
302
|
? schemaToUplcWithDataPath(schema.someType, x, {
|
|
297
303
|
defs,
|
|
298
304
|
dataPath: `${dataPath}::Some`
|
|
299
305
|
})
|
|
300
|
-
:
|
|
306
|
+
: undefined
|
|
301
307
|
)
|
|
302
308
|
case "struct": {
|
|
303
309
|
defs[schema.id] = schema
|
|
@@ -313,7 +319,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
313
319
|
}
|
|
314
320
|
)
|
|
315
321
|
case "list":
|
|
316
|
-
return
|
|
322
|
+
return makeListData(
|
|
317
323
|
schema.fieldTypes.map(({ name, type }) =>
|
|
318
324
|
schemaToUplcWithDataPath(type, x[name], {
|
|
319
325
|
defs,
|
|
@@ -355,7 +361,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
355
361
|
if (ft) {
|
|
356
362
|
const encodingKey = ft.key || ft.name
|
|
357
363
|
const encodingInfo = ft.key ? ` as '${ft.key}'` : ""
|
|
358
|
-
const keyData =
|
|
364
|
+
const keyData = makeByteArrayData(
|
|
359
365
|
encodeUtf8(encodingKey)
|
|
360
366
|
)
|
|
361
367
|
|
|
@@ -377,7 +383,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
377
383
|
//
|
|
378
384
|
// We just return our MapData, and we don't care whether it's used in any particular context.
|
|
379
385
|
|
|
380
|
-
return
|
|
386
|
+
return makeMapData(pairs)
|
|
381
387
|
}
|
|
382
388
|
default:
|
|
383
389
|
throw new Error(
|
|
@@ -401,7 +407,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
401
407
|
|
|
402
408
|
// Gives the encoding of nested data a context to prevent a second ConstrData wrapper on the MapData
|
|
403
409
|
// ... only for the first field.
|
|
404
|
-
return
|
|
410
|
+
return makeConstrData(
|
|
405
411
|
tag,
|
|
406
412
|
schema.variantTypes[tag].fieldTypes.map((f, i) =>
|
|
407
413
|
schemaToUplcWithDataPath(f.type, variantFields[f.name], {
|
|
@@ -414,7 +420,7 @@ function schemaToUplc(schema, x, inputContextOnly) {
|
|
|
414
420
|
case "variant":
|
|
415
421
|
defs[schema.id] = schema
|
|
416
422
|
|
|
417
|
-
return
|
|
423
|
+
return makeConstrData(
|
|
418
424
|
schema.tag,
|
|
419
425
|
schema.fieldTypes.map(({ name, type }) =>
|
|
420
426
|
schemaToUplcWithDataPath(type, x[name], {
|
|
@@ -470,19 +476,22 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
470
476
|
|
|
471
477
|
switch (name) {
|
|
472
478
|
case "Address":
|
|
473
|
-
return
|
|
479
|
+
return convertUplcDataToShelleyAddress(
|
|
480
|
+
config.isMainnet,
|
|
481
|
+
data
|
|
482
|
+
)
|
|
474
483
|
case "Any":
|
|
475
484
|
// TODO: should this throw an error?
|
|
476
485
|
return null
|
|
477
486
|
case "AssetClass":
|
|
478
|
-
return
|
|
487
|
+
return convertUplcDataToAssetClass(data)
|
|
479
488
|
case "Bool":
|
|
480
|
-
return
|
|
489
|
+
return uplcDataToBool(data, true)
|
|
481
490
|
case "ByteArray":
|
|
482
|
-
return
|
|
491
|
+
return expectByteArrayData(data).bytes
|
|
483
492
|
case "SpendingCredential":
|
|
484
493
|
case "Credential":
|
|
485
|
-
return
|
|
494
|
+
return convertUplcDataToSpendingCredential(data)
|
|
486
495
|
case "Data":
|
|
487
496
|
return data
|
|
488
497
|
case "DCert":
|
|
@@ -491,56 +500,56 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
491
500
|
)
|
|
492
501
|
case "Duration":
|
|
493
502
|
case "Int":
|
|
494
|
-
return
|
|
503
|
+
return expectIntData(data).value
|
|
495
504
|
case "MintingPolicyHash":
|
|
496
|
-
return
|
|
505
|
+
return convertUplcDataToMintingPolicyHash(data)
|
|
497
506
|
case "PubKey":
|
|
498
|
-
return
|
|
507
|
+
return convertUplcDataToPubKey(data)
|
|
499
508
|
case "PubKeyHash":
|
|
500
|
-
return
|
|
509
|
+
return convertUplcDataToPubKeyHash(data)
|
|
501
510
|
case "Ratio": {
|
|
502
|
-
const [top, bottom] =
|
|
511
|
+
const [top, bottom] = expectListData(data).items
|
|
503
512
|
return [
|
|
504
|
-
|
|
505
|
-
|
|
513
|
+
expectIntData(top).value,
|
|
514
|
+
expectIntData(bottom).value
|
|
506
515
|
]
|
|
507
516
|
}
|
|
508
517
|
case "Real":
|
|
509
|
-
return
|
|
518
|
+
return uplcDataToReal(data)
|
|
510
519
|
case "ScriptHash":
|
|
511
|
-
return
|
|
520
|
+
return expectByteArrayData(data).bytes
|
|
512
521
|
case "StakingCredential":
|
|
513
|
-
return
|
|
522
|
+
return convertUplcDataToStakingCredential(data)
|
|
514
523
|
case "StakingHash":
|
|
515
|
-
|
|
524
|
+
throw new Error("not yet implemented")
|
|
516
525
|
case "StakingValidatorHash":
|
|
517
|
-
return
|
|
526
|
+
return convertUplcDataToStakingValidatorHash(data)
|
|
518
527
|
case "String":
|
|
519
|
-
return decodeUtf8(
|
|
528
|
+
return decodeUtf8(expectByteArrayData(data).bytes)
|
|
520
529
|
case "Time":
|
|
521
|
-
return Number(
|
|
530
|
+
return Number(expectIntData(data).value) // a number has enough precision to represent ms since 1970 for another 142000 years
|
|
522
531
|
case "TimeRange":
|
|
523
|
-
return
|
|
532
|
+
return convertUplcDataToTimeRange(data)
|
|
524
533
|
case "TxId":
|
|
525
|
-
return
|
|
534
|
+
return convertUplcDataToTxId(data)
|
|
526
535
|
case "TxInput":
|
|
527
|
-
return
|
|
536
|
+
return convertUplcDataToTxInput(config.isMainnet, data)
|
|
528
537
|
case "TxOutput":
|
|
529
|
-
return
|
|
538
|
+
return convertUplcDataToTxOutput(config.isMainnet, data)
|
|
530
539
|
case "TxOutputDatum":
|
|
531
|
-
return
|
|
540
|
+
return convertUplcDataToTxOutputDatum(data)
|
|
532
541
|
case "TxOutputId":
|
|
533
|
-
return
|
|
542
|
+
return convertUplcDataToTxOutputId(data)
|
|
534
543
|
case "ValidatorHash":
|
|
535
|
-
return
|
|
544
|
+
return convertUplcDataToValidatorHash(data)
|
|
536
545
|
case "Value":
|
|
537
|
-
return
|
|
546
|
+
return convertUplcDataToValue(data)
|
|
538
547
|
default:
|
|
539
548
|
throw new Error(`not yet implemented for ${name}`)
|
|
540
549
|
}
|
|
541
550
|
}
|
|
542
551
|
case "list":
|
|
543
|
-
return
|
|
552
|
+
return expectListData(data).items.map((x, i) =>
|
|
544
553
|
uplcToSchemaWithDataPath(schema.itemType, x, {
|
|
545
554
|
...context,
|
|
546
555
|
dataPath: `${dataPath}[${i}]`
|
|
@@ -548,7 +557,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
548
557
|
)
|
|
549
558
|
case "map":
|
|
550
559
|
return new Map(
|
|
551
|
-
|
|
560
|
+
expectMapData(data).items.map(([k, v], i) => {
|
|
552
561
|
const key = uplcToSchemaWithDataPath(schema.keyType, k, {
|
|
553
562
|
...context,
|
|
554
563
|
dataPath: `${dataPath}[mapKey @${i}]`
|
|
@@ -565,20 +574,20 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
565
574
|
})
|
|
566
575
|
)
|
|
567
576
|
case "tuple":
|
|
568
|
-
return
|
|
577
|
+
return expectListData(data).items.map((x, i) =>
|
|
569
578
|
uplcToSchemaWithDataPath(schema.itemTypes[i], x, {
|
|
570
579
|
...context,
|
|
571
580
|
dataPath: `${dataPath}[tuple@${i}]`
|
|
572
581
|
})
|
|
573
582
|
)
|
|
574
583
|
case "option": {
|
|
575
|
-
const optionData =
|
|
584
|
+
const optionData = unwrapUplcDataOption(data)
|
|
576
585
|
return optionData
|
|
577
586
|
? uplcToSchemaWithDataPath(schema.someType, optionData, {
|
|
578
587
|
...context,
|
|
579
588
|
dataPath: `${dataPath}::Some`
|
|
580
589
|
})
|
|
581
|
-
:
|
|
590
|
+
: undefined
|
|
582
591
|
}
|
|
583
592
|
case "struct": {
|
|
584
593
|
defs[schema.id] = schema
|
|
@@ -592,7 +601,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
592
601
|
)
|
|
593
602
|
}
|
|
594
603
|
case "list": {
|
|
595
|
-
const fields =
|
|
604
|
+
const fields = expectListData(data).items
|
|
596
605
|
|
|
597
606
|
if (fields.length != schema.fieldTypes.length) {
|
|
598
607
|
throw new Error(
|
|
@@ -616,7 +625,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
616
625
|
}
|
|
617
626
|
case "map": {
|
|
618
627
|
// never uses a ConstrData (one MAY be used in the Enum layer, if needed)
|
|
619
|
-
const encodedEntries =
|
|
628
|
+
const encodedEntries = expectMapData(data).items
|
|
620
629
|
|
|
621
630
|
if (encodedEntries.length != schema.fieldTypes.length) {
|
|
622
631
|
throw new Error(
|
|
@@ -632,7 +641,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
632
641
|
return encodedEntries.findIndex(([key, _]) => {
|
|
633
642
|
return (
|
|
634
643
|
targetName ==
|
|
635
|
-
decodeUtf8(
|
|
644
|
+
decodeUtf8(expectByteArrayData(key).bytes)
|
|
636
645
|
)
|
|
637
646
|
})
|
|
638
647
|
}
|
|
@@ -640,7 +649,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
640
649
|
return Object.fromEntries(
|
|
641
650
|
schema.fieldTypes
|
|
642
651
|
.map(({ name, type, key: encodingKey }) => {
|
|
643
|
-
const i =
|
|
652
|
+
const i = expectDefined(
|
|
644
653
|
findSerializedFieldIndex(
|
|
645
654
|
encodingKey || name
|
|
646
655
|
)
|
|
@@ -692,7 +701,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
692
701
|
}
|
|
693
702
|
case "enum": {
|
|
694
703
|
defs[schema.id] = schema
|
|
695
|
-
const { tag, fields } =
|
|
704
|
+
const { tag, fields } = expectConstrData(data)
|
|
696
705
|
|
|
697
706
|
const variantSchema = schema.variantTypes[tag]
|
|
698
707
|
|
|
@@ -727,7 +736,7 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
727
736
|
}
|
|
728
737
|
case "variant": {
|
|
729
738
|
defs[schema.id] = schema
|
|
730
|
-
const { fields } =
|
|
739
|
+
const { fields } = expectConstrData(data)
|
|
731
740
|
|
|
732
741
|
return Object.fromEntries(
|
|
733
742
|
fields.map((field, i) => [
|
|
@@ -752,9 +761,9 @@ function uplcToSchema(schema, data, inputContextOnly) {
|
|
|
752
761
|
* @returns {Cast<TStrict, TPermissive>}
|
|
753
762
|
*/
|
|
754
763
|
export function configureCast(cast, config) {
|
|
755
|
-
if (cast
|
|
764
|
+
if ("kind" in cast && cast.kind == "Cast") {
|
|
756
765
|
return cast
|
|
757
766
|
} else {
|
|
758
|
-
return cast(config)
|
|
767
|
+
return /** @type {any} */ (cast)(config)
|
|
759
768
|
}
|
|
760
769
|
}
|