func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) { p, err := peer.IDFromPublicKey(k.GetPublic()) if err != nil { return nil, err } ps := pstore.NewPeerstore() ps.AddAddr(p, a, pstore.PermanentAddrTTL) ps.AddPrivKey(p, k) ps.AddPubKey(p, k.GetPublic()) return mn.AddPeerWithPeerstore(p, ps) }
func testHasPublicKey(t *testing.T, h host.Host, p peer.ID, shouldBe ic.PubKey) { k := h.Peerstore().PubKey(p) if k == nil { t.Error("no public key") return } if !k.Equals(shouldBe) { t.Error("key mismatch") return } p2, err := peer.IDFromPublicKey(k) if err != nil { t.Error("could not make key") } else if p != p2 { t.Error("key does not match peerid") } }
func main() { size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate") typ := flag.String("type", "RSA", "select type of key to generate (RSA or Ed25519)") flag.Parse() var atyp int switch strings.ToLower(*typ) { case "rsa": atyp = ci.RSA case "ed25519": atyp = ci.Ed25519 default: fmt.Fprintln(os.Stderr, "unrecognized key type: ", *typ) os.Exit(1) } fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ) priv, pub, err := ci.GenerateKeyPair(atyp, *size) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Fprintln(os.Stderr, "Success!") pid, err := peer.IDFromPublicKey(pub) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Fprintf(os.Stderr, "ID for generated key: %s\n", pid.Pretty()) data, err := priv.Bytes() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } os.Stdout.Write(data) }
func (ids *IDService) consumeReceivedPubKey(c inet.Conn, kb []byte) { lp := c.LocalPeer() rp := c.RemotePeer() if kb == nil { log.Debugf("%s did not receive public key for remote peer: %s", lp, rp) return } newKey, err := ic.UnmarshalPublicKey(kb) if err != nil { log.Errorf("%s cannot unmarshal key from remote peer: %s", lp, rp) return } // verify key matches peer.ID np, err := peer.IDFromPublicKey(newKey) if err != nil { log.Debugf("%s cannot get peer.ID from key of remote peer: %s, %s", lp, rp, err) return } if np != rp { // if the newKey's peer.ID does not match known peer.ID... if rp == "" && np != "" { // if local peerid is empty, then use the new, sent key. err := ids.Host.Peerstore().AddPubKey(rp, newKey) if err != nil { log.Debugf("%s could not add key for %s to peerstore: %s", lp, rp, err) } } else { // we have a local peer.ID and it does not match the sent key... error. log.Errorf("%s received key for remote peer %s mismatch: %s", lp, rp, np) } return } currKey := ids.Host.Peerstore().PubKey(rp) if currKey == nil { // no key? no auth transport. set this one. err := ids.Host.Peerstore().AddPubKey(rp, newKey) if err != nil { log.Debugf("%s could not add key for %s to peerstore: %s", lp, rp, err) } return } // ok, we have a local key, we should verify they match. if currKey.Equals(newKey) { return // ok great. we're done. } // weird, got a different key... but the different key MATCHES the peer.ID. // this odd. let's log error and investigate. this should basically never happen // and it means we have something funky going on and possibly a bug. log.Errorf("%s identify got a different key for: %s", lp, rp) // okay... does ours NOT match the remote peer.ID? cp, err := peer.IDFromPublicKey(currKey) if err != nil { log.Errorf("%s cannot get peer.ID from local key of remote peer: %s, %s", lp, rp, err) return } if cp != rp { log.Errorf("%s local key for remote peer %s yields different peer.ID: %s", lp, rp, cp) return } // okay... curr key DOES NOT match new key. both match peer.ID. wat? log.Errorf("%s local key and received key for %s do not match, but match peer.ID", lp, rp) }