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:
- Connecting to the address
- 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
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:
- Handshake to agree on whether to use mplex or multistream for the rest of the session.
- Send protocol ID
- 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
Hope this helps!