diff options
author | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-10-22 20:03:57 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus.dahlberg@kau.se> | 2020-10-22 20:03:57 +0200 |
commit | 44e4fb0f4e9e9ff9bf6e31c68b626fea2eb7403a (patch) | |
tree | 1b066ef7b64a70020846b46589378f28bccf3d46 /handler.go | |
parent | ee3e12f3228b876b9fff8466b6d9ad3b7ea81816 (diff) |
Added start on get-proof-by-hash code path
If the provided tree size is (mostly) valid the Trillian back-end is
asked to provide an inclusion proof, which is then placed in an
InclusionProofV1 structure and returned as a JSON object.
Diffstat (limited to 'handler.go')
-rw-r--r-- | handler.go | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -169,6 +169,43 @@ func getAnchors(ctx context.Context, i *instance, w http.ResponseWriter, r *http // getProofByHash provides an inclusion proof based on a given leaf hash func getProofByHash(ctx context.Context, i *instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.Info("in getProofByHash") + request, err := NewGetProofByHashRequest(r) + if err != nil { + return http.StatusBadRequest, err + } + + trillianRequest := trillian.GetInclusionProofByHashRequest{ + LogId: i.logID, + LeafHash: request.Hash, + TreeSize: request.TreeSize, + OrderBySequence: true, + } + trillianResponse, err := i.client.GetInclusionProofByHash(ctx, &trillianRequest) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed fetching inclusion proof from Trillian backend: %v", err) + } + // TODO: check the returned tree size in response? + + // Santity check + if len(trillianResponse.Proof) == 0 { + return http.StatusNotFound, fmt.Errorf("get-proof-by-hash backend returned no proof") + } + // TODO: verify that proof is valid? + + w.Header().Set("Content-Type", "application/json") + data, err := NewGetProofByHashResponse(uint64(request.TreeSize), trillianResponse.Proof[0]) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed creating get-proof-by-hash response: %v", err) + } + json, err := json.Marshal(data) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed json-encoding GetProofByHashResponse: %v", err) + } + _, err = w.Write(json) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("failed writing get-entries response: %v", err) + } + return http.StatusOK, nil // TODO } |