Checking existence of a libp2p-peer without libp2p

Hi,

I recently built an API that is used for exchanging nodes for occasions, where auto-peering is not available. The API is built in PHP and I cannot integrate libp2p directly. However I want to check, if the entered peers exist - I have a valid way to do that right now, but for convenience I would like to make this even better.
My actual question is: Is it possible to check for a peers existence without even using libp2p itself. By the time of checking I have knowledge of the complete peer ID. I just don’t know how to check against the peer.

Thanks for any help or idea!

Can you clarify what you mean by “check for a peer’s existence”?

Reading between the lines, it sounds like you have a multiaddress (complete with a peer’s ID) and that you’re looking for a way to quickly check if the expected peer is listening at that address.

Assuming this is correct, there are two parts to your problem:

  1. Connecting to the address
  2. Authenticating the peer

Problem 1 is trivial; it’s just a question of establishing the appropriate transport connection (usually TCP or WebSocket).

Problem 2 is only slightly trickier. You’ll have to implement a (pretty small) subset of libp2p’s wire protocols. My advice is to begin by manually exploring with netcat (or something like websocat if you’re using websockets). This will allow you to figure out individual steps before coding it up in PHP.

Let’s assume we have the address /ip4/127.0.0.1/tcp/52069/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N. This obviously calls for a TCP connection, so let’s use nc to solve problem 1:

$ nc 127.0.0.1 52069
/multistream/1.0.0

Notice the command blocks, as it is waiting for input (I don’t know how familiar you are with nc, so I’m erring on the side of over-explaining). You can type input, which will be transmitted to the remote peer, and you will be able to observe its response. In this way, you can test things quickly.

The output seems to indicate some kind of protocol negotiation, as if the peer is announcing that it supports multistream v1.0.0. Let’s investigate how this works. My first stop for such things is the libp2p spec repository. The connection spec seems like a good bet. In particular, the section on protocol negotiation catches my eye.

Under this section, you’ll find a helpful diagram for understanding protocol negotiation:

The gist of it seems to be:

  1. Handshake to agree on whether to use mplex or multistream for the rest of the session.
  2. Send protocol ID
  3. If protocol ID is echoed back, you may start using the protocol.

In your case, I suspect the protocol you want to use is identify. It seems like this will confirm the identity of the peer.

It’s worth noting that I didn’t originally know how to answer your question. The general approach for solving this kind of problem is to refer to the specs repository. I literally did this while typing my answer :slight_smile:

Hope this helps!

Thank you very much for the detailed answer! Now I have a direction to go for (which I was lacking before). I will go through it. I’m pretty sure, this will solve my problem. I try to get this working in PHP and will eventually report back (may take some days).

1 Like