diff --git a/jsr.json b/jsr.json index 090fa74..169b07d 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@nostr/tools", - "version": "2.16.0", + "version": "2.16.1", "exports": { ".": "./index.ts", "./core": "./core.ts", diff --git a/nip57.ts b/nip57.ts index 6ff1b8a..3a5b816 100644 --- a/nip57.ts +++ b/nip57.ts @@ -1,6 +1,6 @@ import { bech32 } from '@scure/base' -import { validateEvent, verifyEvent, type Event, type EventTemplate } from './pure.ts' +import { NostrEvent, validateEvent, verifyEvent, type Event, type EventTemplate } from './pure.ts' import { utf8Decoder } from './utils.ts' import { isReplaceableKind, isAddressableKind } from './kinds.ts' @@ -42,48 +42,43 @@ export async function getZapEndpoint(metadata: Event): Promise { return null } -export function makeZapRequest({ - profile, - event, - amount, - relays, - comment = '', -}: { - profile: string - event: string | Event | null +type ProfileZap = { + pubkey: string amount: number - comment: string + comment?: string relays: string[] -}): EventTemplate { - if (!amount) throw new Error('amount not given') - if (!profile) throw new Error('profile not given') +} +type EventZap = { + event: NostrEvent + amount: number + comment?: string + relays: string[] +} + +export function makeZapRequest(params: ProfileZap | EventZap): EventTemplate { let zr: EventTemplate = { kind: 9734, created_at: Math.round(Date.now() / 1000), - content: comment, + content: params.comment || '', tags: [ - ['p', profile], - ['amount', amount.toString()], - ['relays', ...relays], + ['p', 'pubkey' in params ? params.pubkey : params.event.pubkey], + ['amount', params.amount.toString()], + ['relays', ...params.relays], ], } - if (event && typeof event === 'string') { - zr.tags.push(['e', event]) - } - if (event && typeof event === 'object') { - // replacable event - if (isReplaceableKind(event.kind)) { - const a = ['a', `${event.kind}:${event.pubkey}:`] + if ('event' in params) { + if (isReplaceableKind(params.event.kind)) { + const a = ['a', `${params.event.kind}:${params.event.pubkey}:`] zr.tags.push(a) - // addressable event - } else if (isAddressableKind(event.kind)) { - let d = event.tags.find(([t, v]) => t === 'd' && v) + } else if (isAddressableKind(params.event.kind)) { + let d = params.event.tags.find(([t, v]) => t === 'd' && v) if (!d) throw new Error('d tag not found or is empty') - const a = ['a', `${event.kind}:${event.pubkey}:${d[1]}`] + const a = ['a', `${params.event.kind}:${params.event.pubkey}:${d[1]}`] zr.tags.push(a) } + zr.tags.push(['k', params.event.kind.toString()]) } return zr diff --git a/package.json b/package.json index 3092e06..b9d9cf0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.16.0", + "version": "2.16.1", "description": "Tools for making a Nostr client.", "repository": { "type": "git",