Skip to content

Commit

Permalink
update /server
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Nov 3, 2020
1 parent faa822a commit 2324c4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
12 changes: 5 additions & 7 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ required through --multisig-threshold. The keys are sorted by address, unless
the flag --nosort is set.
`,
Args: cobra.ExactArgs(1),
RunE: runAddCmd,
RunE: runAddCmdPrepare,
}
flags := cmd.Flags()
flags.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)")
Expand All @@ -83,7 +83,7 @@ the flag --nosort is set.
return cmd
}

func runAddCmd(cmd *cobra.Command, args []string) error {
func runAddCmdPrepare(cmd *cobra.Command, args []string) error {
buf := bufio.NewReader(cmd.InOrStdin())
clientCtx := client.GetClientContextFromCmd(cmd)

Expand All @@ -104,7 +104,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
return err
}

return RunAddCmd(cmd, args, kr, buf)
return runAddCmd(clientCtx, cmd, args, kr, buf)
}

/*
Expand All @@ -116,7 +116,7 @@ input
output
- armor encrypted private key (saved to file)
*/
func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error {
func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error {
var err error

name := args[0]
Expand Down Expand Up @@ -188,11 +188,9 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *buf
if pubKey != "" {
var pk crypto.PubKey
// TODO: shall we use KeysCdc here (global from this module, = codec.NewLegacyAmino)?
marshaller := client.GetClientContextFromCmd(cmd).JSONMarshaler
if err := marshaller.UnmarshalJSON([]byte(pubKey), &pk); err != nil {
if err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pubKey), &pk); err != nil {
return err
}

_, err := kb.SavePubKey(name, pk, algo.Name())
return err
}
Expand Down
15 changes: 13 additions & 2 deletions codec/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package codec
import (
"bytes"

"github.com/cosmos/cosmos-sdk/codec/types"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded
Expand All @@ -28,3 +29,13 @@ func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, e

return buf.Bytes(), nil
}

// ProtoMarshalJSONI same as ProtoMarshalJSON, but does msg type inspection to assert
// that it implements `proto.Message` and return an error if it doesn't.
func ProtoMarshalJSONI(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) {
msgProto, ok := msg.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", msgProto)
}
return ProtoMarshalJSON(msgProto, resolver)
}
25 changes: 7 additions & 18 deletions server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/tendermint/tendermint/p2p"
pvm "github.com/tendermint/tendermint/privval"
tversion "github.com/tendermint/tendermint/version"
"google.golang.org/protobuf/proto"
yaml "gopkg.in/yaml.v2"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -30,11 +29,9 @@ func ShowNodeIDCmd() *cobra.Command {
cfg := serverCtx.Config

nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
if err == nil {
fmt.Println(nodeKey.ID())
}

fmt.Println(nodeKey.ID())
return nil
},
}
Expand All @@ -50,29 +47,19 @@ func ShowValidatorCmd() *cobra.Command {
cfg := serverCtx.Config

privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valPubKey, err := privValidator.GetPubKey()
pk, err := privValidator.GetPubKey()
if err != nil {
return err
}

output, _ := cmd.Flags().GetString(cli.OutputFlag)
if strings.ToLower(output) == "json" {
return printlnJSON(valPubKey)
}

pk, ok := valPubKey.(proto.Message)

pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey)
out, err := codec.ProtoMarshalJSONI(pk, nil)
if err != nil {
return err
}

fmt.Println(pubkey)
fmt.Println(out)
return nil
},
}

cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)")
return &cmd
}

Expand Down Expand Up @@ -132,6 +119,8 @@ against which this app has been compiled.
}
}

// Deprecated: prints the content to the standard output using Legacy Amino
// TODO: add issue to trace it?
func printlnJSON(v interface{}) error {
cdc := codec.NewLegacyAmino()
cryptocodec.RegisterCrypto(cdc)
Expand Down

0 comments on commit 2324c4e

Please sign in to comment.