1
0
forked from enes/fiatjafbuzz

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