transport

package
v0.7.13 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package transport implements DIDComm v2.1 message transports.

This package provides implementations for sending and receiving DIDComm messages over various protocols:

  • HTTP: RESTful endpoint for DIDComm messages
  • WebSocket: Bidirectional persistent connections

HTTP Transport

The HTTP transport implements DIDComm over HTTP per the specification. Messages are sent as POST requests with appropriate content types.

client := transport.NewHTTPClient()
response, err := client.Send(ctx, transport.SendRequest{
	Endpoint:  "https://example.com/didcomm",
	Message:   packedMessage,
	MediaType: "application/didcomm-encrypted+json",
})

Server Handler

For receiving messages, use the HTTP handler:

handler := transport.NewHTTPHandler(processor)
http.Handle("/didcomm", handler)

Media Types

The transport layer handles content-type negotiation:

application/didcomm-plain+json    - Plaintext messages
application/didcomm-signed+json   - Signed messages
application/didcomm-encrypted+json - Encrypted messages

Index

Constants

View Source
const (
	// DIDCommSubprotocol is the WebSocket subprotocol for DIDComm v2
	DIDCommSubprotocol = "didcomm/v2"
)

Variables

View Source
var (
	// ErrNoEndpoint indicates no endpoint is available.
	ErrNoEndpoint = errors.New("didcomm/transport: no endpoint available")

	// ErrConnectionFailed indicates a connection failure.
	ErrConnectionFailed = errors.New("didcomm/transport: connection failed")

	// ErrSendFailed indicates a send operation failure.
	ErrSendFailed = errors.New("didcomm/transport: send failed")

	// ErrReceiveFailed indicates a receive operation failure.
	ErrReceiveFailed = errors.New("didcomm/transport: receive failed")

	// ErrInvalidContentType indicates an unsupported content type.
	ErrInvalidContentType = errors.New("didcomm/transport: invalid content type")

	// ErrTimeout indicates a timeout.
	ErrTimeout = errors.New("didcomm/transport: timeout")

	// ErrConnectionClosed indicates the connection was closed.
	ErrConnectionClosed = errors.New("didcomm/transport: connection closed")
)

Common transport errors

Functions

This section is empty.

Types

type HTTPClient

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

HTTPClient sends DIDComm messages over HTTP.

func NewHTTPClient

func NewHTTPClient(opts ...HTTPClientOption) *HTTPClient

NewHTTPClient creates a new HTTP transport client.

func (*HTTPClient) Send

func (c *HTTPClient) Send(ctx context.Context, req SendRequest) (*SendResponse, error)

Send sends a DIDComm message to the specified endpoint.

func (*HTTPClient) SendMessage

func (c *HTTPClient) SendMessage(ctx context.Context, endpoint string, message []byte, mediaType string) ([]byte, error)

SendMessage is a convenience method that sends a message and returns just the response body.

type HTTPClientOption

type HTTPClientOption func(*HTTPClient)

HTTPClientOption configures the HTTP client.

func WithHTTPClient

func WithHTTPClient(client *http.Client) HTTPClientOption

WithHTTPClient sets the underlying HTTP client.

func WithTimeout

func WithTimeout(timeout time.Duration) HTTPClientOption

WithTimeout sets the request timeout.

func WithUserAgent

func WithUserAgent(userAgent string) HTTPClientOption

WithUserAgent sets the User-Agent header.

type HTTPHandler

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

HTTPHandler handles incoming DIDComm messages over HTTP.

func NewHTTPHandler

func NewHTTPHandler(processor MessageProcessor, opts ...HTTPHandlerOption) *HTTPHandler

NewHTTPHandler creates a new HTTP handler for receiving DIDComm messages.

func (*HTTPHandler) ServeHTTP

func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type HTTPHandlerOption

type HTTPHandlerOption func(*HTTPHandler)

HTTPHandlerOption configures the HTTP handler.

func WithAllowedContentTypes

func WithAllowedContentTypes(types ...string) HTTPHandlerOption

WithAllowedContentTypes sets the allowed content types.

type MessageProcessor

type MessageProcessor interface {
	// ProcessMessage processes an incoming message and optionally returns a response.
	ProcessMessage(ctx context.Context, message []byte, mediaType string) (response []byte, responseMediaType string, err error)
}

MessageProcessor processes incoming DIDComm messages.

type SendRequest

type SendRequest struct {
	// Endpoint is the target URL.
	Endpoint string

	// Message is the packed message bytes.
	Message []byte

	// MediaType is the content type of the message.
	MediaType string

	// ExpectReturn indicates whether a return route is expected.
	ExpectReturn bool
}

SendRequest represents a DIDComm send request.

type SendResponse

type SendResponse struct {
	// StatusCode is the HTTP status code.
	StatusCode int

	// Body is the response body (if any).
	Body []byte

	// MediaType is the content type of the response.
	MediaType string
}

SendResponse represents the response from sending a message.

type WebSocketClient

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

WebSocketClient provides bidirectional DIDComm messaging over WebSocket.

func NewWebSocketClient

func NewWebSocketClient(processor MessageProcessor, opts ...WebSocketOption) *WebSocketClient

NewWebSocketClient creates a new WebSocket client with optional message processor. The processor is called for incoming messages; if nil, messages must be read manually via Receive().

func (*WebSocketClient) Close

func (c *WebSocketClient) Close() error

Close closes the WebSocket connection.

func (*WebSocketClient) Connect

func (c *WebSocketClient) Connect(ctx context.Context, endpoint string) error

Connect establishes a WebSocket connection to the given endpoint. The endpoint should be a ws:// or wss:// URL.

func (*WebSocketClient) IsConnected

func (c *WebSocketClient) IsConnected() bool

IsConnected returns whether the client is connected.

func (*WebSocketClient) Receive

func (c *WebSocketClient) Receive(ctx context.Context) ([]byte, string, error)

Receive reads a single message from the WebSocket connection. This is for manual message handling when no processor is set.

func (*WebSocketClient) Send

func (c *WebSocketClient) Send(ctx context.Context, message []byte, mediaType string) error

Send sends a DIDComm message over the WebSocket connection.

func (*WebSocketClient) SendWithEnvelope

func (c *WebSocketClient) SendWithEnvelope(ctx context.Context, message []byte, mediaType string) error

SendWithEnvelope sends a DIDComm message wrapped in a JSON envelope with media type. Use this when the receiver needs explicit media type information.

type WebSocketHandler

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

WebSocketHandler handles incoming WebSocket connections for DIDComm. It upgrades HTTP connections and processes DIDComm messages bidirectionally.

func NewWebSocketHandler

func NewWebSocketHandler(processor MessageProcessor, opts ...WebSocketHandlerOption) *WebSocketHandler

NewWebSocketHandler creates a new WebSocket handler for receiving DIDComm messages.

func (*WebSocketHandler) ServeHTTP

func (h *WebSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler and upgrades HTTP connections to WebSocket.

type WebSocketHandlerOption

type WebSocketHandlerOption func(*WebSocketHandler)

WebSocketHandlerOption configures the WebSocket handler.

func WithAllowedOrigins

func WithAllowedOrigins(origins []string) WebSocketHandlerOption

WithAllowedOrigins sets allowed WebSocket origins for CORS.

func WithHandlerMaxMessageSize

func WithHandlerMaxMessageSize(size int64) WebSocketHandlerOption

WithHandlerMaxMessageSize sets the maximum message size for the handler.

type WebSocketOption

type WebSocketOption func(*WebSocketClient)

WebSocketOption configures the WebSocket client.

func WithMaxMessageSize

func WithMaxMessageSize(size int64) WebSocketOption

WithMaxMessageSize sets the maximum message size in bytes.

func WithPingInterval

func WithPingInterval(d time.Duration) WebSocketOption

WithPingInterval sets the WebSocket ping interval for keepalive.

func WithReadTimeout

func WithReadTimeout(d time.Duration) WebSocketOption

WithReadTimeout sets the read timeout for receiving messages.

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) WebSocketOption

WithWriteTimeout sets the write timeout for sending messages.

Jump to

Keyboard shortcuts

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