addressSort.publicAddressesFirst(addresses)
Sort given addresses by putting public addresses first. In case of equality, a certified address will come first.
Name
Type
Description
addresses
Array<Address>
Array of AddressBook addresses
Type
Description
Array<Address>
returns array of sorted addresses
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 )
Verify if two arrays of non primitive types with the "equals" function are equal.
Compatible with multiaddr, peer-id and others.
Name
Type
Description
a
Array<*>
First array to verify
b
Array<*>
Second array to verify
Type
Description
boolean
returns true if arrays are equal, false otherwise
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.
Name
Type
Description
ma
Multiaddr
multiaddr to verify
Type
Description
boolean
returns true if multiaddr is a loopback address, false otherwise
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 )
Check if a given multiaddr has a private address.
Name
Type
Description
ma
Multiaddr
multiaddr to verify
Type
Description
boolean
returns true if multiaddr is a private address, false otherwise
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.
Name
Type
Description
ip
string
ip for multiaddr
port
`number
string`
Type
Description
Multiaddr
returns created multiaddr
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 .
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
Type
Description
Connection
returns a multiaddr Connection
const streamToMaConnection = require ( 'libp2p-utils/src/stream-to-ma-conn' )
const stream = {
sink : async source => { /* ... */ } ,
source : { [ Symbol . asyncIterator ] ( ) { /* ... */ } }
}
const conn = streamToMaConnection ( {
stream,
remoteAddr : /* ... */
localAddr ; /* ... */
} )