2018-08-22 04:38:55 -07:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2018-08-31 10:04:11 -07:00
|
|
|
"fmt"
|
2018-09-26 06:29:39 -07:00
|
|
|
"io/ioutil"
|
2018-08-22 04:38:55 -07:00
|
|
|
"net/http"
|
2018-08-31 10:04:42 -07:00
|
|
|
"strconv"
|
2018-09-26 06:29:39 -07:00
|
|
|
"strings"
|
2018-09-02 11:20:14 -07:00
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-09-26 06:29:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-10-24 06:19:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
|
2018-09-02 11:20:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-10-04 04:00:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-09-07 10:04:58 -07:00
|
|
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
2018-08-22 04:38:55 -07:00
|
|
|
)
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// GasEstimateResponse defines a response definition for tx gas estimation.
|
|
|
|
type GasEstimateResponse struct {
|
|
|
|
GasEstimate uint64 `json:"gas_estimate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-09-26 06:29:39 -07:00
|
|
|
// Basic HTTP utilities
|
|
|
|
|
2018-08-22 04:38:55 -07:00
|
|
|
// WriteErrorResponse prepares and writes a HTTP error
|
|
|
|
// given a status code and an error message.
|
2018-10-19 09:55:20 -07:00
|
|
|
func WriteErrorResponse(w http.ResponseWriter, status int, err string) {
|
2018-08-31 08:10:09 -07:00
|
|
|
w.WriteHeader(status)
|
2018-10-19 09:55:20 -07:00
|
|
|
w.Write([]byte(err))
|
2018-08-22 04:38:55 -07:00
|
|
|
}
|
2018-08-31 10:04:11 -07:00
|
|
|
|
2018-10-19 09:55:20 -07:00
|
|
|
// WriteSimulationResponse prepares and writes an HTTP
|
2018-08-31 10:04:11 -07:00
|
|
|
// response for transactions simulations.
|
2019-01-29 11:22:47 -08:00
|
|
|
func WriteSimulationResponse(w http.ResponseWriter, cdc *codec.Codec, gas uint64) {
|
|
|
|
gasEst := GasEstimateResponse{GasEstimate: gas}
|
|
|
|
resp, err := cdc.MarshalJSON(gasEst)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-08-31 08:10:09 -07:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-01-29 11:22:47 -08:00
|
|
|
w.Write(resp)
|
2018-08-31 10:04:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
// ParseInt64OrReturnBadRequest converts s to a int64 value.
|
|
|
|
func ParseInt64OrReturnBadRequest(w http.ResponseWriter, s string) (n int64, ok bool) {
|
|
|
|
var err error
|
2018-09-02 11:20:14 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
n, err = strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("'%s' is not a valid int64", s)
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return n, false
|
2018-11-06 23:33:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseUint64OrReturnBadRequest converts s to a uint64 value.
|
|
|
|
func ParseUint64OrReturnBadRequest(w http.ResponseWriter, s string) (n uint64, ok bool) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
n, err = strconv.ParseUint(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("'%s' is not a valid uint64", s)
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return n, false
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
2018-08-31 10:04:42 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
return n, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFloat64OrReturnBadRequest converts s to a float64 value. It returns a
|
|
|
|
// default value, defaultIfEmpty, if the string is empty.
|
2018-08-31 08:10:09 -07:00
|
|
|
func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEmpty float64) (n float64, ok bool) {
|
2018-08-31 10:04:42 -07:00
|
|
|
if len(s) == 0 {
|
|
|
|
return defaultIfEmpty, true
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-08-31 10:04:42 -07:00
|
|
|
n, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return n, false
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2018-08-31 10:04:42 -07:00
|
|
|
return n, true
|
|
|
|
}
|
2018-09-02 11:20:14 -07:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-09-26 06:29:39 -07:00
|
|
|
// Building / Sending utilities
|
|
|
|
|
|
|
|
// BaseReq defines a structure that can be embedded in other request structures
|
|
|
|
// that all share common "base" fields.
|
|
|
|
type BaseReq struct {
|
2019-01-29 11:22:47 -08:00
|
|
|
From string `json:"from"`
|
2019-01-18 08:45:20 -08:00
|
|
|
Password string `json:"password"`
|
|
|
|
Memo string `json:"memo"`
|
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
AccountNumber uint64 `json:"account_number"`
|
|
|
|
Sequence uint64 `json:"sequence"`
|
|
|
|
Fees sdk.Coins `json:"fees"`
|
|
|
|
GasPrices sdk.DecCoins `json:"gas_prices"`
|
|
|
|
Gas string `json:"gas"`
|
|
|
|
GasAdjustment string `json:"gas_adjustment"`
|
|
|
|
GenerateOnly bool `json:"generate_only"`
|
|
|
|
Simulate bool `json:"simulate"`
|
2018-12-19 16:26:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseReq creates a new basic request instance and sanitizes its values
|
|
|
|
func NewBaseReq(
|
2019-01-29 11:22:47 -08:00
|
|
|
from, password, memo, chainID string, gas, gasAdjustment string,
|
2019-01-18 08:45:20 -08:00
|
|
|
accNumber, seq uint64, fees sdk.Coins, gasPrices sdk.DecCoins, genOnly, simulate bool,
|
|
|
|
) BaseReq {
|
2018-12-19 16:26:33 -08:00
|
|
|
|
|
|
|
return BaseReq{
|
2019-01-29 11:22:47 -08:00
|
|
|
From: strings.TrimSpace(from),
|
2018-12-19 16:26:33 -08:00
|
|
|
Password: password,
|
|
|
|
Memo: strings.TrimSpace(memo),
|
|
|
|
ChainID: strings.TrimSpace(chainID),
|
|
|
|
Fees: fees,
|
2019-01-18 08:45:20 -08:00
|
|
|
GasPrices: gasPrices,
|
2018-12-19 16:26:33 -08:00
|
|
|
Gas: strings.TrimSpace(gas),
|
|
|
|
GasAdjustment: strings.TrimSpace(gasAdjustment),
|
|
|
|
AccountNumber: accNumber,
|
|
|
|
Sequence: seq,
|
|
|
|
GenerateOnly: genOnly,
|
|
|
|
Simulate: simulate,
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize performs basic sanitization on a BaseReq object.
|
|
|
|
func (br BaseReq) Sanitize() BaseReq {
|
2019-01-18 08:45:20 -08:00
|
|
|
return NewBaseReq(
|
2019-01-29 11:22:47 -08:00
|
|
|
br.From, br.Password, br.Memo, br.ChainID, br.Gas, br.GasAdjustment,
|
2019-01-18 08:45:20 -08:00
|
|
|
br.AccountNumber, br.Sequence, br.Fees, br.GasPrices, br.GenerateOnly, br.Simulate,
|
2018-12-19 16:26:33 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateBasic performs basic validation of a BaseReq. If custom validation
|
|
|
|
// logic is needed, the implementing request handler should perform those
|
|
|
|
// checks manually.
|
|
|
|
func (br BaseReq) ValidateBasic(w http.ResponseWriter) bool {
|
|
|
|
if !br.GenerateOnly && !br.Simulate {
|
|
|
|
switch {
|
|
|
|
case len(br.Password) == 0:
|
|
|
|
WriteErrorResponse(w, http.StatusUnauthorized, "password required but not specified")
|
|
|
|
return false
|
2019-01-18 08:45:20 -08:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
case len(br.ChainID) == 0:
|
|
|
|
WriteErrorResponse(w, http.StatusUnauthorized, "chain-id required but not specified")
|
|
|
|
return false
|
2019-01-18 08:45:20 -08:00
|
|
|
|
|
|
|
case !br.Fees.IsZero() && !br.GasPrices.IsZero():
|
|
|
|
// both fees and gas prices were provided
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, "cannot provide both fees and gas prices")
|
|
|
|
return false
|
|
|
|
|
|
|
|
case !br.Fees.IsValid() && !br.GasPrices.IsValid():
|
|
|
|
// neither fees or gas prices were provided
|
|
|
|
WriteErrorResponse(w, http.StatusPaymentRequired, "invalid fees or gas prices provided")
|
2018-12-19 16:26:33 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 08:45:20 -08:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
if len(br.From) == 0 {
|
|
|
|
WriteErrorResponse(w, http.StatusUnauthorized, "name or address required but not specified")
|
2018-12-19 16:26:33 -08:00
|
|
|
return false
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
2019-01-18 08:45:20 -08:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
return true
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ReadRESTReq is a simple convenience wrapper that reads the body and
|
|
|
|
unmarshals to the req interface.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
type SomeReq struct {
|
|
|
|
BaseReq `json:"base_req"`
|
|
|
|
CustomField string `json:"custom_field"`
|
|
|
|
}
|
|
|
|
|
|
|
|
req := new(SomeReq)
|
|
|
|
err := ReadRESTReq(w, r, cdc, req)
|
|
|
|
*/
|
|
|
|
func ReadRESTReq(w http.ResponseWriter, r *http.Request, cdc *codec.Codec, req interface{}) error {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cdc.UnmarshalJSON(body, req)
|
|
|
|
if err != nil {
|
2018-12-20 06:46:51 -08:00
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to decode JSON payload: %s", err))
|
2018-09-26 06:29:39 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteAndBroadcastTxREST implements a utility function that facilitates
|
2019-01-29 11:22:47 -08:00
|
|
|
// sending a series of messages in a signed tx. In addition, it will handle
|
|
|
|
// tx gas simulation and estimation.
|
2018-09-26 06:29:39 -07:00
|
|
|
//
|
2019-01-29 11:22:47 -08:00
|
|
|
// NOTE: Also see CompleteAndBroadcastTxCLI.
|
2019-01-18 08:45:20 -08:00
|
|
|
func CompleteAndBroadcastTxREST(
|
|
|
|
w http.ResponseWriter, r *http.Request, cliCtx context.CLIContext,
|
|
|
|
baseReq BaseReq, msgs []sdk.Msg, cdc *codec.Codec,
|
|
|
|
) {
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
gasAdj, ok := ParseFloat64OrReturnBadRequest(w, baseReq.GasAdjustment, client.DefaultGasAdjustment)
|
2018-12-19 16:26:33 -08:00
|
|
|
if !ok {
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
simAndExec, gas, err := client.ParseGas(baseReq.Gas)
|
2018-12-19 16:26:33 -08:00
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// derive the from account address and name from the Keybase
|
|
|
|
fromAddress, fromName, err := context.GetFromFields(baseReq.From)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
|
2019-01-18 08:45:20 -08:00
|
|
|
txBldr := authtxb.NewTxBuilder(
|
|
|
|
GetTxEncoder(cdc), baseReq.AccountNumber,
|
2019-01-29 11:22:47 -08:00
|
|
|
baseReq.Sequence, gas, gasAdj, baseReq.Simulate,
|
2019-01-18 08:45:20 -08:00
|
|
|
baseReq.ChainID, baseReq.Memo, baseReq.Fees, baseReq.GasPrices,
|
|
|
|
)
|
2018-12-19 16:26:33 -08:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
txBldr, err = prepareTxBuilder(txBldr, cliCtx)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseReq.Simulate || simAndExec {
|
|
|
|
if gasAdj < 0 {
|
2018-12-19 16:26:33 -08:00
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, "gas adjustment must be a positive float")
|
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
txBldr, err = EnrichWithGas(txBldr, cliCtx, cliCtx.GetFromName(), msgs)
|
2018-09-26 06:29:39 -07:00
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:26:34 -08:00
|
|
|
if baseReq.Simulate {
|
2019-01-29 11:22:47 -08:00
|
|
|
WriteSimulationResponse(w, cdc, txBldr.GetGas())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
txBytes, err := txBldr.BuildAndSign(cliCtx.GetFromName(), baseReq.Password, msgs)
|
2018-10-24 06:19:48 -07:00
|
|
|
if keyerror.IsErrKeyNotFound(err) {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
} else if keyerror.IsErrWrongPassword(err) {
|
2018-09-26 06:29:39 -07:00
|
|
|
WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
|
|
|
return
|
2018-10-24 06:19:48 -07:00
|
|
|
} else if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cliCtx.BroadcastTx(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// PostProcessResponse performs post processing for a REST response.
|
2018-10-04 04:00:24 -07:00
|
|
|
func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}, indent bool) {
|
|
|
|
var output []byte
|
2019-01-29 11:22:47 -08:00
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
switch response.(type) {
|
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
if indent {
|
|
|
|
output, err = cdc.MarshalJSONIndent(response, "", " ")
|
|
|
|
} else {
|
|
|
|
output, err = cdc.MarshalJSON(response)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case []byte:
|
|
|
|
output = response.([]byte)
|
|
|
|
}
|
2019-01-29 11:22:47 -08:00
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-09-26 06:29:39 -07:00
|
|
|
w.Write(output)
|
|
|
|
}
|
2019-01-29 11:22:47 -08:00
|
|
|
|
|
|
|
// WriteGenerateStdTxResponse writes response for the generate only mode.
|
|
|
|
func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec, br BaseReq, msgs []sdk.Msg) {
|
|
|
|
gasAdj, ok := ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, client.DefaultGasAdjustment)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, gas, err := client.ParseGas(br.Gas)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txBldr := authtxb.NewTxBuilder(
|
|
|
|
GetTxEncoder(cdc), br.AccountNumber, br.Sequence, gas, gasAdj,
|
|
|
|
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
|
|
|
|
)
|
|
|
|
|
|
|
|
stdMsg, err := txBldr.Build(msgs)
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := cdc.MarshalJSON(auth.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
|
|
|
|
if err != nil {
|
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(output)
|
|
|
|
return
|
|
|
|
}
|