diff --git a/mutiny-core/src/lib.rs b/mutiny-core/src/lib.rs index 102af45a0..b49a9e770 100644 --- a/mutiny-core/src/lib.rs +++ b/mutiny-core/src/lib.rs @@ -2098,6 +2098,8 @@ impl MutinyWallet { self.ensure_mutiny_nwc_profile(subscription_client, autopay) .await?; + // FIXME: switch the subscription from disabled to enabled if it was disabled + self.check_blind_tokens(); Ok(()) @@ -2117,47 +2119,39 @@ impl MutinyWallet { .iter() .find(|profile| profile.index == reserved_profile_index); - match profile_opt { - None => { - // profile with the reserved index does not exist, create a new one - let nwc = if autopay { - self.nostr - .create_new_nwc_profile( - ProfileType::Reserved(ReservedProfile::MutinySubscription), - SpendingConditions::Budget(BudgetedSpendingConditions { - budget: 21_000, - single_max: None, - payments: vec![], - period: BudgetPeriod::Month, - }), - NwcProfileTag::Subscription, - vec![Method::PayInvoice], // subscription only needs pay invoice - ) - .await? - .nwc_uri - } else { - self.nostr - .create_new_nwc_profile( - ProfileType::Reserved(ReservedProfile::MutinySubscription), - SpendingConditions::RequireApproval, - NwcProfileTag::Subscription, - vec![Method::PayInvoice], // subscription only needs pay invoice - ) - .await? - .nwc_uri - }; + if profile_opt.is_none() { + log_debug!(self.logger, "Did not find a mutiny+ nwc profile"); + // profile with the reserved index does not exist, create a new one + let nwc = if autopay { + self.nostr + .create_new_nwc_profile( + ProfileType::Reserved(ReservedProfile::MutinySubscription), + SpendingConditions::Budget(BudgetedSpendingConditions { + budget: 21_000, + single_max: None, + payments: vec![], + period: BudgetPeriod::Month, + }), + NwcProfileTag::Subscription, + vec![Method::PayInvoice], // subscription only needs pay invoice + ) + .await? + .nwc_uri + } else { + self.nostr + .create_new_nwc_profile( + ProfileType::Reserved(ReservedProfile::MutinySubscription), + SpendingConditions::RequireApproval, + NwcProfileTag::Subscription, + vec![Method::PayInvoice], // subscription only needs pay invoice + ) + .await? + .nwc_uri + }; - if let Some(nwc) = nwc { - // only should have to submit the NWC if never created locally before - subscription_client.submit_nwc(nwc).await?; - } - } - Some(profile) => { - if profile.tag != NwcProfileTag::Subscription { - let mut nwc = profile.clone(); - nwc.tag = NwcProfileTag::Subscription; - self.nostr.edit_nwc_profile(nwc)?; - } + if let Some(nwc) = nwc { + // only should have to submit the NWC if never created locally before + subscription_client.submit_nwc(nwc).await?; } } diff --git a/mutiny-core/src/nostr/mod.rs b/mutiny-core/src/nostr/mod.rs index a96ec2834..5e5c7b06d 100644 --- a/mutiny-core/src/nostr/mod.rs +++ b/mutiny-core/src/nostr/mod.rs @@ -726,6 +726,15 @@ impl NostrManager { } pub fn profiles(&self) -> Vec { + self.nwc + .read() + .unwrap() + .iter() + .map(|x| x.nwc_profile()) + .collect() + } + + pub fn active_profiles(&self) -> Vec { self.nwc .read() .unwrap() @@ -738,7 +747,8 @@ impl NostrManager { pub(crate) fn remove_inactive_profiles(&self) -> Result<(), MutinyError> { let mut profiles = self.nwc.write().unwrap(); - profiles.retain(|x| x.profile.active()); + let mutiny_plus_index = ReservedProfile::MutinySubscription.info().1; + profiles.retain(|x| x.profile.active() || x.profile.index == mutiny_plus_index); // save to storage { @@ -836,6 +846,7 @@ impl NostrManager { // save to storage { + log_info!(self.logger, "Saving nwc to storage"); let profiles = profiles .iter() .map(|x| x.profile.clone()) @@ -992,6 +1003,8 @@ impl NostrManager { tag: NwcProfileTag, commands: Vec, ) -> Result { + log_info!(self.logger, "Creating new internal nwc profile"); + let mut profiles = self.nwc.try_write()?; let (name, index, child_key_index) = get_next_nwc_index(profile_type, &profiles)?; @@ -1569,9 +1582,15 @@ impl NostrManager { } pub fn delete_nwc_profile(&self, index: u32) -> Result<(), MutinyError> { - let mut vec = self.nwc.write().unwrap(); + log_info!(self.logger, "Deleting nwc profile: {index}"); + + // don't delete mutiny+ profile + if index == ReservedProfile::MutinySubscription.info().1 { + return self.disable_mutiny_plus_profile(); + } // update the profile + let mut vec = self.nwc.write().unwrap(); vec.retain(|x| x.profile.index != index); let profiles = vec.iter().map(|x| x.profile.clone()).collect::>(); @@ -1582,6 +1601,30 @@ impl NostrManager { Ok(()) } + pub fn disable_mutiny_plus_profile(&self) -> Result<(), MutinyError> { + log_info!(self.logger, "Disabling mutiny+ subscription"); + + let mut vec = self.nwc.write().unwrap(); + + let profile_opt = vec + .iter_mut() + .find(|p| p.profile.index == ReservedProfile::MutinySubscription.info().1); + + match profile_opt { + Some(p) => { + p.profile.enabled = Some(false); + + let profiles = vec.iter().map(|x| x.profile.clone()).collect::>(); + + self.storage + .set_data(NWC_STORAGE_KEY.to_string(), profiles, None)?; + + Ok(()) + } + None => Err(MutinyError::NotFound), + } + } + pub async fn claim_single_use_nwc( &self, amount_sats: u64, diff --git a/mutiny-wasm/src/models.rs b/mutiny-wasm/src/models.rs index 279632cf2..7bd9b79dc 100644 --- a/mutiny-wasm/src/models.rs +++ b/mutiny-wasm/src/models.rs @@ -846,6 +846,7 @@ pub struct NwcProfile { nwc_uri: Option, tag: String, label: Option, + enabled: bool, } impl Serialize for NwcProfile { @@ -869,6 +870,7 @@ impl Serialize for NwcProfile { "active_payments": self._active_payments(), "spending_conditions_type": self.spending_conditions_type(), "url_suffix": self.url_suffix(), + "enabled": self.enabled(), }); json.serialize(serializer) @@ -983,6 +985,11 @@ impl NwcProfile { None } } + + #[wasm_bindgen(getter)] + pub fn enabled(&self) -> bool { + self.enabled + } } impl From for NwcProfile { @@ -1009,6 +1016,7 @@ impl From for NwcProfile { nwc_uri: value.nwc_uri, tag: value.tag.to_string(), label: value.label, + enabled: value.enabled.unwrap_or(true), } } }