resume-routing.ts (7974B)
1 import { makeFixture as recoveryFixture } from './resume-preparation.ts'; 2 import { finalizeEvent, getPublicKey } from 'applesauce-core/helpers'; 3 import { 4 readRelayPolicy, 5 validateRelayPolicy 6 } from '../../../src/lib/config/relays.ts'; 7 import { 8 createPublicScheduler, 9 createPublicRun, 10 openInboxRequest, 11 closePublicScheduler 12 } from '../../../src/lib/nostr/request-scope.ts'; 13 import { createInboxResolver } from '../../../src/lib/messaging/resolve-inbox.ts'; 14 import { createInboxRoutePlan } from '../../../src/lib/messaging/inbox-routing.ts'; 15 import { 16 createPrivateSession, 17 closePrivateSession 18 } from '../../../src/lib/runtime/private-session.ts'; 19 import { 20 getPrivatePool, 21 closePrivatePool 22 } from '../../../src/lib/nostr/private-pool.ts'; 23 import { 24 resumeEncryptedPreparation, 25 qualifyRecoveredPrivatePair, 26 stopResumePreparation 27 } from '../../../src/lib/messaging/resume-preparation.ts'; 28 import { 29 capturePrivateRetry, 30 runPrivateRetry, 31 type PrivateRetry 32 } from '../../../src/lib/messaging/retry-send.ts'; 33 import { 34 capturePrivateResumeRouting, 35 privateResumeRoutingSnapshot, 36 approvePrivateResumeRouting, 37 type PrivateResumeRouting 38 } from '../../../src/lib/messaging/resume-routing.ts'; 39 import type { PairedDeliveryAcknowledgement } from '../../../src/lib/persistence/private-sends.ts'; 40 import type { PairedDeliveryContext } from '../../../src/lib/messaging/prepare-send.ts'; 41 export async function makeFixture() { 42 const f = await recoveryFixture(); 43 const origins = [ 44 'wss://peer.example.org', 45 'wss://archive.example.org', 46 'wss://new-peer.example.org' 47 ]; 48 const policy = validateRelayPolicy( 49 JSON.stringify({ 50 ...readRelayPolicy(f.context.policy), 51 messagingEnabled: true, 52 inbox: origins.map((origin) => ({ origin, read: true, write: true })) 53 }) 54 ); 55 if (!policy) throw Error('missing qualified fixture policy'); 56 const plan = createInboxRoutePlan( 57 policy, 58 f.owner, 59 f.peer, 60 f.context.own, 61 f.context.other 62 ); 63 if (!plan) throw Error('missing original route'); 64 const original = { ...f.context, policy, plan }; 65 const session = await createPrivateSession( 66 f.identity, 67 'reviewed_private_session' 68 ); 69 if (!session) throw Error('missing private session'); 70 const pool = getPrivatePool(session, policy, origins); 71 if (!pool) throw Error('missing private pool'); 72 const resume = f.capture(); 73 await resumeEncryptedPreparation(resume, original, 'reviewed_private_resume'); 74 const prepared = await qualifyRecoveredPrivatePair( 75 resume, 76 original, 77 'reviewed_stored_private_retry' 78 ); 79 if (!('receipt' in prepared)) throw Error('missing genuine pair'); 80 const receipt: PairedDeliveryAcknowledgement = prepared.receipt; 81 const schedulers: ReturnType<typeof createPublicScheduler>[] = []; 82 let context: PairedDeliveryContext = original, 83 token: PrivateResumeRouting | undefined, 84 heldRetry: PrivateRetry | undefined, 85 current = true; 86 let restore = () => {}; 87 function resolve( 88 origin: string, 89 role: 'peer' | 'self_archive', 90 peerSwitch = false, 91 complete = true 92 ) { 93 const key = new Uint8Array(32).fill( 94 role === 'self_archive' ? 83 : peerSwitch ? 85 : 84 95 ); 96 let event; 97 try { 98 event = finalizeEvent( 99 { 100 kind: 10050, 101 created_at: 102 origin === 'wss://peer.example.org' || 103 origin === 'wss://archive.example.org' 104 ? 100 105 : 101, 106 tags: [['relay', origin]], 107 content: '' 108 }, 109 key 110 ); 111 } finally { 112 key.fill(0); 113 } 114 const scheduler = createPublicScheduler({ 115 now: () => 0, 116 schedule: () => () => {} 117 }); 118 schedulers.push(scheduler); 119 const run = createPublicRun(scheduler, policy!); 120 return createInboxResolver( 121 run, 122 event.pubkey, 123 (next) => 124 openInboxRequest( 125 run, 126 event.pubkey, 127 (sink) => { 128 sink({ 129 type: 'EVENT', 130 from: 'wss://discovery.example.org', 131 id: 'routing-fixture', 132 event 133 }); 134 if (complete) 135 sink({ 136 type: 'EOSE', 137 from: 'wss://discovery.example.org', 138 id: 'routing-fixture' 139 }); 140 return () => {}; 141 }, 142 next 143 ), 144 () => current 145 ); 146 } 147 return { 148 ...f, 149 capture( 150 destination = 'wss://new-peer.example.org', 151 archive = 'wss://archive.example.org', 152 peerSwitch = false, 153 complete = true 154 ) { 155 const own = resolve(archive, 'self_archive'), 156 other = resolve(destination, 'peer', peerSwitch, complete); 157 const alternate = new Uint8Array(32).fill(85); 158 let intended; 159 try { 160 intended = peerSwitch ? getPublicKey(alternate) : f.peer; 161 } finally { 162 alternate.fill(0); 163 } 164 const next = createInboxRoutePlan(policy, f.owner, intended, own, other); 165 token = capturePrivateResumeRouting( 166 f.repository, 167 f.identity, 168 receipt, 169 { policy, own, other }, 170 'reviewed_private_route_resolution' 171 ); 172 // A changed recipient cannot produce a reviewed same-peer update. 173 if (!next) { 174 return false; 175 } 176 context = { policy, plan: next, own, other }; 177 return !!token; 178 }, 179 snapshot: () => token && privateResumeRoutingSnapshot(token), 180 async approve(review: unknown = 'reviewed_private_destination_update') { 181 if (!token) return { status: 'invalid' as const }; 182 try { 183 return await approvePrivateResumeRouting(token, review); 184 } finally { 185 restore(); 186 } 187 }, 188 async retry(old = false) { 189 const retry = capturePrivateRetry( 190 f.repository, 191 session, 192 receipt, 193 old ? original : context, 194 pool, 195 'reviewed_private_retry' 196 ); 197 return retry 198 ? runPrivateRetry(retry, 'reviewed_private_retry') 199 : { status: 'invalid' as const }; 200 }, 201 captureHeld() { 202 heldRetry = capturePrivateRetry( 203 f.repository, 204 session, 205 receipt, 206 original, 207 pool, 208 'reviewed_private_retry' 209 ); 210 return !!heldRetry; 211 }, 212 runHeld() { 213 return heldRetry 214 ? runPrivateRetry(heldRetry, 'reviewed_private_retry') 215 : Promise.resolve({ status: 'invalid' as const }); 216 }, 217 stale() { 218 current = false; 219 }, 220 fault(kind: 'abort' | 'readback') { 221 const put = Reflect.get<IDBObjectStore, 'put'>( 222 IDBObjectStore.prototype, 223 'put' 224 ), 225 cursor = Reflect.get<IDBIndex, 'openCursor'>( 226 IDBIndex.prototype, 227 'openCursor' 228 ); 229 let armed = false; 230 IDBObjectStore.prototype.put = function (value, key) { 231 const request = 232 key === undefined 233 ? put.call(this, value) 234 : put.call(this, value, key); 235 if (this.name === 'private_sends') { 236 armed = true; 237 if (kind === 'abort') this.transaction.abort(); 238 } 239 return request; 240 }; 241 IDBIndex.prototype.openCursor = function (...args) { 242 if ( 243 armed && 244 kind === 'readback' && 245 this.objectStore.name === 'private_sends' && 246 this.objectStore.transaction.mode === 'readonly' 247 ) 248 throw new DOMException( 249 'fixture actual post-put readback failure', 250 'InvalidStateError' 251 ); 252 return cursor.apply(this, args); 253 }; 254 restore = () => { 255 IDBObjectStore.prototype.put = put; 256 IDBIndex.prototype.openCursor = cursor; 257 }; 258 }, 259 close() { 260 restore(); 261 current = false; 262 for (const s of schedulers) closePublicScheduler(s); 263 stopResumePreparation(resume); 264 closePrivatePool(pool); 265 closePrivateSession(session); 266 f.close(); 267 } 268 }; 269 }