2018-03-19 10:13:28 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/tendermint/go-crypto/keys"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-03-19 10:13:28 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/commands"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/ibc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type transferBody struct {
|
|
|
|
// Fees sdk.Coin `json="fees"`
|
|
|
|
Amount sdk.Coins `json:"amount"`
|
|
|
|
LocalAccountName string `json:"name"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
SrcChainID string `json:"src_chain_id"`
|
|
|
|
Sequence int64 `json:"sequence"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransferRequestHandler - http request handler to transfer coins to a address
|
|
|
|
// on a different chain via IBC
|
|
|
|
func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) {
|
|
|
|
c := commands.Commander{cdc}
|
2018-04-03 05:11:49 -07:00
|
|
|
ctx := context.NewCoreContextFromViper()
|
2018-03-19 10:13:28 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// collect data
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
destChainID := vars["destchain"]
|
|
|
|
address := vars["address"]
|
|
|
|
|
|
|
|
var m transferBody
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-04-07 10:56:49 -07:00
|
|
|
err = cdc.UnmarshalJSON(body, &m)
|
2018-03-19 10:13:28 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := kb.Get(m.LocalAccountName)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := hex.DecodeString(address)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
to := sdk.Address(bz)
|
|
|
|
|
|
|
|
// build message
|
|
|
|
packet := ibc.NewIBCPacket(info.PubKey.Address(), to, m.Amount, m.SrcChainID, destChainID)
|
|
|
|
msg := ibc.IBCTransferMsg{packet}
|
|
|
|
|
|
|
|
// sign
|
2018-04-03 05:11:49 -07:00
|
|
|
ctx = ctx.WithSequence(m.Sequence)
|
2018-03-30 05:57:53 -07:00
|
|
|
txBytes, err := ctx.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc)
|
2018-03-19 10:13:28 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// send
|
2018-03-30 05:57:53 -07:00
|
|
|
res, err := ctx.BroadcastTx(txBytes)
|
2018-03-19 10:13:28 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:56:49 -07:00
|
|
|
output, err := cdc.MarshalJSON(res)
|
2018-03-19 10:13:28 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(output)
|
|
|
|
}
|
|
|
|
}
|