inbox-policy.ts (3748B)
1 import { Relay } from 'applesauce-relay'; 2 import { PrivateKeySigner } from 'applesauce-signers'; 3 import { getEventHash, type NostrEvent } from 'applesauce-core/helpers'; 4 import { NEVER, firstValueFrom, filter, take, toArray } from 'rxjs'; 5 6 // Test-owned actual SDK cryptography/transport, never a product signer or relay. 7 const url = 'wss://inbox.example.org/'; 8 export async function makePolicyFixture() { 9 const sender = new PrivateKeySigner(), 10 recipient = new PrivateKeySigner(); 11 const owner = await sender.getPublicKey(), 12 peer = await recipient.getPublicKey(); 13 const now = Math.floor(Date.now() / 1000); 14 const rumorFields = { 15 kind: 14, 16 pubkey: owner, 17 created_at: now, 18 tags: [['p', peer]], 19 content: 'loopback policy fixture' 20 }; 21 const rumor = { ...rumorFields, id: getEventHash(rumorFields) }; 22 async function wrap(destination: string) { 23 const seal = await sender.signEvent({ 24 kind: 13, 25 created_at: now - 300, 26 tags: [], 27 content: await sender.nip44.encrypt(destination, JSON.stringify(rumor)) 28 }); 29 const disposable = new PrivateKeySigner(); 30 try { 31 return await disposable.signEvent({ 32 kind: 1059, 33 created_at: now - 48 * 3600 - 300, 34 tags: [['p', destination]], 35 content: await disposable.nip44.encrypt( 36 destination, 37 JSON.stringify(seal) 38 ) 39 }); 40 } finally { 41 disposable.key.fill(0); 42 } 43 } 44 const wraps = { recipient: await wrap(peer), archive: await wrap(owner) }; 45 async function client(signer: PrivateKeySigner, authenticate: boolean) { 46 const relay = new Relay(url, { 47 keepAlive: 0, 48 enablePing: false, 49 requestReconnect: 0, 50 publishRetry: { count: 0 } 51 }); 52 const hold = relay 53 .req(NEVER, { waitForAuth: false, reconnect: false, resubscribe: false }) 54 .subscribe(); 55 const challenge = await firstValueFrom( 56 relay.challenge$.pipe( 57 filter((v): v is string => typeof v === 'string'), 58 take(1) 59 ) 60 ); 61 if (authenticate) { 62 const event = await signer.signEvent({ 63 kind: 22242, 64 created_at: now, 65 tags: [ 66 ['relay', url], 67 ['challenge', challenge] 68 ], 69 content: '' 70 }); 71 const result = await relay.auth(event); 72 if (!result.ok) throw new Error('fixture AUTH refused'); 73 } 74 return { 75 relay, 76 close() { 77 hold.unsubscribe(); 78 relay.close(); 79 } 80 }; 81 } 82 const clients: Awaited<ReturnType<typeof client>>[] = []; 83 async function connect(which: 'sender' | 'recipient', authenticate = true) { 84 const c = await client( 85 which === 'sender' ? sender : recipient, 86 authenticate 87 ); 88 clients.push(c); 89 return c; 90 } 91 async function read(c: Awaited<ReturnType<typeof client>>, target: string) { 92 try { 93 const events = await firstValueFrom( 94 c.relay 95 .request( 96 { kinds: [1059], '#p': [target], limit: 200 }, 97 { 98 waitForAuth: false, 99 reconnect: false, 100 resubscribe: false, 101 timeout: 3600000 102 } 103 ) 104 .pipe(toArray()) 105 ); 106 return { status: 'complete', ids: events.map((e) => e.id) }; 107 } catch { 108 return { status: 'refused', ids: [] }; 109 } 110 } 111 async function write( 112 c: Awaited<ReturnType<typeof client>>, 113 event: NostrEvent 114 ) { 115 const result = await firstValueFrom(c.relay.event(event, 'EVENT')); 116 return { accepted: result.ok }; 117 } 118 return { 119 owner, 120 peer, 121 wraps, 122 connect, 123 read, 124 write, 125 close() { 126 for (const c of clients) c.close(); 127 sender.key.fill(0); 128 recipient.key.fill(0); 129 } 130 }; 131 }