From 48a4857ac857801bcf6d703e95de87de2dcb0933 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 24 Feb 2023 18:12:20 +0100 Subject: [PATCH] Use nostr-ots without default feautres, avoid ureq dep By providing a Stamper trait impl with the http library already in the dep tree --- crates/nostr/Cargo.toml | 2 +- crates/nostr/src/event/mod.rs | 45 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/nostr/Cargo.toml b/crates/nostr/Cargo.toml index 8921a787e..939281400 100644 --- a/crates/nostr/Cargo.toml +++ b/crates/nostr/Cargo.toml @@ -35,7 +35,7 @@ bitcoin = { version = "0.29", optional = true } bitcoin_hashes = { version = "0.11", features = ["serde"] } cbc = { version = "0.1", features = ["alloc"], optional = true } log = { version = "0.4", optional = true } -nostr-ots = "0.2" +nostr-ots = { git="https://github.com/RCasatta/nostr-ots", branch="ureq_optional", default-features = false } reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls-webpki-roots", "socks"], optional = true } secp256k1 = { version = "0.24", features = ["global-context", "rand-std", "serde"] } serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/crates/nostr/src/event/mod.rs b/crates/nostr/src/event/mod.rs index 2802c8e7a..eeb9953e4 100644 --- a/crates/nostr/src/event/mod.rs +++ b/crates/nostr/src/event/mod.rs @@ -94,14 +94,57 @@ impl Event { serde_json::json!(self).to_string() } + #[cfg(feature = "blocking")] /// Timestamp this event with OpenTimestamps, according to NIP-03 pub fn timestamp(&mut self) -> Result<(), Error> { - let ots = nostr_ots::timestamp_event(&self.id.to_hex())?; + use nostr_ots::Stamper; + let stamper = StamperClient::new(std::time::Duration::from_secs(5)).unwrap(); + let options = nostr_ots::Options::with_stamper(stamper); + let ots = nostr_ots::timestamp_event_with_options(&self.id.to_hex(), &options)?; self.ots = Some(ots); Ok(()) } } +#[cfg(feature = "blocking")] + +struct StamperClient(reqwest::blocking::Client); + +#[cfg(feature = "blocking")] + +impl nostr_ots::Stamper for StamperClient { + fn new(timeout: std::time::Duration) -> Result { + Ok(Self( + reqwest::blocking::ClientBuilder::new() + .timeout(timeout) + .build() + .map_err(|e| e.to_string())?, + )) + } + + fn stamp( + &self, + digest_endpoint: &str, + digest: &[u8], + ) -> Result, nostr_ots::StamperError> { + let resp = self + .0 + .post(digest_endpoint) + .body(digest.to_vec()) + .send() + .map_err(|e| nostr_ots::StamperError::Transport(e.to_string()))?; + if resp.status() == 200 { + Ok(resp + .bytes() + .map_err(|e| nostr_ots::StamperError::Transport(e.to_string()))? + .slice(..) + .to_vec()) + } else { + Err(nostr_ots::StamperError::Status(resp.status().into())) + } + } +} + impl Event { /// This is just for serde sanity checking #[allow(dead_code)]