Getting the peer multiaddr from IPFS Swarm peers

I’m trying to decode the buffer stored in the addr key found in the output returned by the IPFS swarm peers function back to the original string. I tried to use the toString() method with all supported encodings, but the output I’m getting is not correctly decoded.

const swarm_peers = await node.swarm.peers(); 
console.log(swarm_peers['2']['addr']['buffer'].toString());

Below is a sample output of what I’m getting

6sgp-1.bootstrap.libp2p.io���" BD�38\

How do I correctly decode the buffer?

Tried base58/64?

Will check it out tomorrow. Going to sleep :sleeping: now :slight_smile:

Hey, yeah I tried base64, it too gave incorrect output. Base58 I think isn’t supported (check this - https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings)

Hey!

The buffer is based on js-multiaddr. You can see the examples in the repo to play with the buffer.

Here how you can read it:

  const peerInfos = await ipfs.swarm.peers();
  console.log(peerInfos[2].addr.toString());

Oh great, works perfectly! Thanks a lot

1 Like