From ff5387b7786c287c2880b41a9a64e1ab8b48307a Mon Sep 17 00:00:00 2001 From: Pablo Fernandez Date: Sat, 21 Sep 2024 13:44:24 -0400 Subject: [PATCH] updates --- package.json | 2 +- src/daemon/admin/index.ts | 44 ++++++++++++++++++++++++++++++++++++- src/daemon/backend/index.ts | 5 +++-- src/daemon/run.ts | 15 ++++++++++++- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index fc98e34..4232435 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "@fastify/view": "^8.2.0", "@inquirer/password": "^1.1.2", "@inquirer/prompts": "^1.2.3", - "@nostr-dev-kit/ndk": "^2.8.1", + "@nostr-dev-kit/ndk": "workspace:*", "@prisma/client": "^5.4.1", "@scure/base": "^1.1.1", "@types/yargs": "^17.0.24", diff --git a/src/daemon/admin/index.ts b/src/daemon/admin/index.ts index a583f36..4db9cc2 100644 --- a/src/daemon/admin/index.ts +++ b/src/daemon/admin/index.ts @@ -1,5 +1,5 @@ import "websocket-polyfill"; -import NDK, { NDKKind, NDKPrivateKeySigner, NDKRpcRequest, NDKRpcResponse, NDKUser } from '@nostr-dev-kit/ndk'; +import NDK, { NDKEvent, NDKKind, NDKPrivateKeySigner, NDKRpcRequest, NDKRpcResponse, NDKUser, NostrEvent } from '@nostr-dev-kit/ndk'; import { NDKNostrRpc } from '@nostr-dev-kit/ndk'; import createDebug from 'debug'; import { Key, KeyUser } from '../run'; @@ -121,6 +121,8 @@ class AdminInterface { }); this.rpc.on('request', (req) => this.handleRequest(req)); + + pingOrDie(this.ndk); }).catch((err) => { console.log('❌ admin connection failed'); console.log(err); @@ -393,4 +395,44 @@ class AdminInterface { } } +async function pingOrDie(ndk: NDK) { + let deathTimer: NodeJS.Timeout | null = null; + + function resetDeath() { + if (deathTimer) clearTimeout(deathTimer); + deathTimer = setTimeout(() => { + console.log(`❌ No ping event received in 30 seconds. Exiting.`); + process.exit(1); + }, 50000); + } + + const self = await ndk.signer!.user(); + const sub = ndk.subscribe({ + authors: [self.pubkey], + kinds: [NDKKind.NostrConnect], + "#p": [self.pubkey] + }); + sub.on("event", (event: NDKEvent) => { + console.log(`🔔 Received ping event:`, event.created_at); + resetDeath(); + }); + sub.start(); + + resetDeath(); + + setInterval(() => { + const event = new NDKEvent(ndk, { + kind: NDKKind.NostrConnect, + tags: [ ["p", self.pubkey] ], + content: "ping" + } as NostrEvent); + event.publish().then(() => { + console.log(`🔔 Sent ping event:`, event.created_at); + }).catch((e: any) => { + console.log(`❌ Failed to send ping event:`, e.message); + process.exit(1); + }); + }, 20000); +} + export default AdminInterface; diff --git a/src/daemon/backend/index.ts b/src/daemon/backend/index.ts index 5dd240a..7661a09 100644 --- a/src/daemon/backend/index.ts +++ b/src/daemon/backend/index.ts @@ -1,4 +1,4 @@ -import NDK, { NDKNip46Backend, Nip46PermitCallback } from '@nostr-dev-kit/ndk'; +import NDK, { NDKNip46Backend, NDKPrivateKeySigner, Nip46PermitCallback } from '@nostr-dev-kit/ndk'; import prisma from '../../db.js'; import type {FastifyInstance} from "fastify"; @@ -13,7 +13,8 @@ export class Backend extends NDKNip46Backend { cb: Nip46PermitCallback, baseUrl?: string ) { - super(ndk, key, cb); + const signer = new NDKPrivateKeySigner(key); + super(ndk, signer, cb); this.baseUrl = baseUrl; this.fastify = fastify; diff --git a/src/daemon/run.ts b/src/daemon/run.ts index ec5b16a..262a150 100644 --- a/src/daemon/run.ts +++ b/src/daemon/run.ts @@ -226,7 +226,20 @@ class Daemon { */ async startKey(name: string, nsec: string) { const cb = signingAuthorizationCallback(name, this.adminInterface); - const hexpk = nip19.decode(nsec).data as string; + let hexpk: string; + + if (nsec.startsWith('nsec1')) { + try { + const key = new NDKPrivateKeySigner(nsec); + hexpk = key.privateKey!; + } catch(e) { + console.error(`Error loading key ${name}:`, e); + return + } + } else { + hexpk = nsec; + } + const backend = new Backend(this.ndk, this.fastify, hexpk, cb, this.config.baseUrl); await backend.start(); }