adjusted sending + tx tests

This commit is contained in:
Fabian Weber
2018-03-17 22:17:56 +01:00
committed by Ethan Buchman
parent 1cd6ec1084
commit cf6f04978c
8 changed files with 131 additions and 54 deletions
+3 -10
View File
@@ -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
}
+59 -27
View File
@@ -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
}
+2 -2
View File
@@ -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")
}
+6
View File
@@ -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 {