summaryrefslogtreecommitdiff
path: root/srv/src/api/pow.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-05 21:20:22 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-05 21:20:22 -0600
commiteed10ce514f28e4acf772f76c92ca05eebec105f (patch)
treed76820d7a3cd23f09f7dd0e6065bb0cef7ba16dc /srv/src/api/pow.go
parentcc8b6289ac3f7d1abb648217949beb89827d7374 (diff)
Fix various problems with the srv build
Diffstat (limited to 'srv/src/api/pow.go')
-rw-r--r--srv/src/api/pow.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/srv/src/api/pow.go b/srv/src/api/pow.go
new file mode 100644
index 0000000..1b232b1
--- /dev/null
+++ b/srv/src/api/pow.go
@@ -0,0 +1,53 @@
+package api
+
+import (
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "net/http"
+
+ "github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutils"
+)
+
+func (a *api) newPowChallengeHandler() http.Handler {
+ return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+
+ challenge := a.params.PowManager.NewChallenge()
+
+ apiutils.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 {
+ 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)
+ })
+}