Skip to content

Commit

Permalink
fix types, imports and other stuff on nip17 and nip59.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 26, 2024
1 parent a72e471 commit e2ec7a4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
5 changes: 3 additions & 2 deletions nip17.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
11 changes: 6 additions & 5 deletions nip17.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
}

Expand Down
13 changes: 9 additions & 4 deletions nip59.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
21 changes: 3 additions & 18 deletions nip59.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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))
Expand All @@ -102,17 +101,3 @@ export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Ui

return unwrappedEvents
}

export async function getWrappedEvents(pubKey: string, relays: string[] = []): Promise<Event[] | undefined> {
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
}
}

0 comments on commit e2ec7a4

Please sign in to comment.