libp2p
Version:
JavaScript implementation of libp2p, a modular peer to peer network stack
57 lines • 1.53 kB
JavaScript
/**
* @packageDocumentation
*
* Use the `createLibp2p` function to create a libp2p node.
*
* @example
*
* ```typescript
* import { createLibp2p } from 'libp2p'
*
* const node = await createLibp2p({
* // ...other options
* })
* ```
*/
import { generateKeyPair } from '@libp2p/crypto/keys';
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
import { validateConfig } from './config.js';
import { Libp2p as Libp2pClass } from './libp2p.js';
/**
* Returns a new instance of the Libp2p interface, generating a new PeerId
* if one is not passed as part of the options.
*
* The node will be started unless `start: false` is passed as an option.
*
* @example
*
* ```TypeScript
* import { createLibp2p } from 'libp2p'
* import { tcp } from '@libp2p/tcp'
* import { mplex } from '@libp2p/mplex'
* import { noise } from '@chainsafe/libp2p-noise'
* import { yamux } from '@chainsafe/libp2p-yamux'
*
* // specify options
* const options = {
* transports: [tcp()],
* streamMuxers: [yamux(), mplex()],
* connectionEncrypters: [noise()]
* }
*
* // create libp2p
* const libp2p = await createLibp2p(options)
* ```
*/
export async function createLibp2p(options = {}) {
options.privateKey ??= await generateKeyPair('Ed25519');
const node = new Libp2pClass({
...await validateConfig(options),
peerId: peerIdFromPrivateKey(options.privateKey)
});
if (options.start !== false) {
await node.start();
}
return node;
}
//# sourceMappingURL=index.js.map