Make gaiacli keys show multisig-ready

This commit is contained in:
Alessio Treglia 2018-10-22 11:50:21 -07:00
parent 5aae4740db
commit 322a029ab5
No known key found for this signature in database
GPG Key ID: E8A48AE5311D765A
1 changed files with 45 additions and 9 deletions

View File

@ -2,6 +2,8 @@ package keys
import ( import (
"fmt" "fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/tendermint/tendermint/crypto"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -9,6 +11,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/crypto/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
) )
const ( const (
@ -18,31 +22,63 @@ const (
FlagPublicKey = "pubkey" FlagPublicKey = "pubkey"
// FlagBechPrefix defines a desired Bech32 prefix encoding for a key. // FlagBechPrefix defines a desired Bech32 prefix encoding for a key.
FlagBechPrefix = "bech" FlagBechPrefix = "bech"
flagMultiSigThreshold = "multisig-threshold"
) )
var _ keys.Info = (keys.Info)(nil)
type multiSigKey struct {
name string
key crypto.PubKey
}
func (m multiSigKey) GetName() string { return m.name }
func (m multiSigKey) GetType() keys.KeyType { return keys.TypeLocal }
func (m multiSigKey) GetPubKey() crypto.PubKey { return m.key }
func (m multiSigKey) GetAddress() sdk.AccAddress { return sdk.AccAddress(m.key.Address())}
func showKeysCmd() *cobra.Command { func showKeysCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "show [name]", Use: "show [name]",
Short: "Show key info for the given name", Short: "Show key info for the given name",
Long: `Return public details of one local key.`, Long: `Return public details of one local key.`,
Args: cobra.ExactArgs(1), Args: cobra.MinimumNArgs(1),
RunE: runShowCmd, RunE: runShowCmd,
} }
cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)") cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)")
cmd.Flags().Bool(FlagAddress, false, "output the address only (overrides --output)") cmd.Flags().Bool(FlagAddress, false, "output the address only (overrides --output)")
cmd.Flags().Bool(FlagPublicKey, false, "output the public key only (overrides --output)") cmd.Flags().Bool(FlagPublicKey, false, "output the public key only (overrides --output)")
cmd.Flags().UintP(flagMultiSigThreshold, "m", 1, "K out of N required signatures")
return cmd return cmd
} }
func runShowCmd(cmd *cobra.Command, args []string) error { func runShowCmd(cmd *cobra.Command, args []string) (err error) {
name := args[0] var info keys.Info
info, err := GetKeyInfo(name) if len(args) == 1 {
info, err = GetKeyInfo(args[0])
if err != nil { if err != nil {
return err return err
} }
} else {
pks := make([]crypto.PubKey, len(args))
for i, keyName := range args {
info, err := GetKeyInfo(keyName)
if err != nil {
return err
}
pks[i] = info.GetPubKey()
}
multikey := multisig.NewPubKeyMultisigThreshold(viper.GetInt(flagMultiSigThreshold), pks)
info = multiSigKey{
name: "multi",
key: multikey,
}
}
isShowAddr := viper.GetBool(FlagAddress) isShowAddr := viper.GetBool(FlagAddress)
isShowPubKey := viper.GetBool(FlagPublicKey) isShowPubKey := viper.GetBool(FlagPublicKey)