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

Disable mutiny plus profiles instead of delete #1173

Merged
merged 1 commit into from
May 9, 2024
Merged
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
74 changes: 34 additions & 40 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,8 @@ impl<S: MutinyStorage> MutinyWallet<S> {
self.ensure_mutiny_nwc_profile(subscription_client, autopay)
.await?;

// FIXME: switch the subscription from disabled to enabled if it was disabled
benthecarman marked this conversation as resolved.
Show resolved Hide resolved

self.check_blind_tokens();

Ok(())
Expand All @@ -2117,47 +2119,39 @@ impl<S: MutinyStorage> MutinyWallet<S> {
.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?;
}
}
Comment on lines -2155 to 2162
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's not needed. Why keep it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this in the front end to mark it as a subscription so we know to not display the nwc connection string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should already be marked correctly

ah okay, this was for fixing old profiles, that's fine then


Expand Down
47 changes: 45 additions & 2 deletions mutiny-core/src/nostr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
}

pub fn profiles(&self) -> Vec<NwcProfile> {
self.nwc
.read()
.unwrap()
.iter()
.map(|x| x.nwc_profile())
.collect()
}

pub fn active_profiles(&self) -> Vec<NwcProfile> {
self.nwc
.read()
.unwrap()
Expand All @@ -738,7 +747,8 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
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
{
Expand Down Expand Up @@ -836,6 +846,7 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {

// save to storage
{
log_info!(self.logger, "Saving nwc to storage");
let profiles = profiles
.iter()
.map(|x| x.profile.clone())
Expand Down Expand Up @@ -992,6 +1003,8 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
tag: NwcProfileTag,
commands: Vec<Method>,
) -> Result<NwcProfile, MutinyError> {
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)?;
Expand Down Expand Up @@ -1569,9 +1582,15 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
}

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::<Vec<_>>();
Expand All @@ -1582,6 +1601,30 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {
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::<Vec<_>>();

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,
Expand Down
8 changes: 8 additions & 0 deletions mutiny-wasm/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ pub struct NwcProfile {
nwc_uri: Option<String>,
tag: String,
label: Option<String>,
enabled: bool,
}

impl Serialize for NwcProfile {
Expand All @@ -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)
Expand Down Expand Up @@ -983,6 +985,11 @@ impl NwcProfile {
None
}
}

#[wasm_bindgen(getter)]
pub fn enabled(&self) -> bool {
self.enabled
}
}

impl From<nostr::nwc::NwcProfile> for NwcProfile {
Expand All @@ -1009,6 +1016,7 @@ impl From<nostr::nwc::NwcProfile> for NwcProfile {
nwc_uri: value.nwc_uri,
tag: value.tag.to_string(),
label: value.label,
enabled: value.enabled.unwrap_or(true),
}
}
}
Expand Down
Loading