Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit ba6ce9f

Browse files
committed
feat!: allow user defined services
Adds types to libp2p to allow user defined services. Removes all non-essential services to allow them to be defined alongside any custom user versions. This increases the modularity of libp2p as default protocols like identify, fetch and ping will not be enabled unless specifically allowed by the user, which also decreases the attack surface area of libp2p applications. `getPrivate` key has also been removed as it can be replicated by via `node.contentRouting.get` with the appropriate key. BREAKING CHANGE: ping, fetch and identify have been removed, as has getPrivateKey
1 parent 18c5622 commit ba6ce9f

File tree

1 file changed

+23
-125
lines changed

1 file changed

+23
-125
lines changed

packages/interface-libp2p/src/index.ts

+23-125
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ import type { AbortOptions } from '@libp2p/interfaces'
1818
import type { EventEmitter } from '@libp2p/interfaces/events'
1919
import type { Startable } from '@libp2p/interfaces/startable'
2020
import type { Multiaddr } from '@multiformats/multiaddr'
21-
import type { DualDHT } from '@libp2p/interface-dht'
2221
import type { PeerStore } from '@libp2p/interface-peer-store'
2322
import type { PeerId } from '@libp2p/interface-peer-id'
2423
import type { Connection, Stream } from '@libp2p/interface-connection'
2524
import type { PeerRouting } from '@libp2p/interface-peer-routing'
2625
import type { ContentRouting } from '@libp2p/interface-content-routing'
27-
import type { PubSub } from '@libp2p/interface-pubsub'
2826
import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
2927
import type { Metrics } from '@libp2p/interface-metrics'
3028
import type { PeerInfo } from '@libp2p/interface-peer-info'
@@ -78,16 +76,31 @@ export interface Libp2pEvents {
7876
}
7977

8078
/**
81-
* Fetch service lookup function
79+
* A map of user defined services available on the libp2p node via the
80+
* `services` key
81+
*
82+
* @example
83+
*
84+
* ```js
85+
* const node = await createLibp2p({
86+
* // ...other options
87+
* services: {
88+
* myService: myService({
89+
* // ...service options
90+
* })
91+
* }
92+
* })
93+
*
94+
* // invoke methods on the service
95+
* node.services.myService.anOperation()
96+
* ```
8297
*/
83-
export interface LookupFunction {
84-
(key: string): Promise<Uint8Array | null>
85-
}
98+
export type ServiceMap = Record<string, unknown>
8699

87100
/**
88101
* Libp2p nodes implement this interface.
89102
*/
90-
export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
103+
export interface Libp2p<T extends ServiceMap = {}> extends Startable, EventEmitter<Libp2pEvents> {
91104
/**
92105
* The PeerId is a unique identifier for a node on the network.
93106
*
@@ -177,7 +190,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
177190
* @example
178191
*
179192
* ```js
180-
* const metric = libp2p.registerMetric({
193+
* const metric = libp2p.metrics.registerMetric({
181194
* 'my-metric'
182195
* })
183196
*
@@ -187,98 +200,6 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
187200
*/
188201
metrics?: Metrics
189202

190-
/**
191-
* The pubsub component implements a distributed [Publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)
192-
* network made up of libp2p nodes listening on various topics.
193-
*
194-
* @example
195-
*
196-
* ```js
197-
* libp2p.pubsub.addEventListener('message', (event) => {
198-
* // ...
199-
* })
200-
* libp2p.pubsub.subscribe('my-topic')
201-
* ```
202-
*/
203-
pubsub: PubSub
204-
205-
/**
206-
* The [DHT](https://en.wikipedia.org/wiki/Distributed_hash_table) is used by
207-
* libp2p to store and find values such as provider records and also to discover
208-
* information about peers.
209-
*
210-
* @example
211-
*
212-
* ```js
213-
* for await (const event of libp2p.dht.findPeer(peerId)) {
214-
* // ...
215-
* }
216-
* ```
217-
*/
218-
dht: DualDHT
219-
220-
/**
221-
* The fetch service allows registering and unregistering functions that supply
222-
* values for fetch queries - see the [fetch spec](https://github.com/libp2p/specs/tree/master/fetch).
223-
*/
224-
fetchService: {
225-
/**
226-
* Registers a new lookup callback that can map keys to values, for a given set of keys that
227-
* share the same prefix
228-
*
229-
* @example
230-
*
231-
* ```js
232-
* libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
233-
* ```
234-
*/
235-
registerLookupFunction: (prefix: string, lookup: LookupFunction) => void
236-
237-
/**
238-
* Registers a new lookup callback that can map keys to values, for a given set of keys that
239-
* share the same prefix.
240-
*
241-
* @example
242-
*
243-
* ```js
244-
* libp2p.fetchService.unregisterLookupFunction('/prefix')
245-
* ```
246-
*/
247-
unregisterLookupFunction: (prefix: string, lookup?: LookupFunction) => void
248-
}
249-
250-
/**
251-
* The identify service supplies information about this node on request by network peers - see
252-
* this [identify spec](https://github.com/libp2p/specs/blob/master/identify/README.md)
253-
*/
254-
identifyService: {
255-
host: {
256-
/**
257-
* Specifies the supported protocol version
258-
*
259-
* @example
260-
*
261-
* ```js
262-
* libp2p.identifyService.host.protocolVersion
263-
* // ipfs/0.1.0
264-
* ```
265-
*/
266-
protocolVersion: string
267-
268-
/**
269-
* Specifies the supported protocol version
270-
*
271-
* @example
272-
*
273-
* ```js
274-
* libp2p.identifyService.host.agentVersion
275-
* // helia/1.0.0
276-
* ```
277-
*/
278-
agentVersion: string
279-
}
280-
}
281-
282203
/**
283204
* Get a deduplicated list of peer advertising multiaddrs by concatenating
284205
* the listen addresses used by transports with any configured
@@ -449,30 +370,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
449370
unregister: (id: string) => void
450371

451372
/**
452-
* Pings the given peer in order to obtain the operation latency
453-
*
454-
* @example
455-
*
456-
* ```js
457-
* const latency = await libp2p.ping(otherPeerId)
458-
* ```
459-
*/
460-
ping: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<number>
461-
462-
/**
463-
* Sends a request to fetch the value associated with the given key from the given peer.
464-
*
465-
* @example
466-
*
467-
* ```js
468-
* const value = await libp2p.fetch(otherPeerId, '/some/key')
469-
* ```
470-
*/
471-
fetch: (peer: PeerId | Multiaddr, key: string, options?: AbortOptions) => Promise<Uint8Array | null>
472-
473-
/**
474-
* Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
475-
* this may mean searching the DHT if the key is not present in the KeyStore.
373+
* A set of user defined services
476374
*/
477-
getPublicKey: (peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>
375+
services: T
478376
}

0 commit comments

Comments
 (0)