message

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package message provides DIDComm v2.1 message types and operations.

This package implements the core message structures defined in the DIDComm Messaging v2.1 specification:

  • Plaintext messages (Section 3)
  • Attachments (Section 3.4)
  • Message validation

Message Types

DIDComm messages are JSON objects with specific required and optional fields. The base Message type represents a plaintext message that can be:

  • Sent as-is (unprotected, for testing only)
  • Signed to create a SignedMessage (JWS)
  • Encrypted to create an EncryptedMessage (JWE)

Creating Messages

msg := message.New(
    message.WithType("https://example.com/protocols/1.0/hello"),
    message.WithTo("did:example:bob"),
    message.WithFrom("did:example:alice"),
    message.WithBody(map[string]any{"greeting": "Hello, Bob!"}),
)

Attachments

Messages can include attachments with various data formats:

msg.AddAttachment(message.Attachment{
    ID:          "doc-1",
    MediaType:   "application/pdf",
    Data:        message.AttachmentData{Base64: encodedPDF},
})

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMessage    = errors.New("didcomm: invalid message")
	ErrMissingID         = errors.New("didcomm: message missing id")
	ErrMissingType       = errors.New("didcomm: message missing type")
	ErrMessageExpired    = errors.New("didcomm: message expired")
	ErrInvalidAttachment = errors.New("didcomm: invalid attachment")
)

Sentinel errors for message operations.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	// ID is a unique identifier for the attachment within the message.
	// REQUIRED when attachments are referenced by other parts of the message.
	ID string `json:"id,omitempty"`

	// Description is a human-readable description. OPTIONAL.
	Description string `json:"description,omitempty"`

	// Filename is a hint about the filename. OPTIONAL.
	Filename string `json:"filename,omitempty"`

	// MediaType is the MIME type of the attachment content. OPTIONAL.
	MediaType string `json:"media_type,omitempty"`

	// Format describes the format of the attachment if not evident from media_type. OPTIONAL.
	Format string `json:"format,omitempty"`

	// LastModTime is the last modification time. OPTIONAL.
	// Unix timestamp in seconds.
	LastModTime *int64 `json:"lastmod_time,omitempty"`

	// ByteCount is the size in bytes. OPTIONAL.
	ByteCount *int64 `json:"byte_count,omitempty"`

	// Data contains the attachment data. REQUIRED.
	Data AttachmentData `json:"data"`
}

Attachment represents a DIDComm message attachment. Per DIDComm spec Section 3.4.

func NewBase64Attachment

func NewBase64Attachment(id string, mediaType string, data []byte) Attachment

NewBase64Attachment creates an attachment with base64url-encoded data (no padding).

func NewJSONAttachment

func NewJSONAttachment(id string, data any) Attachment

NewJSONAttachment creates an attachment with embedded JSON data.

func NewLinksAttachment

func NewLinksAttachment(id string, mediaType string, links []string, hash string) Attachment

NewLinksAttachment creates an attachment with external links.

func (*Attachment) DecodeBase64

func (a *Attachment) DecodeBase64() ([]byte, error)

DecodeBase64 decodes base64-encoded attachment data. Returns an error if the attachment doesn't have base64 data.

func (*Attachment) GetBytes

func (a *Attachment) GetBytes() ([]byte, error)

GetBytes returns the attachment content as bytes. Works for Base64 and JSON encoded content.

func (*Attachment) GetJSON

func (a *Attachment) GetJSON(v any) error

GetJSON returns the attachment content as a JSON-decoded value. If the content is Base64-encoded JSON, it will be decoded.

func (*Attachment) Validate

func (a *Attachment) Validate() error

Validate checks that the attachment is well-formed.

type AttachmentData

type AttachmentData struct {
	// JWS is a JSON Web Signature wrapping the content.
	// Provides integrity and optional authentication.
	JWS json.RawMessage `json:"jws,omitempty"`

	// Hash is a multihash of the content for integrity checking.
	// Used when content is retrieved via Links.
	Hash string `json:"hash,omitempty"`

	// Links is a list of URIs where the content can be fetched.
	Links []string `json:"links,omitempty"`

	// Base64 is base64url-encoded content.
	Base64 string `json:"base64,omitempty"`

	// JSON is embedded JSON content.
	JSON any `json:"json,omitempty"`
}

AttachmentData represents the actual attachment content. At least one of JWS, Links, Base64, or JSON should be set. Multiple formats are allowed per DIDComm spec. Additional validation rules are enforced by AttachmentData.Validate.

func (*AttachmentData) Validate

func (d *AttachmentData) Validate() error

Validate checks that at least one data format is specified. DIDComm allows multiple formats, so we only check for minimum presence.

type Message

type Message struct {
	// ID is a unique identifier for the message. REQUIRED.
	// MUST be unique to the sender.
	ID string `json:"id"`

	// Type is the message type URI. REQUIRED.
	// Defines the protocol and message semantics.
	Type string `json:"type"`

	// From is the sender's DID. OPTIONAL for anonymous messages.
	// When present, the message can be authenticated.
	From string `json:"from,omitempty"`

	// To is the list of recipient DIDs. OPTIONAL but typically present.
	// Messages may omit To when the recipient is implicit.
	To []string `json:"to,omitempty"`

	// ThID is the thread identifier. OPTIONAL.
	// Groups related messages together.
	ThID string `json:"thid,omitempty"`

	// PThID is the parent thread identifier. OPTIONAL.
	// Links a thread to a parent thread.
	PThID string `json:"pthid,omitempty"`

	// CreatedTime is when the message was created. OPTIONAL.
	// Unix timestamp in seconds.
	CreatedTime *int64 `json:"created_time,omitempty"`

	// ExpiresTime is when the message expires. OPTIONAL.
	// Unix timestamp in seconds.
	ExpiresTime *int64 `json:"expires_time,omitempty"`

	// Body is the message-type-specific content. OPTIONAL.
	// Structure depends on the message type.
	Body any `json:"body,omitempty"`

	// Attachments is a list of attachments. OPTIONAL.
	Attachments []Attachment `json:"attachments,omitempty"`

	// FromPrior is a JWT for sender key rotation. OPTIONAL.
	// Proves continuity when the sender's DID or keys change.
	FromPrior string `json:"from_prior,omitempty"`

	// PleaseAck requests acknowledgment of this message. OPTIONAL.
	// Contains a list of message IDs that should trigger an ack.
	PleaseAck []string `json:"please_ack,omitempty"`

	// Ack acknowledges receipt of previous messages. OPTIONAL.
	// Contains message IDs being acknowledged.
	Ack []string `json:"ack,omitempty"`

	// CustomHeaders contains additional headers not in the spec.
	// These are serialized at the top level of the message JSON.
	CustomHeaders map[string]any `json:"-"`
}

Message represents a DIDComm v2.1 plaintext message. Per DIDComm spec Section 3.

func New

func New(opts ...Option) *Message

New creates a new Message with a generated UUID and applies the given options.

func Parse

func Parse(data []byte) (*Message, error)

Parse parses a JSON-encoded plaintext message.

func (*Message) AddAttachment

func (m *Message) AddAttachment(a Attachment)

AddAttachment adds an attachment to the message.

func (*Message) GetBody

func (m *Message) GetBody(v any) error

GetBody unmarshals the message body into the given struct.

func (*Message) IsExpired

func (m *Message) IsExpired() bool

IsExpired returns true if the message has expired.

func (*Message) MarshalJSON

func (m *Message) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler with custom header support.

func (*Message) Reply

func (m *Message) Reply(typ string, body any) *Message

Reply creates a new message that is a reply to this message. The thread ID is preserved (or set from the original message ID).

func (*Message) SetBody

func (m *Message) SetBody(body any) error

SetBody sets the message body from a struct.

func (*Message) ThreadID

func (m *Message) ThreadID() string

ThreadID returns the effective thread ID. If thid is set, returns it; otherwise returns the message id.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler with custom header support.

func (*Message) Validate

func (m *Message) Validate() error

Validate checks that the message meets DIDComm requirements.

type Option

type Option func(*Message)

Option is a functional option for creating messages.

func WithAttachments

func WithAttachments(attachments []Attachment) Option

WithAttachments sets the message attachments.

func WithBody

func WithBody(body any) Option

WithBody sets the message body.

func WithCreatedTime

func WithCreatedTime(t time.Time) Option

WithCreatedTime sets the creation timestamp.

func WithExpiresTime

func WithExpiresTime(t time.Time) Option

WithExpiresTime sets the expiration timestamp.

func WithFrom

func WithFrom(from string) Option

WithFrom sets the sender DID.

func WithFromPrior

func WithFromPrior(jwt string) Option

WithFromPrior sets the from_prior JWT.

func WithID

func WithID(id string) Option

WithID sets the message ID.

func WithParentThreadID

func WithParentThreadID(pthid string) Option

WithParentThreadID sets the parent thread identifier.

func WithThreadID

func WithThreadID(thid string) Option

WithThreadID sets the thread identifier.

func WithTo

func WithTo(to ...string) Option

WithTo sets the recipient DIDs.

func WithType

func WithType(typ string) Option

WithType sets the message type URI.

Jump to

Keyboard shortcuts

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