github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/client/rest/encode.go (about) 1 package rest 2 3 import ( 4 "encoding/base64" 5 "io/ioutil" 6 "net/http" 7 8 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types" 11 ) 12 13 // EncodeResp defines a tx encoding response. 14 type EncodeResp struct { 15 Tx string `json:"tx" yaml:"tx"` 16 } 17 18 // EncodeTxRequestHandlerFn returns the encode tx REST handler. In particular, 19 // it takes a json-formatted transaction, encodes it to the Amino wire protocol, 20 // and responds with base64-encoded bytes. 21 func EncodeTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 22 return func(w http.ResponseWriter, r *http.Request) { 23 var req types.StdTx 24 25 body, err := ioutil.ReadAll(r.Body) 26 if err != nil { 27 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 28 return 29 } 30 31 err = cliCtx.Codec.UnmarshalJSON(body, &req) 32 if err != nil { 33 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 34 return 35 } 36 37 // re-encode it via the Amino wire protocol 38 txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req) 39 if err != nil { 40 rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 41 return 42 } 43 44 // base64 encode the encoded tx bytes 45 txBytesBase64 := base64.StdEncoding.EncodeToString(txBytes) 46 47 response := EncodeResp{Tx: txBytesBase64} 48 rest.PostProcessResponseBare(w, cliCtx, response) 49 } 50 }