Skip to content

Commit de7bbfc

Browse files
Anderson-Juhascfiatjaf
authored andcommitted
organizing and improving nip17 and nip59
1 parent f2d421f commit de7bbfc

File tree

4 files changed

+117
-69
lines changed

4 files changed

+117
-69
lines changed

nip17.test.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test, expect } from 'bun:test'
22
import { getPublicKey } from './pure.ts'
33
import { decode } from './nip19.ts'
4-
import { wrapEvent, wrapManyEvents, unwrapEvent, unwrapManyEvents, getWrappedEvents } from './nip17.ts'
4+
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
55

66
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
77

@@ -94,33 +94,3 @@ test('unwrapEvent', () => {
9494
expect(result.pubkey).toEqual(expected.pubkey)
9595
expect(result.tags).toEqual(expected.tags)
9696
})
97-
98-
test('getWrappedEvents and unwrapManyEvents', async () => {
99-
const expected = [
100-
{
101-
created_at: 1729721879,
102-
content: 'Hello!',
103-
tags: [['p', '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa']],
104-
kind: 14,
105-
pubkey: 'c0f56665e73eedc90b9565ecb34d961a2eb7ac1e2747899e4f73a813f940bc22',
106-
id: 'aee0a3e6487b2ac8c1851cc84f3ae0fca9af8a9bdad85c4ba5fdf45d3ee817c3',
107-
},
108-
{
109-
created_at: 1729722025,
110-
content: 'How are you?',
111-
tags: [['p', '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa']],
112-
kind: 14,
113-
pubkey: 'c0f56665e73eedc90b9565ecb34d961a2eb7ac1e2747899e4f73a813f940bc22',
114-
id: '212387ec5efee7d6eb20b747121e9fc1adb798de6c3185e932335bb1bcc61a77',
115-
},
116-
]
117-
const relays = ['wss://relay.damus.io', 'wss://nos.lol']
118-
const privateKey = '582c3e7902c10c84d1cfe899a102e56bde628972d58d63011163ce0cdf4279b6'
119-
const publicKey = '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa'
120-
const wrappedEvents = await getWrappedEvents(publicKey, relays)
121-
const unwrappedEvents = unwrapManyEvents(wrappedEvents, privateKey)
122-
123-
unwrappedEvents.forEach((event, index) => {
124-
expect(event).toEqual(expected[index])
125-
})
126-
})

nip17.ts

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Event } from './core.ts'
2-
import { PrivateDirectMessage, GiftWrap } from './kinds.ts'
1+
import { PrivateDirectMessage } from './kinds.ts'
32
import { getPublicKey } from './pure'
4-
import { SimplePool } from './pool'
53
import * as nip59 from './nip59'
64

75
type Recipient = {
@@ -50,10 +48,7 @@ export function wrapEvent(
5048
conversationTitle?: string,
5149
replyTo?: ReplyTo,
5250
) {
53-
// Create the event using createEvent
5451
const event = createEvent(recipient, message, conversationTitle, replyTo)
55-
56-
// Wrap the created event using nip59
5752
return nip59.wrapEvent(event, senderPrivateKey, recipient.publicKey)
5853
}
5954

@@ -70,7 +65,7 @@ export function wrapManyEvents(
7065

7166
const senderPublicKey = getPublicKey(senderPrivateKey)
7267

73-
// Initialize the wraps array with the sender's own wrapped event
68+
// Initialize the wrappeds array with the sender's own wrapped event
7469
const wrappeds = [wrapEvent(senderPrivateKey, { publicKey: senderPublicKey }, message, conversationTitle, replyTo)]
7570

7671
// Wrap the event for each recipient
@@ -81,32 +76,6 @@ export function wrapManyEvents(
8176
return wrappeds
8277
}
8378

84-
export function unwrapEvent(wrappedEvent: Event, recipientPrivateKey: Uint8Array) {
85-
return nip59.unwrapEvent(wrappedEvent, recipientPrivateKey)
86-
}
87-
88-
export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array) {
89-
let unwrappedEvents = []
90-
91-
wrappedEvents.forEach(e => {
92-
unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey))
93-
})
94-
95-
unwrappedEvents.sort((a, b) => a.created_at - b.created_at)
96-
97-
return unwrappedEvents
98-
}
99-
100-
export async function getWrappedEvents(pubKey: string, relays: string[] = []): Promise<Event[] | undefined> {
101-
const pool = new SimplePool()
79+
export const unwrapEvent = nip59.unwrapEvent
10280

103-
try {
104-
const events: Event[] = await pool.querySync(relays, { kinds: [GiftWrap], '#p': [pubKey] })
105-
pool.close(relays)
106-
107-
return events
108-
} catch (error) {
109-
console.error('Failed to:', error)
110-
return undefined
111-
}
112-
}
81+
export const unwrapManyEvents = nip59.unwrapManyEvents

nip59.test.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from 'bun:test'
2-
import { wrapEvent, unwrapEvent } from './nip59.ts'
2+
import { wrapEvent, wrapManyEvents, unwrapEvent, unwrapManyEvents, getWrappedEvents } from './nip59.ts'
33
import { decode } from './nip19.ts'
44
import { getPublicKey } from './pure.ts'
55

@@ -11,7 +11,7 @@ const event = {
1111
content: 'Are you going to the party tonight?',
1212
}
1313

14-
const wrapedEvent = wrapEvent(event, senderPrivateKey, recipientPublicKey)
14+
const wrappedEvent = wrapEvent(event, senderPrivateKey, recipientPublicKey)
1515

1616
test('wrapEvent', () => {
1717
const expected = {
@@ -30,17 +30,79 @@ test('wrapEvent', () => {
3030
expect(result.tags).toEqual(expected.tags)
3131
})
3232

33+
test('wrapManyEvent', () => {
34+
const expected = [
35+
{
36+
kind: 1059,
37+
content: '',
38+
created_at: 1729581521,
39+
tags: [['p', '611df01bfcf85c26ae65453b772d8f1dfd25c264621c0277e1fc1518686faef9']],
40+
pubkey: '',
41+
id: '',
42+
sig: '',
43+
[Symbol('verified')]: true,
44+
},
45+
{
46+
kind: 1059,
47+
content: '',
48+
created_at: 1729594619,
49+
tags: [['p', '166bf3765ebd1fc55decfe395beff2ea3b2a4e0a8946e7eb578512b555737c99']],
50+
pubkey: '',
51+
id: '',
52+
sig: '',
53+
[Symbol('verified')]: true,
54+
},
55+
]
56+
57+
const wrappedEvents = wrapManyEvents(event, senderPrivateKey, [recipientPublicKey])
58+
59+
wrappedEvents.forEach((event, index) => {
60+
expect(event.kind).toEqual(expected[index].kind)
61+
expect(event.tags).toEqual(expected[index].tags)
62+
})
63+
})
64+
3365
test('unwrapEvent', () => {
3466
const expected = {
3567
kind: 1,
3668
content: 'Are you going to the party tonight?',
3769
pubkey: '611df01bfcf85c26ae65453b772d8f1dfd25c264621c0277e1fc1518686faef9',
3870
tags: [],
3971
}
40-
const result = unwrapEvent(wrapedEvent, recipientPrivateKey)
72+
const result = unwrapEvent(wrappedEvent, recipientPrivateKey)
4173

4274
expect(result.kind).toEqual(expected.kind)
4375
expect(result.content).toEqual(expected.content)
4476
expect(result.pubkey).toEqual(expected.pubkey)
4577
expect(result.tags).toEqual(expected.tags)
4678
})
79+
80+
test('getWrappedEvents and unwrapManyEvents', async () => {
81+
const expected = [
82+
{
83+
created_at: 1729721879,
84+
content: 'Hello!',
85+
tags: [['p', '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa']],
86+
kind: 14,
87+
pubkey: 'c0f56665e73eedc90b9565ecb34d961a2eb7ac1e2747899e4f73a813f940bc22',
88+
id: 'aee0a3e6487b2ac8c1851cc84f3ae0fca9af8a9bdad85c4ba5fdf45d3ee817c3',
89+
},
90+
{
91+
created_at: 1729722025,
92+
content: 'How are you?',
93+
tags: [['p', '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa']],
94+
kind: 14,
95+
pubkey: 'c0f56665e73eedc90b9565ecb34d961a2eb7ac1e2747899e4f73a813f940bc22',
96+
id: '212387ec5efee7d6eb20b747121e9fc1adb798de6c3185e932335bb1bcc61a77',
97+
},
98+
]
99+
const relays = ['wss://relay.damus.io', 'wss://nos.lol']
100+
const privateKey = '582c3e7902c10c84d1cfe899a102e56bde628972d58d63011163ce0cdf4279b6'
101+
const publicKey = '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa'
102+
const wrappedEvents = await getWrappedEvents(publicKey, relays)
103+
const unwrappedEvents = unwrapManyEvents(wrappedEvents, privateKey)
104+
105+
unwrappedEvents.forEach((event, index) => {
106+
expect(event).toEqual(expected[index])
107+
})
108+
})

nip59.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventTemplate, UnsignedEvent, Event } from './core.ts'
22
import { getConversationKey, decrypt, encrypt } from './nip44.ts'
33
import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'
44
import { Seal, GiftWrap } from './kinds.ts'
5+
import { SimplePool } from './pool'
56

67
type Rumor = UnsignedEvent & { id: string }
78

@@ -65,7 +66,53 @@ export function wrapEvent(event: Partial<UnsignedEvent>, senderPrivateKey: Uint8
6566
return createWrap(seal, recipientPublicKey)
6667
}
6768

69+
export function wrapManyEvents(
70+
event: Partial<UnsignedEvent>,
71+
senderPrivateKey: Uint8Array,
72+
recipientsPublicKeys: string[],
73+
) {
74+
if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) {
75+
throw new Error('At least one recipient is required.')
76+
}
77+
78+
const senderPublicKey = getPublicKey(senderPrivateKey)
79+
80+
const wrappeds = [wrapEvent(event, senderPrivateKey, senderPublicKey)]
81+
82+
recipientsPublicKeys.forEach(recipientPublicKey => {
83+
wrappeds.push(wrapEvent(event, senderPrivateKey, recipientPublicKey))
84+
})
85+
86+
return wrappeds
87+
}
88+
6889
export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array) {
6990
const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)
7091
return nip44Decrypt(unwrappedSeal, recipientPrivateKey)
7192
}
93+
94+
export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array) {
95+
let unwrappedEvents = []
96+
97+
wrappedEvents.forEach(e => {
98+
unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey))
99+
})
100+
101+
unwrappedEvents.sort((a, b) => a.created_at - b.created_at)
102+
103+
return unwrappedEvents
104+
}
105+
106+
export async function getWrappedEvents(pubKey: string, relays: string[] = []): Promise<Event[] | undefined> {
107+
const pool = new SimplePool()
108+
109+
try {
110+
const events: Event[] = await pool.querySync(relays, { kinds: [GiftWrap], '#p': [pubKey] })
111+
pool.close(relays)
112+
113+
return events
114+
} catch (error) {
115+
console.error('Failed to:', error)
116+
return undefined
117+
}
118+
}

0 commit comments

Comments
 (0)