Skip to content

Commit

Permalink
feat: add --qrcode flag to keys show command
Browse files Browse the repository at this point in the history
  • Loading branch information
levisyin committed Nov 26, 2023
1 parent 34b1634 commit 942ed09
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"

"github.com/mdp/qrterminal/v3"
"github.com/spf13/cobra"

"cosmossdk.io/core/address"
Expand All @@ -30,6 +31,7 @@ const (
FlagDevice = "device"

flagMultiSigThreshold = "multisig-threshold"
flagQRCode = "qrcode"
)

// ShowKeysCmd shows key information for a given key name.
Expand All @@ -49,6 +51,7 @@ consisting of all the keys provided by name and multisig threshold.`,
f.BoolP(FlagPublicKey, "p", false, "Output the public key only (overrides --output)")
f.BoolP(FlagDevice, "d", false, "Output the address in a ledger device")
f.Int(flagMultiSigThreshold, 1, "K out of N required signatures")
f.Bool(flagQRCode, false, "Display keys payment QR code, will be ignored if -a or --address is false")

return cmd
}
Expand Down Expand Up @@ -96,6 +99,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
isShowAddr, _ := cmd.Flags().GetBool(FlagAddress)
isShowPubKey, _ := cmd.Flags().GetBool(FlagPublicKey)
isShowDevice, _ := cmd.Flags().GetBool(FlagDevice)
isShowQRCode, _ := cmd.Flags().GetBool(flagQRCode)

isOutputSet := false
tmp := cmd.Flag(flags.FlagOutput)
Expand Down Expand Up @@ -126,6 +130,8 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
out := ko.Address
if isShowPubKey {
out = ko.PubKey
} else if isShowQRCode {
qrterminal.GenerateHalfBlock(out, qrterminal.H, cmd.OutOrStdout())
}

if _, err := fmt.Fprintln(cmd.OutOrStdout(), out); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ func Test_runShowCmd(t *testing.T) {
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for addresses not pubkeys")

cmd.SetArgs([]string{
fakeKeyName1,
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
fmt.Sprintf("--%s=true", FlagAddress),
fmt.Sprintf("--%s=true", flagQRCode),
// we have to reset following flags as they were set to true above, and won't be auto reset to false if we skip to specify these flags.
// Note: this maybe a bug about spf13/cobra as cmd.flags's value' won't be reset by changing cmd.args with cmd.SetArgs.
fmt.Sprintf("--%s=false", FlagDevice),
fmt.Sprintf("--%s=false", FlagPublicKey),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})

// try fetch by name
require.NoError(t, cmd.ExecuteContext(ctx))
}

func Test_validateMultisigThreshold(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/improbable-eng/grpc-web v0.15.0
github.com/magiconair/properties v1.8.7
github.com/mattn/go-isatty v0.0.20
github.com/mdp/qrterminal/v3 v3.2.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/common v0.45.0
github.com/rs/zerolog v1.31.0
Expand Down Expand Up @@ -157,6 +158,7 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
rsc.io/qr v0.2.0 // indirect
)

// Here are the short-lived replace from the Cosmos SDK
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/mdp/qrterminal/v3 v3.2.0 h1:qteQMXO3oyTK4IHwj2mWsKYYRBOp1Pj2WRYFYYNTCdk=
github.com/mdp/qrterminal/v3 v3.2.0/go.mod h1:XGGuua4Lefrl7TLEsSONiD+UEjQXJZ4mPzF+gWYIJkk=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
Expand Down Expand Up @@ -1297,6 +1299,8 @@ pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
Expand Down
2 changes: 1 addition & 1 deletion testutil/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ApplyMockIODiscardOutErr(c *cobra.Command) BufferReader {
mockIn := strings.NewReader("")

c.SetIn(mockIn)
c.SetOut(io.Discard)
//c.SetOut(io.Discard)
c.SetErr(io.Discard)

return mockIn
Expand Down

0 comments on commit 942ed09

Please sign in to comment.