Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R]: add swagger-ui for gov, stake and slashing #2462

Merged
merged 28 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
126dc8a
Add swagger-ui for gov, stake and slashing
Oct 9, 2018
d8852d7
Add json consume
Oct 9, 2018
eadaf43
Add json to response header
Oct 10, 2018
1774658
Fix some bugs in stake APIs
Oct 10, 2018
da7dc49
Refactor swagger.ymal, remove definitions which are only referenced once
Oct 10, 2018
e529488
Remove duplicated json header
Oct 10, 2018
66eb6f9
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into i…
Oct 10, 2018
4ba59c7
refactor query validator set response
Oct 10, 2018
6e1a4d1
Refactor swagger.yaml according code reviewer feedback
Oct 10, 2018
ba4eadd
Add response model
Oct 11, 2018
cde3db1
Merge with develop
Oct 18, 2018
4c08aed
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into i…
Oct 19, 2018
e050d89
Fix lcd test failure: TestUnjail
Oct 19, 2018
9db8dc2
Add response mode and add error code for all query endpoints
Oct 19, 2018
5270da6
fix ci test failure
Oct 19, 2018
6ec2d88
Resolve some spell error, add missing gov endpoint and modify swagger…
Oct 19, 2018
9136254
Fix wrong response body
Oct 19, 2018
131e0ca
Resolve inconsistence between swagger.yaml and code implementation
Oct 19, 2018
2f09cd7
Merge with develop
Oct 20, 2018
eb7d3df
Update swagger.yaml to accommodate handler implementation changes
Oct 20, 2018
2b267fc
Fix bug in normalize gov parameters
Oct 20, 2018
8363685
Add example value to proposal type and vote option
Oct 20, 2018
88b4c66
Improve error handler in delete and update key
Oct 20, 2018
4631c0b
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into i…
Oct 22, 2018
8045d49
Implement error type in key base
Oct 23, 2018
d88a213
Add /stake/validators/{validatorAddr}/unbonding_delegations and /stak…
Oct 23, 2018
6d6b58a
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into i…
Oct 24, 2018
fb04cfb
Add change to about split issue 2258 to swagger.yaml
Oct 24, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/crypto/keys"
keyerror "github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
"github.com/gorilla/mux"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -81,11 +82,11 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
}

err = kb.Delete(name, m.Password)
if IsKeyNotFoundErr(err, name) {
if keyerror.IsErrKeyNotFound(err) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
return
} else if IsWrongKeyPasswordErr(err) {
} else if keyerror.IsErrWrongPassword(err) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(err.Error()))
return
Expand Down
3 changes: 2 additions & 1 deletion client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
)

const (
Expand Down Expand Up @@ -108,7 +109,7 @@ func GetKeyRequestHandler(indent bool) http.HandlerFunc {
}

info, err := GetKeyInfo(name)
if IsKeyNotFoundErr(err, name) {
if keyerror.IsErrKeyNotFound(err) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
return
Expand Down
5 changes: 3 additions & 2 deletions client/keys/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gorilla/mux"

"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
)

func updateKeyCommand() *cobra.Command {
Expand Down Expand Up @@ -84,11 +85,11 @@ func UpdateKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
getNewpass := func() (string, error) { return m.NewPassword, nil }

err = kb.Update(name, m.OldPassword, getNewpass)
if IsKeyNotFoundErr(err, name) {
if keyerror.IsErrKeyNotFound(err) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
return
} else if IsWrongKeyPasswordErr(err) {
} else if keyerror.IsErrWrongPassword(err) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(err.Error()))
return
Expand Down
17 changes: 0 additions & 17 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"net/http"
"strings"
)

// KeyDBName is the directory under root where we store the keys
Expand Down Expand Up @@ -257,19 +256,3 @@ func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response inter
w.Header().Set("Content-Type", "application/json")
w.Write(output)
}

// IsKeyNotFoundErr - check if the error means that the specified key doesn't exist
func IsKeyNotFoundErr(err error, name string) bool {
if err != nil && strings.Contains(err.Error(), fmt.Sprintf("Key %s not found", name)) {
return true
}
return false
}

// IsWrongKeyPasswordErr - check if the error means that the specified key password is wrong
func IsWrongKeyPasswordErr(err error) bool {
if err != nil && strings.Contains(err.Error(), "Ciphertext decryption failed") {
return true
}
return false
}
23 changes: 4 additions & 19 deletions client/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
Expand Down Expand Up @@ -229,14 +230,14 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
}

txBytes, err := txBldr.BuildAndSign(baseReq.Name, baseReq.Password, msgs)
if IsKeyNotFoundErr(err, baseReq.Name) {
if keyerror.IsErrKeyNotFound(err) {
WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
} else if IsWrongKeyPasswordErr(err) {
} else if keyerror.IsErrWrongPassword(err) {
WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
return
} else if err != nil {
WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand Down Expand Up @@ -270,19 +271,3 @@ func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response inter
w.Header().Set("Content-Type", "application/json")
w.Write(output)
}

// IsKeyNotFoundErr - check if the error means that the specified key doesn't exist
func IsKeyNotFoundErr(err error, name string) bool {
if err != nil && strings.Contains(err.Error(), fmt.Sprintf("Key %s not found", name)) {
return true
}
return false
}

// IsWrongKeyPasswordErr - check if the error means that the specified key password is wrong
func IsWrongKeyPasswordErr(err error) bool {
if err != nil && strings.Contains(err.Error(), "Ciphertext decryption failed") {
return true
}
return false
}
3 changes: 2 additions & 1 deletion crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/tendermint/tendermint/crypto/secp256k1"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
)

var _ Keybase = dbKeybase{}
Expand Down Expand Up @@ -217,7 +218,7 @@ func (kb dbKeybase) List() ([]Info, error) {
func (kb dbKeybase) Get(name string) (Info, error) {
bs := kb.db.Get(infoKey(name))
if len(bs) == 0 {
return nil, fmt.Errorf("Key %s not found", name)
return nil, keyerror.NewErrKeyNotFound(name)
}
return readInfo(bs)
}
Expand Down
81 changes: 81 additions & 0 deletions crypto/keys/keyerror/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package keyerror
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
)

const (
codeKeyNotFound = 1
codeWrongPassword = 2
)

type keybaseError interface {
error
Code() int
}

type errKeyNotFound struct {
code int
name string
}

func (e errKeyNotFound) Code() int {
return e.code
}

func (e errKeyNotFound) Error() string {
return fmt.Sprintf("Key %s not found", e.name)
}

// NewErrKeyNotFound returns a standardized error reflecting that the specified key doesn't exist
func NewErrKeyNotFound(name string) error {
return errKeyNotFound{
code: codeKeyNotFound,
name: name,
}
}

// IsErrKeyNotFound returns true if the given error is errKeyNotFound
func IsErrKeyNotFound(err error) bool {
if err == nil {
return false
}
if keyErr, ok := err.(keybaseError); ok {
if keyErr.Code() == codeKeyNotFound {
return true
}
}
return false
}

type errWrongPassword struct {
code int
}

func (e errWrongPassword) Code() int {
return e.code
}

func (e errWrongPassword) Error() string {
return fmt.Sprintf("Ciphertext decryption failed")
}

// NewErrWrongPassword returns a standardized error reflecting that the specified password is wrong
func NewErrWrongPassword() error {
return errWrongPassword{
code: codeWrongPassword,
}
}

// IsErrWrongPassword returns true if the given error is errWrongPassword
func IsErrWrongPassword(err error) bool {
if err == nil {
return false
}
if keyErr, ok := err.(keybaseError); ok {
if keyErr.Code() == codeWrongPassword {
return true
}
}
return false
}
5 changes: 4 additions & 1 deletion crypto/keys/mintkey/mintkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto/xsalsa20symmetric"

cmn "github.com/tendermint/tendermint/libs/common"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
)

const (
Expand Down Expand Up @@ -144,7 +145,9 @@ func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privK
}
key = crypto.Sha256(key) // Get 32 bytes
privKeyBytes, err := xsalsa20symmetric.DecryptSymmetric(encBytes, key)
if err != nil {
if err != nil && err.Error() == "Ciphertext decryption failed" {
return privKey, keyerror.NewErrWrongPassword()
} else if err != nil {
return privKey, err
}
privKey, err = cryptoAmino.PrivKeyFromBytes(privKeyBytes)
Expand Down
5 changes: 3 additions & 2 deletions x/auth/client/rest/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
)

// SignBody defines the properties of a sign request's body.
Expand Down Expand Up @@ -47,10 +48,10 @@ func SignTxRequestHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
}

signedTx, err := txBldr.SignStdTx(m.LocalAccountName, m.Password, m.Tx, m.AppendSig)
if utils.IsKeyNotFoundErr(err, m.LocalAccountName) {
if keyerror.IsErrKeyNotFound(err) {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
} else if utils.IsWrongKeyPasswordErr(err) {
} else if keyerror.IsErrWrongPassword(err) {
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
return
} else if err != nil {
Expand Down