mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-10 03:31:20 +00:00
fc193b09bf
* initial implementation * it works * tests * extracted helper functions * fix code --------- Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package shared
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/v4/network"
|
|
)
|
|
|
|
func ValidateHex(w http.ResponseWriter, name string, s string) bool {
|
|
if s == "" {
|
|
errJson := &network.DefaultErrorJson{
|
|
Message: name + " is required",
|
|
Code: http.StatusBadRequest,
|
|
}
|
|
network.WriteError(w, errJson)
|
|
return false
|
|
}
|
|
if !bytesutil.IsHex([]byte(s)) {
|
|
errJson := &network.DefaultErrorJson{
|
|
Message: name + " is invalid",
|
|
Code: http.StatusBadRequest,
|
|
}
|
|
network.WriteError(w, errJson)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func ValidateUint(w http.ResponseWriter, name string, s string) (uint64, bool) {
|
|
if s == "" {
|
|
errJson := &network.DefaultErrorJson{
|
|
Message: name + " is required",
|
|
Code: http.StatusBadRequest,
|
|
}
|
|
network.WriteError(w, errJson)
|
|
return 0, false
|
|
}
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
if err != nil {
|
|
errJson := &network.DefaultErrorJson{
|
|
Message: name + " is invalid: " + err.Error(),
|
|
Code: http.StatusBadRequest,
|
|
}
|
|
network.WriteError(w, errJson)
|
|
return 0, false
|
|
}
|
|
return v, true
|
|
}
|