mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-05 16:28:06 +08:00
Commands learned to use peer.ID
This commit is contained in:
parent
e6fc31bbc4
commit
d21f20d3a5
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ Publish a <ref> 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 <ref> 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) {
|
||||
|
||||
@ -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]
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user