Generating Peer Id

Hi @hskang9, thanks for making a thread for this - it’s a good question that’s not covered well enough in the docs yet.

In go-libp2p, you can generate a PeerId using the IdFromPublicKey helper method in go-libp2p-peer. To get the public key, you can use GenerateKeyPair to create a new key pair.

Once you have the PublicKey and PrivateKey generated, you can call IdFromPublicKey to generate the peer id. To save the key for future use, call MarshalPublicKey and MarshalPrivateKey to serialize the keys to byte slices, which you can then save to disk and deserialize using the UnmarshalPublicKey and UnmarshalPrivateKey.

Let me know if that helps and please keep the questions coming :slight_smile:

2 Likes