func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) { // TODO consider stricter error handling // TODO consider Criticalf error logging for _, p := range cfg.Bootstrap { if p.PeerID == "" { log.Criticalf("error: peer does not include PeerID. %v", p) } maddr, err := ma.NewMultiaddr(p.Address) if err != nil { log.Error(err) continue } // setup peer npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID)) if err != nil { log.Criticalf("Bootstrapping error: %v", err) continue } npeer.AddAddress(maddr) if _, err = route.Connect(ctx, npeer); err != nil { log.Criticalf("Bootstrapping error: %v", err) continue } log.Event(ctx, "bootstrap", npeer) } }
// 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) { 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.Get(pid) if err != nil { return nil, err } p.AddAddress(maddrs[i]) peers[i] = p } return peers, nil }
// getOrConstructPeer attempts to fetch a peer from a peerstore. // if succeeds, verify ID and PubKey match. // else, construct it. func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (peer.Peer, error) { rid, err := peer.IDFromPubKey(rpk) if err != nil { return nil, err } npeer, err := peers.Get(rid) if err != nil { return nil, err // unexpected error happened. } // public key verification happens in Peer.VerifyAndSetPubKey if err := npeer.VerifyAndSetPubKey(rpk); err != nil { return nil, err // pubkey mismatch or other problem } return npeer, nil }
func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (peer.Peer, error) { if cfg.PeerID == "" { return nil, debugerror.New("Identity was not set in config (was ipfs init run?)") } if len(cfg.PeerID) == 0 { return nil, debugerror.New("No peer ID in config! (was ipfs init run?)") } // get peer from peerstore (so it is constructed there) id := peer.ID(b58.Decode(cfg.PeerID)) self, err := peers.Get(id) if err != nil { return nil, err } self.SetType(peer.Local) self, err = peers.Add(self) if err != nil { return nil, err } self.SetVersions(handshake.ClientVersion, handshake.IpfsVersion.String()) // when not online, don't need to parse private keys (yet) if online { skb, err := base64.StdEncoding.DecodeString(cfg.PrivKey) if err != nil { return nil, err } if err := self.LoadAndVerifyKeyPair(skb); err != nil { return nil, err } } return self, nil }