Skip to content

Commit

Permalink
gnokey cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Jul 28, 2021
1 parent 8e7254d commit 89771d4
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 44 deletions.
File renamed without changes.
16 changes: 1 addition & 15 deletions pkgs/command/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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...)
Expand All @@ -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{}) {
Expand Down
2 changes: 0 additions & 2 deletions pkgs/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ type PubKey interface {
Bytes() []byte
VerifyBytes(msg []byte, sig []byte) bool
Equals(PubKey) bool

// Unstable
String() string
}

Expand Down
3 changes: 1 addition & 2 deletions pkgs/crypto/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ed25519
import (
"bytes"
"crypto/subtle"
"fmt"
"io"

"golang.org/x/crypto/ed25519"
Expand Down Expand Up @@ -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
Expand Down
34 changes: 25 additions & 9 deletions pkgs/crypto/keys/client/add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"errors"
"fmt"
"sort"

Expand All @@ -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 {
Expand Down Expand Up @@ -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 <keyname>")
return errors.New("invalid args")
}

name := args[0]
showMnemonic := !opts.NoBackup

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
10 changes: 7 additions & 3 deletions pkgs/crypto/keys/client/delete.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 <keyname>")
return errors.New("invalid args")
}

name := args[0]

kb, err := keys.NewKeyBaseFromDir(opts.Home)
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 8 additions & 2 deletions pkgs/crypto/keys/client/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
21 changes: 16 additions & 5 deletions pkgs/crypto/keys/client/list.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
}
12 changes: 9 additions & 3 deletions pkgs/crypto/keys/client/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <keyname>")
return errors.New("invalid args")
}

name := args[0]
docpath := opts.DocPath
kb, err = keys.NewKeyBaseFromDir(opts.Home)
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
11 changes: 10 additions & 1 deletion pkgs/crypto/keys/client/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <keyname> <signature>")
return errors.New("invalid args")
}

name := args[0]
sig, err := parseSignature(args[1])
if err != nil {
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions pkgs/crypto/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"crypto/sha256"
"crypto/subtle"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 89771d4

Please sign in to comment.