Skip to content

Commit

Permalink
feat(cli): add did:key to keys list command
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jan 29, 2024
1 parent 39c87b7 commit 30a5615
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 11 deletions.
6 changes: 2 additions & 4 deletions client/keys/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
"github.com/okp4/okp4d/x/logic/util"
)

var (
flagPubKeyType = "type"
)
var flagPubKeyType = "type"

func DIDCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -61,7 +59,7 @@ $ %s keys did 02d0fe99b214aaeeb5e46ae4d65a1623a95d9d0cecd57d673f7e4c9a19ac0752bc
return nil
},
}
cmd.Flags().StringP(flagPubKeyType, "t", util.KeyAlgSecp256r1.String(),
cmd.Flags().StringP(flagPubKeyType, "t", util.KeyAlgSecp256k1.String(),
fmt.Sprintf("Pubkey type to decode (oneof %s, %s)", util.KeyAlgEd25519, util.KeyAlgSecp256k1))
return cmd
}
Expand Down
133 changes: 133 additions & 0 deletions client/keys/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package keys

import (
"encoding/json"
"fmt"
"io"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"

"github.com/okp4/okp4d/x/logic/util"
)

const (
flagListNames = "list-names"
ListCmdName = "list"
)

// KeyOutput defines a structure wrapping around an Info object used for output
// functionality.
type KeyOutput struct {
keys.KeyOutput
DID string `json:"did,omitempty" yaml:"did"`
}

// ListKeysCmd lists all keys in the key store with additional info, such as the did:key equivalent of the public key.
// This is an improved copy of the ListKeysCmd from the keys module.
func ListKeysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: ListCmdName,
Short: "List all keys",
Long: `Return a list of all public keys stored by this key manager
along with their associated name, address and decentralized identifier (for supported public key algorithms)`,
RunE: runListCmd,
}

cmd.Flags().BoolP(flagListNames, "n", false, "List names only")
return cmd
}

func runListCmd(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

records, err := clientCtx.Keyring.List()
if err != nil {
return err
}

if len(records) == 0 && clientCtx.OutputFormat == flags.OutputFormatText {
cmd.Println("No records were found in keyring")
return nil
}

if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
}

for _, k := range records {
cmd.Println(k.Name)
}

return nil
}

func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output string) error {
kos, err := mkKeyOutput(records)
if err != nil {
return err
}

switch output {
case flags.OutputFormatText:
if err := printTextRecords(w, kos); err != nil {
return err
}

case flags.OutputFormatJSON:
out, err := json.Marshal(kos)
if err != nil {
return err
}

if _, err := fmt.Fprintf(w, "%s", out); err != nil {
return err
}
}

return nil
}

func printTextRecords(w io.Writer, kos []KeyOutput) error {
out, err := yaml.Marshal(&kos)
if err != nil {
return err
}

if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
}

return nil
}

func mkKeyOutput(records []*cryptokeyring.Record) ([]KeyOutput, error) {
kos := make([]KeyOutput, len(records))

for i, r := range records {
kko, err := keys.MkAccKeyOutput(r)
if err != nil {
return nil, err
}
pk, err := r.GetPubKey()
if err != nil {
return nil, err
}
did, _ := util.CreateDIDKeyByPubKey(pk)

kos[i] = KeyOutput{
KeyOutput: kko,
DID: did,
}
}

return kos, nil
}
15 changes: 13 additions & 2 deletions client/keys/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package keys

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"
)

func Enhance(cmd *cobra.Command) *cobra.Command {
func Install(cmd *cobra.Command) *cobra.Command {
cmd.AddCommand(
DIDCmd(),
)

for i, c := range cmd.Commands() {
if c.Name() == ListCmdName {
cmd.RemoveCommand(cmd.Commands()[i])
cmd.AddCommand(ListKeysCmd())
break
}
}

return cmd
}
3 changes: 1 addition & 2 deletions cmd/okp4d/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func initRootCmd(
confixcmd.ConfigCommand(),
pruning.Cmd(newApp, app.DefaultNodeHome),
snapshot.Cmd(newApp),
// did.Cmd(),
)

// add server commands
Expand All @@ -182,7 +181,7 @@ func initRootCmd(
genesisCommand(encodingConfig.TxConfig, basicManager),
queryCommand(),
txCommand(),
okp4keys.Enhance(keys.Commands()),
okp4keys.Install(keys.Commands()),
)
}

Expand Down
6 changes: 3 additions & 3 deletions x/logic/util/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

const (
ED25519PubKeyMultiCodec = 0xed
SECP256k1ubKeyMultiCodec = 0xe7
ED25519PubKeyMultiCodec = 0xed
SECP256k1PubKeyMultiCodec = 0xe7
)

// CreateDIDKeyByPubKey creates a did:key ID using the given public key.
Expand All @@ -24,7 +24,7 @@ func CreateDIDKeyByPubKey(pubKey cryptotypes.PubKey) (string, error) {
case *ed25519.PubKey:
code = ED25519PubKeyMultiCodec
case *secp256k1.PubKey:
code = SECP256k1ubKeyMultiCodec
code = SECP256k1PubKeyMultiCodec
default:
return "", fmt.Errorf("unsupported key type: %s", pubKey.Type())
}
Expand Down

0 comments on commit 30a5615

Please sign in to comment.