Retrieve PeerID from the connected peer through streamHandler function

I am new to libp2p and currently trying to create a p2p application using go-libp2p. I’m asking this because I couldn’t find anything about this. I don’t know this whether this question is a very basic one. Please guide me.

setStreamHandler function sets a function to run when another peer node connected.
I want to get the peerID of that connected node.

host.SetStreamHandler(protocolID, handleStream)

I think getting peerID using this way is not possible because this handleStream function only has the network stream as a parameter:

func handleStream(stream network.Stream) {

     //need to find the peer ID before create a buffer stream
     rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream));
     //read and write

I need to do this because I’m saving buffer read writers (and several other data) of (previously) connected peers and I need to identify the peer and check if this peer has already connected & have a buffer read writer. So, that I can use that instead of creating a new read read writer!
Is there any way i can find peerID of the node connected to my node?

I think one solution is to ask peerID from the connected peer through the stream. But I think it is not a good solution and I need to figure out the peerID without bothering other peers!

Also, I saw there is PeerStore which we can save details of other peers. Can I solve this using PeerStore or Noice because this is a concern with identifying other peers.
Please help me to figure this out.

I found the solution after reading the stream interface in libp2p.
It’s a basic question and I am sorry for asking that (If someone got annoyed).


Is the solution. Conn() on network stream will returns the connection the stream made from and RemotePeer() on connection returns the Peer ID of the remote peer (stream’s other side).

Also, If we need multiAddrs of remote peer RemoteMultiaddr() can be used.