validation

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: Unlicense Imports: 5 Imported by: 0

Documentation

Overview

Package validation provides event validation services for the ORLY relay. It handles structural validation (hex case, JSON format), cryptographic validation (signature, ID), and protocol validation (timestamp, NIP-70).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasProtectedTag

func HasProtectedTag(ev *event.E) bool

HasProtectedTag checks if an event has the NIP-70 protected tag.

func ValidateLowercaseHexInJSON

func ValidateLowercaseHexInJSON(msg []byte) string

ValidateLowercaseHexInJSON checks that all hex-encoded fields in the raw JSON are lowercase. NIP-01 specifies that hex encoding must be lowercase. This must be called on the raw message BEFORE unmarshaling, since unmarshal converts hex strings to binary and loses case information. Returns an error message if validation fails, or empty string if valid.

Types

type Config

type Config struct {
	// MaxFutureSeconds is how far in the future a timestamp can be (default: 3600 = 1 hour)
	MaxFutureSeconds int64
}

Config holds configuration for the validation service.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default validation configuration.

type ReasonCode

type ReasonCode int

ReasonCode identifies the type of validation failure for response formatting.

const (
	ReasonNone ReasonCode = iota
	ReasonBlocked
	ReasonInvalid
	ReasonError
)

type Result

type Result struct {
	Valid bool
	Code  ReasonCode // For response formatting
	Msg   string     // Human-readable error message
}

Result contains the outcome of a validation check.

func Blocked

func Blocked(msg string) Result

Blocked returns a blocked validation result.

func Error

func Error(msg string) Result

Error returns an error validation result.

func Invalid

func Invalid(msg string) Result

Invalid returns an invalid validation result.

func OK

func OK() Result

OK returns a successful validation result.

func ValidateEventID

func ValidateEventID(ev *event.E) Result

ValidateEventID checks that the event ID matches the computed hash.

func ValidateProtectedTagMatch

func ValidateProtectedTagMatch(ev *event.E, authedPubkey []byte) Result

ValidateProtectedTagMatch checks NIP-70 protected tag requirements. Events with the "-" tag can only be published by users authenticated with the same pubkey as the event author.

func ValidateSignature

func ValidateSignature(ev *event.E) Result

ValidateSignature verifies the event signature.

func ValidateTimestamp

func ValidateTimestamp(ev *event.E, maxFutureSeconds int64) Result

ValidateTimestamp checks that the event timestamp is not too far in the future. maxFutureSeconds is the maximum allowed seconds ahead of current time.

type Service

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

Service implements the Validator interface.

func New

func New() *Service

New creates a new validation service with default configuration.

func NewWithConfig

func NewWithConfig(cfg *Config) *Service

NewWithConfig creates a new validation service with the given configuration.

func (*Service) ValidateEvent

func (s *Service) ValidateEvent(ev *event.E) Result

ValidateEvent validates an unmarshaled event.

func (*Service) ValidateProtectedTag

func (s *Service) ValidateProtectedTag(ev *event.E, authedPubkey []byte) Result

ValidateProtectedTag checks NIP-70 protected tag requirements.

func (*Service) ValidateRawJSON

func (s *Service) ValidateRawJSON(msg []byte) Result

ValidateRawJSON validates raw message before unmarshaling.

type Validator

type Validator interface {
	// ValidateRawJSON validates raw message before unmarshaling.
	// This catches issues like uppercase hex that are lost after unmarshal.
	ValidateRawJSON(msg []byte) Result

	// ValidateEvent validates an unmarshaled event.
	// Checks ID computation, signature, and timestamp.
	ValidateEvent(ev *event.E) Result

	// ValidateProtectedTag checks NIP-70 protected tag requirements.
	// The authedPubkey is the authenticated pubkey of the connection.
	ValidateProtectedTag(ev *event.E, authedPubkey []byte) Result
}

Validator validates events before processing.

Source Files

  • hex.go
  • protected.go
  • signature.go
  • timestamp.go
  • validation.go

Jump to

Keyboard shortcuts

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