p2p

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const ProtocolID = "/betar/marketplace/1.0.0"

ProtocolID is the marketplace protocol identifier

View Source
const X402ProtocolID = "/x402/libp2p/1.0.0"

X402ProtocolID is the libp2p protocol identifier for the x402-over-libp2p standard.

Variables

This section is empty.

Functions

func ReadMessage

func ReadMessage(r io.Reader) (string, []byte, error)

ReadMessage reads a message from a stream using msgio

func WriteMessage

func WriteMessage(w io.Writer, msgType string, data []byte) error

WriteMessage writes a message to a stream using msgio

Types

type Discovery

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

Discovery handles peer discovery

func NewDiscovery

func NewDiscovery(ctx context.Context, h host.Host, cfg *config.P2PConfig) (*Discovery, error)

NewDiscovery creates a new discovery service

func (*Discovery) Close

func (d *Discovery) Close() error

Close closes the discovery service

func (*Discovery) DiscoverPeers

func (d *Discovery) DiscoverPeers(ctx context.Context, bootstrapPeers []string) error

DiscoverPeers discovers peers on the network

func (*Discovery) FindPeers

func (d *Discovery) FindPeers(ctx context.Context, service string) []peer.AddrInfo

FindPeers finds peers providing a specific service

func (*Discovery) Routing

func (d *Discovery) Routing() corerouting.Routing

Routing returns the routing implementation used by discovery.

type Host

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

Host wraps the libp2p host

func NewHost

func NewHost(ctx context.Context, cfg *config.P2PConfig) (*Host, error)

NewHost creates a new libp2p host

func (*Host) AddrStrings

func (h *Host) AddrStrings() []string

AddrStrings returns the listen addresses as strings

func (*Host) Addrs

func (h *Host) Addrs() []multiaddr.Multiaddr

Addrs returns the listen addresses

func (*Host) Close

func (h *Host) Close() error

Close closes the host

func (*Host) Connect

func (h *Host) Connect(ctx context.Context, pi peer.AddrInfo) error

Connect connects to a peer

func (*Host) Disconnect

func (h *Host) Disconnect(p peer.ID) error

Disconnect disconnects from a peer

func (*Host) ID

func (h *Host) ID() peer.ID

ID returns the peer ID

func (*Host) NewStream

func (h *Host) NewStream(ctx context.Context, p peer.ID, protocols ...string) error

NewStream creates a new stream to a peer

func (*Host) Peerstore

func (h *Host) Peerstore() peerstore.Peerstore

Peerstore returns the peer store

func (*Host) RawHost

func (h *Host) RawHost() host.Host

RawHost returns the underlying libp2p host

type MessageHandler

type MessageHandler func(ctx context.Context, from peer.ID, data []byte) ([]byte, error)

MessageHandler handles incoming messages

type PubSub

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

PubSub handles pubsub messaging

func NewPubSub

func NewPubSub(ctx context.Context, h host.Host) (*PubSub, error)

NewPubSub creates a new pubsub instance

func (*PubSub) Close

func (ps *PubSub) Close() error

Close closes the pubsub

func (*PubSub) GetTopics

func (ps *PubSub) GetTopics() []string

GetTopics returns list of subscribed topics

func (*PubSub) JoinTopic

func (ps *PubSub) JoinTopic(ctx context.Context, topic string) (*pubsub.Topic, error)

JoinTopic joins a topic

func (*PubSub) ListPeers

func (ps *PubSub) ListPeers(topic string) []peer.ID

ListPeers returns peers subscribed to a topic

func (*PubSub) Publish

func (ps *PubSub) Publish(ctx context.Context, topic string, data []byte) error

Publish publishes a message to a topic

func (*PubSub) Raw

func (ps *PubSub) Raw() *pubsub.PubSub

Raw returns the underlying libp2p pubsub instance.

func (*PubSub) RegisterTopicValidator

func (ps *PubSub) RegisterTopicValidator(topic string, val func(context.Context, peer.ID, *pubsub.Message) pubsub.ValidationResult) error

RegisterTopicValidator registers a validator for a topic

func (*PubSub) Subscribe

func (ps *PubSub) Subscribe(ctx context.Context, topic string) (*pubsub.Subscription, error)

Subscribe subscribes to a topic

type StreamHandler

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

StreamHandler handles direct P2P streams

func NewStreamHandler

func NewStreamHandler(h host.Host) *StreamHandler

NewStreamHandler creates a new stream handler

func (*StreamHandler) RegisterHandler

func (s *StreamHandler) RegisterHandler(msgType string, handler MessageHandler)

RegisterHandler registers a message handler

func (*StreamHandler) SendMessage

func (s *StreamHandler) SendMessage(ctx context.Context, to peer.ID, msgType string, data []byte) ([]byte, error)

SendMessage sends a message to a peer via stream

type X402MessageHandler

type X402MessageHandler func(ctx context.Context, from peer.ID, msgType string, data []byte) (string, []byte, error)

X402MessageHandler handles typed x402 messages. It returns (responseType, responseBytes, error). The error causes an x402.error to be sent; normal typed responses set responseType to the desired message type (e.g., x402.response).

type X402StreamHandler

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

X402StreamHandler manages streams using the /x402/libp2p/1.0.0 protocol. Unlike the existing StreamHandler, both requests and responses carry a type field, enabling a single stream to exchange multiple distinct message types.

func NewX402StreamHandler

func NewX402StreamHandler(h host.Host) *X402StreamHandler

NewX402StreamHandler creates and registers the x402 stream handler on the given host.

func (*X402StreamHandler) RegisterHandler

func (s *X402StreamHandler) RegisterHandler(msgType string, fn X402MessageHandler)

RegisterHandler registers a handler for the given message type.

func (*X402StreamHandler) SendX402Message

func (s *X402StreamHandler) SendX402Message(ctx context.Context, to peer.ID, msgType string, payload []byte) (string, []byte, error)

SendX402Message opens a new stream to the target peer, writes a typed frame, reads the typed response frame, and returns (responseType, responseData, error).

Jump to

Keyboard shortcuts

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