codec

package
v0.400.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSONIndent

func MarshalJSONIndent(cdc *LegacyAmino, obj interface{}) ([]byte, error)

MarshalJSONIndent provides a utility for indented JSON encoding of an object via an Amino codec. It returns an error if it cannot serialize or indent as JSON.

func MarshalYAML added in v0.400.1

func MarshalYAML(cdc JSONCodec, toPrint proto.Message) ([]byte, error)

MarshalYAML marshals toPrint using JSONCodec to leverage specialized MarshalJSON methods (usually related to serialize data with protobuf or amin depending on a configuration). This involves additional roundtrip through JSON.

func MustMarshalJSONIndent added in v0.400.1

func MustMarshalJSONIndent(cdc *LegacyAmino, obj interface{}) []byte

MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.

func ProtoMarshalJSON added in v0.400.1

func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, error)

ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded bytes of a message.

func RegisterEvidences added in v0.400.1

func RegisterEvidences(cdc *LegacyAmino)

RegisterEvidences registers Tendermint evidence types with the provided Amino codec.

Types

type AminoCodec added in v0.400.1

type AminoCodec struct {
	*LegacyAmino
}

AminoCodec defines a codec that utilizes Codec for both binary and JSON encoding.

func NewAminoCodec added in v0.400.1

func NewAminoCodec(codec *LegacyAmino) *AminoCodec

NewAminoCodec returns a reference to a new AminoCodec

func (*AminoCodec) Marshal added in v0.400.1

func (ac *AminoCodec) Marshal(o ProtoMarshaler) ([]byte, error)

Marshal implements BinaryMarshaler.Marshal method.

func (*AminoCodec) MarshalInterface added in v0.400.1

func (ac *AminoCodec) MarshalInterface(i proto.Message) ([]byte, error)

MarshalInterface is a convenience function for amino marshaling interfaces. The `i` must be an interface. NOTE: to marshal a concrete type, you should use Marshal instead

func (*AminoCodec) MarshalInterfaceJSON added in v0.400.1

func (ac *AminoCodec) MarshalInterfaceJSON(i proto.Message) ([]byte, error)

MarshalInterfaceJSON is a convenience function for amino marshaling interfaces. The `i` must be an interface. NOTE: to marshal a concrete type, you should use MarshalJSON instead

func (*AminoCodec) MarshalJSON added in v0.400.1

func (ac *AminoCodec) MarshalJSON(o proto.Message) ([]byte, error)

MarshalJSON implements JSONCodec.MarshalJSON method, it marshals to JSON using legacy amino codec.

func (*AminoCodec) MarshalLengthPrefixed added in v0.400.1

func (ac *AminoCodec) MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error)

MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method.

func (*AminoCodec) MustMarshal added in v0.400.1

func (ac *AminoCodec) MustMarshal(o ProtoMarshaler) []byte

MustMarshal implements BinaryMarshaler.MustMarshal method.

func (*AminoCodec) MustMarshalJSON added in v0.400.1

func (ac *AminoCodec) MustMarshalJSON(o proto.Message) []byte

MustMarshalJSON implements JSONCodec.MustMarshalJSON method, it executes MarshalJSON except it panics upon failure.

func (*AminoCodec) MustMarshalLengthPrefixed added in v0.400.1

func (ac *AminoCodec) MustMarshalLengthPrefixed(o ProtoMarshaler) []byte

MustMarshalLengthPrefixed implements BinaryMarshaler.MustMarshalLengthPrefixed method.

func (*AminoCodec) MustUnmarshal added in v0.400.1

func (ac *AminoCodec) MustUnmarshal(bz []byte, ptr ProtoMarshaler)

MustUnmarshal implements BinaryMarshaler.MustUnmarshal method.

func (*AminoCodec) MustUnmarshalJSON added in v0.400.1

func (ac *AminoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message)

MustUnmarshalJSON implements JSONCodec.MustUnmarshalJSON method, it executes UnmarshalJSON except it panics upon failure.

func (*AminoCodec) MustUnmarshalLengthPrefixed added in v0.400.1

func (ac *AminoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler)

MustUnmarshalLengthPrefixed implements BinaryMarshaler.MustUnmarshalLengthPrefixed method.

func (*AminoCodec) Unmarshal added in v0.400.1

func (ac *AminoCodec) Unmarshal(bz []byte, ptr ProtoMarshaler) error

Unmarshal implements BinaryMarshaler.Unmarshal method.

func (*AminoCodec) UnmarshalInterface added in v0.400.1

func (ac *AminoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error

UnmarshalInterface is a convenience function for amino unmarshaling interfaces. `ptr` must be a pointer to an interface. NOTE: to unmarshal a concrete type, you should use Unmarshal instead

Example:

var x MyInterface
err := cdc.UnmarshalInterface(bz, &x)

func (*AminoCodec) UnmarshalInterfaceJSON added in v0.400.1

func (ac *AminoCodec) UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error

UnmarshalInterfaceJSON is a convenience function for amino unmarshaling interfaces. `ptr` must be a pointer to an interface. NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead

Example:

var x MyInterface
err := cdc.UnmarshalInterfaceJSON(bz, &x)

func (*AminoCodec) UnmarshalJSON added in v0.400.1

func (ac *AminoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error

UnmarshalJSON implements JSONCodec.UnmarshalJSON method, it unmarshals from JSON using legacy amino codec.

func (*AminoCodec) UnmarshalLengthPrefixed added in v0.400.1

func (ac *AminoCodec) UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error

UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method.

type AminoMarshaler added in v0.400.1

type AminoMarshaler interface {
	MarshalAmino() ([]byte, error)
	UnmarshalAmino([]byte) error
	MarshalAminoJSON() ([]byte, error)
	UnmarshalAminoJSON([]byte) error
}

AminoMarshaler defines an interface a type must implement to serialize itself for Amino codec.

type BinaryCodec added in v0.400.1

type BinaryCodec interface {
	// Marshal returns binary encoding of v.
	Marshal(o ProtoMarshaler) ([]byte, error)
	// MustMarshal calls Marshal and panics if error is returned.
	MustMarshal(o ProtoMarshaler) []byte

	// MarshalLengthPrefixed returns binary encoding of v with bytes length prefix.
	MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error)
	// MustMarshalLengthPrefixed calls MarshalLengthPrefixed and panics if
	// error is returned.
	MustMarshalLengthPrefixed(o ProtoMarshaler) []byte

	// Unmarshal parses the data encoded with Marshal method and stores the result
	// in the value pointed to by v.
	Unmarshal(bz []byte, ptr ProtoMarshaler) error
	// MustUnmarshal calls Unmarshal and panics if error is returned.
	MustUnmarshal(bz []byte, ptr ProtoMarshaler)

	// Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores
	// the result in the value pointed to by v.
	UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
	// MustUnmarshalLengthPrefixed calls UnmarshalLengthPrefixed and panics if error
	// is returned.
	MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler)

	// MarshalInterface is a helper method which will wrap `i` into `Any` for correct
	// binary interface (de)serialization.
	MarshalInterface(i proto.Message) ([]byte, error)
	// UnmarshalInterface is a helper method which will parse binary enoded data
	// into `Any` and unpack any into the `ptr`. It fails if the target interface type
	// is not registered in codec, or is not compatible with the serialized data
	UnmarshalInterface(bz []byte, ptr interface{}) error

	types.AnyUnpacker
}

type Codec

type Codec interface {
	BinaryCodec
	JSONCodec
}

Codec defines a functionality for serializing other objects. Users can defin a custom Protobuf-based serialization. Note, Amino can still be used without any dependency on Protobuf. SDK provides to Codec implementations:

1. AminoCodec: Provides full Amino serialization compatibility. 2. ProtoCodec: Provides full Protobuf serialization compatibility.

type JSONCodec added in v0.400.1

type JSONCodec interface {
	// MarshalJSON returns JSON encoding of v.
	MarshalJSON(o proto.Message) ([]byte, error)
	// MustMarshalJSON calls MarshalJSON and panics if error is returned.
	MustMarshalJSON(o proto.Message) []byte
	// MarshalInterfaceJSON is a helper method which will wrap `i` into `Any` for correct
	// JSON interface (de)serialization.
	MarshalInterfaceJSON(i proto.Message) ([]byte, error)
	// UnmarshalInterfaceJSON is a helper method which will parse JSON enoded data
	// into `Any` and unpack any into the `ptr`. It fails if the target interface type
	// is not registered in codec, or is not compatible with the serialized data
	UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error

	// UnmarshalJSON parses the data encoded with MarshalJSON method and stores the result
	// in the value pointed to by v.
	UnmarshalJSON(bz []byte, ptr proto.Message) error
	// MustUnmarshalJSON calls Unmarshal and panics if error is returned.
	MustUnmarshalJSON(bz []byte, ptr proto.Message)
}

type LegacyAmino added in v0.400.1

type LegacyAmino struct {
	Amino *amino.Codec
}

LegacyAmino defines a wrapper for an Amino codec that properly handles protobuf types with Any's. Deprecated.

func NewLegacyAmino added in v0.400.1

func NewLegacyAmino() *LegacyAmino

func (*LegacyAmino) Marshal added in v0.400.1

func (cdc *LegacyAmino) Marshal(o interface{}) ([]byte, error)

func (*LegacyAmino) MarshalJSON added in v0.400.1

func (cdc *LegacyAmino) MarshalJSON(o interface{}) ([]byte, error)

MarshalJSON implements codec.Codec interface

func (*LegacyAmino) MarshalJSONIndent added in v0.400.1

func (cdc *LegacyAmino) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error)

func (*LegacyAmino) MarshalLengthPrefixed added in v0.400.1

func (cdc *LegacyAmino) MarshalLengthPrefixed(o interface{}) ([]byte, error)

func (*LegacyAmino) MustMarshal added in v0.400.1

func (cdc *LegacyAmino) MustMarshal(o interface{}) []byte

func (*LegacyAmino) MustMarshalJSON added in v0.400.1

func (cdc *LegacyAmino) MustMarshalJSON(o interface{}) []byte

func (*LegacyAmino) MustMarshalLengthPrefixed added in v0.400.1

func (cdc *LegacyAmino) MustMarshalLengthPrefixed(o interface{}) []byte

func (*LegacyAmino) MustUnmarshal added in v0.400.1

func (cdc *LegacyAmino) MustUnmarshal(bz []byte, ptr interface{})

func (*LegacyAmino) MustUnmarshalJSON added in v0.400.1

func (cdc *LegacyAmino) MustUnmarshalJSON(bz []byte, ptr interface{})

func (*LegacyAmino) MustUnmarshalLengthPrefixed added in v0.400.1

func (cdc *LegacyAmino) MustUnmarshalLengthPrefixed(bz []byte, ptr interface{})

func (*LegacyAmino) PrintTypes added in v0.400.1

func (cdc *LegacyAmino) PrintTypes(out io.Writer) error

func (*LegacyAmino) RegisterConcrete added in v0.400.1

func (cdc *LegacyAmino) RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions)

func (*LegacyAmino) RegisterInterface added in v0.400.1

func (cdc *LegacyAmino) RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions)

func (*LegacyAmino) Seal added in v0.400.1

func (cdc *LegacyAmino) Seal()

func (*LegacyAmino) Unmarshal added in v0.400.1

func (cdc *LegacyAmino) Unmarshal(bz []byte, ptr interface{}) error

func (*LegacyAmino) UnmarshalJSON added in v0.400.1

func (cdc *LegacyAmino) UnmarshalJSON(bz []byte, ptr interface{}) error

UnmarshalJSON implements codec.Codec interface

func (*LegacyAmino) UnmarshalLengthPrefixed added in v0.400.1

func (cdc *LegacyAmino) UnmarshalLengthPrefixed(bz []byte, ptr interface{}) error

func (*LegacyAmino) UnpackAny added in v0.400.1

func (*LegacyAmino) UnpackAny(*types.Any, interface{}) error

type ProtoCodec added in v0.400.1

type ProtoCodec struct {
	// contains filtered or unexported fields
}

ProtoCodec defines a codec that utilizes Protobuf for both binary and JSON encoding.

func NewProtoCodec added in v0.400.1

func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec

NewProtoCodec returns a reference to a new ProtoCodec

func (*ProtoCodec) InterfaceRegistry added in v0.400.1

func (pc *ProtoCodec) InterfaceRegistry() types.InterfaceRegistry

InterfaceRegistry returns InterfaceRegistry

func (*ProtoCodec) Marshal added in v0.400.1

func (pc *ProtoCodec) Marshal(o ProtoMarshaler) ([]byte, error)

Marshal implements BinaryMarshaler.Marshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.MarshalInterface

func (*ProtoCodec) MarshalInterface added in v0.400.1

func (pc *ProtoCodec) MarshalInterface(i proto.Message) ([]byte, error)

MarshalInterface is a convenience function for proto marshalling interfaces. It packs the provided value, which must be an interface, in an Any and then marshals it to bytes. NOTE: to marshal a concrete type, you should use Marshal instead

func (*ProtoCodec) MarshalInterfaceJSON added in v0.400.1

func (pc *ProtoCodec) MarshalInterfaceJSON(x proto.Message) ([]byte, error)

MarshalInterfaceJSON is a convenience function for proto marshalling interfaces. It packs the provided value in an Any and then marshals it to bytes. NOTE: to marshal a concrete type, you should use MarshalJSON instead

func (*ProtoCodec) MarshalJSON added in v0.400.1

func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error)

MarshalJSON implements JSONCodec.MarshalJSON method, it marshals to JSON using proto codec. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.MarshalInterfaceJSON

func (*ProtoCodec) MarshalLengthPrefixed added in v0.400.1

func (pc *ProtoCodec) MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error)

MarshalLengthPrefixed implements BinaryMarshaler.MarshalLengthPrefixed method.

func (*ProtoCodec) MustMarshal added in v0.400.1

func (pc *ProtoCodec) MustMarshal(o ProtoMarshaler) []byte

MustMarshal implements BinaryMarshaler.MustMarshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.MarshalInterface

func (*ProtoCodec) MustMarshalJSON added in v0.400.1

func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte

MustMarshalJSON implements JSONCodec.MustMarshalJSON method, it executes MarshalJSON except it panics upon failure. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.MarshalInterfaceJSON

func (*ProtoCodec) MustMarshalLengthPrefixed added in v0.400.1

func (pc *ProtoCodec) MustMarshalLengthPrefixed(o ProtoMarshaler) []byte

MustMarshalLengthPrefixed implements BinaryMarshaler.MustMarshalLengthPrefixed method.

func (*ProtoCodec) MustUnmarshal added in v0.400.1

func (pc *ProtoCodec) MustUnmarshal(bz []byte, ptr ProtoMarshaler)

MustUnmarshal implements BinaryMarshaler.MustUnmarshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.UnmarshalInterface

func (*ProtoCodec) MustUnmarshalJSON added in v0.400.1

func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message)

MustUnmarshalJSON implements JSONCodec.MustUnmarshalJSON method, it executes UnmarshalJSON except it panics upon failure. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.UnmarshalInterfaceJSON

func (*ProtoCodec) MustUnmarshalLengthPrefixed added in v0.400.1

func (pc *ProtoCodec) MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler)

MustUnmarshalLengthPrefixed implements BinaryMarshaler.MustUnmarshalLengthPrefixed method.

func (*ProtoCodec) Unmarshal added in v0.400.1

func (pc *ProtoCodec) Unmarshal(bz []byte, ptr ProtoMarshaler) error

Unmarshal implements BinaryMarshaler.Unmarshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.UnmarshalInterface

func (*ProtoCodec) UnmarshalInterface added in v0.400.1

func (pc *ProtoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error

UnmarshalInterface is a convenience function for proto unmarshaling interfaces. It unmarshals an Any from bz bytes and then unpacks it to the `ptr`, which must be a pointer to a non empty interface with registered implementations. NOTE: to unmarshal a concrete type, you should use Unmarshal instead

Example:

var x MyInterface
err := cdc.UnmarshalInterface(bz, &x)

func (*ProtoCodec) UnmarshalInterfaceJSON added in v0.400.1

func (pc *ProtoCodec) UnmarshalInterfaceJSON(bz []byte, iface interface{}) error

UnmarshalInterfaceJSON is a convenience function for proto unmarshaling interfaces. It unmarshals an Any from bz bytes and then unpacks it to the `iface`, which must be a pointer to a non empty interface, implementing proto.Message with registered implementations. NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead

Example:

var x MyInterface  // must implement proto.Message
err := cdc.UnmarshalInterfaceJSON(&x, bz)

func (*ProtoCodec) UnmarshalJSON added in v0.400.1

func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error

UnmarshalJSON implements JSONCodec.UnmarshalJSON method, it unmarshals from JSON using proto codec. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.UnmarshalInterfaceJSON

func (*ProtoCodec) UnmarshalLengthPrefixed added in v0.400.1

func (pc *ProtoCodec) UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error

UnmarshalLengthPrefixed implements BinaryMarshaler.UnmarshalLengthPrefixed method.

func (*ProtoCodec) UnpackAny added in v0.400.1

func (pc *ProtoCodec) UnpackAny(any *types.Any, iface interface{}) error

UnpackAny implements AnyUnpacker.UnpackAny method, it unpacks the value in any to the interface pointer passed in as iface.

type ProtoCodecMarshaler added in v0.400.1

type ProtoCodecMarshaler interface {
	Codec
	InterfaceRegistry() types.InterfaceRegistry
}

ProtoCodecMarshaler defines an interface for codecs that utilize Protobuf for both binary and JSON encoding.

type ProtoMarshaler added in v0.400.1

type ProtoMarshaler interface {
	proto.Message // for JSON serialization

	Marshal() ([]byte, error)
	MarshalTo(data []byte) (n int, err error)
	MarshalToSizedBuffer(dAtA []byte) (int, error)
	Size() int
	Unmarshal(data []byte) error
}

ProtoMarshaler defines an interface a type must implement to serialize itself as a protocol buffer defined message.

Directories

Path Synopsis
Package legacy contains a global amino Cdc which is deprecated but still used in several places within the SDK.
Package legacy contains a global amino Cdc which is deprecated but still used in several places within the SDK.
Package types defines a custom wrapper for google.protobuf.Any which supports cached values as well as InterfaceRegistry which keeps track of types which can be used with Any for both security and introspection
Package types defines a custom wrapper for google.protobuf.Any which supports cached values as well as InterfaceRegistry which keeps track of types which can be used with Any for both security and introspection
unknownproto implements functionality to "type check" protobuf serialized byte sequences against an expected proto.Message to report: a) Unknown fields in the stream -- this is indicative of mismatched services, perhaps a malicious actor b) Mismatched wire types for a field -- this is indicative of mismatched services Its API signature is similar to proto.Unmarshal([]byte, proto.Message) in the strict case if err := RejectUnknownFieldsStrict(protoBlob, protoMessage, false); err != nil { // Handle the error.
unknownproto implements functionality to "type check" protobuf serialized byte sequences against an expected proto.Message to report: a) Unknown fields in the stream -- this is indicative of mismatched services, perhaps a malicious actor b) Mismatched wire types for a field -- this is indicative of mismatched services Its API signature is similar to proto.Unmarshal([]byte, proto.Message) in the strict case if err := RejectUnknownFieldsStrict(protoBlob, protoMessage, false); err != nil { // Handle the error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL