fix pow error and missing import, wrap relay with trycatch

This commit is contained in:
en 2025-01-18 14:42:55 +01:00
parent 1b9b51f063
commit c8bfb9d1ad

View File

@ -1,4 +1,10 @@
import { finalizeEvent, verifyEvent, getPublicKey, nip19 } from "nostr-tools"; import {
finalizeEvent,
verifyEvent,
getPublicKey,
nip19,
getEventHash,
} from "nostr-tools";
import { useWebSocketImplementation, Relay } from "nostr-tools/relay"; import { useWebSocketImplementation, Relay } from "nostr-tools/relay";
import WebSocket from "ws"; import WebSocket from "ws";
useWebSocketImplementation(WebSocket); useWebSocketImplementation(WebSocket);
@ -48,10 +54,14 @@ try {
const isGood = verifyEvent(profileEvent); const isGood = verifyEvent(profileEvent);
if (isGood) { if (isGood) {
(async () => { (async () => {
const relay = await Relay.connect(RELAY); try {
await relay.publish(profileEvent); const relay = await Relay.connect(RELAY);
console.log("IT IS ALIVE!!"); await relay.publish(profileEvent);
relay.close(); console.log("IT IS ALIVE!!");
relay.close();
} catch (error) {
exit(error);
}
})(); })();
} }
@ -73,12 +83,16 @@ try {
const isGood = verifyEvent(event); const isGood = verifyEvent(event);
if (isGood) { if (isGood) {
(async () => { (async () => {
console.log("BOT IS CONNECTING.."); try {
const relay = await Relay.connect(RELAY); console.log("BOT IS CONNECTING..");
console.log("BOT CONNECTED"); const relay = await Relay.connect(RELAY);
await relay.publish(event); console.log("BOT CONNECTED");
console.log(`[${today.toISOString()}] ${message}`, event); await relay.publish(event);
relay.close(); console.log(`[${today.toISOString()}] ${message}`, event);
relay.close();
} catch (error) {
exit(error);
}
})(); })();
} }
}; };
@ -86,27 +100,31 @@ try {
console.log("BOT IS BOTTING as", nip19.npubEncode(pk)); console.log("BOT IS BOTTING as", nip19.npubEncode(pk));
intervalId = setInterval(run, TWO_DAYS); intervalId = setInterval(run, TWO_DAYS);
} catch (error) { } catch (error) {
exit(error);
}
function exit(error) {
console.error(`[${new Date().toISOString()}]`, "RIP BOT", error); console.error(`[${new Date().toISOString()}]`, "RIP BOT", error);
clearInterval(intervalId); clearInterval(intervalId);
process.exit(1); process.exit(1);
} }
function finalizeEventWithPoW(event, sk, difficulty = 0) { function finalizeEventWithPoW(event, sk, difficulty = 0) {
if (difficulty === 0) return finalizeEvent(event, sk); if (!difficulty) return finalizeEvent(event, sk);
const pk = getPublicKey(sk); const pk = getPublicKey(sk);
let nonce = 0; let nonce = 0;
let leadingZeroes = 0; let leadingZeroes = 0;
let event = { ...event, pubkey: pk }; let unsignedEvent = { ...event, pubkey: pk };
while (true) { while (true) {
event.tags = [["nonce", nonce.toString(), difficulty.toString()]]; unsignedEvent.tags = [["nonce", nonce.toString(), difficulty.toString()]];
event.created_at = Math.floor(Date.now() / 1000); unsignedEvent.created_at = Math.floor(Date.now() / 1000);
const hash = getEventHash(event); const hash = getEventHash(unsignedEvent);
leadingZeroes = countLeadingZeroes(hash); leadingZeroes = countLeadingZeroes(hash);
if (leadingZeroes >= difficulty) { if (leadingZeroes >= difficulty) {
return finalizeEvent(event, sk); return finalizeEvent(unsignedEvent, sk);
} }
nonce++; nonce++;