krithik
January 23, 2020, 7:40pm
#1
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 now
krithik
January 24, 2020, 6:04am
#3
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());
krithik
January 26, 2020, 3:30pm
#5
Oh great, works perfectly! Thanks a lot
1 Like