From cf6f04978ce33679ebe55a263d74bd67780e7e9e Mon Sep 17 00:00:00 2001 From: Fabian Weber Date: Wed, 14 Mar 2018 13:01:55 +0100 Subject: [PATCH] adjusted sending + tx tests --- client/builder/builder.go | 13 +--- client/lcd/lcd_test.go | 86 +++++++++++++++++-------- client/tx/root.go | 4 +- client/tx/search.go | 6 ++ examples/basecoin/x/cool/commands/tx.go | 28 +++++++- x/auth/rest/query.go | 2 - x/bank/commands/sendtx.go | 15 ++++- x/bank/rest/sendtx.go | 31 ++++++--- 8 files changed, 131 insertions(+), 54 deletions(-) diff --git a/client/builder/builder.go b/client/builder/builder.go index a64dfeda74..69c95fa3b6 100644 --- a/client/builder/builder.go +++ b/client/builder/builder.go @@ -88,7 +88,7 @@ func GetFromAddress() (from sdk.Address, err error) { } // sign and build the transaction from the msg -func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) { +func SignAndBuild(name, passphrase string, msg sdk.Msg, cdc *wire.Codec) ([]byte, error) { // build the Sign Messsage from the Standard Message chainID := viper.GetString(client.FlagChainID) @@ -103,16 +103,9 @@ func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) { if err != nil { return nil, err } - name := viper.GetString(client.FlagName) // sign and build bz := signMsg.Bytes() - buf := client.BufferStdin() - prompt := fmt.Sprintf("Password to sign with '%s':", name) - passphrase, err := client.GetPassword(prompt, buf) - if err != nil { - return nil, err - } sig, pubkey, err := keybase.Sign(name, passphrase, bz) if err != nil { return nil, err @@ -130,8 +123,8 @@ func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) { } // sign and build the transaction from the msg -func SignBuildBroadcast(msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) { - txBytes, err := SignAndBuild(msg, cdc) +func SignBuildBroadcast(name string, passphrase string, msg sdk.Msg, cdc *wire.Codec) (*ctypes.ResultBroadcastTxCommit, error) { + txBytes, err := SignAndBuild(name, passphrase, msg, cdc) if err != nil { return nil, err } diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 6fda0bc4c7..e4be056e25 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -197,14 +197,10 @@ func TestCoinSend(t *testing.T) { res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil) require.Equal(t, http.StatusNoContent, res.StatusCode, body) - // create account from seed who has keys - var jsonStr = []byte(fmt.Sprintf(`{"name":"account_with_coins", "password":"1234567890", "seed": "%s"}`, seed)) - res, body = request(t, port, "POST", "/keys", jsonStr) + // create TX + addr, receiveAddr := doSend(t, port, seed) - assert.Equal(t, http.StatusOK, res.StatusCode, body) - addr := body - - // query + // query sender res, body = request(t, port, "GET", "/accounts/"+addr, nil) require.Equal(t, http.StatusOK, res.StatusCode, body) @@ -212,30 +208,12 @@ func TestCoinSend(t *testing.T) { "coins": [ { "denom": "mycoin", - "amount": 9007199254740992 + "amount": 9007199254740991 } ] }`, body) - // create receive address - kb := client.MockKeyBase() - receiveInfo, _, err := kb.Create("receive_address", "1234567890", cryptoKeys.CryptoAlgo("ed25519")) - require.Nil(t, err) - receiveAddr := string(receiveInfo.Address()) - - // send - jsonStr = []byte(`{ - "name":"account_with_coins", - "password":"1234567890", - "amount":[{ - "denom": "mycoin", - "amount": 1 - }] - }`) - res, body = request(t, port, "POST", "/accounts/"+receiveAddr+"/send", jsonStr) - require.Equal(t, http.StatusOK, res.StatusCode, body) - - // check if received + // query receiver res, body = request(t, port, "GET", "/accounts/"+receiveAddr, nil) require.Equal(t, http.StatusOK, res.StatusCode, body) @@ -249,6 +227,38 @@ func TestCoinSend(t *testing.T) { }`, body) } +func TestTxs(t *testing.T) { + kill, port, seed := junkInit(t) + defer kill() + + // query wrong + res, body := request(t, port, "GET", "/txs", nil) + require.Equal(t, http.StatusBadRequest, res.StatusCode, body) + + // query empty + res, body = request(t, port, "GET", fmt.Sprintf("/txs?tag=coin.sender='%s'", "8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6"), nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + assert.Equal(t, "[]", body) + + // create TX + addr, receiveAddr := doSend(t, port, seed) + + // query sender + res, body = request(t, port, "GET", fmt.Sprintf("/txs?tag=coin.sender='%s'", addr), nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + assert.NotEqual(t, "[]", body) + + // query receiver + res, body = request(t, port, "GET", fmt.Sprintf("/txs?tag=coin.receiver='%s'", receiveAddr), nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + assert.NotEqual(t, "[]", body) + + // get TX by hash +} + //__________________________________________________________ // helpers @@ -283,3 +293,25 @@ func request(t *testing.T, port, method, path string, payload []byte) (*http.Res return res, string(output) } + +func doSend(t *testing.T, port, seed string) (sendAddr string, receiveAddr string) { + // create account from seed who has keys + var jsonStr = []byte(fmt.Sprintf(`{"name":"test", "password":"1234567890", "seed": "%s"}`, seed)) + res, body := request(t, port, "POST", "/keys", jsonStr) + + assert.Equal(t, http.StatusOK, res.StatusCode, body) + sendAddr = body + + // create receive address + kb := client.MockKeyBase() + receiveInfo, _, err := kb.Create("receive_address", "1234567890", cryptoKeys.CryptoAlgo("ed25519")) + require.Nil(t, err) + receiveAddr = receiveInfo.PubKey.Address().String() + + // send + jsonStr = []byte(`{ "name":"test", "password":"1234567890", "amount":[{ "denom": "mycoin", "amount": 1 }] }`) + res, body = request(t, port, "POST", "/accounts/"+receiveAddr+"/send", jsonStr) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + return sendAddr, receiveAddr +} diff --git a/client/tx/root.go b/client/tx/root.go index fd8b3f0975..477259e97b 100644 --- a/client/tx/root.go +++ b/client/tx/root.go @@ -24,6 +24,6 @@ func AddCommands(cmd *cobra.Command, cdc *wire.Codec) { func RegisterRoutes(r *mux.Router, cdc *wire.Codec) { r.HandleFunc("/txs", SearchTxRequestHandler(cdc)).Methods("GET") r.HandleFunc("/txs/{hash}", QueryTxRequestHandler(cdc)).Methods("GET") - r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST") - r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST") + // r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST") + // r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST") } diff --git a/client/tx/search.go b/client/tx/search.go index 90f89e3e2c..2790750ebe 100644 --- a/client/tx/search.go +++ b/client/tx/search.go @@ -98,6 +98,12 @@ func SearchTxRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Req c := commander{cdc} return func(w http.ResponseWriter, r *http.Request) { tag := r.FormValue("tag") + if tag == "" { + w.WriteHeader(400) + w.Write([]byte("You need to provide a tag to search for.")) + return + } + tags := []string{tag} output, err := c.searchTx(tags) if err != nil { diff --git a/examples/basecoin/x/cool/commands/tx.go b/examples/basecoin/x/cool/commands/tx.go index 2b16546806..f06eb8af42 100644 --- a/examples/basecoin/x/cool/commands/tx.go +++ b/examples/basecoin/x/cool/commands/tx.go @@ -5,7 +5,9 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/builder" "github.com/cosmos/cosmos-sdk/wire" @@ -31,8 +33,19 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command { // create the message msg := cool.NewQuizMsg(from, args[0]) + // get account name + name := viper.GetString(client.FlagName) + + // get password + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password to sign with '%s':", name) + passphrase, err := client.GetPassword(prompt, buf) + if err != nil { + return err + } + // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(msg, cdc) + res, err := builder.SignBuildBroadcast(name, passphrase, msg, cdc) if err != nil { return err } @@ -59,11 +72,22 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command { return err } + // get account name + name := viper.GetString(client.FlagName) + + // get password + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password to sign with '%s':", name) + passphrase, err := client.GetPassword(prompt, buf) + if err != nil { + return err + } + // create the message msg := cool.NewSetTrendMsg(from, args[0]) // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(msg, cdc) + res, err := builder.SignBuildBroadcast(name, passphrase, msg, cdc) if err != nil { return err } diff --git a/x/auth/rest/query.go b/x/auth/rest/query.go index dbb53834d8..5629f15476 100644 --- a/x/auth/rest/query.go +++ b/x/auth/rest/query.go @@ -25,8 +25,6 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, parser sdk.Pa vars := mux.Vars(r) addr := vars["address"] - fmt.Println("ADDR", addr) - bz, err := hex.DecodeString(addr) if err != nil { w.WriteHeader(http.StatusBadRequest) diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index 5b692acec5..8e6b10dc78 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -7,6 +7,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "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" @@ -58,14 +59,22 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error { } to := sdk.Address(bz) - // build send msg - msg, err := buildMsg(from) + // get account name + name := viper.GetString(client.FlagName) + + // get password + buf := client.BufferStdin() + prompt := fmt.Sprintf("Password to sign with '%s':", name) + passphrase, err := client.GetPassword(prompt, buf) if err != nil { return err } + // build message + msg := BuildMsg(from, to, coins) + // build and sign the transaction, then broadcast to Tendermint - res, err := builder.SignBuildBroadcast(msg, c.cdc) + res, err := builder.SignBuildBroadcast(name, passphrase, msg, c.Cdc) if err != nil { return err } diff --git a/x/bank/rest/sendtx.go b/x/bank/rest/sendtx.go index 7ce99dadd8..8fd743f515 100644 --- a/x/bank/rest/sendtx.go +++ b/x/bank/rest/sendtx.go @@ -3,11 +3,13 @@ package rest import ( "encoding/hex" "encoding/json" + "io/ioutil" "net/http" "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/builder" "github.com/cosmos/cosmos-sdk/client/keys" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" @@ -17,9 +19,11 @@ import ( type SendBody struct { // fees is not used currently // Fees sdk.Coin `json="fees"` - Amount sdk.Coins `json="amount"` - LocalAccountName string `json="account"` - Password string `json="password"` + Amount sdk.Coins `json:"amount"` + LocalAccountName string `json:"name"` + Password string `json:"password"` + ChainID string `json:"chain_id"` + Sequence string `json:"sequence"` } func SendRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request) { @@ -30,8 +34,13 @@ func SendRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request address := vars["address"] var m SendBody - decoder := json.NewDecoder(r.Body) - err := decoder.Decode(&m) + 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())) @@ -60,16 +69,22 @@ func SendRequestHandler(cdc *wire.Codec) func(http.ResponseWriter, *http.Request } to := sdk.Address(bz) - // build - msg := commands.BuildMsg(info.Address(), to, m.Amount) + // build message + msg := commands.BuildMsg(info.PubKey.Address(), to, m.Amount) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) return } + signMsg := sdk.StdSignMsg{ + ChainID: m.ChainID, + Sequences: []int64{m.Sequence}, + Msg: msg, + } + // sign - txBytes, err := c.SignMessage(msg, kb, m.LocalAccountName, m.Password) + txBytes, err := builder.SignAndBuild(m.LocalAccountName, m.Password, signMsg, c.Cdc) if err != nil { w.WriteHeader(http.StatusUnauthorized) w.Write([]byte(err.Error()))