@multiformats/multiaddr
TypeScript icon, indicating that this package has built-in type declarations

12.4.4 • Public • Published

@multiformats/multiaddr

multiformats.io codecov CI

multiaddr implementation (binary + string representation of network addresses)

About

A standard way to represent addresses that

  • support any standard network protocol
  • are self-describing
  • have a binary packed format
  • have a nice string representation
  • encapsulate well

Example

import { multiaddr } from '@multiformats/multiaddr'

const addr = multiaddr('/ip4/127.0.0.1/udp/1234')
// Multiaddr(/ip4/127.0.0.1/udp/1234)

addr.bytes
// <Uint8Array 04 7f 00 00 01 11 04 d2>

addr.toString()
// '/ip4/127.0.0.1/udp/1234'

addr.protos()
// [
//   {code: 4, name: 'ip4', size: 32},
//   {code: 273, name: 'udp', size: 16}
// ]

// gives you an object that is friendly with what Node.js core modules expect for addresses
addr.nodeAddress()
// {
//   family: 4,
//   port: 1234,
//   address: "127.0.0.1"
// }

addr.encapsulate('/sctp/5678')
// Multiaddr(/ip4/127.0.0.1/udp/1234/sctp/5678)

Resolving DNSADDR addresses

DNSADDR is a spec that allows storing a TXT DNS record that contains a Multiaddr.

To resolve DNSADDR addresses, call the .resolve() function the multiaddr, optionally passing a DNS resolver.

DNSADDR addresses can resolve to multiple multiaddrs, since there is no limit to the number of TXT records that can be stored.

Example - Resolving DNSADDR Multiaddrs

import { multiaddr, resolvers } from '@multiformats/multiaddr'
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'

resolvers.set('dnsaddr', dnsaddrResolver)

const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')

// resolve with a 5s timeout
const resolved = await ma.resolve({
  signal: AbortSignal.timeout(5000)
})

console.info(resolved)
// [Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...')...]

Example - Using a custom DNS resolver to resolve DNSADDR Multiaddrs

See the docs for @multiformats/dns for a full breakdown of how to specify multiple resolvers or resolvers that can be used for specific TLDs.

import { multiaddr } from '@multiformats/multiaddr'
import { dns } from '@multiformats/dns'
import { dnsJsonOverHttps } from '@multiformats/dns/resolvers'

const resolver = dns({
  resolvers: {
    '.': dnsJsonOverHttps('https://cloudflare-dns.com/dns-query')
  }
})

const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')
const resolved = await ma.resolve({
 dns: resolver
})

console.info(resolved)
// [Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...')...]

Install

$ npm i @multiformats/multiaddr

Browser <script> tag

Loading this module through a script tag will make its exports available as MultiformatsMultiaddr in the global namespace.

<script src="https://unpkg.com/@multiformats/multiaddr/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
12.4.4
1,823latest

Version History

VersionDownloads (Last 7 Days)Published
12.4.4
1,823
12.4.3
161
12.4.2
310
12.4.1
44,941
12.4.0
27,174
12.3.5
1,293
12.3.4
5,972
12.3.3
289
12.3.2
0
12.3.1
108,166
12.3.0
18,930
12.2.3
1,167
12.2.2
0
12.2.1
11,721
12.2.0
54
12.1.14
1,369
12.1.13
0
12.1.12
1,787
12.1.11
985
12.1.10370
12.1.90
12.1.81,212
12.1.71,335
12.1.614,718
12.1.5241
12.1.40
12.1.33,241
12.1.225,988
12.1.14
12.1.0357
12.0.0497
11.6.147,353
11.6.04,485
11.5.00
11.4.01,628
11.3.01,421
11.2.00
11.1.530
11.1.445
11.1.31
11.1.20
11.1.11
11.1.01,573
11.0.120
11.0.110
11.0.100
11.0.90
11.0.80
11.0.7943
11.0.625
11.0.552
11.0.40
11.0.32,039
11.0.219
11.0.113
11.0.040
10.5.02,499
10.4.332
10.4.20
10.4.111
10.4.08
10.3.50
10.3.41
10.3.3162
10.3.20
10.3.10
10.3.00
10.2.1308
10.2.012
10.1.82
10.1.712
10.1.60
10.1.51
10.1.40
10.1.324
10.1.20
10.1.10
10.1.00
0.0.00

Package Sidebar

Install

npm i @multiformats/multiaddr

Weekly Downloads

336,845

Version

12.4.4

License

Apache-2.0 OR MIT

Unpacked Size

437 kB

Total Files

70

Last publish

Collaborators

  • daviddias
  • vmx
  • mikeal
  • hugomrdias
  • alanshaw
  • achingbrain
  • vascosantos
  • rvagg
  • lidel
  • npm-service-account-multiformats