Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 81832a7

Browse files
Disable mutiny plus profiles instead of delete
1 parent 256876a commit 81832a7

File tree

3 files changed

+87
-42
lines changed

3 files changed

+87
-42
lines changed

mutiny-core/src/lib.rs

+34-40
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,8 @@ impl<S: MutinyStorage> MutinyWallet<S> {
20982098
self.ensure_mutiny_nwc_profile(subscription_client, autopay)
20992099
.await?;
21002100

2101+
// FIXME: switch the subscription from disabled to enabled if it was disabled
2102+
21012103
self.check_blind_tokens();
21022104

21032105
Ok(())
@@ -2117,47 +2119,39 @@ impl<S: MutinyStorage> MutinyWallet<S> {
21172119
.iter()
21182120
.find(|profile| profile.index == reserved_profile_index);
21192121

2120-
match profile_opt {
2121-
None => {
2122-
// profile with the reserved index does not exist, create a new one
2123-
let nwc = if autopay {
2124-
self.nostr
2125-
.create_new_nwc_profile(
2126-
ProfileType::Reserved(ReservedProfile::MutinySubscription),
2127-
SpendingConditions::Budget(BudgetedSpendingConditions {
2128-
budget: 21_000,
2129-
single_max: None,
2130-
payments: vec![],
2131-
period: BudgetPeriod::Month,
2132-
}),
2133-
NwcProfileTag::Subscription,
2134-
vec![Method::PayInvoice], // subscription only needs pay invoice
2135-
)
2136-
.await?
2137-
.nwc_uri
2138-
} else {
2139-
self.nostr
2140-
.create_new_nwc_profile(
2141-
ProfileType::Reserved(ReservedProfile::MutinySubscription),
2142-
SpendingConditions::RequireApproval,
2143-
NwcProfileTag::Subscription,
2144-
vec![Method::PayInvoice], // subscription only needs pay invoice
2145-
)
2146-
.await?
2147-
.nwc_uri
2148-
};
2122+
if profile_opt.is_none() {
2123+
log_debug!(self.logger, "Did not find a mutiny+ nwc profile");
2124+
// profile with the reserved index does not exist, create a new one
2125+
let nwc = if autopay {
2126+
self.nostr
2127+
.create_new_nwc_profile(
2128+
ProfileType::Reserved(ReservedProfile::MutinySubscription),
2129+
SpendingConditions::Budget(BudgetedSpendingConditions {
2130+
budget: 21_000,
2131+
single_max: None,
2132+
payments: vec![],
2133+
period: BudgetPeriod::Month,
2134+
}),
2135+
NwcProfileTag::Subscription,
2136+
vec![Method::PayInvoice], // subscription only needs pay invoice
2137+
)
2138+
.await?
2139+
.nwc_uri
2140+
} else {
2141+
self.nostr
2142+
.create_new_nwc_profile(
2143+
ProfileType::Reserved(ReservedProfile::MutinySubscription),
2144+
SpendingConditions::RequireApproval,
2145+
NwcProfileTag::Subscription,
2146+
vec![Method::PayInvoice], // subscription only needs pay invoice
2147+
)
2148+
.await?
2149+
.nwc_uri
2150+
};
21492151

2150-
if let Some(nwc) = nwc {
2151-
// only should have to submit the NWC if never created locally before
2152-
subscription_client.submit_nwc(nwc).await?;
2153-
}
2154-
}
2155-
Some(profile) => {
2156-
if profile.tag != NwcProfileTag::Subscription {
2157-
let mut nwc = profile.clone();
2158-
nwc.tag = NwcProfileTag::Subscription;
2159-
self.nostr.edit_nwc_profile(nwc)?;
2160-
}
2152+
if let Some(nwc) = nwc {
2153+
// only should have to submit the NWC if never created locally before
2154+
subscription_client.submit_nwc(nwc).await?;
21612155
}
21622156
}
21632157

mutiny-core/src/nostr/mod.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,15 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
726726
}
727727

728728
pub fn profiles(&self) -> Vec<NwcProfile> {
729+
self.nwc
730+
.read()
731+
.unwrap()
732+
.iter()
733+
.map(|x| x.nwc_profile())
734+
.collect()
735+
}
736+
737+
pub fn active_profiles(&self) -> Vec<NwcProfile> {
729738
self.nwc
730739
.read()
731740
.unwrap()
@@ -738,7 +747,8 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
738747
pub(crate) fn remove_inactive_profiles(&self) -> Result<(), MutinyError> {
739748
let mut profiles = self.nwc.write().unwrap();
740749

741-
profiles.retain(|x| x.profile.active());
750+
let mutiny_plus_index = ReservedProfile::MutinySubscription.info().1;
751+
profiles.retain(|x| x.profile.active() || x.profile.index == mutiny_plus_index);
742752

743753
// save to storage
744754
{
@@ -836,6 +846,7 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
836846

837847
// save to storage
838848
{
849+
log_info!(self.logger, "Saving nwc to storage");
839850
let profiles = profiles
840851
.iter()
841852
.map(|x| x.profile.clone())
@@ -992,6 +1003,8 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
9921003
tag: NwcProfileTag,
9931004
commands: Vec<Method>,
9941005
) -> Result<NwcProfile, MutinyError> {
1006+
log_info!(self.logger, "Creating new internal nwc profile");
1007+
9951008
let mut profiles = self.nwc.try_write()?;
9961009

9971010
let (name, index, child_key_index) = get_next_nwc_index(profile_type, &profiles)?;
@@ -1569,9 +1582,15 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
15691582
}
15701583

15711584
pub fn delete_nwc_profile(&self, index: u32) -> Result<(), MutinyError> {
1572-
let mut vec = self.nwc.write().unwrap();
1585+
log_info!(self.logger, "Deleting nwc profile: {index}");
1586+
1587+
// TODO don't delete mutiny+ profile
1588+
if index == ReservedProfile::MutinySubscription.info().1 {
1589+
return self.disable_mutiny_plus_profile();
1590+
}
15731591

15741592
// update the profile
1593+
let mut vec = self.nwc.write().unwrap();
15751594
vec.retain(|x| x.profile.index != index);
15761595

15771596
let profiles = vec.iter().map(|x| x.profile.clone()).collect::<Vec<_>>();
@@ -1582,6 +1601,30 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
15821601
Ok(())
15831602
}
15841603

1604+
pub fn disable_mutiny_plus_profile(&self) -> Result<(), MutinyError> {
1605+
log_info!(self.logger, "Disabling mutiny+ subscription");
1606+
1607+
let mut vec = self.nwc.write().unwrap();
1608+
1609+
let profile_opt = vec
1610+
.iter_mut()
1611+
.find(|p| p.profile.index == ReservedProfile::MutinySubscription.info().1);
1612+
1613+
match profile_opt {
1614+
Some(p) => {
1615+
p.profile.enabled = Some(false);
1616+
1617+
let profiles = vec.iter().map(|x| x.profile.clone()).collect::<Vec<_>>();
1618+
1619+
self.storage
1620+
.set_data(NWC_STORAGE_KEY.to_string(), profiles, None)?;
1621+
1622+
Ok(())
1623+
}
1624+
None => Err(MutinyError::NotFound),
1625+
}
1626+
}
1627+
15851628
pub async fn claim_single_use_nwc(
15861629
&self,
15871630
amount_sats: u64,

mutiny-wasm/src/models.rs

+8
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ pub struct NwcProfile {
846846
nwc_uri: Option<String>,
847847
tag: String,
848848
label: Option<String>,
849+
enabled: bool,
849850
}
850851

851852
impl Serialize for NwcProfile {
@@ -869,6 +870,7 @@ impl Serialize for NwcProfile {
869870
"active_payments": self._active_payments(),
870871
"spending_conditions_type": self.spending_conditions_type(),
871872
"url_suffix": self.url_suffix(),
873+
"enabled": self.enabled(),
872874
});
873875

874876
json.serialize(serializer)
@@ -983,6 +985,11 @@ impl NwcProfile {
983985
None
984986
}
985987
}
988+
989+
#[wasm_bindgen(getter)]
990+
pub fn enabled(&self) -> bool {
991+
self.enabled.clone()
992+
}
986993
}
987994

988995
impl From<nostr::nwc::NwcProfile> for NwcProfile {
@@ -1009,6 +1016,7 @@ impl From<nostr::nwc::NwcProfile> for NwcProfile {
10091016
nwc_uri: value.nwc_uri,
10101017
tag: value.tag.to_string(),
10111018
label: value.label,
1019+
enabled: value.enabled.unwrap_or(true),
10121020
}
10131021
}
10141022
}

0 commit comments

Comments
 (0)