Persistant PeerId for Libp2p js

Hey Ive done some looking around and cant find any helpful info as to how to implement a static peerId for a BootNode.

From what i have found so fare i have generated an id priv and pub key using (peer-id).

`const PeerId =require('peer-id')


const createId = async () => {
    const id = await PeerId.create({ bits: 1024, keyType: 'RSA' })
    console.log((id.toJSON()))
}

createId()`

Then i copied the out put in the terminal in to my BootNode peerId section.

const   createLibp2p  = require('libp2p')
const   TCP  = require('libp2p-tcp')
const   Mplex  = require('libp2p-mplex')
const   { NOISE } = require('libp2p-noise')
const   Gossipsub  = require('libp2p-gossipsub')
const   MulticastDNS = require('libp2p-mdns')
const   { fromString } = require('uint8arrays/from-string')
const   { toString } = require('uint8arrays/to-string')


const createNode = async () => {
    const node = await createLibp2p.create({
        peerId: {
            id: 'QmeRhaUCD71y3fi43EDnoeX2MP7gGUNBFmGqAK5mcKhbce',
            privKey: 'CAAS3wQwggJbAgEAAoGBAMKf7QL0zbTd4bHfOHz4+H4vxV...',
            pubKey: 'CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBA...'              
        },
        addresses: {
            listen: ['/ip4/0.0.0.0/tcp/0']
        },
        modules: {
            transport: [TCP],
            streamMuxer: [Mplex],
            connEncryption: [NOISE],
            pubsub: Gossipsub,
            peerDiscovery: [MulticastDNS]
        },
        config:{
            peerDiscovery: {
            autoDial: true,
            [MulticastDNS.tag]: {
                broadcast: true,
                interval: 1000,
                enabled: true
            }
            },
            pubsub:{
            emitSelf: true,
            enabled: true
            },
        }
    })
}

But i get the error.

/home/tank/MyCode/Posibly-Usfule-Code/js-libp2p/node_modules/libp2p/src/connection-manager/index.js:76
    this._peerId = libp2p.peerId.toB58String()
                                 ^

TypeError: libp2p.peerId.toB58String is not a function

What am i doing wrong !! ha thanks in advance.

I think you have to provide a peerid instance which you can create from a JSON object or a private key

Hey thanks for your reply

Im kinda new to libp2p if you how to provide a peerid the way you specified an example would be help full

You can create the peerId Instance from json like this:

var myPeerId = await PeerId.createFromJSON({
    id: 'QmeRhaUCD71y3fi43EDnoeX2MP7gGUNBFmGqAK5mcKhbce',
    privKey: 'CAAS3wQwggJbAgEAAoGBAMKf7QL0zbTd4bHfOHz4+H4vxV...',
    pubKey: 'CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBA...' 
});

And then you can use it in your createLibp2p with

peerId: myPeerId,

import { createLibp2p } from 'libp2p';
import { TCP } from '@libp2p/tcp';
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { Noise } from 'libp2p-noise';

const id = await createEd25519PeerId();

const node = await createLibp2p({
    peerId: id,
    addresses: {
        // To signal the addresses we want to be available, we use
        // the multiaddr format, a self describable address
        listen: [
            '/ip4/0.0.0.0/tcp/0'
        ]
    },
    transports: [
        new TCP()
    ],
    connectionEncryption: [
        new Noise()
    ]
})

node.start()
console.log(node.peerId === id)

Hello @LucaPanofsky ,

Is there function export peerId to json. So I want to create peerId from config file.

Thanks you!