js-peer-info
A PeerInfo object contains information about a
PeerID and its
multiaddrs. This module is used by
IPFS and
libp2p.
Table of Contents
Installation
npm
> npm i peer-info
Node.JS, Browserify, Webpack
const PeerInfo = require('peer-info')
Browser: <script>
Tag
Loading this module through a script tag will make the PeerInfo
obj available in the global namespace.
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
Usage
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const peer = new PeerInfo()
peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/tcp/5001'))
peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/udp/8001'))
peer.multiaddr.add(multiaddr('/sonic/bfsk/697/1209'))
API
const PeerInfo = require('peer-info')
PeerInfo.create([id, ] callback)
id: PeerID
, optionalcallback: Function
Creates a new PeerInfo instance and if no id
is passed it
generates a new underlying PeerID
for it.
new PeerInfo(id)
Creates a new PeerInfo instance from an existing PeerID.
multiaddrs
A list of multiaddresses instances that peer
can be reached at.
multiaddr.add(addr)
Adds a new multiaddress that peer
can be reached at. addr
is an instance of
a multiaddr.
multiaddr.addSafe(addr)
The addSafe
call, in comparison to add
, will only add the multiaddr to
multiaddrs
if the same multiaddr tries to be added twice.
This is a simple mechanism to prevent multiaddrs
from becoming bloated with
unusable addresses, which happens when we exchange observed multiaddrs with
peers which will not provide a useful multiaddr to be shared to the rest of the
network (e.g. a multiaddr referring to a peer inside a LAN being shared to the
outside world).
multiaddr.rm(addr)
Removes a multiaddress instance addr
from peer
.
multiaddr.replace(existing, fresh)
existing: Multiaddr
fresh: Multiaddr
Removes the array of multiaddresses existing
from peer
, and adds the array
of multiaddresses fresh
.
Contribute
PRs accepted.
Small note: If editing the Readme, please conform to the standard-readme specification.
License
MIT © David Dias