-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide default libp2p instance (#127)
The original intention was to allow users to configure a libp2p node to their requirements but it's a non-trivial undertaking unless the user is deeply familiar with the libp2p stack. Instead, let's provide node and browser libp2p instances with sensible defaults that give the user the best chance of success on first try. Fixes #121
- Loading branch information
1 parent
a4477ca
commit 45c9d89
Showing
15 changed files
with
277 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { gossipsub } from '@chainsafe/libp2p-gossipsub' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { bootstrap } from '@libp2p/bootstrap' | ||
import { ipniContentRouting } from '@libp2p/ipni-content-routing' | ||
import { kadDHT, type DualKadDHT } from '@libp2p/kad-dht' | ||
import { mplex } from '@libp2p/mplex' | ||
import { webRTC, webRTCDirect } from '@libp2p/webrtc' | ||
import { webSockets } from '@libp2p/websockets' | ||
import { webTransport } from '@libp2p/webtransport' | ||
import { createLibp2p as create } from 'libp2p' | ||
import { autoNATService } from 'libp2p/autonat' | ||
import { circuitRelayTransport, circuitRelayServer } from 'libp2p/circuit-relay' | ||
import { identifyService } from 'libp2p/identify' | ||
import type { CreateLibp2pOptions } from './libp2p.js' | ||
import type { Libp2p } from '@libp2p/interface-libp2p' | ||
import type { PubSub } from '@libp2p/interface-pubsub' | ||
|
||
export async function createLibp2p (opts: CreateLibp2pOptions): Promise<Libp2p<{ dht: DualKadDHT, pubsub: PubSub }>> { | ||
return create({ | ||
...opts, | ||
addresses: { | ||
listen: [ | ||
'/webrtc' | ||
] | ||
}, | ||
transports: [ | ||
webRTC(), | ||
webRTCDirect(), | ||
webTransport(), | ||
webSockets(), | ||
circuitRelayTransport({ | ||
discoverRelays: 1 | ||
}) | ||
], | ||
connectionEncryption: [ | ||
noise() | ||
], | ||
streamMuxers: [ | ||
yamux(), | ||
mplex() | ||
], | ||
peerDiscovery: [ | ||
bootstrap({ | ||
list: [ | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt' | ||
] | ||
}) | ||
], | ||
contentRouters: [ | ||
ipniContentRouting('https://cid.contact') | ||
], | ||
services: { | ||
identify: identifyService(), | ||
autoNAT: autoNATService(), | ||
pubsub: gossipsub(), | ||
dht: kadDHT({ | ||
clientMode: true | ||
}), | ||
relay: circuitRelayServer({ | ||
advertise: true | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { gossipsub } from '@chainsafe/libp2p-gossipsub' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { bootstrap } from '@libp2p/bootstrap' | ||
import { ipniContentRouting } from '@libp2p/ipni-content-routing' | ||
import { type DualKadDHT, kadDHT } from '@libp2p/kad-dht' | ||
import { mdns } from '@libp2p/mdns' | ||
import { mplex } from '@libp2p/mplex' | ||
import { tcp } from '@libp2p/tcp' | ||
import { webSockets } from '@libp2p/websockets' | ||
import { createLibp2p as create } from 'libp2p' | ||
import { autoNATService } from 'libp2p/autonat' | ||
import { circuitRelayTransport, circuitRelayServer, type CircuitRelayService } from 'libp2p/circuit-relay' | ||
import { identifyService } from 'libp2p/identify' | ||
import { uPnPNATService } from 'libp2p/upnp-nat' | ||
import type { Libp2p } from '@libp2p/interface-libp2p' | ||
import type { PubSub } from '@libp2p/interface-pubsub' | ||
import type { Datastore } from 'interface-datastore' | ||
|
||
export interface CreateLibp2pOptions { | ||
datastore: Datastore | ||
start?: boolean | ||
} | ||
|
||
export async function createLibp2p (opts: CreateLibp2pOptions): Promise<Libp2p<{ dht: DualKadDHT, pubsub: PubSub, relay: CircuitRelayService }>> { | ||
return create({ | ||
...opts, | ||
addresses: { | ||
listen: [ | ||
'/ip4/0.0.0.0/tcp/0' | ||
] | ||
}, | ||
transports: [ | ||
tcp(), | ||
webSockets(), | ||
circuitRelayTransport({ | ||
discoverRelays: 1 | ||
}) | ||
], | ||
connectionEncryption: [ | ||
noise() | ||
], | ||
streamMuxers: [ | ||
yamux(), | ||
mplex() | ||
], | ||
peerDiscovery: [ | ||
mdns(), | ||
bootstrap({ | ||
list: [ | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', | ||
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt' | ||
] | ||
}) | ||
], | ||
contentRouters: [ | ||
ipniContentRouting('https://cid.contact') | ||
], | ||
services: { | ||
identify: identifyService(), | ||
autoNAT: autoNATService(), | ||
upnp: uPnPNATService(), | ||
pubsub: gossipsub(), | ||
dht: kadDHT(), | ||
relay: circuitRelayServer({ | ||
advertise: true | ||
}) | ||
} | ||
}) | ||
} |
Oops, something went wrong.