| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
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
- data Metadatum
- data View
- mkInteger :: Integer -> Either Error Metadatum
- integerFromText :: Radix -> Text -> Either Error Metadatum
- mkBytes :: ByteString -> Either Error Metadatum
- mkText :: Text -> Either Error Metadatum
- list :: [Metadatum] -> Metadatum
- map :: [(Metadatum, Metadatum)] -> Metadatum
- view :: Metadatum -> View
- data Metadata
- singleton :: Word64 -> Metadatum -> Metadata
- mkMetadata :: [(Word64, Metadatum)] -> Either Error Metadata
- toList :: Metadata -> [(Word64, Metadatum)]
- lookup :: Word64 -> Metadata -> Maybe Metadatum
- data EncodedMetadata
- data EncodedMetadatum
- encodeCbor :: Metadata -> ByteString
- decodeCbor :: ByteString -> Either Error EncodedMetadata
- toCbor :: EncodedMetadata -> ByteString
- toMetadata :: EncodedMetadata -> Metadata
- encodeMetadatumCbor :: Metadatum -> ByteString
- decodeMetadatumCbor :: ByteString -> Either Error EncodedMetadatum
- toMetadatumCbor :: EncodedMetadatum -> ByteString
- toMetadatum :: EncodedMetadatum -> Metadatum
- toDetailedJson :: Metadata -> Value
- fromDetailedJson :: Value -> Either Error Metadata
- metadatumToDetailedJson :: Metadatum -> Value
- metadatumFromDetailedJson :: Value -> Either Error Metadatum
- metadatumToJson :: Metadatum -> Either Error Value
- metadatumFromJson :: Value -> Either Error Metadatum
Nested values
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
A read-only view. Constructing a view cannot bypass Metadatum validation.
Constructors
| IntegerValue Integer | A primitive CBOR integer in the inclusive range |
| 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. |
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.
map :: [(Metadatum, Metadatum)] -> Metadatum Source #
Assemble a map, preserving pair order and duplicate keys. Empty maps are valid.
Transaction labels
Unique labels. Equality ignores insertion order. Combining values is left-biased on duplicate labels, with empty metadata as identity.
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
| Show EncodedMetadata Source # | |
Defined in Cometa.Metadata Methods showsPrec :: Int -> EncodedMetadata -> ShowS # show :: EncodedMetadata -> String # showList :: [EncodedMetadata] -> ShowS # | |
| Eq EncodedMetadata Source # | |
Defined in Cometa.Metadata Methods (==) :: EncodedMetadata -> EncodedMetadata -> Bool # (/=) :: EncodedMetadata -> EncodedMetadata -> Bool # | |
data EncodedMetadatum Source #
Validated metadatum plus its original bytes. Equality compares exact bytes.
Instances
| Show EncodedMetadatum Source # | |
Defined in Cometa.Metadata Methods showsPrec :: Int -> EncodedMetadatum -> ShowS # show :: EncodedMetadatum -> String # showList :: [EncodedMetadatum] -> ShowS # | |
| Eq EncodedMetadatum Source # | |
Defined in Cometa.Metadata Methods (==) :: EncodedMetadatum -> EncodedMetadatum -> Bool # (/=) :: EncodedMetadatum -> EncodedMetadatum -> Bool # | |
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.