fix: processing gift wraps and notifications

This commit is contained in:
enes 2024-09-06 18:01:49 +02:00
parent 7b64bc523a
commit c09837ea20

View File

@ -341,12 +341,6 @@ export const createWrap = (unsignedEvent: UnsignedEvent, receiver: string) => {
// Check if the leading zero bits meet the required difficulty
if (leadingZeroes >= difficulty) {
// Finalize the event (sign it) and return the result
console.log(
'event OUT: ',
unsignedEvent,
' for this user ',
hexToNpub(receiver)
)
return finalizeEvent(event, randomKey)
}
@ -532,18 +526,13 @@ export const updateUsersAppData = async (meta: Meta) => {
if (existingMeta.modifiedAt < meta.modifiedAt) {
sigits[id] = meta
isUpdated = true
} else {
console.log('event NEWER EXISTS', existingMeta, meta)
}
} else {
sigits[id] = meta
isUpdated = true
}
if (!isUpdated) {
console.log('event NOT UPDATED', sigits[id], meta)
return null
}
if (!isUpdated) return null
const blossomUrls = [...appData.blossomUrls]
@ -870,9 +859,16 @@ export const subscribeForSigits = async (pubkey: string) => {
'#p': [pubkey]
}
relayController.subscribeForEvents(filter, relaySet.read, (event) => {
processReceivedEvent(event) // Process the received event
})
// Process the received event synchronously
const events = await relayController.fetchEvents(filter, relaySet.read)
for (const e of events) {
await processReceivedEvent(e)
}
// Async processing of the events has a race condition
// relayController.subscribeForEvents(filter, relaySet.read, (event) => {
// processReceivedEvent(event)
// })
}
const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
@ -880,13 +876,13 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
.processedGiftWraps
if (processedEvents.includes(event.id)) return
// Consider the gift wrap as processed immediately
store.dispatch(updateProcessedGiftWraps([...processedEvents, event.id]))
// validate PoW
// Count the number of leading zero bits in the hash
const leadingZeroes = countLeadingZeroes(event.id)
if (leadingZeroes < difficulty) {
console.log('processReceivedEvent leading zeroes error')
return
}
if (leadingZeroes < difficulty) return
// decrypt the content of gift wrap event
const nostrController = NostrController.getInstance()
@ -904,21 +900,8 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
}
)
if (!internalUnsignedEvent || internalUnsignedEvent.kind !== 938) {
console.log(
'processReceivedEvent missing internal unsigned event, or internal event not sigit kind'
)
return
}
if (!internalUnsignedEvent || internalUnsignedEvent.kind !== 938) return
console.log(
'event IN: ',
new Date(internalUnsignedEvent.created_at * 1000),
internalUnsignedEvent.kind,
internalUnsignedEvent.pubkey,
hexToNpub(internalUnsignedEvent.pubkey),
internalUnsignedEvent.content
)
const meta = await parseJson<Meta>(internalUnsignedEvent.content).catch(
(err) => {
console.log(
@ -932,7 +915,6 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
if (!meta) return
await updateUsersAppData(meta)
store.dispatch(updateProcessedGiftWraps([...processedEvents, event.id]))
}
/**