forked from yun-yeo/keyserver
/
encode.go
48 lines (41 loc) · 1013 Bytes
/
encode.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
package api
import (
"encoding/hex"
"io/ioutil"
"net/http"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/tendermint/crypto/tmhash"
)
// EncodeResopnse nolint
type EncodeResponse struct {
TxBytes string `json:"txbytes"`
TxID string `json:"txid"`
}
// Marshal - no-lint
func (s *Server) EncodeTx(w http.ResponseWriter, r *http.Request) {
var stdTx auth.StdTx
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(err).marshal())
return
}
err = cdc.UnmarshalJSON(body, &stdTx)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(err).marshal())
return
}
txBytes, err := cdc.MarshalBinaryLengthPrefixed(stdTx)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(err).marshal())
return
}
w.WriteHeader(http.StatusOK)
w.Write(cdc.MustMarshalJSON(EncodeResponse{
TxBytes: hex.EncodeToString(txBytes),
TxID: hex.EncodeToString(tmhash.Sum(txBytes)),
}))
return
}