libp2p / go-libp2p-examples

Example libp2p applications
MIT License
339 stars 145 forks source link

How do I keep nodeID unchanged every time I start it #186

Closed itachiliu closed 3 years ago

itachiliu commented 3 years ago

Chat - with - rendezvous example, when I start a new node, generates a new node ID, such as QmR4j8pDzvy9JMjmXeYhcFXypjZJBKGWGZHBaittyy41is, but shut down the node (CTR + c), and then restart, node ID of the change, I want to make the node ID remains the same, have what way?

aschmahmann commented 3 years ago

You can create a libp2p host with an existing private key using the Identity(sk crypto.PrivKey) function. If you choose to save and reload the private keys from disk that will work.

itachiliu commented 3 years ago

You can create a libp2p host with an existing private key using the Identity(sk crypto.PrivKey) function. If you choose to save and reload the private keys from disk that will work.

Thank you for your reply,I am a newbie. how do I generate a fixed PrivKey and preserve it?

aschmahmann commented 3 years ago

@itachiliu you can use the libraries in go-libp2p-core/crypto to generate keys (e.g. crypto.GenerateKeyPair(crypto.Ed25519, 0), or crypto.GenerateKeyPair(crypto.RSA, 2048)) , marshal the private key (i.e. crypto.MarshalPrivateKey(priv)) and then store the bytes somewhere.

You can then unmarshal your private key using crypto.UnmarshalPrivateKey()

itachiliu commented 3 years ago

@itachiliu you can use the libraries in go-libp2p-core/crypto to generate keys (e.g. crypto.GenerateKeyPair(crypto.Ed25519, 0), or crypto.GenerateKeyPair(crypto.RSA, 2048)) , marshal the private key (i.e. crypto.MarshalPrivateKey(priv)) and then store the bytes somewhere.

You can then unmarshal your private key using crypto.UnmarshalPrivateKey()

Thx again,it works.😄