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

WIP: Using rust-nostr LocalRelay for testing #35

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
1 change: 1 addition & 0 deletions coordinator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde_json = { workspace = true }
rand = { workspace = true }
sha2 = { workspace = true }
env_logger = { workspace = true }
nostr-relay-builder = { workspace = true }

cashu_escrow_common = { path = "../common" }

27 changes: 24 additions & 3 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
mod escrow_coordinator;

use std::{env, str::FromStr};

use anyhow::Context;
use cashu_escrow_common::nostr::NostrClient;
use dotenvy::dotenv;
use escrow_coordinator::EscrowCoordinator;
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use nostr_relay_builder::{LocalRelay, RelayBuilder};
use nostr_sdk::{Keys, ToBech32};
use std::{env, str::FromStr};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -16,12 +17,19 @@ async fn main() -> anyhow::Result<()> {
.filter_module("cashu_escrow_coordinator", log::LevelFilter::Trace) // level for the application itself
.filter_level(log::LevelFilter::Info) // level for imported crates
.init();
let local_relay: LocalRelay; // has to stay in scope to keep the relay running

let keys = Keys::from_str(&env::var("ESCROW_NSEC")?)?;
let relays = env::var("NOSTR_RELAYS")?
let mut relays: Vec<String> = env::var("NOSTR_RELAYS")?
.split(',')
.filter(|s| !s.is_empty())
.map(String::from)
.collect();
if relays.is_empty() {
local_relay = run_local_relay().await?;
relays = vec![local_relay.url()]
};

let nostr_client = NostrClient::new(keys, relays).await?;
info!(
"Coordinator npub: {}",
Expand All @@ -30,3 +38,16 @@ async fn main() -> anyhow::Result<()> {
info!("Starting service and waiting for trades...");
return EscrowCoordinator::new(nostr_client)?.run().await;
}

/// Starts a in-memory mock relay for testing purposes
async fn run_local_relay() -> anyhow::Result<LocalRelay> {
let builder = RelayBuilder::default();
let local_relay = LocalRelay::run(builder)
.await
.context("Failed to start local relay")?;
warn!(
"Running in-memory relay at {}. Only for testing purposes",
local_relay.url()
);
Ok(local_relay)
}