11import { test , expect } from 'bun:test'
2- import { wrapEvent , unwrapEvent } from './nip59.ts'
2+ import { wrapEvent , wrapManyEvents , unwrapEvent , unwrapManyEvents , getWrappedEvents } from './nip59.ts'
33import { decode } from './nip19.ts'
44import { 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
1616test ( '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+
3365test ( '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+ } )
0 commit comments