This commit is contained in:
sergeych committed 2026-07-04 19:36:12 +03:00
commit 335b521229
11 files changed
+1032

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
#!/usr/bin/env node
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { basename, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Command } from 'commander';
const require = createRequire(import.meta.url);
const scriptDirectory = dirname(fileURLToPath(import.meta.url));
const topologyFile = resolve(scriptDirectory, 'indigo_demo_2025.json');
const universaCoreDist = resolve(scriptDirectory, '../node_modules/universa-core/dist');
function withTimeout(promise, timeoutMs, message) {
let timeoutId;
const timeout = new Promise((_, reject) => {
timeoutId = setTimeout(() => reject(new Error(message)), timeoutMs);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timeoutId));
}
async function withUniversaWasmLoader(callback) {
const fetch = globalThis.fetch;
const read = globalThis.read;
const readBinary = globalThis.readBinary;
globalThis.fetch = undefined;
globalThis.read = (fileName, binary) => {
const wasmPath = resolve(universaCoreDist, basename(fileName));
const data = readFileSync(wasmPath);
return binary ? data : data.toString('binary');
};
globalThis.readBinary = (fileName) => {
const wasmPath = resolve(universaCoreDist, basename(fileName));
return readFileSync(wasmPath);
};
try {
return await callback();
} finally {
globalThis.fetch = fetch;
if (read === undefined) delete globalThis.read;
else globalThis.read = read;
if (readBinary === undefined) delete globalThis.readBinary;
else globalThis.readBinary = readBinary;
}
}
function loadUniversaCore() {
const window = globalThis.window;
globalThis.window = globalThis;
try {
return require('universa-core');
} finally {
if (window === undefined) delete globalThis.window;
else globalThis.window = window;
}
}
function generatePrivateKey() {
return withUniversaWasmLoader(async () => {
const { PrivateKey } = require('unicrypto');
return PrivateKey.generate({ strength: 2048 });
});
}
async function loadTopology(Topology) {
return withUniversaWasmLoader(async () => {
const packedTopology = JSON.parse(readFileSync(topologyFile, 'utf8'));
return Topology.load(packedTopology);
});
}
const program = new Command();
program
.name('u-js-sample')
.description('Example Node.js console script with universa-core functions.')
.option('--generate-key', 'generate a Universa private key and print its short address')
.option('--test-network', 'generate a key and connect to the Universa network from indigo_demo_2025.json')
.action(async (options) => {
if (options.generateKey || options.testNetwork) {
const privateKey = await generatePrivateKey();
console.log(`Universa short address: ${privateKey.publicKey.shortAddress58}`);
if (options.testNetwork) {
const { Network, Topology } = loadUniversaCore();
const topology = await loadTopology(Topology);
const network = new Network(privateKey, { topology, directConnection: true });
console.log(`Connecting with topology: ${topologyFile}`);
await withTimeout(network.connect(), 20000, 'Network connection timed out');
console.log(`Connected to ${network.size()} nodes`);
const response = await withTimeout(network.command('sping'), 10000, 'Network sping command timed out');
console.log(`sping response: ${JSON.stringify(response)}`);
}
}
});
try {
await program.parseAsync();
} catch (error) {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
} finally {
process.exit();
}
+49
View File
@@ -0,0 +1,49 @@
{
"list": [
{
"number": 1,
"domain_urls": [
"https://31.41.155.11"
],
"name": "node-01",
"direct_urls": [
"http://31.41.155.11:8080"
],
"key": "HggcAQABxAAB5IfycVepakIjOt3c5eyLFswYH8dokrmHTyRIgaCSz+EY+mm8hM2Cn1PJjyiJj4K1A8CPN9p9mj6z/yyGSH6pvW3HMGs6Az7KvhGbZJlsFP0tNy6A3YQEWP/XToSqtT3f0Ji/r2nYw2zEgnGvOBkkcyN/EEslnxTj1R3fZlaSHh9Uem6NFk62ZxSBj8hJkvfHbTB/XZ2z8wtpwcAJClylITbbCSg9RdXoD8ZwCJMaCWasxfdo5+sMYTcABYFErbw8ndeQ5aL3N2+XiFM8WO5wQnd3SNTYUQGucBkXO5zqmZjnreVOWIsQtKzjwH4WIi57kz+87V72TbDrX0jYMYsOSw=="
},
{
"number": 2,
"domain_urls": [
"https://37.9.5.102"
],
"name": "node-02",
"direct_urls": [
"http://37.9.5.102:8080"
],
"key": "HggcAQABxAAB3GST2bRm2SrwypfKZRplqDwLUvDF7DoRgVMZfzOHzYodHfIpNrRKZfJdr63UdpK6359G1TAAHm52UaLCaCqBZNuFDqE9s9fEe+2UoSzoMKGG0NtgFD2HdDhuNWw4LnqZiZ5lPlZuUKtxVCzhlI0SNweBKMn0oopjdOYfx7hkPHVB2TyADXPrcnxFHEpc6USjJgP3aHPgum4CxYD09KTF59t5W+B+rDpTd/ro3ENBSFi9xlt4qMrLx5KzIdm8IqcsEm+mTYS5mx0F4e6wlCiXElwGe1NdXNwb/A8S2JBSUKP2CfSKdJtB0D7GHFnMuF3+u29/pykBOvlUoUa4S5QxJQ=="
},
{
"number": 3,
"domain_urls": [
"https://37.9.4.133"
],
"name": "node-03",
"direct_urls": [
"http://37.9.4.133:8080"
],
"key": "HggcAQABxAABtTajk8wkzstB+mmj09fkP/a9IqX4tVuuXnd/fO6a3TAjNbulqtkvVyfhu2DeUdUFiYP9kc+YQmHsIe5T/xZgdecWAbHKT0Ok530nLju8dog+75j6aCqD+winCCb+s7dzAYNIsKS0QgBPPWyBUyDL+di17s2Z5U2mibV+sXQYfWU4Gy02WNuavC8QZ0gA7K3Ah6ohR/Yn37WpiWvS+kQOnZF/q/8LYRQgxdDrztzTgpouggwk/1VJ181fYcXq/54n+L5yV2M0GZy7B942ce07MV6EGmQJu4eoOQi8DN3Ee30WOuIIiEyiQLMJjva0uxvkXmlftsr8rGiLXB+kiMd2fQ=="
},
{
"number": 4,
"domain_urls": [
"https://37.9.4.137"
],
"name": "node-04",
"direct_urls": [
"http://37.9.4.137:8080"
],
"key": "HggcAQABxAABkGzNgQnnNh/aNR3zQujD/6mgcqqfZ3tl14DDLhXLunUhc8hkn3y+ViukIj1qbkPi6Ps5irDZ2TzVfepoVWqX+XkI8wUbXKpDN/zWYx9kW4/soA4C7KS6fS/NFsFflUyzDNQeQXmiQkPTNnn43sdSqYb6Zr3I7Wa6mEwIP7MakKo6SbJQ/gTdXPlKWnU1nGJISMowCfhawYQLZgI5URsgS3VV7Owb3Yo4IZ0YNDTceboY4ljYuc/R2LqJcGjcWfSweaHV2T64RY6yQt1W0h2KweK99cflJdh/QSCqBhRdSYYfgKDlmp2ZTvrrfj0bbz2mPBh5JG5RQe+O9MzT0Is4ew=="
}
],
"updated": 1742911675
}