@ocap/chain-node 1.32.1 → 1.32.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/access-log.d.mts +5 -2
- package/esm/access-log.mjs +5 -3
- package/esm/config-consistency.mjs +38 -2
- package/esm/did-tls.d.mts +192 -0
- package/esm/did-tls.mjs +1606 -0
- package/esm/env.d.mts +17 -2
- package/esm/env.mjs +126 -1
- package/esm/follower-config.d.mts +3 -2
- package/esm/follower-config.mjs +30 -4
- package/esm/index.d.mts +6 -4
- package/esm/index.mjs +5 -3
- package/esm/mesh-egress.d.mts +30 -0
- package/esm/mesh-egress.mjs +152 -0
- package/esm/start-chain-node.d.mts +6 -1
- package/esm/start-chain-node.mjs +822 -49
- package/esm/types.d.mts +68 -2
- package/lib/access-log.cjs +4 -2
- package/lib/access-log.d.cts +5 -2
- package/lib/config-consistency.cjs +38 -2
- package/lib/did-tls.cjs +1628 -0
- package/lib/did-tls.d.cts +192 -0
- package/lib/env.cjs +131 -1
- package/lib/env.d.cts +17 -2
- package/lib/follower-config.cjs +30 -3
- package/lib/follower-config.d.cts +3 -2
- package/lib/index.cjs +30 -1
- package/lib/index.d.cts +6 -4
- package/lib/mesh-egress.cjs +155 -0
- package/lib/mesh-egress.d.cts +30 -0
- package/lib/start-chain-node.cjs +824 -48
- package/lib/start-chain-node.d.cts +6 -1
- package/lib/types.d.cts +67 -1
- package/package.json +16 -16
package/esm/start-chain-node.mjs
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import { createAccessLogMiddleware } from "./access-log.mjs";
|
|
2
|
-
import { verifyFollowerConfigAgainstUpstream } from "./config-consistency.mjs";
|
|
3
|
-
import {
|
|
2
|
+
import { fetchUpstreamChainConfig, verifyFollowerConfigAgainstUpstream } from "./config-consistency.mjs";
|
|
3
|
+
import { canonicalizeTlsDid, decodeEd25519PublicKeyPin, didTlsBytesToHex, issueDidTlsCertificate, parseDidTlsCertificate, verifyDidTlsPeerCertificate } from "./did-tls.mjs";
|
|
4
|
+
import { createChainNodePeerFetchGuard, parseMeshEgressUrl } from "./mesh-egress.mjs";
|
|
5
|
+
import { assertNoSymlinkAncestors } from "./env.mjs";
|
|
6
|
+
import { isCompleteChainConfig, orphanIndexFromSequence, rehydrateFollowerChainConfig } from "./follower-config.mjs";
|
|
4
7
|
import { assertValidChainNodeTestHooks } from "./test-hooks-token.mjs";
|
|
5
8
|
import { CustomError } from "@ocap/util/lib/error";
|
|
6
|
-
import { LeaderRouter, Ledger, bytesToHex, canonicalizeMultiCurvePublicKey, createCheckpointSigner, createEnvAnchorPublisher, createEnvColdArchiveReader, createEnvSegmentPublisher, createEnvWitnessRegistry, decodeMultiCurvePublicKey, normalizeDidForCompare, resolveConsensusProposerFromSigner, toHotStuffClusterConfig, verifyDidMatchesMultiCurvePublicKey } from "@arcblock/ledger-core";
|
|
7
|
-
import { SqliteHotStuffStateStore, SqliteLedgerStorage, formatLocalProcessHolderId, hasExistingFollowerDatabases, persistFollowerHighWaterMark, prepareFollowerDataDirectory, readFollowerTrustedSecretKeyIfPresent, unlinkFollowerTrustedSecretKeyIfPresent } from "@arcblock/ledger-sqlite";
|
|
8
9
|
import crypto from "node:crypto";
|
|
10
|
+
import { isValid } from "@arcblock/did";
|
|
9
11
|
import fs from "node:fs";
|
|
10
12
|
import path from "node:path";
|
|
11
|
-
import {
|
|
13
|
+
import { LeaderRouter, Ledger, bytesToHex, canonicalizeMultiCurvePublicKey, createCheckpointSigner, createDaFollowerSource, createEnvAnchorPublisher, createEnvColdArchiveReader, createEnvSegmentPublisher, createEnvWitnessRegistry, daPublisherInputsFromEnv, decodeMultiCurvePublicKey, isDaArchiveMode, normalizeDidForCompare, resolveConsensusProposerFromSigner, scrubDaCredentials, toHotStuffClusterConfig, verifyDidMatchesMultiCurvePublicKey } from "@arcblock/ledger-core";
|
|
14
|
+
import { SqliteHotStuffStateStore, SqliteLedgerStorage, createDaResumeFileStore, formatLocalProcessHolderId, hasExistingFollowerDatabases, persistFollowerHighWaterMark, prepareFollowerDataDirectory, readFollowerTrustedSecretKeyIfPresent, unlinkFollowerTrustedSecretKeyIfPresent } from "@arcblock/ledger-sqlite";
|
|
15
|
+
import net from "node:net";
|
|
16
|
+
import OCAPResolver, { FollowerSyncService, HotStuffNodeRuntime, NodePreferencesManager, applyBootRuntimePreferences, createAdminAuthGuard, createExplorerMiddleware, createNodeDashboardRouter, createNodeMaintenanceController, createUnifiedConsoleMiddleware, isDashboardEnabled, isExplorerEnabled } from "@ocap/resolver";
|
|
17
|
+
import tls from "node:tls";
|
|
12
18
|
import { createHttpHandler, createSocketHandler } from "@ocap/gql";
|
|
13
19
|
import SqliteIndexDB from "@ocap/indexdb-sqlite";
|
|
14
|
-
import OCAPResolver, { FollowerSyncService, HotStuffNodeRuntime, NodePreferencesManager, applyBootRuntimePreferences, createAdminAuthGuard, createExplorerMiddleware, createFollowerPeerFetchGuard, createNodeDashboardRouter, createNodeMaintenanceController, createUnifiedConsoleMiddleware, isDashboardEnabled, isExplorerEnabled } from "@ocap/resolver";
|
|
15
20
|
import { createAPIHandler } from "@ocap/resolver/api";
|
|
16
21
|
import { Blacklist } from "@ocap/state";
|
|
17
22
|
import SqliteStateDB from "@ocap/statedb-sqlite";
|
|
@@ -23,7 +28,132 @@ import express from "express";
|
|
|
23
28
|
const CHAIN_STATE_ADDR = crypto.createHash("md5").update("OCAP_CHAIN_ADDR").digest("hex");
|
|
24
29
|
const NODE_ID_FILENAME = ".node-id";
|
|
25
30
|
const DEFAULT_DRAIN_TIMEOUT_MS = 5e3;
|
|
31
|
+
const DEFAULT_TLS_HANDSHAKE_TIMEOUT_MS = 1e4;
|
|
32
|
+
const MAX_TLS_CREDENTIAL_FILE_BYTES = 65536;
|
|
26
33
|
const FORMATTED_HOLDER_ID_RE = /:\d+:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
34
|
+
function readNonSymlinkRegularFileUtf8(rawPath, label) {
|
|
35
|
+
const resolved = path.resolve(rawPath.trim());
|
|
36
|
+
assertNoSymlinkAncestors(resolved, label);
|
|
37
|
+
let lstat;
|
|
38
|
+
try {
|
|
39
|
+
lstat = fs.lstatSync(resolved);
|
|
40
|
+
} catch (err) {
|
|
41
|
+
throw new CustomError("INVALID_CONFIG", `${label} file not found (${resolved}): ${err?.code || "ENOENT"}`);
|
|
42
|
+
}
|
|
43
|
+
if (lstat.isSymbolicLink()) throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked ${label} file: ${resolved}`);
|
|
44
|
+
if (!lstat.isFile()) throw new CustomError("INVALID_CONFIG", `${label} must be a regular file: ${resolved}`);
|
|
45
|
+
if (lstat.size === 0 || lstat.size > MAX_TLS_CREDENTIAL_FILE_BYTES) throw new CustomError("INVALID_CONFIG", `${label} file size (${lstat.size}B) out of range 1..${MAX_TLS_CREDENTIAL_FILE_BYTES}`);
|
|
46
|
+
const openFlags = fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0);
|
|
47
|
+
let fd;
|
|
48
|
+
try {
|
|
49
|
+
fd = fs.openSync(resolved, openFlags);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
if (err?.code === "ELOOP") throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked ${label} file: ${resolved}`);
|
|
52
|
+
throw new CustomError("INVALID_CONFIG", `Failed to open ${label} file (${resolved}): ${err?.code || "EIO"}`);
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const fstat = fs.fstatSync(fd);
|
|
56
|
+
if (!fstat.isFile() || fstat.size === 0 || fstat.size > MAX_TLS_CREDENTIAL_FILE_BYTES) throw new CustomError("INVALID_CONFIG", `Invalid ${label} file descriptor state: ${resolved}`);
|
|
57
|
+
const buf = new Uint8Array(fstat.size);
|
|
58
|
+
const bytesRead = fs.readSync(fd, buf, 0, fstat.size, 0);
|
|
59
|
+
return new TextDecoder().decode(buf.subarray(0, bytesRead));
|
|
60
|
+
} finally {
|
|
61
|
+
fs.closeSync(fd);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function buildChainTlsSecureContextOptions(certPem, keyPem) {
|
|
65
|
+
return {
|
|
66
|
+
minVersion: "TLSv1.3",
|
|
67
|
+
maxVersion: "TLSv1.3",
|
|
68
|
+
secureOptions: crypto.constants.SSL_OP_NO_TICKET,
|
|
69
|
+
cert: certPem,
|
|
70
|
+
key: keyPem
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function countPinnedIssuers(pins) {
|
|
74
|
+
if (!pins) return 0;
|
|
75
|
+
if (pins instanceof Map) {
|
|
76
|
+
for (const [didKey, pkVal] of pins.entries()) {
|
|
77
|
+
canonicalizeTlsDid(didKey, "allowedIssuerPublicKeys DID");
|
|
78
|
+
decodeEd25519PublicKeyPin(pkVal, "allowedIssuerPublicKeys pubkey");
|
|
79
|
+
}
|
|
80
|
+
return pins.size;
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(pins) || pins instanceof Set) {
|
|
83
|
+
let count = 0;
|
|
84
|
+
for (const item of pins) {
|
|
85
|
+
if (typeof item === "string" && item.includes("=")) {
|
|
86
|
+
const eq = item.indexOf("=");
|
|
87
|
+
canonicalizeTlsDid(item.slice(0, eq), "allowedIssuerPublicKeys DID");
|
|
88
|
+
decodeEd25519PublicKeyPin(item.slice(eq + 1), "allowedIssuerPublicKeys pubkey");
|
|
89
|
+
} else decodeEd25519PublicKeyPin(item, "allowedIssuerPublicKeys pubkey");
|
|
90
|
+
count++;
|
|
91
|
+
}
|
|
92
|
+
return count;
|
|
93
|
+
}
|
|
94
|
+
if (typeof pins === "object") {
|
|
95
|
+
const entries = Object.entries(pins);
|
|
96
|
+
for (const [didKey, pkVal] of entries) {
|
|
97
|
+
canonicalizeTlsDid(didKey, "allowedIssuerPublicKeys DID");
|
|
98
|
+
decodeEd25519PublicKeyPin(pkVal, "allowedIssuerPublicKeys pubkey");
|
|
99
|
+
}
|
|
100
|
+
return entries.length;
|
|
101
|
+
}
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
function validateChainHttpTlsConfig(rawTls) {
|
|
105
|
+
if (!rawTls?.enabled) return null;
|
|
106
|
+
const wireAlgorithm = rawTls.wireAlgorithm ?? "p256";
|
|
107
|
+
if (wireAlgorithm !== "ed25519" && wireAlgorithm !== "p256") throw new CustomError("INVALID_CONFIG", `Invalid http.tls.wireAlgorithm="${String(wireAlgorithm)}"`);
|
|
108
|
+
const requireClientCert = rawTls.requireClientCert !== false;
|
|
109
|
+
const handshakeTimeoutMs = typeof rawTls.handshakeTimeoutMs === "number" && Number.isFinite(rawTls.handshakeTimeoutMs) && rawTls.handshakeTimeoutMs >= 500 ? Math.trunc(rawTls.handshakeTimeoutMs) : DEFAULT_TLS_HANDSHAKE_TIMEOUT_MS;
|
|
110
|
+
const certFile = rawTls.certFile?.trim() || void 0;
|
|
111
|
+
const keyFile = rawTls.keyFile?.trim() || void 0;
|
|
112
|
+
if (Boolean(certFile) !== Boolean(keyFile)) throw new CustomError("INVALID_CONFIG", "http.tls.certFile and http.tls.keyFile must both be provided together");
|
|
113
|
+
if (certFile && keyFile) {
|
|
114
|
+
readNonSymlinkRegularFileUtf8(certFile, "http.tls.certFile");
|
|
115
|
+
readNonSymlinkRegularFileUtf8(keyFile, "http.tls.keyFile");
|
|
116
|
+
}
|
|
117
|
+
let pinCount = 0;
|
|
118
|
+
try {
|
|
119
|
+
pinCount = countPinnedIssuers(rawTls.allowedIssuerPublicKeys);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
throw new CustomError("INVALID_CONFIG", `Invalid http.tls.allowedIssuerPublicKeys: ${err instanceof Error ? err.message : String(err)}`);
|
|
122
|
+
}
|
|
123
|
+
if (requireClientCert && pinCount === 0) throw new CustomError("INVALID_CONFIG", "DID-TLS requireClientCert=true requires at least one pinned Ed25519 key in http.tls.allowedIssuerPublicKeys");
|
|
124
|
+
const allowedClientDids = [];
|
|
125
|
+
for (const rawDid of rawTls.allowedClientDids ?? []) try {
|
|
126
|
+
allowedClientDids.push(canonicalizeTlsDid(rawDid, "http.tls.allowedClientDids"));
|
|
127
|
+
} catch (err) {
|
|
128
|
+
throw new CustomError("INVALID_CONFIG", `Invalid DID in http.tls.allowedClientDids: ${err instanceof Error ? err.message : String(err)}`);
|
|
129
|
+
}
|
|
130
|
+
let subjectDid;
|
|
131
|
+
if (rawTls.subjectDid?.trim()) try {
|
|
132
|
+
subjectDid = canonicalizeTlsDid(rawTls.subjectDid, "http.tls.subjectDid");
|
|
133
|
+
} catch (err) {
|
|
134
|
+
throw new CustomError("INVALID_CONFIG", `Invalid http.tls.subjectDid: ${err instanceof Error ? err.message : String(err)}`);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
enabled: true,
|
|
138
|
+
wireAlgorithm,
|
|
139
|
+
requireClientCert,
|
|
140
|
+
hasConfiguredClientTrustPins: pinCount > 0,
|
|
141
|
+
handshakeTimeoutMs,
|
|
142
|
+
...certFile ? { certFile } : {},
|
|
143
|
+
...keyFile ? { keyFile } : {},
|
|
144
|
+
...rawTls.identitySecretKey?.trim() ? { identitySecretKey: rawTls.identitySecretKey.trim() } : {},
|
|
145
|
+
...subjectDid ? { subjectDid } : {},
|
|
146
|
+
sanDnsNames: rawTls.sanDnsNames ?? [],
|
|
147
|
+
sanIpAddresses: rawTls.sanIpAddresses ?? [],
|
|
148
|
+
...rawTls.validityDays !== void 0 ? { validityDays: rawTls.validityDays } : {},
|
|
149
|
+
basePeerPolicy: {
|
|
150
|
+
allowedIssuerPublicKeys: rawTls.allowedIssuerPublicKeys,
|
|
151
|
+
...allowedClientDids.length > 0 ? { allowedSubjectDids: allowedClientDids } : {},
|
|
152
|
+
expectedRole: "client",
|
|
153
|
+
...rawTls.clockSkewSeconds !== void 0 ? { clockSkewSeconds: rawTls.clockSkewSeconds } : {}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
27
157
|
function createDefaultLogger(prefix = "chain-node") {
|
|
28
158
|
return {
|
|
29
159
|
info: (...args) => console.info(`[${prefix}]`, ...args),
|
|
@@ -54,12 +184,55 @@ function classifyFetchKind(rawUrl) {
|
|
|
54
184
|
}
|
|
55
185
|
function assertSafeNonSymlinkDirectory(rawDataDir) {
|
|
56
186
|
const dataDir = path.resolve(rawDataDir);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (fs.lstatSync(dataDir).
|
|
187
|
+
assertNoSymlinkAncestors(dataDir, "dataDir");
|
|
188
|
+
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
|
|
189
|
+
assertNoSymlinkAncestors(dataDir, "dataDir");
|
|
190
|
+
if (!fs.lstatSync(dataDir).isDirectory()) throw new CustomError("INVALID_CONFIG", `Chain node dataDir is not a directory: ${dataDir}`);
|
|
61
191
|
return dataDir;
|
|
62
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Write a small file atomically and durably (`wx` `0o600` temp file + `fsync` + `rename` + parent dir `fsync`),
|
|
195
|
+
* refusing symlinks on both the target file and its immediate parent directory (#2026, #2036, #2039).
|
|
196
|
+
*/
|
|
197
|
+
function writeFileDurably(filePath, content) {
|
|
198
|
+
const resolved = path.resolve(filePath);
|
|
199
|
+
const parentDir = path.dirname(resolved);
|
|
200
|
+
const parentSt = fs.lstatSync(parentDir);
|
|
201
|
+
if (parentSt.isSymbolicLink() || !parentSt.isDirectory()) throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked or non-directory parent: ${parentDir}`);
|
|
202
|
+
const assertTargetNotSymlink = () => {
|
|
203
|
+
try {
|
|
204
|
+
if (fs.lstatSync(resolved).isSymbolicLink()) throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked target file: ${resolved}`);
|
|
205
|
+
} catch (err) {
|
|
206
|
+
if (err?.code !== "ENOENT") throw err;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
assertTargetNotSymlink();
|
|
210
|
+
const tmpPath = `${resolved}.tmp.${process.pid}.${crypto.randomBytes(6).toString("hex")}`;
|
|
211
|
+
const fd = fs.openSync(tmpPath, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW ?? 0), 384);
|
|
212
|
+
let closed = false;
|
|
213
|
+
try {
|
|
214
|
+
fs.writeFileSync(fd, content);
|
|
215
|
+
fs.fsyncSync(fd);
|
|
216
|
+
fs.closeSync(fd);
|
|
217
|
+
closed = true;
|
|
218
|
+
assertTargetNotSymlink();
|
|
219
|
+
fs.renameSync(tmpPath, resolved);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
if (!closed) try {
|
|
222
|
+
fs.closeSync(fd);
|
|
223
|
+
} catch {}
|
|
224
|
+
fs.rmSync(tmpPath, { force: true });
|
|
225
|
+
throw err;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const dirFd = fs.openSync(parentDir, "r");
|
|
229
|
+
try {
|
|
230
|
+
fs.fsyncSync(dirFd);
|
|
231
|
+
} finally {
|
|
232
|
+
fs.closeSync(dirFd);
|
|
233
|
+
}
|
|
234
|
+
} catch {}
|
|
235
|
+
}
|
|
63
236
|
function loadOrCreateNodeId(dataDir, explicitNodeId) {
|
|
64
237
|
const nodeIdPath = path.join(dataDir, NODE_ID_FILENAME);
|
|
65
238
|
if (fs.existsSync(nodeIdPath) && fs.lstatSync(nodeIdPath).isSymbolicLink()) throw new CustomError("SECURITY_VIOLATION", `Refusing symlinked .node-id file: ${nodeIdPath}`);
|
|
@@ -121,6 +294,10 @@ function buildFollowerNodeRoleConfig(role, resolvedSigner) {
|
|
|
121
294
|
upstreamChainUrl: role.upstreamChainUrl ?? null,
|
|
122
295
|
clusterPeerUrls: role.clusterPeerUrls ?? [],
|
|
123
296
|
snapshotBaseUrl: role.snapshotBaseUrl ?? null,
|
|
297
|
+
...role.bootstrapSource ? { bootstrapSource: role.bootstrapSource } : {},
|
|
298
|
+
...role.da ? { da: role.da } : {},
|
|
299
|
+
...role.daCrossCheck ? { daCrossCheck: role.daCrossCheck } : {},
|
|
300
|
+
...role.daCrossCheckIncomplete ? { daCrossCheckIncomplete: role.daCrossCheckIncomplete } : {},
|
|
124
301
|
trustedProposer: isHotStuffCluster ? null : hasExplicitTrustedAnchor ? role.trustedProposer ?? null : resolvedSigner?.address ?? null,
|
|
125
302
|
trustedProposerPublicKey: isHotStuffCluster ? null : hasExplicitTrustedAnchor ? role.trustedProposerPublicKey ?? null : resolvedSigner?.publicKey ?? null,
|
|
126
303
|
allowUnauthenticatedBootstrap: Boolean(role.allowUnauthenticatedBootstrap),
|
|
@@ -179,6 +356,9 @@ async function bootChainNodeOnce(params) {
|
|
|
179
356
|
if ((role.kind === "validator" || role.kind === "hotstuff") && !isCompleteChainConfig(profile.chainConfig)) throw new CustomError("INVALID_CONFIG", `${role.kind} role requires a complete chainConfig`);
|
|
180
357
|
if ((role.kind === "validator" || role.kind === "hotstuff") && !role.signer) throw new CustomError("INVALID_CONFIG", `${role.kind} role requires role.signer`);
|
|
181
358
|
if (role.kind === "hotstuff" && (!Array.isArray(role.validators) || role.validators.length === 0)) throw new CustomError("INVALID_CONFIG", "HotStuff role requires a non-empty validators list");
|
|
359
|
+
if (role.kind === "follower" && role.orphanIndexFromSequence !== void 0 && (!Number.isSafeInteger(role.orphanIndexFromSequence) || role.orphanIndexFromSequence < 1)) throw new CustomError("INVALID_CONFIG", `role.orphanIndexFromSequence must be a positive integer, got ${String(role.orphanIndexFromSequence)}`);
|
|
360
|
+
const effectiveMeshEgress = parseMeshEgressUrl(profile.meshEgressUrl ?? ("meshEgressUrl" in role ? role.meshEgressUrl : void 0), "meshEgressUrl");
|
|
361
|
+
const validatedTls = validateChainHttpTlsConfig(profile.http?.tls);
|
|
182
362
|
const logger = testHooks?.logger ?? profile.logger ?? createDefaultLogger("chain-node");
|
|
183
363
|
const variadicLogger = toVariadicLogger(logger);
|
|
184
364
|
const followerExpectedChainId = "expectedChainId" in profile ? profile.expectedChainId : void 0;
|
|
@@ -226,6 +406,7 @@ async function bootChainNodeOnce(params) {
|
|
|
226
406
|
let ledgerCleanupIndex = -1;
|
|
227
407
|
let followerPrep = null;
|
|
228
408
|
const isKeylessFollower = role.kind === "follower" && !profile.storage?.trustedStorageSecretKey?.trim();
|
|
409
|
+
let followerFetchGuard;
|
|
229
410
|
if (role.kind === "follower") {
|
|
230
411
|
if (fs.existsSync(ledgerPath) && fs.statSync(ledgerPath).size > 0) {
|
|
231
412
|
const precheckLedgerStorage = createLedgerStorage();
|
|
@@ -244,13 +425,17 @@ async function bootChainNodeOnce(params) {
|
|
|
244
425
|
ledgerCleanupIndex = -1;
|
|
245
426
|
}
|
|
246
427
|
}
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
428
|
+
const staticTrustedUrls = [
|
|
429
|
+
role.upstreamChainUrl,
|
|
430
|
+
role.snapshotBaseUrl,
|
|
431
|
+
role.da?.baseUrl,
|
|
432
|
+
role.daCrossCheck?.baseUrl,
|
|
433
|
+
...role.clusterPeerUrls ?? []
|
|
434
|
+
].filter((u) => Boolean(u && String(u).trim()));
|
|
435
|
+
const guardedBootstrapFetch = createChainNodePeerFetchGuard({
|
|
436
|
+
allowPrivatePeers: role.allowPrivatePeers,
|
|
437
|
+
meshEgress: effectiveMeshEgress,
|
|
438
|
+
trustedStaticUrls: staticTrustedUrls,
|
|
254
439
|
baseFetch: globalThis.fetch
|
|
255
440
|
});
|
|
256
441
|
const interceptFetch = testHooks?.followerSync?.interceptUpstreamFetch;
|
|
@@ -262,6 +447,7 @@ async function bootChainNodeOnce(params) {
|
|
|
262
447
|
init
|
|
263
448
|
}, guardedBootstrapFetch);
|
|
264
449
|
}) : guardedBootstrapFetch;
|
|
450
|
+
followerFetchGuard = bootstrapFetchImpl;
|
|
265
451
|
const explicitExpectedChainId = followerExpectedChainId?.trim() || "";
|
|
266
452
|
const profileChainId = profile.chainConfig?.chainId?.trim() || "";
|
|
267
453
|
if (explicitExpectedChainId && profileChainId && explicitExpectedChainId !== profileChainId) throw new CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Follower expectedChainId (${explicitExpectedChainId}) !== profile.chainConfig.chainId (${profileChainId})`);
|
|
@@ -274,7 +460,8 @@ async function bootChainNodeOnce(params) {
|
|
|
274
460
|
ledgerPath,
|
|
275
461
|
trustedPath,
|
|
276
462
|
keyless: isKeylessFollower,
|
|
277
|
-
fetchImpl: bootstrapFetchImpl
|
|
463
|
+
fetchImpl: bootstrapFetchImpl,
|
|
464
|
+
logger: { warn: (...args) => variadicLogger.warn(...args) }
|
|
278
465
|
});
|
|
279
466
|
}
|
|
280
467
|
if (!ledgerStorage) {
|
|
@@ -341,7 +528,7 @@ async function bootChainNodeOnce(params) {
|
|
|
341
528
|
const storedChainId = typeof storedChainPartial.chainId === "string" ? storedChainPartial.chainId.trim() : "";
|
|
342
529
|
const overrideChainId = typeof profile.chainConfig?.chainId === "string" ? profile.chainConfig.chainId.trim() : "";
|
|
343
530
|
if (storedChainId && overrideChainId && overrideChainId !== storedChainId) throw new CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Follower profile.chainConfig.chainId (${overrideChainId}) !== statedb chain.chainId (${storedChainId})`);
|
|
344
|
-
chainConfig = rehydrateFollowerChainConfig(storedChain, profile.chainConfig);
|
|
531
|
+
chainConfig = rehydrateFollowerChainConfig(storedChain, profile.chainConfig, profile.reservedSymbols);
|
|
345
532
|
} else {
|
|
346
533
|
const baseConfig = profile.chainConfig;
|
|
347
534
|
if (role.kind === "follower") {
|
|
@@ -352,15 +539,32 @@ async function bootChainNodeOnce(params) {
|
|
|
352
539
|
chainConfig = baseConfig;
|
|
353
540
|
}
|
|
354
541
|
if (role.kind === "follower" && followerExpectedChainId && chainConfig.chainId !== followerExpectedChainId) throw new CustomError("SNAPSHOT_CHAIN_ID_MISMATCH", `Rehydrated Follower chainId (${chainConfig.chainId}) !== expectedChainId (${followerExpectedChainId})`);
|
|
542
|
+
const orphanIndexStart = role.kind === "follower" ? orphanIndexFromSequence(chainConfig.chainId, role.orphanIndexFromSequence) : 1;
|
|
355
543
|
const hotstuffValidators = role.kind === "hotstuff" ? role.validators : role.kind === "follower" ? role.hotstuffValidators ?? [] : [];
|
|
356
544
|
const hotstuffQuorumThreshold = role.kind === "hotstuff" ? role.quorumThreshold : role.kind === "follower" ? role.hotstuffQuorumThreshold : void 0;
|
|
357
545
|
const hotstuffClusterConfig = role.kind === "hotstuff" || hotstuffValidators.length > 0 ? toHotStuffClusterConfig(chainConfig.chainId, hotstuffValidators, hotstuffQuorumThreshold) : void 0;
|
|
358
|
-
const
|
|
546
|
+
const daModeFromEnv = profile.da?.segmentPublisher === void 0 && role.kind !== "follower" && Boolean(profile.da?.env) && isDaArchiveMode(profile.da.env);
|
|
547
|
+
const daPublisherInputs = daModeFromEnv && role.kind === "validator" && resolvedProposer ? daPublisherInputsFromEnv({
|
|
359
548
|
env: profile.da.env,
|
|
549
|
+
chainId: chainConfig.chainId,
|
|
550
|
+
wallet: resolvedProposer.proposerWallet,
|
|
551
|
+
retentionMode: role.retentionMode ?? "archive",
|
|
552
|
+
resumeStore: createDaResumeFileStore(path.join(dataDir, "da-publisher-resume.json")),
|
|
360
553
|
logger: variadicLogger
|
|
554
|
+
}) : {};
|
|
555
|
+
const segmentPublisher = profile.da?.segmentPublisher !== void 0 ? profile.da.segmentPublisher : role.kind !== "follower" && profile.da?.env ? createEnvSegmentPublisher({
|
|
556
|
+
env: profile.da.env,
|
|
557
|
+
logger: variadicLogger,
|
|
558
|
+
...daPublisherInputs,
|
|
559
|
+
...profile.da.fetchImpl ? { fetchImpl: profile.da.fetchImpl } : {}
|
|
361
560
|
}) : null;
|
|
362
561
|
const explicitColdReader = profile.da?.coldArchiveReader ?? null;
|
|
363
|
-
const
|
|
562
|
+
const followsDaArchive = role.kind === "follower" && role.bootstrapSource === "da";
|
|
563
|
+
const envColdReader = !explicitColdReader && profile.da?.env && !daModeFromEnv && !followsDaArchive ? createEnvColdArchiveReader({
|
|
564
|
+
env: profile.da.env,
|
|
565
|
+
logger: variadicLogger
|
|
566
|
+
}) : null;
|
|
567
|
+
scrubDaCredentials(profile.da?.env);
|
|
364
568
|
const coldSegmentStore = explicitColdReader ? adaptColdReaderToArchiveStore(explicitColdReader) : segmentPublisher?.getArchiveStore?.() || (envColdReader ? adaptColdReaderToArchiveStore(envColdReader) : void 0);
|
|
365
569
|
const hasExplicitTrustedAnchor = role.kind === "follower" && (role.trustedProposer != null || role.trustedProposerPublicKey != null);
|
|
366
570
|
const followerExplicitPk = role.kind === "follower" ? followerPrep?.trustedProposerPublicKey || role.trustedProposerPublicKey || null : null;
|
|
@@ -520,6 +724,9 @@ async function bootChainNodeOnce(params) {
|
|
|
520
724
|
});
|
|
521
725
|
}
|
|
522
726
|
const indexDbExisted = fs.existsSync(indexdbPath);
|
|
727
|
+
const indexBuildMarkerPath = `${indexdbPath}.build-pending`;
|
|
728
|
+
let followerIndexBuildNeeded = role.kind === "follower" && (Boolean(followerPrep?.bootstrapped) || !indexDbExisted || fs.existsSync(indexBuildMarkerPath));
|
|
729
|
+
if (followerIndexBuildNeeded) writeFileDurably(indexBuildMarkerPath, `${(/* @__PURE__ */ new Date()).toISOString()}\n`);
|
|
523
730
|
const indexdb = new SqliteIndexDB({
|
|
524
731
|
filename: indexdbPath,
|
|
525
732
|
...profile.storage?.indexWalAutoCheckpoint !== void 0 ? { walAutoCheckpoint: profile.storage.indexWalAutoCheckpoint } : {}
|
|
@@ -529,8 +736,16 @@ async function bootChainNodeOnce(params) {
|
|
|
529
736
|
});
|
|
530
737
|
await indexdb.initialize();
|
|
531
738
|
await waitEventEmitterReady(indexdb);
|
|
739
|
+
if (role.kind === "follower" && !followerIndexBuildNeeded) {
|
|
740
|
+
if (await indexdb.tx.count() === 0 && await ledger.getLatestSequence() > 0) {
|
|
741
|
+
logger.warn?.("follower IndexDB has no transactions while the ledger has entries; rebuilding it", { indexdbPath });
|
|
742
|
+
followerIndexBuildNeeded = true;
|
|
743
|
+
writeFileDurably(indexBuildMarkerPath, `${(/* @__PURE__ */ new Date()).toISOString()}\n`);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
532
746
|
let resolverRef = null;
|
|
533
|
-
const
|
|
747
|
+
const explicitAdminEnv = profile.http.adminEnv;
|
|
748
|
+
const adminEnv = explicitAdminEnv ?? {};
|
|
534
749
|
const preferencesManager = new NodePreferencesManager({
|
|
535
750
|
dataDir,
|
|
536
751
|
env: adminEnv,
|
|
@@ -590,7 +805,7 @@ async function bootChainNodeOnce(params) {
|
|
|
590
805
|
mode: profile.configCheck?.mode ?? "off",
|
|
591
806
|
localConfig: resolver.config,
|
|
592
807
|
upstreamUrl: role.upstreamChainUrl ?? role.clusterPeerUrls?.[0] ?? null,
|
|
593
|
-
fetchUpstreamConfig: profile.configCheck?.fetchUpstreamConfig,
|
|
808
|
+
fetchUpstreamConfig: profile.configCheck?.fetchUpstreamConfig ?? ((u) => fetchUpstreamChainConfig(u, followerFetchGuard ? { fetchImpl: followerFetchGuard } : void 0)),
|
|
594
809
|
hasLocalState: !followerPrep?.bootstrapped && await ledger.getLatestSequence() > 0,
|
|
595
810
|
...profile.configCheck?.retryDeadlineMs !== void 0 ? { retryDeadlineMs: profile.configCheck.retryDeadlineMs } : {},
|
|
596
811
|
logger
|
|
@@ -609,7 +824,24 @@ async function bootChainNodeOnce(params) {
|
|
|
609
824
|
trustedProposerPublicKey: followerPrep?.trustedProposerPublicKey || role.trustedProposerPublicKey || void 0,
|
|
610
825
|
allowUnauthenticatedLeaderFailover: Boolean(role.allowUnauthenticatedBootstrap)
|
|
611
826
|
}) : void 0;
|
|
612
|
-
const
|
|
827
|
+
const guardedFollowerRuntimeFetch = createChainNodePeerFetchGuard({
|
|
828
|
+
allowPrivatePeers: role.allowPrivatePeers,
|
|
829
|
+
meshEgress: effectiveMeshEgress,
|
|
830
|
+
trustedStaticUrls: [
|
|
831
|
+
role.upstreamChainUrl,
|
|
832
|
+
role.snapshotBaseUrl,
|
|
833
|
+
role.da?.baseUrl,
|
|
834
|
+
...mergedPeerUrls,
|
|
835
|
+
...role.witnessSubmitUrls ?? []
|
|
836
|
+
],
|
|
837
|
+
baseFetch: globalThis.fetch
|
|
838
|
+
});
|
|
839
|
+
const coldReaderForFollower = (role.bootstrapSource === "da" && role.da ? createDaFollowerSource({
|
|
840
|
+
da: role.da,
|
|
841
|
+
proposer: role.trustedProposer ?? "",
|
|
842
|
+
proposerPublicKey: role.trustedProposerPublicKey ?? "",
|
|
843
|
+
fetchImpl: guardedFollowerRuntimeFetch
|
|
844
|
+
}) : null)?.reader ?? explicitColdReader ?? envColdReader ?? void 0;
|
|
613
845
|
followerSync = new FollowerSyncService({
|
|
614
846
|
resolver,
|
|
615
847
|
role: role.runtimeRole ?? "follower",
|
|
@@ -635,6 +867,7 @@ async function bootChainNodeOnce(params) {
|
|
|
635
867
|
maxUpstreamStaleMs: role.maxUpstreamStaleMs,
|
|
636
868
|
startPaused: testHooks?.followerSync?.startPaused,
|
|
637
869
|
artificialPollDelayMs: testHooks?.followerSync?.artificialPollDelayMs,
|
|
870
|
+
fetchImpl: guardedFollowerRuntimeFetch,
|
|
638
871
|
interceptUpstreamFetch: testHooks?.followerSync?.interceptUpstreamFetch,
|
|
639
872
|
...profile.replayGuards?.futureTimestamp ? { futureTimestampGuard: {
|
|
640
873
|
...profile.replayGuards.futureTimestamp,
|
|
@@ -652,12 +885,40 @@ async function bootChainNodeOnce(params) {
|
|
|
652
885
|
triggerSyncOnce: () => syncRef.triggerSyncOnce()
|
|
653
886
|
});
|
|
654
887
|
}
|
|
655
|
-
if (
|
|
656
|
-
|
|
657
|
-
|
|
888
|
+
if (followerIndexBuildNeeded) {
|
|
889
|
+
const buildStart = Date.now();
|
|
890
|
+
logger.info?.("follower initial index build started", {
|
|
891
|
+
indexdbPath,
|
|
892
|
+
resumed: indexDbExisted
|
|
893
|
+
});
|
|
894
|
+
const built = await resolver.rebuildIndex();
|
|
895
|
+
const orphans = await followerSync.replayer.indexExistingOrphanEntries(orphanIndexStart);
|
|
896
|
+
fs.rmSync(indexBuildMarkerPath, { force: true });
|
|
897
|
+
logger.info?.("follower initial index build finished", {
|
|
898
|
+
tables: built?.tables,
|
|
899
|
+
orphanEntries: orphans,
|
|
900
|
+
durationMs: Date.now() - buildStart
|
|
901
|
+
});
|
|
658
902
|
}
|
|
659
903
|
} else if (role.kind === "hotstuff") {
|
|
660
904
|
const hotstuffStateStore = new SqliteHotStuffStateStore(ledgerStorage);
|
|
905
|
+
const guardedHotStuffFetch = createChainNodePeerFetchGuard({
|
|
906
|
+
allowPrivatePeers: role.allowPrivatePeers,
|
|
907
|
+
meshEgress: effectiveMeshEgress,
|
|
908
|
+
trustedStaticUrls: role.validators.flatMap((v) => [v.rpcUrl, v.consensusUrl]),
|
|
909
|
+
baseFetch: globalThis.fetch
|
|
910
|
+
});
|
|
911
|
+
const selfValidatorDid = resolvedProposer.address.trim();
|
|
912
|
+
const allHttpPeersMatchMeshEgress = effectiveMeshEgress !== null && role.validators.every((v) => {
|
|
913
|
+
if (v.did.trim() === selfValidatorDid) return true;
|
|
914
|
+
try {
|
|
915
|
+
const parsedConsensus = new URL(v.consensusUrl);
|
|
916
|
+
if (parsedConsensus.protocol === "https:") return true;
|
|
917
|
+
return parsedConsensus.origin.toLowerCase() === effectiveMeshEgress.origin;
|
|
918
|
+
} catch {
|
|
919
|
+
return false;
|
|
920
|
+
}
|
|
921
|
+
});
|
|
661
922
|
hotstuffRuntime = new HotStuffNodeRuntime({
|
|
662
923
|
resolver,
|
|
663
924
|
validatorDid: resolvedProposer.address,
|
|
@@ -668,9 +929,10 @@ async function bootChainNodeOnce(params) {
|
|
|
668
929
|
consensusBindHost: role.consensusBindHost,
|
|
669
930
|
consensusPort: role.consensusPort,
|
|
670
931
|
allowPublicBind: Boolean(role.allowPublicConsensusBind),
|
|
671
|
-
allowPrivateHttpPeers: Boolean(role.allowPrivatePeers),
|
|
932
|
+
allowPrivateHttpPeers: Boolean(role.allowPrivatePeers || allHttpPeersMatchMeshEgress),
|
|
672
933
|
writePolicy: role.writePolicy ?? "forward",
|
|
673
934
|
stateStore: hotstuffStateStore,
|
|
935
|
+
fetchImpl: guardedHotStuffFetch,
|
|
674
936
|
onOutboundConsensusRequest: testHooks?.hotstuff?.onOutboundConsensusRequest ?? testHooks?.hotstuff?.interceptOutboundRpc
|
|
675
937
|
});
|
|
676
938
|
resolver.setNodeRuntime(hotstuffRuntime);
|
|
@@ -724,7 +986,7 @@ async function bootChainNodeOnce(params) {
|
|
|
724
986
|
res.once("close", markDone);
|
|
725
987
|
next();
|
|
726
988
|
});
|
|
727
|
-
if (profile.http.enableAccessLog) app.use(createAccessLogMiddleware(logger));
|
|
989
|
+
if (profile.http.enableAccessLog) app.use(createAccessLogMiddleware(logger, explicitAdminEnv ? { env: explicitAdminEnv } : void 0));
|
|
728
990
|
app.head("/", (_req, res) => {
|
|
729
991
|
res.status(200).send("OK");
|
|
730
992
|
});
|
|
@@ -767,13 +1029,18 @@ async function bootChainNodeOnce(params) {
|
|
|
767
1029
|
const server = createSocketHandler({
|
|
768
1030
|
indexdb,
|
|
769
1031
|
app,
|
|
770
|
-
wsOptions: {
|
|
1032
|
+
wsOptions: {
|
|
1033
|
+
broadcastEventName: `@arcblock/ws:broadcast:${nodeId}:${crypto.randomUUID()}`,
|
|
1034
|
+
...explicitAdminEnv ? { env: explicitAdminEnv } : {}
|
|
1035
|
+
}
|
|
771
1036
|
});
|
|
772
1037
|
pushCleanup(async () => {
|
|
773
1038
|
isShuttingDown = true;
|
|
774
|
-
|
|
1039
|
+
server.closeAllConnections?.();
|
|
1040
|
+
if (server.listening) await new Promise((resolveClose) => {
|
|
775
1041
|
server.close(() => resolveClose());
|
|
776
1042
|
});
|
|
1043
|
+
else server.close();
|
|
777
1044
|
});
|
|
778
1045
|
app.use("/api", createAPIHandler({
|
|
779
1046
|
resolver,
|
|
@@ -802,6 +1069,7 @@ async function bootChainNodeOnce(params) {
|
|
|
802
1069
|
});
|
|
803
1070
|
app.use("/", createHttpHandler({
|
|
804
1071
|
resolver,
|
|
1072
|
+
...explicitAdminEnv ? { env: explicitAdminEnv } : {},
|
|
805
1073
|
onError: profile.http.onGraphqlError ? (err) => {
|
|
806
1074
|
profile.http.onGraphqlError(err);
|
|
807
1075
|
} : () => {}
|
|
@@ -809,24 +1077,513 @@ async function bootChainNodeOnce(params) {
|
|
|
809
1077
|
const bindHost = profile.http.host?.trim() || "127.0.0.1";
|
|
810
1078
|
const requestedPort = profile.http.port;
|
|
811
1079
|
if (!Number.isSafeInteger(requestedPort) || requestedPort < 0 || requestedPort > 65535) throw new CustomError("INVALID_CONFIG", `Invalid http.port="${String(requestedPort)}": expected 0..65535`);
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
1080
|
+
let tlsServer;
|
|
1081
|
+
let publicTcpServer;
|
|
1082
|
+
const retiredTlsServers = /* @__PURE__ */ new Set();
|
|
1083
|
+
const activeTlsSockets = /* @__PURE__ */ new Set();
|
|
1084
|
+
const activeRawSockets = /* @__PURE__ */ new Set();
|
|
1085
|
+
const bridgeSockets = /* @__PURE__ */ new Set();
|
|
1086
|
+
const handshakeTimers = /* @__PURE__ */ new Map();
|
|
1087
|
+
const admittedBridgeByClientPort = /* @__PURE__ */ new Map();
|
|
1088
|
+
let reloadTlsCredentials = async () => ({
|
|
1089
|
+
reloaded: false,
|
|
1090
|
+
subjectDid: null,
|
|
1091
|
+
spkiSha256Hex: null
|
|
1092
|
+
});
|
|
1093
|
+
const closeAllTlsNetworking = async () => {
|
|
1094
|
+
for (const timer of handshakeTimers.values()) clearTimeout(timer);
|
|
1095
|
+
handshakeTimers.clear();
|
|
1096
|
+
for (const bSock of Array.from(bridgeSockets)) bSock.destroy();
|
|
1097
|
+
bridgeSockets.clear();
|
|
1098
|
+
admittedBridgeByClientPort.clear();
|
|
1099
|
+
for (const sock of Array.from(activeTlsSockets)) sock.destroy();
|
|
1100
|
+
activeTlsSockets.clear();
|
|
1101
|
+
for (const rawSock of Array.from(activeRawSockets)) rawSock.destroy();
|
|
1102
|
+
activeRawSockets.clear();
|
|
1103
|
+
const closeTasks = [];
|
|
1104
|
+
if (publicTcpServer?.listening) {
|
|
1105
|
+
const tcpSrv = publicTcpServer;
|
|
1106
|
+
closeTasks.push(new Promise((r) => tcpSrv.close(() => r())));
|
|
1107
|
+
}
|
|
1108
|
+
for (const oldTls of Array.from(retiredTlsServers)) if (oldTls.listening) closeTasks.push(new Promise((r) => oldTls.close(() => r())));
|
|
1109
|
+
retiredTlsServers.clear();
|
|
1110
|
+
if (tlsServer?.listening) {
|
|
1111
|
+
const activeSrv = tlsServer;
|
|
1112
|
+
closeTasks.push(new Promise((r) => activeSrv.close(() => r())));
|
|
1113
|
+
}
|
|
1114
|
+
server.closeAllConnections?.();
|
|
1115
|
+
if (server.listening) closeTasks.push(new Promise((r) => server.close(() => r())));
|
|
1116
|
+
else server.close();
|
|
1117
|
+
await Promise.all(closeTasks);
|
|
1118
|
+
};
|
|
1119
|
+
const loadOrSynthesizeTlsMaterial = () => {
|
|
1120
|
+
if (!validatedTls) throw new CustomError("INVALID_STATE", "TLS is not enabled on this chain node");
|
|
1121
|
+
if (validatedTls.certFile && validatedTls.keyFile) {
|
|
1122
|
+
const certPem = readNonSymlinkRegularFileUtf8(validatedTls.certFile, "http.tls.certFile");
|
|
1123
|
+
const keyPem = readNonSymlinkRegularFileUtf8(validatedTls.keyFile, "http.tls.keyFile");
|
|
1124
|
+
let subjectDid = null;
|
|
1125
|
+
let spkiSha256Hex = null;
|
|
1126
|
+
try {
|
|
1127
|
+
const parsed = parseDidTlsCertificate(certPem);
|
|
1128
|
+
subjectDid = parsed.subjectDid;
|
|
1129
|
+
spkiSha256Hex = didTlsBytesToHex(new Uint8Array(crypto.createHash("sha256").update(parsed.spkiDer).digest()));
|
|
1130
|
+
} catch {
|
|
1131
|
+
try {
|
|
1132
|
+
const spkiDer = new crypto.X509Certificate(certPem).publicKey.export({
|
|
1133
|
+
format: "der",
|
|
1134
|
+
type: "spki"
|
|
1135
|
+
});
|
|
1136
|
+
spkiSha256Hex = crypto.createHash("sha256").update(new Uint8Array(spkiDer)).digest("hex");
|
|
1137
|
+
} catch {}
|
|
1138
|
+
}
|
|
1139
|
+
return {
|
|
1140
|
+
certPem,
|
|
1141
|
+
keyPem,
|
|
1142
|
+
subjectDid,
|
|
1143
|
+
spkiSha256Hex
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
const proposerEd25519Sk = resolvedProposer?.signatureKeyType === "ed25519" ? resolvedProposer.secretKey : void 0;
|
|
1147
|
+
const rawSeed = validatedTls.identitySecretKey || proposerEd25519Sk || profile.storage?.trustedStorageSecretKey?.trim() || trustedSecretKey;
|
|
1148
|
+
if (!rawSeed) throw new CustomError("INVALID_CONFIG", "DID-TLS server certificate synthesis requires an Ed25519 secret key (CHAIN_SK / signer.secretKey / http.tls.identitySecretKey)");
|
|
1149
|
+
const issued = issueDidTlsCertificate({
|
|
1150
|
+
subjectSeed: rawSeed,
|
|
1151
|
+
subjectDid: validatedTls.subjectDid ?? (proposerEd25519Sk && rawSeed === proposerEd25519Sk ? canonicalizeTlsDid(resolvedProposer.address) : void 0),
|
|
1152
|
+
wireAlgorithm: validatedTls.wireAlgorithm,
|
|
1153
|
+
role: "server",
|
|
1154
|
+
dnsNames: validatedTls.sanDnsNames,
|
|
1155
|
+
ipAddresses: validatedTls.sanIpAddresses,
|
|
1156
|
+
notBefore: new Date(clockNow()),
|
|
1157
|
+
...validatedTls.validityDays !== void 0 ? { validityDays: validatedTls.validityDays } : {}
|
|
1158
|
+
});
|
|
1159
|
+
return {
|
|
1160
|
+
certPem: issued.certificatePem,
|
|
1161
|
+
keyPem: issued.privateKeyPem,
|
|
1162
|
+
subjectDid: issued.subjectDid,
|
|
1163
|
+
spkiSha256Hex: issued.spkiSha256Hex
|
|
816
1164
|
};
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1165
|
+
};
|
|
1166
|
+
let boundPort;
|
|
1167
|
+
if (validatedTls) {
|
|
1168
|
+
const MAX_PRE_BRIDGE_BUFFER_BYTES = 65536;
|
|
1169
|
+
const GATED_HTTP_DISPATCH_EVENTS = new Set([
|
|
1170
|
+
"request",
|
|
1171
|
+
"upgrade",
|
|
1172
|
+
"checkContinue",
|
|
1173
|
+
"checkExpectation",
|
|
1174
|
+
"connect",
|
|
1175
|
+
"dropRequest"
|
|
1176
|
+
]);
|
|
1177
|
+
const initialMaterial = loadOrSynthesizeTlsMaterial();
|
|
1178
|
+
const shouldRequestClientCert = validatedTls.requireClientCert || validatedTls.hasConfiguredClientTrustPins;
|
|
1179
|
+
const needsLoopbackHttpBridge = server.listenerCount("connection") === 0;
|
|
1180
|
+
const needsAtomicTlsSwap = needsLoopbackHttpBridge || Boolean(process.versions.bun);
|
|
1181
|
+
let internalHttpPort = 0;
|
|
1182
|
+
let internalTlsPort = 0;
|
|
1183
|
+
if (needsLoopbackHttpBridge) {
|
|
1184
|
+
const isLoopbackRemoteAddress = (addr) => addr === "127.0.0.1" || addr === "::ffff:127.0.0.1";
|
|
1185
|
+
const verifyAndBindServerSocket = (srvSock) => {
|
|
1186
|
+
if (!srvSock || !isLoopbackRemoteAddress(srvSock.remoteAddress)) return;
|
|
1187
|
+
const remotePort = srvSock.remotePort;
|
|
1188
|
+
if (typeof remotePort !== "number" || remotePort <= 0) return;
|
|
1189
|
+
const entry = admittedBridgeByClientPort.get(remotePort);
|
|
1190
|
+
if (!entry || entry.bridgeSock.destroyed) return;
|
|
1191
|
+
if (entry.boundServerSocket && entry.boundServerSocket !== srvSock) return;
|
|
1192
|
+
if (!entry.boundServerSocket) {
|
|
1193
|
+
entry.boundServerSocket = srvSock;
|
|
1194
|
+
const revokePortEntry = () => {
|
|
1195
|
+
if (admittedBridgeByClientPort.get(remotePort) === entry) admittedBridgeByClientPort.delete(remotePort);
|
|
1196
|
+
};
|
|
1197
|
+
srvSock.once("close", revokePortEntry);
|
|
1198
|
+
const origDestroy = srvSock.destroy.bind(srvSock);
|
|
1199
|
+
srvSock.destroy = ((error) => {
|
|
1200
|
+
revokePortEntry();
|
|
1201
|
+
return origDestroy(error);
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
return entry;
|
|
1205
|
+
};
|
|
1206
|
+
const origEmit = server.emit.bind(server);
|
|
1207
|
+
server.emit = ((event, ...args) => {
|
|
1208
|
+
if (event === "connection") {
|
|
1209
|
+
const srvSock = args[0];
|
|
1210
|
+
if (!verifyAndBindServerSocket(srvSock)) {
|
|
1211
|
+
srvSock?.destroy();
|
|
1212
|
+
return false;
|
|
1213
|
+
}
|
|
1214
|
+
return origEmit(event, ...args);
|
|
1215
|
+
}
|
|
1216
|
+
if (GATED_HTTP_DISPATCH_EVENTS.has(event)) {
|
|
1217
|
+
const req = args[0];
|
|
1218
|
+
const extraSock = event === "upgrade" || event === "connect" || event === "dropRequest" ? args[1] : void 0;
|
|
1219
|
+
const reqSock = req?.socket ?? extraSock;
|
|
1220
|
+
const entry = verifyAndBindServerSocket(reqSock);
|
|
1221
|
+
if (!entry) {
|
|
1222
|
+
extraSock?.destroy();
|
|
1223
|
+
req?.socket?.destroy();
|
|
1224
|
+
return false;
|
|
1225
|
+
}
|
|
1226
|
+
if (entry.peer && reqSock) reqSock.didTlsPeer = entry.peer;
|
|
1227
|
+
}
|
|
1228
|
+
return origEmit(event, ...args);
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
const registerRawSocketTimer = (rawSocket) => {
|
|
1232
|
+
if (isShuttingDown) {
|
|
1233
|
+
rawSocket.destroy();
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
activeRawSockets.add(rawSocket);
|
|
1237
|
+
const timer = setTimeout(() => {
|
|
1238
|
+
handshakeTimers.delete(rawSocket);
|
|
1239
|
+
rawSocket.destroy();
|
|
1240
|
+
}, validatedTls.handshakeTimeoutMs);
|
|
1241
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
1242
|
+
handshakeTimers.set(rawSocket, timer);
|
|
1243
|
+
rawSocket.once("close", () => {
|
|
1244
|
+
const pendingTimer = handshakeTimers.get(rawSocket);
|
|
1245
|
+
if (pendingTimer) {
|
|
1246
|
+
clearTimeout(pendingTimer);
|
|
1247
|
+
handshakeTimers.delete(rawSocket);
|
|
1248
|
+
}
|
|
1249
|
+
activeRawSockets.delete(rawSocket);
|
|
1250
|
+
});
|
|
1251
|
+
rawSocket.on("error", () => {
|
|
1252
|
+
rawSocket.destroy();
|
|
1253
|
+
});
|
|
820
1254
|
};
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
1255
|
+
const clearRawSocketTimer = (rawSocket) => {
|
|
1256
|
+
if (!rawSocket) return;
|
|
1257
|
+
const pendingTimer = handshakeTimers.get(rawSocket);
|
|
1258
|
+
if (pendingTimer) {
|
|
1259
|
+
clearTimeout(pendingTimer);
|
|
1260
|
+
handshakeTimers.delete(rawSocket);
|
|
1261
|
+
}
|
|
1262
|
+
};
|
|
1263
|
+
const createConfiguredTlsServer = (certPem, keyPem) => {
|
|
1264
|
+
const srv = tls.createServer({
|
|
1265
|
+
...buildChainTlsSecureContextOptions(certPem, keyPem),
|
|
1266
|
+
requestCert: shouldRequestClientCert,
|
|
1267
|
+
rejectUnauthorized: false,
|
|
1268
|
+
handshakeTimeout: validatedTls.handshakeTimeoutMs
|
|
1269
|
+
});
|
|
1270
|
+
srv.on("connection", (rawSocket) => {
|
|
1271
|
+
registerRawSocketTimer(rawSocket);
|
|
1272
|
+
});
|
|
1273
|
+
srv.on("secureConnection", (tlsSocket) => {
|
|
1274
|
+
tlsSocket.setTimeout(0);
|
|
1275
|
+
const parentRaw = tlsSocket._parent;
|
|
1276
|
+
if (parentRaw) clearRawSocketTimer(parentRaw);
|
|
1277
|
+
else for (const [sock, pendingTimer] of handshakeTimers.entries()) if (sock.remotePort === tlsSocket.remotePort && sock.remoteAddress === tlsSocket.remoteAddress) {
|
|
1278
|
+
clearTimeout(pendingTimer);
|
|
1279
|
+
handshakeTimers.delete(sock);
|
|
1280
|
+
break;
|
|
1281
|
+
}
|
|
1282
|
+
if (isShuttingDown) {
|
|
1283
|
+
tlsSocket.destroy();
|
|
1284
|
+
return;
|
|
1285
|
+
}
|
|
1286
|
+
const pendingChunks = [];
|
|
1287
|
+
let pendingBytes = 0;
|
|
1288
|
+
let liveBridgeSock;
|
|
1289
|
+
if (needsLoopbackHttpBridge) tlsSocket.on("data", (chunk) => {
|
|
1290
|
+
const bytes = new Uint8Array(chunk);
|
|
1291
|
+
if (liveBridgeSock) {
|
|
1292
|
+
if (!liveBridgeSock.destroyed) liveBridgeSock.write(bytes);
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
pendingBytes += bytes.byteLength;
|
|
1296
|
+
if (pendingBytes > MAX_PRE_BRIDGE_BUFFER_BYTES) {
|
|
1297
|
+
pendingChunks.length = 0;
|
|
1298
|
+
tlsSocket.destroy();
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
pendingChunks.push(bytes);
|
|
1302
|
+
});
|
|
1303
|
+
else tlsSocket.pause();
|
|
1304
|
+
if (tlsSocket.getProtocol() !== "TLSv1.3" || tlsSocket.isSessionReused() === true) {
|
|
1305
|
+
pendingChunks.length = 0;
|
|
1306
|
+
logger.warn?.("[did-tls] rejected non-TLSv1.3 or resumed PSK/session-ticket connection", {
|
|
1307
|
+
protocol: tlsSocket.getProtocol(),
|
|
1308
|
+
sessionReused: tlsSocket.isSessionReused(),
|
|
1309
|
+
remoteAddress: tlsSocket.remoteAddress
|
|
1310
|
+
});
|
|
1311
|
+
tlsSocket.destroy();
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
let verifiedPeer;
|
|
1315
|
+
const peerCert = tlsSocket.getPeerCertificate(true);
|
|
1316
|
+
const peerPresentedCert = Boolean(peerCert && Object.keys(peerCert).length > 0);
|
|
1317
|
+
if (validatedTls.requireClientCert || peerPresentedCert) {
|
|
1318
|
+
const rawDer = peerCert && peerCert.raw instanceof Uint8Array && peerCert.raw.byteLength > 0 ? new Uint8Array(peerCert.raw.buffer, peerCert.raw.byteOffset, peerCert.raw.byteLength) : null;
|
|
1319
|
+
if (!rawDer) {
|
|
1320
|
+
pendingChunks.length = 0;
|
|
1321
|
+
tlsSocket.destroy();
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
const verdict = verifyDidTlsPeerCertificate(rawDer, {
|
|
1325
|
+
...validatedTls.basePeerPolicy,
|
|
1326
|
+
now: clockNow()
|
|
1327
|
+
});
|
|
1328
|
+
if (!verdict.ok) {
|
|
1329
|
+
pendingChunks.length = 0;
|
|
1330
|
+
logger.warn?.("[did-tls] rejected unauthenticated TLS peer before HTTP/WS dispatch", {
|
|
1331
|
+
reason: verdict.reason,
|
|
1332
|
+
detail: verdict.detail,
|
|
1333
|
+
remoteAddress: tlsSocket.remoteAddress
|
|
1334
|
+
});
|
|
1335
|
+
tlsSocket.destroy();
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
verifiedPeer = verdict;
|
|
1339
|
+
tlsSocket.didTlsPeer = verdict;
|
|
1340
|
+
}
|
|
1341
|
+
activeTlsSockets.add(tlsSocket);
|
|
1342
|
+
tlsSocket.once("close", () => {
|
|
1343
|
+
activeTlsSockets.delete(tlsSocket);
|
|
1344
|
+
});
|
|
1345
|
+
tlsSocket.on("error", () => {
|
|
1346
|
+
tlsSocket.destroy();
|
|
1347
|
+
});
|
|
1348
|
+
if (!needsLoopbackHttpBridge) {
|
|
1349
|
+
server.emit("connection", tlsSocket);
|
|
1350
|
+
tlsSocket.resume();
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
if (internalHttpPort <= 0) {
|
|
1354
|
+
pendingChunks.length = 0;
|
|
1355
|
+
tlsSocket.destroy();
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
const bridgeSock = net.connect({
|
|
1359
|
+
host: "127.0.0.1",
|
|
1360
|
+
port: internalHttpPort
|
|
1361
|
+
});
|
|
1362
|
+
bridgeSockets.add(bridgeSock);
|
|
1363
|
+
let registeredPort;
|
|
1364
|
+
const entryObj = {
|
|
1365
|
+
bridgeSock,
|
|
1366
|
+
peer: verifiedPeer
|
|
1367
|
+
};
|
|
1368
|
+
const cleanupBridge = () => {
|
|
1369
|
+
bridgeSockets.delete(bridgeSock);
|
|
1370
|
+
if (registeredPort !== void 0) {
|
|
1371
|
+
const portKey = registeredPort;
|
|
1372
|
+
registeredPort = void 0;
|
|
1373
|
+
if (admittedBridgeByClientPort.get(portKey) === entryObj) admittedBridgeByClientPort.delete(portKey);
|
|
1374
|
+
}
|
|
1375
|
+
};
|
|
1376
|
+
bridgeSock.on("error", () => {
|
|
1377
|
+
cleanupBridge();
|
|
1378
|
+
tlsSocket.destroy();
|
|
1379
|
+
});
|
|
1380
|
+
bridgeSock.on("data", (chunk) => {
|
|
1381
|
+
if (!tlsSocket.destroyed) tlsSocket.write(new Uint8Array(chunk));
|
|
1382
|
+
});
|
|
1383
|
+
bridgeSock.once("close", () => {
|
|
1384
|
+
cleanupBridge();
|
|
1385
|
+
if (!tlsSocket.destroyed) tlsSocket.end();
|
|
1386
|
+
});
|
|
1387
|
+
tlsSocket.once("end", () => {
|
|
1388
|
+
if (!bridgeSock.destroyed) bridgeSock.end();
|
|
1389
|
+
});
|
|
1390
|
+
tlsSocket.once("close", () => {
|
|
1391
|
+
pendingChunks.length = 0;
|
|
1392
|
+
cleanupBridge();
|
|
1393
|
+
if (!bridgeSock.destroyed) bridgeSock.destroy();
|
|
1394
|
+
});
|
|
1395
|
+
bridgeSock.once("connect", () => {
|
|
1396
|
+
if (tlsSocket.destroyed || typeof bridgeSock.localPort !== "number" || bridgeSock.localPort <= 0) {
|
|
1397
|
+
pendingChunks.length = 0;
|
|
1398
|
+
cleanupBridge();
|
|
1399
|
+
bridgeSock.destroy();
|
|
1400
|
+
tlsSocket.destroy();
|
|
1401
|
+
return;
|
|
1402
|
+
}
|
|
1403
|
+
registeredPort = bridgeSock.localPort;
|
|
1404
|
+
admittedBridgeByClientPort.set(registeredPort, entryObj);
|
|
1405
|
+
liveBridgeSock = bridgeSock;
|
|
1406
|
+
for (const buffered of pendingChunks) bridgeSock.write(buffered);
|
|
1407
|
+
pendingChunks.length = 0;
|
|
1408
|
+
});
|
|
1409
|
+
});
|
|
1410
|
+
srv.on("tlsClientError", () => {});
|
|
1411
|
+
return srv;
|
|
1412
|
+
};
|
|
1413
|
+
const listenTlsLoopback = (srv) => new Promise((resolvePort, rejectPort) => {
|
|
1414
|
+
const onError = (err) => {
|
|
1415
|
+
srv.off("listening", onListening);
|
|
1416
|
+
rejectPort(err);
|
|
1417
|
+
};
|
|
1418
|
+
const onListening = () => {
|
|
1419
|
+
srv.off("error", onError);
|
|
1420
|
+
const addr = srv.address();
|
|
1421
|
+
resolvePort(addr && typeof addr === "object" ? addr.port : 0);
|
|
1422
|
+
};
|
|
1423
|
+
srv.once("error", onError);
|
|
1424
|
+
srv.once("listening", onListening);
|
|
1425
|
+
srv.listen(0, "127.0.0.1");
|
|
1426
|
+
});
|
|
1427
|
+
let activeTlsServer = createConfiguredTlsServer(initialMaterial.certPem, initialMaterial.keyPem);
|
|
1428
|
+
tlsServer = activeTlsServer;
|
|
1429
|
+
pushCleanup(async () => {
|
|
1430
|
+
isShuttingDown = true;
|
|
1431
|
+
await closeAllTlsNetworking();
|
|
1432
|
+
});
|
|
1433
|
+
if (needsLoopbackHttpBridge && !server.listening) await new Promise((resolveHttp, rejectHttp) => {
|
|
1434
|
+
const onError = (err) => {
|
|
1435
|
+
server.off("listening", onListening);
|
|
1436
|
+
rejectHttp(err);
|
|
1437
|
+
};
|
|
1438
|
+
const onListening = () => {
|
|
1439
|
+
server.off("error", onError);
|
|
1440
|
+
const bAddr = server.address();
|
|
1441
|
+
if (bAddr && typeof bAddr === "object") internalHttpPort = bAddr.port;
|
|
1442
|
+
resolveHttp();
|
|
1443
|
+
};
|
|
1444
|
+
server.once("error", onError);
|
|
1445
|
+
server.once("listening", onListening);
|
|
1446
|
+
server.listen(0, "127.0.0.1");
|
|
1447
|
+
});
|
|
1448
|
+
if (needsAtomicTlsSwap) {
|
|
1449
|
+
internalTlsPort = await listenTlsLoopback(activeTlsServer);
|
|
1450
|
+
const tcpListener = net.createServer((clientTcp) => {
|
|
1451
|
+
registerRawSocketTimer(clientTcp);
|
|
1452
|
+
if (isShuttingDown || internalTlsPort <= 0) {
|
|
1453
|
+
clientTcp.destroy();
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
const upstream = net.connect({
|
|
1457
|
+
host: "127.0.0.1",
|
|
1458
|
+
port: internalTlsPort
|
|
1459
|
+
});
|
|
1460
|
+
bridgeSockets.add(upstream);
|
|
1461
|
+
const earlyTcp = [];
|
|
1462
|
+
let earlyBytes = 0;
|
|
1463
|
+
let connected = false;
|
|
1464
|
+
clientTcp.on("data", (chunk) => {
|
|
1465
|
+
clearRawSocketTimer(clientTcp);
|
|
1466
|
+
const bytes = new Uint8Array(chunk);
|
|
1467
|
+
if (connected) {
|
|
1468
|
+
if (!upstream.destroyed) upstream.write(bytes);
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
earlyBytes += bytes.byteLength;
|
|
1472
|
+
if (earlyBytes > MAX_PRE_BRIDGE_BUFFER_BYTES) {
|
|
1473
|
+
earlyTcp.length = 0;
|
|
1474
|
+
clientTcp.destroy();
|
|
1475
|
+
upstream.destroy();
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
earlyTcp.push(bytes);
|
|
1479
|
+
});
|
|
1480
|
+
let upstreamBytesSent = 0;
|
|
1481
|
+
upstream.on("error", () => {
|
|
1482
|
+
bridgeSockets.delete(upstream);
|
|
1483
|
+
clientTcp.destroy();
|
|
1484
|
+
});
|
|
1485
|
+
upstream.on("data", (chunk) => {
|
|
1486
|
+
const bytes = new Uint8Array(chunk);
|
|
1487
|
+
upstreamBytesSent += bytes.byteLength;
|
|
1488
|
+
if (!clientTcp.destroyed) clientTcp.write(bytes);
|
|
1489
|
+
});
|
|
1490
|
+
upstream.once("close", () => {
|
|
1491
|
+
bridgeSockets.delete(upstream);
|
|
1492
|
+
if (!clientTcp.destroyed) if (upstreamBytesSent === 0) clientTcp.destroy();
|
|
1493
|
+
else clientTcp.end();
|
|
1494
|
+
});
|
|
1495
|
+
clientTcp.once("end", () => {
|
|
1496
|
+
if (!upstream.destroyed) upstream.end();
|
|
1497
|
+
});
|
|
1498
|
+
clientTcp.once("close", () => {
|
|
1499
|
+
earlyTcp.length = 0;
|
|
1500
|
+
bridgeSockets.delete(upstream);
|
|
1501
|
+
if (!upstream.destroyed) upstream.destroy();
|
|
1502
|
+
});
|
|
1503
|
+
upstream.once("connect", () => {
|
|
1504
|
+
if (clientTcp.destroyed) {
|
|
1505
|
+
bridgeSockets.delete(upstream);
|
|
1506
|
+
upstream.destroy();
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
connected = true;
|
|
1510
|
+
for (const c of earlyTcp) upstream.write(c);
|
|
1511
|
+
earlyTcp.length = 0;
|
|
1512
|
+
});
|
|
1513
|
+
});
|
|
1514
|
+
publicTcpServer = tcpListener;
|
|
1515
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
1516
|
+
const onError = (err) => {
|
|
1517
|
+
tcpListener.off("listening", onListening);
|
|
1518
|
+
rejectListen(err);
|
|
1519
|
+
};
|
|
1520
|
+
const onListening = () => {
|
|
1521
|
+
tcpListener.off("error", onError);
|
|
1522
|
+
resolveListen();
|
|
1523
|
+
};
|
|
1524
|
+
tcpListener.once("error", onError);
|
|
1525
|
+
tcpListener.once("listening", onListening);
|
|
1526
|
+
tcpListener.listen(requestedPort, bindHost);
|
|
1527
|
+
});
|
|
1528
|
+
const tcpAddr = tcpListener.address();
|
|
1529
|
+
boundPort = tcpAddr && typeof tcpAddr === "object" && typeof tcpAddr.port === "number" ? tcpAddr.port : requestedPort;
|
|
1530
|
+
} else {
|
|
1531
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
1532
|
+
const onError = (err) => {
|
|
1533
|
+
activeTlsServer.off("listening", onListening);
|
|
1534
|
+
rejectListen(err);
|
|
1535
|
+
};
|
|
1536
|
+
const onListening = () => {
|
|
1537
|
+
activeTlsServer.off("error", onError);
|
|
1538
|
+
resolveListen();
|
|
1539
|
+
};
|
|
1540
|
+
activeTlsServer.once("error", onError);
|
|
1541
|
+
activeTlsServer.once("listening", onListening);
|
|
1542
|
+
activeTlsServer.listen(requestedPort, bindHost);
|
|
1543
|
+
});
|
|
1544
|
+
const tlsAddr = activeTlsServer.address();
|
|
1545
|
+
boundPort = tlsAddr && typeof tlsAddr === "object" && typeof tlsAddr.port === "number" ? tlsAddr.port : requestedPort;
|
|
1546
|
+
}
|
|
1547
|
+
reloadTlsCredentials = async () => {
|
|
1548
|
+
const refreshed = loadOrSynthesizeTlsMaterial();
|
|
1549
|
+
if (needsAtomicTlsSwap && activeTlsServer.listening) {
|
|
1550
|
+
const nextTlsServer = createConfiguredTlsServer(refreshed.certPem, refreshed.keyPem);
|
|
1551
|
+
const nextPort = await listenTlsLoopback(nextTlsServer);
|
|
1552
|
+
const previousTlsServer = activeTlsServer;
|
|
1553
|
+
activeTlsServer = nextTlsServer;
|
|
1554
|
+
tlsServer = nextTlsServer;
|
|
1555
|
+
internalTlsPort = nextPort;
|
|
1556
|
+
retiredTlsServers.add(previousTlsServer);
|
|
1557
|
+
previousTlsServer.close(() => {
|
|
1558
|
+
retiredTlsServers.delete(previousTlsServer);
|
|
1559
|
+
});
|
|
1560
|
+
} else activeTlsServer.setSecureContext(buildChainTlsSecureContextOptions(refreshed.certPem, refreshed.keyPem));
|
|
1561
|
+
return {
|
|
1562
|
+
reloaded: true,
|
|
1563
|
+
subjectDid: refreshed.subjectDid,
|
|
1564
|
+
spkiSha256Hex: refreshed.spkiSha256Hex
|
|
1565
|
+
};
|
|
1566
|
+
};
|
|
1567
|
+
} else {
|
|
1568
|
+
await new Promise((resolveListen, rejectListen) => {
|
|
1569
|
+
const onError = (err) => {
|
|
1570
|
+
server.off("listening", onListening);
|
|
1571
|
+
rejectListen(err);
|
|
1572
|
+
};
|
|
1573
|
+
const onListening = () => {
|
|
1574
|
+
server.off("error", onError);
|
|
1575
|
+
resolveListen();
|
|
1576
|
+
};
|
|
1577
|
+
server.once("error", onError);
|
|
1578
|
+
server.once("listening", onListening);
|
|
1579
|
+
server.listen(requestedPort, bindHost);
|
|
1580
|
+
});
|
|
1581
|
+
const addressInfo = server.address();
|
|
1582
|
+
boundPort = addressInfo && typeof addressInfo === "object" && typeof addressInfo.port === "number" ? addressInfo.port : requestedPort;
|
|
1583
|
+
}
|
|
827
1584
|
const urlHost = formatHttpHostForUrl(bindHost);
|
|
828
|
-
const endpoint =
|
|
829
|
-
const wsUrl =
|
|
1585
|
+
const endpoint = `${validatedTls ? "https" : "http"}://${urlHost}:${boundPort}`;
|
|
1586
|
+
const wsUrl = `${validatedTls ? "wss" : "ws"}://${urlHost}:${boundPort}`;
|
|
830
1587
|
if (testHooks?.onAfterBoot) await testHooks.onAfterBoot({
|
|
831
1588
|
dataDir,
|
|
832
1589
|
statedb,
|
|
@@ -867,7 +1624,8 @@ async function bootChainNodeOnce(params) {
|
|
|
867
1624
|
isShuttingDown = true;
|
|
868
1625
|
server.closeIdleConnections?.();
|
|
869
1626
|
await waitForActiveHttpDrain();
|
|
870
|
-
|
|
1627
|
+
if (validatedTls) await closeAllTlsNetworking();
|
|
1628
|
+
else await new Promise((resolveClose) => {
|
|
871
1629
|
server.close(() => resolveClose());
|
|
872
1630
|
});
|
|
873
1631
|
await resolver.shutdown().catch(captureErr);
|
|
@@ -931,10 +1689,14 @@ async function bootChainNodeOnce(params) {
|
|
|
931
1689
|
indexdb,
|
|
932
1690
|
trustedStorage,
|
|
933
1691
|
server,
|
|
1692
|
+
get tlsServer() {
|
|
1693
|
+
return tlsServer;
|
|
1694
|
+
},
|
|
934
1695
|
...followerSync ? { followerSync } : {},
|
|
935
1696
|
...hotstuffRuntime ? { hotstuffRuntime } : {},
|
|
936
1697
|
preferencesManager,
|
|
937
1698
|
maintenanceController,
|
|
1699
|
+
reloadTlsCredentials,
|
|
938
1700
|
stop
|
|
939
1701
|
};
|
|
940
1702
|
} catch (err) {
|
|
@@ -1031,6 +1793,9 @@ async function startChainNode(optionsOrProfile) {
|
|
|
1031
1793
|
get server() {
|
|
1032
1794
|
return currentBooted.server;
|
|
1033
1795
|
},
|
|
1796
|
+
get tlsServer() {
|
|
1797
|
+
return currentBooted.tlsServer;
|
|
1798
|
+
},
|
|
1034
1799
|
get followerSync() {
|
|
1035
1800
|
return currentBooted.followerSync;
|
|
1036
1801
|
},
|
|
@@ -1046,15 +1811,23 @@ async function startChainNode(optionsOrProfile) {
|
|
|
1046
1811
|
async ledgerHealth() {
|
|
1047
1812
|
return currentBooted.ledger.getHealth();
|
|
1048
1813
|
},
|
|
1814
|
+
async reloadTlsCredentials() {
|
|
1815
|
+
return withLifecycleLock(async () => currentBooted.reloadTlsCredentials());
|
|
1816
|
+
},
|
|
1049
1817
|
...advanceClockFn ? { advanceClock: advanceClockFn } : {},
|
|
1050
1818
|
async restart(nextProfile) {
|
|
1051
1819
|
return withLifecycleLock(async () => {
|
|
1052
1820
|
const previousPort = currentBooted.port;
|
|
1053
1821
|
await currentBooted.stop();
|
|
1822
|
+
const mergedTls = nextProfile?.http && "tls" in nextProfile.http ? nextProfile.http.tls ? {
|
|
1823
|
+
...currentProfile.http.tls ?? {},
|
|
1824
|
+
...nextProfile.http.tls
|
|
1825
|
+
} : void 0 : currentProfile.http.tls;
|
|
1054
1826
|
const mergedHttp = {
|
|
1055
1827
|
...currentProfile.http,
|
|
1056
1828
|
...nextProfile?.http ?? {},
|
|
1057
|
-
port: nextProfile?.http?.port !== void 0 ? nextProfile.http.port : previousPort
|
|
1829
|
+
port: nextProfile?.http?.port !== void 0 ? nextProfile.http.port : previousPort,
|
|
1830
|
+
...mergedTls !== void 0 ? { tls: mergedTls } : { tls: void 0 }
|
|
1058
1831
|
};
|
|
1059
1832
|
const mergedProfile = {
|
|
1060
1833
|
...currentProfile,
|
|
@@ -1104,4 +1877,4 @@ async function startChainNode(optionsOrProfile) {
|
|
|
1104
1877
|
}
|
|
1105
1878
|
|
|
1106
1879
|
//#endregion
|
|
1107
|
-
export { startChainNode };
|
|
1880
|
+
export { startChainNode, writeFileDurably };
|