server

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 16 Imported by: 1

Documentation

Overview

Package server is the server package for Paymail

Index

Constants

View Source
const (
	DefaultAPIVersion       = "v1"             // Version of API
	DefaultPrefix           = "https://"       // Paymail specs require SSL
	DefaultSenderValidation = false            // If true, it requires extra sender validation
	DefaultServerPort       = 3000             // Port for the server
	DefaultTimeout          = 15 * time.Second // Default timeouts
)

Server default values

View Source
const (
	ErrorFindingPaymail      = "error-finding-paymail"
	ErrorInvalidDt           = "invalid-dt"
	ErrorInvalidParameter    = "invalid-parameter"
	ErrorInvalidPubKey       = "invalid-pubkey"
	ErrorInvalidSenderHandle = "invalid-sender-handle"
	ErrorInvalidSignature    = "invalid-signature"
	ErrorMethodNotFound      = "method-405"
	ErrorMissingHex          = "missing-hex"
	ErrorMissingReference    = "missing-reference"
	ErrorMissingSatoshis     = "missing-satoshis"
	ErrorPaymailNotFound     = "not-found"
	ErrorRecordingTx         = "error-recording-tx"
	ErrorRequestNotFound     = "request-404"
	ErrorScript              = "script-error"
	ErrorUnknownDomain       = "unknown-domain"
)

Error codes for server response errors

Variables

View Source
var (
	// ErrDomainMissing is the error for missing domain
	ErrDomainMissing = errors.New("domain is missing")

	// ErrServiceProviderNil is the error for having a nil service provider
	ErrServiceProviderNil = errors.New("service provider is nil")

	// ErrPortMissing is when the port is not found
	ErrPortMissing = errors.New("missing a port")

	// ErrServiceNameMissing is when the service name is not found
	ErrServiceNameMissing = errors.New("missing service name")

	// ErrCapabilitiesMissing is when the capabilities struct is nil or not set
	ErrCapabilitiesMissing = errors.New("missing capabilities struct")

	// ErrBsvAliasMissing is when the bsv alias version is missing
	ErrBsvAliasMissing = errors.New("missing bsv alias version")
)

Functions

func CreateServer added in v0.4.2

func CreateServer(c *Configuration) *http.Server

CreateServer will create a basic Paymail Server

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, req *http.Request, code, message string, statusCode int)

ErrorResponse is a standard way to return errors to the client

Specs: http://bsvalias.org/99-01-recommendations.html

func GenerateServiceURL added in v0.4.0

func GenerateServiceURL(prefix, domain, apiVersion, serviceName string) string

GenerateServiceURL will create the service URL

func GenericCapabilities added in v0.5.0

func GenericCapabilities(bsvAliasVersion string, senderValidation bool) *paymail.CapabilitiesPayload

GenericCapabilities will make generic capabilities

func Handlers

func Handlers(configuration *Configuration) *nrhttprouter.Router

Handlers are used to isolate loading the routes (used for testing)

func P2PCapabilities added in v0.7.5

func P2PCapabilities(bsvAliasVersion string, senderValidation bool) *paymail.CapabilitiesPayload

P2PCapabilities will make generic capabilities & add additional p2p capabilities

func StartServer added in v0.4.2

func StartServer(srv *http.Server)

StartServer will run the Paymail server

Types

type ConfigOps added in v0.4.0

type ConfigOps func(c *Configuration)

ConfigOps allow functional options to be supplied that overwrite default options.

func WithBasicRoutes added in v0.4.0

func WithBasicRoutes() ConfigOps

WithBasicRoutes will turn on all the basic routes

func WithCapabilities added in v0.4.0

func WithCapabilities(capabilities *paymail.CapabilitiesPayload) ConfigOps

WithCapabilities will modify the capabilities

func WithDomain added in v0.4.0

func WithDomain(domain string) ConfigOps

WithDomain will add the domain if not found

func WithDomainValidationDisabled added in v0.7.4

func WithDomainValidationDisabled() ConfigOps

WithDomainValidationDisabled will disable checking domains (from request for allowed domains)

func WithGenericCapabilities added in v0.4.0

func WithGenericCapabilities() ConfigOps

WithGenericCapabilities will load the generic Paymail capabilities

func WithP2PCapabilities added in v0.7.5

func WithP2PCapabilities() ConfigOps

WithP2PCapabilities will load the generic & p2p capabilities

func WithPort added in v0.4.0

func WithPort(port int) ConfigOps

WithPort will overwrite the default port

func WithSenderValidation added in v0.4.0

func WithSenderValidation() ConfigOps

WithSenderValidation will enable sender validation

func WithServiceName added in v0.4.0

func WithServiceName(serviceName string) ConfigOps

WithServiceName will set a custom service name

func WithTimeout added in v0.4.0

func WithTimeout(timeout time.Duration) ConfigOps

WithTimeout will set a custom timeout

type Configuration added in v0.4.0

type Configuration struct {
	APIVersion                       string                       `json:"api_version"`
	BasicRoutes                      *basicRoutes                 `json:"basic_routes"`
	BSVAliasVersion                  string                       `json:"bsv_alias_version"`
	Capabilities                     *paymail.CapabilitiesPayload `json:"capabilities"`
	PaymailDomains                   []*Domain                    `json:"paymail_domains"`
	PaymailDomainsValidationDisabled bool                         `json:"paymail_domains_validation_disabled"`
	Port                             int                          `json:"port"`
	Prefix                           string                       `json:"prefix"`
	SenderValidationEnabled          bool                         `json:"sender_validation_enabled"`
	ServiceName                      string                       `json:"service_name"`
	Timeout                          time.Duration                `json:"timeout"`
	// contains filtered or unexported fields
}

Configuration paymail server configuration object

func NewConfig added in v0.4.0

func NewConfig(serviceProvider PaymailServiceProvider, opts ...ConfigOps) (*Configuration, error)

NewConfig will make a new server configuration

func (*Configuration) AddDomain added in v0.4.0

func (c *Configuration) AddDomain(domain string) (err error)

AddDomain will add the domain if it does not exist

func (*Configuration) EnrichCapabilities added in v0.4.0

func (c *Configuration) EnrichCapabilities(domain string) *paymail.CapabilitiesPayload

EnrichCapabilities will update the capabilities with the appropriate service url

func (*Configuration) IsAllowedDomain added in v0.4.0

func (c *Configuration) IsAllowedDomain(domain string) (success bool)

IsAllowedDomain will return true if it's an allowed paymail domain

func (*Configuration) RegisterBasicRoutes added in v0.4.0

func (c *Configuration) RegisterBasicRoutes(r *apirouter.Router)

RegisterBasicRoutes register the basic routes to the http router

func (*Configuration) RegisterRoutes added in v0.4.0

func (c *Configuration) RegisterRoutes(r *apirouter.Router)

RegisterRoutes register all the available paymail routes to the http router

func (*Configuration) Validate added in v0.4.0

func (c *Configuration) Validate() error

Validate will check that the configuration meets a minimum requirement to run the server

type Domain added in v0.4.0

type Domain struct {
	Name string `json:"name"`
}

Domain is the Paymail Domain information

type PaymailServiceProvider added in v0.4.0

type PaymailServiceProvider interface {
	CreateAddressResolutionResponse(
		ctx context.Context,
		alias, domain string,
		senderValidation bool,
		metaData *RequestMetadata,
	) (*paymail.ResolutionPayload, error)

	CreateP2PDestinationResponse(
		ctx context.Context,
		alias, domain string,
		satoshis uint64,
		metaData *RequestMetadata,
	) (*paymail.PaymentDestinationPayload, error)

	GetPaymailByAlias(
		ctx context.Context,
		alias, domain string,
		metaData *RequestMetadata,
	) (*paymail.AddressInformation, error)

	RecordTransaction(
		ctx context.Context,
		p2pTx *paymail.P2PTransaction,
		metaData *RequestMetadata,
	) (*paymail.P2PTransactionPayload, error)
}

PaymailServiceProvider the paymail server interface that needs to be implemented

type RequestMetadata added in v0.4.2

type RequestMetadata struct {
	Alias              string                  `json:"alias,omitempty"`               // Alias of the paymail
	Domain             string                  `json:"domain,omitempty"`              // Domain of the request
	IPAddress          string                  `json:"ip_address,omitempty"`          // IP address of the requesting user
	Note               string                  `json:"note,omitempty"`                // Generic note field used for extra information
	PaymentDestination *paymail.PaymentRequest `json:"payment_destination,omitempty"` // Information from the P2P Payment Destination request
	RequestURI         string                  `json:"request_uri,omitempty"`         // Full requesting URL path
	ResolveAddress     *paymail.SenderRequest  `json:"resolve_address,omitempty"`     // Information from the Resolve Address request
	UserAgent          string                  `json:"user_agent,omitempty"`          // User agent of the requesting user
}

RequestMetadata is the struct with extra metadata

func CreateMetadata added in v0.4.2

func CreateMetadata(req *http.Request, alias, domain, optionalNote string) *RequestMetadata

CreateMetadata will create the base metadata using the request

Jump to

Keyboard shortcuts

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