diff options
author | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-10-28 13:38:39 +0100 |
---|---|---|
committer | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-10-28 13:38:39 +0100 |
commit | d752d967335e1418f27e03e0389b01178b28f232 (patch) | |
tree | abc3b6f2e3b64af67f19ca50f6e5c3609d829fb9 /handler.go | |
parent | e7801b268c97c6b72bfcd76549ce5fd50ab0b1b5 (diff) |
added signed tree head and get-sth code path
Diffstat (limited to 'handler.go')
-rw-r--r-- | handler.go | 35 |
1 files changed, 33 insertions, 2 deletions
@@ -9,6 +9,7 @@ import ( "github.com/golang/glog" "github.com/google/trillian" + "github.com/google/trillian/types" ) // appHandler implements the http.Handler interface, and contains a reference @@ -177,7 +178,37 @@ func getConsistencyProof(ctx context.Context, i *Instance, w http.ResponseWriter } // getSth provides the most recent STH -func getSth(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { +func getSth(ctx context.Context, i *Instance, w http.ResponseWriter, _ *http.Request) (int, error) { glog.Info("in getSth") - return http.StatusOK, nil // TODO + trillianRequest := trillian.GetLatestSignedLogRootRequest{ + LogId: i.LogParameters.TreeId, + } + trillianResponse, err := i.Client.GetLatestSignedLogRoot(ctx, &trillianRequest) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed fetching signed tree head from Trillian backend: %v", err) + } + if trillianResponse.SignedLogRoot == nil { + return http.StatusInternalServerError, fmt.Errorf("Trillian returned no tree head") + } + + var lr types.LogRootV1 + if err := lr.UnmarshalBinary(trillianResponse.SignedLogRoot.GetLogRoot()); err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed unmarshaling tree head: %v", err) + } + + th := NewTreeHeadV1(uint64(lr.TimestampNanos / 1000 / 1000), uint64(lr.TreeSize), lr.RootHash) + sth, err := GenV1STH(i.LogParameters, th) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed creating signed tree head: %v", err) + } + glog.Infof("%v", sth) + + response, err := NewGetSthResponse(sth) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed creating GetSthResponse: %v", err) + } + if err := WriteJsonResponse(response, w); err != nil { + return http.StatusInternalServerError, err + } + return http.StatusOK, nil } |