Skip to content

Latest commit

 

History

History
209 lines (151 loc) · 5.18 KB

API.md

File metadata and controls

209 lines (151 loc) · 5.18 KB

API

addressSort.publicAddressesFirst(addresses)

Sort given addresses by putting public addresses first. In case of equality, a certified address will come first.

Parameters

Name Type Description
addresses Array<Address> Array of AddressBook addresses

Returns

Type Description
Array<Address> returns array of sorted addresses

Example

const multiaddr = require('multiaddr')
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')

const addresses = [
    {
        multiaddr: multiaddr('/ip4/127.0.0.1/tcp/4000'),
        isCertified: false
    },
    {
        multiaddr: multiaddr('/ip4/30.0.0.1/tcp/4000'),
        isCertified: false
    }
]

const sortedAddresses = publicAddressesFirst(addresses)

arrayEquals(a, b)

Verify if two arrays of non primitive types with the "equals" function are equal. Compatible with multiaddr, peer-id and others.

Parameters

Name Type Description
a Array<*> First array to verify
b Array<*> Second array to verify

Returns

Type Description
boolean returns true if arrays are equal, false otherwise

Example

const PeerId = require('peer-id')
const arrayEquals = require('libp2p-utils/src/array-equals')

const peerId1 = await PeerId.create()
const peerId2 = await PeerId.create()

const equals = arrayEquals([peerId1], [peerId2])

multiaddr .isLoopback(ma)

Check if a given multiaddr is a loopback address.

Parameters

Name Type Description
ma Multiaddr multiaddr to verify

Returns

Type Description
boolean returns true if multiaddr is a loopback address, false otherwise

Example

const multiaddr = require('multiaddr')
const isLoopback = require('libp2p-utils/src/multiaddr/is-loopback')

const ma = multiaddr('/ip4/127.0.0.1/tcp/1000')
isMultiaddrLoopbackAddrs = isLoopback(ma)

multiaddr .isPrivate(ma)

Check if a given multiaddr has a private address.

Parameters

Name Type Description
ma Multiaddr multiaddr to verify

Returns

Type Description
boolean returns true if multiaddr is a private address, false otherwise

Example

const multiaddr = require('multiaddr')
const isPrivate = require('libp2p-utils/src/multiaddr/is-private')

const ma = multiaddr('/ip4/10.0.0.1/tcp/1000')
isMultiaddrPrivateAddrs = isPrivate(ma)

ipPortToMultiaddr(ip, port)

Transform an IP, Port pair into a multiaddr with tcp transport.

Parameters

Name Type Description
ip string ip for multiaddr
port `number string`

Returns

Type Description
Multiaddr returns created multiaddr

Example

const ipPortPairToMultiaddr = require('libp2p-utils/src/multiaddr/ip-port-to-multiaddr')
const ip = '127.0.0.1'
const port = '9090'

const ma = ipPortPairToMultiaddr(ma)

streamToMaConnection(streamProperties, options)

Convert a duplex stream into a MultiaddrConnection.

Parameters

Name Type Description
streamProperties object duplex stream properties
streamProperties.stream DuplexStream duplex stream
streamProperties.remoteAddr Multiaddr stream remote address
streamProperties.localAddr Multiaddr stream local address
[options] object options
[options.signal] AbortSignal abort signal

Returns

Type Description
Connection returns a multiaddr Connection

Example

const streamToMaConnection = require('libp2p-utils/src/stream-to-ma-conn')

const stream = {
  sink: async source => {/* ... */},
  source: { [Symbol.asyncIterator] () {/* ... */} }
}

const conn = streamToMaConnection({
  stream,
  remoteAddr: /* ... */
  localAddr; /* ... */
})