-
Notifications
You must be signed in to change notification settings - Fork 20
/
rest.go
76 lines (62 loc) · 1.86 KB
/
rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package client
import (
"fmt"
"github.com/QOSGroup/kepler/cert"
"github.com/QOSGroup/qbase/client/context"
"github.com/QOSGroup/qbase/client/rpc"
"github.com/QOSGroup/qbase/types"
"github.com/QOSGroup/qos/module/qcp/txs"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
)
func RegisterRoutes(ctx context.CLIContext, r *mux.Router) {
registerQueryRoutes(ctx, r)
registerTxRoutes(ctx, r)
}
func registerQueryRoutes(ctx context.CLIContext, r *mux.Router) {
}
func registerTxRoutes(ctx context.CLIContext, r *mux.Router) {
r.HandleFunc("/qcp/qcps", InitQCPHandlerFn(ctx)).Methods("POST")
}
func InitQCPHandlerFn(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
bq, err := rpc.ParseRequestForm(request)
if err != nil {
rpc.WriteErrorResponse(writer, http.StatusBadRequest, err.Error())
return
}
caFile, _, err := request.FormFile("ca_file")
if err != nil {
rpc.WriteErrorResponse(writer, http.StatusBadRequest, err.Error())
return
}
caBytes, err := ioutil.ReadAll(caFile)
if err != nil {
rpc.WriteErrorResponse(writer, http.StatusBadRequest, err.Error())
return
}
var crt = cert.Certificate{}
err = cliContext.Codec.UnmarshalJSON(caBytes, &crt)
if err != nil {
rpc.WriteErrorResponse(writer, http.StatusBadRequest, fmt.Sprintf("crt file unmarshal err. err: %s", err.Error()))
return
}
_, ok := crt.CSR.Subj.(cert.QCPSubject)
if !ok {
rpc.WriteErrorResponse(writer, http.StatusBadRequest, "invalid crt file")
return
}
fromAddr, _ := types.AccAddressFromBech32(bq.From)
tx := txs.TxInitQCP{
Creator: fromAddr,
QCPCA: &crt,
}
err = tx.ValidateInputs()
if err != nil {
rpc.WriteErrorResponse(writer, http.StatusBadRequest, err.Error())
return
}
rpc.WriteGenStdTxResponse(writer, cliContext, bq, tx)
}
}