2018-03-10 09:33:05 -08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/gorilla/mux"
|
2019-02-04 07:48:26 -08:00
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-02-14 08:53:36 -08:00
|
|
|
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-08-06 11:11:30 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
2018-03-10 09:33:05 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-03-10 09:33:05 -08:00
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// RegisterRoutes - Central function to define routes that get registered by the main application
|
2018-09-13 11:17:32 -07:00
|
|
|
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {
|
2018-10-04 04:00:24 -07:00
|
|
|
r.HandleFunc("/bank/accounts/{address}/transfers", SendRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
|
2018-04-18 21:49:24 -07:00
|
|
|
}
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
// SendReq defines the properties of a send request's body.
|
|
|
|
type SendReq struct {
|
2019-02-04 07:48:26 -08:00
|
|
|
BaseReq rest.BaseReq `json:"base_req"`
|
|
|
|
Amount sdk.Coins `json:"amount"`
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
var msgCdc = codec.New()
|
2018-05-31 11:51:15 -07:00
|
|
|
|
|
|
|
func init() {
|
2018-09-13 11:17:32 -07:00
|
|
|
bank.RegisterCodec(msgCdc)
|
2018-05-31 11:51:15 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
// SendRequestHandlerFn - http request handler to send coins to a address.
|
2018-09-13 11:17:32 -07:00
|
|
|
func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
|
2018-03-10 09:33:05 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2018-09-26 06:29:39 -07:00
|
|
|
bech32Addr := vars["address"]
|
2018-06-05 21:53:04 -07:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
toAddr, err := sdk.AccAddressFromBech32(bech32Addr)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-06-05 21:53:04 -07:00
|
|
|
return
|
|
|
|
}
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
var req SendReq
|
2019-02-04 16:45:18 -08:00
|
|
|
if !rest.ReadRESTReq(w, r, cdc, &req) {
|
2018-08-31 10:04:42 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-22 04:38:55 -07:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
req.BaseReq = req.BaseReq.Sanitize()
|
|
|
|
if !req.BaseReq.ValidateBasic(w) {
|
2018-09-02 11:20:14 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
if req.BaseReq.GenerateOnly {
|
|
|
|
// When generate only is supplied, the from field must be a valid Bech32
|
|
|
|
// address.
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
|
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 15:58:02 -08:00
|
|
|
msg := bank.NewMsgSend(fromAddr, toAddr, req.Amount)
|
2019-02-14 08:53:36 -08:00
|
|
|
clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive the from account address and name from the Keybase
|
|
|
|
fromAddress, fromName, err := context.GetFromFields(req.BaseReq.From)
|
2018-03-10 09:33:05 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
|
2019-02-04 15:58:02 -08:00
|
|
|
msg := bank.NewMsgSend(cliCtx.GetFromAddress(), toAddr, req.Amount)
|
2019-01-29 11:22:47 -08:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
clientrest.CompleteAndBroadcastTxREST(w, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
}
|