From ddc58a2f1cf540a26152d79dc9b753d76f5ecaee Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 2 Jul 2023 22:56:32 -0400 Subject: [PATCH] feat: config sending dms on pay to relay signup --- config.toml | 5 +++++ src/config.rs | 17 +++++++++++++---- src/payment/mod.rs | 27 +++++++++++++++++++-------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/config.toml b/config.toml index e6db8d5..b4f7dcc 100644 --- a/config.toml +++ b/config.toml @@ -202,6 +202,9 @@ reject_future_seconds = 1800 # LNBits api secret #api_secret = "" +# Nostr direct message on signup +#direct_message=true + # Terms of service #terms_message = """ #This service (and supporting services) are provided "as is", without warranty of any kind, express or implied. @@ -223,4 +226,6 @@ reject_future_seconds = 1800 # Whether or not new sign ups should be allowed #sign_ups = false + +# optional if `direct_message=false` #secret_key = "" diff --git a/src/config.rs b/src/config.rs index 9d92d39..03cc243 100644 --- a/src/config.rs +++ b/src/config.rs @@ -93,8 +93,9 @@ pub struct PayToRelay { pub node_url: String, pub api_secret: String, pub terms_message: String, - pub sign_ups: bool, // allow new users to sign up to relay - pub secret_key: String, + pub sign_ups: bool, // allow new users to sign up to relay + pub direct_message: bool, // Send direct message to user with invoice and terms + pub secret_key: Option, pub processor: Processor, } @@ -243,7 +244,14 @@ impl Settings { // Should check that url is valid assert_ne!(settings.pay_to_relay.node_url, ""); assert_ne!(settings.pay_to_relay.terms_message, ""); - assert_ne!(settings.pay_to_relay.secret_key, ""); + + if settings.pay_to_relay.direct_message { + assert_ne!( + settings.pay_to_relay.secret_key, + Some("".to_string()) + ); + assert!(settings.pay_to_relay.secret_key.is_some()); + } } Ok(settings) @@ -306,7 +314,8 @@ impl Default for Settings { node_url: "".to_string(), api_secret: "".to_string(), sign_ups: false, - secret_key: "".to_string(), + direct_message: true, + secret_key: None, processor: Processor::LNBits, }, verified_users: VerifiedUsers { diff --git a/src/payment/mod.rs b/src/payment/mod.rs index a6d07b3..0158cf8 100644 --- a/src/payment/mod.rs +++ b/src/payment/mod.rs @@ -25,7 +25,7 @@ pub struct Payment { /// Settings settings: crate::config::Settings, // Nostr Keys - nostr_keys: Keys, + nostr_keys: Option, /// Payment Processor processor: Arc, } @@ -102,7 +102,11 @@ impl Payment { info!("Create payment handler"); // Create nostr key from sk string - let nostr_keys = Keys::from_sk_str(&settings.pay_to_relay.secret_key)?; + let nostr_keys = if let Some(secret_key) = &settings.pay_to_relay.secret_key { + Some(Keys::from_sk_str(secret_key)?) + } else { + None + }; // Create processor kind defined in settings let processor = match &settings.pay_to_relay.processor { @@ -193,6 +197,11 @@ impl Payment { pubkey: &str, invoice_info: &InvoiceInfo, ) -> Result<()> { + let nostr_keys = match &self.nostr_keys { + Some(key) => key, + None => return Err(Error::CustomError("Nostr key not defined".to_string())), + }; + // Create Nostr key from pk let key = Keys::from_pk_str(pubkey)?; @@ -200,16 +209,16 @@ impl Payment { // Event DM with terms of service let message_event: NostrEvent = EventBuilder::new_encrypted_direct_msg( - &self.nostr_keys, + nostr_keys, pubkey, &self.settings.pay_to_relay.terms_message, )? - .to_event(&self.nostr_keys)?; + .to_event(nostr_keys)?; // Event DM with invoice let invoice_event: NostrEvent = - EventBuilder::new_encrypted_direct_msg(&self.nostr_keys, pubkey, &invoice_info.bolt11)? - .to_event(&self.nostr_keys)?; + EventBuilder::new_encrypted_direct_msg(nostr_keys, pubkey, &invoice_info.bolt11)? + .to_event(nostr_keys)?; // Persist DM events to DB self.repo.write_event(&message_event.clone().into()).await?; @@ -246,8 +255,10 @@ impl Payment { .create_invoice_record(&key, invoice_info.clone()) .await?; - // Admission event invoice and terms to pubkey that is joining - self.send_admission_message(pubkey, &invoice_info).await?; + if self.settings.pay_to_relay.direct_message { + // Admission event invoice and terms to pubkey that is joining + self.send_admission_message(pubkey, &invoice_info).await?; + } Ok(invoice_info) }