From 581c4e558e34229ba0b45ea4a42d615dd5b4b1c2 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Tue, 3 Feb 2015 04:56:57 -0800 Subject: [PATCH] cmds/id: show self addrs --- core/commands/id.go | 33 ++++++++++++++++++++++++++++++++- core/core.go | 12 ++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/core/commands/id.go b/core/commands/id.go index 911d50397..15bc8b16e 100644 --- a/core/commands/id.go +++ b/core/commands/id.go @@ -14,8 +14,10 @@ import ( b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58" cmds "github.com/jbenet/go-ipfs/commands" + core "github.com/jbenet/go-ipfs/core" ic "github.com/jbenet/go-ipfs/p2p/crypto" "github.com/jbenet/go-ipfs/p2p/peer" + identify "github.com/jbenet/go-ipfs/p2p/protocol/identify" kb "github.com/jbenet/go-ipfs/routing/kbucket" u "github.com/jbenet/go-ipfs/util" ) @@ -63,7 +65,7 @@ ipfs id supports the format option for output with the following keys: } if len(req.Arguments()) == 0 { - output, err := printPeer(node.Peerstore, node.Identity) + output, err := printSelf(node) if err != nil { res.SetError(err, cmds.ErrNormal) return @@ -168,3 +170,32 @@ func printPeer(ps peer.Peerstore, p peer.ID) (interface{}, error) { return info, nil } + +// printing self is special cased as we get values differently. +func printSelf(node *core.IpfsNode) (interface{}, error) { + info := new(IdOutput) + info.ID = node.Identity.Pretty() + + if node.PrivateKey == nil { + if err := node.LoadPrivateKey(); err != nil { + return nil, err + } + } + + pk := node.PrivateKey.GetPublic() + pkb, err := ic.MarshalPublicKey(pk) + if err != nil { + return nil, err + } + info.PublicKey = base64.StdEncoding.EncodeToString(pkb) + + if node.PeerHost != nil { + for _, a := range node.PeerHost.Addrs() { + s := a.String() + "/ipfs/" + info.ID + info.Addresses = append(info.Addresses, s) + } + } + info.ProtocolVersion = identify.IpfsVersion + info.AgentVersion = identify.ClientVersion + return info, nil +} diff --git a/core/core.go b/core/core.go index f8c42a223..4703e8347 100644 --- a/core/core.go +++ b/core/core.go @@ -67,8 +67,9 @@ type IpfsNode struct { Repo repo.Repo // Local node - Pinning pin.Pinner // the pinning manager - Mounts Mounts // current mount state, if any. + Pinning pin.Pinner // the pinning manager + Mounts Mounts // current mount state, if any. + PrivateKey ic.PrivKey // the local node's private Key // Services Peerstore peer.Peerstore // storage for other Peer instances @@ -78,7 +79,6 @@ type IpfsNode struct { Resolver *path.Resolver // the path resolution system // Online - PrivateKey ic.PrivKey // the local node's private Key PeerHost p2phost.Host // the network host (server+client) Bootstrapper io.Closer // the periodic bootstrapper Routing routing.IpfsRouting // the routing system. recommend ipfs-dht @@ -222,7 +222,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, maybeRouter routing. } // load private key - if err := n.loadPrivateKey(); err != nil { + if err := n.LoadPrivateKey(); err != nil { return err } @@ -368,7 +368,7 @@ func (n *IpfsNode) loadID() error { return nil } -func (n *IpfsNode) loadPrivateKey() error { +func (n *IpfsNode) LoadPrivateKey() error { if n.Identity == "" || n.Peerstore == nil { return debugerror.New("loaded private key out of order.") } @@ -400,7 +400,7 @@ func (n *IpfsNode) loadBootstrapPeers() ([]peer.PeerInfo, error) { // uses it to instantiate a routing system in offline mode. // This is primarily used for offline ipns modifications. func (n *IpfsNode) SetupOfflineRouting() error { - err := n.loadPrivateKey() + err := n.LoadPrivateKey() if err != nil { return err }