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

Commit e0553ec

Browse files
committed
Fix labeling
1 parent 6285c56 commit e0553ec

File tree

4 files changed

+73
-43
lines changed

4 files changed

+73
-43
lines changed

mutiny-core/src/lib.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -1204,18 +1204,16 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12041204
.get(federation_id)
12051205
.ok_or(MutinyError::NotFound)?;
12061206

1207+
let labels = vec![SWAP_LABEL.to_string()];
1208+
12071209
// if the user provided amount, this is easy
12081210
if let Some(amt) = amount {
1209-
let (inv, fee) = self.node_manager.create_invoice(amt).await?;
1210-
self.storage.set_invoice_labels(
1211-
inv.bolt11.clone().expect("just created"),
1212-
vec![SWAP_LABEL.to_string()],
1213-
)?;
1211+
let (inv, fee) = self
1212+
.node_manager
1213+
.create_invoice(amt, labels.clone())
1214+
.await?;
12141215
let pay_res = fedimint_client
1215-
.pay_invoice(
1216-
inv.bolt11.expect("create inv had one job"),
1217-
vec![SWAP_LABEL.to_string()],
1218-
)
1216+
.pay_invoice(inv.bolt11.expect("create inv had one job"), labels.clone())
12191217
.await?;
12201218
let total_fees_paid = pay_res.fees_paid.unwrap_or(0) + fee;
12211219

@@ -1239,7 +1237,10 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12391237
log_debug!(self.logger, "max spendable: {}", amt);
12401238

12411239
// try to get an invoice for this exact amount
1242-
let (inv, fee) = self.node_manager.create_invoice(amt).await?;
1240+
let (inv, fee) = self
1241+
.node_manager
1242+
.create_invoice(amt, labels.clone())
1243+
.await?;
12431244

12441245
// check if we can afford that invoice
12451246
let inv_amt = inv.amount_sats.ok_or(MutinyError::BadAmountError)?;
@@ -1253,22 +1254,15 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12531254
// if invoice amount changed, create a new invoice
12541255
let (inv_to_pay, fee) = if first_invoice_amount != inv_amt {
12551256
self.node_manager
1256-
.create_invoice(first_invoice_amount)
1257+
.create_invoice(first_invoice_amount, labels.clone())
12571258
.await?
12581259
} else {
12591260
(inv.clone(), fee)
12601261
};
1261-
self.storage.set_invoice_labels(
1262-
inv_to_pay.bolt11.clone().expect("just created"),
1263-
vec![SWAP_LABEL.to_string()],
1264-
)?;
12651262

12661263
log_debug!(self.logger, "attempting payment from fedimint client");
12671264
let first_invoice_res = fedimint_client
1268-
.pay_invoice(
1269-
inv_to_pay.bolt11.expect("create inv had one job"),
1270-
vec![SWAP_LABEL.to_string()],
1271-
)
1265+
.pay_invoice(inv_to_pay.bolt11.expect("create inv had one job"), labels)
12721266
.await?;
12731267

12741268
let remaining_balance = fedimint_client.get_balance().await?;
@@ -1362,9 +1356,8 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13621356
}
13631357

13641358
// Fallback to node_manager invoice creation if no federation invoice created
1365-
let (inv, _fee) = self.node_manager.create_invoice(amount).await?;
1366-
self.storage
1367-
.set_invoice_labels(inv.bolt11.clone().expect("just created"), labels)?;
1359+
let (inv, _fee) = self.node_manager.create_invoice(amount, labels).await?;
1360+
13681361
Ok(inv)
13691362
}
13701363

mutiny-core/src/node.rs

+42-8
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ impl<S: MutinyStorage> Node<S> {
10251025
&self,
10261026
amount_sat: u64,
10271027
route_hints: Option<Vec<PhantomRouteHints>>,
1028+
labels: Vec<String>,
10281029
) -> Result<(Bolt11Invoice, u64), MutinyError> {
10291030
match self.lsp_client.as_ref() {
10301031
Some(lsp) => {
@@ -1093,6 +1094,7 @@ impl<S: MutinyStorage> Node<S> {
10931094
Some(amount_minus_fee),
10941095
Some(lsp_fee.fee_amount_msat),
10951096
route_hints,
1097+
labels,
10961098
)
10971099
.await?;
10981100

@@ -1129,8 +1131,13 @@ impl<S: MutinyStorage> Node<S> {
11291131
AnyLsp::Lsps(client) => {
11301132
if has_inbound_capacity {
11311133
Ok((
1132-
self.create_internal_invoice(Some(amount_sat), None, route_hints)
1133-
.await?,
1134+
self.create_internal_invoice(
1135+
Some(amount_sat),
1136+
None,
1137+
route_hints,
1138+
labels,
1139+
)
1140+
.await?,
11341141
0,
11351142
))
11361143
} else {
@@ -1147,6 +1154,7 @@ impl<S: MutinyStorage> Node<S> {
11471154
invoice.clone(),
11481155
Some(amount_sat * 1_000),
11491156
Some(lsp_fee.fee_amount_msat),
1157+
labels,
11501158
)
11511159
.await?;
11521160

@@ -1163,7 +1171,7 @@ impl<S: MutinyStorage> Node<S> {
11631171
}
11641172
}
11651173
None => Ok((
1166-
self.create_internal_invoice(Some(amount_sat), None, route_hints)
1174+
self.create_internal_invoice(Some(amount_sat), None, route_hints, labels)
11671175
.await?,
11681176
0,
11691177
)),
@@ -1175,6 +1183,7 @@ impl<S: MutinyStorage> Node<S> {
11751183
amount_sat: Option<u64>,
11761184
fee_amount_msat: Option<u64>,
11771185
route_hints: Option<Vec<PhantomRouteHints>>,
1186+
labels: Vec<String>,
11781187
) -> Result<Bolt11Invoice, MutinyError> {
11791188
let amount_msat = amount_sat.map(|s| s * 1_000);
11801189
// Set description to empty string to make smallest possible invoice/QR code
@@ -1228,7 +1237,7 @@ impl<S: MutinyStorage> Node<S> {
12281237
MutinyError::InvoiceCreationFailed
12291238
})?;
12301239

1231-
self.save_invoice_payment_info(invoice.clone(), amount_msat, fee_amount_msat)
1240+
self.save_invoice_payment_info(invoice.clone(), amount_msat, fee_amount_msat, labels)
12321241
.await?;
12331242

12341243
log_info!(self.logger, "SUCCESS: generated invoice: {invoice}");
@@ -1241,6 +1250,7 @@ impl<S: MutinyStorage> Node<S> {
12411250
invoice: Bolt11Invoice,
12421251
amount_msat: Option<u64>,
12431252
fee_amount_msat: Option<u64>,
1253+
labels: Vec<String>,
12441254
) -> Result<(), MutinyError> {
12451255
let last_update = utils::now().as_secs();
12461256
let payment_hash = PaymentHash(invoice.payment_hash().into_inner());
@@ -1265,6 +1275,8 @@ impl<S: MutinyStorage> Node<S> {
12651275
MutinyError::InvoiceCreationFailed
12661276
})?;
12671277

1278+
self.persister.storage.set_invoice_labels(invoice, labels)?;
1279+
12681280
Ok(())
12691281
}
12701282

@@ -2473,7 +2485,10 @@ mod tests {
24732485

24742486
let amount_sats = 1_000;
24752487

2476-
let invoice = node.create_invoice(amount_sats, None).await.unwrap().0;
2488+
let (invoice, _) = node
2489+
.create_invoice(amount_sats, None, vec![])
2490+
.await
2491+
.unwrap();
24772492

24782493
assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
24792494
match invoice.description() {
@@ -2504,7 +2519,7 @@ mod tests {
25042519
let storage = MemoryStorage::default();
25052520
let node = create_node(storage).await;
25062521

2507-
let invoice = node.create_invoice(10_000, None).await.unwrap().0;
2522+
let invoice = node.create_invoice(10_000, None, vec![]).await.unwrap().0;
25082523

25092524
let result = node
25102525
.pay_invoice_with_timeout(&invoice, None, None, vec![])
@@ -2599,6 +2614,7 @@ mod tests {
25992614
#[cfg(target_arch = "wasm32")]
26002615
mod wasm_test {
26012616
use crate::event::{MillisatAmount, PaymentInfo};
2617+
use crate::labels::LabelStorage;
26022618
use crate::storage::MemoryStorage;
26032619
use crate::test_utils::create_node;
26042620
use crate::HTLCStatus;
@@ -2626,8 +2642,13 @@ mod wasm_test {
26262642
let now = crate::utils::now().as_secs();
26272643

26282644
let amount_sats = 1_000;
2645+
let label = "test".to_string();
2646+
let labels = vec![label.clone()];
26292647

2630-
let invoice = node.create_invoice(amount_sats, None).await.unwrap().0;
2648+
let (invoice, _) = node
2649+
.create_invoice(amount_sats, None, labels.clone())
2650+
.await
2651+
.unwrap();
26312652

26322653
assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
26332654
match invoice.description() {
@@ -2649,16 +2670,29 @@ mod wasm_test {
26492670
assert_eq!(from_storage.amount_sats, Some(amount_sats));
26502671
assert_eq!(from_storage.status, HTLCStatus::Pending);
26512672
assert_eq!(from_storage.fees_paid, None);
2673+
assert_eq!(from_storage.labels, labels.clone());
26522674
assert!(from_storage.inbound);
26532675
assert!(from_storage.last_updated >= now);
2676+
2677+
// check labels
2678+
2679+
let invoice_labels = storage.get_invoice_labels().unwrap();
2680+
assert_eq!(invoice_labels.len(), 1);
2681+
assert_eq!(invoice_labels.get(&invoice).cloned(), Some(labels));
2682+
2683+
let label_item = storage.get_label("test").unwrap().unwrap();
2684+
2685+
assert!(label_item.last_used_time >= now);
2686+
assert!(label_item.addresses.is_empty());
2687+
assert_eq!(label_item.invoices, vec![invoice]);
26542688
}
26552689

26562690
#[test]
26572691
async fn test_fail_own_invoice() {
26582692
let storage = MemoryStorage::default();
26592693
let node = create_node(storage).await;
26602694

2661-
let invoice = node.create_invoice(10_000, None).await.unwrap().0;
2695+
let invoice = node.create_invoice(10_000, None, vec![]).await.unwrap().0;
26622696

26632697
let result = node
26642698
.pay_invoice_with_timeout(&invoice, None, None, vec![])

mutiny-core/src/nodemanager.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,11 @@ impl<S: MutinyStorage> NodeManager<S> {
13691369
///
13701370
/// If the manager has more than one node it will create a phantom invoice.
13711371
/// If there is only one node it will create an invoice just for that node.
1372-
pub async fn create_invoice(&self, amount: u64) -> Result<(MutinyInvoice, u64), MutinyError> {
1372+
pub async fn create_invoice(
1373+
&self,
1374+
amount: u64,
1375+
labels: Vec<String>,
1376+
) -> Result<(MutinyInvoice, u64), MutinyError> {
13731377
let nodes = self.nodes.lock().await;
13741378
let use_phantom = nodes.len() > 1 && self.lsp_config.is_none();
13751379
if nodes.len() == 0 {
@@ -1392,7 +1396,9 @@ impl<S: MutinyStorage> NodeManager<S> {
13921396
} else {
13931397
return Err(MutinyError::WalletOperationFailed);
13941398
};
1395-
let invoice = first_node.create_invoice(amount, route_hints).await?;
1399+
let invoice = first_node
1400+
.create_invoice(amount, route_hints, labels)
1401+
.await?;
13961402

13971403
Ok((invoice.0.into(), invoice.1))
13981404
}

mutiny-wasm/src/lib.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1087,20 +1087,17 @@ impl MutinyWallet {
10871087
let activity = self.inner.node_manager.get_label_activity(&label).await?;
10881088
let mut activity: Vec<ActivityItem> = activity.into_iter().map(|a| a.into()).collect();
10891089

1090-
// add contact to the activity item if it is one
1091-
let Some(contact) = self.inner.node_manager.get_contact(&label)? else {
1092-
return Ok(JsValue::from_serde(&activity)?);
1090+
// add contact to the activity item it has one, otherwise return the activity list
1091+
let contact = match self.inner.node_manager.get_contact(&label)? {
1092+
Some(contact) => contact,
1093+
None => return Ok(JsValue::from_serde(&activity)?),
10931094
};
10941095

1096+
// if we have a contact, add it to the activity item, remove it as a label
1097+
// This is the same as we do in get_activity
10951098
for a in activity.iter_mut() {
1096-
// find labels that have a contact and add them to the item
1097-
for a_label in a.labels.iter() {
1098-
if label == *a_label {
1099-
a.contacts
1100-
.push(TagItem::from((a_label.clone(), contact.clone())));
1101-
}
1102-
}
1103-
// remove labels that have the contact to prevent duplicates
1099+
a.contacts
1100+
.push(TagItem::from((label.clone(), contact.clone())));
11041101
a.labels.retain(|l| l != &label);
11051102
}
11061103

0 commit comments

Comments
 (0)