nano-pow
Version:
Proof-of-work generation and validation with WebGPU/WebGL for Nano cryptocurrency.
309 lines (301 loc) • 9.93 kB
JavaScript
// src/bin/server.ts
import * as http from "node:http";
import { hash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { homedir } from "node:os";
import { join } from "node:path";
import { launch } from "puppeteer";
// src/utils/index.ts
function isHex(input, min, max) {
if (typeof input !== "string") {
return false;
}
if (typeof min !== "undefined" && typeof min !== "number") {
throw new Error(`Invalid argument for parameter 'min'`);
}
if (typeof max !== "undefined" && typeof max !== "number") {
throw new Error(`Invalid argument for parameter 'max'`);
}
const range = min === void 0 && max === void 0 ? "+" : `{${min ?? "0"},${max ?? ""}}`;
const regexp = new RegExp(`^[0-9A-Fa-f]${range}$`, "m");
return regexp.test(input);
}
function isNotHex(input, min, max) {
return !isHex(input, min, max);
}
function log(...args) {
if (process?.env?.NANO_POW_DEBUG) {
const entry = `${new Date(Date.now()).toLocaleString(Intl.DateTimeFormat().resolvedOptions().locale ?? "en-US", { hour12: false, dateStyle: "medium", timeStyle: "medium" })} NanoPow[${process.pid}]: ${args}`;
console.log(entry);
process.send?.({ type: "console", message: entry });
}
}
// src/bin/server.ts
//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
//! SPDX-License-Identifier: GPL-3.0-or-later
process.title = "NanoPow Server";
var MAX_CONNECTIONS = 1024;
var MAX_HEADER_COUNT = 32;
var MAX_IDLE_TIME = 5e3;
var MAX_REQUEST_COUNT = 10;
var MAX_REQUEST_SIZE = 256;
var MAX_REQUEST_TIME = 6e4;
var CONFIG = {
DEBUG: false,
EFFORT: 8,
PORT: 5040
};
var configPatterns = {
DEBUG: {
r: /^[ \t]*debug[ \t]*(true|false)[ \t]*(#.*)?$/i,
v: (b) => ["1", "true", "yes"].includes(b)
},
EFFORT: {
r: /^[ \t]*effort[ \t]*(\d{1,2})[ \t]*(#.*)?$/i,
v: (n) => +n
},
PORT: {
r: /^[ \t]*port[ \t]*(\d{1,5})[ \t]*(#.*)?$/i,
v: (n) => +n
}
};
async function loadConfig() {
let configFile = "";
try {
configFile = await readFile(join(homedir(), ".nano-pow", "config"), "utf-8");
} catch {
log("Config file not found");
}
if (configFile.length > 0) {
for (const line of configFile.split("\n")) {
for (const [k, { r, v }] of Object.entries(configPatterns)) {
const m = r.exec(line);
if (m) CONFIG[k] = v(m[1]);
}
}
}
CONFIG.DEBUG = ["1", "true", "yes"].includes(process.env.NANO_POW_DEBUG ?? "") || CONFIG.DEBUG;
CONFIG.EFFORT = +(process.env.NANO_POW_EFFORT ?? "") || CONFIG.EFFORT;
CONFIG.PORT = process.send ? 0 : +(process.env.NANO_POW_PORT ?? "") || CONFIG.PORT;
log(`Config loaded ${JSON.stringify(CONFIG)}`);
}
await loadConfig();
log("Starting NanoPow work server");
var NanoPow = await readFile(new URL("../main.min.js", import.meta.url), "utf-8");
var browser = await launch({
handleSIGHUP: false,
handleSIGINT: false,
handleSIGTERM: false,
headless: true,
args: [
"--headless=new",
"--disable-vulkan-surface",
"--enable-features=Vulkan,DefaultANGLEVulkan,VulkanFromANGLE",
"--enable-gpu",
"--enable-unsafe-webgpu"
]
});
var page = await browser.newPage();
var src = `${NanoPow};window.NanoPow=NanoPowGpu;`;
var enc = `sha256-${hash("sha256", src, "base64")}`;
var body = `
<!DOCTYPE html>
<link rel="icon" href="data:,">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; base-uri 'none'; form-action 'none'; script-src '${enc}';">
<script type="module">${src}</script>
`;
await page.setRequestInterception(true);
page.on("request", (req) => {
if (req.isInterceptResolutionHandled()) return;
if (req.url() === "https://nanopow.invalid/") {
req.respond({ status: 200, contentType: "text/html", body });
} else {
req.continue();
}
});
page.on("console", (msg) => log(msg.text()));
await page.goto("https://nanopow.invalid/");
var NanoPowHandle = await page.waitForFunction(() => {
return window.NanoPow;
});
log("Puppeteer initialized");
var requests = /* @__PURE__ */ new Map();
setInterval(() => {
const now = Date.now();
for (const [ip, { time }] of requests) {
if (time < now - MAX_REQUEST_TIME) {
requests.delete(ip);
}
}
}, MAX_REQUEST_TIME);
function get(res) {
res.writeHead(200, { "Content-Type": "text/plain" }).end(
`Usage: Send POST request to server URL to generate or validate Nano proof-of-work
Generate work for a BLOCKHASH with an optional DIFFICULTY:
curl -d '{ action: "work_generate", hash: BLOCKHASH, difficulty?: DIFFICULTY }'
Validate WORK previously calculated for a BLOCKHASH with an optional DIFFICULTY:
curl -d '{ action: "work_validate", work: WORK, hash: BLOCKHASH, difficulty?: DIFFICULTY }'
BLOCKHASH is a 64-character hexadecimal string.
WORK is 16-character hexadecimal string.
DIFFICULTY is a 16-character hexadecimal string (default: FFFFFFF800000000)
Report bugs: <bug-nano-pow@zoso.dev>
Full documentation: <https://www.npmjs.com/package/nano-pow>
`
);
}
async function post(res, reqData) {
const resHeaders = { "Content-Type": "application/json" };
let resStatusCode = 500;
let resBody = "request failed";
try {
const reqBody = JSON.parse(Buffer.concat(reqData).toString());
if (Object.getPrototypeOf(reqBody) !== Object.prototype) {
throw new Error("Data corrupted.");
}
const { action, hash: hash2, work, difficulty } = reqBody;
if (action !== "work_generate" && action !== "work_validate") {
throw new Error("Action must be work_generate or work_validate.");
}
resBody = `${action} failed`;
if (isNotHex(hash2, 64)) {
throw new Error("Hash must be a 64-character hex string.");
}
if (difficulty && isNotHex(difficulty, 1, 16)) {
throw new Error("Difficulty must be a hex string between 0-FFFFFFFFFFFFFFFF.");
}
if (action === "work_validate" && isNotHex(work, 16)) {
throw new Error("Work must be a 16-character hex string.");
}
const options = {
debug: CONFIG.DEBUG,
effort: CONFIG.EFFORT,
difficulty
};
const result = action === "work_generate" ? await page.evaluate((n, h, o) => {
if (n == null) throw new Error("NanoPow not found");
return n.work_generate(h, o);
}, NanoPowHandle, hash2, options) : await page.evaluate((n, w, h, o) => {
if (n == null) throw new Error("NanoPow not found");
return n.work_validate(w, h, o);
}, NanoPowHandle, work, hash2, options);
resBody = JSON.stringify(result);
resStatusCode = 200;
} catch (err) {
log(err);
resStatusCode = 400;
} finally {
res.writeHead(resStatusCode, resHeaders).end(resBody);
}
}
function getIp(req) {
const xff = req.headers["x-forwarded-for"];
const ip = typeof xff === "string" ? xff.split(",")[0].trim().replace(/^::ffff:/, "") : req.socket.remoteAddress;
return ip;
}
function isRateLimited(ip) {
if (ip === "127.0.0.1" || ip === "::1" || process.send) {
return false;
}
const client = requests.get(ip);
if (client && client.tokens-- <= 0) {
log(`==== Potential Abuse: ${ip} ====`);
return true;
}
if (Date.now() - MAX_REQUEST_TIME > (client?.time ?? 0)) {
requests.set(ip, { tokens: MAX_REQUEST_COUNT, time: Date.now() });
}
return false;
}
function listen() {
server.listen(CONFIG.PORT, "127.0.0.1", () => {
const { port } = server.address();
CONFIG.PORT = port;
log(`Server listening on port ${port}`);
process.send?.({ type: "listening", message: port });
});
}
async function readIncomingMessage(req) {
return new Promise((resolve, reject) => {
const contentLength = +(req.headers["content-length"] ?? 0);
if (contentLength === 0 || contentLength > MAX_REQUEST_SIZE) {
reject(new Error("Content Too Large", { cause: { code: 413 } }));
}
const kill = setTimeout(() => {
reject(new Error("Request Timeout", { cause: { code: 408 } }));
}, MAX_IDLE_TIME);
const reqData = [];
let reqSize = 0;
req.on("data", (chunk) => {
reqSize += chunk.byteLength;
if (reqSize > MAX_REQUEST_SIZE) {
reject(new Error("Content Too Large", { cause: { code: 413 } }));
}
reqData.push(chunk);
});
req.on("end", () => {
clearTimeout(kill);
resolve(reqData);
});
req.once("error", reject);
});
}
var server = http.createServer(async (req, res) => {
const ip = getIp(req);
if (!ip) return res.writeHead(401).end("Unauthorized");
if (isRateLimited(ip)) return res.writeHead(429).end("Too Many Requests");
if (req.method === "POST") {
try {
const reqData = await readIncomingMessage(req);
post(res, reqData);
} catch (err) {
req.socket.destroy();
return res.writeHead(err.cause?.code ?? 500).end(err.message ?? "Internal Server Error");
}
} else {
get(res);
}
});
server.headersTimeout = MAX_IDLE_TIME;
server.keepAliveTimeout = MAX_IDLE_TIME;
server.maxConnections = MAX_CONNECTIONS;
server.maxHeadersCount = MAX_HEADER_COUNT;
server.on("connection", (s) => {
s.setTimeout(MAX_IDLE_TIME, () => s.destroy());
});
server.on("error", (serverErr) => {
log("Server error", serverErr);
try {
shutdown();
} catch (shutdownErr) {
log("Failed to shut down", shutdownErr);
process.exit(1);
}
});
function shutdown() {
log("Shutdown signal received");
const kill = setTimeout(() => {
log("Server unresponsive, forcefully stopped");
process.exit(1);
}, 1e4);
server.close(async () => {
await browser.close();
clearTimeout(kill);
log("Server stopped");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
process.on("SIGHUP", async () => {
log("Reloading configuration");
server.close(async () => {
await loadConfig();
await page.reload();
NanoPowHandle = await page.waitForFunction(() => {
return window.NanoPow;
});
listen();
});
});
listen();