2018-03-10 09:33:05 -08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-08-22 04:38:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
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"
|
2018-05-31 11:51:15 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-04-25 07:18:06 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/client"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
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-08-06 11:11:30 -07:00
|
|
|
r.HandleFunc("/accounts/{address}/send", SendRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
|
2018-09-08 02:26:20 -07:00
|
|
|
r.HandleFunc("/tx/broadcast", BroadcastTxRequestHandlerFn(cdc, cliCtx)).Methods("POST")
|
2018-04-18 21:49:24 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
type sendReq struct {
|
|
|
|
BaseReq utils.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
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
to, err := sdk.AccAddressFromBech32(bech32Addr)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-06-05 21:53:04 -07:00
|
|
|
return
|
|
|
|
}
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
var req sendReq
|
|
|
|
err = utils.ReadRESTReq(w, r, cdc, &req)
|
2018-03-14 05:01:55 -07:00
|
|
|
if err != nil {
|
2018-08-31 10:04:42 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-22 04:38:55 -07:00
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
baseReq := req.BaseReq.Sanitize()
|
|
|
|
if !baseReq.ValidateBasic(w) {
|
2018-09-02 11:20:14 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
info, err := kb.Get(baseReq.Name)
|
2018-03-10 09:33:05 -08:00
|
|
|
if err != nil {
|
2018-08-31 08:10:09 -07:00
|
|
|
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
msg := client.CreateMsg(sdk.AccAddress(info.GetPubKey().Address()), to, req.Amount)
|
|
|
|
utils.CompleteAndBroadcastTxREST(w, r, cliCtx, baseReq, []sdk.Msg{msg}, cdc)
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
}
|