Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
import { createLibp2p } from 'libp2p'import { preSharedKey, generateKey } from '@libp2p/pnet'// Create a Uint8Array and write the swarm key to itconst swarmKey = new Uint8Array(95)generateKey(swarmKey)const node = await createLibp2p({ // ...other options connectionProtector: preSharedKey({ psk: swarmKey })}) Copy
import { createLibp2p } from 'libp2p'import { preSharedKey, generateKey } from '@libp2p/pnet'// Create a Uint8Array and write the swarm key to itconst swarmKey = new Uint8Array(95)generateKey(swarmKey)const node = await createLibp2p({ // ...other options connectionProtector: preSharedKey({ psk: swarmKey })})
Private Shared Keys are expected to be in the following format:
/key/swarm/psk/1.0.0//base16/dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d Copy
/key/swarm/psk/1.0.0//base16/dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
A utility method has been created to generate a key for your private network. You can use one of the methods below to generate your key.
If you have a module locally that depends on libp2p, you can run the following from that project, assuming the node_modules are installed.
node -e "import('@libp2p/pnet').then(({ generateKey }) => generateKey(process.stdout))" > swarm.key Copy
node -e "import('@libp2p/pnet').then(({ generateKey }) => generateKey(process.stdout))" > swarm.key
import fs from 'fs'import { generateKey } from '@libp2p/pnet'const swarmKey = new Uint8Array(95)generateKey(swarmKey)fs.writeFileSync('swarm.key', swarmKey) Copy
import fs from 'fs'import { generateKey } from '@libp2p/pnet'const swarmKey = new Uint8Array(95)generateKey(swarmKey)fs.writeFileSync('swarm.key', swarmKey)
Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
Example
Private Shared Keys
Private Shared Keys are expected to be in the following format:
PSK Generation
A utility method has been created to generate a key for your private network. You can use one of the methods below to generate your key.
From a module using libp2p
If you have a module locally that depends on libp2p, you can run the following from that project, assuming the node_modules are installed.
Programmatically