inet256

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: GPL-3.0, MPL-2.0 Imports: 16 Imported by: 16

Documentation

Index

Constants

View Source
const (
	// AddrSize is the size of an address in bytes
	AddrSize = 32
	// Base64Alphabet is used when encoding IDs as base64 strings.
	// It is a URL and filepath safe encoding, which maintains ordering.
	Base64Alphabet = "-0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "_" + "abcdefghijklmnopqrstuvwxyz"
)
View Source
const (
	// MaxPublicKeySize is the maximum size of a serialized PublicKey in bytes
	MaxPublicKeySize = 1 << 15
	// MaxSignatureSize is the maximum size of a signature in bytes
	MaxSignatureSize = 1 << 15
)
View Source
const (
	// MinMTU is the minimum MTU a network can provide to any address.
	// Applications should be designed to operate correctly if they can only send messages up to this size.
	MinMTU = 1 << 15
	// MaxMTU is the size of largest message that a network will ever receive from any address.
	// Applications should be prepared to receieve this much data at a time or they may encounter io.ErrShortBuffer
	MaxMTU = 1 << 16
)

Variables

View Source
var (
	ErrPublicKeyNotFound = p2p.ErrPublicKeyNotFound
	ErrNoAddrWithPrefix  = errors.New("no address with prefix")
	ErrClosed            = net.ErrClosed
)

Functions

func GenerateKey added in v0.0.7

func GenerateKey(rng io.Reader) (PublicKey, PrivateKey, error)

GenerateKey generates a new key pair using entropy read from rng.

The algorithm used currently is Ed25519, but this may change at any time, and callers *must not* depend on this. If a specific public key algorithm is required use the standard library to generate a key and convert it using PrivateKeyFromBuiltIn.

func HasPrefix

func HasPrefix(x []byte, prefix []byte, nbits int) bool

func IsErrClosed

func IsErrClosed(err error) bool

func IsErrPublicKeyNotFound added in v0.0.3

func IsErrPublicKeyNotFound(err error) bool

func IsErrUnreachable added in v0.0.3

func IsErrUnreachable(err error) bool

func MarshalPublicKey

func MarshalPublicKey(out []byte, pubKey PublicKey) []byte

MarshalPublicKey marshals pubKey and the resulting bytes to out. All keys returned by ParsePublic key will successfully marshal, so a panic indicates a bug.

func NewPacketConn

func NewPacketConn(n Node) net.PacketConn

NewPacketConn wraps a node with the net.PacketConn interface

func Receive

func Receive(ctx context.Context, n Node, msg *Message) error

Receive is a utility function for copying a message from the Node n into msg

func Sign added in v0.0.7

func Sign(out []byte, privateKey PrivateKey, purpose string, msg []byte) []byte

Sign appends a signature to out and returns it. Sign implements the INET256 Signature Scheme.

func Verify added in v0.0.7

func Verify(publicKey PublicKey, purpose string, msg, sig []byte) bool

Verify checks that sig is a valid signature for msg, produces by publicKey. Verify returns true for a correct signature and false otherwise. Verify implements the INET256 Signature Scheme

Types

type Addr

type Addr [AddrSize]byte

Addr is an address in an INET256 Network. It uniquely identifies a Node.

func AddrFromBytes

func AddrFromBytes(x []byte) Addr

AddrFromBytes creates a new address by reading up to 32 bytes from x Note that these bytes are not interpretted as a public key, they are interpretted as the raw address. To derive an address from a PublicKey use NewAddr

func NewAddr

func NewAddr(pubKey PublicKey) Addr

NewAddr creates a new Addr from a PublicKey

func ParseAddrBase64 added in v0.0.5

func ParseAddrBase64(data []byte) (Addr, error)

ParseAddrBase64 attempts to parse a base64 encoded INET256 address from data

func (Addr) Base64String added in v0.0.5

func (a Addr) Base64String() string

Base64String returns the base64 encoding of the Addr as a string

func (Addr) IsZero

func (a Addr) IsZero() bool

IsZero returns true if the address is the zero value for the Addr type

func (Addr) MarshalText

func (a Addr) MarshalText() ([]byte, error)

func (Addr) Network

func (a Addr) Network() string

Network implements net.Addr.Network

func (Addr) String

func (a Addr) String() string

String implements net.Addr.String

func (*Addr) UnmarshalText

func (a *Addr) UnmarshalText(x []byte) error

type Ed25519PrivateKey added in v0.0.7

type Ed25519PrivateKey [ed25519.PrivateKeySize]byte

func (*Ed25519PrivateKey) BuiltIn added in v0.0.7

func (pk *Ed25519PrivateKey) BuiltIn() crypto.Signer

func (*Ed25519PrivateKey) Public added in v0.0.7

func (pk *Ed25519PrivateKey) Public() PublicKey

type Ed25519PublicKey added in v0.0.7

type Ed25519PublicKey [ed25519.PublicKeySize]byte

Ed25519PublicKey implements PublicKey

func (*Ed25519PublicKey) BuiltIn added in v0.0.7

func (pk *Ed25519PublicKey) BuiltIn() crypto.PublicKey

BuiltIn implements PublicKey.BuiltIn

type ErrAddrUnreachable

type ErrAddrUnreachable struct {
	Addr Addr
}

func (ErrAddrUnreachable) Error

func (e ErrAddrUnreachable) Error() string

type ID

type ID = Addr

ID is an alias for Addr

type Message

type Message struct {
	Src     Addr
	Dst     Addr
	Payload []byte
}

Message is the essential information carried by Tell and Receive provided as a struct for use in queues or other APIs

type Node

type Node interface {
	// Tell sends a message containing data to the node at addr.
	// The message will be delivered at most once.
	Send(ctx context.Context, addr Addr, data []byte) error
	// Receive calls fn with a message sent to this node.
	// The message fields, and payload must not be accessed outside fn.
	Receive(ctx context.Context, fn ReceiveFunc) error

	// MTU finds the maximum message size that can be sent to addr.
	// If the context expires, a reasonable default (normally a significant underestimate) will be returned.
	MTU(ctx context.Context, addr Addr) int
	// LookupPublicKey attempts to find the public key corresponding to addr.
	// If it can't find it, ErrPublicKeyNotFound is returned.
	LookupPublicKey(ctx context.Context, addr Addr) (PublicKey, error)
	// FindAddr looks for an address with nbits leading bits in common with prefix.
	FindAddr(ctx context.Context, prefix []byte, nbits int) (Addr, error)

	// LocalAddr returns this Node's address
	LocalAddr() Addr
	// PublicKey returns this Node's public key
	PublicKey() PublicKey

	// Close indicates no more messages should be sent or received from this node
	// and releases any resources allocated for this node.
	Close() error
}

Node is a single host in an INET256 network. Nodes send and receive messages to and from other nodes in the network. Nodes are usually created and managed by a Service. Nodes have an single ID or address corresponding to their public key.

This interface is compatible with the INET256 specification.

type NodeConfig added in v0.0.3

type NodeConfig struct {
}

NodeConfig is an aggregate of applied NodeOptions Not all implementations will support all the options.

func CollectNodeOptions added in v0.0.3

func CollectNodeOptions(opts []NodeOption) (cfg NodeConfig)

CollectNodeOptions applyies each option in opts to a NodeOptions and returns the result.

type NodeOption added in v0.0.3

type NodeOption = func(*NodeConfig)

NodeOption is the type of functions which configure a Node.

type PrivateKey

type PrivateKey interface {
	// BuiltIn returns a crypto.Signer from the standard library.
	BuiltIn() crypto.Signer

	// Public returns the corresponding public key for this PrivateKey
	Public() PublicKey
	// contains filtered or unexported methods
}

func PrivateKeyFromBuiltIn added in v0.0.7

func PrivateKeyFromBuiltIn(x crypto.Signer) (PrivateKey, error)

PrivateKeyFromBuiltIn

type PublicKey

type PublicKey interface {
	// BuiltIn returns a crypto.PublicKey from the standard library
	BuiltIn() crypto.PublicKey
	// contains filtered or unexported methods
}

PublicKey is a signing key used to prove identity within INET256.

func ParsePublicKey

func ParsePublicKey(data []byte) (PublicKey, error)

ParsePublicKey attempts to parse a PublicKey from data

func PublicKeyFromBuiltIn added in v0.0.7

func PublicKeyFromBuiltIn(x crypto.PublicKey) (PublicKey, error)

PublicKeyFromBuiltIn returns a PublicKey from the BuiltIn crypto.PublicKey type

type RSAPrivateKey added in v0.0.7

type RSAPrivateKey rsa.PrivateKey

func (*RSAPrivateKey) BuiltIn added in v0.0.7

func (pk *RSAPrivateKey) BuiltIn() crypto.Signer

func (*RSAPrivateKey) Public added in v0.0.7

func (pk *RSAPrivateKey) Public() PublicKey

type RSAPublicKey added in v0.0.7

type RSAPublicKey rsa.PublicKey

RSAPublicKey implements PublicKey

func (*RSAPublicKey) BuiltIn added in v0.0.7

func (pk *RSAPublicKey) BuiltIn() crypto.PublicKey

BuiltIn implements PublicKey.BuiltIn

type ReceiveFunc

type ReceiveFunc = func(Message)

ReceiveFunc is passed as a callback to Node.Receive

type Service

type Service interface {
	Open(ctx context.Context, privKey PrivateKey, opts ...NodeOption) (Node, error)
	Drop(ctx context.Context, privKey PrivateKey) error
}

Service is the top level INET256 object. It manages a set of nodes which can be created and deleted.

This interface is compatible with the INET256 specification.

Jump to

Keyboard shortcuts

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