diff --git a/lib/nostr.js b/lib/nostr.js index ae2165e0b..0250eb037 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -33,14 +33,14 @@ export class Nostr { */ _ndk = null - constructor ({ privKey, defaultSigner, relays, supportNip07 = true, ...ndkOptions } = {}) { + constructor ({ privKey, defaultSigner, relays, supportNip07 = false, ...ndkOptions } = {}) { this._ndk = new NDK({ explicitRelayUrls: relays, blacklistRelayUrls: RELAYS_BLACKLIST, autoConnectUserRelays: false, autoFetchUserMutelist: false, clientName: 'stacker.news', - signer: defaultSigner ?? (privKey ? new NDKPrivateKeySigner(privKey) : null) ?? (supportNip07 && typeof window !== 'undefined' && window?.nostr ? new NDKNip07Signer() : undefined), + signer: defaultSigner ?? this.getSigner({ privKey, supportNip07 }), ...ndkOptions }) } @@ -52,6 +52,19 @@ export class Nostr { return this._ndk } + /** + * + * @param {args} param0 + * @param {string} [args.privKey] - private key to use for signing + * @param {boolean} [args.supportNip07] - whether to use NIP-07 signer if available + * @returns {NDKPrivateKeySigner | NDKNip46Signer | NDKNip07Signer | null} - a signer instance + */ + getSigner ({ privKey, supportNip07 = true } = {}) { + if (privKey) return new NDKPrivateKeySigner(privKey) + if (supportNip07 && typeof window !== 'undefined' && window?.nostr) return new NDKNip07Signer() + return null + } + /** * @param {Object} rawEvent * @param {number} rawEvent.kind @@ -64,14 +77,14 @@ export class Nostr { * @returns {Promise} */ /* eslint-disable camelcase */ - async sign ({ kind, created_at, content, tags }, { privKey, signer } = {}) { - const event = new NDKEvent(this._ndk) + async sign ({ kind, created_at, content, tags }, { signer } = {}) { + const event = new NDKEvent(this.ndk) event.kind = kind event.created_at = created_at event.content = content event.tags = tags - signer ??= (privKey ? new NDKPrivateKeySigner(privKey) : this.ndk.signer) + signer ??= this.ndk.signer if (!signer) throw new Error('no way to sign this event, please provide a signer or private key') await event.sign(signer) return event @@ -91,13 +104,13 @@ export class Nostr { * @returns {Promise} */ /* eslint-disable camelcase */ - async publish ({ created_at, content, tags = [], kind }, { relays, privKey, signer, timeout } = {}) { - const event = await this.sign({ kind, created_at, content, tags }, { privKey, signer }) + async publish ({ created_at, content, tags = [], kind }, { relays, signer, timeout } = {}) { + const event = await this.sign({ kind, created_at, content, tags }, { signer }) const successfulRelays = [] const failedRelays = [] - const relaySet = NDKRelaySet.fromRelayUrls(relays, this._ndk, true) + const relaySet = NDKRelaySet.fromRelayUrls(relays, this.ndk, true) event.on('relay:publish:failed', (relay, error) => { failedRelays.push({ relay: relay.url, error }) @@ -115,9 +128,10 @@ export class Nostr { } /* eslint-disable camelcase */ - async crosspost ({ created_at, content, tags = [], kind }, { relays = DEFAULT_CROSSPOSTING_RELAYS, privKey, signer, timeout } = {}) { + async crosspost ({ created_at, content, tags = [], kind }, { relays = DEFAULT_CROSSPOSTING_RELAYS, signer, timeout } = {}) { try { - const { event: signedEvent, successfulRelays, failedRelays } = await this.publish({ created_at, content, tags, kind }, { relays, privKey, signer, timeout }) + signer ??= this.getSigner({ supportNip07: true }) + const { event: signedEvent, successfulRelays, failedRelays } = await this.publish({ created_at, content, tags, kind }, { relays, signer, timeout }) let noteId = null if (signedEvent.kind !== 1) { diff --git a/worker/nostr.js b/worker/nostr.js index 6ad15d6bd..fe2201262 100644 --- a/worker/nostr.js +++ b/worker/nostr.js @@ -45,9 +45,10 @@ export async function nip57 ({ data: { hash }, boss, lnd, models }) { } console.log('zap note', e, relays) + const signer = Nostr.getSigner({ privKey: process.env.NOSTR_PRIVATE_KEY }) await Nostr.publish(e, { relays, - privKey: process.env.NOSTR_PRIVATE_KEY, + signer, timeout: 1000 }) } catch (e) {