prysm-pulse/tools/ssz-server/main.go
terence tsao 1b5b8a57e0 Remove unused proto schemas (#3005)
* Update io_kubernetes_build commit hash to 1246899

* Update dependency build_bazel_rules_nodejs to v0.33.1

* Update dependency com_github_hashicorp_golang_lru to v0.5.1

* Update libp2p

* Update io_bazel_rules_k8s commit hash to e68d5d7

* Starting to remove old protos

* Bazel build proto passes

* Fixing pb version

* Cleaned up core package

* Fixing tests

* 6 tests failing

* Update proto bugs

* Fixed incorrect validator ordering proto

* Sync with master

* Update go-ssz commit

* Removed bad copies from v1alpha1 folder

* add json spec json to pb handler

* add nested proto example

* proto/testing test works

* fix refactoring build failures

* use merged ssz

* push latest changes

* used forked json encoding

* used forked json encoding

* fix warning

* fix build issues

* fix test and lint

* fix build

* lint
2019-07-22 10:03:57 -04:00

97 lines
2.4 KiB
Go

// This binary is a simple ssz deserializer REST API endpoint for the testnet
// frontend to decode the deposit data for the user.
// This should be removed after https://github.com/prysmaticlabs/prysm-testnet-site/issues/37
// is resolved.
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
ssz "github.com/prysmaticlabs/go-ssz"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
_ "go.uber.org/automaxprocs"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/healthz", healthz)
mux.HandleFunc("/api/decodeDepositData", decodeDepositData)
log.Println("Starting on port 4000")
if err := http.ListenAndServe(":4000", mux); err != nil {
log.Fatalf("Failed to start server %v", err)
}
}
func healthz(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
type deserializationRequest struct {
Data string `json:"data"`
}
func decodeDepositData(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to read request body: %v\n", err)
return
}
requestData := &deserializationRequest{}
if err := json.Unmarshal(data, requestData); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Unable to unmarshal JSON: %v\n", err)
return
}
if len(requestData.Data) < 2 {
w.WriteHeader(http.StatusBadRequest)
return
}
encodedData, err := hex.DecodeString(requestData.Data[2:])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err = fmt.Fprintf(w, "Failed to decode hex string")
if err != nil {
log.Printf("Failed to write data to client: %v\n", err)
}
return
}
di := &ethpb.Deposit_Data{}
if err := ssz.Unmarshal(encodedData, di); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err := fmt.Fprintf(w, "Failed to decode SSZ data: %v", err)
if err != nil {
log.Printf("Failed to write data to client: %v\n", err)
}
return
}
b, err := json.Marshal(di)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to marshal deposit data: %v\n", err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(b)
if err != nil {
log.Printf("Failed to write data to client: %v\n", err)
}
}