From e2ec7a4b551150b8f658ed237880ac82abee1f76 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 25 Oct 2024 22:10:05 -0300 Subject: [PATCH] fix types, imports and other stuff on nip17 and nip59. --- nip17.test.ts | 5 +++-- nip17.ts | 11 ++++++----- nip59.test.ts | 13 +++++++++---- nip59.ts | 21 +++------------------ 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/nip17.test.ts b/nip17.test.ts index 2f6b438..ac7eaf0 100644 --- a/nip17.test.ts +++ b/nip17.test.ts @@ -2,11 +2,12 @@ import { test, expect } from 'bun:test' import { getPublicKey } from './pure.ts' import { decode } from './nip19.ts' import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts' +import { hexToBytes } from '@noble/hashes/utils' const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data -const sk1 = 'f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840' -const sk2 = '5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747' +const sk1 = hexToBytes('f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840') +const sk2 = hexToBytes('5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747') const recipients = [ { publicKey: getPublicKey(sk1), relayUrl: 'wss://relay1.com' }, diff --git a/nip17.ts b/nip17.ts index 7e5ece2..d579207 100644 --- a/nip17.ts +++ b/nip17.ts @@ -1,6 +1,6 @@ import { PrivateDirectMessage } from './kinds.ts' -import { getPublicKey } from './pure' -import * as nip59 from './nip59' +import { EventTemplate, getPublicKey } from './pure.ts' +import * as nip59 from './nip59.ts' type Recipient = { publicKey: string @@ -17,10 +17,11 @@ function createEvent( message: string, conversationTitle?: string, replyTo?: ReplyTo, -) { - const baseEvent = { +): EventTemplate { + const baseEvent: EventTemplate = { + created_at: Math.ceil(Date.now() / 1000), kind: PrivateDirectMessage, - tags: [] as (string | string[])[], + tags: [], content: message, } diff --git a/nip59.test.ts b/nip59.test.ts index 2863248..fba153f 100644 --- a/nip59.test.ts +++ b/nip59.test.ts @@ -1,7 +1,10 @@ import { test, expect } from 'bun:test' -import { wrapEvent, wrapManyEvents, unwrapEvent, unwrapManyEvents, getWrappedEvents } from './nip59.ts' +import { wrapEvent, wrapManyEvents, unwrapEvent, unwrapManyEvents } from './nip59.ts' import { decode } from './nip19.ts' -import { getPublicKey } from './pure.ts' +import { NostrEvent, getPublicKey } from './pure.ts' +import { SimplePool } from './pool.ts' +import { GiftWrap } from './kinds.ts' +import { hexToBytes } from '@noble/hashes/utils' const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data @@ -97,9 +100,11 @@ test('getWrappedEvents and unwrapManyEvents', async () => { }, ] const relays = ['wss://relay.damus.io', 'wss://nos.lol'] - const privateKey = '582c3e7902c10c84d1cfe899a102e56bde628972d58d63011163ce0cdf4279b6' + const privateKey = hexToBytes('582c3e7902c10c84d1cfe899a102e56bde628972d58d63011163ce0cdf4279b6') const publicKey = '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa' - const wrappedEvents = await getWrappedEvents(publicKey, relays) + + const pool = new SimplePool() + const wrappedEvents: NostrEvent[] = await pool.querySync(relays, { kinds: [GiftWrap], '#p': [publicKey] }) const unwrappedEvents = unwrapManyEvents(wrappedEvents, privateKey) unwrappedEvents.forEach((event, index) => { diff --git a/nip59.ts b/nip59.ts index 63a07d4..4cbd76e 100644 --- a/nip59.ts +++ b/nip59.ts @@ -2,7 +2,6 @@ import { EventTemplate, UnsignedEvent, Event } from './core.ts' import { getConversationKey, decrypt, encrypt } from './nip44.ts' import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts' import { Seal, GiftWrap } from './kinds.ts' -import { SimplePool } from './pool' type Rumor = UnsignedEvent & { id: string } @@ -86,13 +85,13 @@ export function wrapManyEvents( return wrappeds } -export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array) { +export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array): Rumor { const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey) return nip44Decrypt(unwrappedSeal, recipientPrivateKey) } -export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array) { - let unwrappedEvents = [] +export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array): Rumor[] { + let unwrappedEvents: Rumor[] = [] wrappedEvents.forEach(e => { unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey)) @@ -102,17 +101,3 @@ export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Ui return unwrappedEvents } - -export async function getWrappedEvents(pubKey: string, relays: string[] = []): Promise { - const pool = new SimplePool() - - try { - const events: Event[] = await pool.querySync(relays, { kinds: [GiftWrap], '#p': [pubKey] }) - pool.close(relays) - - return events - } catch (error) { - console.error('Failed to:', error) - return undefined - } -}