pow

package
v0.0.0-...-b14b177 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pow contains the PoW service, that handles the logic of proof-of-work for the application. It currently uses hashcash v1, but can be extended to support other PoW systems. It stores the challenges in a store, that can be in memory, Redis, or any other storage.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChallengeNotFound is returned when the challenge is not found in the store.
	ErrChallengeNotFound = errors.New("challenge not found")

	// ErrChallengeKeyEmpty is returned when the challenge key is empty.
	// This should never happen and could lead to incorrect storage of challenges.
	ErrChallengeKeyEmpty = errors.New("challenge key is empty")

	// ErrChallengeDifficultyInvalid is returned when the challenge difficulty is invalid.
	ErrChallengeDifficultyInvalid = errors.New("challenge difficulty is invalid")

	// ErrChallengeSaltLengthInvalid is returned when the challenge salt length is invalid.
	ErrChallengeSaltLengthInvalid = errors.New("challenge salt length is invalid")
)

Functions

This section is empty.

Types

type ChallengeKey

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

ChallengeKey is a key for a challenge.

func NewChallengeKey

func NewChallengeKey(clientID string, resourceID string) *ChallengeKey

NewChallengeKey returns a new challenge key.

func (*ChallengeKey) ClientID

func (c *ChallengeKey) ClientID() string

ClientID returns the client ID.

func (*ChallengeKey) ResourceID

func (c *ChallengeKey) ResourceID() string

ResourceID returns the resource ID.

func (*ChallengeKey) String

func (c *ChallengeKey) String() string

String returns a string representation of the challenge key.

type ChallengeStore

type ChallengeStore interface {
	// Add adds a challenge to the store.
	Add(ctx context.Context, key, value string) error
	// Get checks if a challenge is in the store.
	Get(ctx context.Context, key string) (bool, error)
	// Delete deletes a challenge from the store.
	Delete(ctx context.Context, key string) error
}

ChallengeStore is an interface for storing which challenges are currently active.

type Key

type Key interface {
	String() string
	ClientID() string
	ResourceID() string
}

Key is a key for uniquely identifying a challenge.

type Service

type Service struct {

	// ChallengeStore is a store for challenges.
	Store ChallengeStore
	// contains filtered or unexported fields
}

Service is a PoW service.

func NewService

func NewService(logger *zap.Logger, store ChallengeStore) *Service

NewService creates a new PoW service.

func (*Service) CheckSolution

func (s *Service) CheckSolution(ctx context.Context, solution string, key Key) (bool, error)

CheckSolution checks if the solution with the given challenge key exists in the store and if it is correct.

func (*Service) NewChallenge

func (s *Service) NewChallenge(ctx context.Context, key Key, difficulty, saltLength int) (string, error)

NewChallenge generates a new challenge, saves it to the store and returns it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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