Skip to main content

Crate bitcoin_consensus_encoding

Crate bitcoin_consensus_encoding 

Source
Expand description

§Rust Bitcoin Consensus Encoding

Traits and utilities for encoding and decoding Bitcoin data types in a consensus-consistent way, using a sans-I/O architecture.

Rather than reading from or writing to std::io::Read/std::io::Write traits directly, the codec types work with byte slices. This keeps codec logic I/O-agnostic, so the same implementation works in no_std environments, sync I/O, async I/O, and hash engines without duplicating logic or surfacing I/O errors in non-I/O contexts (e.g. when hashing an encoding).

Consensus encoding is the canonical byte representation of Bitcoin data types used across the peer-to-peer network and transaction serialization. This crate only supports deterministic encoding and will never support types like floats whose encoding is non-deterministic or platform-dependent.

§Encoding

Types implement Encodable to produce an Encoder, which yields encoded bytes in chunks via Encoder::current_chunk and Encoder::advance. The caller drives the process by pulling chunks until advance returns false.

§Decoding

Types implement Decodable to produce a Decoder, which consumes bytes via Decoder::push_bytes until it signals completion by returning Ok(false). The caller then calls Decoder::end to obtain the decoded value.

Unlike encoding, decoding is fallible. Both push_bytes and end return Result. I/O errors are handled by the caller, keeping the codec logic I/O-agnostic.

§Drivers

This crate provides free functions which drive codecs for common I/O interfaces. On the decoding side we provide:

And on the encoding side we provide:

§Feature Flags

  • std - Enables std lib I/O driver functions and std::error::Error impls (implies alloc).
  • alloc - Enables encode_to_vec, Vec-based decoders, and allocation-based helpers.

Re-exports§

pub use self::error::LengthPrefixExceedsMaxError;
pub use self::error::ReadError;
pub use self::error::ByteVecDecoderError;
pub use self::error::VecDecoderError;
pub use self::error::CompactSizeDecoderError;
pub use self::error::DecodeError;
pub use self::error::Decoder2Error;
pub use self::error::Decoder3Error;
pub use self::error::Decoder4Error;
pub use self::error::Decoder6Error;
pub use self::error::UnconsumedError;
pub use self::error::UnexpectedEofError;

Modules§

error
Error types for the whole crate.

Macros§

encoder_newtype
Implements a newtype around an encoder.
encoder_newtype_exact
Implements a newtype around an exact-size encoder.

Structs§

ArrayDecoder
A decoder that expects exactly N bytes and returns them as an array.
ArrayEncoder
An encoder for a single array.
ArrayRefEncoder
An encoder for a reference to an array.
ByteVecDecoder
A decoder that decodes a byte vector.
BytesEncoder
An encoder for a single byte slice.
CompactSizeDecoder
Decodes a compact size encoded integer as a length prefix.
CompactSizeEncoder
Encoder for a compact size encoded integer.
CompactSizeU64Decoder
Decodes a compact size encoded integer as a raw u64.
Decoder2
A decoder which wraps two inner decoders and returns the output of both.
Decoder3
A decoder which decodes three objects, one after the other.
Decoder4
A decoder which decodes four objects, one after the other.
Decoder6
A decoder which decodes six objects, one after the other.
EncodableByteIter
Yields bytes from any Encodable instance.
Encoder2
An encoder which encodes two objects, one after the other.
Encoder3
An encoder which encodes three objects, one after the other.
Encoder4
An encoder which encodes four objects, one after the other.
Encoder6
An encoder which encodes six objects, one after the other.
SliceEncoder
An encoder for a list of encodable types.
VecDecoder
A decoder that decodes a vector of Ts.

Traits§

Decodable
A Bitcoin object which can be consensus-decoded using a push decoder.
Decoder
A push decoder for a consensus-decodable object.
Encodable
A Bitcoin object which can be consensus-encoded.
Encoder
An encoder for a consensus-encodable object.
ExactSizeEncoder
An encoder with a known size.

Functions§

decode_from_read
Decodes an object from a buffered reader.
decode_from_read_unbuffered
Decodes an object from an unbuffered reader using a fixed-size buffer.
decode_from_read_unbuffered_with
Decodes an object from an unbuffered reader using a custom-sized buffer.
decode_from_slice
Decodes an object from a byte slice.
decode_from_slice_unbounded
Decodes an object from an unbounded byte slice.
encode_to_vec
Encodes an object into a vector.
encode_to_writer
Encodes an object to a standard I/O writer.
flush_to_vec
Flushes the output of an Encoder into a vector.
flush_to_writer
Flushes the output of an Encoder to a standard I/O writer.