Skip to content

Commit fee919d

Browse files
authored
fix: enode field should contain node ID, not private key (#72)
## Summary The `enode` field in `GeneratedNodeKey` was incorrectly set to the private key instead of the node ID (public key with 0x04 prefix stripped). ## Issues Fixed - Individual validator enode ConfigMaps contained private keys instead of node IDs - Duplicate private key data (stored in both `privateKey` and `enode` fields) - Misleading ConfigMap names (`besu-node-validator-X-enode` contained private key) ## Changes 1. **Fixed `src/keys/node-key-factory.ts`**: Strip the 0x04 prefix from the public key to derive the node ID 2. **Fixed `src/keys/node-key-factory.test.ts`**: Updated test to validate correct behavior (enode should be 128-char hex node ID) ## Testing - ✅ All 90 tests pass - ✅ Verified enode field now contains correct node ID format ## Note This bug only affected the individual validator `enode` field. The `static-nodes.json` generation was not affected because it used the correct `deriveNodeId()` function. ## Links - Bug in code: https://github.com/settlemint/network-bootstrapper/blob/184bc9469aca3d08db69f8a0a9f4cf5ce6643deb/src/keys/node-key-factory.ts#L30 - Test that validated bug: https://github.com/settlemint/network-bootstrapper/blob/184bc9469aca3d08db69f8a0a9f4cf5ce6643deb/src/keys/node-key-factory.test.ts#L15 ## Summary by Sourcery Fix incorrect enode field derivation by using the public key (node ID) instead of the private key, and update tests to validate the new behavior. Bug Fixes: - Derive enode node ID by stripping the 0x04 prefix from the uncompressed public key instead of reusing the private key Tests: - Update NodeKeyFactory tests to assert enode is a 128-character hex node ID without 0x prefix and unique per key
1 parent 347ca0a commit fee919d

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

bun.lock

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/keys/node-key-factory.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ describe("NodeKeyFactory", () => {
1212
expect(first.privateKey.startsWith("0x")).toBe(true);
1313
expect(first.publicKey.startsWith("0x")).toBe(true);
1414
expect(first.address.startsWith("0x")).toBe(true);
15-
expect(first.enode).toBe(first.privateKey);
15+
16+
// enode should be the node ID (public key with 0x04 prefix stripped)
17+
expect(first.enode).toBe(first.publicKey.slice(4));
18+
expect(first.enode).not.toContain("0x");
19+
expect(first.enode.length).toBe(128); // 64 bytes hex-encoded
1620

1721
expect(first.privateKey).not.toBe(second.privateKey);
1822
expect(first.publicKey).not.toBe(second.publicKey);
1923
expect(first.address).not.toBe(second.address);
24+
expect(first.enode).not.toBe(second.enode);
2025
});
2126

2227
test("throws on empty label", () => {

src/keys/node-key-factory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ export class NodeKeyFactory {
2020

2121
/**
2222
* Generates a fresh private key alongside the derived public key, address,
23-
* and exposes the private key again under `enode` for downstream tooling.
23+
* and derives the node ID (enode) by stripping the 0x04 prefix from the uncompressed public key.
2424
*/
2525
generate(): GeneratedNodeKey {
2626
const privateKey = generatePrivateKey();
2727
const account = privateKeyToAccount(privateKey);
2828
const publicKey = account.publicKey;
2929
const address = account.address;
30-
const enode = privateKey;
30+
// Derive node ID by stripping the 0x04 prefix from the uncompressed public key
31+
const enode = publicKey.startsWith("0x04")
32+
? publicKey.slice(4)
33+
: publicKey.slice(2);
3134

3235
return {
3336
address,

0 commit comments

Comments
 (0)