@helios-lang/effect 0.1.4 → 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.
- package/bun.lock +60 -0
- package/dist/Ledger/NetworkParams.js +40 -0
- package/dist/Ledger/NetworkParams.js.map +1 -0
- package/dist/Ledger/index.js +1 -0
- package/dist/Ledger/index.js.map +1 -1
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.check.tsbuildinfo +1 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types/Address.d.ts +5 -0
- package/types/Address.d.ts.map +1 -0
- package/types/Bech32.d.ts +30 -0
- package/types/Bech32.d.ts.map +1 -0
- package/types/Cbor.d.ts +430 -0
- package/types/Cbor.d.ts.map +1 -0
- package/types/Ledger/Address.d.ts +109 -0
- package/types/Ledger/Address.d.ts.map +1 -0
- package/types/Ledger/AssetClass.d.ts +101 -0
- package/types/Ledger/AssetClass.d.ts.map +1 -0
- package/types/Ledger/Assets.d.ts +70 -0
- package/types/Ledger/Assets.d.ts.map +1 -0
- package/types/Ledger/Credential.d.ts +26 -0
- package/types/Ledger/Credential.d.ts.map +1 -0
- package/types/Ledger/DatumHash.d.ts +40 -0
- package/types/Ledger/DatumHash.d.ts.map +1 -0
- package/types/Ledger/IsMainnet.d.ts +6 -0
- package/types/Ledger/IsMainnet.d.ts.map +1 -0
- package/types/Ledger/MintingPolicy.d.ts +39 -0
- package/types/Ledger/MintingPolicy.d.ts.map +1 -0
- package/types/Ledger/NetworkParams.d.ts +40 -0
- package/types/Ledger/NetworkParams.d.ts.map +1 -0
- package/types/Ledger/PubKeyHash.d.ts +40 -0
- package/types/Ledger/PubKeyHash.d.ts.map +1 -0
- package/types/Ledger/TxId.d.ts +10 -0
- package/types/Ledger/TxId.d.ts.map +1 -0
- package/types/Ledger/TxInput.d.ts +55 -0
- package/types/Ledger/TxInput.d.ts.map +1 -0
- package/types/Ledger/TxOutput.d.ts +63 -0
- package/types/Ledger/TxOutput.d.ts.map +1 -0
- package/types/Ledger/TxOutputDatum.d.ts +41 -0
- package/types/Ledger/TxOutputDatum.d.ts.map +1 -0
- package/types/Ledger/TxOutputId.d.ts +14 -0
- package/types/Ledger/TxOutputId.d.ts.map +1 -0
- package/types/Ledger/ValidatorHash.d.ts +40 -0
- package/types/Ledger/ValidatorHash.d.ts.map +1 -0
- package/types/Ledger/index.d.ts +16 -0
- package/types/Ledger/index.d.ts.map +1 -0
- package/types/Uplc/Cek.d.ts +72 -0
- package/types/Uplc/Cek.d.ts.map +1 -0
- package/types/Uplc/Data.d.ts +530 -0
- package/types/Uplc/Data.d.ts.map +1 -0
- package/types/Uplc/DataSchema.d.ts +227 -0
- package/types/Uplc/DataSchema.d.ts.map +1 -0
- package/types/Uplc/Primitive.d.ts +26 -0
- package/types/Uplc/Primitive.d.ts.map +1 -0
- package/types/Uplc/index.d.ts +3 -0
- package/types/Uplc/index.d.ts.map +1 -0
- package/types/index.d.ts +5 -0
- package/types/index.d.ts.map +1 -0
- package/types/internal/Base32.d.ts +49 -0
- package/types/internal/Base32.d.ts.map +1 -0
- package/types/internal/BigEndian.d.ts +22 -0
- package/types/internal/BigEndian.d.ts.map +1 -0
- package/types/internal/Bits.d.ts +123 -0
- package/types/internal/Bits.d.ts.map +1 -0
- package/types/internal/Bytes.d.ts +88 -0
- package/types/internal/Bytes.d.ts.map +1 -0
- package/types/internal/Flat.d.ts +71 -0
- package/types/internal/Flat.d.ts.map +1 -0
- package/types/internal/Float.d.ts +38 -0
- package/types/internal/Float.d.ts.map +1 -0
- package/types/internal/Utf8.d.ts +24 -0
- package/types/internal/Utf8.d.ts.map +1 -0
- package/src/Bech32.test.ts +0 -117
- package/src/Bech32.ts +0 -198
- package/src/Cbor.test.ts +0 -1610
- package/src/Cbor.ts +0 -1704
- package/src/Ledger/Address.ts +0 -248
- package/src/Ledger/AssetClass.ts +0 -90
- package/src/Ledger/Assets.ts +0 -164
- package/src/Ledger/Credential.ts +0 -29
- package/src/Ledger/DatumHash.ts +0 -36
- package/src/Ledger/IsMainnet.ts +0 -6
- package/src/Ledger/MintingPolicy.ts +0 -57
- package/src/Ledger/PubKeyHash.ts +0 -36
- package/src/Ledger/TxId.ts +0 -31
- package/src/Ledger/TxInput.test.ts +0 -21
- package/src/Ledger/TxInput.ts +0 -66
- package/src/Ledger/TxOutput.ts +0 -166
- package/src/Ledger/TxOutputDatum.ts +0 -64
- package/src/Ledger/TxOutputId.ts +0 -63
- package/src/Ledger/ValidatorHash.ts +0 -36
- package/src/Ledger/index.ts +0 -14
- package/src/Uplc/Cek.ts +0 -92
- package/src/Uplc/Data.test.ts +0 -321
- package/src/Uplc/Data.ts +0 -657
- package/src/Uplc/Primitive.ts +0 -56
- package/src/Uplc/index.ts +0 -2
- package/src/index.ts +0 -4
- package/src/internal/Base32.test.ts +0 -219
- package/src/internal/Base32.ts +0 -341
- package/src/internal/BigEndian.test.ts +0 -79
- package/src/internal/BigEndian.ts +0 -67
- package/src/internal/Bits.test.ts +0 -300
- package/src/internal/Bits.ts +0 -398
- package/src/internal/Bytes.test.ts +0 -369
- package/src/internal/Bytes.ts +0 -343
- package/src/internal/Flat.test.ts +0 -29
- package/src/internal/Flat.ts +0 -387
- package/src/internal/Float.test.ts +0 -51
- package/src/internal/Float.ts +0 -190
- package/src/internal/Utf8.test.ts +0 -69
- package/src/internal/Utf8.ts +0 -58
package/src/Ledger/Address.ts
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import { Effect, Option, ParseResult, Schema } from "effect"
|
|
2
|
-
import * as Bech32 from "../Bech32.js"
|
|
3
|
-
import * as Cbor from "../Cbor.js"
|
|
4
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
5
|
-
import { Data } from "../Uplc/index.js"
|
|
6
|
-
import * as Credential from "./Credential.js"
|
|
7
|
-
import { IsMainnet } from "./IsMainnet.js"
|
|
8
|
-
import * as PubKeyHash from "./PubKeyHash.js"
|
|
9
|
-
import * as ValidatorHash from "./ValidatorHash.js"
|
|
10
|
-
|
|
11
|
-
export function isValid(addr: string): boolean {
|
|
12
|
-
if (addr.startsWith("addr1") || addr.startsWith("addr_test1")) {
|
|
13
|
-
return Bech32.isValid(addr) // TODO: full validation
|
|
14
|
-
}
|
|
15
|
-
// TODO: validate Byron format
|
|
16
|
-
|
|
17
|
-
return false
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const Address = Schema.String.pipe(
|
|
21
|
-
Schema.filter((addr: string) => {
|
|
22
|
-
return isValid(addr) || "Invalid Cardano Address"
|
|
23
|
-
}),
|
|
24
|
-
Schema.brand("Address")
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
export type Address = Schema.Schema.Type<typeof Address>
|
|
28
|
-
|
|
29
|
-
export const FromUplcData = Schema.transformOrFail(
|
|
30
|
-
Data.EnumVariant(0, {
|
|
31
|
-
spendingCredential: Credential.FromUplcData,
|
|
32
|
-
stakingCredential: Data.Option(Credential.FromUplcData)
|
|
33
|
-
}),
|
|
34
|
-
Address,
|
|
35
|
-
{
|
|
36
|
-
strict: true,
|
|
37
|
-
decode: (data) =>
|
|
38
|
-
Effect.gen(function* () {
|
|
39
|
-
const isMainnet = yield* IsMainnet
|
|
40
|
-
|
|
41
|
-
return make(
|
|
42
|
-
isMainnet,
|
|
43
|
-
data.spendingCredential,
|
|
44
|
-
data.stakingCredential._tag == "Some"
|
|
45
|
-
? data.stakingCredential.value
|
|
46
|
-
: undefined
|
|
47
|
-
)
|
|
48
|
-
}),
|
|
49
|
-
encode: (address) => {
|
|
50
|
-
const { spendingCredential, stakingCredential } = Effect.runSync(
|
|
51
|
-
decodeInternal(address)
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
return ParseResult.succeed({
|
|
55
|
-
spendingCredential,
|
|
56
|
-
stakingCredential: stakingCredential
|
|
57
|
-
? Option.some(stakingCredential)
|
|
58
|
-
: Option.none()
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
export function make(
|
|
65
|
-
isMainnet: boolean,
|
|
66
|
-
spendingCredential: Credential.Credential,
|
|
67
|
-
stakingCredential?: Credential.Credential
|
|
68
|
-
): Address {
|
|
69
|
-
const prefix = isMainnet ? "addr" : "addr_test"
|
|
70
|
-
const bytes: number[] = toShelleyBytes(
|
|
71
|
-
isMainnet,
|
|
72
|
-
spendingCredential,
|
|
73
|
-
stakingCredential
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
const s: string = Bech32.encode(prefix, bytes)
|
|
77
|
-
|
|
78
|
-
return s as Address
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// returns the byte representation of an Address
|
|
82
|
-
function toShelleyBytes(
|
|
83
|
-
isMainnet: boolean,
|
|
84
|
-
spendingCredential: Credential.Credential,
|
|
85
|
-
stakingCredential?: Credential.Credential
|
|
86
|
-
): number[] {
|
|
87
|
-
const spendingCredBytes = Bytes.toArray(spendingCredential.hash)
|
|
88
|
-
|
|
89
|
-
if (stakingCredential) {
|
|
90
|
-
const stakingCredBytes = Bytes.toArray(stakingCredential.hash)
|
|
91
|
-
|
|
92
|
-
if (spendingCredential._tag == "PubKey") {
|
|
93
|
-
if (stakingCredential._tag == "PubKey") {
|
|
94
|
-
return [isMainnet ? 0x01 : 0x00]
|
|
95
|
-
.concat(spendingCredBytes)
|
|
96
|
-
.concat(stakingCredBytes)
|
|
97
|
-
} else {
|
|
98
|
-
return [isMainnet ? 0x21 : 0x20]
|
|
99
|
-
.concat(spendingCredBytes)
|
|
100
|
-
.concat(stakingCredBytes)
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
if (stakingCredential._tag == "PubKey") {
|
|
104
|
-
return [isMainnet ? 0x11 : 0x10]
|
|
105
|
-
.concat(spendingCredBytes)
|
|
106
|
-
.concat(stakingCredBytes)
|
|
107
|
-
} else {
|
|
108
|
-
return [isMainnet ? 0x31 : 0x30]
|
|
109
|
-
.concat(spendingCredBytes)
|
|
110
|
-
.concat(stakingCredBytes)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
} else if (spendingCredential._tag == "PubKey") {
|
|
114
|
-
return [isMainnet ? 0x61 : 0x60].concat(spendingCredBytes)
|
|
115
|
-
} else {
|
|
116
|
-
return [isMainnet ? 0x71 : 0x70].concat(spendingCredBytes)
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const decodeInternal = (bytes: Bytes.BytesLike) =>
|
|
121
|
-
Effect.gen(function* () {
|
|
122
|
-
if (typeof bytes == "string" && bytes.startsWith("addr")) {
|
|
123
|
-
bytes = (yield* Bech32.decode(bytes)).bytes
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const innerBytes = (yield* Cbor.isBytes(bytes))
|
|
127
|
-
? yield* Cbor.decodeBytes(bytes)
|
|
128
|
-
: Bytes.toArray(bytes)
|
|
129
|
-
|
|
130
|
-
const head = innerBytes[0]
|
|
131
|
-
|
|
132
|
-
const isMainnet = (head & 0b00001111) != 0
|
|
133
|
-
|
|
134
|
-
const type = head & 0b11110000
|
|
135
|
-
|
|
136
|
-
const firstPart = () => {
|
|
137
|
-
return innerBytes.slice(1, 29)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const secondPart = () => {
|
|
141
|
-
return innerBytes.slice(29, 57)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
switch (type) {
|
|
145
|
-
case 0x00:
|
|
146
|
-
return {
|
|
147
|
-
isMainnet,
|
|
148
|
-
spendingCredential: Credential.makePubKey(
|
|
149
|
-
yield* PubKeyHash.make(firstPart())
|
|
150
|
-
),
|
|
151
|
-
stakingCredential: Credential.makePubKey(
|
|
152
|
-
yield* PubKeyHash.make(secondPart())
|
|
153
|
-
)
|
|
154
|
-
}
|
|
155
|
-
case 0x10:
|
|
156
|
-
return {
|
|
157
|
-
isMainnet,
|
|
158
|
-
spendingCredential: Credential.makeValidator(
|
|
159
|
-
yield* ValidatorHash.make(firstPart())
|
|
160
|
-
),
|
|
161
|
-
stakingCredential: Credential.makePubKey(
|
|
162
|
-
yield* PubKeyHash.make(secondPart())
|
|
163
|
-
)
|
|
164
|
-
}
|
|
165
|
-
case 0x20:
|
|
166
|
-
return {
|
|
167
|
-
isMainnet,
|
|
168
|
-
spendingCredential: Credential.makePubKey(
|
|
169
|
-
yield* PubKeyHash.make(firstPart())
|
|
170
|
-
),
|
|
171
|
-
stakingCredential: Credential.makeValidator(
|
|
172
|
-
yield* ValidatorHash.make(secondPart())
|
|
173
|
-
)
|
|
174
|
-
}
|
|
175
|
-
case 0x30:
|
|
176
|
-
return {
|
|
177
|
-
isMainnet,
|
|
178
|
-
spendingCredential: Credential.makeValidator(
|
|
179
|
-
yield* ValidatorHash.make(firstPart())
|
|
180
|
-
),
|
|
181
|
-
stakingCredential: Credential.makeValidator(
|
|
182
|
-
yield* ValidatorHash.make(secondPart())
|
|
183
|
-
)
|
|
184
|
-
}
|
|
185
|
-
case 0x60:
|
|
186
|
-
return {
|
|
187
|
-
isMainnet,
|
|
188
|
-
spendingCredential: Credential.makePubKey(
|
|
189
|
-
yield* PubKeyHash.make(firstPart())
|
|
190
|
-
)
|
|
191
|
-
}
|
|
192
|
-
case 0x70:
|
|
193
|
-
return {
|
|
194
|
-
isMainnet,
|
|
195
|
-
spendingCredential: Credential.makeValidator(
|
|
196
|
-
yield* ValidatorHash.make(firstPart())
|
|
197
|
-
)
|
|
198
|
-
}
|
|
199
|
-
default:
|
|
200
|
-
return yield* Effect.fail(
|
|
201
|
-
new Cbor.DecodeError(
|
|
202
|
-
Bytes.makeStream(bytes),
|
|
203
|
-
`invalid Shelley Address header ${head}`
|
|
204
|
-
)
|
|
205
|
-
)
|
|
206
|
-
}
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
export const decode = (bytes: Bytes.BytesLike): Cbor.DecodeEffect<Address> =>
|
|
210
|
-
decodeInternal(bytes).pipe(
|
|
211
|
-
Effect.map(({ isMainnet, spendingCredential, stakingCredential }) =>
|
|
212
|
-
make(isMainnet, spendingCredential, stakingCredential)
|
|
213
|
-
),
|
|
214
|
-
Effect.catchTag(
|
|
215
|
-
"ParseError",
|
|
216
|
-
(e) => new Cbor.DecodeError(Bytes.makeStream(bytes), e.message)
|
|
217
|
-
),
|
|
218
|
-
Effect.catchTag(
|
|
219
|
-
"DecodeException",
|
|
220
|
-
(e) =>
|
|
221
|
-
new Cbor.DecodeError(
|
|
222
|
-
Bytes.makeStream(bytes),
|
|
223
|
-
`bech32 decoding failed (${e.message})`
|
|
224
|
-
)
|
|
225
|
-
)
|
|
226
|
-
)
|
|
227
|
-
|
|
228
|
-
export function encode(address: Address): number[] {
|
|
229
|
-
return Cbor.encodeBytes(bytes(address))
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export function bytes(address: Address): number[] {
|
|
233
|
-
return Effect.runSync(Bech32.decode(address)).bytes
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
export function isMainnet(address: Address): boolean {
|
|
237
|
-
return !address.startsWith("addr_test")
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export function spendingCredential(address: Address): Credential.Credential {
|
|
241
|
-
return Effect.runSync(decodeInternal(address)).spendingCredential
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export function stakingCredential(
|
|
245
|
-
address: Address
|
|
246
|
-
): Credential.Credential | undefined {
|
|
247
|
-
return Effect.runSync(decodeInternal(address)).stakingCredential
|
|
248
|
-
}
|
package/src/Ledger/AssetClass.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { Encoding, Option, Schema } from "effect"
|
|
2
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
3
|
-
import { Data } from "../Uplc"
|
|
4
|
-
import * as MintingPolicy from "./MintingPolicy.js"
|
|
5
|
-
import * as ValidatorHash from "./ValidatorHash.js"
|
|
6
|
-
|
|
7
|
-
export function isValid(assetClass: string): assetClass is AssetClass {
|
|
8
|
-
const n = assetClass.length
|
|
9
|
-
return (
|
|
10
|
-
/^[0-9a-fA-F]+$/.test(assetClass) &&
|
|
11
|
-
n < 120 &&
|
|
12
|
-
(n == 0 || (n >= 56 && n % 2 == 0))
|
|
13
|
-
)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const AssetClass = Schema.transform(
|
|
17
|
-
Schema.String,
|
|
18
|
-
Schema.String.pipe(
|
|
19
|
-
Schema.filter((ac: string) => isValid(ac) || "Invalid Cardano AssetClass"),
|
|
20
|
-
Schema.brand("AssetClass")
|
|
21
|
-
),
|
|
22
|
-
{
|
|
23
|
-
strict: false,
|
|
24
|
-
decode: (s) => s.split(".").join(""),
|
|
25
|
-
encode: (s) => s
|
|
26
|
-
}
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
export type AssetClass = Schema.Schema.Type<typeof AssetClass>
|
|
30
|
-
|
|
31
|
-
export const ADA = "" as AssetClass
|
|
32
|
-
|
|
33
|
-
export function make(
|
|
34
|
-
policy: MintingPolicy.MintingPolicy,
|
|
35
|
-
tokenName: Bytes.BytesLike
|
|
36
|
-
) {
|
|
37
|
-
if (policy._tag == "None") {
|
|
38
|
-
return ADA
|
|
39
|
-
} else {
|
|
40
|
-
return policy.value + Encoding.encodeHex(Bytes.toUint8Array(tokenName))
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const FromUplcData = Schema.transform(
|
|
45
|
-
Data.EnumVariant(0, {
|
|
46
|
-
policy: MintingPolicy.FromUplcData,
|
|
47
|
-
tokenName: Data.ByteArray
|
|
48
|
-
}),
|
|
49
|
-
AssetClass,
|
|
50
|
-
{
|
|
51
|
-
strict: true,
|
|
52
|
-
decode: ({ policy, tokenName }) => {
|
|
53
|
-
if (policy._tag == "None") {
|
|
54
|
-
return ADA
|
|
55
|
-
} else {
|
|
56
|
-
return policy.value + Encoding.encodeHex(tokenName)
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
encode: (assetClass) => {
|
|
60
|
-
return {
|
|
61
|
-
policy: policy(assetClass),
|
|
62
|
-
tokenName: Bytes.toUint8Array(tokenName(assetClass))
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
export function pretty(assetClass: string): string {
|
|
69
|
-
if (assetClass.length == 0) {
|
|
70
|
-
return "."
|
|
71
|
-
} else {
|
|
72
|
-
return assetClass.slice(0, 56) + "." + assetClass.slice(56)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function policy(assetClass: string): MintingPolicy.MintingPolicy {
|
|
77
|
-
if (assetClass.length == 0) {
|
|
78
|
-
return Option.none()
|
|
79
|
-
} else {
|
|
80
|
-
return Option.some(assetClass.slice(0, 56) as ValidatorHash.ValidatorHash)
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function tokenName(assetClass: string): string {
|
|
85
|
-
if (assetClass.length == 0) {
|
|
86
|
-
return ""
|
|
87
|
-
} else {
|
|
88
|
-
return assetClass.slice(56)
|
|
89
|
-
}
|
|
90
|
-
}
|
package/src/Ledger/Assets.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect"
|
|
2
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
3
|
-
import * as Cbor from "../Cbor.js"
|
|
4
|
-
import { Data } from "../Uplc"
|
|
5
|
-
import * as AssetClass from "./AssetClass.js"
|
|
6
|
-
import * as MintingPolicy from "./MintingPolicy.js"
|
|
7
|
-
|
|
8
|
-
export const Assets = Schema.Record({
|
|
9
|
-
key: Schema.String, // can sadly not use AssetClass.AssetClass here
|
|
10
|
-
value: Schema.BigIntFromSelf
|
|
11
|
-
}).pipe(
|
|
12
|
-
Schema.filter((assets) => {
|
|
13
|
-
for (const key in assets) {
|
|
14
|
-
if (!AssetClass.isValid(key)) {
|
|
15
|
-
return `Invalid AssetClass ${key}`
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return true
|
|
20
|
-
})
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
export type Assets = Schema.Schema.Type<typeof Assets>
|
|
24
|
-
|
|
25
|
-
export const FromUplcData = Schema.transform(
|
|
26
|
-
Data.PairArray(
|
|
27
|
-
MintingPolicy.FromUplcData,
|
|
28
|
-
Data.PairArray(Data.Hex, Data.BigInt)
|
|
29
|
-
),
|
|
30
|
-
Assets,
|
|
31
|
-
{
|
|
32
|
-
strict: true,
|
|
33
|
-
decode: (outer) => {
|
|
34
|
-
const assets: Record<string, bigint> = {}
|
|
35
|
-
|
|
36
|
-
for (const [policy, inner] of outer) {
|
|
37
|
-
for (const [tokenName, quantity] of inner) {
|
|
38
|
-
assets[AssetClass.make(policy, tokenName)] = quantity
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return assets as Assets
|
|
43
|
-
},
|
|
44
|
-
encode: (assets) => {
|
|
45
|
-
const outer = nestedRecords(assets)
|
|
46
|
-
|
|
47
|
-
return Object.entries(outer).map(
|
|
48
|
-
([policy, inner]) =>
|
|
49
|
-
[
|
|
50
|
-
Effect.runSync(MintingPolicy.make(policy)),
|
|
51
|
-
Object.entries(inner)
|
|
52
|
-
] as const
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
function nestedRecords(assets: Assets): Record<string, Record<string, bigint>> {
|
|
59
|
-
const outer: Record<string, Record<string, bigint>> = {}
|
|
60
|
-
|
|
61
|
-
Object.entries(assets).forEach(([assetClass, quantity]) => {
|
|
62
|
-
if (assetClass.length == 0) {
|
|
63
|
-
outer[assetClass] = { "": quantity }
|
|
64
|
-
} else {
|
|
65
|
-
const policy = assetClass.slice(0, 56)
|
|
66
|
-
const tokenName = assetClass.slice(56)
|
|
67
|
-
|
|
68
|
-
if (policy in outer) {
|
|
69
|
-
outer[policy] = {
|
|
70
|
-
...outer[policy],
|
|
71
|
-
[tokenName]: quantity
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
outer[policy] = {
|
|
75
|
-
[tokenName]: quantity
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
return outer
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export const decode = (bytes: Bytes.BytesLike): Cbor.DecodeEffect<Assets> =>
|
|
85
|
-
Effect.gen(function* () {
|
|
86
|
-
const stream = Bytes.makeStream(bytes)
|
|
87
|
-
|
|
88
|
-
if (yield* Cbor.isTuple(bytes)) {
|
|
89
|
-
const [lovelace, otherAssets] = yield* Cbor.decodeTuple([
|
|
90
|
-
Cbor.decodeInt,
|
|
91
|
-
Cbor.decodeMap(
|
|
92
|
-
MintingPolicy.decode,
|
|
93
|
-
Cbor.decodeMap(Cbor.decodeBytes, Cbor.decodeInt)
|
|
94
|
-
)
|
|
95
|
-
])(stream)
|
|
96
|
-
|
|
97
|
-
const assets: Record<string, bigint> = {}
|
|
98
|
-
|
|
99
|
-
if (lovelace != 0n) {
|
|
100
|
-
assets[AssetClass.ADA] = lovelace
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
for (const [policy, inner] of otherAssets) {
|
|
104
|
-
if (policy._tag == "None") {
|
|
105
|
-
return yield* new Cbor.DecodeError(
|
|
106
|
-
stream,
|
|
107
|
-
"unexpected ADA assetclass in encoded non-ADA assets"
|
|
108
|
-
)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
for (const [tokenName, quantity] of inner) {
|
|
112
|
-
const assetClass = AssetClass.make(policy, tokenName)
|
|
113
|
-
|
|
114
|
-
assets[assetClass] = quantity
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return assets as Assets
|
|
119
|
-
} else {
|
|
120
|
-
return { [AssetClass.ADA]: yield* Cbor.decodeInt(stream) }
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
export function encode(assets: Assets): number[] {
|
|
125
|
-
const acs = nonAdaAssetClasses(assets)
|
|
126
|
-
|
|
127
|
-
if (acs.length == 0) {
|
|
128
|
-
return Cbor.encodeInt(lovelace(assets))
|
|
129
|
-
} else {
|
|
130
|
-
const obj = nestedRecords(assets)
|
|
131
|
-
if (AssetClass.ADA in obj) {
|
|
132
|
-
delete obj[AssetClass.ADA]
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return Cbor.encodeTuple([
|
|
136
|
-
Cbor.encodeInt(lovelace(assets)),
|
|
137
|
-
Cbor.encodeMap(
|
|
138
|
-
Object.entries(obj).map(([mph, tokens]) => {
|
|
139
|
-
return [
|
|
140
|
-
Cbor.encodeBytes(mph),
|
|
141
|
-
Cbor.encodeMap(
|
|
142
|
-
Object.entries(tokens).map(([tokenName, qty]) => [
|
|
143
|
-
Cbor.encodeBytes(tokenName),
|
|
144
|
-
Cbor.encodeInt(qty)
|
|
145
|
-
])
|
|
146
|
-
)
|
|
147
|
-
]
|
|
148
|
-
})
|
|
149
|
-
)
|
|
150
|
-
])
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export function allAssetClasses(assets: Assets): AssetClass.AssetClass[] {
|
|
155
|
-
return Object.keys(assets) as AssetClass.AssetClass[]
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export function nonAdaAssetClasses(assets: Assets): AssetClass.AssetClass[] {
|
|
159
|
-
return allAssetClasses(assets).filter((ac) => ac != AssetClass.ADA)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export function lovelace(assets: Assets): bigint {
|
|
163
|
-
return assets[AssetClass.ADA] ?? 0n
|
|
164
|
-
}
|
package/src/Ledger/Credential.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Schema } from "effect"
|
|
2
|
-
import { Data } from "../Uplc"
|
|
3
|
-
import { PubKeyHash } from "./PubKeyHash.js"
|
|
4
|
-
import { ValidatorHash } from "./ValidatorHash.js"
|
|
5
|
-
|
|
6
|
-
export const Credential = Schema.Union(
|
|
7
|
-
Schema.TaggedStruct("PubKey", { hash: PubKeyHash }),
|
|
8
|
-
Schema.TaggedStruct("Validator", { hash: ValidatorHash })
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
export type Credential = Schema.Schema.Type<typeof Credential>
|
|
12
|
-
|
|
13
|
-
export function makePubKey(pkh: PubKeyHash): Credential {
|
|
14
|
-
return { _tag: "PubKey", hash: pkh }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function makeValidator(vh: ValidatorHash): Credential {
|
|
18
|
-
return { _tag: "Validator", hash: vh }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const FromUplcData = Schema.transform(
|
|
22
|
-
Data.Enum({ PubKey: { hash: Data.Hex }, Validator: { hash: Data.Hex } }),
|
|
23
|
-
Credential,
|
|
24
|
-
{
|
|
25
|
-
strict: true,
|
|
26
|
-
decode: (cred) => cred,
|
|
27
|
-
encode: (cred) => cred
|
|
28
|
-
}
|
|
29
|
-
)
|
package/src/Ledger/DatumHash.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Effect, Encoding, Schema } from "effect"
|
|
2
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
3
|
-
import { decodeBytes, DecodeEffect, encodeBytes } from "../Cbor.js"
|
|
4
|
-
import { Data } from "../Uplc"
|
|
5
|
-
|
|
6
|
-
export function isValid(dh: string): boolean {
|
|
7
|
-
return /^[0-9a-fA-F]+$/.test(dh) && dh.length == 64
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const DatumHash = Schema.String.pipe(
|
|
11
|
-
Schema.filter((dh: string) => isValid(dh) || "Invalid Cardano DatumHash"),
|
|
12
|
-
Schema.brand("DatumHash")
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
export type DatumHash = Schema.Schema.Type<typeof DatumHash>
|
|
16
|
-
|
|
17
|
-
export function make(bytes: Bytes.BytesLike) {
|
|
18
|
-
return Schema.decode(DatumHash)(Bytes.toHex(bytes))
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const FromUplcData = Schema.transform(Data.ByteArray, DatumHash, {
|
|
22
|
-
strict: true,
|
|
23
|
-
decode: Encoding.encodeHex,
|
|
24
|
-
encode: Bytes.toUint8Array
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
export const decode = (bytes: Bytes.BytesLike): DecodeEffect<DatumHash> =>
|
|
28
|
-
decodeBytes(bytes).pipe(
|
|
29
|
-
Effect.map((bytes) => new Uint8Array(bytes)),
|
|
30
|
-
Effect.map(Encoding.encodeHex),
|
|
31
|
-
Effect.map(Schema.decodeSync(DatumHash))
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
export function encode(dh: DatumHash): number[] {
|
|
35
|
-
return encodeBytes(dh)
|
|
36
|
-
}
|
package/src/Ledger/IsMainnet.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Effect, Encoding, Option, Schema } from "effect"
|
|
2
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
3
|
-
import * as Cbor from "../Cbor.js"
|
|
4
|
-
import { Data } from "../Uplc"
|
|
5
|
-
import * as ValidatorHash from "./ValidatorHash.js"
|
|
6
|
-
|
|
7
|
-
// None is used for ADA
|
|
8
|
-
export const MintingPolicy = Schema.Option(ValidatorHash.ValidatorHash)
|
|
9
|
-
|
|
10
|
-
export type MintingPolicy = Schema.Schema.Type<typeof MintingPolicy>
|
|
11
|
-
|
|
12
|
-
export const FromUplcData = Schema.transform(Data.ByteArray, MintingPolicy, {
|
|
13
|
-
strict: true,
|
|
14
|
-
decode: (bs) => {
|
|
15
|
-
if (bs.length == 0) {
|
|
16
|
-
return Option.none()
|
|
17
|
-
} else {
|
|
18
|
-
return Option.some(Encoding.encodeHex(bs))
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
encode: (opt) => {
|
|
22
|
-
if (opt._tag == "None") {
|
|
23
|
-
return new Uint8Array()
|
|
24
|
-
} else {
|
|
25
|
-
return Effect.runSync(Encoding.decodeHex(opt.value))
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
export function make(policy: Bytes.BytesLike) {
|
|
31
|
-
const p = Bytes.toHex(policy)
|
|
32
|
-
|
|
33
|
-
if (p.length == 0) {
|
|
34
|
-
return Effect.succeed(Option.none())
|
|
35
|
-
} else {
|
|
36
|
-
return ValidatorHash.make(p).pipe(Effect.map(Option.some))
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const decode = (
|
|
41
|
-
bytes: Bytes.BytesLike
|
|
42
|
-
): Cbor.DecodeEffect<MintingPolicy> =>
|
|
43
|
-
Cbor.decodeBytes(bytes).pipe(
|
|
44
|
-
Effect.flatMap(make),
|
|
45
|
-
Effect.catchTag(
|
|
46
|
-
"ParseError",
|
|
47
|
-
(e) => new Cbor.DecodeError(Bytes.makeStream(bytes), e.message)
|
|
48
|
-
)
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
export function encode(policy: MintingPolicy): number[] {
|
|
52
|
-
if (policy._tag == "None") {
|
|
53
|
-
return Cbor.encodeBytes([])
|
|
54
|
-
} else {
|
|
55
|
-
return Cbor.encodeBytes(policy.value)
|
|
56
|
-
}
|
|
57
|
-
}
|
package/src/Ledger/PubKeyHash.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Effect, Encoding, Schema } from "effect"
|
|
2
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
3
|
-
import { decodeBytes, DecodeEffect, encodeBytes } from "../Cbor.js"
|
|
4
|
-
import { Data } from "../Uplc"
|
|
5
|
-
|
|
6
|
-
export function isValid(pkh: string): pkh is PubKeyHash {
|
|
7
|
-
return /^[0-9a-fA-F]+$/.test(pkh) && pkh.length == 56
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const PubKeyHash = Schema.String.pipe(
|
|
11
|
-
Schema.filter((pkh: string) => isValid(pkh) || "Invalid Cardano PubKeyHash"),
|
|
12
|
-
Schema.brand("PubKeyHash")
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
export type PubKeyHash = Schema.Schema.Type<typeof PubKeyHash>
|
|
16
|
-
|
|
17
|
-
export function make(bytes: Bytes.BytesLike) {
|
|
18
|
-
return Schema.decode(PubKeyHash)(Bytes.toHex(bytes))
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const FromUplcData = Schema.transform(Data.ByteArray, PubKeyHash, {
|
|
22
|
-
strict: true,
|
|
23
|
-
decode: Encoding.encodeHex,
|
|
24
|
-
encode: (s) => Effect.runSync(Encoding.decodeHex(s))
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
export const decode = (bytes: Bytes.BytesLike): DecodeEffect<PubKeyHash> =>
|
|
28
|
-
decodeBytes(bytes).pipe(
|
|
29
|
-
Effect.map((bytes) => new Uint8Array(bytes)),
|
|
30
|
-
Effect.map(Encoding.encodeHex),
|
|
31
|
-
Effect.map(Schema.decodeSync(PubKeyHash))
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
export function encode(pkh: PubKeyHash): number[] {
|
|
35
|
-
return encodeBytes(pkh)
|
|
36
|
-
}
|
package/src/Ledger/TxId.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect"
|
|
2
|
-
import * as Bytes from "../internal/Bytes.js"
|
|
3
|
-
import * as Cbor from "../Cbor.js"
|
|
4
|
-
|
|
5
|
-
export function isValid(txId: string): txId is TxId {
|
|
6
|
-
return txId.length == 64 && /^[0-9a-fA-F]+$/.test(txId)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const TxId = Schema.String.pipe(
|
|
10
|
-
Schema.filter((id: string) => isValid(id) || "Invalid Cardano TxId"),
|
|
11
|
-
Schema.brand("TxId")
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
export type TxId = Schema.Schema.Type<typeof TxId>
|
|
15
|
-
|
|
16
|
-
export function make(txId: Bytes.BytesLike) {
|
|
17
|
-
return Schema.decode(TxId)(Bytes.toHex(txId))
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const decode = (bytes: Bytes.BytesLike): Cbor.DecodeEffect<TxId> =>
|
|
21
|
-
Cbor.decodeBytes(bytes).pipe(
|
|
22
|
-
Effect.flatMap(make),
|
|
23
|
-
Effect.catchTag(
|
|
24
|
-
"ParseError",
|
|
25
|
-
(e) => new Cbor.DecodeError(Bytes.makeStream(bytes), e.message)
|
|
26
|
-
)
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
export function encode(txId: TxId): number[] {
|
|
30
|
-
return Cbor.encodeBytes(txId)
|
|
31
|
-
}
|