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

Commit

Permalink
Set invoice labels after payment and invoice creation
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jan 16, 2024
1 parent 5014401 commit 1baa89e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 63 deletions.
22 changes: 17 additions & 5 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,10 @@ impl<S: MutinyStorage> MutinyWallet<S> {
.pay_invoice(inv.clone(), labels.clone())
.await;
match payment_result {
Ok(r) => return Ok(r),
Ok(r) => {
self.storage.set_invoice_labels(inv.clone(), labels)?;
return Ok(r);
}
Err(e) => match e {
MutinyError::PaymentTimeout => return Err(e),
MutinyError::RoutingFailed => {
Expand Down Expand Up @@ -859,9 +862,12 @@ impl<S: MutinyStorage> MutinyWallet<S> {
.sum::<u64>()
> 0
{
self.node_manager
.pay_invoice(None, inv, amt_sats, labels)
.await
let res = self
.node_manager
.pay_invoice(None, inv, amt_sats, labels.clone())
.await?;
self.storage.set_invoice_labels(inv.clone(), labels)?;
Ok(res)
} else {
Err(last_federation_error.unwrap_or(MutinyError::InsufficientBalance))
}
Expand Down Expand Up @@ -938,13 +944,19 @@ impl<S: MutinyStorage> MutinyWallet<S> {
.get_invoice(amount.unwrap_or_default(), labels.clone())
.await
{
self.storage
.set_invoice_labels(inv.bolt11.clone().expect("just created"), labels)?;
return Ok(inv);
}
}
}

// Fallback to node_manager invoice creation if no federation invoice created
self.node_manager.create_invoice(amount, labels).await
let inv = self.node_manager.create_invoice(amount).await?;
self.storage
.set_invoice_labels(inv.bolt11.clone().expect("just created"), labels)?;

Ok(inv)
}

/// Gets the current balance of the wallet.
Expand Down
51 changes: 8 additions & 43 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,6 @@ impl<S: MutinyStorage> Node<S> {
pub async fn create_invoice(
&self,
amount_sat: Option<u64>,
labels: Vec<String>,
route_hints: Option<Vec<PhantomRouteHints>>,
) -> Result<Bolt11Invoice, MutinyError> {
match self.lsp_client.as_ref() {
Expand Down Expand Up @@ -1034,7 +1033,6 @@ impl<S: MutinyStorage> Node<S> {
.create_internal_invoice(
Some(amount_minus_fee),
Some(lsp_fee.fee_amount_msat),
labels,
route_hints,
)
.await?;
Expand Down Expand Up @@ -1064,12 +1062,7 @@ impl<S: MutinyStorage> Node<S> {
AnyLsp::Lsps(client) => {
if has_inbound_capacity {
Ok(self
.create_internal_invoice(
Some(amount_sat),
None,
labels,
route_hints,
)
.create_internal_invoice(Some(amount_sat), None, route_hints)
.await?)
} else {
let lsp_invoice = match client
Expand All @@ -1085,7 +1078,6 @@ impl<S: MutinyStorage> Node<S> {
invoice.clone(),
Some(amount_sat * 1_000),
Some(lsp_fee.fee_amount_msat),
labels.clone(),
)
.await?;

Expand All @@ -1102,7 +1094,7 @@ impl<S: MutinyStorage> Node<S> {
}
}
None => Ok(self
.create_internal_invoice(amount_sat, None, labels, route_hints)
.create_internal_invoice(amount_sat, None, route_hints)
.await?),
}
}
Expand All @@ -1111,7 +1103,6 @@ impl<S: MutinyStorage> Node<S> {
&self,
amount_sat: Option<u64>,
fee_amount_msat: Option<u64>,
labels: Vec<String>,
route_hints: Option<Vec<PhantomRouteHints>>,
) -> Result<Bolt11Invoice, MutinyError> {
let amount_msat = amount_sat.map(|s| s * 1_000);
Expand Down Expand Up @@ -1166,7 +1157,7 @@ impl<S: MutinyStorage> Node<S> {
MutinyError::InvoiceCreationFailed
})?;

self.save_invoice_payment_info(invoice.clone(), amount_msat, fee_amount_msat, labels)
self.save_invoice_payment_info(invoice.clone(), amount_msat, fee_amount_msat)
.await?;

log_info!(self.logger, "SUCCESS: generated invoice: {invoice}");
Expand All @@ -1179,7 +1170,6 @@ impl<S: MutinyStorage> Node<S> {
invoice: Bolt11Invoice,
amount_msat: Option<u64>,
fee_amount_msat: Option<u64>,
labels: Vec<String>,
) -> Result<(), MutinyError> {
let last_update = utils::now().as_secs();
let payment_hash = PaymentHash(invoice.payment_hash().into_inner());
Expand All @@ -1200,8 +1190,6 @@ impl<S: MutinyStorage> Node<S> {
MutinyError::InvoiceCreationFailed
})?;

self.persister.storage.set_invoice_labels(invoice, labels)?;

Ok(())
}

Expand Down Expand Up @@ -1313,7 +1301,6 @@ impl<S: MutinyStorage> Node<S> {
&self,
invoice: &Bolt11Invoice,
amt_sats: Option<u64>,
labels: Vec<String>,
) -> Result<(PaymentId, PaymentHash), MutinyError> {
let payment_hash = invoice.payment_hash().as_inner();

Expand Down Expand Up @@ -1386,14 +1373,6 @@ impl<S: MutinyStorage> Node<S> {
)
};

if let Err(e) = self
.persister
.storage
.set_invoice_labels(invoice.clone(), labels)
{
log_error!(self.logger, "could not set invoice label: {e}");
}

let last_update = utils::now().as_secs();
let mut payment_info = PaymentInfo {
preimage: None,
Expand Down Expand Up @@ -1515,9 +1494,7 @@ impl<S: MutinyStorage> Node<S> {
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyError> {
// initiate payment
let (payment_id, payment_hash) = self
.init_invoice_payment(invoice, amt_sats, labels.clone())
.await?;
let (payment_id, payment_hash) = self.init_invoice_payment(invoice, amt_sats).await?;
let timeout: u64 = timeout_secs.unwrap_or(DEFAULT_PAYMENT_TIMEOUT);

self.await_payment(payment_id, payment_hash, timeout, labels)
Expand Down Expand Up @@ -2448,10 +2425,7 @@ mod tests {
let label = "test".to_string();
let labels = vec![label.clone()];

let invoice = node
.create_invoice(Some(amount_sats), labels.clone(), None)
.await
.unwrap();
let invoice = node.create_invoice(Some(amount_sats), None).await.unwrap();

assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
match invoice.description() {
Expand Down Expand Up @@ -2495,10 +2469,7 @@ mod tests {
let storage = MemoryStorage::default();
let node = create_node(storage).await;

let invoice = node
.create_invoice(Some(10_000), vec![], None)
.await
.unwrap();
let invoice = node.create_invoice(Some(10_000), None).await.unwrap();

let result = node
.pay_invoice_with_timeout(&invoice, None, None, vec![])
Expand Down Expand Up @@ -2612,10 +2583,7 @@ mod wasm_test {
let label = "test".to_string();
let labels = vec![label.clone()];

let invoice = node
.create_invoice(Some(amount_sats), labels.clone(), None)
.await
.unwrap();
let invoice = node.create_invoice(Some(amount_sats), None).await.unwrap();

assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
match invoice.description() {
Expand Down Expand Up @@ -2659,10 +2627,7 @@ mod wasm_test {
let storage = MemoryStorage::default();
let node = create_node(storage).await;

let invoice = node
.create_invoice(Some(10_000), vec![], None)
.await
.unwrap();
let invoice = node.create_invoice(Some(10_000), None).await.unwrap();

let result = node
.pay_invoice_with_timeout(&invoice, None, None, vec![])
Expand Down
10 changes: 2 additions & 8 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,11 +1505,7 @@ impl<S: MutinyStorage> NodeManager<S> {
///
/// If the manager has more than one node it will create a phantom invoice.
/// If there is only one node it will create an invoice just for that node.
pub async fn create_invoice(
&self,
amount: Option<u64>,
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyError> {
pub async fn create_invoice(&self, amount: Option<u64>) -> Result<MutinyInvoice, MutinyError> {
let nodes = self.nodes.lock().await;
let use_phantom = nodes.len() > 1 && self.lsp_config.is_none();
if nodes.len() == 0 {
Expand All @@ -1532,9 +1528,7 @@ impl<S: MutinyStorage> NodeManager<S> {
} else {
return Err(MutinyError::WalletOperationFailed);
};
let invoice = first_node
.create_invoice(amount, labels, route_hints)
.await?;
let invoice = first_node.create_invoice(amount, route_hints).await?;

Ok(invoice.into())
}
Expand Down
9 changes: 2 additions & 7 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use mutiny_core::nostr::nwc::{BudgetedSpendingConditions, NwcProfileTag, Spendin
use mutiny_core::storage::{DeviceLock, MutinyStorage, DEVICE_LOCK_KEY};
use mutiny_core::utils::{now, parse_npub, sleep};
use mutiny_core::vss::MutinyVssClient;
use mutiny_core::{encrypt::encryption_key_from_pass, MutinyWalletConfigBuilder};
use mutiny_core::{encrypt::encryption_key_from_pass, InvoiceHandler, MutinyWalletConfigBuilder};
use mutiny_core::{labels::Contact, MutinyWalletBuilder};
use mutiny_core::{
labels::LabelStorage,
Expand Down Expand Up @@ -732,12 +732,7 @@ impl MutinyWallet {
amount: Option<u64>,
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyJsError> {
Ok(self
.inner
.node_manager
.create_invoice(amount, labels)
.await?
.into())
Ok(self.inner.create_invoice(amount, labels).await?.into())
}

/// Pays a lightning invoice from the selected node.
Expand Down

0 comments on commit 1baa89e

Please sign in to comment.