interface Multiaddr {
    bytes: Uint8Array;
    decapsulate(addr): Multiaddr;
    decapsulateCode(code): Multiaddr;
    encapsulate(addr): Multiaddr;
    equals(addr): boolean;
    getPath(): null | string;
    getPeerId(): null | string;
    isThinWaistAddress(addr?): boolean;
    nodeAddress(): NodeAddress;
    protoCodes(): number[];
    protoNames(): string[];
    protos(): Protocol[];
    resolve(options?): Promise<Multiaddr[]>;
    stringTuples(): StringTuple[];
    toJSON(): string;
    toOptions(): MultiaddrObject;
    toString(): string;
    tuples(): Tuple[];
}

Properties

bytes: Uint8Array

Methods

  • Decapsulates a Multiaddr from another Multiaddr

    Parameters

    • addr: string | Multiaddr

      Multiaddr to remove from this Multiaddr

    Returns Multiaddr

    Example

    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.

    Parameters

    • code: number

    Returns Multiaddr

    Example

    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

    Parameters

    Returns Multiaddr

    Example

    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

    Parameters

    Returns boolean

    Example

    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

    Returns null | string

    Example

    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

    Returns null | string

    Example

    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}

    Parameters

    Returns boolean

    Example

    import { 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

    Returns NodeAddress

    Example

    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

    Returns number[]

    Example

    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

    Returns string[]

    Example

    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

    Returns Protocol[]

    Example

    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.

    Parameters

    Returns Promise<Multiaddr[]>

    Example

    import { 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

    • tuples[][0] = code of protocol
    • tuples[][1] = contents of address

    Returns StringTuple[]

    Example

    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 JSON encoded object

    Returns string

    Example

    import { multiaddr } from '@multiformats/multiaddr'

    JSON.stringify(multiaddr('/ip4/127.0.0.1/tcp/4001'))
    // '/ip4/127.0.0.1/tcp/4001'
  • Returns Multiaddr as a convinient options object to be used with net.createConnection

    Returns MultiaddrObject

    Example

    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 }
  • Returns Multiaddr as a String

    Returns string

    Example

    import { multiaddr } from '@multiformats/multiaddr'

    multiaddr('/ip4/127.0.0.1/tcp/4001').toString()
    // '/ip4/127.0.0.1/tcp/4001'
  • Returns a tuple of parts

    Returns Tuple[]

    Example

    import { multiaddr } from '@multiformats/multiaddr'

    multiaddr('/ip4/127.0.0.1/tcp/4001').tuples()
    // [ [ 4, <Buffer 7f 00 00 01> ], [ 6, <Buffer 0f a1> ] ]