Skip to content

Commit

Permalink
don't init Nip07 signer by default
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Nov 22, 2024
1 parent 3ce40a6 commit e60be89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
34 changes: 24 additions & 10 deletions lib/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand All @@ -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
Expand All @@ -64,14 +77,14 @@ export class Nostr {
* @returns {Promise<NDKEvent>}
*/
/* 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
Expand All @@ -91,13 +104,13 @@ export class Nostr {
* @returns {Promise<NDKEvent>}
*/
/* 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 })
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion worker/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e60be89

Please sign in to comment.