Files
proxmox/scripts/verify-ws-rpc-chain138.mjs

90 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
/**
* Verify WebSocket RPC for Chain 138 (wss://rpc-ws-pub.d-bis.org, wss://wss.defi-oracle.io).
* Sends eth_chainId and expects result "0x8a" (138).
* From LAN: set NPM_HOST=192.168.11.167 and use --resolve equivalent by connecting to that IP with Host header.
*
* Usage:
* node scripts/verify-ws-rpc-chain138.mjs [wss://rpc-ws-pub.d-bis.org|wss://wss.defi-oracle.io]
* NPM_HOST=192.168.11.167 node scripts/verify-ws-rpc-chain138.mjs # LAN: connect to NPMplus with Host
*
* Optional: npm install ws (or pnpm add -w ws)
*/
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const WSS_URL = process.argv[2] || 'wss://rpc-ws-pub.d-bis.org';
const NPM_HOST = process.env.NPM_HOST; // If set, connect to this IP with Host header (LAN test)
let WebSocket;
try {
WebSocket = require('ws');
} catch (e) {
console.error('Missing "ws" package. Run: pnpm add -w ws or npm install ws');
process.exit(1);
}
const url = NPM_HOST
? `wss://${NPM_HOST}`
: new URL(WSS_URL).href;
const options = NPM_HOST
? {
headers: { Host: new URL(WSS_URL).hostname },
rejectUnauthorized: false,
}
: {};
const payload = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
id: 1,
});
console.log(`Connecting to ${url}${NPM_HOST ? ` (Host: ${new URL(WSS_URL).hostname})` : ''}...`);
const ws = new WebSocket(url, options);
const timeout = setTimeout(() => {
console.error('Timeout waiting for response');
ws.close();
process.exit(1);
}, 10000);
ws.on('open', () => {
ws.send(payload);
});
ws.on('message', (data) => {
clearTimeout(timeout);
try {
const j = JSON.parse(data.toString());
const result = j.result || j.error;
if (j.result === '0x8a' || (typeof j.result === 'string' && j.result.toLowerCase() === '0x8a')) {
console.log('OK:', JSON.stringify(j));
console.log('Chain ID: 0x8a (138) Defi Oracle Meta Mainnet');
ws.close();
process.exit(0);
} else {
console.error('Unexpected result:', JSON.stringify(j));
ws.close();
process.exit(1);
}
} catch (e) {
console.error('Parse error:', e.message);
ws.close();
process.exit(1);
}
});
ws.on('error', (err) => {
clearTimeout(timeout);
console.error('WebSocket error:', err.message);
process.exit(1);
});
ws.on('close', (code, reason) => {
if (code !== 1000 && process.exitCode === undefined) process.exit(1);
});