From f9febcce1e375fa537e788e0cf288adc284dee41 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 29 Dec 2025 16:30:15 -0600 Subject: [PATCH] Don't sign new state events if they match the old one, after all --- src/lib/git.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/git.ts b/src/lib/git.ts index 322f0be5..c98e3d19 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -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 = {