Skip to content

Commit

Permalink
Merge pull request #1128 from MutinyWallet/lots-o-tests
Browse files Browse the repository at this point in the history
Tests for lots new nostr functionality
  • Loading branch information
benthecarman authored Apr 12, 2024
2 parents 3d47049 + 2d5a078 commit 3606842
Show file tree
Hide file tree
Showing 5 changed files with 659 additions and 94 deletions.
36 changes: 25 additions & 11 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod test_utils;
pub use crate::gossip::{GOSSIP_SYNC_TIME_KEY, NETWORK_GRAPH_KEY, PROB_SCORER_KEY};
pub use crate::keymanager::generate_seed;
pub use crate::ldkstorage::{CHANNEL_CLOSURE_PREFIX, CHANNEL_MANAGER_KEY, MONITORS_PREFIX_KEY};
use crate::nostr::primal::{PrimalApi, PrimalClient};
use crate::storage::{
get_payment_hash_from_key, list_payment_info, persist_payment_info, update_nostr_contact_list,
IndexItem, MutinyStorage, DEVICE_ID_KEY, EXPECTED_NETWORK_KEY, NEED_FULL_SYNC_KEY,
Expand Down Expand Up @@ -109,7 +110,7 @@ use moksha_core::primitives::{
PostMeltQuoteBolt11Response,
};
use moksha_core::token::TokenV3;
use nostr_sdk::{NostrSigner, RelayPoolNotification};
use nostr_sdk::{Client, NostrSigner, RelayPoolNotification};
use reqwest::multipart::{Form, Part};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -848,15 +849,28 @@ impl<S: MutinyStorage> MutinyWalletBuilder<S> {
// start syncing node manager
NodeManager::start_sync(node_manager.clone());

let primal_client = PrimalClient::new(
config
.primal_url
.clone()
.unwrap_or("https://primal-cache.mutinywallet.com/api".to_string()),
);

let client = Client::default();

// create nostr manager
let nostr = Arc::new(NostrManager::from_mnemonic(
self.xprivkey,
self.nostr_key_source,
self.storage.clone(),
config.primal_url.clone(),
logger.clone(),
stop.clone(),
)?);
let nostr = Arc::new(
NostrManager::from_mnemonic(
self.xprivkey,
self.nostr_key_source,
self.storage.clone(),
primal_client,
client,
logger.clone(),
stop.clone(),
)
.await?,
);

// connect to relays when not in tests
#[cfg(not(test))]
Expand Down Expand Up @@ -1099,7 +1113,7 @@ pub struct MutinyWallet<S: MutinyStorage> {
config: MutinyWalletConfig,
pub(crate) storage: S,
pub node_manager: Arc<NodeManager<S>>,
pub nostr: Arc<NostrManager<S>>,
pub nostr: Arc<NostrManager<S, PrimalClient, nostr_sdk::Client>>,
pub federation_storage: Arc<RwLock<FederationStorage>>,
pub(crate) federations: Arc<RwLock<HashMap<FederationId, Arc<FederationClient<S>>>>>,
lnurl_client: Arc<LnUrlClient>,
Expand Down Expand Up @@ -1172,7 +1186,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
log_warn!(logger, "Failed to clear invalid NWC invoices: {e}");
}

let client = &nostr.client;
let client = nostr_sdk::Client::default();

client
.add_relays(nostr.get_relays())
Expand Down
109 changes: 109 additions & 0 deletions mutiny-core/src/nostr/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use nostr::{Event, EventBuilder, EventId, Filter, PublicKey, SubscriptionId};
use nostr_sdk::client::Error;
use nostr_sdk::{NostrSigner, SubscribeAutoCloseOptions};
use std::time::Duration;

#[cfg_attr(test, mockall::automock)]
pub trait NostrClient {
async fn add_relays(&self, relays: Vec<String>) -> nostr::Result<(), Error>;
async fn add_relay(&self, relay: &str) -> nostr::Result<bool, Error>;
async fn connect_relay(&self, relay: &str) -> nostr::Result<(), Error>;
async fn connect(&self);
async fn disconnect(&self) -> nostr::Result<(), Error>;

async fn sign_event_builder(&self, builder: EventBuilder) -> nostr::Result<Event, Error>;
async fn send_event_builder(&self, builder: EventBuilder) -> nostr::Result<EventId, Error>;
async fn send_event(&self, event: Event) -> nostr::Result<EventId, Error>;
async fn send_event_to(&self, urls: Vec<String>, event: Event)
-> nostr::Result<EventId, Error>;
async fn send_direct_msg(
&self,
receiver: PublicKey,
msg: String,
reply_to: Option<EventId>,
) -> nostr::Result<EventId, Error>;

async fn subscribe(
&self,
filters: Vec<Filter>,
opts: Option<SubscribeAutoCloseOptions>,
) -> SubscriptionId;
async fn get_events_of(
&self,
filters: Vec<Filter>,
timeout: Option<Duration>,
) -> Result<Vec<Event>, Error>;

async fn set_signer(&self, signer: Option<NostrSigner>);
}

impl NostrClient for nostr_sdk::Client {
async fn add_relays(&self, relays: Vec<String>) -> nostr::Result<(), Error> {
self.add_relays(relays).await
}

async fn add_relay(&self, relay: &str) -> nostr::Result<bool, Error> {
self.add_relay(relay).await
}

async fn connect_relay(&self, relay: &str) -> nostr::Result<(), Error> {
self.connect_relay(relay).await
}

async fn connect(&self) {
self.connect().await
}

async fn disconnect(&self) -> nostr::Result<(), Error> {
self.disconnect().await
}

async fn sign_event_builder(&self, builder: EventBuilder) -> nostr::Result<Event, Error> {
self.sign_event_builder(builder).await
}

async fn send_event_builder(&self, builder: EventBuilder) -> nostr::Result<EventId, Error> {
self.send_event_builder(builder).await
}

async fn send_event(&self, event: Event) -> nostr::Result<EventId, Error> {
self.send_event(event).await
}

async fn send_event_to(
&self,
urls: Vec<String>,
event: Event,
) -> nostr::Result<EventId, Error> {
self.send_event_to(urls, event).await
}

async fn send_direct_msg(
&self,
receiver: PublicKey,
msg: String,
reply_to: Option<EventId>,
) -> nostr::Result<EventId, Error> {
self.send_direct_msg(receiver, msg, reply_to).await
}

async fn subscribe(
&self,
filters: Vec<Filter>,
opts: Option<SubscribeAutoCloseOptions>,
) -> SubscriptionId {
self.subscribe(filters, opts).await
}

async fn get_events_of(
&self,
filters: Vec<Filter>,
timeout: Option<Duration>,
) -> Result<Vec<Event>, Error> {
self.get_events_of(filters, timeout).await
}

async fn set_signer(&self, signer: Option<NostrSigner>) {
self.set_signer(signer).await
}
}
Loading

0 comments on commit 3606842

Please sign in to comment.