From 64a86de9fc5ded51a1b5405223fc5dce16839fef Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 5 Nov 2025 12:50:03 +0000 Subject: Refactor: abstract announcement event creation into AuditClient helper - Add create_repo_announcement() method to AuditClient - Remove duplicate code from nip01_smoke.rs - Update grasp01_nostr_relay.rs to use centralized helper - All tests passing (GRASP-01: 4/18, NIP-01: 6/6) --- grasp-audit/src/client.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'grasp-audit/src/client.rs') diff --git a/grasp-audit/src/client.rs b/grasp-audit/src/client.rs index b80b59f..7706ee3 100644 --- a/grasp-audit/src/client.rs +++ b/grasp-audit/src/client.rs @@ -158,6 +158,49 @@ impl AuditClient { pub fn keys(&self) -> &Keys { &self.keys } + + /// Create a NIP-34 repository announcement event + /// + /// This helper creates a properly formatted NIP-34 announcement that will be + /// accepted by GRASP relays (which require events to list the relay in clone/relays tags). + /// + /// # Arguments + /// * `test_name` - Name of the test (used to create unique repo identifier) + /// + /// # Returns + /// A built and signed Event ready to be sent to the relay + pub async fn create_repo_announcement(&self, test_name: &str) -> Result { + // Get relay URL from client + let relay_url = self.client.relays().await + .keys() + .next() + .ok_or_else(|| anyhow!("No relay connected"))? + .to_string(); + + // Convert WebSocket URL to HTTP URL for clone tag + let http_url = relay_url + .replace("ws://", "http://") + .replace("wss://", "https://"); + + // Create unique repository identifier using UUID for consistency + let repo_id = format!("{}-{}", test_name, uuid::Uuid::new_v4()); + + // Get npub for clone URL + let npub = self.public_key().to_bech32() + .map_err(|e| anyhow!("Failed to convert public key to bech32 npub format: {}", e))?; + + // Build kind 30617 repository announcement + let event = self.event_builder(Kind::GitRepoAnnouncement, format!("Test repository for {}", test_name)) + .tag(Tag::identifier(&repo_id)) + .tag(Tag::custom(TagKind::custom("name"), vec![format!("{} Test Repository", test_name)])) + .tag(Tag::custom(TagKind::custom("description"), vec![format!("Repository for {} testing", test_name)])) + .tag(Tag::custom(TagKind::custom("clone"), vec![format!("{}/{}/{}.git", http_url, npub, repo_id)])) + .tag(Tag::custom(TagKind::custom("relays"), vec![relay_url.clone()])) + .build(self.keys()) + .map_err(|e| anyhow!("Failed to build repository announcement event: {}", e))?; + + Ok(event) + } } #[cfg(test)] -- cgit v1.2.3