From ef68be2f38e84c0b3ad7c73dc22ad6be6e197792 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 19 Mar 2018 18:13:28 +0100 Subject: [PATCH] Added IBC transfer REST endpoint --- client/lcd/root.go | 2 + x/ibc/rest/root.go | 14 ++++++ x/ibc/rest/transfer.go | 100 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 x/ibc/rest/root.go create mode 100644 x/ibc/rest/transfer.go diff --git a/client/lcd/root.go b/client/lcd/root.go index 7f18af59dc..9464081e0d 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -20,6 +20,7 @@ import ( "github.com/cosmos/cosmos-sdk/wire" auth "github.com/cosmos/cosmos-sdk/x/auth/rest" bank "github.com/cosmos/cosmos-sdk/x/bank/rest" + ibc "github.com/cosmos/cosmos-sdk/x/ibc/rest" ) const ( @@ -78,5 +79,6 @@ func createHandler(cdc *wire.Codec) http.Handler { tx.RegisterRoutes(r, cdc) auth.RegisterRoutes(r, cdc, "main") bank.RegisterRoutes(r, cdc, kb) + ibc.RegisterRoutes(r, cdc, kb) return r } diff --git a/x/ibc/rest/root.go b/x/ibc/rest/root.go new file mode 100644 index 0000000000..81e74d0312 --- /dev/null +++ b/x/ibc/rest/root.go @@ -0,0 +1,14 @@ +package rest + +import ( + "github.com/gorilla/mux" + + keys "github.com/tendermint/go-crypto/keys" + + "github.com/cosmos/cosmos-sdk/wire" +) + +// RegisterRoutes - Central function to define routes that get registered by the main application +func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) { + r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb)).Methods("POST") +} diff --git a/x/ibc/rest/transfer.go b/x/ibc/rest/transfer.go new file mode 100644 index 0000000000..f47159160e --- /dev/null +++ b/x/ibc/rest/transfer.go @@ -0,0 +1,100 @@ +package rest + +import ( + "encoding/hex" + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/gorilla/mux" + "github.com/spf13/viper" + "github.com/tendermint/go-crypto/keys" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/builder" + 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} + 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 + } + err = json.Unmarshal(body, &m) + 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 + // XXX: OMG + viper.Set(client.FlagSequence, m.Sequence) + txBytes, err := builder.SignAndBuild(m.LocalAccountName, m.Password, msg, c.Cdc) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(err.Error())) + return + } + + // send + res, err := builder.BroadcastTx(txBytes) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + output, err := json.MarshalIndent(res, "", " ") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(output) + } +}