Skip to content

Commit f5741cd

Browse files
committed
Refactor NodeKeyFactory to improve key derivation logic
- Updated the `NodeKeyFactory` to use a constant for the uncompressed public key prefix length, enhancing code readability and maintainability. - Adjusted the logic for deriving the `enode` to utilize the new constant, ensuring consistency across the implementation. - Modified tests in `node-key-factory.test.ts` to reflect the changes in the key derivation process and validate the correct behavior of the `enode` field. This refactor aims to streamline the key generation process and improve the clarity of the codebase.
1 parent 995e11c commit f5741cd

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import { NodeKeyFactory } from "./node-key-factory.ts";
44

55
const factory = new NodeKeyFactory();
66

7+
/**
8+
* Offset to skip the 0x04 uncompressed public key prefix when deriving the node ID.
9+
*/
10+
const UNCOMPRESSED_PUBLIC_KEY_PREFIX_LENGTH = 4;
11+
12+
/**
13+
* Expected length of a hex-encoded 64-byte node ID (128 hex characters).
14+
*/
15+
const NODE_ID_HEX_LENGTH = 128;
16+
717
describe("NodeKeyFactory", () => {
818
test("generates unique key material", () => {
919
const first = factory.generate();
@@ -14,9 +24,11 @@ describe("NodeKeyFactory", () => {
1424
expect(first.address.startsWith("0x")).toBe(true);
1525

1626
// enode should be the node ID (public key with 0x04 prefix stripped)
17-
expect(first.enode).toBe(first.publicKey.slice(4));
27+
expect(first.enode).toBe(
28+
first.publicKey.slice(UNCOMPRESSED_PUBLIC_KEY_PREFIX_LENGTH)
29+
);
1830
expect(first.enode).not.toContain("0x");
19-
expect(first.enode.length).toBe(128); // 64 bytes hex-encoded
31+
expect(first.enode.length).toBe(NODE_ID_HEX_LENGTH);
2032

2133
expect(first.privateKey).not.toBe(second.privateKey);
2234
expect(first.publicKey).not.toBe(second.publicKey);

src/keys/node-key-factory.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ export type GeneratedNodeKey = {
88
publicKey: Hex;
99
};
1010

11+
/**
12+
* Offset to skip the 0x04 uncompressed public key prefix when deriving the node ID.
13+
*/
14+
const UNCOMPRESSED_PUBLIC_KEY_PREFIX_LENGTH = 4;
15+
16+
/**
17+
* Offset to skip the 0x hex prefix.
18+
*/
19+
const HEX_PREFIX_LENGTH = 2;
20+
1121
/**
1222
* Provides lightweight wrappers around viem's account generation helpers.
1323
*/
@@ -29,8 +39,8 @@ export class NodeKeyFactory {
2939
const address = account.address;
3040
// Derive node ID by stripping the 0x04 prefix from the uncompressed public key
3141
const enode = publicKey.startsWith("0x04")
32-
? publicKey.slice(4)
33-
: publicKey.slice(2);
42+
? publicKey.slice(UNCOMPRESSED_PUBLIC_KEY_PREFIX_LENGTH)
43+
: publicKey.slice(HEX_PREFIX_LENGTH);
3444

3545
return {
3646
address,

0 commit comments

Comments
 (0)