/
key_list.go
137 lines (111 loc) · 3.23 KB
/
key_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cmd
import (
"context"
"errors"
"fmt"
"io"
"github.com/elysiumstation/fury/cmd/furywallet/commands/cli"
"github.com/elysiumstation/fury/cmd/furywallet/commands/flags"
"github.com/elysiumstation/fury/cmd/furywallet/commands/printer"
"github.com/elysiumstation/fury/wallet/api"
"github.com/elysiumstation/fury/wallet/wallets"
"github.com/spf13/cobra"
)
var (
listKeysLong = cli.LongDesc(`
List the keys of a given wallet.
`)
listKeysExample = cli.Examples(`
# List all keys
{{.Software}} key list --wallet WALLET
`)
)
type ListKeysHandler func(api.AdminListKeysParams, string) (api.AdminListKeysResult, error)
func NewCmdListKeys(w io.Writer, rf *RootFlags) *cobra.Command {
h := func(params api.AdminListKeysParams, passphrase string) (api.AdminListKeysResult, error) {
ctx := context.Background()
walletStore, err := wallets.InitialiseStore(rf.Home, false)
if err != nil {
return api.AdminListKeysResult{}, fmt.Errorf("couldn't initialise wallets store: %w", err)
}
defer walletStore.Close()
if _, errDetails := api.NewAdminUnlockWallet(walletStore).Handle(ctx, api.AdminUnlockWalletParams{
Wallet: params.Wallet,
Passphrase: passphrase,
}); errDetails != nil {
return api.AdminListKeysResult{}, errors.New(errDetails.Data)
}
rawResult, errDetails := api.NewAdminListKeys(walletStore).Handle(ctx, params)
if errDetails != nil {
return api.AdminListKeysResult{}, errors.New(errDetails.Data)
}
return rawResult.(api.AdminListKeysResult), nil
}
return BuildCmdListKeys(w, h, rf)
}
func BuildCmdListKeys(w io.Writer, handler ListKeysHandler, rf *RootFlags) *cobra.Command {
f := &ListKeysFlags{}
cmd := &cobra.Command{
Use: "list",
Short: "List the keys of a given wallet",
Long: listKeysLong,
Example: listKeysExample,
RunE: func(_ *cobra.Command, _ []string) error {
req, pass, err := f.Validate()
if err != nil {
return err
}
resp, err := handler(req, pass)
if err != nil {
return err
}
switch rf.Output {
case flags.InteractiveOutput:
PrintListKeysResponse(w, resp)
case flags.JSONOutput:
return printer.FprintJSON(w, resp)
}
return nil
},
}
cmd.Flags().StringVarP(&f.Wallet,
"wallet", "w",
"",
"Name of the wallet to use",
)
cmd.Flags().StringVarP(&f.PassphraseFile,
"passphrase-file", "p",
"",
"Path to the file containing the wallet's passphrase",
)
autoCompleteWallet(cmd, rf.Home, "wallet")
return cmd
}
type ListKeysFlags struct {
Wallet string
PassphraseFile string
}
func (f *ListKeysFlags) Validate() (api.AdminListKeysParams, string, error) {
if len(f.Wallet) == 0 {
return api.AdminListKeysParams{}, "", flags.MustBeSpecifiedError("wallet")
}
passphrase, err := flags.GetPassphrase(f.PassphraseFile)
if err != nil {
return api.AdminListKeysParams{}, "", err
}
return api.AdminListKeysParams{
Wallet: f.Wallet,
}, passphrase, nil
}
func PrintListKeysResponse(w io.Writer, resp api.AdminListKeysResult) {
p := printer.NewInteractivePrinter(w)
str := p.String()
defer p.Print(str)
for i, key := range resp.Keys {
if i != 0 {
str.NextLine()
}
str.Text("Name: ").WarningText(key.Name).NextLine()
str.Text("Public key: ").WarningText(key.PublicKey).NextLine()
}
}