From 89771d45a2260037ab2ae51cc5047e13646b7f18 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 27 Jul 2021 17:39:34 -0700 Subject: [PATCH] gnokey cleanup --- .../keys/client/cmd => cmd/gnokey}/gnokeys.go | 0 pkgs/command/prompt.go | 16 +-------- pkgs/crypto/crypto.go | 2 -- pkgs/crypto/ed25519/ed25519.go | 3 +- pkgs/crypto/keys/client/add.go | 34 ++++++++++++++----- pkgs/crypto/keys/client/delete.go | 10 ++++-- pkgs/crypto/keys/client/generate.go | 10 ++++-- pkgs/crypto/keys/client/list.go | 21 +++++++++--- pkgs/crypto/keys/client/sign.go | 12 +++++-- pkgs/crypto/keys/client/verify.go | 11 +++++- pkgs/crypto/secp256k1/secp256k1.go | 3 +- 11 files changed, 78 insertions(+), 44 deletions(-) rename {pkgs/crypto/keys/client/cmd => cmd/gnokey}/gnokeys.go (100%) diff --git a/pkgs/crypto/keys/client/cmd/gnokeys.go b/cmd/gnokey/gnokeys.go similarity index 100% rename from pkgs/crypto/keys/client/cmd/gnokeys.go rename to cmd/gnokey/gnokeys.go diff --git a/pkgs/command/prompt.go b/pkgs/command/prompt.go index 17fb80f8df7..a64e26f555a 100644 --- a/pkgs/command/prompt.go +++ b/pkgs/command/prompt.go @@ -68,7 +68,7 @@ func (cmd *Command) GetConfirmation(prompt string) (bool, error) { // GetString simply returns the trimmed string output of a given reader. func (cmd *Command) GetString(prompt string) (string, error) { if prompt != "" { - cmd.PrintPrefixed(prompt) + cmd.Println(prompt) } out, err := cmd.readLineFromInBuf() @@ -110,13 +110,6 @@ func (cmd *Command) readPasswordFromInBuf() (string, error) { return pass, nil } -// PrintPrefixed prints a string with > prefixed for use in prompts. -func (cmd *Command) PrintPrefixed(msg string) { - msg = fmt.Sprintf("> %s\n", msg) - fmt.Fprint(cmd.OutBuf, msg) - cmd.OutBuf.Flush() -} - // Println prints a line terminated by a newline. func (cmd *Command) Println(args ...interface{}) { fmt.Fprintln(cmd.OutBuf, args...) @@ -135,13 +128,6 @@ func (cmd *Command) Printfln(format string, args ...interface{}) { cmd.OutBuf.Flush() } -// ErrPrintPrefixed prints a string with > prefixed for use in prompts to cmd.Err(Buf). -func (cmd *Command) ErrPrintPrefixed(msg string) { - msg = fmt.Sprintf("> %s\n", msg) - fmt.Fprint(cmd.ErrBuf, msg) - cmd.ErrBuf.Flush() -} - // ErrPrintln prints a line terminated by a newline to // cmd.Err(Buf). func (cmd *Command) ErrPrintln(args ...interface{}) { diff --git a/pkgs/crypto/crypto.go b/pkgs/crypto/crypto.go index dc459e32687..92627d375f3 100644 --- a/pkgs/crypto/crypto.go +++ b/pkgs/crypto/crypto.go @@ -127,8 +127,6 @@ type PubKey interface { Bytes() []byte VerifyBytes(msg []byte, sig []byte) bool Equals(PubKey) bool - - // Unstable String() string } diff --git a/pkgs/crypto/ed25519/ed25519.go b/pkgs/crypto/ed25519/ed25519.go index 3d79e94f434..a4ba48ab6b1 100644 --- a/pkgs/crypto/ed25519/ed25519.go +++ b/pkgs/crypto/ed25519/ed25519.go @@ -3,7 +3,6 @@ package ed25519 import ( "bytes" "crypto/subtle" - "fmt" "io" "golang.org/x/crypto/ed25519" @@ -139,7 +138,7 @@ func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig []byte) bool { } func (pubKey PubKeyEd25519) String() string { - return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:]) + return crypto.PubKeyToBech32(pubKey) } // nolint: golint diff --git a/pkgs/crypto/keys/client/add.go b/pkgs/crypto/keys/client/add.go index 924f70e632f..fd3d2325c59 100644 --- a/pkgs/crypto/keys/client/add.go +++ b/pkgs/crypto/keys/client/add.go @@ -1,7 +1,6 @@ package client import ( - "errors" "fmt" "sort" @@ -10,6 +9,7 @@ import ( "github.com/gnolang/gno/pkgs/crypto/bip39" "github.com/gnolang/gno/pkgs/crypto/keys" "github.com/gnolang/gno/pkgs/crypto/multisig" + "github.com/gnolang/gno/pkgs/errors" ) type BaseOptions struct { @@ -52,6 +52,11 @@ func addApp(cmd *command.Command, args []string, iopts interface{}) error { var encryptPassword string var opts AddOptions = iopts.(AddOptions) + if len(args) != 1 { + cmd.ErrPrintfln("Usage: add ") + return errors.New("invalid args") + } + name := args[0] showMnemonic := !opts.NoBackup @@ -69,7 +74,7 @@ func addApp(cmd *command.Command, args []string, iopts interface{}) error { _, err = kb.Get(name) if err == nil { // account exists, ask for user confirmation - response, err2 := cmd.GetConfirmation(fmt.Sprintf("override the existing name %s", name)) + response, err2 := cmd.GetConfirmation(fmt.Sprintf("Override the existing name %s", name)) if err2 != nil { return err2 } @@ -193,17 +198,28 @@ func addApp(cmd *command.Command, args []string, iopts interface{}) error { } func printCreate(cmd *command.Command, info keys.Info, showMnemonic bool, mnemonic string) error { - cmd.Printfln("\n%#v", info) + cmd.Println("") + printNewInfo(cmd, info) // print mnemonic unless requested not to. if showMnemonic { - cmd.Printfln( - `**Important** write this mnemonic phrase in a safe place. - It is the only way to recover your account if you ever forget your password. - - %v - `, mnemonic) + cmd.Printfln(` +**IMPORTANT** write this mnemonic phrase in a safe place. +It is the only way to recover your account if you ever forget your password. + +%v +`, mnemonic) } return nil } + +func printNewInfo(cmd *command.Command, info keys.Info) { + keyname := info.GetName() + keytype := info.GetType() + keypub := info.GetPubKey() + keyaddr := info.GetAddress() + keypath, _ := info.GetPath() + cmd.Printfln("* %s (%s) - addr: %v pub: %v, path: %v", + keyname, keytype, keyaddr, keypub, keypath) +} diff --git a/pkgs/crypto/keys/client/delete.go b/pkgs/crypto/keys/client/delete.go index 7d8ae89558f..83b954165ae 100644 --- a/pkgs/crypto/keys/client/delete.go +++ b/pkgs/crypto/keys/client/delete.go @@ -1,10 +1,9 @@ package client import ( - "errors" - "github.com/gnolang/gno/pkgs/command" "github.com/gnolang/gno/pkgs/crypto/keys" + "github.com/gnolang/gno/pkgs/errors" ) type DeleteOptions struct { @@ -18,6 +17,11 @@ var DefaultDeleteOptions = DeleteOptions{} func deleteApp(cmd *command.Command, args []string, iopts interface{}) error { var opts DeleteOptions = iopts.(DeleteOptions) + if len(args) != 1 { + cmd.ErrPrintfln("Usage: delete ") + return errors.New("invalid args") + } + name := args[0] kb, err := keys.NewKeyBaseFromDir(opts.Home) @@ -57,7 +61,7 @@ func deleteApp(cmd *command.Command, args []string, iopts interface{}) error { if err != nil { return err } - cmd.ErrPrintln("Key deleted forever (uh oh!)") + cmd.ErrPrintln("Key deleted") return nil } diff --git a/pkgs/crypto/keys/client/generate.go b/pkgs/crypto/keys/client/generate.go index 281d64b7e4a..5bf624f48f7 100644 --- a/pkgs/crypto/keys/client/generate.go +++ b/pkgs/crypto/keys/client/generate.go @@ -6,6 +6,7 @@ import ( "github.com/gnolang/gno/pkgs/command" "github.com/gnolang/gno/pkgs/crypto/bip39" + "github.com/gnolang/gno/pkgs/errors" ) type GenerateOptions struct { @@ -18,18 +19,23 @@ func generateApp(cmd *command.Command, args []string, iopts interface{}) error { opts := iopts.(GenerateOptions) customEntropy := opts.CustomEntropy + if len(args) != 0 { + cmd.ErrPrintfln("Usage: generate (no args)") + return errors.New("invalid args") + } + var entropySeed []byte if customEntropy { // prompt the user to enter some entropy - inputEntropy, err := cmd.GetString("> WARNING: Generate at least 256-bits of entropy and enter the results here:") + inputEntropy, err := cmd.GetString("WARNING: Generate at least 256-bits of entropy and enter the results here:") if err != nil { return err } if len(inputEntropy) < 43 { return fmt.Errorf("256-bits is 43 characters in Base-64, and 100 in Base-6. You entered %v, and probably want more", len(inputEntropy)) } - conf, err := cmd.GetConfirmation(fmt.Sprintf("> Input length: %d", len(inputEntropy))) + conf, err := cmd.GetConfirmation(fmt.Sprintf("Input length: %d", len(inputEntropy))) if err != nil { return err } diff --git a/pkgs/crypto/keys/client/list.go b/pkgs/crypto/keys/client/list.go index 4d2c3eefc51..7f04f0cb472 100644 --- a/pkgs/crypto/keys/client/list.go +++ b/pkgs/crypto/keys/client/list.go @@ -1,10 +1,9 @@ package client import ( - "fmt" - "github.com/gnolang/gno/pkgs/command" "github.com/gnolang/gno/pkgs/crypto/keys" + "github.com/gnolang/gno/pkgs/errors" ) type ListOptions struct { @@ -14,6 +13,12 @@ type ListOptions struct { var DefaultListOptions = ListOptions{} func listApp(cmd *command.Command, args []string, iopts interface{}) error { + + if len(args) != 0 { + cmd.ErrPrintfln("Usage: list (no args)") + return errors.New("invalid args") + } + opts := iopts.(ListOptions) kb, err := keys.NewKeyBaseFromDir(opts.Home) if err != nil { @@ -22,13 +27,19 @@ func listApp(cmd *command.Command, args []string, iopts interface{}) error { infos, err := kb.List() if err == nil { - printInfos(infos) + printInfos(cmd, infos) } return err } -func printInfos(infos []keys.Info) { +func printInfos(cmd *command.Command, infos []keys.Info) { for i, info := range infos { - fmt.Println(">>> [XXX TODO IMPROVE LISTING]", i, info) + keyname := info.GetName() + keytype := info.GetType() + keypub := info.GetPubKey() + keyaddr := info.GetAddress() + keypath, _ := info.GetPath() + cmd.Printfln("%d. %s (%s) - addr: %v pub: %v, path: %v", + i, keyname, keytype, keyaddr, keypub, keypath) } } diff --git a/pkgs/crypto/keys/client/sign.go b/pkgs/crypto/keys/client/sign.go index 1cd058c27f1..5171be31e09 100644 --- a/pkgs/crypto/keys/client/sign.go +++ b/pkgs/crypto/keys/client/sign.go @@ -5,6 +5,7 @@ import ( "github.com/gnolang/gno/pkgs/command" "github.com/gnolang/gno/pkgs/crypto/keys" + "github.com/gnolang/gno/pkgs/errors" ) type SignOptions struct { @@ -21,6 +22,11 @@ func signApp(cmd *command.Command, args []string, iopts interface{}) error { var err error var opts SignOptions = iopts.(SignOptions) + if len(args) != 1 { + cmd.ErrPrintfln("Usage: sign ") + return errors.New("invalid args") + } + name := args[0] docpath := opts.DocPath kb, err = keys.NewKeyBaseFromDir(opts.Home) @@ -31,7 +37,7 @@ func signApp(cmd *command.Command, args []string, iopts interface{}) error { // read document to sign if docpath == "" { // from stdin. - msgstr, err := cmd.GetString("enter document to sign.") + msgstr, err := cmd.GetString("Enter document to sign.") if err != nil { return err } @@ -46,7 +52,7 @@ func signApp(cmd *command.Command, args []string, iopts interface{}) error { // validate document to sign. // XXX - pass, err := cmd.GetPassword("enter password.") + pass, err := cmd.GetPassword("Enter password.") if err != nil { return err } @@ -55,6 +61,6 @@ func signApp(cmd *command.Command, args []string, iopts interface{}) error { return err } - cmd.Printfln("signature: %v\npub: %v", sig, pub) + cmd.Printfln("Signature: %X\nPub: %v", sig, pub) return nil } diff --git a/pkgs/crypto/keys/client/verify.go b/pkgs/crypto/keys/client/verify.go index 86807d7cf8f..3c7ba456417 100644 --- a/pkgs/crypto/keys/client/verify.go +++ b/pkgs/crypto/keys/client/verify.go @@ -6,6 +6,7 @@ import ( "github.com/gnolang/gno/pkgs/command" "github.com/gnolang/gno/pkgs/crypto/keys" + "github.com/gnolang/gno/pkgs/errors" ) type VerifyOptions struct { @@ -22,6 +23,11 @@ func verifyApp(cmd *command.Command, args []string, iopts interface{}) error { var err error var opts VerifyOptions = iopts.(VerifyOptions) + if len(args) != 2 { + cmd.ErrPrintfln("Usage: verify ") + return errors.New("invalid args") + } + name := args[0] sig, err := parseSignature(args[1]) if err != nil { @@ -36,7 +42,7 @@ func verifyApp(cmd *command.Command, args []string, iopts interface{}) error { // read document to sign if docpath == "" { // from stdin. - msgstr, err := cmd.GetString("enter document to sign.") + msgstr, err := cmd.GetString("Enter document to sign.") if err != nil { return err } @@ -53,6 +59,9 @@ func verifyApp(cmd *command.Command, args []string, iopts interface{}) error { // verify signature. err = kb.Verify(name, msg, sig) + if err == nil { + cmd.Println("Valid signature!") + } return err } diff --git a/pkgs/crypto/secp256k1/secp256k1.go b/pkgs/crypto/secp256k1/secp256k1.go index f8514f09c44..9d3495364d9 100644 --- a/pkgs/crypto/secp256k1/secp256k1.go +++ b/pkgs/crypto/secp256k1/secp256k1.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/sha256" "crypto/subtle" - "fmt" "io" "math/big" @@ -136,7 +135,7 @@ func (pubKey PubKeySecp256k1) Bytes() []byte { } func (pubKey PubKeySecp256k1) String() string { - return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:]) + return crypto.PubKeyToBech32(pubKey) } func (pubKey PubKeySecp256k1) Equals(other crypto.PubKey) bool {