Skip to content

Commit 942ed09

Browse files
committed
feat: add --qrcode flag to keys show command
1 parent 34b1634 commit 942ed09

5 files changed

Lines changed: 28 additions & 1 deletion

File tree

client/keys/show.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/mdp/qrterminal/v3"
78
"github.com/spf13/cobra"
89

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

3233
flagMultiSigThreshold = "multisig-threshold"
34+
flagQRCode = "qrcode"
3335
)
3436

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

5356
return cmd
5457
}
@@ -96,6 +99,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
9699
isShowAddr, _ := cmd.Flags().GetBool(FlagAddress)
97100
isShowPubKey, _ := cmd.Flags().GetBool(FlagPublicKey)
98101
isShowDevice, _ := cmd.Flags().GetBool(FlagDevice)
102+
isShowQRCode, _ := cmd.Flags().GetBool(flagQRCode)
99103

100104
isOutputSet := false
101105
tmp := cmd.Flag(flags.FlagOutput)
@@ -126,6 +130,8 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
126130
out := ko.Address
127131
if isShowPubKey {
128132
out = ko.PubKey
133+
} else if isShowQRCode {
134+
qrterminal.GenerateHalfBlock(out, qrterminal.H, cmd.OutOrStdout())
129135
}
130136

131137
if _, err := fmt.Fprintln(cmd.OutOrStdout(), out); err != nil {

client/keys/show_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ func Test_runShowCmd(t *testing.T) {
174174
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
175175
})
176176
require.EqualError(t, cmd.ExecuteContext(ctx), "the device flag (-d) can only be used for addresses not pubkeys")
177+
178+
cmd.SetArgs([]string{
179+
fakeKeyName1,
180+
fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome),
181+
fmt.Sprintf("--%s=true", FlagAddress),
182+
fmt.Sprintf("--%s=true", flagQRCode),
183+
// 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.
184+
// Note: this maybe a bug about spf13/cobra as cmd.flags's value' won't be reset by changing cmd.args with cmd.SetArgs.
185+
fmt.Sprintf("--%s=false", FlagDevice),
186+
fmt.Sprintf("--%s=false", FlagPublicKey),
187+
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
188+
})
189+
190+
// try fetch by name
191+
require.NoError(t, cmd.ExecuteContext(ctx))
177192
}
178193

179194
func Test_validateMultisigThreshold(t *testing.T) {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/improbable-eng/grpc-web v0.15.0
4343
github.com/magiconair/properties v1.8.7
4444
github.com/mattn/go-isatty v0.0.20
45+
github.com/mdp/qrterminal/v3 v3.2.0
4546
github.com/prometheus/client_golang v1.17.0
4647
github.com/prometheus/common v0.45.0
4748
github.com/rs/zerolog v1.31.0
@@ -157,6 +158,7 @@ require (
157158
gopkg.in/ini.v1 v1.67.0 // indirect
158159
gopkg.in/yaml.v3 v3.0.1 // indirect
159160
nhooyr.io/websocket v1.8.6 // indirect
161+
rsc.io/qr v0.2.0 // indirect
160162
)
161163

162164
// Here are the short-lived replace from the Cosmos SDK

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
549549
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
550550
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
551551
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
552+
github.com/mdp/qrterminal/v3 v3.2.0 h1:qteQMXO3oyTK4IHwj2mWsKYYRBOp1Pj2WRYFYYNTCdk=
553+
github.com/mdp/qrterminal/v3 v3.2.0/go.mod h1:XGGuua4Lefrl7TLEsSONiD+UEjQXJZ4mPzF+gWYIJkk=
552554
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
553555
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
554556
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
@@ -1297,6 +1299,8 @@ pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
12971299
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
12981300
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
12991301
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
1302+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
1303+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=
13001304
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
13011305
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
13021306
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=

testutil/ioutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ApplyMockIODiscardOutErr(c *cobra.Command) BufferReader {
4444
mockIn := strings.NewReader("")
4545

4646
c.SetIn(mockIn)
47-
c.SetOut(io.Discard)
47+
//c.SetOut(io.Discard)
4848
c.SetErr(io.Discard)
4949

5050
return mockIn

0 commit comments

Comments
 (0)