Unexpected 68-byte Private Key Length Leads to privateKey not set Error & DHT Issues #3149
Replies: 2 comments
-
I can't run your example code since it's missing several imports, but I notice you're using older APIs than the current release, and that you have a custom storage implementation that's loading/storing node info. I can't tell if it's doing it correctly as the implementation is missing. Anyway here's how to correctly serialize and deserialize private keys, hopefully you can adapt it for your app: import { generateKeyPair, privateKeyToProtobuf, privateKeyFromProtobuf } from '@libp2p/crypto/keys'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
// create a private key
const key = await generateKeyPair('Ed25519')
// derive the peer id from the private key
const peerId = peerIdFromPrivateKey(key)
console.info('derived peer id', peerId.toString())
// serialize the private key
const serialized = privateKeyToProtobuf(key)
console.info('serialized key length', serialized.byteLength)
// length is 68 (e.g. key type + private key + public key
// deserialize private key
const deserializedKey = privateKeyFromProtobuf(serialized)
// derive peer id from deserialized private key
const deserializedPeerId = peerIdFromPrivateKey(deserializedKey)
// should match initial peer id
console.info('deserialized peer id', deserializedPeerId.toString()) |
Beta Was this translation helpful? Give feedback.
-
To store the private key encrypted at rest, you can use a datastore implementation and the helper from For example if you run this code multiple times, you should see a import { loadOrCreateSelfKey } from '@libp2p/config'
import { FsDatastore } from 'datastore-fs'
import { createLibp2p } from 'libp2p'
const datastore = new FsDatastore('./datastore')
await datastore.open()
const privateKey = await loadOrCreateSelfKey(datastore, {
keyType: 'Ed25519',
pass: 'very secure, very secret password'
})
const libp2p = await createLibp2p({
privateKey,
// ... other options here
})
console.info(libp2p.peerId.toString()) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm encountering an issue where my Ed25519 private keys, when loaded from storage, are appearing as 68 bytes long instead of the expected 64 bytes (or 32 bytes for a seed). This subsequently leads to a privateKey not set error when the Identify service initializes, preventing my libp2p node and its Kademlia DHT from functioning correctly.
My primary goal is to establish a stable peer-to-peer network with a fully operational Kademlia DHT. This network is intended to support a blockchain application, so robust peer discovery and connectivity are crucial.
Context:
I'm using js-libp2p with the standard modules for TCP transport, Noise encryption, Mplex stream multiplexing, Bootstrap peer discovery, and kad-dht.
I have logic to create an Ed25519 PeerId or load it from persistent storage (a JSON file).
When a new PeerId is generated using @libp2p/peer-id-factory (createEd25519PeerId), its privateKey property (a Uint8Array) is correctly 64 bytes long.
This 64-byte key is then base64 encoded and saved.
Upon subsequent startups, the base64 string is loaded, decoded back into a Buffer, and this buffer is where I'm observing the 68-byte length.
I have no clear ideas on how to correctly populate the DHT and how to make the nodes connect automatically to each other, this attachment is the code of the NetworkManager of my node (normal), which connects to a bootstrap node and subsequently tries to connect to other nodes to start the network, the code of my bootstraop node is almost similar, or rather the basic logic for the peerId are those,
I thank in advance anyone who can enlighten me a little more as I have not found many answers for those who intend to create their network from scratch via libp2p with its protocols and services.
Beta Was this translation helpful? Give feedback.
All reactions