From d21f20d3a58a4a3f95fdedb0910da1d4b6a2e388 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sun, 21 Dec 2014 05:37:46 -0800 Subject: [PATCH] Commands learned to use peer.ID --- cmd/ipfs/init.go | 2 +- core/commands/id.go | 34 ++++++++++++++++++++++------------ core/commands/publish.go | 5 ++--- core/commands/resolve.go | 4 ++-- core/commands/swarm.go | 21 +++++++-------------- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 78054c8ff..bc609659f 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -266,7 +266,7 @@ func identityConfig(nbits int) (config.Identity, error) { } ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes) - id, err := peer.IDFromPubKey(pk) + id, err := peer.IDFromPublicKey(pk) if err != nil { return ident, err } diff --git a/core/commands/id.go b/core/commands/id.go index 25bc9fdc3..96e462a09 100644 --- a/core/commands/id.go +++ b/core/commands/id.go @@ -11,6 +11,7 @@ import ( b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58" cmds "github.com/jbenet/go-ipfs/commands" + ic "github.com/jbenet/go-ipfs/crypto" "github.com/jbenet/go-ipfs/peer" kb "github.com/jbenet/go-ipfs/routing/kbucket" u "github.com/jbenet/go-ipfs/util" @@ -49,7 +50,7 @@ if no peer is specified, prints out local peers info. } if len(req.Arguments()) == 0 { - return printPeer(node.Identity) + return printPeer(node.Peerstore, node.Identity) } pid := req.Arguments()[0] @@ -72,7 +73,7 @@ if no peer is specified, prints out local peers info. if err != nil { return nil, err } - return printPeer(p) + return printPeer(node.Peerstore, p.ID) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { @@ -87,27 +88,36 @@ if no peer is specified, prints out local peers info. Type: &IdOutput{}, } -func printPeer(p peer.Peer) (interface{}, error) { - if p == nil { +func printPeer(ps peer.Peerstore, p peer.ID) (interface{}, error) { + if p == "" { return nil, errors.New("Attempted to print nil peer!") } - info := new(IdOutput) - info.ID = p.ID().String() - if p.PubKey() != nil { - pkb, err := p.PubKey().Bytes() + info := new(IdOutput) + info.ID = p.Pretty() + + if pk := ps.PubKey(p); pk != nil { + pkb, err := ic.MarshalPublicKey(pk) if err != nil { return nil, err } info.PublicKey = base64.StdEncoding.EncodeToString(pkb) } - for _, a := range p.Addresses() { + + for _, a := range ps.Addresses(p) { info.Addresses = append(info.Addresses, a.String()) } - agent, protocol := p.GetVersions() - info.AgentVersion = agent - info.ProtocolVersion = protocol + if v, err := ps.Get(p, "ProtocolVersion"); err == nil { + if vs, ok := v.(string); ok { + info.AgentVersion = vs + } + } + if v, err := ps.Get(p, "AgentVersion"); err == nil { + if vs, ok := v.(string); ok { + info.ProtocolVersion = vs + } + } return info, nil } diff --git a/core/commands/publish.go b/core/commands/publish.go index f7165394e..97455b9c7 100644 --- a/core/commands/publish.go +++ b/core/commands/publish.go @@ -57,7 +57,7 @@ Publish a to another public key: return nil, errNotOnline } - if n.Identity == nil { + if n.Identity == "" { return nil, errors.New("Identity not loaded!") } @@ -75,8 +75,7 @@ Publish a to another public key: } // TODO n.Keychain.Get(name).PrivKey - k := n.Identity.PrivKey() - return publish(n, k, ref) + return publish(n, n.PrivateKey, ref) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { diff --git a/core/commands/resolve.go b/core/commands/resolve.go index 70ee9735e..dad72d755 100644 --- a/core/commands/resolve.go +++ b/core/commands/resolve.go @@ -52,10 +52,10 @@ Resolve te value of another name: } if len(req.Arguments()) == 0 { - if n.Identity == nil { + if n.Identity == "" { return nil, errors.New("Identity not loaded!") } - name = n.Identity.ID().String() + name = n.Identity.Pretty() } else { name = req.Arguments()[0] diff --git a/core/commands/swarm.go b/core/commands/swarm.go index c7fd7218c..9b583f3ae 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -58,7 +58,7 @@ ipfs swarm peers lists the set of peers this node is connected to. conns := n.Network.Conns() addrs := make([]string, len(conns)) for i, c := range conns { - pid := c.RemotePeer().ID() + pid := c.RemotePeer() addr := c.RemoteMultiaddr() addrs[i] = fmt.Sprintf("%s/%s", addr, pid) } @@ -106,7 +106,7 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/QmaCpDMGvV2BGHeYERUEnRQAwe3N8Szb output := make([]string, len(peers)) for i, p := range peers { - output[i] = "connect " + p.ID().String() + output[i] = "connect " + p.Pretty() err := n.Network.DialPeer(ctx, p) if err != nil { @@ -149,7 +149,7 @@ func splitAddresses(addrs []string) (maddrs []ma.Multiaddr, pids []peer.ID, err if err != nil { return nil, nil, cmds.ClientError("invalid peer address: " + err.Error()) } - id, err := peer.DecodePrettyID(path.Base(addr)) + id, err := peer.IDB58Decode(path.Base(addr)) if err != nil { return nil, nil, err } @@ -161,21 +161,14 @@ func splitAddresses(addrs []string) (maddrs []ma.Multiaddr, pids []peer.ID, err // peersWithAddresses is a function that takes in a slice of string peer addresses // (multiaddr + peerid) and returns a slice of properly constructed peers -func peersWithAddresses(ps peer.Peerstore, addrs []string) ([]peer.Peer, error) { +func peersWithAddresses(ps peer.Peerstore, addrs []string) ([]peer.ID, error) { maddrs, pids, err := splitAddresses(addrs) if err != nil { return nil, err } - peers := make([]peer.Peer, len(pids)) - for i, pid := range pids { - p, err := ps.FindOrCreate(pid) - if err != nil { - return nil, err - } - - p.AddAddress(maddrs[i]) - peers[i] = p + for i, p := range pids { + ps.AddAddress(p, maddrs[i]) } - return peers, nil + return pids, nil }