Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nostr-ots without default feautres, avoid ureq dep #53

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/nostr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
45 changes: 44 additions & 1 deletion crates/nostr/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, String> {
Ok(Self(
reqwest::blocking::ClientBuilder::new()
.timeout(timeout)
.build()
.map_err(|e| e.to_string())?,
))
}

fn stamp(
&self,
digest_endpoint: &str,
digest: &[u8],
) -> Result<Vec<u8>, 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)]
Expand Down