private-send-settlements.ts (2253B)
1 import { 2 privateGiftWrapAttemptEvidence, 3 type PrivateGiftWrapAttemptResult 4 } from '../nostr/private-pool.ts'; 5 import { privateRecordSnapshot } from './private-records.ts'; 6 import { loadPrivateRecord } from './private-storage.ts'; 7 import { 8 preparePrivateReceiptTransition, 9 commitPrivateReceiptTransition, 10 type PrivateReceiptCommitResult 11 } from './private-receipts.ts'; 12 // Late bookkeeping is bound to the original actual EVENT, owner and immutable 13 // ciphertexts. It performs no SDK work, resumes nothing and renews no ACK. 14 export async function reconcilePrivateSendAttempt( 15 result: PrivateGiftWrapAttemptResult 16 ): Promise<PrivateReceiptCommitResult> { 17 const original = privateGiftWrapAttemptEvidence(result); 18 if (!original || typeof window === 'undefined' || !navigator.locks?.request) 19 return { status: 'invalid' }; 20 try { 21 return await navigator.locks.request( 22 'harvestcircle:owner:' + original.owner, 23 { mode: 'exclusive' }, 24 async () => { 25 const loaded = await loadPrivateRecord( 26 original.repository, 27 'private_sends', 28 original.command 29 ); 30 if (!loaded.ok) return { status: loaded.reason }; 31 const row = privateRecordSnapshot( 32 loaded.value, 33 original.owner, 34 original.command 35 ); 36 if ( 37 !row || 38 row.family !== 'private_send_operation' || 39 JSON.stringify({ 40 owner: row.owner, 41 id: row.id, 42 peer: row.peer, 43 rumorHash: row.rumorHash, 44 createdAt: row.createdAt, 45 self: row.self, 46 peerArtifact: row.peerArtifact 47 }) !== original.pairWire 48 ) 49 return { status: 'conflict' }; 50 const transition = preparePrivateReceiptTransition( 51 loaded.value, 52 original.owner, 53 original.command, 54 original.receiptWire 55 ); 56 if (!transition.ok) 57 return { 58 status: transition.reason === 'conflict' ? 'conflict' : 'invalid' 59 }; 60 return commitPrivateReceiptTransition( 61 original.repository, 62 transition.value 63 ); 64 } 65 ); 66 } catch { 67 return { status: 'unknown_completion' }; 68 } 69 }