import type { EventTemplate, Event } from 'nostr-tools' import { getEventHash, signEvent } from 'nostr-tools' import { nostrConnectService } from './nostrconnect' import { nostrService } from './nostr' /** * Remote signer using NostrConnect * Supports both direct signing (if private key available) and remote signing via bridge */ export class NostrRemoteSigner { /** * Sign an event template * Requires private key to be available */ async signEvent(eventTemplate: EventTemplate): Promise { // Get the event hash first const eventId = getEventHash(eventTemplate) // Try to get private key from nostrService (if available from NostrConnect) const privateKey = nostrService.getPrivateKey() if (privateKey) { // Use direct signing if private key is available const event = { ...eventTemplate, id: eventId, sig: signEvent(eventTemplate, privateKey), } as Event return event } // If no private key, remote signing is required // Note: use.nsec.app might not support direct signing via postMessage throw new Error( 'Private key required for signing. ' + 'Please use a NostrConnect wallet that provides signing capabilities, ' + 'or ensure your wallet is properly connected.' ) } /** * Check if remote signing is available */ isAvailable(): boolean { const state = nostrConnectService.getState() return state.connected && !!state.pubkey } /** * Check if direct signing (with private key) is available */ isDirectSigningAvailable(): boolean { return !!nostrService.getPrivateKey() } } export const nostrRemoteSigner = new NostrRemoteSigner()