plutus-cbor-1.0.1: On-chain CBOR encoders/decoders
Safe HaskellSafe-Inferred
LanguageGHC2021

Plutus.Codec.CBOR.Encoding

Description

An on-chain implementation of RFC 8949: Concise Binary Object Representation (CBOR).

This is generally something one would want to avoid as the memory and CPU budgets on-chain are heavily limited. However, there are scenarios when one needs to serialise some higher level data-type into binary. This library provides an efficient way of doing it.

How to use

Let's start with some relatively simple type:

data Foo = Foo
 { bar :: Bool
 , baz :: Integer
 }

From there, one can define an Encoding for the Foo data-type. This is a simple record of unary constructors, so using a 2-tuple should do. Note that Encoding is a Semigroup, and thus, they are easily composed using the concatenation operator (<>).

encodeFoo :: Foo -> Encoding
encodeFoo Foo{bar,baz} =
  encodeListLen 2
    <> encodeBool bar
    <> encodeInteger baz

Then, any Encoding can be turned into a BuiltinByteString using: encodingToBuiltinByteString. For instance, one could define:

serialiseFoo :: Foo -> BuiltinByteString
serialiseFoo = encodingToBuiltinByteString . encodeFoo
Synopsis

Encoding

data Encoding Source #

An opaque Encoding type. See also encodingToBuiltinByteString.

Instances

Instances details
Monoid Encoding Source # 
Instance details

Defined in Plutus.Codec.CBOR.Encoding

Methods

mempty :: Encoding

Semigroup Encoding Source # 
Instance details

Defined in Plutus.Codec.CBOR.Encoding

Methods

(<>) :: Encoding -> Encoding -> Encoding

encodingToBuiltinByteString :: Encoding -> BuiltinByteString Source #

Runs an encoder and produce a BuiltinByteString.

Basic Types

encodeBool :: Bool -> Encoding Source #

Encode a Bool as a CBOR type-07 major type.

encodeInteger :: Integer -> Encoding Source #

Encode an Integer as a CBOR type-00 or type-01 (negative) number.

Note (1): The Encoding is of variable-length, larger numbers are larger to encode.

Note (2): This can only encode numbers up to 2^64 - 1 and down to -2^63

encodeByteString :: BuiltinByteString -> Encoding Source #

Encode a BuiltinByteString as a CBOR type-02 major type.

encodeString :: BuiltinString -> Encoding Source #

Encode a BuiltinString as a CBOR type-03 major type.

encodeNull :: Encoding Source #

Encode a null character, useful to encode optional values.

Data-Structures

Finite Structures

encodeMaybe :: (a -> Encoding) -> Maybe a -> Encoding Source #

Helper for optionally encoding a type. Note that in the Nothing case, this is a no-op.

encodeListLen :: Integer -> Encoding Source #

Declare a list of fixed size. Each element of the list must then be separately provided via appending them (Encoding is a Semigroup).

This is useful to construct non-uniform arrays where elements may have different types. For uniform list, see encodeList.

-- Encoding (14, 42) as a finite list...
encodeListLen 2
  <> encodeInteger 14
  <> encodeInteger 42

encodeList :: (a -> Encoding) -> [a] -> Encoding Source #

Shorthand for encoding a uniform list. Note that CBOR supports non-uniform lists (i.e. n-tuples) for which one should use encodeListLen or encodeBeginList / encodeBreak.

encodeMapLen :: Integer -> Encoding Source #

Declare a map of fixed size. Each key/value pair of the map must then be separately provided via appending them (Encoding is a Semigroup).

This is useful to construct non-uniform maps where keys and values may have different types. For uniform maps, see encodeMap.

-- Encoding { 14: b'1abc', 42: b'0000' } as a finite map...
encodeMapLen 2
  <> encodeInteger 14 <> encodeByteString "1abc"
  <> encodeInteger 42 <> encodeByteString "0000"

encodeMap :: (k -> Encoding) -> (v -> Encoding) -> Map k v -> Encoding Source #

Shorthand for encoding a uniform map of fixed size.

see also: encodeMapLen / encodeBreak for non-uniform maps.

Indefinite Structures

encodeBreak :: Encoding Source #

Encode a indefinite list or map termination. This must come (not necessarily immediately) after an encodeListIndef or encodeMapIndef

encodeBeginList :: Encoding Source #

Declare a list of indefinite size. Each element of the list must then be separately provided via appending them (Encoding is a Semigroup).

This is useful to construct non-uniform arrays where elements may have different types. For uniform list, see encodeListIndef.

-- Encoding (14, 42) as an indefinite list...
encodeBeginList
  <> encodeInteger 14
  <> encodeInteger 42
  <> encodeBreak

encodeListIndef :: (a -> Encoding) -> [a] -> Encoding Source #

Shorthand for encoding uniform list of indefinite sizes. Note that CBOR supports non-uniform indefinite list (i.e. n-tuples) for which one should use encodeListLen or encodeBeginList / encodeBreak.

encodeBeginMap :: Encoding Source #

Declare a map of indefinite size. Each key/value pair of the map must then be separately provided via appending them (Encoding is a Semigroup).

This is useful to construct non-uniform maps where keys and values may have different types. For uniform maps, see encodeMap.

-- Encoding { 14: b'1abc', 42: b'0000' } as a finite map...
encodeBeginMap
  <> encodeInteger 14 <> encodeByteString "1abc"
  <> encodeInteger 42 <> encodeByteString "0000"
  <> encodeBreak

encodeMapIndef :: (k -> Encoding) -> (v -> Encoding) -> Map k v -> Encoding Source #

Shorthand for encoding a uniform map of indefinite size.

see also: encodeBeginMap / encodeBreak for non-uniform maps.

Tags

encodeTag :: Integer -> Encoding Source #

Encode a CBOR-tag as a major type-06.

Well known tags:

TagDescriptionType
0Standard date/time stringtext string
1Epoch-based date/timenumber
2Positive bignumbyte string
3Negative bignumbyte string
4Decimal fractionarray
24Encoded CBOR data itembyte string

For more tags, have a look at iana's Concise Binary Object Representation (CBOR) Tags list.

Backdoor / Unsafe

unsafeEncodeRaw :: BuiltinByteString -> Encoding Source #

Inject an already CBOR-encoded bytestring into an Encoding. Do not use unless you know what you're doing, this may creates an Encoding not compliant with the CBOR specification.