add --peerid-base to ipfs id
Petar Maymounkov committed
Aug 11, 2020 at 16:43 UTC
a68e1469babc8a7ab3b2b1ebcae155631f823041
1 file changed
+15
-7
core/commands/id.go
+15
-7
@@ -14,6 +14,7 @@ import (
14
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
15
16
cmds "github.com/ipfs/go-ipfs-cmds"
17
+ ke "github.com/ipfs/go-ipfs/core/commands/keyencode"
18
ic "github.com/libp2p/go-libp2p-core/crypto"
19
"github.com/libp2p/go-libp2p-core/host"
20
peer "github.com/libp2p/go-libp2p-core/peer"
@@ -41,7 +42,8 @@ type IdOutput struct {
42
}
43
44
const (
44
- formatOptionName = "format"
45
+ formatOptionName = "format"
46
+ idFormatOptionName = "peerid-base"
47
)
48
49
var IDCmd = &cmds.Command{
@@ -68,8 +70,14 @@ EXAMPLE:
70
},
71
Options: []cmds.Option{
72
cmds.StringOption(formatOptionName, "f", "Optional output format."),
73
+ cmds.StringOption(idFormatOptionName, "", "Encoding used for peer IDs: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("b58mh"),
74
},
75
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
76
+ keyEnc, err := ke.KeyEncoderFromString(req.Options[idFormatOptionName].(string))
77
+ if err != nil {
78
+ return err
79
+ }
80
+
81
n, err := cmdenv.GetNode(env)
82
if err != nil {
83
return err
@@ -87,7 +95,7 @@ EXAMPLE:
95
}
96
97
if id == n.Identity {
90
- output, err := printSelf(n)
98
+ output, err := printSelf(keyEnc, n)
99
if err != nil {
100
return err
101
}
@@ -109,7 +117,7 @@ EXAMPLE:
117
return err
118
}
119
112
- output, err := printPeer(n.Peerstore, id)
120
+ output, err := printPeer(keyEnc, n.Peerstore, id)
121
if err != nil {
122
return err
123
}
@@ -143,13 +151,13 @@ EXAMPLE:
151
Type: IdOutput{},
152
}
153
146
-func printPeer(ps pstore.Peerstore, p peer.ID) (interface{}, error) {
154
+func printPeer(keyEnc ke.KeyEncoder, ps pstore.Peerstore, p peer.ID) (interface{}, error) {
155
if p == "" {
156
return nil, errors.New("attempted to print nil peer")
157
}
158
159
info := new(IdOutput)
152
- info.ID = p.Pretty()
160
+ info.ID = keyEnc.FormatID(p)
161
162
if pk := ps.PubKey(p); pk != nil {
163
pkb, err := ic.MarshalPublicKey(pk)
@@ -191,9 +199,9 @@ func printPeer(ps pstore.Peerstore, p peer.ID) (interface{}, error) {
199
}
200
201
// printing self is special cased as we get values differently.
194
-func printSelf(node *core.IpfsNode) (interface{}, error) {
202
+func printSelf(keyEnc ke.KeyEncoder, node *core.IpfsNode) (interface{}, error) {
203
info := new(IdOutput)
196
- info.ID = node.Identity.Pretty()
204
+ info.ID = keyEnc.FormatID(node.Identity)
205
206
pk := node.PrivateKey.GetPublic()
207
pkb, err := ic.MarshalPublicKey(pk)