UNPKG

13.5 kBTypeScriptView Raw
1export type Resolver = (addr: Multiaddr) => Promise<string[]>;
2export type MultiaddrInput = string | Multiaddr | Uint8Array | null;
3export type MultiaddrObject = import('./types').MultiaddrObject;
4export type Protocol = import('./types').Protocol;
5/**
6 * Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
7 * a Uint8Array, String or another Multiaddr instance
8 * public key.
9 *
10 */
11export class Multiaddr {
12 /**
13 * Creates a Multiaddr from a node-friendly address object
14 *
15 * @example
16 * ```js
17 * Multiaddr.fromNodeAddress({address: '127.0.0.1', port: '4001'}, 'tcp')
18 * // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
19 * ```
20 *
21 * @param {{family: 4 | 6, address: string, port: number}} addr
22 * @param {string} transport
23 */
24 static fromNodeAddress(addr: {
25 family: 4 | 6;
26 address: string;
27 port: number;
28 }, transport: string): Multiaddr;
29 /**
30 * Returns if something is a Multiaddr that is a name
31 *
32 * @param {Multiaddr} addr
33 * @returns {boolean} isName
34 */
35 static isName(addr: Multiaddr): boolean;
36 /**
37 * Check if object is a CID instance
38 *
39 * @param {any} value
40 * @returns {value is Multiaddr}
41 */
42 static isMultiaddr(value: any): value is Multiaddr;
43 /**
44 * @example
45 * ```js
46 * new Multiaddr('/ip4/127.0.0.1/tcp/4001')
47 * // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
48 * ```
49 *
50 * @param {MultiaddrInput} [addr] - If String or Uint8Array, needs to adhere to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
51 */
52 constructor(addr?: MultiaddrInput | undefined);
53 /** @type {Uint8Array} - The raw bytes representing this multiaddress */
54 bytes: Uint8Array;
55 /**
56 * Returns Multiaddr as a String
57 *
58 * @example
59 * ```js
60 * new Multiaddr('/ip4/127.0.0.1/tcp/4001').toString()
61 * // '/ip4/127.0.0.1/tcp/4001'
62 * ```
63 */
64 toString(): string;
65 /**
66 * Returns Multiaddr as a JSON encoded object
67 *
68 * @example
69 * ```js
70 * JSON.stringify(new Multiaddr('/ip4/127.0.0.1/tcp/4001'))
71 * // '/ip4/127.0.0.1/tcp/4001'
72 * ```
73 */
74 toJSON(): string;
75 /**
76 * Returns Multiaddr as a convinient options object to be used with net.createConnection
77 *
78 * @example
79 * ```js
80 * new Multiaddr('/ip4/127.0.0.1/tcp/4001').toOptions()
81 * // { family: 4, host: '127.0.0.1', transport: 'tcp', port: 4001 }
82 * ```
83 */
84 toOptions(): import("./types").MultiaddrObject;
85 /**
86 * Returns the protocols the Multiaddr is defined with, as an array of objects, in
87 * left-to-right order. Each object contains the protocol code, protocol name,
88 * and the size of its address space in bits.
89 * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
90 *
91 * @example
92 * ```js
93 * new Multiaddr('/ip4/127.0.0.1/tcp/4001').protos()
94 * // [ { code: 4, size: 32, name: 'ip4' },
95 * // { code: 6, size: 16, name: 'tcp' } ]
96 * ```
97 *
98 * @returns {Protocol[]} protocols - All the protocols the address is composed of
99 */
100 protos(): Protocol[];
101 /**
102 * Returns the codes of the protocols in left-to-right order.
103 * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
104 *
105 * @example
106 * ```js
107 * Multiaddr('/ip4/127.0.0.1/tcp/4001').protoCodes()
108 * // [ 4, 6 ]
109 * ```
110 *
111 * @returns {number[]} protocol codes
112 */
113 protoCodes(): number[];
114 /**
115 * Returns the names of the protocols in left-to-right order.
116 * [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
117 *
118 * @example
119 * ```js
120 * new Multiaddr('/ip4/127.0.0.1/tcp/4001').protoNames()
121 * // [ 'ip4', 'tcp' ]
122 * ```
123 *
124 * @returns {string[]} protocol names
125 */
126 protoNames(): string[];
127 /**
128 * Returns a tuple of parts
129 *
130 * @example
131 * ```js
132 * new Multiaddr("/ip4/127.0.0.1/tcp/4001").tuples()
133 * // [ [ 4, <Buffer 7f 00 00 01> ], [ 6, <Buffer 0f a1> ] ]
134 * ```
135 */
136 tuples(): [number, (Uint8Array | undefined)?][];
137 /**
138 * Returns a tuple of string/number parts
139 * - tuples[][0] = code of protocol
140 * - tuples[][1] = contents of address
141 *
142 * @example
143 * ```js
144 * new Multiaddr("/ip4/127.0.0.1/tcp/4001").stringTuples()
145 * // [ [ 4, '127.0.0.1' ], [ 6, '4001' ] ]
146 * ```
147 */
148 stringTuples(): [number, (string | undefined)?][];
149 /**
150 * Encapsulates a Multiaddr in another Multiaddr
151 *
152 * @example
153 * ```js
154 * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080')
155 * // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080>
156 *
157 * const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
158 * // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
159 *
160 * const mh3 = mh1.encapsulate(mh2)
161 * // <Multiaddr 0408080808060438047f000001060fa1 - /ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001>
162 *
163 * mh3.toString()
164 * // '/ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001'
165 * ```
166 *
167 * @param {MultiaddrInput} addr - Multiaddr to add into this Multiaddr
168 */
169 encapsulate(addr: MultiaddrInput): Multiaddr;
170 /**
171 * Decapsulates a Multiaddr from another Multiaddr
172 *
173 * @example
174 * ```js
175 * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080')
176 * // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080>
177 *
178 * const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
179 * // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
180 *
181 * const mh3 = mh1.encapsulate(mh2)
182 * // <Multiaddr 0408080808060438047f000001060fa1 - /ip4/8.8.8.8/tcp/1080/ip4/127.0.0.1/tcp/4001>
183 *
184 * mh3.decapsulate(mh2).toString()
185 * // '/ip4/8.8.8.8/tcp/1080'
186 * ```
187 *
188 * @param {Multiaddr | string} addr - Multiaddr to remove from this Multiaddr
189 * @returns {Multiaddr}
190 */
191 decapsulate(addr: Multiaddr | string): Multiaddr;
192 /**
193 * A more reliable version of `decapsulate` if you are targeting a
194 * specific code, such as 421 (the `p2p` protocol code). The last index of the code
195 * will be removed from the `Multiaddr`, and a new instance will be returned.
196 * If the code is not present, the original `Multiaddr` is returned.
197 *
198 * @example
199 * ```js
200 * const addr = new Multiaddr('/ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
201 * // <Multiaddr 0400... - /ip4/0.0.0.0/tcp/8080/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC>
202 *
203 * addr.decapsulateCode(421).toString()
204 * // '/ip4/0.0.0.0/tcp/8080'
205 *
206 * new Multiaddr('/ip4/127.0.0.1/tcp/8080').decapsulateCode(421).toString()
207 * // '/ip4/127.0.0.1/tcp/8080'
208 * ```
209 *
210 * @param {number} code - The code of the protocol to decapsulate from this Multiaddr
211 * @returns {Multiaddr}
212 */
213 decapsulateCode(code: number): Multiaddr;
214 /**
215 * Extract the peerId if the multiaddr contains one
216 *
217 * @example
218 * ```js
219 * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
220 * // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string>
221 *
222 * // should return QmValidBase58string or null if the id is missing or invalid
223 * const peerId = mh1.getPeerId()
224 * ```
225 *
226 * @returns {string | null} peerId - The id of the peer or null if invalid or missing from the ma
227 */
228 getPeerId(): string | null;
229 /**
230 * Extract the path if the multiaddr contains one
231 *
232 * @example
233 * ```js
234 * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock')
235 * // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/unix/tmp/p2p.sock>
236 *
237 * // should return utf8 string or null if the id is missing or invalid
238 * const path = mh1.getPath()
239 * ```js
240 *
241 * @returns {string | null} path - The path of the multiaddr, or null if no path protocol is present
242 */
243 getPath(): string | null;
244 /**
245 * Checks if two Multiaddrs are the same
246 *
247 * @example
248 * ```js
249 * const mh1 = new Multiaddr('/ip4/8.8.8.8/tcp/1080')
250 * // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080>
251 *
252 * const mh2 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
253 * // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
254 *
255 * mh1.equals(mh1)
256 * // true
257 *
258 * mh1.equals(mh2)
259 * // false
260 * ```
261 *
262 * @param {Multiaddr} addr
263 * @returns {boolean}
264 */
265 equals(addr: Multiaddr): boolean;
266 /**
267 * Resolve multiaddr if containing resolvable hostname.
268 *
269 * @example
270 * ```js
271 * Multiaddr.resolvers.set('dnsaddr', resolverFunction)
272 * const mh1 = new Multiaddr('/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb')
273 * const resolvedMultiaddrs = await mh1.resolve()
274 * // [
275 * // <Multiaddr 04934b5353060fa1a503221220c10f9319dac35c270a6b74cd644cb3acfc1f6efc8c821f8eb282599fd1814f64 - /ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb>,
276 * // <Multiaddr 04934b53530601bbde03a503221220c10f9319dac35c270a6b74cd644cb3acfc1f6efc8c821f8eb282599fd1814f64 - /ip4/147.75.83.83/tcp/443/wss/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb>,
277 * // <Multiaddr 04934b535391020fa1cc03a503221220c10f9319dac35c270a6b74cd644cb3acfc1f6efc8c821f8eb282599fd1814f64 - /ip4/147.75.83.83/udp/4001/quic/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb>
278 * // ]
279 * ```
280 *
281 * @returns {Promise<Array<Multiaddr>>}
282 */
283 resolve(): Promise<Array<Multiaddr>>;
284 /**
285 * Gets a Multiaddrs node-friendly address object. Note that protocol information
286 * is left out: in Node (and most network systems) the protocol is unknowable
287 * given only the address.
288 *
289 * Has to be a ThinWaist Address, otherwise throws error
290 *
291 * @example
292 * ```js
293 * new Multiaddr('/ip4/127.0.0.1/tcp/4001').nodeAddress()
294 * // {family: 4, address: '127.0.0.1', port: 4001}
295 * ```
296 *
297 * @returns {{family: 4 | 6, address: string, port: number}}
298 * @throws {Error} Throws error if Multiaddr is not a Thin Waist address
299 */
300 nodeAddress(): {
301 family: 4 | 6;
302 address: string;
303 port: number;
304 };
305 /**
306 * Returns if a Multiaddr is a Thin Waist address or not.
307 *
308 * Thin Waist is if a Multiaddr adheres to the standard combination of:
309 *
310 * `{IPv4, IPv6}/{TCP, UDP}`
311 *
312 * @example
313 * ```js
314 * const mh1 = new Multiaddr('/ip4/127.0.0.1/tcp/4001')
315 * // <Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>
316 * const mh2 = new Multiaddr('/ip4/192.168.2.1/tcp/5001')
317 * // <Multiaddr 04c0a80201061389 - /ip4/192.168.2.1/tcp/5001>
318 * const mh3 = mh1.encapsulate(mh2)
319 * // <Multiaddr 047f000001060fa104c0a80201061389 - /ip4/127.0.0.1/tcp/4001/ip4/192.168.2.1/tcp/5001>
320 * const mh4 = new Multiaddr('/ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a')
321 * // <Multiaddr 047f0000010607d0de039302a503221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a64835843 - /ip4/127.0.0.1/tcp/2000/wss/p2p-webrtc-star/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooo2a>
322 * mh1.isThinWaistAddress()
323 * // true
324 * mh2.isThinWaistAddress()
325 * // true
326 * mh3.isThinWaistAddress()
327 * // false
328 * mh4.isThinWaistAddress()
329 * // false
330 * ```
331 *
332 * @param {Multiaddr} [addr] - Defaults to using `this` instance
333 */
334 isThinWaistAddress(addr?: Multiaddr | undefined): boolean;
335 /**
336 * Returns Multiaddr as a human-readable string.
337 * Fallback for pre Node.js v10.0.0.
338 * https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect
339 *
340 * @example
341 * ```js
342 * new Multiaddr('/ip4/127.0.0.1/tcp/4001').inspect()
343 * // '<Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>'
344 * ```
345 *
346 * @returns {string}
347 */
348 inspect(): string;
349 /**
350 * Returns Multiaddr as a human-readable string.
351 * For post Node.js v10.0.0.
352 * https://nodejs.org/api/deprecations.html#deprecations_dep0079_custom_inspection_function_on_objects_via_inspect
353 *
354 * @example
355 * ```js
356 * console.log(new Multiaddr('/ip4/127.0.0.1/tcp/4001'))
357 * // '<Multiaddr 047f000001060fa1 - /ip4/127.0.0.1/tcp/4001>'
358 * ```
359 *
360 * @returns {string}
361 */
362 [inspect](): string;
363}
364export namespace Multiaddr {
365 export { protocols };
366 export { resolvers };
367}
368/**
369 * Static factory
370 *
371 * @param {MultiaddrInput} addr
372 */
373export function multiaddr(addr: MultiaddrInput): Multiaddr;
374import protocols = require("./protocols-table");
375/**
376 * @typedef {(addr: Multiaddr) => Promise<string[]>} Resolver
377 * @typedef {string | Multiaddr | Uint8Array | null} MultiaddrInput
378 * @typedef {import('./types').MultiaddrObject} MultiaddrObject
379 * @typedef {import('./types').Protocol} Protocol
380 */
381/** @type {Map<string, Resolver>} */
382export const resolvers: Map<string, Resolver>;
383declare const inspect: unique symbol;
384export { protocols };
385//# sourceMappingURL=index.d.ts.map
\No newline at end of file