Reputation: 454
Can I keep the same Node ID (PeerId) in Helia - libp2p across restarts? I am using Helia implementation of IPFS. Helia documentation really doesn't provide enough info on this. Helia forums also dones't have any info on this
Persist NodeId across every Restart
Upvotes: 1
Views: 103
Reputation: 1
peerId
generating by private key.
Look at the source code https://github.com/libp2p/js-libp2p/blob/635283461e87399c5cde6e729c4b993188e10168/packages/libp2p/src/index.ts#L41-L48
Here we see, that we should generate private key.
Try this
import {generateKeyPair, privateKeyFromRaw} from "@libp2p/crypto/keys";
import {Key} from "interface-datastore";
const KEY_NAME = new Key('privateKey');
export async function getOrCreatePrivateKey(datastore: FsDatastore) {
try {
const keyData = await datastore.get(KEY_NAME);
// Key deserialization
const privateKey = privateKeyFromRaw(keyData);
console.log('Private key loaded from datastore.');
return privateKey;
} catch (error) {
console.log('Private key not found, generating a new one.');
// Generating a new key if it is not in the datastore
const privateKey = await generateKeyPair('Ed25519');
// Serialization and saving of a new key
await datastore.put(KEY_NAME, privateKey.raw);
console.log('Private key saved to datastore.');
return privateKey;
}
}
const libp2pNode = await createLibp2p({
privateKey: await getOrCreatePrivateKey(datastoreCommonNode),
...
In this function we check if the private key exists, if not we generate a new one and store it locally. Thus, we have a persistent peerId.
Upvotes: 0