Fix event ID changing issue in publishToRelay function
This commit is contained in:
parent
58d16dbe85
commit
fe233ee417
@ -82,50 +82,18 @@ export async function publishToRelay(event: NostrEvent, relayUrl: string): Promi
|
||||
reject(new Error(`Timed out connecting to relay: ${relayUrl}`));
|
||||
}, 10000);
|
||||
|
||||
// Create a standard formatted event to ensure it follows the relay protocol
|
||||
const standardEvent = {
|
||||
// Use the original event without modifying timestamp or re-signing
|
||||
// This preserves the original event ID and signature
|
||||
const eventToPublish = {
|
||||
id: event.id,
|
||||
pubkey: event.pubkey,
|
||||
created_at: Math.floor(Date.now() / 1000), // Always use current timestamp
|
||||
created_at: event.created_at,
|
||||
kind: Number(event.kind),
|
||||
tags: event.tags,
|
||||
content: event.content,
|
||||
sig: event.sig
|
||||
};
|
||||
|
||||
// Make sure all fields are in the correct format
|
||||
if (typeof standardEvent.created_at !== 'number') {
|
||||
standardEvent.created_at = Math.floor(Date.now() / 1000);
|
||||
}
|
||||
|
||||
if (typeof standardEvent.kind !== 'number') {
|
||||
standardEvent.kind = 21120;
|
||||
}
|
||||
|
||||
// Since we updated the timestamp, we need to recalculate the event ID and get a new signature
|
||||
// First create an unsigned event
|
||||
const unsignedEvent = {
|
||||
kind: standardEvent.kind,
|
||||
created_at: standardEvent.created_at,
|
||||
tags: standardEvent.tags,
|
||||
content: standardEvent.content,
|
||||
pubkey: standardEvent.pubkey
|
||||
};
|
||||
|
||||
// Get a new signature using the extension
|
||||
if (!window.nostr) {
|
||||
reject(new Error('No Nostr extension available to sign the event'));
|
||||
return;
|
||||
}
|
||||
|
||||
let signedEvent;
|
||||
try {
|
||||
signedEvent = await window.nostr.signEvent(unsignedEvent);
|
||||
} catch (signError) {
|
||||
reject(new Error(`Failed to sign event with new timestamp: ${String(signError)}`));
|
||||
return;
|
||||
}
|
||||
|
||||
// Try direct WebSocket approach first
|
||||
let ws: WebSocket | null = null;
|
||||
let wsTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
@ -146,7 +114,7 @@ export async function publishToRelay(event: NostrEvent, relayUrl: string): Promi
|
||||
|
||||
ws.onopen = (): void => {
|
||||
// Send the event directly using the relay protocol format ["EVENT", event]
|
||||
const messageToSend = JSON.stringify(["EVENT", signedEvent]);
|
||||
const messageToSend = JSON.stringify(["EVENT", eventToPublish]);
|
||||
ws?.send(messageToSend);
|
||||
};
|
||||
|
||||
@ -199,7 +167,7 @@ export async function publishToRelay(event: NostrEvent, relayUrl: string): Promi
|
||||
console.log('WebSocket approach failed, trying nostr-tools:', wsError);
|
||||
|
||||
// Use the nostr-tools publish method as a fallback
|
||||
const publishPromises = relayPool.publish([relayUrl], signedEvent as nostrTools.Event);
|
||||
const publishPromises = relayPool.publish([relayUrl], eventToPublish as nostrTools.Event);
|
||||
|
||||
// Use Promise.all to wait for all promises to resolve
|
||||
Promise.all(publishPromises)
|
||||
|
Loading…
x
Reference in New Issue
Block a user