enr

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package enr implements Ethereum Node Records as defined in EIP-778. A node record holds arbitrary information about a node on the peer-to-peer network. Node information is stored in key/value Pairs. To store and retrieve key/values in a record, use the Entry interface.

Signature Handling

Records must be signed before transmitting them to another node.

Decoding a record doesn't check its Signature. Code working with records from an untrusted source must always verify two things: that the record uses an identity scheme deemed secure, and that the Signature is valid according to the declared scheme.

When creating a record, set the entries you want and use a signing function provided by the identity scheme to add the Signature. Modifying a record invalidates the Signature.

Package enr supports the "secp256k1-keccak" identity scheme.

Index

Constants

View Source
const IDv4 = ID("v4") // the default identity scheme
View Source
const SizeLimit = 300 // maximum encoded size of a node record in bytes

Variables

View Source
var (
	ErrInvalidSig = errors.New("invalid Signature on node record")
)

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether the given error means that a key/value pair is missing from a record.

Types

type Entry

type Entry interface {
	ENRKey() string
	msg.MsgpMember
}

Entry is implemented by known node record entry types.

To define a new entry that is to be included in a node record, create a Go type that satisfies this interface. The type should also implement rlp.Decoder if additional checks are needed on the value.

func WithEntry

func WithEntry(k string, v msg.MsgpMember) Entry

WithEntry wraps any value with a key name. It can be used to set and load arbitrary values in a record. The value v must be supported by rlp. To use WithEntry with Load, the value must be a pointer.

type ID

type ID string

ID is the "id" key, which holds the name of the identity scheme.

func (*ID) DecodeMsg

func (z *ID) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (ID) ENRKey

func (v ID) ENRKey() string

func (ID) EncodeMsg

func (z ID) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (ID) MarshalMsg

func (z ID) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (ID) Msgsize

func (z ID) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*ID) UnmarshalMsg

func (z *ID) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type IP

type IP net.IP

IP is the "ip" key, which holds the IP address of the node.

func (*IP) DecodeMsg

func (v *IP) DecodeMsg(en *msgp.Reader) error

func (IP) ENRKey

func (v IP) ENRKey() string

func (IP) EncodeMsg

func (v IP) EncodeMsg(en *msgp.Writer) (err error)

func (IP) MarshalMsg

func (v IP) MarshalMsg(b []byte) ([]byte, error)

EncodeRLP implements rlp.Encoder.

func (IP) Msgsize

func (v IP) Msgsize() int

func (*IP) UnmarshalMsg

func (v *IP) UnmarshalMsg(b []byte) ([]byte, error)

DecodeRLP implements rlp.Decoder.

type IdentityScheme

type IdentityScheme interface {
	Verify(r *Record, sig []byte) error
	NodeAddr(r *Record) []byte
}

An IdentityScheme is capable of verifying record Signatures and deriving node addresses.

type KeyError

type KeyError struct {
	Key string
	Err error
}

KeyError is an error related to a key.

func (*KeyError) Error

func (err *KeyError) Error() string

Error implements error.

type Pair

type Pair struct {
	K string
	//v rlp.RawValue
	V []byte
}

Pair is a key/value Pair in a record.

func (*Pair) DecodeMsg

func (z *Pair) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Pair) EncodeMsg

func (z *Pair) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Pair) MarshalMsg

func (z *Pair) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Pair) Msgsize

func (z *Pair) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Pair) UnmarshalMsg

func (z *Pair) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Record

type Record struct {
	Seq       uint64 // sequence number
	Signature []byte // the Signature
	Pairs     []Pair // sorted list of all key/value Pairs
	// contains filtered or unexported fields
}

Record represents a node record. The zero value is an empty record.

func (*Record) AppendElements

func (r *Record) AppendElements(list []msg.MsgpMember) []msg.MsgpMember

AppendElements appends the sequence number and entries to the given slice.

func (*Record) Decode

func (r *Record) Decode(b []byte) ([]byte, error)

DecodeRLP implements rlp.Decoder. Decoding verifies the Signature.

func (*Record) DecodeMsg

func (z *Record) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Record) Encode

func (r Record) Encode(b []byte) ([]byte, error)

EncodeRLP implements rlp.Encoder. Encoding fails if the record is unsigned.

func (*Record) EncodeMsg

func (z *Record) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Record) Equal

func (r Record) Equal(r1 Record) (ok bool, reason error)

func (*Record) GetSeq

func (r *Record) GetSeq() uint64

Seq returns the sequence number.

func (*Record) IdentityScheme

func (r *Record) IdentityScheme() string

IdentityScheme returns the name of the identity scheme in the record.

func (*Record) Load

func (r *Record) Load(e Entry) error

Load retrieves the value of a key/value Pair. The given Entry must be a pointer and will be set to the value of the entry in the record.

Errors returned by Load are wrapped in KeyError. You can distinguish decoding errors from missing keys using the IsNotFound function.

func (*Record) MarshalMsg

func (z *Record) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Record) Msgsize

func (z *Record) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Record) Set

func (r *Record) Set(e Entry)

Set adds or updates the given entry in the record. It panics if the value can't be encoded. If the record is signed, Set increments the sequence number and invalidates the sequence number.

func (*Record) SetSeq

func (r *Record) SetSeq(s uint64)

SetSeq updates the record sequence number. This invalidates any Signature on the record. Calling SetSeq is usually not required because setting any key in a signed record increments the sequence number.

func (*Record) SetSig

func (r *Record) SetSig(s IdentityScheme, sig []byte) error

SetSig sets the record Signature. It returns an error if the encoded record is larger than the size limit or if the Signature is invalid according to the passed scheme.

You can also use SetSig to remove the Signature explicitly by passing a nil scheme and Signature.

SetSig panics when either the scheme or the Signature (but not both) are nil.

func (*Record) UnmarshalMsg

func (z *Record) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

func (*Record) VerifySignature

func (r *Record) VerifySignature(s IdentityScheme) error

VerifySignature checks whether the record is signed using the given identity scheme.

type SchemeMap

type SchemeMap map[string]IdentityScheme

SchemeMap is a registry of named identity schemes.

func (SchemeMap) NodeAddr

func (m SchemeMap) NodeAddr(r *Record) []byte

func (SchemeMap) Verify

func (m SchemeMap) Verify(r *Record, sig []byte) error

type TCP

type TCP uint16

TCP is the "tcp" key, which holds the TCP port of the node.

func (*TCP) DecodeMsg

func (z *TCP) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (TCP) ENRKey

func (v TCP) ENRKey() string

func (TCP) EncodeMsg

func (z TCP) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (TCP) MarshalMsg

func (z TCP) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (TCP) Msgsize

func (z TCP) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*TCP) UnmarshalMsg

func (z *TCP) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type UDP

type UDP uint16

UDP is the "udp" key, which holds the UDP port of the node.

func (*UDP) DecodeMsg

func (z *UDP) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (UDP) ENRKey

func (v UDP) ENRKey() string

func (UDP) EncodeMsg

func (z UDP) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (UDP) MarshalMsg

func (z UDP) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (UDP) Msgsize

func (z UDP) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*UDP) UnmarshalMsg

func (z *UDP) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

Jump to

Keyboard shortcuts

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