merge with develop

This commit is contained in:
HaoyangLiu
2018-09-13 23:09:17 +08:00
parent 0c171ec761
commit c2668335a7
12 changed files with 84 additions and 229 deletions
-2
View File
@@ -22,7 +22,6 @@ func Commands() *cobra.Command {
addKeyCommand(),
listKeysCmd,
showKeysCmd(),
signCommand(),
client.LineBreak,
deleteKeyCommand(),
updateKeyCommand(),
@@ -35,7 +34,6 @@ func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/keys", QueryKeysRequestHandler).Methods("GET")
r.HandleFunc("/keys", AddNewKeyRequestHandler).Methods("POST")
r.HandleFunc("/keys/{name}/recover", RecoverKeyRequestHandler).Methods("POST")
r.HandleFunc("/keys/{name}/sign", SignResuest).Methods("POST")
r.HandleFunc("/keys/{name}", GetKeyRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", UpdateKeyRequestHandler).Methods("PUT")
r.HandleFunc("/keys/{name}", DeleteKeyRequestHandler).Methods("DELETE")
-98
View File
@@ -1,98 +0,0 @@
package keys
import (
"encoding/base64"
"fmt"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
)
const (
flagFrom = "from"
flagPassword = "password"
flagTx = "tx"
)
func signCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sign",
Short: "Sign user specified data",
Long: `Sign user data with specified key and password`,
RunE: runSignCmd,
}
cmd.Flags().String(flagFrom, "", "Name of private key with which to sign")
cmd.Flags().String(flagPassword, "", "Password of private key")
cmd.Flags().String(flagTx, "", "Base64 encoded tx data for sign")
return cmd
}
func runSignCmd(cmd *cobra.Command, args []string) error {
name := viper.GetString(flagFrom)
password := viper.GetString(flagPassword)
tx := viper.GetString(flagTx)
decodedTx, err := base64.StdEncoding.DecodeString(tx)
if err != nil {
return err
}
kb, err := GetKeyBase()
if err != nil {
return err
}
sig, _, err := kb.Sign(name, password, decodedTx)
if err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(sig)
fmt.Println(string(encoded))
return nil
}
type keySignBody struct {
Tx []byte `json:"tx_bytes"`
Password string `json:"password"`
}
// SignResuest is the handler of creating seed in swagger rest server
func SignResuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
var m keySignBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
sig, _, err := kb.Sign(name, m.Password, m.Tx)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
encoded := base64.StdEncoding.EncodeToString(sig)
w.Write([]byte(encoded))
}
+5 -4
View File
@@ -238,7 +238,7 @@ func TestCoinSend(t *testing.T) {
someFakeAddr := sdk.AccAddress(bz)
// query empty
res, body := Request(t, port, "GET", fmt.Sprintf("/auth/accounts/%s", someFakeAddr), nil)
res, body := Request(t, port, "GET", fmt.Sprintf("/accounts/%s", someFakeAddr), nil)
require.Equal(t, http.StatusNoContent, res.StatusCode, body)
acc := getAccount(t, port, addr)
@@ -368,7 +368,8 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
// broadcast tx
broadcastPayload := struct {
Tx auth.StdTx `json:"tx"`
}{Tx: signedMsg}
Return string `json:"return"`
}{Tx: signedMsg, Return: "block"}
json, err = cdc.MarshalJSON(broadcastPayload)
require.Nil(t, err)
res, body = Request(t, port, "POST", "/tx/broadcast", json)
@@ -794,7 +795,7 @@ func TestProposalsQuery(t *testing.T) {
//_____________________________________________________________________________
// get the account to get the sequence
func getAccount(t *testing.T, port string, addr sdk.AccAddress) auth.Account {
res, body := Request(t, port, "GET", fmt.Sprintf("/auth/accounts/%s", addr), nil)
res, body := Request(t, port, "GET", fmt.Sprintf("/accounts/%s", addr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var acc auth.Account
err := cdc.UnmarshalJSON([]byte(body), &acc)
@@ -842,7 +843,7 @@ func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.Acc
"chain_id":"%s"
}`, gasStr, gasAdjustmentStr, name, password, accnum, sequence, coinbz, chainID))
res, body = Request(t, port, "POST", fmt.Sprintf("/bank/%s/transfers%v", receiveAddr, queryStr), jsonStr)
res, body = Request(t, port, "POST", fmt.Sprintf("/accounts/%s/send%v", receiveAddr, queryStr), jsonStr)
return
}
-1
View File
@@ -165,7 +165,6 @@ func createLiteHandler(cdc *wire.Codec) *mux.Router {
r.HandleFunc("/version", CLIVersionRequestHandler).Methods("GET")
r.HandleFunc("/node_version", NodeVersionRequestHandler(cliCtx)).Methods("GET")
tx.RegisterLiteRoutes(cliCtx, r, cdc)
bank.RegisterLiteRoutes(cliCtx, r, cdc, kb)
return r
+15 -68
View File
@@ -1,90 +1,37 @@
package tx
import (
"encoding/json"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire"
"io/ioutil"
)
const (
flagSync = "sync"
flagAsync = "async"
flagBlock = "block"
)
// Tx Broadcast Body
// BroadcastBody contains the data of tx and specify how to broadcast tx
type BroadcastBody struct {
Transaction string `json:"transaction"`
Return string `json:"return"`
type BroadcastTxBody struct {
TxBytes string `json:"tx"`
}
// BroadcastTxRequestHandlerFn REST Handler
// nolint: gocyclo
func BroadcastTxRequestHandlerFn(cdc *wire.Codec, ctx context.CLIContext) http.HandlerFunc {
// BroadcastTx REST Handler
func BroadcastTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var txBody BroadcastBody
body, err := ioutil.ReadAll(r.Body)
var m BroadcastTxBody
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
err = cdc.UnmarshalJSON(body, &txBody)
res, err := cliCtx.BroadcastTx([]byte(m.TxBytes))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
var output []byte
switch txBody.Return {
case flagBlock:
res, err := ctx.BroadcastTx([]byte(txBody.Transaction))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
output, err = cdc.MarshalJSON(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
case flagSync:
res, err := ctx.BroadcastTxSync([]byte(txBody.Transaction))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
output, err = cdc.MarshalJSON(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
case flagAsync:
res, err := ctx.BroadcastTxAsync([]byte(txBody.Transaction))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
output, err = cdc.MarshalJSON(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
default:
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("unsupported return type. supported types: block, sync, async"))
return
}
w.WriteHeader(http.StatusOK)
w.Write(output)
w.Write([]byte(string(res.Height)))
}
}
-5
View File
@@ -23,8 +23,3 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec) {
// r.HandleFunc("/txs/sign", SignTxRequstHandler).Methods("POST")
// r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandler).Methods("POST")
}
// RegisterLiteRoutes registers REST routes to gaia-lite
func RegisterLiteRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/txs", BroadcastTxRequestHandlerFn(cdc, cliCtx)).Methods("POST")
}