diff --git a/abstract-pool.ts b/abstract-pool.ts index f543cee..2b4f34c 100644 --- a/abstract-pool.ts +++ b/abstract-pool.ts @@ -23,7 +23,7 @@ export type SubscribeManyParams = Omit & { } export class AbstractSimplePool { - protected relays = new Map() + protected relays: Map = new Map() public seenOn: Map> = new Map() public trackRelays: boolean = false diff --git a/nip17.ts b/nip17.ts index d579207..63ae36c 100644 --- a/nip17.ts +++ b/nip17.ts @@ -1,5 +1,5 @@ import { PrivateDirectMessage } from './kinds.ts' -import { EventTemplate, getPublicKey } from './pure.ts' +import { EventTemplate, NostrEvent, getPublicKey } from './pure.ts' import * as nip59 from './nip59.ts' type Recipient = { @@ -48,7 +48,7 @@ export function wrapEvent( message: string, conversationTitle?: string, replyTo?: ReplyTo, -) { +): NostrEvent { const event = createEvent(recipient, message, conversationTitle, replyTo) return nip59.wrapEvent(event, senderPrivateKey, recipient.publicKey) } @@ -59,22 +59,17 @@ export function wrapManyEvents( message: string, conversationTitle?: string, replyTo?: ReplyTo, -) { +): NostrEvent[] { if (!recipients || recipients.length === 0) { throw new Error('At least one recipient is required.') } const senderPublicKey = getPublicKey(senderPrivateKey) - // Initialize the wrappeds array with the sender's own wrapped event - const wrappeds = [wrapEvent(senderPrivateKey, { publicKey: senderPublicKey }, message, conversationTitle, replyTo)] - - // Wrap the event for each recipient - recipients.forEach(recipient => { - wrappeds.push(wrapEvent(senderPrivateKey, recipient, message, conversationTitle, replyTo)) - }) - - return wrappeds + // wrap the event for the sender and then for each recipient + return [{ publicKey: senderPublicKey }, ...recipients].map(recipient => + wrapEvent(senderPrivateKey, recipient, message, conversationTitle, replyTo), + ) } export const unwrapEvent = nip59.unwrapEvent diff --git a/nip59.ts b/nip59.ts index 4cbd76e..3db94fa 100644 --- a/nip59.ts +++ b/nip59.ts @@ -1,4 +1,4 @@ -import { EventTemplate, UnsignedEvent, Event } from './core.ts' +import { EventTemplate, UnsignedEvent, NostrEvent } from './core.ts' import { getConversationKey, decrypt, encrypt } from './nip44.ts' import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts' import { Seal, GiftWrap } from './kinds.ts' @@ -15,10 +15,10 @@ const nip44ConversationKey = (privateKey: Uint8Array, publicKey: string) => getC const nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) => encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey)) -const nip44Decrypt = (data: Event, privateKey: Uint8Array) => +const nip44Decrypt = (data: NostrEvent, privateKey: Uint8Array) => JSON.parse(decrypt(data.content, nip44ConversationKey(privateKey, data.pubkey))) -export function createRumor(event: Partial, privateKey: Uint8Array) { +export function createRumor(event: Partial, privateKey: Uint8Array): Rumor { const rumor = { created_at: now(), content: '', @@ -32,7 +32,7 @@ export function createRumor(event: Partial, privateKey: Uint8Arra return rumor as Rumor } -export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublicKey: string) { +export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublicKey: string): NostrEvent { return finalizeEvent( { kind: Seal, @@ -41,10 +41,10 @@ export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublic tags: [], }, privateKey, - ) as Event + ) } -export function createWrap(seal: Event, recipientPublicKey: string) { +export function createWrap(seal: NostrEvent, recipientPublicKey: string): NostrEvent { const randomKey = generateSecretKey() return finalizeEvent( @@ -55,10 +55,14 @@ export function createWrap(seal: Event, recipientPublicKey: string) { tags: [['p', recipientPublicKey]], }, randomKey, - ) as Event + ) as NostrEvent } -export function wrapEvent(event: Partial, senderPrivateKey: Uint8Array, recipientPublicKey: string) { +export function wrapEvent( + event: Partial, + senderPrivateKey: Uint8Array, + recipientPublicKey: string, +): NostrEvent { const rumor = createRumor(event, senderPrivateKey) const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey) @@ -69,7 +73,7 @@ export function wrapManyEvents( event: Partial, senderPrivateKey: Uint8Array, recipientsPublicKeys: string[], -) { +): NostrEvent[] { if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) { throw new Error('At least one recipient is required.') } @@ -85,12 +89,12 @@ export function wrapManyEvents( return wrappeds } -export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array): Rumor { +export function unwrapEvent(wrap: NostrEvent, recipientPrivateKey: Uint8Array): Rumor { const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey) return nip44Decrypt(unwrappedSeal, recipientPrivateKey) } -export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array): Rumor[] { +export function unwrapManyEvents(wrappedEvents: NostrEvent[], recipientPrivateKey: Uint8Array): Rumor[] { let unwrappedEvents: Rumor[] = [] wrappedEvents.forEach(e => {