diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-19 11:55:32 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-19 15:43:29 +0000 |
| commit | fa065ad128882755f2a988d6203b59a2ab5e38ff (patch) | |
| tree | e8326de70a6e6ea56b5bf4250e0a00a3cda4afed /src/nostr/builder.rs | |
| parent | 98c6fa4bfa897ff0b8f9c95ea698d4d065b5e9f3 (diff) | |
add landing page and nostr-relay-builder relay on same port
Diffstat (limited to 'src/nostr/builder.rs')
| -rw-r--r-- | src/nostr/builder.rs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs new file mode 100644 index 0000000..cd1f4d2 --- /dev/null +++ b/src/nostr/builder.rs | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | /// Nostr Relay Builder Configuration | ||
| 2 | /// | ||
| 3 | /// This module integrates nostr-relay-builder with NIP-34 validation logic | ||
| 4 | /// preserved from the original implementation. | ||
| 5 | use std::net::SocketAddr; | ||
| 6 | use std::path::Path; | ||
| 7 | |||
| 8 | use nostr::nips::nip19::ToBech32; | ||
| 9 | use nostr_relay_builder::prelude::*; | ||
| 10 | |||
| 11 | use crate::config::Config; | ||
| 12 | use crate::nostr::events::{ | ||
| 13 | validate_announcement, validate_state, KIND_REPOSITORY_ANNOUNCEMENT, KIND_REPOSITORY_STATE, | ||
| 14 | }; | ||
| 15 | |||
| 16 | /// NIP-34 Write Policy | ||
| 17 | /// | ||
| 18 | /// Validates repository announcement and state events according to GRASP-01 spec. | ||
| 19 | /// Preserves all original validation logic from src/nostr/events.rs. | ||
| 20 | #[derive(Debug, Clone)] | ||
| 21 | pub struct Nip34WritePolicy { | ||
| 22 | domain: String, | ||
| 23 | } | ||
| 24 | |||
| 25 | impl Nip34WritePolicy { | ||
| 26 | pub fn new(domain: impl Into<String>) -> Self { | ||
| 27 | Self { | ||
| 28 | domain: domain.into(), | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | impl WritePolicy for Nip34WritePolicy { | ||
| 34 | fn admit_event<'a>( | ||
| 35 | &'a self, | ||
| 36 | event: &'a nostr_relay_builder::prelude::Event, | ||
| 37 | _addr: &'a SocketAddr, | ||
| 38 | ) -> BoxedFuture<'a, PolicyResult> { | ||
| 39 | Box::pin(async move { | ||
| 40 | match event.kind.as_u16() { | ||
| 41 | KIND_REPOSITORY_ANNOUNCEMENT => match validate_announcement(event, &self.domain) { | ||
| 42 | Ok(_) => { | ||
| 43 | tracing::debug!( | ||
| 44 | "Accepted repository announcement: {}", | ||
| 45 | event | ||
| 46 | .id | ||
| 47 | .to_bech32() | ||
| 48 | .unwrap_or_else(|_| "invalid".to_string()) | ||
| 49 | ); | ||
| 50 | PolicyResult::Accept | ||
| 51 | } | ||
| 52 | Err(e) => { | ||
| 53 | tracing::warn!( | ||
| 54 | "Rejected repository announcement {}: {}", | ||
| 55 | event | ||
| 56 | .id | ||
| 57 | .to_bech32() | ||
| 58 | .unwrap_or_else(|_| "invalid".to_string()), | ||
| 59 | e | ||
| 60 | ); | ||
| 61 | PolicyResult::Reject(e.to_string()) | ||
| 62 | } | ||
| 63 | }, | ||
| 64 | KIND_REPOSITORY_STATE => match validate_state(event) { | ||
| 65 | Ok(_) => { | ||
| 66 | tracing::debug!( | ||
| 67 | "Accepted repository state: {}", | ||
| 68 | event | ||
| 69 | .id | ||
| 70 | .to_bech32() | ||
| 71 | .unwrap_or_else(|_| "invalid".to_string()) | ||
| 72 | ); | ||
| 73 | PolicyResult::Accept | ||
| 74 | } | ||
| 75 | Err(e) => { | ||
| 76 | tracing::warn!( | ||
| 77 | "Rejected repository state {}: {}", | ||
| 78 | event | ||
| 79 | .id | ||
| 80 | .to_bech32() | ||
| 81 | .unwrap_or_else(|_| "invalid".to_string()), | ||
| 82 | e | ||
| 83 | ); | ||
| 84 | PolicyResult::Reject(e.to_string()) | ||
| 85 | } | ||
| 86 | }, | ||
| 87 | // Accept all other event kinds without validation | ||
| 88 | _ => PolicyResult::Accept, | ||
| 89 | } | ||
| 90 | }) | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | /// Create a configured LocalRelay with NIP-34 validation | ||
| 95 | pub fn create_relay(config: &Config) -> Result<LocalRelay> { | ||
| 96 | tracing::info!("Configuring nostr relay..."); | ||
| 97 | |||
| 98 | // Determine database path | ||
| 99 | let db_path = Path::new(&config.relay_data_path); | ||
| 100 | |||
| 101 | // Create database - using in-memory for now, can switch to persistent later | ||
| 102 | // TODO: Add configuration for NostrDB or LMDB backends | ||
| 103 | let database = MemoryDatabase::with_opts(MemoryDatabaseOptions { | ||
| 104 | events: true, | ||
| 105 | max_events: Some(100_000), | ||
| 106 | }); | ||
| 107 | |||
| 108 | tracing::info!("Using in-memory database (path: {})", db_path.display()); | ||
| 109 | |||
| 110 | // Build relay with NIP-34 validation | ||
| 111 | let builder = RelayBuilder::default() | ||
| 112 | .database(database) | ||
| 113 | .write_policy(Nip34WritePolicy::new(&config.domain)); | ||
| 114 | |||
| 115 | tracing::info!( | ||
| 116 | "Relay configured with NIP-34 validation for domain: {}", | ||
| 117 | config.domain | ||
| 118 | ); | ||
| 119 | |||
| 120 | Ok(LocalRelay::new(builder)) | ||
| 121 | } | ||