Skip to content

Commit

Permalink
Merge branch 'develop' into matt/ibc-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman authored Mar 19, 2018
2 parents 77b2a63 + 2721307 commit 8231fd4
Show file tree
Hide file tree
Showing 66 changed files with 3,923 additions and 622 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ docs/_build
.DS_Store
coverage.txt
profile.out

.vscode

### Vagrant ###
.vagrant/
Expand Down
14 changes: 13 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
name = "github.com/tendermint/tendermint"

[[override]]
branch = "develop"
branch = "rigel/cli-refactor"
name = "github.com/tendermint/tmlibs"

[prune]
Expand Down
10 changes: 7 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte, tx sdk.Tx) (result sdk
}
}

// Match route.
msgType := msg.Type()
handler := app.router.Route(msgType)
if handler == nil {
return sdk.ErrUnknownRequest("Unrecognized Msg type: " + msgType).Result()
}

// Get the correct cache
var msCache sdk.CacheMultiStore
if isCheckTx == true {
Expand All @@ -384,9 +391,6 @@ func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte, tx sdk.Tx) (result sdk

}

// Match and run route.
msgType := msg.Type()
handler := app.router.Route(msgType)
result = handler(ctx, msg)

// If result was successful, write to app.checkState.ms or app.deliverState.ms
Expand Down
1 change: 0 additions & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ func (tx testUpdatePowerTx) GetMsg() sdk.Msg { return tx
func (tx testUpdatePowerTx) GetSignBytes() []byte { return nil }
func (tx testUpdatePowerTx) ValidateBasic() sdk.Error { return nil }
func (tx testUpdatePowerTx) GetSigners() []sdk.Address { return nil }
func (tx testUpdatePowerTx) GetFeePayer() sdk.Address { return nil }
func (tx testUpdatePowerTx) GetSignatures() []sdk.StdSignature { return nil }

func TestValidatorChange(t *testing.T) {
Expand Down
27 changes: 15 additions & 12 deletions client/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,25 @@ 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)
sequence := int64(viper.GetInt(client.FlagSequence))
signMsg := sdk.StdSignMsg{
ChainID: chainID,
Sequences: []int64{sequence},
Msg: msg,
}

keybase, err := keys.GetKeyBase()
if err != nil {
return nil, err
}
name := viper.GetString(client.FlagName)

// sign and build
bz := msg.GetSignBytes()
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err := client.GetPassword(prompt, buf)
if err != nil {
return nil, err
}
bz := signMsg.Bytes()

sig, pubkey, err := keybase.Sign(name, passphrase, bz)
if err != nil {
return nil, err
Expand All @@ -115,14 +118,14 @@ func SignAndBuild(msg sdk.Msg, cdc *wire.Codec) ([]byte, error) {
}}

// marshal bytes
tx := sdk.NewStdTx(msg, sigs)
tx := sdk.NewStdTx(signMsg.Msg, signMsg.Fee, sigs)

return cdc.MarshalBinary(tx)
}

// 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
}
Expand Down
3 changes: 2 additions & 1 deletion client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
dbm "github.com/tendermint/tmlibs/db"
)

// GetKeyBase initializes a keybase based on the configuration
// GetKeyBase initializes a keybase based on the given db.
// The KeyBase manages all activity requiring access to a key.
func GetKeyBase(db dbm.DB) keys.Keybase {
keybase := keys.New(
db,
Expand Down
90 changes: 90 additions & 0 deletions client/keys/add.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package keys

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -120,3 +124,89 @@ func printCreate(info keys.Info, seed string) {
panic(fmt.Sprintf("I can't speak: %s", output))
}
}

// REST

type NewKeyBody struct {
Name string `json:"name"`
Password string `json:"password"`
Seed string `json:"seed"`
}

func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
var kb keys.Keybase
var m NewKeyBody

kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}

body, err := ioutil.ReadAll(r.Body)
err = json.Unmarshal(body, &m)

if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if m.Name == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("You have to specify a name for the locally stored account."))
return
}
if m.Password == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("You have to specify a password for the locally stored account."))
return
}
if m.Seed == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("You have to specify a seed for the locally stored account."))
return
}

// check if already exists
infos, err := kb.List()
for _, i := range infos {
if i.Name == m.Name {
w.WriteHeader(http.StatusConflict)
w.Write([]byte(fmt.Sprintf("Account with name %s already exists.", m.Name)))
return
}
}

// create account
info, err := kb.Recover(m.Name, m.Password, m.Seed)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(info.PubKey.Address().String()))
}

// function to just a new seed to display in the UI before actually persisting it in the keybase
func getSeed(algo keys.CryptoAlgo) string {
kb := client.MockKeyBase()
pass := "throwing-this-key-away"
name := "inmemorykey"

_, seed, _ := kb.Create(name, pass, algo)
return seed
}

func SeedRequestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
algoType := vars["type"]
// algo type defaults to ed25519
if algoType == "" {
algoType = "ed25519"
}
algo := keys.CryptoAlgo(algoType)

seed := getSeed(algo)
w.Write([]byte(seed))
}
42 changes: 42 additions & 0 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package keys

import (
"encoding/json"
"fmt"
"net/http"

"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
"github.com/pkg/errors"
keys "github.com/tendermint/go-crypto/keys"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -43,3 +47,41 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
fmt.Println("Password deleted forever (uh oh!)")
return nil
}

// REST

type DeleteKeyBody struct {
Password string `json:"password"`
}

func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
var kb keys.Keybase
var m DeleteKeyBody

decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}

kb, err = GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}

// TODO handle error if key is not available or pass is wrong
err = kb.Delete(name, m.Password)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}

w.WriteHeader(200)
}
42 changes: 41 additions & 1 deletion client/keys/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package keys

import "github.com/spf13/cobra"
import (
"encoding/json"
"net/http"

"github.com/spf13/cobra"
)

// CMD

// listKeysCmd represents the list command
var listKeysCmd = &cobra.Command{
Expand All @@ -23,3 +30,36 @@ func runListCmd(cmd *cobra.Command, args []string) error {
}
return err
}

//REST

func QueryKeysRequestHandler(w http.ResponseWriter, r *http.Request) {
kb, err := GetKeyBase()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
infos, err := kb.List()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
// an empty list will be JSONized as null, but we want to keep the empty list
if len(infos) == 0 {
w.Write([]byte("[]"))
return
}
keysOutput := make([]KeyOutput, len(infos))
for i, info := range infos {
keysOutput[i] = KeyOutput{Name: info.Name, Address: info.PubKey.Address().String()}
}
output, err := json.MarshalIndent(keysOutput, "", " ")
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
}
10 changes: 10 additions & 0 deletions client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keys

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
)

Expand All @@ -27,3 +28,12 @@ func Commands() *cobra.Command {
)
return cmd
}

func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/keys", QueryKeysRequestHandler).Methods("GET")
r.HandleFunc("/keys", AddNewKeyRequestHandler).Methods("POST")
r.HandleFunc("/keys/seed", SeedRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", GetKeyRequestHandler).Methods("GET")
r.HandleFunc("/keys/{name}", UpdateKeyRequestHandler).Methods("PUT")
r.HandleFunc("/keys/{name}", DeleteKeyRequestHandler).Methods("DELETE")
}
Loading

0 comments on commit 8231fd4

Please sign in to comment.