Don't sign new state events if they match the old one, after all

This commit is contained in:
Alex Gleason committed 2025-12-29 16:30:15 -06:00
1 parent 71f3a08325
commit f9febcce1e
1 file changed
+24 -7
+24 -7
View File
@@ -969,13 +969,20 @@ export class Git {
// Republish repo announcement event
await this.nostr.event(repo, { signal: AbortSignal.timeout(5000) });
// Construct the state event (kind 30618)
const stateEvent = await this.signer.signEvent({
kind: 30618,
content: '',
tags: stateTags,
created_at: Math.floor(Date.now() / 1000),
});
// If the new state matches the existing state, avoid an unnecessary signing operation
let stateEvent: NostrEvent;
if (state && this.areTagsEqual(state.tags, stateTags)) {
// Tags are identical, republish the existing state event
stateEvent = state;
} else {
// Tags have changed, sign a new state event
stateEvent = await this.signer.signEvent({
kind: 30618,
content: '',
tags: stateTags,
created_at: Math.floor(Date.now() / 1000),
});
}
// Publish the state event to Nostr
await this.nostr.event(stateEvent, { signal: AbortSignal.timeout(5000) });
@@ -1016,6 +1023,16 @@ export class Git {
}
}
/** Compare two tag arrays for equality by sorting and stringifying */
private areTagsEqual(tags1: string[][], tags2: string[][]): boolean {
// Sort both tag arrays for comparison
const sorted1 = [...tags1].sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
const sorted2 = [...tags2].sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
// Compare stringified versions
return JSON.stringify(sorted1) === JSON.stringify(sorted2);
}
/** Fetch NIP-34 repo announcement and state events from relays */
private async fetchRepoEvents(nostrURI: NostrURI): Promise<{ repo?: NostrEvent; state?: NostrEvent }> {
const filter = {