Skip to content

Commit 4f7ee88

Browse files
committed
fix: add RPC proxy to avoid CORS issues in web terminal
1 parent 9778601 commit 4f7ee88

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

apps/hub/src/app/api/rpc/route.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* RPC Proxy Route
3+
*
4+
* Proxies RPC requests to the Demiurge blockchain node.
5+
* This avoids CORS issues when making requests from the browser.
6+
*/
7+
8+
import { NextRequest, NextResponse } from 'next/server';
9+
10+
const RPC_ENDPOINT = process.env.DEMIURGE_RPC_URL || 'https://rpc.demiurge.cloud';
11+
12+
export async function POST(request: NextRequest) {
13+
try {
14+
const body = await request.json();
15+
16+
// Validate it's a JSON-RPC request
17+
if (!body.jsonrpc || !body.method) {
18+
return NextResponse.json(
19+
{ error: { code: -32600, message: 'Invalid Request' } },
20+
{ status: 400 }
21+
);
22+
}
23+
24+
// Forward to the RPC endpoint
25+
const response = await fetch(RPC_ENDPOINT, {
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
},
30+
body: JSON.stringify(body),
31+
});
32+
33+
const data = await response.json();
34+
35+
return NextResponse.json(data);
36+
} catch (error) {
37+
console.error('[RPC Proxy] Error:', error);
38+
39+
return NextResponse.json(
40+
{
41+
jsonrpc: '2.0',
42+
id: null,
43+
error: {
44+
code: -32603,
45+
message: 'Internal error',
46+
data: error instanceof Error ? error.message : 'Unknown error',
47+
},
48+
},
49+
{ status: 500 }
50+
);
51+
}
52+
}
53+
54+
export async function OPTIONS() {
55+
return new NextResponse(null, {
56+
status: 200,
57+
headers: {
58+
'Access-Control-Allow-Origin': '*',
59+
'Access-Control-Allow-Methods': 'POST, OPTIONS',
60+
'Access-Control-Allow-Headers': 'Content-Type',
61+
},
62+
});
63+
}

apps/hub/src/components/terminal/WebTerminal.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ interface CommandResult {
3232
// Constants
3333
// ============================================================================
3434

35-
const RPC_URL = process.env.NEXT_PUBLIC_RPC_URL || 'https://rpc.demiurge.cloud';
35+
// Use local proxy to avoid CORS issues
36+
const RPC_URL = '/api/rpc';
37+
const RPC_DISPLAY_URL = process.env.NEXT_PUBLIC_RPC_URL || 'https://rpc.demiurge.cloud';
3638

3739
const HELP_TEXT = `
3840
╔══════════════════════════════════════════════════════════════════════════╗
@@ -83,7 +85,7 @@ const WELCOME_MESSAGE = `
8385
║ On-Chain CLI Terminal v1.0.0 - Web Interface ║
8486
║ ║
8587
║ Type 'help' for available commands ║
86-
║ Connected to: ${RPC_URL.padEnd(52)}
88+
║ Connected to: ${RPC_DISPLAY_URL.padEnd(52)}
8789
╚══════════════════════════════════════════════════════════════════════════╝
8890
`;
8991

@@ -94,9 +96,11 @@ const WELCOME_MESSAGE = `
9496
class CommandExecutor {
9597
private client: DemiurgeClient;
9698
private rpcUrl: string;
99+
private displayUrl: string;
97100

98-
constructor(rpcUrl: string = RPC_URL) {
101+
constructor(rpcUrl: string = RPC_URL, displayUrl: string = RPC_DISPLAY_URL) {
99102
this.rpcUrl = rpcUrl;
103+
this.displayUrl = displayUrl;
100104
this.client = new DemiurgeClient({ endpoint: rpcUrl });
101105
}
102106

@@ -158,7 +162,7 @@ class CommandExecutor {
158162
type: 'output',
159163
content: JSON.stringify({
160164
blockHeight: blockNumber,
161-
rpcEndpoint: this.rpcUrl,
165+
rpcEndpoint: this.displayUrl,
162166
network: 'Demiurge Mainnet',
163167
status: 'Online',
164168
}, null, 2),
@@ -172,7 +176,7 @@ class CommandExecutor {
172176
│ Metric │ Value │
173177
├────────────────────────────────┼────────────────────────────────────────────┤
174178
│ Block Height │ ${String(blockNumber).padEnd(42)}
175-
│ RPC Endpoint │ ${this.rpcUrl.padEnd(42)}
179+
│ RPC Endpoint │ ${this.displayUrl.padEnd(42)}
176180
│ Network │ Demiurge Mainnet │
177181
│ Status │ ● Online │
178182
└────────────────────────────────┴────────────────────────────────────────────┘
@@ -685,7 +689,7 @@ export function WebTerminal({ isOpen, onClose }: WebTerminalProps) {
685689
</span>
686690
</div>
687691
<div className="flex items-center gap-4 text-xs text-text-tertiary font-mono">
688-
<span>RPC: {RPC_URL}</span>
692+
<span>RPC: {RPC_DISPLAY_URL}</span>
689693
<span className="text-green-400">● Connected</span>
690694
</div>
691695
</div>

0 commit comments

Comments
 (0)