cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cometa.Metadata

Description

Validated transaction metadata. Nested maps preserve pair order and duplicate keys. Transaction labels are unique unsigned 64-bit numbers. Imported encodings remain exact until the caller explicitly encodes their semantic view again.

{-# LANGUAGE OverloadedStrings #-}
import Cometa.Error (Error)
import qualified Cometa.Metadata as Metadata

message :: Either Error Metadata.Metadata
message = Metadata.singleton 674 . Metadata.list . pure $ Metadata.mkText "hello"
Synopsis

Nested values

data Metadatum Source #

A metadata value whose integers fit primitive CBOR and whose byte/text strings contain at most 64 bytes. Equality compares values and nested ordering.

Instances

Instances details
Show Metadatum Source # 
Instance details

Defined in Cometa.Metadata

Eq Metadatum Source # 
Instance details

Defined in Cometa.Metadata

data View Source #

A read-only view. Constructing a view cannot bypass Metadatum validation.

Constructors

IntegerValue Integer

A primitive CBOR integer in the inclusive range -2^64 .. 2^64-1.

BytesValue ByteString

Zero through 64 arbitrary bytes.

TextValue Text

Text containing at most 64 UTF-8 bytes, without normalization.

ListValue [Metadatum]

An ordered list of validated values.

MapValue [(Metadatum, Metadatum)]

Ordered key/value pairs; duplicate keys are preserved.

Instances

Instances details
Show View Source # 
Instance details

Defined in Cometa.Metadata

Methods

showsPrec :: Int -> View -> ShowS #

show :: View -> String #

showList :: [View] -> ShowS #

Eq View Source # 
Instance details

Defined in Cometa.Metadata

Methods

(==) :: View -> View -> Bool #

(/=) :: View -> View -> Bool #

mkInteger :: Integer -> Either Error Metadatum Source #

Validate the inclusive range -2^64 .. 2^64-1. Tagged bignums are excluded.

integerFromText :: Radix -> Text -> Either Error Metadatum Source #

Parse native-compatible integer text in a validated radix, then apply the ledger metadata integer bounds. Parsing errors are InvalidIntegerText; out-of-range values return InvalidMetadataInteger.

mkBytes :: ByteString -> Either Error Metadatum Source #

Validate a byte string of 0 through 64 bytes.

mkText :: Text -> Either Error Metadatum Source #

Validate at most 64 UTF-8 bytes; no Unicode normalization is performed.

list :: [Metadatum] -> Metadatum Source #

Assemble a list, preserving order. Empty lists are valid.

map :: [(Metadatum, Metadatum)] -> Metadatum Source #

Assemble a map, preserving pair order and duplicate keys. Empty maps are valid.

view :: Metadatum -> View Source #

Inspect a value without validation or effects.

Transaction labels

data Metadata Source #

Unique labels. Equality ignores insertion order. Combining values is left-biased on duplicate labels, with empty metadata as identity.

Instances

Instances details
Monoid Metadata Source # 
Instance details

Defined in Cometa.Metadata

Semigroup Metadata Source # 
Instance details

Defined in Cometa.Metadata

Show Metadata Source # 
Instance details

Defined in Cometa.Metadata

Eq Metadata Source # 
Instance details

Defined in Cometa.Metadata

singleton :: Word64 -> Metadatum -> Metadata Source #

Assemble one transaction label and its validated value.

mkMetadata :: [(Word64, Metadatum)] -> Either Error Metadata Source #

Validate label uniqueness; duplicate labels return DuplicateMetadataLabel.

toList :: Metadata -> [(Word64, Metadatum)] Source #

Inspect entries in ascending numeric label order.

lookup :: Word64 -> Metadata -> Maybe Metadatum Source #

Find a label; missing labels are ordinary absence.

CBOR and encoding identity

data EncodedMetadata Source #

Validated metadata plus its original bytes. Equality compares exact bytes.

Instances

Instances details
Show EncodedMetadata Source # 
Instance details

Defined in Cometa.Metadata

Eq EncodedMetadata Source # 
Instance details

Defined in Cometa.Metadata

data EncodedMetadatum Source #

Validated metadatum plus its original bytes. Equality compares exact bytes.

encodeCbor :: Metadata -> ByteString Source #

Encode a semantic value with minimal integers and definite strings and collections. Labels are ascending; nested map order is preserved. This is normalization, not a claim of canonical sorting of arbitrary metadata keys.

decodeCbor :: ByteString -> Either Error EncodedMetadata Source #

Decode exactly one metadata map. Rejects duplicate labels, trailing input, invalid UTF-8, unsupported CBOR types, and out-of-bound values. Definite and indefinite collections and strings are accepted; original bytes are retained.

toCbor :: EncodedMetadata -> ByteString Source #

Retrieve the exact imported bytes without normalization.

toMetadata :: EncodedMetadata -> Metadata Source #

Inspect the semantic metadata view; encoding it again explicitly normalizes.

encodeMetadatumCbor :: Metadatum -> ByteString Source #

Encode a metadatum with minimal integers and definite strings/collections. Map pair order and duplicate keys are preserved.

decodeMetadatumCbor :: ByteString -> Either Error EncodedMetadatum Source #

Decode one bounded metadatum, preserving exact bytes. The same restrictions as decodeCbor apply.

toMetadatumCbor :: EncodedMetadatum -> ByteString Source #

Retrieve exact imported metadatum bytes.

toMetadatum :: EncodedMetadatum -> Metadatum Source #

Retrieve the validated semantic metadatum.

Detailed JSON

toDetailedJson :: Metadata -> Value Source #

Lossless cardano-api detailed-schema JSON: top-level object keys are decimal labels. This is distinct from CIP-116 inspector JSON. JSON object key order is insignificant; nested map pair arrays preserve order and duplicates.

fromDetailedJson :: Value -> Either Error Metadata Source #

Import detailed-schema JSON, validating labels and every leaf. Labels must be canonical unsigned decimal strings, without leading zeros except zero.

metadatumToDetailedJson :: Metadatum -> Value Source #

Convert every metadatum constructor without loss. Shapes are {"int":n}, {"bytes":"hex"}, {"string":"text"}, {"list":[...]} and {"map":[{"k":...,"v":...}]}. Hex is lowercase without a prefix.

metadatumFromDetailedJson :: Value -> Either Error Metadatum Source #

Validate a detailed-schema metadatum. Integers remain exact; fractional numbers, unsupported shapes, invalid hex and overlong strings fail.

Plain JSON

metadatumToJson :: Metadatum -> Either Error Value Source #

Convert a metadatum to ordinary JSON, matching cardano-c's plain JSON schema: integers, text, arrays and objects with text keys. Bytes, nontext map keys and duplicate object keys return InvalidMetadataJson instead of losing information. Object member ordering is not preserved by JSON. Integer output retains the full metadata range, including negatives below -2^63 which the native plain JSON importer does not accept. Use detailed JSON for all metadata alternatives and exact ordered map pairs.

metadatumFromJson :: Value -> Either Error Metadatum Source #

Import ordinary JSON as metadata, matching native plain JSON conversion. Strings stay text, including 0x prefixes; object keys stay text, including decimal names. Objects assemble pairs in lexicographic key order. Integers must be in -2^63 through 2^64-1; fractions, booleans and null return InvalidMetadataJson. Text and object keys retain the 64 UTF-8 byte bound. Invalid integer/text lengths return their existing metadata validation errors. This is not cardano-api's no-schema conversion, which infers bytes and numeric keys. Use metadatumFromDetailedJson when those distinctions are needed.