summaryrefslogtreecommitdiff
path: root/srv/src/http/pow.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-20 11:17:31 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-20 11:17:31 -0600
commit09acb111a2b22f5794541fac175b024dd0f9100e (patch)
tree11d4578a42ad4aea968b42a2689f64c799f9176e /srv/src/http/pow.go
parentf69ed83de73bbfc4b7af0931de6ced8cf12dea61 (diff)
Rename api package to http
Diffstat (limited to 'srv/src/http/pow.go')
-rw-r--r--srv/src/http/pow.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/srv/src/http/pow.go b/srv/src/http/pow.go
new file mode 100644
index 0000000..1bd5cb5
--- /dev/null
+++ b/srv/src/http/pow.go
@@ -0,0 +1,53 @@
+package http
+
+import (
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "net/http"
+
+ "github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
+)
+
+func (a *api) newPowChallengeHandler() http.Handler {
+ return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+
+ challenge := a.params.PowManager.NewChallenge()
+
+ apiutil.JSONResult(rw, r, struct {
+ 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 {
+ apiutil.BadRequest(rw, r, errors.New("invalid powSeed"))
+ return
+ }
+
+ solutionHex := r.FormValue("powSolution")
+ solution, err := hex.DecodeString(solutionHex)
+ if err != nil || len(seed) == 0 {
+ apiutil.BadRequest(rw, r, errors.New("invalid powSolution"))
+ return
+ }
+
+ err = a.params.PowManager.CheckSolution(seed, solution)
+
+ if err != nil {
+ apiutil.BadRequest(rw, r, fmt.Errorf("checking proof-of-work solution: %w", err))
+ return
+ }
+
+ h.ServeHTTP(rw, r)
+ })
+}