adjusted sending + tx tests
This commit is contained in:
parent
1cd6ec1084
commit
cf6f04978c
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()))
|
||||
|
|
Loading…
Reference in New Issue