@helios-lang/effect 0.1.5 → 0.1.6

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 (109) hide show
  1. package/bun.lock +60 -0
  2. package/package.json +1 -1
  3. package/tsconfig.build.tsbuildinfo +1 -0
  4. package/tsconfig.check.tsbuildinfo +1 -0
  5. package/tsconfig.tsbuildinfo +1 -0
  6. package/types/Address.d.ts +5 -0
  7. package/types/Address.d.ts.map +1 -0
  8. package/types/Bech32.d.ts +30 -0
  9. package/types/Bech32.d.ts.map +1 -0
  10. package/types/Cbor.d.ts +430 -0
  11. package/types/Cbor.d.ts.map +1 -0
  12. package/types/Ledger/Address.d.ts +109 -0
  13. package/types/Ledger/Address.d.ts.map +1 -0
  14. package/types/Ledger/AssetClass.d.ts +101 -0
  15. package/types/Ledger/AssetClass.d.ts.map +1 -0
  16. package/types/Ledger/Assets.d.ts +70 -0
  17. package/types/Ledger/Assets.d.ts.map +1 -0
  18. package/types/Ledger/Credential.d.ts +26 -0
  19. package/types/Ledger/Credential.d.ts.map +1 -0
  20. package/types/Ledger/DatumHash.d.ts +40 -0
  21. package/types/Ledger/DatumHash.d.ts.map +1 -0
  22. package/types/Ledger/IsMainnet.d.ts +6 -0
  23. package/types/Ledger/IsMainnet.d.ts.map +1 -0
  24. package/types/Ledger/MintingPolicy.d.ts +39 -0
  25. package/types/Ledger/MintingPolicy.d.ts.map +1 -0
  26. package/{src/Ledger/NetworkParams.ts → types/Ledger/NetworkParams.d.ts} +24 -26
  27. package/types/Ledger/NetworkParams.d.ts.map +1 -0
  28. package/types/Ledger/PubKeyHash.d.ts +40 -0
  29. package/types/Ledger/PubKeyHash.d.ts.map +1 -0
  30. package/types/Ledger/TxId.d.ts +10 -0
  31. package/types/Ledger/TxId.d.ts.map +1 -0
  32. package/types/Ledger/TxInput.d.ts +55 -0
  33. package/types/Ledger/TxInput.d.ts.map +1 -0
  34. package/types/Ledger/TxOutput.d.ts +63 -0
  35. package/types/Ledger/TxOutput.d.ts.map +1 -0
  36. package/types/Ledger/TxOutputDatum.d.ts +41 -0
  37. package/types/Ledger/TxOutputDatum.d.ts.map +1 -0
  38. package/types/Ledger/TxOutputId.d.ts +14 -0
  39. package/types/Ledger/TxOutputId.d.ts.map +1 -0
  40. package/types/Ledger/ValidatorHash.d.ts +40 -0
  41. package/types/Ledger/ValidatorHash.d.ts.map +1 -0
  42. package/types/Ledger/index.d.ts +16 -0
  43. package/types/Ledger/index.d.ts.map +1 -0
  44. package/types/Uplc/Cek.d.ts +72 -0
  45. package/types/Uplc/Cek.d.ts.map +1 -0
  46. package/types/Uplc/Data.d.ts +530 -0
  47. package/types/Uplc/Data.d.ts.map +1 -0
  48. package/types/Uplc/DataSchema.d.ts +227 -0
  49. package/types/Uplc/DataSchema.d.ts.map +1 -0
  50. package/types/Uplc/Primitive.d.ts +26 -0
  51. package/types/Uplc/Primitive.d.ts.map +1 -0
  52. package/types/Uplc/index.d.ts +3 -0
  53. package/types/Uplc/index.d.ts.map +1 -0
  54. package/types/index.d.ts +5 -0
  55. package/types/index.d.ts.map +1 -0
  56. package/types/internal/Base32.d.ts +49 -0
  57. package/types/internal/Base32.d.ts.map +1 -0
  58. package/types/internal/BigEndian.d.ts +22 -0
  59. package/types/internal/BigEndian.d.ts.map +1 -0
  60. package/types/internal/Bits.d.ts +123 -0
  61. package/types/internal/Bits.d.ts.map +1 -0
  62. package/types/internal/Bytes.d.ts +88 -0
  63. package/types/internal/Bytes.d.ts.map +1 -0
  64. package/types/internal/Flat.d.ts +71 -0
  65. package/types/internal/Flat.d.ts.map +1 -0
  66. package/types/internal/Float.d.ts +38 -0
  67. package/types/internal/Float.d.ts.map +1 -0
  68. package/types/internal/Utf8.d.ts +24 -0
  69. package/types/internal/Utf8.d.ts.map +1 -0
  70. package/src/Bech32.test.ts +0 -117
  71. package/src/Bech32.ts +0 -198
  72. package/src/Cbor.test.ts +0 -1610
  73. package/src/Cbor.ts +0 -1704
  74. package/src/Ledger/Address.ts +0 -248
  75. package/src/Ledger/AssetClass.ts +0 -90
  76. package/src/Ledger/Assets.ts +0 -164
  77. package/src/Ledger/Credential.ts +0 -29
  78. package/src/Ledger/DatumHash.ts +0 -36
  79. package/src/Ledger/IsMainnet.ts +0 -6
  80. package/src/Ledger/MintingPolicy.ts +0 -57
  81. package/src/Ledger/PubKeyHash.ts +0 -36
  82. package/src/Ledger/TxId.ts +0 -31
  83. package/src/Ledger/TxInput.test.ts +0 -21
  84. package/src/Ledger/TxInput.ts +0 -66
  85. package/src/Ledger/TxOutput.ts +0 -166
  86. package/src/Ledger/TxOutputDatum.ts +0 -64
  87. package/src/Ledger/TxOutputId.ts +0 -63
  88. package/src/Ledger/ValidatorHash.ts +0 -36
  89. package/src/Ledger/index.ts +0 -15
  90. package/src/Uplc/Cek.ts +0 -92
  91. package/src/Uplc/Data.test.ts +0 -321
  92. package/src/Uplc/Data.ts +0 -657
  93. package/src/Uplc/Primitive.ts +0 -56
  94. package/src/Uplc/index.ts +0 -2
  95. package/src/index.ts +0 -4
  96. package/src/internal/Base32.test.ts +0 -219
  97. package/src/internal/Base32.ts +0 -341
  98. package/src/internal/BigEndian.test.ts +0 -79
  99. package/src/internal/BigEndian.ts +0 -67
  100. package/src/internal/Bits.test.ts +0 -300
  101. package/src/internal/Bits.ts +0 -398
  102. package/src/internal/Bytes.test.ts +0 -369
  103. package/src/internal/Bytes.ts +0 -343
  104. package/src/internal/Flat.test.ts +0 -29
  105. package/src/internal/Flat.ts +0 -387
  106. package/src/internal/Float.test.ts +0 -51
  107. package/src/internal/Float.ts +0 -190
  108. package/src/internal/Utf8.test.ts +0 -69
  109. package/src/internal/Utf8.ts +0 -58
package/src/Uplc/Data.ts DELETED
@@ -1,657 +0,0 @@
1
- import { Effect, Encoding, ParseResult, Schema } from "effect"
2
- import * as Bytes from "../internal/Bytes.js"
3
- import { decode as decodeUtf8, encode as encodeUtf8 } from "../internal/Utf8.js"
4
- import * as Cbor from "../Cbor.js"
5
-
6
- const SuspendedData = Schema.suspend(
7
- (): Schema.Schema<Data, DataJSON> => DataFromJSON
8
- )
9
-
10
- export const ByteArrayDataFromJSON = Schema.Struct({
11
- bytes: Schema.Uint8ArrayFromHex
12
- })
13
-
14
- export type ByteArrayData = Schema.Schema.Type<typeof ByteArrayDataFromJSON>
15
- export type ByteArrayDataJSON = Schema.Schema.Encoded<
16
- typeof ByteArrayDataFromJSON
17
- >
18
-
19
- export function makeByteArrayData(
20
- bytes: string | number[] | Uint8Array
21
- ): ByteArrayData {
22
- return { bytes: Bytes.toUint8Array(bytes) }
23
- }
24
-
25
- /**
26
- * No need to serialize
27
- *
28
- * Cannot used Branded types due Schema issues.
29
- */
30
- export const IntDataFromJSON = Schema.Struct({
31
- int: Schema.BigIntFromNumber
32
- })
33
-
34
- export type IntData = Schema.Schema.Type<typeof IntDataFromJSON>
35
- export type IntDataJSON = Schema.Schema.Encoded<typeof IntDataFromJSON>
36
-
37
- export function makeIntData(value: number | bigint): IntData {
38
- return { int: BigInt(value) }
39
- }
40
-
41
- export const ListDataFromJSON = Schema.Struct({
42
- list: Schema.Array(SuspendedData)
43
- })
44
-
45
- /**
46
- * Must be defined explicitly to avoid circular reference problems
47
- */
48
- export type ListData = {
49
- readonly list: ReadonlyArray<Data>
50
- }
51
-
52
- export type ListDataJSON = {
53
- readonly list: ReadonlyArray<DataJSON>
54
- }
55
-
56
- export function makeListData(items: Data[]): ListData {
57
- return {
58
- list: items
59
- }
60
- }
61
-
62
- export const MapDataFromJSON = Schema.Struct({
63
- map: Schema.Array(
64
- Schema.Struct({
65
- k: SuspendedData,
66
- v: SuspendedData
67
- })
68
- )
69
- })
70
-
71
- /**
72
- * Must be defined explicitly to avoid circular reference problems
73
- */
74
- export type MapData = {
75
- readonly map: ReadonlyArray<{
76
- readonly k: Data
77
- readonly v: Data
78
- }>
79
- }
80
-
81
- export type MapDataJSON = {
82
- readonly map: ReadonlyArray<{
83
- readonly k: DataJSON
84
- readonly v: DataJSON
85
- }>
86
- }
87
-
88
- export function makeMapData(entries: [Data, Data][]): MapData {
89
- return {
90
- map: entries.map(([k, v]) => ({ k, v }))
91
- }
92
- }
93
-
94
- export const ConstrDataFromJSON = Schema.Struct({
95
- constructor: Schema.Number,
96
- fields: Schema.Array(SuspendedData)
97
- })
98
-
99
- /**
100
- * Must be defined explicitly to avoid circular reference problems
101
- */
102
- export type ConstrData = {
103
- readonly constructor: number
104
- readonly fields: ReadonlyArray<Data>
105
- }
106
-
107
- export type ConstrDataJSON = {
108
- readonly constructor: number
109
- readonly fields: ReadonlyArray<DataJSON>
110
- }
111
-
112
- export function makeConstrData(
113
- tag: bigint | number,
114
- fields: Data[]
115
- ): ConstrData {
116
- return {
117
- constructor: Number(tag),
118
- fields
119
- }
120
- }
121
-
122
- export const DataFromJSON = Schema.Union(
123
- ByteArrayDataFromJSON,
124
- IntDataFromJSON,
125
- ListDataFromJSON,
126
- MapDataFromJSON,
127
- ConstrDataFromJSON
128
- )
129
-
130
- export const Data = Schema.typeSchema(DataFromJSON)
131
-
132
- /**
133
- * Must be defined explicitly to avoid circular reference problems
134
- */
135
- export type Data = ByteArrayData | ConstrData | IntData | ListData | MapData
136
-
137
- export type DataJSON =
138
- | ByteArrayDataJSON
139
- | ConstrDataJSON
140
- | IntDataJSON
141
- | ListDataJSON
142
- | MapDataJSON
143
-
144
- /**
145
- * Simple recursive CBOR decoder
146
- * @param bytes
147
- * @returns
148
- */
149
- export const decode = (bytes: Bytes.BytesLike): Cbor.DecodeEffect<Data> =>
150
- Effect.gen(function* () {
151
- const stream = Bytes.makeStream(bytes)
152
-
153
- if (yield* Cbor.isList(stream)) {
154
- const items = yield* Cbor.decodeList(decode)(stream)
155
-
156
- return makeListData(items)
157
- } else if (yield* Cbor.isBytes(stream)) {
158
- return makeByteArrayData(yield* Cbor.decodeBytes(stream))
159
- } else if (yield* Cbor.isMap(stream)) {
160
- const entries = yield* Cbor.decodeMap(decode, decode)(stream)
161
-
162
- return makeMapData(entries)
163
- } else if (yield* Cbor.isConstr(stream)) {
164
- const [tag, fields] = yield* Cbor.decodeConstr(decode)(stream)
165
- return makeConstrData(tag, fields)
166
- } else {
167
- return makeIntData(yield* Cbor.decodeInt(stream))
168
- }
169
- })
170
-
171
- /**
172
- * Simple recursive CBOR encoder
173
- * @param data
174
- * @returns
175
- */
176
- export function encode(data: Data): number[] {
177
- if ("bytes" in data) {
178
- return Cbor.encodeBytes(data.bytes.slice(), true)
179
- } else if ("fields" in data) {
180
- return Cbor.encodeConstr(data.constructor, data.fields.map(encode))
181
- } else if ("int" in data) {
182
- return Cbor.encodeInt(data.int)
183
- } else if ("list" in data) {
184
- return Cbor.encodeList(data.list.map(encode))
185
- } else if ("map" in data) {
186
- return Cbor.encodeMap(data.map.map(({ k, v }) => [encode(k), encode(v)]))
187
- } else {
188
- throw new Error("Unrecognized Uplc.Data type")
189
- }
190
- }
191
-
192
- export const NODE_MEM_SIZE = 4
193
-
194
- /**
195
- * Simple recursive algorithm
196
- * @param data
197
- * @returns
198
- */
199
- export function memSize(data: Data): number {
200
- if ("bytes" in data) {
201
- return NODE_MEM_SIZE + calcBytesMemSize(data.bytes)
202
- } else if ("fields" in data) {
203
- return data.fields.reduce(
204
- (prev, field) => prev + memSize(field),
205
- NODE_MEM_SIZE
206
- )
207
- } else if ("int" in data) {
208
- return NODE_MEM_SIZE + calcIntMemSize(data.int)
209
- } else if ("list" in data) {
210
- return data.list.reduce((prev, item) => prev + memSize(item), NODE_MEM_SIZE)
211
- } else if ("map" in data) {
212
- return data.map.reduce(
213
- (prev, { k, v }) => prev + memSize(k) + memSize(v),
214
- NODE_MEM_SIZE
215
- )
216
- } else {
217
- throw new Error("Unrecognized Uplc.Data type")
218
- }
219
- }
220
-
221
- /**
222
- * Calculates the mem size of a byte array without the DATA_NODE overhead.
223
- * @param bytes
224
- * @returns
225
- */
226
- export function calcBytesMemSize(
227
- bytes: string | readonly number[] | Uint8Array
228
- ): number {
229
- const n = Bytes.toArray(bytes).length
230
-
231
- if (n === 0) {
232
- return 1 // this is so annoying: haskell reference implementation says it should be 0, but current (20220925) testnet and mainnet settings say it's 1
233
- } else {
234
- return Math.floor((n - 1) / 8) + 1
235
- }
236
- }
237
-
238
- /**
239
- * Calculate the mem size of a integer (without the DATA_NODE overhead)
240
- * @param value
241
- * @returns
242
- */
243
- export function calcIntMemSize(value: bigint) {
244
- if (value == 0n) {
245
- return 1
246
- } else {
247
- const abs = value > 0n ? value : -value
248
-
249
- return Math.floor(log2i(abs) / 64) + 1
250
- }
251
- }
252
-
253
- /**
254
- * Math.log2 truncates, but we need a rounding down version
255
- * @param x positive number
256
- * @returns
257
- */
258
- export function log2i(x: bigint): number {
259
- let p = 0
260
-
261
- while (x > 1n) {
262
- x >>= 1n
263
- p++
264
- }
265
-
266
- return p
267
- }
268
-
269
- const BigInt$ = Schema.transformOrFail(Data, Schema.BigIntFromSelf, {
270
- strict: true,
271
- decode: (data) => {
272
- if ("int" in data) {
273
- return ParseResult.succeed(data.int)
274
- } else {
275
- return ParseResult.fail(
276
- new ParseResult.Unexpected(data, "expected IntData")
277
- )
278
- }
279
- },
280
- encode: (value) => ParseResult.succeed({ int: value })
281
- })
282
-
283
- export { BigInt$ as BigInt }
284
-
285
- export const Int = Schema.transformOrFail(Data, Schema.Int, {
286
- strict: true,
287
- decode: (data) => {
288
- if ("int" in data) {
289
- return ParseResult.succeed(Number(data.int))
290
- } else {
291
- return ParseResult.fail(
292
- new ParseResult.Unexpected(data, "expected IntData")
293
- )
294
- }
295
- },
296
- encode: (value) => {
297
- if (value % 1.0 != 0) {
298
- return ParseResult.fail(
299
- new ParseResult.Unexpected(value, "not an integer")
300
- )
301
- } else {
302
- return ParseResult.succeed({ int: BigInt(Math.round(value)) })
303
- }
304
- }
305
- })
306
-
307
- export const ByteArray = Schema.transformOrFail(
308
- Data,
309
- Schema.Uint8ArrayFromSelf,
310
- {
311
- strict: true,
312
- decode: (data) => {
313
- if ("bytes" in data) {
314
- return ParseResult.succeed(data.bytes)
315
- } else {
316
- return ParseResult.fail(
317
- new ParseResult.Unexpected(data, "expected ByteArrayData")
318
- )
319
- }
320
- },
321
- encode: (hex) => ParseResult.succeed({ bytes: hex })
322
- }
323
- )
324
-
325
- export const Hex = Schema.transformOrFail(Data, Schema.String, {
326
- strict: true,
327
- decode: (data) => {
328
- if ("bytes" in data) {
329
- return ParseResult.succeed(Encoding.encodeHex(data.bytes))
330
- } else {
331
- return ParseResult.fail(
332
- new ParseResult.Unexpected(data, "expected ByteArrayData")
333
- )
334
- }
335
- },
336
- encode: (hex) =>
337
- Encoding.decodeHex(hex).pipe(
338
- Effect.map((bs) => ({ bytes: bs })),
339
- Effect.mapError(
340
- (_e) => new ParseResult.Unexpected(hex, "invalid Hex string")
341
- )
342
- )
343
- })
344
-
345
- export const Option = <SomeType>(
346
- someSchema: Schema.Schema<SomeType, Schema.Schema.Encoded<typeof Data>>
347
- ) =>
348
- Schema.transformOrFail(Data, Schema.Option(someSchema), {
349
- strict: true,
350
- decode: (data) => {
351
- if ("fields" in data) {
352
- if (data.constructor == 0) {
353
- if (data.fields.length < 1) {
354
- return ParseResult.fail(
355
- new ParseResult.Unexpected(
356
- data,
357
- "expected at least one field in ConstrData"
358
- )
359
- )
360
- }
361
-
362
- return ParseResult.succeed({
363
- _tag: "Some" as const,
364
- value: data.fields[0]
365
- })
366
- } else if (data.constructor == 1) {
367
- return ParseResult.succeed({ _tag: "None" as const })
368
- } else {
369
- return ParseResult.fail(
370
- new ParseResult.Unexpected(
371
- data,
372
- "expected ConstrData with tag 0 or 1"
373
- )
374
- )
375
- }
376
- } else {
377
- return ParseResult.fail(
378
- new ParseResult.Unexpected(data, "expected ConstrData")
379
- )
380
- }
381
- },
382
- encode: (value) => {
383
- if (value._tag == "None") {
384
- return ParseResult.succeed({ constructor: 1, fields: [] })
385
- } else {
386
- return ParseResult.succeed({ constructor: 0, fields: [value.value] })
387
- }
388
- }
389
- })
390
-
391
- const String$ = Schema.transformOrFail(Data, Schema.String, {
392
- strict: true,
393
- decode: (data) => {
394
- if ("bytes" in data) {
395
- return decodeUtf8(data.bytes).pipe(
396
- Effect.mapError((e) => {
397
- return new ParseResult.Unexpected(data.bytes, e.message)
398
- })
399
- )
400
- } else {
401
- return ParseResult.fail(
402
- new ParseResult.Unexpected(data, "expected ByteArrayData")
403
- )
404
- }
405
- },
406
- encode: (s) => ParseResult.succeed({ bytes: encodeUtf8(s) })
407
- })
408
-
409
- export { String$ as String }
410
-
411
- const Array$ = <ItemType>(
412
- itemSchema: Schema.Schema<ItemType, Schema.Schema.Encoded<typeof Data>>
413
- ) =>
414
- Schema.transformOrFail(Data, Schema.Array(itemSchema), {
415
- strict: true,
416
- decode: (data) => {
417
- if ("list" in data) {
418
- return ParseResult.succeed(data.list)
419
- } else {
420
- return ParseResult.fail(
421
- new ParseResult.Unexpected(data, "expected ListData")
422
- )
423
- }
424
- },
425
- encode: (items) => ParseResult.succeed({ list: items })
426
- })
427
-
428
- export { Array$ as Array }
429
-
430
- export const PairArray = <KeyType, ValueType>(
431
- keySchema: Schema.Schema<KeyType, Schema.Schema.Encoded<typeof Data>>,
432
- valueSchema: Schema.Schema<ValueType, Schema.Schema.Encoded<typeof Data>>
433
- ) =>
434
- Schema.transformOrFail(
435
- Data,
436
- Schema.Array(Schema.Tuple(keySchema, valueSchema)),
437
- {
438
- strict: true,
439
- decode: (data) => {
440
- if ("map" in data) {
441
- return ParseResult.succeed(
442
- data.map.map(({ k, v }) => [k, v] as const)
443
- )
444
- } else {
445
- return ParseResult.fail(
446
- new ParseResult.Unexpected(data, "expected MapData")
447
- )
448
- }
449
- },
450
- encode: (pairs) =>
451
- ParseResult.succeed({ map: pairs.map(([k, v]) => ({ k, v })) })
452
- }
453
- )
454
-
455
- export const Struct = <
456
- FieldTypes extends { [fieldName: string]: Schema.Schema<any, Data> }
457
- >(
458
- fields: FieldTypes
459
- ) =>
460
- Schema.transformOrFail(Data, Schema.Struct(fields), {
461
- strict: true,
462
- decode: (data) => {
463
- if ("list" in data) {
464
- return Effect.all(
465
- Object.entries(fields).map(([fieldName], i) => {
466
- if (i >= data.list.length) {
467
- return Effect.fail(
468
- new ParseResult.Unexpected(
469
- data,
470
- `expected at least ${i + 1} entries in ListData`
471
- )
472
- )
473
- }
474
-
475
- const itemData = data.list[i]
476
-
477
- return Effect.succeed([fieldName, itemData] as [string, Data])
478
- })
479
- ).pipe(Effect.map(Object.fromEntries))
480
- } else {
481
- return ParseResult.fail(
482
- new ParseResult.Unexpected(data, "expected ListData")
483
- )
484
- }
485
- },
486
- encode: (fields) => ParseResult.succeed({ list: Object.values(fields) })
487
- })
488
-
489
- export const EnumVariant = <
490
- FieldTypes extends { [fieldName: string]: Schema.Schema<any, Data> }
491
- >(
492
- tag: number | bigint,
493
- fields: FieldTypes
494
- ) =>
495
- Schema.transformOrFail(Data, Schema.Struct(fields), {
496
- strict: true,
497
- decode: (data) => {
498
- if ("fields" in data) {
499
- if (data.constructor != Number(tag)) {
500
- return ParseResult.fail(
501
- new ParseResult.Unexpected(
502
- data,
503
- `expected ConstrData with constructor tag ${tag}`
504
- )
505
- )
506
- }
507
-
508
- return Effect.all(
509
- Object.entries(fields).map(([fieldName], i) => {
510
- if (i >= data.fields.length) {
511
- return Effect.fail(
512
- new ParseResult.Unexpected(
513
- data,
514
- `expected at least ${i + 1} entries in ConstrData`
515
- )
516
- )
517
- }
518
-
519
- const itemData = data.fields[i]
520
-
521
- return Effect.succeed([fieldName, itemData] as [string, Data])
522
- })
523
- ).pipe(Effect.map(Object.fromEntries))
524
- } else {
525
- return ParseResult.fail(
526
- new ParseResult.Unexpected(data, "expected ConstrData")
527
- )
528
- }
529
- },
530
- encode: (fields) =>
531
- ParseResult.succeed({
532
- constructor: Number(tag),
533
- fields: Object.values(fields)
534
- })
535
- })
536
-
537
- type EnumUnionTypeInternal<
538
- VariantName,
539
- VariantTypes extends {
540
- [variantName: string]: { [fieldName: string]: Schema.Schema<any, Data> }
541
- }
542
- > = VariantName extends string
543
- ? { _tag: VariantName } & {
544
- [FieldName in keyof VariantTypes[VariantName]]: Schema.Schema.Type<
545
- VariantTypes[VariantName][FieldName]
546
- >
547
- }
548
- : never
549
- type EnumUnionType<
550
- VariantTypes extends {
551
- [variantName: string]: { [fieldName: string]: Schema.Schema<any, Data> }
552
- }
553
- > = EnumUnionTypeInternal<keyof VariantTypes, VariantTypes>
554
- type EnumUnionDataInternal<
555
- VariantName,
556
- VariantTypes extends {
557
- [variantName: string]: { [fieldName: string]: Schema.Schema<any, Data> }
558
- }
559
- > = VariantName extends string
560
- ? { _tag: VariantName } & {
561
- [FieldName in keyof VariantTypes[VariantName]]: Data
562
- }
563
- : never
564
- type EnumUnionData<
565
- VariantTypes extends {
566
- [variantName: string]: { [fieldName: string]: Schema.Schema<any, Data> }
567
- }
568
- > = EnumUnionDataInternal<keyof VariantTypes, VariantTypes>
569
-
570
- export const Enum = <
571
- VariantTypes extends {
572
- [variantName: string]: { [fieldName: string]: Schema.Schema<any, Data> }
573
- }
574
- >(
575
- variants: VariantTypes
576
- ): Schema.Schema<EnumUnionType<VariantTypes>, Data> =>
577
- Schema.transformOrFail(
578
- Data,
579
- Schema.Union(
580
- ...Object.entries(variants).map(
581
- ([variantName, fieldSchemas]) =>
582
- Schema.TaggedStruct(
583
- variantName,
584
- fieldSchemas
585
- ) as unknown as Schema.Schema<
586
- EnumUnionType<VariantTypes>,
587
- EnumUnionData<VariantTypes>
588
- >
589
- )
590
- ),
591
- {
592
- strict: true,
593
- decode: (
594
- data
595
- ): Effect.Effect<EnumUnionData<VariantTypes>, ParseResult.ParseIssue> => {
596
- if ("fields" in data) {
597
- const tag = data.constructor
598
-
599
- const variantName: keyof VariantTypes = Object.keys(variants)[tag]
600
-
601
- if ((variantName as string | undefined) == undefined) {
602
- return ParseResult.fail(
603
- new ParseResult.Unexpected(
604
- data,
605
- `no variant defined for tag ${tag}`
606
- )
607
- )
608
- }
609
-
610
- const fields = variants[variantName]
611
-
612
- return Effect.all(
613
- Object.entries(fields).map(([fieldName], i) => {
614
- if (i >= data.fields.length) {
615
- return Effect.fail(
616
- new ParseResult.Unexpected(
617
- data,
618
- `expected at least ${i + 1} entries in ConstrData of ${variantName as unknown as string}`
619
- )
620
- )
621
- }
622
-
623
- const itemData = data.fields[i]
624
-
625
- return Effect.succeed([fieldName, itemData] as [string, Data])
626
- })
627
- ).pipe(
628
- Effect.map(
629
- (entries) =>
630
- ({
631
- _tag: variantName,
632
- ...Object.fromEntries(entries)
633
- }) as unknown as EnumUnionData<VariantTypes>
634
- )
635
- )
636
- } else {
637
- return ParseResult.fail(
638
- new ParseResult.Unexpected(data, "expected ConstrData")
639
- )
640
- }
641
- },
642
- encode: (value) => {
643
- const variantName = value._tag
644
-
645
- const tag = Object.keys(variants).indexOf(
646
- variantName as unknown as string
647
- )
648
-
649
- return ParseResult.succeed({
650
- constructor: tag,
651
- fields: Object.entries(value)
652
- .filter(([key]) => key != "_tag")
653
- .map(([, field]) => field) as Data[]
654
- })
655
- }
656
- }
657
- )
@@ -1,56 +0,0 @@
1
- import { Brand, Schema } from "effect"
2
-
3
- export type Bool = boolean & Brand.Brand<"Bool">
4
-
5
- export const makeBool = Brand.nominal<Bool>()
6
-
7
- export type ByteArray = number[] & Brand.Brand<"ByteArray">
8
-
9
- export const makeByteArray = Brand.nominal<ByteArray>()
10
-
11
- export const Int = Schema.BigInt.pipe(Schema.brand("Int"))
12
-
13
- export type Int = bigint & Brand.Brand<"Int">
14
-
15
- export const makeInt = Brand.nominal<Int>()
16
-
17
- export type List = {
18
- readonly _tag: "List"
19
- readonly itemType: string // needed for empty lists
20
- readonly items: Primitive[]
21
- }
22
-
23
- export function makeList(itemType: string, items: Primitive[]): List {
24
- return {
25
- _tag: "List",
26
- itemType,
27
- items
28
- }
29
- }
30
-
31
- export type Pair = {
32
- readonly _tag: "Pair"
33
- readonly first: Primitive
34
- readonly second: Primitive
35
- }
36
-
37
- export function makePair(first: Primitive, second: Primitive): Pair {
38
- return {
39
- _tag: "Pair",
40
- first,
41
- second
42
- }
43
- }
44
-
45
- export type String = string & Brand.Brand<"String">
46
-
47
- export const makeString = Brand.nominal<String>()
48
-
49
- export type Unit = Brand.Brand<"Unit">
50
-
51
- const makeUnitInternal = Brand.nominal<Unit>()
52
-
53
- export const makeUnit = () => makeUnitInternal({})
54
-
55
- // TODO: add Bls12_381 primitives
56
- export type Primitive = Bool | ByteArray | Int | List | Pair | String | Unit
package/src/Uplc/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * as Data from "./Data.js"
2
- export * as Primitive from "./Primitive.js"
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * as Bech32 from "./Bech32.js"
2
- export * as Cbor from "./Cbor.js"
3
- export * as Ledger from "./Ledger"
4
- export * as Uplc from "./Uplc"