aiken/cbor

Functions

diagnostic(self: Data) -> String

Obtain a String representation of anything. This is particularly (and only) useful for tracing and debugging. This function is expensive and should not be used in any production code as it will very likely explodes the validator’s budget.

The output is a CBOR diagnostic of the underlying on-chain binary representation of the data. It’s not as easy to read as plain Aiken code, but it is handy for troubleshooting values at runtime. Incidentally, getting familiar with reading CBOR diagnostic is a good idea in the Cardano world.

diagnostic(42) == "42"
diagnostic(#"a1b2") == "h'A1B2'"
diagnostic([1, 2, 3]) == "[_ 1, 2, 3]"
diagnostic([]) == "[]"
diagnostic((1, 2)) == "[_ 1, 2]"
diagnostic((1, #"ff", 3)) == "[_ 1, h'FF', 3]"
diagnostic([(1, #"ff")]) == "{_ 1: h'FF' }"
diagnostic(Some(42)) == "121([_ 42])"
diagnostic(None) == "122([])"

serialise(self: Data) -> ByteArray

Serialise any value to binary, encoding using CBOR.

This is particularly useful in combination with hashing functions, as a way to obtain a byte representation that matches the serialised representation used by the ledger in the context of on-chain code.

Note that the output matches the output of diagnostic, though with a different encoding. diagnostic is merely a textual representation of the CBOR encoding that is human friendly and useful for debugging.

serialise(42) == #"182a"
serialise(#"a1b2") == #"42a1b2"
serialise([]) == #"80"
serialise((1, 2)) == #"9f0102ff"
serialise((1, #"ff", 3)) == #"9f0141ff03ff"
serialise([(1, #"ff")]) == #"a10141ff"
serialise(Some(42)) == #"d8799f182aff"
serialise(None) == #"d87a80"
Search Document