@@ -5,10 +5,11 @@ import (
5
5
"io"
6
6
"text/tabwriter"
7
7
8
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
-
10
8
cmds "github.com/ipfs/go-ipfs-cmds"
9
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11
10
options "github.com/ipfs/interface-go-ipfs-core/options"
11
+ peer "github.com/libp2p/go-libp2p-core/peer"
12
+ mbase "github.com/multiformats/go-multibase"
12
13
)
13
14
14
15
var KeyCmd = & cmds.Command {
@@ -56,6 +57,7 @@ type KeyRenameOutput struct {
56
57
const (
57
58
keyStoreTypeOptionName = "type"
58
59
keyStoreSizeOptionName = "size"
60
+ keyFormatOptionName = "format"
59
61
)
60
62
61
63
var keyGenCmd = & cmds.Command {
@@ -65,6 +67,7 @@ var keyGenCmd = &cmds.Command{
65
67
Options : []cmds.Option {
66
68
cmds .StringOption (keyStoreTypeOptionName , "t" , "type of the key to create: rsa, ed25519" ).WithDefault ("rsa" ),
67
69
cmds .IntOption (keyStoreSizeOptionName , "s" , "size of the key to generate" ),
70
+ cmds .StringOption (keyFormatOptionName , "f" , "output format: b58mh or b36cid" ).WithDefault ("b58mh" ),
68
71
},
69
72
Arguments : []cmds.Argument {
70
73
cmds .StringArg ("name" , true , false , "name of key to create" ),
@@ -91,6 +94,9 @@ var keyGenCmd = &cmds.Command{
91
94
if sizefound {
92
95
opts = append (opts , options .Key .Size (size ))
93
96
}
97
+ if err = verifyFormatLabel (req .Options [keyFormatOptionName ].(string )); err != nil {
98
+ return err
99
+ }
94
100
95
101
key , err := api .Key ().Generate (req .Context , name , opts ... )
96
102
@@ -100,7 +106,7 @@ var keyGenCmd = &cmds.Command{
100
106
101
107
return cmds .EmitOnce (res , & KeyOutput {
102
108
Name : name ,
103
- Id : key .ID (). Pretty ( ),
109
+ Id : formatID ( key .ID (), req . Options [ keyFormatOptionName ].( string ) ),
104
110
})
105
111
},
106
112
Encoders : cmds.EncoderMap {
@@ -112,14 +118,44 @@ var keyGenCmd = &cmds.Command{
112
118
Type : KeyOutput {},
113
119
}
114
120
121
+ func verifyFormatLabel (formatLabel string ) error {
122
+ switch formatLabel {
123
+ case "b58mh" :
124
+ return nil
125
+ case "b36cid" :
126
+ return nil
127
+ }
128
+ return fmt .Errorf ("invalid output format option" )
129
+ }
130
+
131
+ func formatID (id peer.ID , formatLabel string ) string {
132
+ switch formatLabel {
133
+ case "b58mh" :
134
+ return id .Pretty ()
135
+ case "b36cid" :
136
+ if s , err := peer .ToCid (id ).StringOfBase (mbase .Base36 ); err != nil {
137
+ panic (err )
138
+ } else {
139
+ return s
140
+ }
141
+ default :
142
+ panic ("unreachable" )
143
+ }
144
+ }
145
+
115
146
var keyListCmd = & cmds.Command {
116
147
Helptext : cmds.HelpText {
117
148
Tagline : "List all local keypairs" ,
118
149
},
119
150
Options : []cmds.Option {
120
151
cmds .BoolOption ("l" , "Show extra information about keys." ),
152
+ cmds .StringOption (keyFormatOptionName , "f" , "output format: b58mh or b36cid" ).WithDefault ("b58mh" ),
121
153
},
122
154
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
155
+ if err := verifyFormatLabel (req .Options [keyFormatOptionName ].(string )); err != nil {
156
+ return err
157
+ }
158
+
123
159
api , err := cmdenv .GetApi (env , req )
124
160
if err != nil {
125
161
return err
@@ -133,7 +169,10 @@ var keyListCmd = &cmds.Command{
133
169
list := make ([]KeyOutput , 0 , len (keys ))
134
170
135
171
for _ , key := range keys {
136
- list = append (list , KeyOutput {Name : key .Name (), Id : key .ID ().Pretty ()})
172
+ list = append (list , KeyOutput {
173
+ Name : key .Name (),
174
+ Id : formatID (key .ID (), req .Options [keyFormatOptionName ].(string )),
175
+ })
137
176
}
138
177
139
178
return cmds .EmitOnce (res , & KeyOutputList {list })
0 commit comments