-
Notifications
You must be signed in to change notification settings - Fork 3
Proof of concept on replies in nostr #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09338bb
add proof of concept for sending orders and replying to them
ikripaka c62116c
fix warn with tokio runtime
ikripaka 7e2180b
improve cli interaction
ikripaka 392e817
improve cli
ikripaka cd08395
apply suggestions
ikripaka d758dfa
remove unused file
ikripaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <your_private_key> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <your_preferred_relays> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| pub mod env_parser; | ||
| pub mod logger; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [package] | ||
| name = "nostr_relay_connector" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| authors.workspace = true | ||
| readme.workspace = true | ||
|
|
||
| [dependencies] | ||
| tokio = { workspace = true, features = ["time"] } | ||
| futures-util = { workspace = true } | ||
| serde_json = { workspace = true } | ||
| anyhow = { workspace = true } | ||
| url = { workspace = true } | ||
| nostr = { workspace = true } | ||
| global_utils = { workspace = true } | ||
| nostr-sdk = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| tracing = { workspace = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum RelayClientError { | ||
| #[error("Failed to convert custom url to RelayURL, err: {err_msg}")] | ||
| FailedToConvertRelayUrl { err_msg: String }, | ||
| #[error("An error occurred in Nostr Client, err: {0}")] | ||
| NostrClientFailure(#[from] nostr_sdk::client::Error), | ||
| #[error("Relay Client requires for operation signature, add key to the Client")] | ||
| MissingSigner, | ||
| } | ||
|
|
||
| pub type Result<T> = std::result::Result<T, RelayClientError>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pub mod error; | ||
| pub mod relay_client; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| use crate::error::RelayClientError; | ||
| use nostr::prelude::*; | ||
| use nostr_sdk::pool::Output; | ||
| use nostr_sdk::prelude::Events; | ||
| use nostr_sdk::{Client, Relay, SubscribeAutoCloseOptions}; | ||
| use std::collections::HashMap; | ||
| use std::fmt::Debug; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use tracing::instrument; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct RelayClient { | ||
| client: Client, | ||
| timeout: Duration, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ClientConfig { | ||
| pub timeout: Duration, | ||
| } | ||
|
|
||
| impl RelayClient { | ||
| #[instrument(skip_all, level = "debug", err)] | ||
| pub async fn connect( | ||
| relay_urls: impl IntoIterator<Item = impl TryIntoUrl>, | ||
| keys: Option<impl IntoNostrSigner>, | ||
| client_config: ClientConfig, | ||
| ) -> crate::error::Result<Self> { | ||
| tracing::debug!(client_config = ?client_config, "Connecting to Nostr Relay Client(s)"); | ||
|
|
||
| let client = match keys { | ||
| None => Client::default(), | ||
| Some(keys) => { | ||
| let client = Client::new(keys); | ||
| client.automatic_authentication(true); | ||
| client | ||
| } | ||
| }; | ||
|
|
||
| for url in relay_urls { | ||
| let url = url | ||
| .try_into_url() | ||
| .map_err(|err| RelayClientError::FailedToConvertRelayUrl { | ||
| err_msg: format!("{:?}", err), | ||
| })?; | ||
| client.add_relay(url).await?; | ||
| } | ||
|
|
||
| client.connect().await; | ||
|
|
||
| Ok(Self { | ||
| client, | ||
| timeout: client_config.timeout, | ||
| }) | ||
| } | ||
|
|
||
| #[instrument(skip_all, level = "debug", ret)] | ||
| pub async fn req_and_wait(&self, filter: Filter) -> crate::error::Result<Events> { | ||
| tracing::debug!(filter = ?filter, "Requesting events with filter"); | ||
| let events = self.client.fetch_combined_events(filter, self.timeout).await?; | ||
| Ok(events) | ||
| } | ||
|
|
||
| #[instrument(skip_all, level = "debug", ret)] | ||
| pub async fn get_signer(&self) -> crate::error::Result<Arc<dyn NostrSigner>> { | ||
| if !self.client.has_signer().await { | ||
| return Err(RelayClientError::MissingSigner); | ||
| } | ||
| Ok(self.client.signer().await?) | ||
| } | ||
|
|
||
| #[instrument(skip_all, level = "debug", ret)] | ||
| pub async fn get_relays(&self) -> HashMap<RelayUrl, Relay> { | ||
| self.client.relays().await | ||
| } | ||
|
|
||
| #[instrument(skip_all, level = "debug", ret)] | ||
| pub async fn publish_event(&self, event: &Event) -> crate::error::Result<EventId> { | ||
| if !self.client.has_signer().await { | ||
| return Err(RelayClientError::MissingSigner); | ||
| } | ||
| let event_id = self.client.send_event(event).await?; | ||
| let event_id = Self::handle_relay_output(event_id)?; | ||
| Ok(event_id) | ||
| } | ||
|
|
||
| #[instrument(skip(self), level = "debug")] | ||
| pub async fn subscribe( | ||
| &self, | ||
| filter: Filter, | ||
| opts: Option<SubscribeAutoCloseOptions>, | ||
| ) -> crate::error::Result<SubscriptionId> { | ||
| Ok(self.client.subscribe(filter, opts).await?.val) | ||
| } | ||
|
|
||
| #[instrument(skip(self), level = "debug")] | ||
| pub async fn unsubscribe(&self, subscription_id: &SubscriptionId) { | ||
| self.client.unsubscribe(subscription_id).await; | ||
| } | ||
|
|
||
| #[instrument(skip_all, level = "debug", ret)] | ||
| pub async fn disconnect(&self) -> crate::error::Result<()> { | ||
| self.client.disconnect().await; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[instrument(level = "debug")] | ||
| fn handle_relay_output<T: Debug>(output: Output<T>) -> crate::error::Result<T> { | ||
| tracing::debug!(output = ?output, "Handling Relay output"); | ||
| Ok(output.val) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| use nostr::SignerError; | ||
| use nostr::filter::SingleLetterTagError; | ||
| use nostr_relay_connector::error::RelayClientError; | ||
|
|
||
| #[derive(thiserror::Error, Debug)] | ||
| pub enum RelayProcessorError { | ||
| #[error(transparent)] | ||
| RelayClient(#[from] RelayClientError), | ||
| #[error("Signer error: {0}")] | ||
| Signer(#[from] SignerError), | ||
| #[error("Single letter error: {0}")] | ||
| SingleLetterTag(#[from] SingleLetterTagError), | ||
| } | ||
|
|
||
| pub type Result<T> = std::result::Result<T, RelayProcessorError>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| pub(crate) mod get_events; | ||
| pub(crate) mod list_orders; | ||
| pub(crate) mod order_replies; | ||
| pub(crate) mod place_order; | ||
| pub(crate) mod reply_order; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| pub mod ids { | ||
| use nostr::{EventId, Filter}; | ||
| use nostr_relay_connector::relay_client::RelayClient; | ||
| use nostr_sdk::prelude::Events; | ||
| use std::collections::{BTreeMap, BTreeSet}; | ||
|
|
||
| pub async fn handle(client: &RelayClient, event_id: EventId) -> crate::error::Result<Events> { | ||
| let events = client | ||
| .req_and_wait(Filter { | ||
| ids: Some(BTreeSet::from([event_id])), | ||
| authors: None, | ||
| kinds: None, | ||
| search: None, | ||
| since: None, | ||
| until: None, | ||
| limit: None, | ||
| generic_tags: BTreeMap::default(), | ||
| }) | ||
| .await?; | ||
| Ok(events) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,34 @@ | ||
| #[warn(unused)] | ||
| pub fn handle() -> anyhow::Result<()> { | ||
| Ok(()) | ||
| use crate::types::{CustomKind, MakerOrderKind}; | ||
| use nostr::{Filter, Timestamp}; | ||
| use nostr_relay_connector::relay_client::RelayClient; | ||
| use nostr_sdk::prelude::Events; | ||
| use std::collections::{BTreeMap, BTreeSet}; | ||
|
|
||
| pub async fn handle(client: &RelayClient) -> crate::error::Result<Events> { | ||
| let events = client | ||
| .req_and_wait(Filter { | ||
| ids: None, | ||
| authors: None, | ||
| kinds: Some(BTreeSet::from([MakerOrderKind::get_kind()])), | ||
| search: None, | ||
| since: None, | ||
| until: None, | ||
| limit: None, | ||
| generic_tags: BTreeMap::default(), | ||
| }) | ||
| .await?; | ||
| let events = filter_expired_events(events); | ||
| Ok(events) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn filter_expired_events(events_to_filter: Events) -> Events { | ||
| let time_now = Timestamp::now(); | ||
| events_to_filter | ||
| .into_iter() | ||
| .filter(|x| match x.tags.expiration() { | ||
| None => false, | ||
| Some(t) => t.as_u64() > time_now.as_u64(), | ||
| }) | ||
| .collect() | ||
| } |
21 changes: 21 additions & 0 deletions
21
crates/nostr_relay_processor/src/handlers/order_replies.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use crate::types::{CustomKind, TakerOrderKind}; | ||
| use nostr::{EventId, Filter, SingleLetterTag}; | ||
| use nostr_relay_connector::relay_client::RelayClient; | ||
| use nostr_sdk::prelude::Events; | ||
| use std::collections::{BTreeMap, BTreeSet}; | ||
|
|
||
| pub async fn handle(client: &RelayClient, event_id: EventId) -> crate::error::Result<Events> { | ||
| let events = client | ||
| .req_and_wait(Filter { | ||
| ids: None, | ||
| authors: None, | ||
| kinds: Some(BTreeSet::from([TakerOrderKind::get_kind()])), | ||
| search: None, | ||
| since: None, | ||
| until: None, | ||
| limit: None, | ||
| generic_tags: BTreeMap::from([(SingleLetterTag::from_char('e')?, BTreeSet::from([event_id.to_string()]))]), | ||
| }) | ||
| .await?; | ||
| Ok(events) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,33 @@ | ||
| #[allow(unused)] | ||
| pub fn handle() -> anyhow::Result<()> { | ||
| Ok(()) | ||
| use crate::relay_processor::OrderPlaceEventTags; | ||
| use crate::types::{BLOCKSTREAM_MAKER_CONTENT, CustomKind, MAKER_EXPIRATION_TIME, MakerOrderKind}; | ||
| use nostr::{EventBuilder, EventId, Tag, TagKind, Timestamp}; | ||
| use nostr_relay_connector::relay_client::RelayClient; | ||
| use std::borrow::Cow; | ||
|
|
||
| pub async fn handle(client: &RelayClient, tags: OrderPlaceEventTags) -> crate::error::Result<EventId> { | ||
| let client_signer = client.get_signer().await?; | ||
| let client_pubkey = client_signer.get_public_key().await?; | ||
|
|
||
| let timestamp_now = Timestamp::now(); | ||
|
|
||
| let maker_order = EventBuilder::new(MakerOrderKind::get_kind(), BLOCKSTREAM_MAKER_CONTENT) | ||
| .tags([ | ||
| Tag::public_key(client_pubkey), | ||
| Tag::expiration(Timestamp::from(timestamp_now.as_u64() + MAKER_EXPIRATION_TIME)), | ||
| Tag::custom( | ||
| TagKind::Custom(Cow::from("compiler")), | ||
| [tags.compiler_name, tags.compiler_build_hash], | ||
| ), | ||
| Tag::custom(TagKind::Custom(Cow::from("asset_to_buy")), [tags.asset_to_buy]), | ||
| Tag::custom(TagKind::Custom(Cow::from("asset_to_sell")), [tags.asset_to_sell]), | ||
| Tag::custom(TagKind::Custom(Cow::from("price")), [tags.price.to_string()]), | ||
| ]) | ||
| .custom_created_at(timestamp_now); | ||
|
|
||
| let text_note = maker_order.build(client_pubkey); | ||
| let signed_event = client_signer.sign_event(text_note).await?; | ||
|
|
||
| let maker_order_event_id = client.publish_event(&signed_event).await?; | ||
|
|
||
| Ok(maker_order_event_id) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.