Esempio n. 1
0
// wrapConnAsProto establishes the moose protocol on the raw ipfs connection
func wrapConnAsProto(conn net.Conn, node *ipfsutil.Node, peerHash string) (*protocol.Protocol, error) {
	pub, err := node.PublicKeyFor(peerHash)
	if err != nil {
		return nil, err
	}

	priv, err := node.PrivateKey()
	if err != nil {
		return nil, err
	}

	authrw := security.NewAuthReadWriter(conn, priv, pub)
	if err := authrw.Trigger(); err != nil {
		return nil, err
	}

	return protocol.NewProtocol(authrw, true), nil
}
Esempio n. 2
0
func register(node *ipfsutil.Node, id ID) error {
	hash := id.Hash()

	peers, err := ipfsutil.Locate(node, hash, 1, 5*time.Second)
	if err != nil && err != util.ErrTimeout {
		return err
	}

	// Check if some id is our own:
	if len(peers) > 0 {
		self, err := node.Identity()
		if err != nil {
			return err
		}

		wasSelf := false
		for _, peer := range peers {
			if peer.ID == self {
				wasSelf = true
			}
		}

		if wasSelf {
			return ErrAlreadyRegistered
		}
	}

	// If it was an timeout, it's probably not yet registered.
	otherHash, err := ipfsutil.AddBlock(node, id.asBlockData())
	if !bytes.Equal(otherHash, hash) {
		log.Warningf("Hash differ during register; did the hash func changed?")
	}

	if err != nil {
		return err
	}

	return nil
}