2019-09-17 14:08:09 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2020-07-30 07:45:18 -07:00
|
|
|
"fmt"
|
2019-09-17 14:08:09 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2020-07-30 07:45:18 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-07-30 07:45:18 -07:00
|
|
|
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
|
2019-09-17 14:08:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// DecodeReq defines a tx decoding request.
|
|
|
|
DecodeReq struct {
|
|
|
|
Tx string `json:"tx"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeResp defines a tx decoding response.
|
|
|
|
DecodeResp authtypes.StdTx
|
|
|
|
)
|
|
|
|
|
|
|
|
// DecodeTxRequestHandlerFn returns the decode tx REST handler. In particular,
|
|
|
|
// it takes base64-decoded bytes, decodes it from the Amino wire protocol,
|
|
|
|
// and responds with a json-formatted transaction.
|
2020-06-01 05:46:03 -07:00
|
|
|
func DecodeTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2019-09-17 14:08:09 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req DecodeReq
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-09-17 14:08:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-30 07:45:18 -07:00
|
|
|
// NOTE: amino is used intentionally here, don't migrate it
|
2020-08-10 12:41:21 -07:00
|
|
|
err = clientCtx.LegacyAmino.UnmarshalJSON(body, &req)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-09-17 14:08:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txBytes, err := base64.StdEncoding.DecodeString(req.Tx)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-09-17 14:08:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:48:28 -07:00
|
|
|
stdTx, ok := convertToStdTx(w, clientCtx, txBytes)
|
2020-07-30 07:45:18 -07:00
|
|
|
if !ok {
|
2019-09-17 14:08:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := DecodeResp(stdTx)
|
2020-07-30 07:45:18 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponse(w, clientCtx, response)
|
2019-09-17 14:08:09 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 09:48:28 -07:00
|
|
|
|
|
|
|
// convertToStdTx converts tx proto binary bytes retrieved from Tendermint into
|
|
|
|
// a StdTx. Returns the StdTx, as well as a flag denoting if the function
|
|
|
|
// successfully converted or not.
|
|
|
|
func convertToStdTx(w http.ResponseWriter, clientCtx client.Context, txBytes []byte) (authtypes.StdTx, bool) {
|
|
|
|
txI, err := clientCtx.TxConfig.TxDecoder()(txBytes)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
|
|
return authtypes.StdTx{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, ok := txI.(signing.Tx)
|
|
|
|
if !ok {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("%+v is not backwards compatible with %T", tx, authtypes.StdTx{}))
|
|
|
|
return authtypes.StdTx{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
stdTx, err := clienttx.ConvertTxToStdTx(clientCtx.LegacyAmino, tx)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
|
|
return authtypes.StdTx{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdTx, true
|
|
|
|
}
|