Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] change(gnokey): use filename as arg in sign cmd #877

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tm2/pkg/crypto/keys/client/addpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func signAndBroadcast(
sequence: sequence,
accountNumber: accountNumber,
chainID: txopts.chainID,
nameOrBech32: nameOrBech32,
key: nameOrBech32,
txJSON: amino.MustMarshalJSON(tx),
}
if baseopts.Quiet {
Expand Down
59 changes: 26 additions & 33 deletions tm2/pkg/crypto/keys/client/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import (
type signCfg struct {
rootCfg *baseCfg

txPath string
chainID string
key string
accountNumber uint64
sequence uint64
showSignBytes bool

// internal flags, when called programmatically
nameOrBech32 string
txJSON []byte
pass string
txJSON []byte
pass string
}

func newSignCmd(rootCfg *baseCfg) *commands.Command {
Expand All @@ -36,8 +35,8 @@ func newSignCmd(rootCfg *baseCfg) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "sign",
ShortUsage: "sign [flags] <key-name or address>",
ShortHelp: "Signs the document",
ShortUsage: "sign [flags] <file>",
ShortHelp: "Signs transaction(s)",
},
cfg,
func(_ context.Context, args []string) error {
Expand All @@ -47,20 +46,20 @@ func newSignCmd(rootCfg *baseCfg) *commands.Command {
}

func (c *signCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.txPath,
"txpath",
"-",
"path to file of tx to sign",
)

fs.StringVar(
&c.chainID,
"chainid",
"dev",
"chainid to sign for",
)

fs.StringVar(
&c.key,
"key",
"",
"key-name or address to sign with (required)",
)

fs.Uint64Var(
&c.accountNumber,
"number",
Expand All @@ -86,40 +85,34 @@ func (c *signCfg) RegisterFlags(fs *flag.FlagSet) {
func execSign(cfg *signCfg, args []string, io *commands.IO) error {
var err error

if len(args) != 1 {
if cfg.key == "" {
return flag.ErrHelp
}

cfg.nameOrBech32 = args[0]

// read tx to sign
txpath := cfg.txPath
if txpath == "-" { // from stdin.
// read tx from file or stdin to sign.
if len(args) == 0 { // from stdin
txjsonstr, err := io.GetString(
"Enter tx to sign, terminated by a newline.",
)
if err != nil {
return err
}
cfg.txJSON = []byte(txjsonstr)
} else { // from file
cfg.txJSON, err = os.ReadFile(txpath)
} else if len(args) == 1 { // from file
cfg.txJSON, err = os.ReadFile(args[0])
if err != nil {
return err
}
} else {
return flag.ErrHelp
}

if cfg.rootCfg.Quiet {
cfg.pass, err = io.GetPassword(
"",
cfg.rootCfg.InsecurePasswordStdin,
)
} else {
cfg.pass, err = io.GetPassword(
"Enter password.",
cfg.rootCfg.InsecurePasswordStdin,
)
askPasswordMsg := ""
if !cfg.rootCfg.Quiet {
askPasswordMsg = "Enter password:"
}

cfg.pass, err = io.GetPassword(askPasswordMsg, cfg.rootCfg.InsecurePasswordStdin)
if err != nil {
return err
}
Expand Down Expand Up @@ -183,7 +176,7 @@ func SignHandler(cfg *signCfg) (*std.Tx, error) {
return nil, nil
}

sig, pub, err := kb.Sign(cfg.nameOrBech32, cfg.pass, signbz)
sig, pub, err := kb.Sign(cfg.key, cfg.pass, signbz)
if err != nil {
return nil, err
}
Expand All @@ -201,7 +194,7 @@ func SignHandler(cfg *signCfg) (*std.Tx, error) {
}
if !found {
return nil, errors.New(
fmt.Sprintf("addr %v (%s) not in signer set", addr, cfg.nameOrBech32),
fmt.Sprintf("addr %v (%s) not in signer set", addr, cfg.key),
)
}

Expand Down
17 changes: 8 additions & 9 deletions tm2/pkg/crypto/keys/client/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func Test_execSign(t *testing.T) {
assert.NotNil(t, kbHome)
defer kbCleanUp()

fakeKeyName1 := "signApp_Key1"
fakeKeyName2 := "signApp_Key2"
encPassword := "12345678"
args := []string{}

// initialize test options
cfg := &signCfg{
rootCfg: &baseCfg{
Expand All @@ -30,16 +35,12 @@ func Test_execSign(t *testing.T) {
InsecurePasswordStdin: true,
},
},
txPath: "-", // stdin
key: fakeKeyName1,
chainID: "dev",
accountNumber: 0,
sequence: 0,
}

fakeKeyName1 := "signApp_Key1"
fakeKeyName2 := "signApp_Key2"
encPassword := "12345678"

io := commands.NewTestIO()

// add test account to keybase.
Expand All @@ -55,17 +56,15 @@ func Test_execSign(t *testing.T) {
tx := std.NewTx([]std.Msg{msg}, fee, nil, "")
txjson := string(amino.MustMarshalJSON(tx))

args := []string{fakeKeyName1}
io.SetIn(strings.NewReader(txjson))
err = execSign(cfg, args, io)
assert.Error(t, err)

args = []string{fakeKeyName1}
io.SetIn(strings.NewReader(txjson + "\n"))
err = execSign(cfg, args, io)
assert.Error(t, err)

args = []string{fakeKeyName2}
cfg.key = fakeKeyName2
io.SetIn(strings.NewReader(
fmt.Sprintf("%s\n%s\n",
txjson,
Expand All @@ -75,7 +74,7 @@ func Test_execSign(t *testing.T) {
err = execSign(cfg, args, io)
assert.Error(t, err)

args = []string{fakeKeyName1}
cfg.key = fakeKeyName1
io.SetIn(strings.NewReader(
fmt.Sprintf("%s\n%s\n",
txjson,
Expand Down