|
| 1 | +import { Data, FastCheck, Inspectable, pipe, Schema } from "effect"; |
| 2 | +import * as Hex from "./Hex.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * CDDL specs |
| 6 | + * asset_name = bytes .size (0 .. 32) |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * The maximum length in bytes of an asset name. |
| 11 | + * |
| 12 | + * @since 2.0.0 |
| 13 | + * @category constants |
| 14 | + */ |
| 15 | +export const ASSET_NAME_MAX_LENGTH = 32; |
| 16 | + |
| 17 | +/** |
| 18 | + * Error class for AssetName related operations. |
| 19 | + * |
| 20 | + * @example |
| 21 | + * import { AssetName } from "@lucid-evolution/experimental"; |
| 22 | + * import assert from "assert"; |
| 23 | + * |
| 24 | + * const error = new AssetName.AssetNameError({ message: "Invalid asset name" }); |
| 25 | + * assert(error.message === "Invalid asset name"); |
| 26 | + * |
| 27 | + * @since 2.0.0 |
| 28 | + * @category errors |
| 29 | + */ |
| 30 | +export class AssetNameError extends Data.TaggedError("AssetNameError")<{ |
| 31 | + message?: string; |
| 32 | + reason?: "InvalidBytesLength" | "InvalidHexFormat" | "InvalidCBORFormat"; |
| 33 | +}> {} |
| 34 | + |
| 35 | +/** |
| 36 | + * Schema for validating AssetName bytes with CDDL constraint (0..32 bytes) |
| 37 | + * |
| 38 | + * @since 2.0.0 |
| 39 | + * @category schemas |
| 40 | + */ |
| 41 | +export const AssetNameBytes = pipe( |
| 42 | + Schema.Uint8Array, |
| 43 | + Schema.filter((a) => a.length >= 0 && a.length <= ASSET_NAME_MAX_LENGTH), |
| 44 | + Schema.typeSchema, |
| 45 | +).annotations({ |
| 46 | + message: (issue) => |
| 47 | + `Asset name must be a byte array of length 0-${ASSET_NAME_MAX_LENGTH}, got length ${ |
| 48 | + (issue.actual as Uint8Array)?.length |
| 49 | + }`, |
| 50 | + identifier: "AssetNameBytes", |
| 51 | +}); |
| 52 | + |
| 53 | +export declare const NominalType: unique symbol; |
| 54 | +export interface AssetName { |
| 55 | + readonly [NominalType]: unique symbol; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Schema for AssetName representing an asset name. |
| 60 | + * Follows CDDL specification: bytes .size (0 .. 32) |
| 61 | + * |
| 62 | + * @since 2.0.0 |
| 63 | + * @category schemas |
| 64 | + */ |
| 65 | +export class AssetName extends Schema.TaggedClass<AssetName>("AssetName")( |
| 66 | + "AssetName", |
| 67 | + { |
| 68 | + bytes: AssetNameBytes, |
| 69 | + }, |
| 70 | +) { |
| 71 | + [Inspectable.NodeInspectSymbol]() { |
| 72 | + return { |
| 73 | + _tag: "AssetName", |
| 74 | + bytes: this.bytes, |
| 75 | + }; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Check if the given value is a valid AssetName |
| 81 | + * |
| 82 | + * @since 2.0.0 |
| 83 | + * @category predicates |
| 84 | + */ |
| 85 | +export const isAssetName = Schema.is(AssetName); |
| 86 | + |
| 87 | +/** |
| 88 | + * Schema for transforming between bytes and AssetName |
| 89 | + * |
| 90 | + * @since 2.0.0 |
| 91 | + * @category encoding/decoding |
| 92 | + */ |
| 93 | +export const Bytes = Schema.transform(AssetNameBytes, AssetName, { |
| 94 | + strict: true, |
| 95 | + encode: (_, toA) => toA.bytes, |
| 96 | + decode: (fromA) => new AssetName({ bytes: fromA }), |
| 97 | +}); |
| 98 | + |
| 99 | +/** |
| 100 | + * Schema for transforming between hex string and AssetName |
| 101 | + * |
| 102 | + * @since 2.0.0 |
| 103 | + * @category encoding/decoding |
| 104 | + */ |
| 105 | +export const HexString = Schema.transform(Hex.HexString, AssetName, { |
| 106 | + strict: true, |
| 107 | + encode: (_toI, toA) => Hex.fromBytes(toA.bytes), |
| 108 | + decode: (fromI, _fromA) => new AssetName({ bytes: Hex.toBytes(fromI) }), |
| 109 | +}); |
| 110 | + |
| 111 | +/** |
| 112 | + * Check if two AssetName instances are equal. |
| 113 | + * |
| 114 | + * @example |
| 115 | + * import { AssetName } from "@lucid-evolution/experimental"; |
| 116 | + * import assert from "assert"; |
| 117 | + * |
| 118 | + * const name1 = new AssetName({ bytes: new Uint8Array([0x74, 0x6f, 0x6b, 0x65, 0x6e]) }); |
| 119 | + * const name2 = new AssetName({ bytes: new Uint8Array([0x74, 0x6f, 0x6b, 0x65, 0x6e]) }); |
| 120 | + * const name3 = new AssetName({ bytes: new Uint8Array([0x6f, 0x74, 0x68, 0x65, 0x72]) }); |
| 121 | + * |
| 122 | + * assert(AssetName.equals(name1, name2) === true); |
| 123 | + * assert(AssetName.equals(name1, name3) === false); |
| 124 | + * |
| 125 | + * @since 2.0.0 |
| 126 | + * @category equality |
| 127 | + */ |
| 128 | +export const equals = (a: AssetName, b: AssetName): boolean => |
| 129 | + a.bytes.length === b.bytes.length && |
| 130 | + a.bytes.every((byte, index) => byte === b.bytes[index]); |
| 131 | + |
| 132 | +/** |
| 133 | + * Generator for creating random AssetName instances for testing |
| 134 | + * |
| 135 | + * @example |
| 136 | + * import { AssetName } from "@lucid-evolution/experimental"; |
| 137 | + * import { FastCheck } from "effect"; |
| 138 | + * import assert from "assert"; |
| 139 | + * |
| 140 | + * const randomSamples = FastCheck.sample(AssetName.generator, 10); |
| 141 | + * randomSamples.forEach((assetName) => { |
| 142 | + * assert(assetName instanceof AssetName); |
| 143 | + * assert(assetName.bytes.length <= 32); |
| 144 | + * }); |
| 145 | + * |
| 146 | + * @since 2.0.0 |
| 147 | + * @category generators |
| 148 | + */ |
| 149 | +export const generator = FastCheck.uint8Array({ |
| 150 | + minLength: 0, |
| 151 | + maxLength: ASSET_NAME_MAX_LENGTH, |
| 152 | +}).map((bytes) => new AssetName({ bytes })); |
0 commit comments