Fix write verification and re-enable it

This commit is contained in:
pleb
2026-03-22 22:59:02 -07:00
parent ef7463a29a
commit ec51dcc52e
9 changed files with 106 additions and 38 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ const DEFAULT_LISTEN_ADDR = "0.0.0.0";
const DEFAULT_PORT = 9464;
const DEFAULT_LOG_LEVEL: LogLevel = "info";
const DEFAULT_WRITE_CHECK_ENABLED = true;
const DEFAULT_WRITE_CHECK_VERIFY_READ = false;
const DEFAULT_WRITE_CHECK_VERIFY_READ = true;
const DEFAULT_WRITE_CHECK_KIND = 30078;
const MIN_TIMEOUT_SECONDS = 1;
+46 -20
View File
@@ -1,5 +1,6 @@
import { performance } from "node:perf_hooks";
import { finalizeEvent, getPublicKey, nip19, Relay, type Filter } from "nostr-tools";
import WebSocket from "ws";
interface WriteConfirmInput {
relay: string;
@@ -35,6 +36,13 @@ function decodePrivateKey(raw: string): Uint8Array {
return Uint8Array.from(Buffer.from(value, "hex"));
}
function ensureWebSocketSupport(): void {
const globalWithWebSocket = globalThis as { WebSocket?: unknown };
if (typeof globalWithWebSocket.WebSocket === "undefined") {
globalWithWebSocket.WebSocket = WebSocket;
}
}
function withTimeout<T>(promise: Promise<T>, timeoutMs: number, label: string): Promise<T> {
return new Promise<T>((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`${label} timed out after ${timeoutMs}ms`)), timeoutMs);
@@ -56,35 +64,53 @@ async function waitForEvent(
expectedEventId: string,
timeoutMs: number,
): Promise<boolean> {
return withTimeout<boolean>(
new Promise<boolean>((resolve) => {
let settled = false;
const sub = relay.subscribe([filter], {
onevent: (evt) => {
if (settled) return;
if (evt.id === expectedEventId) {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
const elapsed = Date.now() - started;
const remainingMs = Math.max(1, timeoutMs - elapsed);
const queryTimeoutMs = Math.max(200, Math.min(1_000, remainingMs));
const found = await withTimeout<boolean>(
new Promise<boolean>((resolve) => {
let settled = false;
const sub = relay.subscribe([filter], {
onevent: (evt) => {
if (settled) return;
if (evt.id === expectedEventId) {
settled = true;
sub.close();
resolve(true);
}
},
oneose: () => {
if (settled) return;
settled = true;
sub.close();
resolve(true);
}
},
oneose: () => {
if (settled) return;
settled = true;
sub.close();
resolve(false);
},
resolve(false);
},
});
}),
queryTimeoutMs,
"write confirm read-after-write query",
).catch(() => false);
if (found) return true;
const sleepMs = Math.min(200, Math.max(0, timeoutMs - (Date.now() - started)));
if (sleepMs > 0) {
await new Promise<void>((resolve) => {
setTimeout(resolve, sleepMs);
});
}),
timeoutMs,
"write confirm read-after-write",
);
}
}
return false;
}
export async function runWriteConfirm(input: WriteConfirmInput): Promise<WriteConfirmResult> {
const startedAt = performance.now();
let relay: Relay | null = null;
try {
ensureWebSocketSupport();
const secretKey = decodePrivateKey(input.privkey);
const derivedPubkey = getPublicKey(secretKey);