github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/serve/lc_sign.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package serve 10 11 import ( 12 "encoding/json" 13 "fmt" 14 "net/http" 15 "strings" 16 17 "github.com/vchain-us/vcn/pkg/api" 18 "github.com/vchain-us/vcn/pkg/cmd/internal/types" 19 "github.com/vchain-us/vcn/pkg/meta" 20 ) 21 22 func lcSign(user *api.LcUser, status meta.Status, kinds map[string]bool, w http.ResponseWriter, r *http.Request) { 23 if user.Client.ApiKey == "" { 24 writeError(w, http.StatusUnauthorized, fmt.Errorf("api key not provided")) 25 return 26 } 27 err := user.Client.Connect() 28 if err != nil { 29 writeError(w, http.StatusBadGateway, err) 30 return 31 } 32 opts := []api.LcSignOption{ 33 api.LcSignWithStatus(status), 34 } 35 36 if r.Body == http.NoBody { 37 writeError(w, http.StatusBadRequest, fmt.Errorf("no artifact submitted")) 38 return 39 } 40 41 decoder := json.NewDecoder(r.Body) 42 var artifact api.Artifact 43 err = decoder.Decode(&artifact) 44 if err != nil { 45 writeError(w, http.StatusBadRequest, err) 46 return 47 } 48 49 if artifact.Name == "" { 50 writeError(w, http.StatusBadRequest, fmt.Errorf("name cannot be empty")) 51 return 52 } 53 54 if !kinds[artifact.Kind] { 55 writeError(w, http.StatusBadRequest, fmt.Errorf(`"%s" is not a valid value for kind`, artifact.Kind)) 56 return 57 } 58 59 artifact.Hash = strings.ToLower(artifact.Hash) 60 61 verified, tx, err := user.Sign( 62 artifact, 63 opts..., 64 ) 65 66 if err != nil { 67 writeError(w, http.StatusBadRequest, err) 68 return 69 } 70 71 ar, verified, err := user.LoadArtifact(artifact.Hash, "", "", tx, nil) 72 if err != nil { 73 writeError(w, http.StatusBadRequest, err) 74 return 75 } 76 77 writeLcResult(w, http.StatusOK, types.NewLcResult(ar, verified, nil)) 78 }