From 4188f2c596b11dbc13db41f98bc512d0b02b1dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Conselheiro?= <91137293+antonioconselheiro@users.noreply.github.com> Date: Mon, 10 Mar 2025 01:58:00 -0300 Subject: [PATCH] Generic repost --- nip18.test.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- nip18.ts | 19 ++++++++++++++----- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/nip18.test.ts b/nip18.test.ts index 0144289..a2630fd 100644 --- a/nip18.test.ts +++ b/nip18.test.ts @@ -1,7 +1,7 @@ import { describe, test, expect } from 'bun:test' import { hexToBytes } from '@noble/hashes/utils' -import { finalizeEvent, getPublicKey } from './pure.ts' -import { Repost, ShortTextNote } from './kinds.ts' +import { EventTemplate, finalizeEvent, getPublicKey } from './pure.ts' +import { GenericRepost, Repost, ShortTextNote, BadgeDefinition as BadgeDefinitionKind } from './kinds.ts' import { finishRepostEvent, getRepostedEventPointer, getRepostedEvent } from './nip18.ts' import { buildEvent } from './test-helpers.ts' @@ -86,6 +86,51 @@ describe('finishRepostEvent + getRepostedEventPointer + getRepostedEvent', () => }) }) +describe('GenericRepost', () => { + const privateKey = hexToBytes('d217c1ff2f8a65c3e3a1740db3b9f58b8c848bb45e26d00ed4714e4a0f4ceecf') + const publicKey = getPublicKey(privateKey) + + const eventTemplate: EventTemplate = { + content: '', + created_at: 1617932114, + kind: BadgeDefinitionKind, + tags: [ + ['d', 'badge-id'], + ['name', 'Badge Name'], + ['description', 'Badge Description'], + ['image', 'https://example.com/badge.png', '1024x1024'], + ['thumb', 'https://example.com/thumb.png', '100x100'], + ['thumb', 'https://example.com/thumb2.png', '200x200'], + ], + } + + const repostedEvent = finalizeEvent(eventTemplate, privateKey) + test('should create a generic reposted event', () => { + const template = { created_at: 1617932115 } + const event = finishRepostEvent(template, repostedEvent, relayUrl, privateKey) + + expect(event.kind).toEqual(GenericRepost) + expect(event.tags).toEqual([ + ['e', repostedEvent.id, relayUrl], + ['p', repostedEvent.pubkey], + ['k', '30009'], + ]) + expect(event.content).toEqual(JSON.stringify(repostedEvent)) + expect(event.created_at).toEqual(template.created_at) + expect(event.pubkey).toEqual(publicKey) + + const repostedEventPointer = getRepostedEventPointer(event) + + expect(repostedEventPointer!.id).toEqual(repostedEvent.id) + expect(repostedEventPointer!.author).toEqual(repostedEvent.pubkey) + expect(repostedEventPointer!.relays).toEqual([relayUrl]) + + const repostedEventFromContent = getRepostedEvent(event) + + expect(repostedEventFromContent).toEqual(repostedEvent) + }) +}) + describe('getRepostedEventPointer', () => { test('should parse an event with only an `e` tag', () => { const event = buildEvent({ diff --git a/nip18.ts b/nip18.ts index 9fd6065..12ffadb 100644 --- a/nip18.ts +++ b/nip18.ts @@ -1,6 +1,6 @@ -import { Event, finalizeEvent, verifyEvent } from './pure.ts' -import { Repost } from './kinds.ts' +import { GenericRepost, Repost, ShortTextNote } from './kinds.ts' import { EventPointer } from './nip19.ts' +import { Event, finalizeEvent, verifyEvent } from './pure.ts' export type RepostEventTemplate = { /** @@ -25,10 +25,19 @@ export function finishRepostEvent( relayUrl: string, privateKey: Uint8Array, ): Event { + let kind: Repost | GenericRepost + const tags = [...(t.tags ?? []), ['e', reposted.id, relayUrl], ['p', reposted.pubkey]] + if (reposted.kind === ShortTextNote) { + kind = Repost + } else { + kind = GenericRepost + tags.push(['k', String(reposted.kind)]) + } + return finalizeEvent( { - kind: Repost, - tags: [...(t.tags ?? []), ['e', reposted.id, relayUrl], ['p', reposted.pubkey]], + kind, + tags, content: t.content === '' || reposted.tags?.find(tag => tag[0] === '-') ? '' : JSON.stringify(reposted), created_at: t.created_at, }, @@ -37,7 +46,7 @@ export function finishRepostEvent( } export function getRepostedEventPointer(event: Event): undefined | EventPointer { - if (event.kind !== Repost) { + if (![Repost, GenericRepost].includes(event.kind)) { return undefined }