node

package
v0.0.0-...-6fdbc3c Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PayloadSize is 50 bytes
	// 32 byte pubkey + 18 byte NetAddr
	PayloadSize = crypto.PublicKeySize + NetAddrSize

	// NetAddrSize is 18 bytes
	// 16 byte ipv6 address + 2 byte port
	NetAddrSize = 18
)

Variables

View Source
var (
	// ErrNodeID is thrown on parsing an invalid node ID
	ErrNodeID = errors.New("invalid node id")

	// ErrPubkey is thrown on an invalid pubkey
	ErrPubkey = errors.New("invalid public key")
)
View Source
var (
	// ErrHost is thrown on an invalid listening address
	ErrHost = errors.New("invalid host")

	// ErrDisconnect is thrown when disconnecting from an invalid peer
	ErrDisconnect = errors.New("already disconnected")
)
View Source
var (
	// ErrDelete is thrown on an unsuccessful delete
	ErrDelete = errors.New("unsuccessful delete")
)

Functions

func Decode

func Decode(id string) (pubkey []byte, ip net.IP, port uint16, err error)

Decode a hex encoded node ID

func Encode

func Encode(pubkey []byte, addr net.IP, port uint16) (id string)

Encode hex encodes a node ID into the format: (32 byte public key || 18 byte host)

func EncodeAddr

func EncodeAddr(addr net.IP, port uint16) []byte

EncodeAddr encodes a host into the format: (16 byte ipv6 addr || 2 byte port)

Types

type Entry

type Entry struct {
	ID  primitive.ObjectID
	Key crypto.KeyPair
}

Entry represents a node store entry

type Key

type Key struct {
	ID        primitive.ObjectID `bson:"_id,omitempty"`
	CreatedAt time.Time          `bson:"created_at,omitempty"`
	UpdatedAt time.Time          `bson:"updated_at,omitempty"`
	Raw       []byte             `bson:"raw"`
}

Key represents an encrypted key pair

type Node

type Node struct {
	ID          string          // unique node ID
	Addr        net.IP          // public listening addr
	Port        uint16          // listening port
	KeyPair     *crypto.KeyPair // Node identifier
	Server      rpc.Server      // RPC server
	Store       Store           // persistent store
	Ctx         context.Context // calling context
	Peers       map[string]Peer // connected peers
	PeerUpdates chan Peer       // provider manager
}

Node represents a host on the network

func New

func New(host string, port uint16) (*Node, error)

New returns an empty node

func (*Node) Connect

func (n *Node) Connect(p Peer) (err error)

Connect to a peer once

func (*Node) Disconnect

func (n *Node) Disconnect(p Peer) error

Disconnect from a peer if connected

func (*Node) Hex

func (n *Node) Hex() string

Hex encode a node ID

func (*Node) Host

func (n *Node) Host() []byte

Host returns the node's public address

func (*Node) Init

func (n *Node) Init(db *mongo.Database, password []byte) error

Init a node and generate a host ID

func (*Node) Start

func (n *Node) Start() error

Start the rpc server and listen for connections

func (*Node) Stop

func (n *Node) Stop() error

Stop the node and terminate the rpc server

type Peer

type Peer struct {
	Pubkey []byte // public key
	Host   net.IP // host ip
	Port   uint16 // tcp port
	// contains filtered or unexported fields
}

Peer wraps an RPC client used to connect to a peer

func NewPeer

func NewPeer(pubkey, addr []byte) (Peer, error)

NewPeer returns an empty peer

func (*Peer) Addr

func (p *Peer) Addr() string

Addr returns a peer's network address

func (*Peer) Connect

func (p *Peer) Connect() error

Connect to a peer over JSON-RPC

func (*Peer) Connected

func (p *Peer) Connected() bool

Connected checks for an active connection

func (*Peer) ID

func (p *Peer) ID() string

ID returns a peer's ID

type Store

type Store struct {
	Ctx ctx.Context       // calling context
	ID  *mongo.Collection // collection of encrypted node identifiers
}

Store wraps a mongo connection to a node store

func (*Store) AddEntry

func (s *Store) AddEntry(raw []byte) (primitive.ObjectID, error)

AddEntry adds an entry to the store and returns its id

func (*Store) GetEntries

func (s *Store) GetEntries(password []byte) ([]Entry, error)

GetEntries decrypts the node store

func (*Store) Init

func (s *Store) Init(db *mongo.Database) error

Init the node store

func (*Store) RemoveEntry

func (s *Store) RemoveEntry(id primitive.ObjectID) error

RemoveEntry removes an entry from the store by id

Jump to

Keyboard shortcuts

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