cardano-wallet-core-2022.7.1: The Wallet Backend for a Cardano node.
Copyright © 2018-2020 IOHK
License Apache-2.0
Safe Haskell None
Language Haskell2010

Cardano.Byron.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).

The format described in the decoders below are the one used in the Byron era of Cardano and will endure in the first stages of Shelley. They are also used by components like the Rust cardano-http-bridge .

Synopsis

Decoding

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.

Encoding

encodeAddress :: XPub -> [ Encoding ] -> Encoding Source #

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

Helpers

deserialiseCbor :: ( forall s. Decoder s a) -> ByteString -> Maybe a Source #

Shortcut for deserialising a strict Bytestring with the given decoder.

decodeListIndef :: forall s a. Decoder s a -> Decoder s [a] Source #

Decode an arbitrary long list. CBOR introduce a "break" character to mark the end of the list, so we simply decode each item until we encounter a break character.

    myDecoder :: CBOR.Decoder s [MyType]
    myDecoder = decodeListIndef decodeOne
      where
        decodeOne :: CBOR.Decoder s MyType

decodeNestedBytes :: MonadFail m => ( forall s. Decoder s r) -> ByteString -> m r Source #

Byron CBOR encodings often have CBOR nested in CBOR. This helps decoding a particular ByteString that represents a CBOR object.