Multiaddr to remove from this Multiaddr
import { multiaddr } from '@multiformats/multiaddr'
const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080')
// Multiaddr(/ip4/8.8.8.8/tcp/1080)
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/4001')
// Multiaddr(/ip4/127.0.0.1/tcp/4001)
const mh3 = mh1.encapsulate(mh2)
// Multiaddr(/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001)
mh3.decapsulate(mh2).toString()
// '/ip4/8.8.8.8/tcp/1080'
A more reliable version of decapsulate
if you are targeting a
specific code, such as 421 (the p2p
protocol code). The last index of the code
will be removed from the Multiaddr
, and a new instance will be returned.
If the code is not present, the original Multiaddr
is returned.
import { multiaddr } from '@multiformats/multiaddr'
const addr = multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
// Multiaddr(/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC)
addr.decapsulateCode(421).toString()
// '/ip4/0.0.0.0/tcp/8080'
multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString()
// '/ip4/127.0.0.1/tcp/8080'
Encapsulates a Multiaddr in another Multiaddr
Multiaddr to add into this Multiaddr
import { multiaddr } from '@multiformats/multiaddr'
const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080')
// Multiaddr(/ip4/8.8.8.8/tcp/1080)
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/4001')
// Multiaddr(/ip4/127.0.0.1/tcp/4001)
const mh3 = mh1.encapsulate(mh2)
// Multiaddr(/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001)
mh3.toString()
// '/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001'
Checks if two Multiaddrs are the same
import { multiaddr } from '@multiformats/multiaddr'
const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080')
// Multiaddr(/ip4/8.8.8.8/tcp/1080)
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/4001')
// Multiaddr(/ip4/127.0.0.1/tcp/4001)
mh1.equals(mh1)
// true
mh1.equals(mh2)
// false
Extract the path if the multiaddr contains one
import { multiaddr } from '@multiformats/multiaddr'
const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock')
// Multiaddr(/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock)
// should return utf8 string or null if the id is missing or invalid
const path = mh1.getPath()
Extract the peerId if the multiaddr contains one
import { multiaddr } from '@multiformats/multiaddr'
const mh1 = multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
// Multiaddr(/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string)
// should return QmValidBase58string or null if the id is missing or invalid
const peerId = mh1.getPeerId()
Returns if a Multiaddr is a Thin Waist address or not.
Thin Waist is if a Multiaddr adheres to the standard combination of:
{IPv4, IPv6}/{TCP, UDP}
Optional
addr: Multiaddrimport { multiaddr } from '@multiformats/multiaddr'
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/4001')
// Multiaddr(/ip4/127.0.0.1/tcp/4001)
const mh2 = multiaddr('/ip4/192.168.2.1/tcp/5001')
// Multiaddr(/ip4/192.168.2.1/tcp/5001)
const mh3 = mh1.encapsulate(mh2)
// Multiaddr(/ip4/127.0.0.1/tcp/4001/ip4/192.168.2.1/tcp/5001)
const mh4 = multiaddr('/ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a')
// Multiaddr(/ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a)
mh1.isThinWaistAddress()
// true
mh2.isThinWaistAddress()
// true
mh3.isThinWaistAddress()
// false
mh4.isThinWaistAddress()
// false
Gets a Multiaddrs node-friendly address object. Note that protocol information is left out: in Node (and most network systems) the protocol is unknowable given only the address.
Has to be a ThinWaist Address, otherwise throws error
import { multiaddr } from '@multiformats/multiaddr'
multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress()
// {family: 4, address: '127.0.0.1', port: 4001}
Returns the codes of the protocols in left-to-right order. See list of protocols
import { multiaddr } from '@multiformats/multiaddr'
multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes()
// [ 4, 6 ]
Returns the names of the protocols in left-to-right order. See list of protocols
import { multiaddr } from '@multiformats/multiaddr'
multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames()
// [ 'ip4', 'tcp' ]
Returns the protocols the Multiaddr is defined with, as an array of objects, in left-to-right order. Each object contains the protocol code, protocol name, and the size of its address space in bits. See list of protocols
import { multiaddr } from '@multiformats/multiaddr'
multiaddr('/ip4/127.0.0.1/tcp/4001').protos()
// [ { code: 4, size: 32, name: 'ip4' },
// { code: 6, size: 16, name: 'tcp' } ]
Resolve multiaddr if containing resolvable hostname.
Optional
options: ResolveOptionsimport { multiaddr, resolvers } from '@multiformats/multiaddr'
resolvers.set('dnsaddr', resolverFunction)
const mh1 = multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
const resolvedMultiaddrs = await mh1.resolve()
// [
// Multiaddr(/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb),
// Multiaddr(/ip4/147.75.83.83/tcp/443/wss/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb),
// Multiaddr(/ip4/147.75.83.83/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb)
// ]
Returns a tuple of string/number parts
import { multiaddr } from '@multiformats/multiaddr'
multiaddr('/ip4/127.0.0.1/tcp/4001').stringTuples()
// [ [ 4, '127.0.0.1' ], [ 6, '4001' ] ]
Returns Multiaddr as a convinient options object to be used with net.createConnection
import { multiaddr } from '@multiformats/multiaddr'
multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions()
// { family: 4, host: '127.0.0.1', transport: 'tcp', port: 4001 }
Decapsulates a Multiaddr from another Multiaddr