mediocre-blog/srv/api/pow.go

54 lines
1.2 KiB
Go
Raw Normal View History

package api
2021-08-01 23:54:53 +00:00
import (
"encoding/hex"
"errors"
"fmt"
2021-08-01 23:54:53 +00:00
"net/http"
"github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutils"
2021-08-01 23:54:53 +00:00
)
func (a *api) newPowChallengeHandler() http.Handler {
2021-08-01 23:54:53 +00:00
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
challenge := a.params.PowManager.NewChallenge()
2021-08-01 23:54:53 +00:00
apiutils.JSONResult(rw, r, struct {
2021-08-01 23:54:53 +00:00
Seed string `json:"seed"`
Target uint32 `json:"target"`
}{
Seed: hex.EncodeToString(challenge.Seed),
Target: challenge.Target,
})
})
}
func (a *api) requirePowMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
seedHex := r.FormValue("powSeed")
seed, err := hex.DecodeString(seedHex)
if err != nil || len(seed) == 0 {
apiutils.BadRequest(rw, r, errors.New("invalid powSeed"))
return
}
solutionHex := r.FormValue("powSolution")
solution, err := hex.DecodeString(solutionHex)
if err != nil || len(seed) == 0 {
apiutils.BadRequest(rw, r, errors.New("invalid powSolution"))
return
}
err = a.params.PowManager.CheckSolution(seed, solution)
if err != nil {
apiutils.BadRequest(rw, r, fmt.Errorf("checking proof-of-work solution: %w", err))
return
}
h.ServeHTTP(rw, r)
})
}