Copyright | 2020 Input Output (Hong Kong) Ltd. 2021-2022 Input Output Global Inc. (IOG) 2023-2025 Intersect |
---|---|
License | Apache-2.0 |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Cardano.Codec.Cbor
Description
These are (partial) CBOR decoders for Byron binary types. Note that we ignore most of the block's and header's content and only retrieve the pieces of information relevant to us, wallet (we do assume a trusted node and therefore, we needn't to care about verifying signatures and blocks themselves).
Synopsis
- encodeAddress :: XPub -> [Encoding] -> Encoding
- encodeDerivationPathAttr :: ScrubbedBytes -> Word32 -> Word32 -> Encoding
- decodeAllAttributes :: Decoder s [(Word8, ByteString)]
- deserialiseCbor :: (forall s. Decoder s a) -> ByteString -> Either DeserialiseFailure a
- unsafeDeserialiseCbor :: HasCallStack => (forall s. Decoder s a) -> ByteString -> a
- encodeBytes :: ByteString -> Encoding
- toStrictByteString :: Encoding -> ByteString
- toLazyByteString :: Encoding -> ByteString
Encoders
encodeAddress :: XPub -> [Encoding] -> Encoding Source #
Byron Address Binary Format In the composition of a Cardano address, the following functions concern the "Derivation Path" box. +-------------------------------------------------------------------------------+ | | | CBOR-Serialized Object with CRC¹ | | | +-------------------------------------------------------------------------------+ | | v +-------------------------------------------------------------------------------+ | Address Root | Address Attributes | AddrType | | | | | | Hash (224 bits) | Der. Path² + Stake + NM | PubKey | (Script) | Redeem | | | (open for extension) | (open for extension) | +-------------------------------------------------------------------------------+ | | | | +----------------------------------+ v | | Derivation Path | +---------------------------+ |---->| | | SHA3-256 | | | ChaChaPoly⁴ AccountIx/AddressIx | | |> Blake2b 224 | | +----------------------------------+ | |> CBOR | | | | | | -AddrType | | +----------------------------------+ | -ASD³ (~AddrType+PubKey) | | | Stake Distribution | | -Address Attributes | | | | +---------------------------+ |---->| BootstrapEra | (Single | Multi) | | +----------------------------------+ | | | +----------------------------------+ | | Network Magic | |---->| | | Addr Discr: MainNet vs TestNet | +----------------------------------+
Encode a public key to a corresponding Cardano Address. The encoding of the attributes part of an address is left out to the caller; This allows for distinguishing between Sequential and Random addresses (the former doesn't have any attributes to encode).
-- Old / Random Addresses let encodeAddrAttributes = mempty <> CBOR.encodeMapLen 1 <> CBOR.encodeWord8 1 <> encodeDerivationPath (hdPassphrase rootXPub) accIx addrIx let addr = encodeAddress xpub encodeAddrAttributes -- New / Sequential Addresses let encodeAddrAttributes = mempty <> CBOR.encodeMapLen 0 let addr = encodeAddress xpub encodeAddrAttributes
Note that we are passing the behavior to encode attributes as a parameter
here and do not handle multiple cases in encodeAddress
itself for multiple
reasons:
- Inversion of control gives us a nicer implementation overall
- Encoding attributes for Random addresses requires more context than just the public key (like the wallet root id and some extra logic for encoding passphrases). This is just scheme-specific and is better left out of this particular function
encodeDerivationPathAttr :: ScrubbedBytes -> Word32 -> Word32 -> Encoding Source #
This is the opposite of decodeDerivationPathAttr
.
NOTE: The caller must ensure that the passphrase length is 32 bytes.
Decoders
decodeAllAttributes :: Decoder s [(Word8, ByteString)] Source #
The attributes are pairs of numeric tags and bytes, where the bytes will be CBOR-encoded stuff. This decoder does not enforce "canonicity" of entries.
deserialiseCbor :: (forall s. Decoder s a) -> ByteString -> Either DeserialiseFailure a Source #
Shortcut for deserialising a strict Bytestring
with the given decoder.
unsafeDeserialiseCbor :: HasCallStack => (forall s. Decoder s a) -> ByteString -> a Source #
CBOR deserialise without error handling - handy for prototypes or testing.
Re-exports from CBOR
encodeBytes :: ByteString -> Encoding #
Encode an arbitrary strict ByteString
in
a flattened format.
Since: cborg-0.2.0.0
Arguments
:: Encoding | The |
-> ByteString | The encoded value. |
Turn an Encoding
into a strict ByteString
in CBOR binary
format.
Since: cborg-0.2.0.0
Arguments
:: Encoding | The |
-> ByteString | The encoded CBOR value. |
Turn an Encoding
into a lazy ByteString
in CBOR binary
format.
Since: cborg-0.2.0.0