peer

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 33 Imported by: 2

README

Peer

This package implements the full peer logic in go. It means you don't need the intermediate rmb-relay

The peer once created establishes and retain the connection to your relay. Any message is received by this peer is forwarded (after passing all validation and authorization) to a custom handler

The RpcClient on the other hand is a thin wrapper around the Peer, that allows you to make Rpc calls directly. It does this by building a special handler that routes the received responses directly to the caller but this is completely abstract to the caller.

Functionality

The peer implements the full rmb protocol include:

  • Connecting/Authenticating to relay
  • Building an RMB envelope
    • The envelope type is generated from the types.proto file which is a copy from the one defined by RMB.
  • Sign the envelope
  • Send the messages to the relay.
  • Received and verify received envelopes

Types generation

protoc -I. --go_out=types types.proto

Examples

Please check the examples directory

Documentation

Overview

Package direct package provides the functionality to create a direct websocket connection to rmb relays without the need to rmb peers.

Index

Constants

View Source
const (
	KeyTypeEd25519 = "ed25519"
	KeyTypeSr25519 = "sr25519"
)
View Source
const (
	SignatureTypeEd25519 = "ed25519"
	SignatureTypeSr25519 = "sr25519"
)
View Source
const CustomSigning = "RMB"

Variables

View Source
var (
	// ErrFunctionNotFound is an err returned if the handler function is not found
	ErrFunctionNotFound = fmt.Errorf("function is not found")
)

Functions

func Challenge

func Challenge(env *types.Envelope) ([]byte, error)

func GetEnvelope

func GetEnvelope(ctx context.Context) *types.Envelope

GetEnvelope gets an envelope from the context, panics if it's not there

func GetTwinID

func GetTwinID(ctx context.Context) uint32

GetTwinID returns the twin id from context.

func Json

func Json(response *types.Envelope, callBackErr error) ([]byte, error)

Json extracts the json payload envelope and validate the schema

func NewJWT

func NewJWT(identity substrate.Identity, id uint32, session string, ttl uint32) (string, error)

func Sign

func Sign(signer substrate.Identity, input []byte) ([]byte, error)

func VerifySignature

func VerifySignature(twinDB TwinDB, env *types.Envelope) error

VerifySignature is responsible for verifying that the source produced this signature

Types

type Ed25519VerifyingKey

type Ed25519VerifyingKey []byte

func (Ed25519VerifyingKey) Verify

func (k Ed25519VerifyingKey) Verify(msg []byte, sig []byte) bool

type Handler

type Handler func(ctx context.Context, peer Peer, env *types.Envelope, err error)

Handler is a call back that is called with verified and decrypted incoming messages. An error can be non-nil error if verification or decryption failed

type HandlerFunc

type HandlerFunc func(ctx context.Context, payload []byte) (interface{}, error)

Handler is a handler function type

type InnerConnection

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

InnerConnection holds the required state to create a self healing websocket connection to the rmb relay.

func NewConnection

func NewConnection(identity substrate.Identity, url string, session string, twinID uint32) InnerConnection

NewConnection creates a new InnerConnection instance

func (*InnerConnection) Start

func (c *InnerConnection) Start(ctx context.Context) (Reader, Writer)

Start initiates the websocket connection

type Middleware

type Middleware func(ctx context.Context, payload []byte) (context.Context, error)

Middleware is middleware function type

type Peer

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

Peer exposes the functionality to talk directly to an rmb relay

func NewPeer

func NewPeer(
	ctx context.Context,
	mnemonics string,
	subManager substrate.Manager,
	handler Handler,
	opts ...PeerOpt) (*Peer, error)

NewPeer creates a new RMB peer client. It connects directly to the RMB-Relay, and tries to reconnect if the connection broke.

You can close the connection by canceling the passed context.

Make sure the context passed to Call() does not outlive the directClient's context. Call() will panic if called while the directClient's context is canceled.

func (*Peer) Encoder added in v0.11.10

func (p *Peer) Encoder() encoder.Encoder

Encoder returns the peer's encoder.

func (*Peer) SendRequest

func (d *Peer) SendRequest(ctx context.Context, id string, twin uint32, session *string, fn string, data interface{}) error

SendRequest sends an rmb message to the relay

func (*Peer) SendResponse

func (d *Peer) SendResponse(ctx context.Context, id string, twin uint32, session *string, responseError error, data interface{}) error

SendRequest sends an rmb message to the relay

type PeerOpt added in v0.11.10

type PeerOpt func(*peerCfg)

func WithEncoder added in v0.11.10

func WithEncoder(encoder encoder.Encoder) PeerOpt

WithEncoder sets encoding of the payload default is application/json

func WithEncryption added in v0.11.10

func WithEncryption(enable bool) PeerOpt

enable or disable encryption, default is enabled

func WithKeyType added in v0.11.10

func WithKeyType(keyType string) PeerOpt

WithKeyType set up the menmonic key type, default is Sr25519

func WithRelay added in v0.11.10

func WithRelay(url string) PeerOpt

WithRelay set up the relay url, default is mainnet relay

func WithSession added in v0.11.10

func WithSession(session string) PeerOpt

WithSession set a custom session name, default is the nil session

func WithTwinCache added in v0.14.4

func WithTwinCache(ttl uint64) PeerOpt

WithTwinCache cache twin information for this ttl number of seconds by default twins are cached in memory forever

type Reader

type Reader <-chan []byte

Reader is a channel that receives incoming messages

func (Reader) Read

func (r Reader) Read() []byte

type RmbSigner

type RmbSigner struct{}

func (*RmbSigner) Alg

func (s *RmbSigner) Alg() string

func (*RmbSigner) Sign

func (s *RmbSigner) Sign(signingString string, key interface{}) (string, error)

func (*RmbSigner) Verify

func (s *RmbSigner) Verify(signingString, signature string, key interface{}) error

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) Serve

func (r *Router) Serve(ctx context.Context, peer Peer, env *types.Envelope, err error)

func (*Router) SubRoute

func (r *Router) SubRoute(prefix string) *Router

SubRoute add a route prefix to include more sub routes with handler from it

func (*Router) Use

func (r *Router) Use(mw Middleware)

Use adds a middleware to the router

func (*Router) WithHandler

func (r *Router) WithHandler(subCommand string, handler HandlerFunc)

WithHandler adds a handler function to a router sub command

type RpcClient added in v0.13.11

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

RpcClient is a peer connection that makes it easy to make rpc calls

func NewRpcClient

func NewRpcClient(
	ctx context.Context,
	mnemonics string,
	subManager substrate.Manager,
	opts ...PeerOpt) (*RpcClient, error)

NewRpcClient create a new rpc client the rpc client is a full peer, but provide a custom handler to make it easy to make rpc calls

func (*RpcClient) Call added in v0.13.11

func (d *RpcClient) Call(ctx context.Context, twin uint32, fn string, data interface{}, result interface{}) error

func (*RpcClient) CallWithSession added in v0.13.11

func (d *RpcClient) CallWithSession(ctx context.Context, twin uint32, session *string, fn string, data interface{}, result interface{}) error

type Sr25519VerifyingKey

type Sr25519VerifyingKey []byte

func (Sr25519VerifyingKey) Verify

func (k Sr25519VerifyingKey) Verify(msg []byte, sig []byte) bool

type Twin

type Twin struct {
	ID        uint32
	PublicKey []byte
	Relay     *string
	E2EKey    []byte
}

Twin is used to store a twin id and its public key

type TwinDB

type TwinDB interface {
	Get(id uint32) (Twin, error)
	GetByPk(pk []byte) (uint32, error)
}

TwinDB is used to get Twin instances

func NewTwinDB

func NewTwinDB(subConn *substrate.Substrate) TwinDB

NewTwinDB creates a new twinDBImpl instance, with a non expiring cache.

type Verifier

type Verifier interface {
	Verify(msg []byte, sig []byte) bool
}

type Writer

type Writer chan<- []byte

Writer is a channel that sends outgoing messages

func (Writer) Write

func (w Writer) Write(data []byte)

Directories

Path Synopsis
examples
rpc

Jump to

Keyboard shortcuts

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