* update rest endpoints * Add conversion logic for auth encode/decode/broadcast * Cleanup * Add tx conversion to legacy REST tx endpoints. * Cleanup * Add tests * Add tests for ConvertAndEncodeStdTx * Fix for StdSignature * Test coverage improvements * Add integration test for REST broadcast Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network>
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package rest
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
|
|
"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.
|
|
func DecodeTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var req DecodeReq
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
// NOTE: amino is used intentionally here, don't migrate it
|
|
err = clientCtx.Codec.UnmarshalJSON(body, &req)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
txBytes, err := base64.StdEncoding.DecodeString(req.Tx)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
sigFeeMemoTx, ok := tx.(signing.SigFeeMemoTx)
|
|
if !ok {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("%+v is not backwards compatible with %T", tx, authtypes.StdTx{}))
|
|
return
|
|
}
|
|
|
|
stdTx, err := clienttx.ConvertTxToStdTx(clientCtx.Codec, sigFeeMemoTx)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
response := DecodeResp(stdTx)
|
|
|
|
// NOTE: amino is set intentionally here, don't migrate it
|
|
clientCtx = clientCtx.WithJSONMarshaler(clientCtx.Codec)
|
|
rest.PostProcessResponse(w, clientCtx, response)
|
|
}
|
|
}
|