Documentation ¶
Index ¶
- Constants
- Variables
- func DBytes32(r io.Reader, val interface{}, _ *[8]byte, l uint64) error
- func DBytes33(r io.Reader, val interface{}, _ *[8]byte, l uint64) error
- func DBytes64(r io.Reader, val interface{}, _ *[8]byte, l uint64) error
- func DNOP(io.Reader, interface{}, *[8]byte, uint64) error
- func DPubKey(r io.Reader, val interface{}, _ *[8]byte, l uint64) error
- func DTUint16(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DTUint32(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DTUint64(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DUint16(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DUint32(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DUint64(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DUint8(r io.Reader, val interface{}, buf *[8]byte, l uint64) error
- func DVarBytes(r io.Reader, val interface{}, _ *[8]byte, l uint64) error
- func EBytes32(w io.Writer, val interface{}, _ *[8]byte) error
- func EBytes33(w io.Writer, val interface{}, _ *[8]byte) error
- func EBytes64(w io.Writer, val interface{}, _ *[8]byte) error
- func ENOP(io.Writer, interface{}, *[8]byte) error
- func EPubKey(w io.Writer, val interface{}, _ *[8]byte) error
- func ETUint16(w io.Writer, val interface{}, buf *[8]byte) error
- func ETUint32(w io.Writer, val interface{}, buf *[8]byte) error
- func ETUint64(w io.Writer, val interface{}, buf *[8]byte) error
- func EUint16(w io.Writer, val interface{}, buf *[8]byte) error
- func EUint16T(w io.Writer, val uint16, buf *[8]byte) error
- func EUint32(w io.Writer, val interface{}, buf *[8]byte) error
- func EUint32T(w io.Writer, val uint32, buf *[8]byte) error
- func EUint64(w io.Writer, val interface{}, buf *[8]byte) error
- func EUint64T(w io.Writer, val uint64, buf *[8]byte) error
- func EUint8(w io.Writer, val interface{}, buf *[8]byte) error
- func EUint8T(w io.Writer, val uint8, buf *[8]byte) error
- func EVarBytes(w io.Writer, val interface{}, _ *[8]byte) error
- func ReadVarInt(r io.Reader, buf *[8]byte) (uint64, error)
- func RecordsToMap(records []Record) (map[uint64][]byte, error)
- func SizeTUint16(v uint16) uint64
- func SizeTUint32(v uint32) uint64
- func SizeTUint64(v uint64) uint64
- func SortRecords(records []Record)
- func WriteVarInt(w io.Writer, val uint64, buf *[8]byte) error
- type Decoder
- type Encoder
- type ErrTypeForDecoding
- type ErrTypeForEncoding
- type ErrUnknownRequiredType
- type Record
- func MakeDynamicRecord(typ Type, val interface{}, sizeFunc SizeFunc, encoder Encoder, decoder Decoder) Record
- func MakePrimitiveRecord(typ Type, val interface{}) Record
- func MakeStaticRecord(typ Type, val interface{}, size uint64, encoder Encoder, decoder Decoder) Record
- func MapToRecords(tlvMap map[uint64][]byte) ([]Record, error)
- type SizeFunc
- type Stream
- type Type
- type TypeSet
Constants ¶
const MaxRecordSize = 65535 // 65KB
MaxRecordSize is the maximum size of a particular record that will be parsed by a stream decoder. This value is currently chosen to the be equal to the maximum message size permitted by BOLT 1, as no record should be bigger than an entire message.
Variables ¶
var ErrRecordTooLarge = errors.New("record is too large")
ErrRecordTooLarge signals that a decoded record has a length that is too long to parse.
var ErrStreamNotCanonical = errors.New("tlv stream is not canonical")
ErrStreamNotCanonical signals that a decoded stream does not contain records sorting by monotonically-increasing type.
var ErrTUintNotMinimal = errors.New("truncated uint not minimally encoded")
ErrTUintNotMinimal signals that decoding a truncated uint failed because the value was not minimally encoded.
var ErrVarIntNotCanonical = errors.New("decoded varint is not canonical")
ErrVarIntNotCanonical signals that the decoded varint was not minimally encoded.
Functions ¶
func DBytes32 ¶
DBytes32 is a Decoder for 32-byte arrays. An error is returned if val is not a *[32]byte.
func DBytes33 ¶
DBytes33 is a Decoder for 33-byte arrays. An error is returned if val is not a *[33]byte.
func DBytes64 ¶
DBytes64 is an Decoder for 64-byte arrays. An error is returned if val is not a *[64]byte.
func DPubKey ¶
DPubKey is a Decoder for *btcec.PublicKey values. An error is returned if val is not a **btcec.PublicKey.
func DTUint16 ¶
DTUint16 is an Decoder for truncated uint16 values, where leading zeros will be resurrected. An error is returned if val is not a *uint16.
func DTUint32 ¶
DTUint32 is an Decoder for truncated uint32 values, where leading zeros will be resurrected. An error is returned if val is not a *uint32.
func DTUint64 ¶
DTUint64 is an Decoder for truncated uint64 values, where leading zeros will be resurrected. An error is returned if val is not a *uint64.
func DUint16 ¶
DUint16 is a Decoder for uint16 values. An error is returned if val is not a *uint16.
func DUint32 ¶
DUint32 is a Decoder for uint32 values. An error is returned if val is not a *uint32.
func DUint64 ¶
DUint64 is a Decoder for uint64 values. An error is returned if val is not a *uint64.
func DVarBytes ¶
DVarBytes is a Decoder for variable byte slices. An error is returned if val is not *[]byte.
func EBytes32 ¶
EBytes32 is an Encoder for 32-byte arrays. An error is returned if val is not a *[32]byte.
func EBytes33 ¶
EBytes33 is an Encoder for 33-byte arrays. An error is returned if val is not a *[33]byte.
func EBytes64 ¶
EBytes64 is an Encoder for 64-byte arrays. An error is returned if val is not a *[64]byte.
func EPubKey ¶
EPubKey is an Encoder for *btcec.PublicKey values. An error is returned if val is not a **btcec.PublicKey.
func ETUint16 ¶
ETUint16 is an Encoder for truncated uint16 values, where leading zeros will be omitted. An error is returned if val is not a *uint16.
func ETUint32 ¶
ETUint32 is an Encoder for truncated uint32 values, where leading zeros will be omitted. An error is returned if val is not a *uint32.
func ETUint64 ¶
ETUint64 is an Encoder for truncated uint64 values, where leading zeros will be omitted. An error is returned if val is not a *uint64.
func EUint16 ¶
EUint16 is an Encoder for uint16 values. An error is returned if val is not a *uint16.
func EUint16T ¶
EUint16T encodes a uint16 val to the provided io.Writer. This method is exposed so that encodings for custom uint16-like types can be created without incurring an extra heap allocation.
func EUint32 ¶
EUint32 is an Encoder for uint32 values. An error is returned if val is not a *uint32.
func EUint32T ¶
EUint32T encodes a uint32 val to the provided io.Writer. This method is exposed so that encodings for custom uint32-like types can be created without incurring an extra heap allocation.
func EUint64 ¶
EUint64 is an Encoder for uint64 values. An error is returned if val is not a *uint64.
func EUint64T ¶
EUint64T encodes a uint64 val to the provided io.Writer. This method is exposed so that encodings for custom uint64-like types can be created without incurring an extra heap allocation.
func EUint8T ¶
EUint8T encodes a uint8 val to the provided io.Writer. This method is exposed so that encodings for custom uint8-like types can be created without incurring an extra heap allocation.
func EVarBytes ¶
EVarBytes is an Encoder for variable byte slices. An error is returned if val is not *[]byte.
func ReadVarInt ¶
ReadVarInt reads a variable length integer from r and returns it as a uint64.
func RecordsToMap ¶
RecordsToMap encodes a series of TLV records as raw key-value pairs in the form of a map.
func SizeTUint16 ¶
SizeTUint16 returns the number of bytes remaining in a uint16 after truncating the leading zeros.
func SizeTUint32 ¶
SizeTUint32 returns the number of bytes remaining in a uint32 after truncating the leading zeros.
func SizeTUint64 ¶
SizeTUint64 returns the number of bytes remaining in a uint64 after truncating the leading zeros.
func SortRecords ¶
func SortRecords(records []Record)
SortRecords is a helper function that will sort a slice of records in place according to their type.
Types ¶
type Decoder ¶
Decoder is a signature for methods that can decode TLV values. An error should be returned if the Decoder cannot support the underlying type of val. The provided scratch buffer must be non-nil.
type Encoder ¶
Encoder is a signature for methods that can encode TLV values. An error should be returned if the Encoder cannot support the underlying type of val. The provided scratch buffer must be non-nil.
func StubEncoder ¶
StubEncoder is a factory function that makes a stub tlv.Encoder out of a raw value. We can use this to make a record that can be encoded when we don't actually know it's true underlying value, and only it serialization.
type ErrTypeForDecoding ¶
type ErrTypeForDecoding struct {
// contains filtered or unexported fields
}
ErrTypeForDecoding signals that an incorrect type was passed to a Decoder or that the expected length of the encoding is different from that required by the expected type.
func NewTypeForDecodingErr ¶
func NewTypeForDecodingErr(val interface{}, expType string, valLength, expLength uint64) ErrTypeForDecoding
NewTypeForDecodingErr creates a new ErrTypeForDecoding given the incorrect val and expected type, or the mismatch in their expected lengths.
func (ErrTypeForDecoding) Error ¶
func (e ErrTypeForDecoding) Error() string
Error returns a human-readable description of the type mismatch.
type ErrTypeForEncoding ¶
type ErrTypeForEncoding struct {
// contains filtered or unexported fields
}
ErrTypeForEncoding signals that an incorrect type was passed to an Encoder.
func NewTypeForEncodingErr ¶
func NewTypeForEncodingErr(val interface{}, expType string) ErrTypeForEncoding
NewTypeForEncodingErr creates a new ErrTypeForEncoding given the incorrect val and the expected type.
func (ErrTypeForEncoding) Error ¶
func (e ErrTypeForEncoding) Error() string
Error returns a human-readable description of the type mismatch.
type ErrUnknownRequiredType ¶
type ErrUnknownRequiredType Type
ErrUnknownRequiredType is an error returned when decoding an unknown and even type from a Stream.
func (ErrUnknownRequiredType) Error ¶
func (t ErrUnknownRequiredType) Error() string
Error returns a human-readable description of unknown required type.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record holds the required information to encode or decode a TLV record.
func MakeDynamicRecord ¶
func MakeDynamicRecord(typ Type, val interface{}, sizeFunc SizeFunc, encoder Encoder, decoder Decoder) Record
MakeDynamicRecord creates a record whose size may vary, and will be determined at the time of encoding via sizeFunc.
func MakePrimitiveRecord ¶
MakePrimitiveRecord creates a record for common types.
func MakeStaticRecord ¶
func MakeStaticRecord(typ Type, val interface{}, size uint64, encoder Encoder, decoder Decoder) Record
MakeStaticRecord creates a record for a field of fixed-size
func MapToRecords ¶
MapToRecords encodes the passed TLV map as a series of regular tlv.Record instances. The resulting set of records will be returned in sorted order by their type.
func (*Record) Encode ¶
Encode writes out the TLV record to the passed writer. This is useful when a caller wants to obtain the raw encoding of a *single* TLV record, outside the context of the Stream struct.
type SizeFunc ¶
type SizeFunc func() uint64
SizeFunc is a function that can compute the length of a given field. Since the size of the underlying field can change, this allows the size of the field to be evaluated at the time of encoding.
func SizeVarBytes ¶
SizeVarBytes returns a SizeFunc that can compute the length of a byte slice.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream defines a TLV stream that can be used for encoding or decoding a set of TLV Records.
func MustNewStream ¶
MustNewStream creates a new TLV Stream given an encoding codec, a decoding codec, and a set of known records. If an error is encountered in creating the stream, this method will panic instead of returning the error.
func NewStream ¶
NewStream creates a new TLV Stream given an encoding codec, a decoding codec, and a set of known records.
func (*Stream) Decode ¶
Decode deserializes TLV Stream from the passed io.Reader. The Stream will inspect each record that is parsed and check to see if it has a corresponding Record to facilitate deserialization of that field. If the record is unknown, the Stream will discard the record's bytes and proceed to the subsequent record.
Each record has the following format:
[varint: type] [varint: length] [length: value]
A series of (possibly zero) records are concatenated into a stream, this example contains two records:
(t: 0x01, l: 0x04, v: 0xff, 0xff, 0xff, 0xff) (t: 0x02, l: 0x01, v: 0x01)
This method asserts that the byte stream is canonical, namely that each record is unique and that all records are sorted in ascending order. An ErrNotCanonicalStream error is returned if the encoded TLV stream is not.
We permit an io.EOF error only when reading the type byte which signals that the last record was read cleanly and we should stop parsing. All other io.EOF or io.ErrUnexpectedEOF errors are returned.
func (*Stream) DecodeWithParsedTypes ¶
DecodeWithParsedTypes is identical to Decode, but if successful, returns a TypeSet containing the types of all records that were decoded or ignored from the stream.
func (*Stream) Encode ¶
Encode writes a Stream to the passed io.Writer. Each of the Records known to the Stream is written in ascending order of their type so as to be canonical.
The stream is constructed by concatenating the individual, serialized Records where each record has the following format:
[varint: type] [varint: length] [length: value]
An error is returned if the io.Writer fails to accept bytes from the encoding, and nothing else. The ordering of the Records is asserted upon the creation of a Stream, and thus the output will be by definition canonical.