65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package keys
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// CMD
|
|
|
|
// listKeysCmd represents the list command
|
|
var listKeysCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all keys",
|
|
Long: `Return a list of all public keys stored by this key manager
|
|
along with their associated name and address.`,
|
|
RunE: runListCmd,
|
|
}
|
|
|
|
func runListCmd(cmd *cobra.Command, args []string) error {
|
|
kb, err := GetKeyBase()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
infos, err := kb.List()
|
|
if err == nil {
|
|
printInfos(infos)
|
|
}
|
|
return err
|
|
}
|
|
|
|
/////////////////////////
|
|
// REST
|
|
|
|
// query key list REST handler
|
|
func QueryKeysRequestHandler(indent bool) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
kb, err := GetKeyBase()
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
infos, err := kb.List()
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
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 {
|
|
PostProcessResponse(w, cdc, []string{}, indent)
|
|
return
|
|
}
|
|
keysOutput, err := Bech32KeysOutput(infos)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
PostProcessResponse(w, cdc, keysOutput, indent)
|
|
}
|
|
}
|