@helios-lang/effect 0.1.1 → 0.1.2
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/dist/Ledger/Address.js +153 -0
- package/dist/Ledger/Address.js.map +1 -0
- package/dist/Ledger/Credential.js +17 -0
- package/dist/Ledger/Credential.js.map +1 -0
- package/dist/Ledger/PubKeyHash.js +21 -0
- package/dist/Ledger/PubKeyHash.js.map +1 -0
- package/dist/Ledger/ValidatorHash.js +21 -0
- package/dist/Ledger/ValidatorHash.js.map +1 -0
- package/dist/Ledger/index.js +5 -0
- package/dist/Ledger/index.js.map +1 -0
- package/dist/Uplc/Data.js +209 -21
- package/dist/Uplc/Data.js.map +1 -1
- package/dist/Uplc/index.js +0 -1
- package/dist/Uplc/index.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +13 -10
- package/src/Ledger/Address.ts +238 -0
- package/src/Ledger/Credential.ts +29 -0
- package/src/Ledger/PubKeyHash.ts +36 -0
- package/src/Ledger/ValidatorHash.ts +36 -0
- package/src/Ledger/index.ts +4 -0
- package/src/Uplc/Data.test.ts +321 -0
- package/src/Uplc/Data.ts +418 -47
- package/src/Uplc/index.ts +0 -1
- package/src/index.ts +1 -1
- package/src/Address.ts +0 -20
- package/src/Uplc/DataSchema.test.ts +0 -207
- package/src/Uplc/DataSchema.ts +0 -181
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "bun:test"
|
|
2
|
-
import { Schema } from "effect"
|
|
3
|
-
import * as DataSchema from "./DataSchema.js"
|
|
4
|
-
|
|
5
|
-
describe("Uplc.DataSchema.BigInt", () => {
|
|
6
|
-
it("succeeds for {int: 0n}", () => {
|
|
7
|
-
expect(Schema.decodeSync(DataSchema.BigInt)({ int: 0n })).toBe(0n)
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it("fails for {int: '0'}", () => {
|
|
11
|
-
expect(() =>
|
|
12
|
-
Schema.decodeUnknownSync(DataSchema.BigInt)({ int: "0" })
|
|
13
|
-
).toThrow()
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
describe("Uplc.DataSchema.Int", () => {
|
|
18
|
-
it("succeeds for {int: 0n}", () => {
|
|
19
|
-
expect(Schema.decodeSync(DataSchema.Int)({ int: 0n })).toBe(0)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it("fails for {int: '0'}", () => {
|
|
23
|
-
expect(() =>
|
|
24
|
-
Schema.decodeUnknownSync(DataSchema.Int)({ int: "0" })
|
|
25
|
-
).toThrow()
|
|
26
|
-
})
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
describe("Uplc.DataSchema.String", () => {
|
|
30
|
-
it("succeeds for {bytes: ''}", () => {
|
|
31
|
-
expect(Schema.decodeSync(DataSchema.String)({ bytes: "" })).toBe("")
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it("fails for {bytes: 'ff'}", () => {
|
|
35
|
-
expect(() =>
|
|
36
|
-
Schema.decodeSync(DataSchema.String)({ bytes: "ff" })
|
|
37
|
-
).toThrow()
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it("succeeds for {bytes: '48656C6C6F20576F726C64'}", () => {
|
|
41
|
-
expect(
|
|
42
|
-
Schema.decodeSync(DataSchema.String)({ bytes: "48656C6C6F20576F726C64" })
|
|
43
|
-
).toBe("Hello World")
|
|
44
|
-
})
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
describe("Uplc.DataSchema.Array", () => {
|
|
48
|
-
it("succeeds for empty ListData", () => {
|
|
49
|
-
expect(
|
|
50
|
-
Schema.decodeSync(DataSchema.Array(DataSchema.String))({ list: [] })
|
|
51
|
-
).toEqual([])
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it("succeeds for ListData containg single 'Hello World' string", () => {
|
|
55
|
-
expect(
|
|
56
|
-
Schema.decodeSync(DataSchema.Array(DataSchema.String))({
|
|
57
|
-
list: [{ bytes: "48656C6C6F20576F726C64" }]
|
|
58
|
-
})
|
|
59
|
-
).toEqual(["Hello World"])
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it("fails if ListData items are heterogenous", () => {
|
|
63
|
-
expect(() =>
|
|
64
|
-
Schema.decodeSync(DataSchema.Array(DataSchema.String))({
|
|
65
|
-
list: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }]
|
|
66
|
-
})
|
|
67
|
-
).toThrow()
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
describe("Uplc.DataSchema.Struct", () => {
|
|
72
|
-
it("succeeds for empty ListData for empty Struct", () => {
|
|
73
|
-
expect(Schema.decodeSync(DataSchema.Struct({}))({ list: [] })).toEqual({})
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it("fails for empty ListData if one field is defined", () => {
|
|
77
|
-
expect(() =>
|
|
78
|
-
Schema.decodeSync(DataSchema.Struct({ foo: DataSchema.String }))({
|
|
79
|
-
list: []
|
|
80
|
-
})
|
|
81
|
-
).toThrow()
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
it("succeeds for ListData with single entry if one field is defined", () => {
|
|
85
|
-
expect(
|
|
86
|
-
Schema.decodeSync(DataSchema.Struct({ foo: DataSchema.String }))({
|
|
87
|
-
list: [{ bytes: "48656C6C6F20576F726C64" }]
|
|
88
|
-
})
|
|
89
|
-
).toEqual({ foo: "Hello World" })
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it("fails for ListData with wrong entry in first place with one field is defined", () => {
|
|
93
|
-
expect(() =>
|
|
94
|
-
Schema.decodeSync(DataSchema.Struct({ foo: DataSchema.String }))({
|
|
95
|
-
list: [{ int: 0n }]
|
|
96
|
-
})
|
|
97
|
-
).toThrow()
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
it("succeeds for ListData with spurious entries at end with one field is defined", () => {
|
|
101
|
-
expect(
|
|
102
|
-
Schema.decodeSync(DataSchema.Struct({ foo: DataSchema.String }))({
|
|
103
|
-
list: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }]
|
|
104
|
-
})
|
|
105
|
-
).toEqual({ foo: "Hello World" })
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
it("succeeds for ListData with two entries with two fields", () => {
|
|
109
|
-
expect(
|
|
110
|
-
Schema.decodeSync(
|
|
111
|
-
DataSchema.Struct({ foo: DataSchema.String, bar: DataSchema.Int })
|
|
112
|
-
)({ list: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }] })
|
|
113
|
-
).toEqual({ foo: "Hello World", bar: 0 })
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it("fails for ListData with two entries in wrong order with two fields", () => {
|
|
117
|
-
expect(() =>
|
|
118
|
-
Schema.decodeSync(
|
|
119
|
-
DataSchema.Struct({ bar: DataSchema.Int, foo: DataSchema.String })
|
|
120
|
-
)({ list: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }] })
|
|
121
|
-
).toThrow()
|
|
122
|
-
})
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
describe("Uplc.DataSchema.EnumVariant", () => {
|
|
126
|
-
it("succeeds for empty ConstrData for empty EnumVariant", () => {
|
|
127
|
-
expect(
|
|
128
|
-
Schema.decodeSync(DataSchema.EnumVariant(0, {}))({
|
|
129
|
-
constructor: 0,
|
|
130
|
-
fields: []
|
|
131
|
-
})
|
|
132
|
-
).toEqual({})
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
it("fields for ConstrData with wrong tag", () => {
|
|
136
|
-
expect(() =>
|
|
137
|
-
Schema.decodeSync(DataSchema.EnumVariant(0, {}))({
|
|
138
|
-
constructor: 1,
|
|
139
|
-
fields: []
|
|
140
|
-
})
|
|
141
|
-
).toThrow()
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
it("fails for empty ConstrData if one field is defined", () => {
|
|
145
|
-
expect(() =>
|
|
146
|
-
Schema.decodeSync(DataSchema.EnumVariant(0, { foo: DataSchema.String }))({
|
|
147
|
-
constructor: 0,
|
|
148
|
-
fields: []
|
|
149
|
-
})
|
|
150
|
-
).toThrow()
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
it("succeeds for ConstrData with single entry if one field is defined", () => {
|
|
154
|
-
expect(
|
|
155
|
-
Schema.decodeSync(DataSchema.EnumVariant(0, { foo: DataSchema.String }))({
|
|
156
|
-
constructor: 0,
|
|
157
|
-
fields: [{ bytes: "48656C6C6F20576F726C64" }]
|
|
158
|
-
})
|
|
159
|
-
).toEqual({ foo: "Hello World" })
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
it("fails for ConstrData with wrong entry in first place with one field is defined", () => {
|
|
163
|
-
expect(() =>
|
|
164
|
-
Schema.decodeSync(DataSchema.EnumVariant(0, { foo: DataSchema.String }))({
|
|
165
|
-
constructor: 0,
|
|
166
|
-
fields: [{ int: 0n }]
|
|
167
|
-
})
|
|
168
|
-
).toThrow()
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
it("succeeds for EnumVariant with spurious entries at end with one field is defined", () => {
|
|
172
|
-
expect(
|
|
173
|
-
Schema.decodeSync(DataSchema.EnumVariant(0, { foo: DataSchema.String }))({
|
|
174
|
-
constructor: 0,
|
|
175
|
-
fields: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }]
|
|
176
|
-
})
|
|
177
|
-
).toEqual({ foo: "Hello World" })
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
it("succeeds for EnumVariant with two entries with two fields", () => {
|
|
181
|
-
expect(
|
|
182
|
-
Schema.decodeSync(
|
|
183
|
-
DataSchema.EnumVariant(0, {
|
|
184
|
-
foo: DataSchema.String,
|
|
185
|
-
bar: DataSchema.Int
|
|
186
|
-
})
|
|
187
|
-
)({
|
|
188
|
-
constructor: 0,
|
|
189
|
-
fields: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }]
|
|
190
|
-
})
|
|
191
|
-
).toEqual({ foo: "Hello World", bar: 0 })
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
it("fails for EnumVariant with two entries in wrong order with two fields", () => {
|
|
195
|
-
expect(() =>
|
|
196
|
-
Schema.decodeSync(
|
|
197
|
-
DataSchema.EnumVariant(0, {
|
|
198
|
-
bar: DataSchema.Int,
|
|
199
|
-
foo: DataSchema.String
|
|
200
|
-
})
|
|
201
|
-
)({
|
|
202
|
-
constructor: 0,
|
|
203
|
-
fields: [{ bytes: "48656C6C6F20576F726C64" }, { int: 0n }]
|
|
204
|
-
})
|
|
205
|
-
).toThrow()
|
|
206
|
-
})
|
|
207
|
-
})
|
package/src/Uplc/DataSchema.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { Effect, Encoding, ParseResult, Schema } from "effect"
|
|
2
|
-
import { decode as decodeUtf8, encode as encodeUtf8 } from "../internal/Utf8.js"
|
|
3
|
-
import { DataUnencoded as Data } from "./Data.js"
|
|
4
|
-
|
|
5
|
-
const BigIntSchema = Schema.transformOrFail(Data, Schema.BigIntFromSelf, {
|
|
6
|
-
strict: true,
|
|
7
|
-
decode: (data) => {
|
|
8
|
-
if ("int" in data) {
|
|
9
|
-
return ParseResult.succeed(data.int)
|
|
10
|
-
} else {
|
|
11
|
-
return ParseResult.fail(
|
|
12
|
-
new ParseResult.Unexpected(data, "expected IntData")
|
|
13
|
-
)
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
encode: (value) => ParseResult.succeed({ int: value })
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
export { BigIntSchema as BigInt }
|
|
20
|
-
|
|
21
|
-
export const Int = Schema.transformOrFail(Data, Schema.Int, {
|
|
22
|
-
strict: true,
|
|
23
|
-
decode: (data) => {
|
|
24
|
-
if ("int" in data) {
|
|
25
|
-
return ParseResult.succeed(Number(data.int))
|
|
26
|
-
} else {
|
|
27
|
-
return ParseResult.fail(
|
|
28
|
-
new ParseResult.Unexpected(data, "expected IntData")
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
encode: (value) => {
|
|
33
|
-
if (value % 1.0 != 0) {
|
|
34
|
-
return ParseResult.fail(
|
|
35
|
-
new ParseResult.Unexpected(value, "not an integer")
|
|
36
|
-
)
|
|
37
|
-
} else {
|
|
38
|
-
return ParseResult.succeed({ int: BigInt(Math.round(value)) })
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
export const ByteArray = Schema.transformOrFail(
|
|
44
|
-
Data,
|
|
45
|
-
Schema.Uint8ArrayFromHex,
|
|
46
|
-
{
|
|
47
|
-
strict: true,
|
|
48
|
-
decode: (data) => {
|
|
49
|
-
if ("bytes" in data) {
|
|
50
|
-
return ParseResult.succeed(data.bytes)
|
|
51
|
-
} else {
|
|
52
|
-
return ParseResult.fail(
|
|
53
|
-
new ParseResult.Unexpected(data, "expected ByteArrayData")
|
|
54
|
-
)
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
encode: (hex) => ParseResult.succeed({ bytes: hex })
|
|
58
|
-
}
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
const StringSchema = Schema.transformOrFail(Data, Schema.String, {
|
|
62
|
-
strict: true,
|
|
63
|
-
decode: (data) => {
|
|
64
|
-
if ("bytes" in data) {
|
|
65
|
-
return decodeUtf8(data.bytes).pipe(
|
|
66
|
-
Effect.mapError((e) => {
|
|
67
|
-
return new ParseResult.Unexpected(data.bytes, e.message)
|
|
68
|
-
})
|
|
69
|
-
)
|
|
70
|
-
} else {
|
|
71
|
-
return ParseResult.fail(
|
|
72
|
-
new ParseResult.Unexpected(data, "expected ByteArrayData")
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
encode: (s) =>
|
|
77
|
-
ParseResult.succeed({ bytes: Encoding.encodeHex(encodeUtf8(s)) })
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
export { StringSchema as String }
|
|
81
|
-
|
|
82
|
-
const ArraySchema = <ItemType>(
|
|
83
|
-
itemSchema: Schema.Schema<ItemType, Schema.Schema.Encoded<typeof Data>>
|
|
84
|
-
) =>
|
|
85
|
-
Schema.transformOrFail(Data, Schema.Array(itemSchema), {
|
|
86
|
-
strict: true,
|
|
87
|
-
decode: (data) => {
|
|
88
|
-
if ("list" in data) {
|
|
89
|
-
return ParseResult.succeed(data.list)
|
|
90
|
-
} else {
|
|
91
|
-
return ParseResult.fail(
|
|
92
|
-
new ParseResult.Unexpected(data, "expected ListData")
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
encode: (items) => ParseResult.succeed({ list: items })
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
export { ArraySchema as Array }
|
|
100
|
-
|
|
101
|
-
export const Struct = <
|
|
102
|
-
FieldTypes extends { [key: string]: any }
|
|
103
|
-
>(fieldSchemas: {
|
|
104
|
-
[FieldName in keyof FieldTypes]: Schema.Schema<FieldTypes[FieldName], Data>
|
|
105
|
-
}) =>
|
|
106
|
-
Schema.transformOrFail(Data, Schema.Struct(fieldSchemas), {
|
|
107
|
-
strict: true,
|
|
108
|
-
decode: (data) => {
|
|
109
|
-
if ("list" in data) {
|
|
110
|
-
return Effect.all(
|
|
111
|
-
Object.entries(fieldSchemas).map(([fieldName], i) => {
|
|
112
|
-
if (i >= data.list.length) {
|
|
113
|
-
return Effect.fail(
|
|
114
|
-
new ParseResult.Unexpected(
|
|
115
|
-
data,
|
|
116
|
-
`expected at least ${i + 1} entries in ListData`
|
|
117
|
-
)
|
|
118
|
-
)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const itemData = data.list[i]
|
|
122
|
-
|
|
123
|
-
return Effect.succeed([fieldName, itemData] as [string, Data])
|
|
124
|
-
})
|
|
125
|
-
).pipe(Effect.map(Object.fromEntries))
|
|
126
|
-
} else {
|
|
127
|
-
return ParseResult.fail(
|
|
128
|
-
new ParseResult.Unexpected(data, "expected ListData")
|
|
129
|
-
)
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
encode: (fields) => ParseResult.succeed({ list: Object.values(fields) })
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
export const EnumVariant = <FieldTypes extends { [key: string]: any }>(
|
|
136
|
-
tag: number | bigint,
|
|
137
|
-
fieldSchemas: {
|
|
138
|
-
[FieldName in keyof FieldTypes]: Schema.Schema<FieldTypes[FieldName], Data>
|
|
139
|
-
}
|
|
140
|
-
) =>
|
|
141
|
-
Schema.transformOrFail(Data, Schema.Struct(fieldSchemas), {
|
|
142
|
-
strict: true,
|
|
143
|
-
decode: (data) => {
|
|
144
|
-
if ("fields" in data) {
|
|
145
|
-
if (data.constructor != Number(tag)) {
|
|
146
|
-
return ParseResult.fail(
|
|
147
|
-
new ParseResult.Unexpected(
|
|
148
|
-
data,
|
|
149
|
-
`expected ConstrData with constructor tag ${tag}`
|
|
150
|
-
)
|
|
151
|
-
)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return Effect.all(
|
|
155
|
-
Object.entries(fieldSchemas).map(([fieldName], i) => {
|
|
156
|
-
if (i >= data.fields.length) {
|
|
157
|
-
return Effect.fail(
|
|
158
|
-
new ParseResult.Unexpected(
|
|
159
|
-
data,
|
|
160
|
-
`expected at least ${i + 1} entries in ConstrData`
|
|
161
|
-
)
|
|
162
|
-
)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const itemData = data.fields[i]
|
|
166
|
-
|
|
167
|
-
return Effect.succeed([fieldName, itemData] as [string, Data])
|
|
168
|
-
})
|
|
169
|
-
).pipe(Effect.map(Object.fromEntries))
|
|
170
|
-
} else {
|
|
171
|
-
return ParseResult.fail(
|
|
172
|
-
new ParseResult.Unexpected(data, "expected ConstrData")
|
|
173
|
-
)
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
encode: (fields) =>
|
|
177
|
-
ParseResult.succeed({
|
|
178
|
-
constructor: Number(tag),
|
|
179
|
-
fields: Object.values(fields)
|
|
180
|
-
})
|
|
181
|
-
})
|