x402

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BlockhashValidityWindow is the conservative window for Solana blockhash validity.
	// Solana blockhashes are valid for ~150 slots (~60 seconds on mainnet).
	// We use 90 seconds as a conservative estimate.
	BlockhashValidityWindow = 90 * time.Second

	// RPCPollInterval is how frequently we poll RPC for transaction status when WebSocket fails.
	RPCPollInterval = 2 * time.Second

	// DefaultConfirmationTimeout is the maximum time to wait for transaction confirmation.
	DefaultConfirmationTimeout = 2 * time.Minute

	// DefaultAccessTTL is how long verified payments remain cached.
	DefaultAccessTTL = 45 * time.Minute
)

Transaction confirmation timeouts and intervals

View Source
const (
	// AmountTolerance is the epsilon used when comparing cryptocurrency amounts.
	// This accounts for floating point precision issues.
	AmountTolerance = 1e-9
)

Floating point tolerance for amount comparisons

Variables

This section is empty.

Functions

func GetUserFriendlyMessage

func GetUserFriendlyMessage(code errors.ErrorCode, err error) string

GetUserFriendlyMessage converts error codes to user-friendly messages.

Types

type PaymentPayload

type PaymentPayload struct {
	X402Version int    `json:"x402Version"`
	Scheme      string `json:"scheme"`
	Network     string `json:"network"`
	Payload     any    `json:"payload"` // scheme-dependent
}

PaymentPayload follows the x402 specification for the X-PAYMENT header. Reference: https://github.com/coinbase/x402

type PaymentProof

type PaymentProof struct {
	X402Version int
	Scheme      string
	Network     string
	Signature   string
	Payer       string
	Transaction string
	Memo        string
	Metadata    map[string]string

	// Resource identification (prevents resource ID leakage in URL paths)
	Resource     string // Resource ID from payment payload
	ResourceType string // "regular" | "cart" | "refund"

	// Solana-specific
	RecipientTokenAccount string
	FeePayer              string // Server wallet that pays fees (gasless mode)
}

PaymentProof is the internal representation after parsing and validation.

func ParsePaymentProof

func ParsePaymentProof(header string) (PaymentProof, error)

ParsePaymentProof decodes the X-PAYMENT header into a PaymentProof. Follows the x402 specification: https://github.com/coinbase/x402

type Requirement

type Requirement struct {
	ResourceID            string
	RecipientOwner        string
	RecipientTokenAccount string
	TokenMint             string
	Amount                float64
	Network               string
	TokenDecimals         uint8
	AllowedTokens         []string
	QuoteTTL              time.Duration
	SkipPreflight         bool
	Commitment            string
}

Requirement describes the verification constraints for a resource.

type SolanaPayload

type SolanaPayload struct {
	// Required fields
	Signature   string `json:"signature"`
	Transaction string `json:"transaction"`

	// Resource identification (prevents resource ID leakage in URL paths)
	Resource     string `json:"resource,omitempty"`     // Resource ID (product, cart, refund)
	ResourceType string `json:"resourceType,omitempty"` // "regular" | "cart" | "refund"

	// Optional Solana-specific extensions
	FeePayer              string            `json:"feePayer,omitempty"` // Server wallet (pays transaction fees in gasless mode)
	Memo                  string            `json:"memo,omitempty"`
	RecipientTokenAccount string            `json:"recipientTokenAccount,omitempty"` // SPL token account
	Metadata              map[string]string `json:"metadata,omitempty"`
}

SolanaPayload is the scheme-specific payload for Solana SPL transfers. Extends the x402 standard with Solana-specific fields.

type VerificationError

type VerificationError struct {
	Code    errors.ErrorCode // Machine-readable error code
	Message string           // User-friendly message
	Err     error            // Technical error for logging
}

VerificationError classifies failures encountered during transaction validation.

func NewVerificationError

func NewVerificationError(code errors.ErrorCode, err error) VerificationError

NewVerificationError creates a new verification error with a user-friendly message.

func (VerificationError) Error

func (e VerificationError) Error() string

func (VerificationError) Unwrap

func (e VerificationError) Unwrap() error

type VerificationResult

type VerificationResult struct {
	Wallet    string
	Amount    float64
	Signature string
	ExpiresAt time.Time
}

VerificationResult captures the verifier outcome.

type Verifier

type Verifier interface {
	Verify(ctx context.Context, proof PaymentProof, requirement Requirement) (VerificationResult, error)
}

Verifier validates incoming payments before the protected handler executes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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