lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

commit 2f9af736483e3913d97291063b047f2d9b1681fb
parent 4cf5c573dd202bc5c8106ba76d2b83a6ad4d519a
Author: triesap <tyson@radroots.org>
Date:   Thu, 19 Feb 2026 18:50:51 +0000

nostr-ndb: add optional giftwrap key processing


- add feature-gated giftwrap secret key registration methods
- add helper for hex key parsing with existing validation errors
- add feature-gated processing entry point using transaction snapshots
- add giftwrap feature tests for key validation and process execution

Diffstat:
Mnostr-ndb/src/ndb.rs | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+), 0 deletions(-)

diff --git a/nostr-ndb/src/ndb.rs b/nostr-ndb/src/ndb.rs @@ -61,6 +61,27 @@ impl RadrootsNostrNdb { self.ingest_event_json_with_source(json.as_str(), source) } + #[cfg(feature = "giftwrap")] + pub fn add_giftwrap_secret_key(&self, secret_key: [u8; 32]) -> bool { + self.inner.add_key(&secret_key) + } + + #[cfg(feature = "giftwrap")] + pub fn add_giftwrap_secret_key_hex( + &self, + secret_key_hex: &str, + ) -> Result<bool, RadrootsNostrNdbError> { + let secret_key = parse_hex_32(secret_key_hex, "secret_key")?; + Ok(self.add_giftwrap_secret_key(secret_key)) + } + + #[cfg(feature = "giftwrap")] + pub fn process_giftwraps(&self) -> Result<(), RadrootsNostrNdbError> { + let txn = nostrdb::Transaction::new(&self.inner)?; + self.inner.process_giftwraps(&txn); + Ok(()) + } + pub fn subscribe( &self, spec: &RadrootsNostrNdbSubscriptionSpec, @@ -386,4 +407,36 @@ mod tests { assert_eq!(profile.display_name.as_deref(), Some("Alice")); assert_eq!(profile.lud16.as_deref(), Some("alice@example.com")); } + + #[cfg(feature = "giftwrap")] + #[test] + fn giftwrap_secret_key_hex_validates_length() { + let tmp_dir = TempDir::new().expect("tempdir should open"); + let db_dir = tmp_dir.path().join("ndb"); + let config = RadrootsNostrNdbConfig::new(&db_dir); + let ndb = RadrootsNostrNdb::open(config).expect("database should open"); + + let result = ndb.add_giftwrap_secret_key_hex("abcd"); + assert!(matches!( + result, + Err(RadrootsNostrNdbError::InvalidHexLength { + field: "secret_key", + .. + }) + )); + } + + #[cfg(feature = "giftwrap")] + #[test] + fn giftwrap_process_flow_executes() { + let tmp_dir = TempDir::new().expect("tempdir should open"); + let db_dir = tmp_dir.path().join("ndb"); + let config = RadrootsNostrNdbConfig::new(&db_dir); + let ndb = RadrootsNostrNdb::open(config).expect("database should open"); + + let secret_key = [7u8; 32]; + let _ = ndb.add_giftwrap_secret_key(secret_key); + ndb.process_giftwraps() + .expect("giftwrap processing should run"); + } }