libp2p / py-libp2p

The Python implementation of the libp2p networking stack 🐍 [under development]
https://libp2p.io
Other
470 stars 101 forks source link

Provide utilities to persist network identities between runs of a node #312

Open ralexstokes opened 4 years ago

ralexstokes commented 4 years ago

We are currently missing the ability to persist node identities b/t runs of a node.

The user should provide a "key pair provider" when creating a host; I'd expect the type to be Callable[[], libp2p.crypto.keys.KeyPair] for maximum flexibility.

There are different methods of providing key pairs based on the "lifetime" of the key pair:

  1. New key pair for some cryptosystem; for example, the user provides a function with the above signature that returns the result of libp2p.crypto.rsa.create_new_key_pair with the key parameters passed in via closure, curry, etc.

  2. Loading a persisted key pair where the user provides a path to a serialized key pair which the library then loads into memory; for example, a callable that closes over the path which, when called does the necessary IO to load and return a KeyPair object.

Having persistence opens some questions:

  1. Serialization/keystore format

  2. How to manage multiple key pairs and select b/t them... I'd think the nice thing is to "auto-save" every key pair the library makes at some default keystore path -- the user then needs a way to specify one key from this keystore.

mhchia commented 4 years ago

I'm thinking if it makes sense to provide persisting versions of KeyStore, PeerStore, ...etc. It can be passed into initialize_default_swarm as the parameter peerstore_opt. For reference, go-ipfs has FSKeystore whose backend is a file.

ralexstokes commented 4 years ago

oh yeah -- i think we definitely want these as well! i'd expect we are workings towards parity w/ the Go implementation feature-wise -- more a question of what resources get dedicated to this repo and on what timeframe.

when i opened this issue, i definitely had the specific use case in mind for trinity/eth2

moshemalawach commented 4 years ago

Implemented something similar in aleph-im/pyaleph, coming from configuration or file (if the key is there, load it, if it's none, and a listening host, print out the key to prompt user to save it. If not listening, a new host id isn't an issue): https://github.com/aleph-im/pyaleph/blob/22b06c02b82f0759b81142156ad3b978aee485c4/src/aleph/services/p2p/manager.py#L16