prysm-pulse/beacon-chain/rpc/eth/shared/request.go
Radosław Kapka a9b003e1fe
HTTP Beacon API: /eth/v1/validator/contribution_and_proofs (#12660)
* HTTP Beacon API: `/eth/v1/validator/contribution_and_proofs`

* add comment to invalid test case

* fix validation and test

* review

---------

Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
2023-07-31 17:32:39 +00:00

51 lines
1.1 KiB
Go

package shared
import (
"net/http"
"strconv"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
http2 "github.com/prysmaticlabs/prysm/v4/network/http"
)
func ValidateHex(w http.ResponseWriter, name string, s string) bool {
if s == "" {
errJson := &http2.DefaultErrorJson{
Message: name + " is required",
Code: http.StatusBadRequest,
}
http2.WriteError(w, errJson)
return false
}
if !bytesutil.IsHex([]byte(s)) {
errJson := &http2.DefaultErrorJson{
Message: name + " is invalid",
Code: http.StatusBadRequest,
}
http2.WriteError(w, errJson)
return false
}
return true
}
func ValidateUint(w http.ResponseWriter, name string, s string) (uint64, bool) {
if s == "" {
errJson := &http2.DefaultErrorJson{
Message: name + " is required",
Code: http.StatusBadRequest,
}
http2.WriteError(w, errJson)
return 0, false
}
v, err := strconv.ParseUint(s, 10, 64)
if err != nil {
errJson := &http2.DefaultErrorJson{
Message: name + " is invalid: " + err.Error(),
Code: http.StatusBadRequest,
}
http2.WriteError(w, errJson)
return 0, false
}
return v, true
}