Skip to content

Commit

Permalink
fix slow types.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 26, 2024
1 parent d3fc473 commit ab7eaa3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion abstract-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose' | 'id'> & {
}

export class AbstractSimplePool {
protected relays = new Map<string, AbstractRelay>()
protected relays: Map<string, AbstractRelay> = new Map()
public seenOn: Map<string, Set<AbstractRelay>> = new Map()
public trackRelays: boolean = false

Expand Down
19 changes: 7 additions & 12 deletions nip17.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
26 changes: 15 additions & 11 deletions nip59.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<UnsignedEvent>, privateKey: Uint8Array) {
export function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array): Rumor {
const rumor = {
created_at: now(),
content: '',
Expand All @@ -32,7 +32,7 @@ export function createRumor(event: Partial<UnsignedEvent>, 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,
Expand All @@ -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(
Expand All @@ -55,10 +55,14 @@ export function createWrap(seal: Event, recipientPublicKey: string) {
tags: [['p', recipientPublicKey]],
},
randomKey,
) as Event
) as NostrEvent
}

export function wrapEvent(event: Partial<UnsignedEvent>, senderPrivateKey: Uint8Array, recipientPublicKey: string) {
export function wrapEvent(
event: Partial<UnsignedEvent>,
senderPrivateKey: Uint8Array,
recipientPublicKey: string,
): NostrEvent {
const rumor = createRumor(event, senderPrivateKey)

const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey)
Expand All @@ -69,7 +73,7 @@ export function wrapManyEvents(
event: Partial<UnsignedEvent>,
senderPrivateKey: Uint8Array,
recipientsPublicKeys: string[],
) {
): NostrEvent[] {
if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) {
throw new Error('At least one recipient is required.')
}
Expand All @@ -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 => {
Expand Down

0 comments on commit ab7eaa3

Please sign in to comment.