Files
wikideck/shared/network.ts
T
2026-11-01 16:43:28 -05:00

42 lines
2.4 KiB
TypeScript
Executable File

/** Selection is fixed at server startup / browser build, never learned from an untrusted RPC. */
export type NetworkProfile = 'testnet' | 'mainnet';
export function networkProfile(value: string | undefined): NetworkProfile {
const profile = value ?? 'mainnet';
if (profile !== 'testnet' && profile !== 'mainnet') {
throw new Error('WIKIDECK_NETWORK must be testnet or mainnet');
}
return profile;
}
const isNodeRuntime = typeof process !== 'undefined' && !!process.versions?.node;
export const PROFILE = networkProfile(isNodeRuntime
? process.env.WIKIDECK_NETWORK : import.meta.env?.VITE_WIKIDECK_NETWORK);
export const IS_MAINNET = PROFILE === 'mainnet';
export const NETWORK = IS_MAINNET ? 'wikideck-randomx-mainnet-1' as const : 'wikideck-randomx-testnet-6' as const;
const TESTNET_GENESIS_TIME = 1_792_411_200;
const DEFAULT_MAINNET_GENESIS_TIME = 1_793_491_200; // 2026-11-01 00:00 UTC; fixed launch identity, never derived from the current clock.
function genesisTime(raw: string | undefined, profile: NetworkProfile): number {
const value = raw ?? String(profile === 'mainnet' ? DEFAULT_MAINNET_GENESIS_TIME : TESTNET_GENESIS_TIME);
if (!/^(0|[1-9]\d{8,10})$/.test(value)) throw new Error('WIKIDECK_GENESIS_TIME must be a canonical Unix-seconds integer');
const parsed = Number(value);
if (!Number.isSafeInteger(parsed) || parsed < 1_700_000_000 || parsed > 4_102_444_800) {
throw new Error('WIKIDECK_GENESIS_TIME is outside the supported range');
}
return parsed;
}
export const NETWORK_GENESIS_TIME = genesisTime(isNodeRuntime
? process.env.WIKIDECK_GENESIS_TIME : import.meta.env?.VITE_WIKIDECK_GENESIS_TIME, PROFILE);
if (isNodeRuntime) {
const imageProfile = process.env.WIKIDECK_IMAGE_NETWORK;
const imageGenesisTime = process.env.WIKIDECK_IMAGE_GENESIS_TIME;
if (imageProfile !== undefined && imageProfile !== PROFILE) {
throw new Error('Runtime network profile does not match the immutable image manifest');
}
if (imageGenesisTime !== undefined && Number(imageGenesisTime) !== NETWORK_GENESIS_TIME) {
throw new Error('Runtime genesis time does not match the immutable image manifest');
}
if (imageGenesisTime !== undefined && !/^(0|[1-9]\d{8,10})$/.test(imageGenesisTime)) {
throw new Error('Image genesis time manifest is malformed');
}
}
export const NETWORK_LABEL = IS_MAINNET ? 'MAINNET' : 'TESTNET';