Cardano.Cip67

CIP-0067 support.

CIP-0067 describes a standard format for assets so that their purpose can be deduced solely by their token names. So far, the primary application of this standard is to easily distinguish between CIP-0068 assets and their reference counterparts.

type alias Cip67 =
{ label : Int
, assetName : Bytes AssetName
}

Datatype for modeling CIP-0067.

This standard offers a label number preceding the actual token name, which can be from 0 to 65535 (i.e. 2 bytes, and left-padded with 00 for numbers smaller than 2 bytes). This label should be formatted as such:

--  openning bracket                             closing bracket
--       ┌──┐                                         ┌──┐
--     [ 0000 | 16 bits label_num | 8 bits checksum | 0000 ]
--              └───────────────┘   └───────┬─────┘
--        fixed 2 bytes for the label       │
--                                          │
-- label's checksum found by applying the CRC-8 algorithm to its 2 bytes

The polynomial representation of the CRC-8 is 0x07 (0b00000111).

As an example, a label of 222 turn into: 0x000de140

  1. Hex equivalent of decimal 222 is 0xde
  2. Since it's less than 2 bytes, we add 0x00 to the left and get 0x00de
  3. Find its CRC-8 checksum (0x14)
  4. Concatenate the two and wrap in "brackets" to get the final bytes: 0x000de140

Finally, a complete CIP-0067 example:

spacebudz2921 : Maybe Cip67
spacebudz2921 =
    Maybe.andThen Cip67.fromBytes <| Bytes.fromHex "000de14042756432393231"

-- Just { assetName = Bytes "42756432393231", label = 222 }

Phantom type for CIP-0067 asset names with the label prefix removed.

fromBytes : Bytes AssetName -> Maybe Cip67

Validate and separate the label of a CIP-0067 asset name.

Given a valid CIP-0067 token name [Bytes], this function separates the label as an [Int], and returns the asset name without the label bytes.

labelFromHex : String -> Maybe Int

Extract the label if it is valid. Return Nothing otherwise.

fromCbor : Decoder Cip67

CBOR decoder for [Cip67].

Converts a [Cip67] to [Bytes].

labelToHex : Int -> String

Convert an Int label into its CIP-0067 hex string.

toCbor : Cip67 -> Encoder

CBOR encoder.