@cryptotaxi247 / kubo / commits / 581c4e558

cmds/id: show self addrs

Juan Batiz-Benet committed Feb 3, 2015 at 04:56 UTC 581c4e558e34229ba0b45ea4a42d615dd5b4b1c2
2 files changed +38 -7
core/commands/id.go
+32 -1
@@ -14,8 +14,10 @@ import (
14 b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
15
16 cmds "github.com/jbenet/go-ipfs/commands"
17 + core "github.com/jbenet/go-ipfs/core"
18 ic "github.com/jbenet/go-ipfs/p2p/crypto"
19 "github.com/jbenet/go-ipfs/p2p/peer"
20 + identify "github.com/jbenet/go-ipfs/p2p/protocol/identify"
21 kb "github.com/jbenet/go-ipfs/routing/kbucket"
22 u "github.com/jbenet/go-ipfs/util"
23 )
@@ -63,7 +65,7 @@ ipfs id supports the format option for output with the following keys:
65 }
66
67 if len(req.Arguments()) == 0 {
66 - output, err := printPeer(node.Peerstore, node.Identity)
68 + output, err := printSelf(node)
69 if err != nil {
70 res.SetError(err, cmds.ErrNormal)
71 return
@@ -168,3 +170,32 @@ func printPeer(ps peer.Peerstore, p peer.ID) (interface{}, error) {
170
171 return info, nil
172 }
173 +
174 +// printing self is special cased as we get values differently.
175 +func printSelf(node *core.IpfsNode) (interface{}, error) {
176 + info := new(IdOutput)
177 + info.ID = node.Identity.Pretty()
178 +
179 + if node.PrivateKey == nil {
180 + if err := node.LoadPrivateKey(); err != nil {
181 + return nil, err
182 + }
183 + }
184 +
185 + pk := node.PrivateKey.GetPublic()
186 + pkb, err := ic.MarshalPublicKey(pk)
187 + if err != nil {
188 + return nil, err
189 + }
190 + info.PublicKey = base64.StdEncoding.EncodeToString(pkb)
191 +
192 + if node.PeerHost != nil {
193 + for _, a := range node.PeerHost.Addrs() {
194 + s := a.String() + "/ipfs/" + info.ID
195 + info.Addresses = append(info.Addresses, s)
196 + }
197 + }
198 + info.ProtocolVersion = identify.IpfsVersion
199 + info.AgentVersion = identify.ClientVersion
200 + return info, nil
201 +}
core/core.go
+6 -6
@@ -67,8 +67,9 @@ type IpfsNode struct {
67 Repo repo.Repo
68
69 // Local node
70 - Pinning pin.Pinner // the pinning manager
71 - Mounts Mounts // current mount state, if any.
70 + Pinning pin.Pinner // the pinning manager
71 + Mounts Mounts // current mount state, if any.
72 + PrivateKey ic.PrivKey // the local node's private Key
73
74 // Services
75 Peerstore peer.Peerstore // storage for other Peer instances
@@ -78,7 +79,6 @@ type IpfsNode struct {
79 Resolver *path.Resolver // the path resolution system
80
81 // Online
81 - PrivateKey ic.PrivKey // the local node's private Key
82 PeerHost p2phost.Host // the network host (server+client)
83 Bootstrapper io.Closer // the periodic bootstrapper
84 Routing routing.IpfsRouting // the routing system. recommend ipfs-dht
@@ -222,7 +222,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, maybeRouter routing.
222 }
223
224 // load private key
225 - if err := n.loadPrivateKey(); err != nil {
225 + if err := n.LoadPrivateKey(); err != nil {
226 return err
227 }
228
@@ -368,7 +368,7 @@ func (n *IpfsNode) loadID() error {
368 return nil
369 }
370
371 -func (n *IpfsNode) loadPrivateKey() error {
371 +func (n *IpfsNode) LoadPrivateKey() error {
372 if n.Identity == "" || n.Peerstore == nil {
373 return debugerror.New("loaded private key out of order.")
374 }
@@ -400,7 +400,7 @@ func (n *IpfsNode) loadBootstrapPeers() ([]peer.PeerInfo, error) {
400 // uses it to instantiate a routing system in offline mode.
401 // This is primarily used for offline ipns modifications.
402 func (n *IpfsNode) SetupOfflineRouting() error {
403 - err := n.loadPrivateKey()
403 + err := n.LoadPrivateKey()
404 if err != nil {
405 return err
406 }