github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/client/rest/decode.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  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    11  )
    12  
    13  type (
    14  	// DecodeReq defines a tx decoding request.
    15  	DecodeReq struct {
    16  		Tx string `json:"tx"`
    17  	}
    18  
    19  	// DecodeResp defines a tx decoding response.
    20  	DecodeResp authtypes.StdTx
    21  )
    22  
    23  // DecodeTxRequestHandlerFn returns the decode tx REST handler. In particular,
    24  // it takes base64-decoded bytes, decodes it from the Amino wire protocol,
    25  // and responds with a json-formatted transaction.
    26  func DecodeTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    27  	return func(w http.ResponseWriter, r *http.Request) {
    28  		var req DecodeReq
    29  
    30  		body, err := ioutil.ReadAll(r.Body)
    31  		if err != nil {
    32  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    33  			return
    34  		}
    35  
    36  		err = cliCtx.Codec.UnmarshalJSON(body, &req)
    37  		if err != nil {
    38  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    39  			return
    40  		}
    41  
    42  		txBytes, err := base64.StdEncoding.DecodeString(req.Tx)
    43  		if err != nil {
    44  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    45  			return
    46  		}
    47  
    48  		var stdTx authtypes.StdTx
    49  		err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx)
    50  		if err != nil {
    51  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    52  			return
    53  		}
    54  
    55  		response := DecodeResp(stdTx)
    56  		rest.PostProcessResponse(w, cliCtx, response)
    57  	}
    58  }