Skip to content

Commit b70b080

Browse files
committed
1 parent a308c1e commit b70b080

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

src/onionRouters/simpleOnionRouter.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
import bodyParser from "body-parser";
22
import express, { Request, Response } from "express";
3-
import { BASE_ONION_ROUTER_PORT } from "../config";
3+
import { BASE_ONION_ROUTER_PORT, REGISTRY_PORT } from "../config";
4+
import { generateRsaKeyPair, exportPubKey, exportPrvKey } from "../crypto";
5+
import { webcrypto } from "node:crypto";
6+
7+
type CK = webcrypto.CryptoKey;
48

59
export async function simpleOnionRouter(nodeId: number) {
610
const onionRouter = express();
711
onionRouter.use(express.json());
812
onionRouter.use(bodyParser.json());
913

14+
let privateKey: CK;
15+
let publicKey: CK;
16+
let privateKeyStr: string;
17+
let publicKeyStr: string;
18+
1019
// Store the last received messages and destination (default: null)
1120
let lastReceivedEncryptedMessage: string | null = null;
1221
let lastReceivedDecryptedMessage: string | null = null;
1322
let lastMessageDestination: number | null = null;
1423

24+
try {
25+
const keyPair = await generateRsaKeyPair();
26+
privateKey = keyPair.privateKey;
27+
publicKey = keyPair.publicKey;
28+
29+
// Convert keys to base64
30+
publicKeyStr = await exportPubKey(publicKey);
31+
privateKeyStr = await exportPrvKey(privateKey);
32+
33+
// Register with the registry
34+
const response = await fetch(`http://localhost:${REGISTRY_PORT}/registerNode`, {
35+
method: "POST",
36+
headers: { "Content-Type": "application/json" },
37+
body: JSON.stringify({ nodeId, pubKey: publicKeyStr }),
38+
});
39+
40+
if (!response.ok) {
41+
throw new Error(`Failed to register node ${nodeId}`);
42+
}
43+
44+
console.log(`Node ${nodeId} registered successfully`);
45+
} catch (error) {
46+
console.error("Failed to initialize node:", error);
47+
throw error;
48+
}
49+
1550
// Status route
1651
onionRouter.get("/status", (req: Request, res: Response) => {
1752
res.send("live");
@@ -32,10 +67,13 @@ export async function simpleOnionRouter(nodeId: number) {
3267
res.json({ result: lastMessageDestination });
3368
});
3469

70+
// Provide private key for testing purposes
71+
onionRouter.get("/getPrivateKey", (req: Request, res: Response) => {
72+
res.json({ result: privateKeyStr });
73+
});
74+
3575
const server = onionRouter.listen(BASE_ONION_ROUTER_PORT + nodeId, () => {
36-
console.log(
37-
`Onion router ${nodeId} is listening on port ${BASE_ONION_ROUTER_PORT + nodeId}`
38-
);
76+
console.log(`Onion router ${nodeId} is listening on port ${BASE_ONION_ROUTER_PORT + nodeId}`);
3977
});
4078

4179
return server;

src/registry/registry.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import express, { Request, Response } from "express";
33
import { REGISTRY_PORT } from "../config";
44

55
export type Node = { nodeId: number; pubKey: string };
6+
const nodes: Node[] = [];
67

78
export type RegisterNodeBody = {
89
nodeId: number;
@@ -18,12 +19,36 @@ export async function launchRegistry() {
1819
_registry.use(express.json());
1920
_registry.use(bodyParser.json());
2021

21-
// TODO implement the status route
22-
// _registry.get("/status", (req, res) => {});
22+
// Status route
2323
_registry.get("/status", (req: Request, res: Response) => {
2424
res.send("live");
2525
});
2626

27+
_registry.post("/registerNode", (req: Request, res: Response) => {
28+
const { nodeId, pubKey } = req.body as RegisterNodeBody;
29+
30+
if (typeof nodeId !== "number" || typeof pubKey !== "string") {
31+
return res.status(400).json({ error: "Invalid request body" });
32+
}
33+
34+
// Check if the node already exists
35+
const existingNode = nodes.find((n) => n.nodeId === nodeId);
36+
if (existingNode) {
37+
existingNode.pubKey = pubKey;
38+
} else {
39+
nodes.push({ nodeId, pubKey });
40+
}
41+
42+
res.status(200).json({ success: true });
43+
return res.status(200).json({ success: true });
44+
});
45+
46+
// Implement /getNodeRegistry to return the list of registered nodes
47+
_registry.get("/getNodeRegistry", (req: Request, res: Response) => {
48+
res.json({ nodes });
49+
});
50+
51+
2752
const server = _registry.listen(REGISTRY_PORT, () => {
2853
console.log(`registry is listening on port ${REGISTRY_PORT}`);
2954
});

0 commit comments

Comments
 (0)