|
| 1 | +import { Schema } from "effect" |
| 2 | + |
| 3 | +import * as AssetName from "../AssetName.js" |
| 4 | +import * as Bytes from "../Bytes.js" |
| 5 | +import * as PolicyId from "../PolicyId.js" |
| 6 | +import * as Label from "./Label.js" |
| 7 | + |
| 8 | +/** |
| 9 | + * Unit represents the concatenation of PolicyId and AssetName as a single hex string. |
| 10 | + * Format: policyId (56 chars) + assetName (0-64 chars) |
| 11 | + * Special case: "lovelace" represents ADA |
| 12 | + * |
| 13 | + * @since 2.0.0 |
| 14 | + * @category model |
| 15 | + */ |
| 16 | +export type Unit = string |
| 17 | + |
| 18 | +/** |
| 19 | + * Result of parsing a Unit string. |
| 20 | + * |
| 21 | + * @since 2.0.0 |
| 22 | + * @category model |
| 23 | + */ |
| 24 | +export interface UnitDetails { |
| 25 | + policyId: PolicyId.PolicyId |
| 26 | + assetName: AssetName.AssetName | null |
| 27 | + name: AssetName.AssetName | null |
| 28 | + label: number | null |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Parse a Unit string into its components. |
| 33 | + * Extracts policy ID, asset name, and CIP-67 label if present. |
| 34 | + * |
| 35 | + * @since 2.0.0 |
| 36 | + * @category conversions |
| 37 | + * @example |
| 38 | + * ```typescript |
| 39 | + * import * as Unit from "@evolution-sdk/core/Assets/Unit" |
| 40 | + * |
| 41 | + * // NFT with CIP-67 label 222 |
| 42 | + * const details = Unit.fromUnit("a0b1c2...policyId...000de140...name...") |
| 43 | + * // => { |
| 44 | + * // policyId: PolicyId, |
| 45 | + * // assetName: AssetName, |
| 46 | + * // name: AssetName (without label), |
| 47 | + * // label: 222 |
| 48 | + * // } |
| 49 | + * |
| 50 | + * // Regular token without label |
| 51 | + * const token = Unit.fromUnit("a0b1c2...policyId...tokenName") |
| 52 | + * // => { policyId, assetName, name, label: null } |
| 53 | + * ``` |
| 54 | + */ |
| 55 | +export const fromUnit = (unit: Unit): UnitDetails => { |
| 56 | + const policyIdHex = unit.slice(0, 56) |
| 57 | + const assetNameHex = unit.slice(56) || null |
| 58 | + |
| 59 | + const policyId = Schema.decodeSync(PolicyId.FromHex)(policyIdHex) |
| 60 | + |
| 61 | + if (!assetNameHex) { |
| 62 | + return { |
| 63 | + policyId, |
| 64 | + assetName: null, |
| 65 | + name: null, |
| 66 | + label: null |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const assetName = Schema.decodeSync(AssetName.FromHex)(assetNameHex) |
| 71 | + |
| 72 | + // Check for CIP-67 label (first 8 chars of asset name) |
| 73 | + if (assetNameHex.length >= 8) { |
| 74 | + const potentialLabel = assetNameHex.slice(0, 8) |
| 75 | + const labelNum = Label.fromLabel(potentialLabel) |
| 76 | + |
| 77 | + if (labelNum !== undefined) { |
| 78 | + // Has valid label, extract name without label |
| 79 | + const nameHex = assetNameHex.slice(8) |
| 80 | + const name = nameHex ? Schema.decodeSync(AssetName.FromHex)(nameHex) : null |
| 81 | + return { |
| 82 | + policyId, |
| 83 | + assetName, |
| 84 | + name, |
| 85 | + label: labelNum |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // No label found |
| 91 | + return { |
| 92 | + policyId, |
| 93 | + assetName, |
| 94 | + name: assetName, |
| 95 | + label: null |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Construct a Unit string from components. |
| 101 | + * Combines policy ID, optional CIP-67 label, and asset name. |
| 102 | + * |
| 103 | + * @since 2.0.0 |
| 104 | + * @category conversions |
| 105 | + * @throws {Error} If asset name exceeds 32 bytes |
| 106 | + * @throws {Error} If policy ID is invalid length |
| 107 | + * @example |
| 108 | + * ```typescript |
| 109 | + * import * as Unit from "@evolution-sdk/core/Assets/Unit" |
| 110 | + * |
| 111 | + * // With CIP-67 label |
| 112 | + * const unit = Unit.toUnit(policyId, "name", 222) |
| 113 | + * // => "policyId" + "000de140" + "name" |
| 114 | + * |
| 115 | + * // Without label |
| 116 | + * const simple = Unit.toUnit(policyId, "tokenName") |
| 117 | + * // => "policyId" + "tokenName" |
| 118 | + * |
| 119 | + * // Policy only (no asset name) |
| 120 | + * const policyOnly = Unit.toUnit(policyId) |
| 121 | + * // => "policyId" |
| 122 | + * ``` |
| 123 | + */ |
| 124 | +export const toUnit = ( |
| 125 | + policyId: PolicyId.PolicyId, |
| 126 | + name?: AssetName.AssetName | string | null, |
| 127 | + label?: number | null |
| 128 | +): Unit => { |
| 129 | + const policyIdHex = Schema.encodeSync(PolicyId.FromHex)(policyId) |
| 130 | + |
| 131 | + if (policyIdHex.length !== 56) { |
| 132 | + throw new Error(`Policy id invalid: ${policyIdHex}`) |
| 133 | + } |
| 134 | + |
| 135 | + if (!name && !label) { |
| 136 | + return policyIdHex |
| 137 | + } |
| 138 | + |
| 139 | + const nameHex = name ? (typeof name === "string" ? name : Bytes.toHex(name.bytes)) : "" |
| 140 | + const labelHex = label !== null && label !== undefined ? Label.toLabel(label) : "" |
| 141 | + |
| 142 | + const totalHex = labelHex + nameHex |
| 143 | + if (totalHex.length > 64) { |
| 144 | + throw new Error("Asset name size exceeds 32 bytes.") |
| 145 | + } |
| 146 | + |
| 147 | + return policyIdHex + totalHex |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Check if a value is the special "lovelace" unit. |
| 152 | + * |
| 153 | + * @since 2.0.0 |
| 154 | + * @category predicates |
| 155 | + */ |
| 156 | +export const isLovelace = (unit: Unit): boolean => unit === "lovelace" |
| 157 | + |
| 158 | +/** |
| 159 | + * Schema for validating Unit strings. |
| 160 | + * |
| 161 | + * @since 2.0.0 |
| 162 | + * @category schemas |
| 163 | + */ |
| 164 | +export const UnitSchema = Schema.String.pipe( |
| 165 | + Schema.filter( |
| 166 | + (s) => s === "lovelace" || (s.length >= 56 && s.length <= 120 && /^[0-9a-fA-F]+$/.test(s)), |
| 167 | + { |
| 168 | + message: () => 'Unit must be "lovelace" or hex string (56-120 chars)' |
| 169 | + } |
| 170 | + ) |
| 171 | +).annotations({ |
| 172 | + identifier: "Assets.Unit", |
| 173 | + description: "Unit identifier for native assets (policyId + assetName)" |
| 174 | +}) |
0 commit comments