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

Commit 7884455

Browse files
committed
Try mutiny2mutiny offers
1 parent 5c387e6 commit 7884455

File tree

3 files changed

+98
-10
lines changed

3 files changed

+98
-10
lines changed

mutiny-core/src/node.rs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use bitcoin::util::bip32::ExtendedPrivKey;
4444
use futures_util::lock::Mutex;
4545
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
4646
use lightning::ln::PaymentSecret;
47-
use lightning::offers::offer::Offer;
47+
use lightning::offers::offer::{Offer, Quantity};
4848
use lightning::onion_message::OnionMessenger as LdkOnionMessenger;
4949
use lightning::sign::{EntropySource, InMemorySigner, NodeSigner, Recipient};
5050
use lightning::util::config::MaxDustHTLCExposure;
@@ -957,6 +957,43 @@ impl<S: MutinyStorage> Node<S> {
957957
Ok(invoice)
958958
}
959959

960+
pub async fn create_offer(
961+
&self,
962+
amount_sat: Option<u64>,
963+
labels: Vec<String>,
964+
) -> Result<Offer, MutinyError> {
965+
// wait for first sync to complete
966+
for _ in 0..60 {
967+
// check if we've been stopped
968+
if self.stop.load(Ordering::Relaxed) {
969+
return Err(MutinyError::NotRunning);
970+
}
971+
972+
if let Ok(true) = self.persister.storage.has_done_first_sync() {
973+
break;
974+
}
975+
976+
sleep(1_000).await;
977+
}
978+
979+
let mut builder = self
980+
.channel_manager
981+
.create_offer_builder("".to_string())
982+
.supported_quantity(Quantity::Unbounded);
983+
984+
if let Some(amount_sats) = amount_sat {
985+
builder = builder.amount_msats(amount_sats * 1_000);
986+
}
987+
988+
let offer = builder.build()?;
989+
990+
self.persister
991+
.storage
992+
.set_invoice_labels(offer.to_string(), labels)?;
993+
994+
Ok(offer)
995+
}
996+
960997
pub fn get_invoice(&self, invoice: &Bolt11Invoice) -> Result<MutinyInvoice, MutinyError> {
961998
self.get_invoice_by_hash(invoice.payment_hash())
962999
}
@@ -1238,16 +1275,32 @@ impl<S: MutinyStorage> Node<S> {
12381275
getrandom::getrandom(&mut bytes).map_err(|_| MutinyError::SeedGenerationFailed)?;
12391276
let payment_id = PaymentId(bytes);
12401277

1278+
let quantity = match quantity {
1279+
None => {
1280+
if offer.expects_quantity() {
1281+
Some(1)
1282+
} else {
1283+
None
1284+
}
1285+
}
1286+
Some(q) => Some(q),
1287+
};
1288+
12411289
let amount_msats = amt_sats.map(|a| a * 1_000);
1242-
self.channel_manager.pay_for_offer(
1243-
&offer,
1244-
quantity,
1245-
amount_msats,
1246-
payer_note,
1247-
payment_id,
1248-
Self::retry_strategy(),
1249-
None,
1250-
)?;
1290+
self.channel_manager
1291+
.pay_for_offer(
1292+
&offer,
1293+
quantity,
1294+
amount_msats,
1295+
payer_note,
1296+
payment_id,
1297+
Self::retry_strategy(),
1298+
None,
1299+
)
1300+
.map_err(|e| {
1301+
log_error!(self.logger, "failed to make payment: {e:?}");
1302+
e
1303+
})?;
12511304

12521305
// persist and label offer after calling pay_for_offer, it only fails if we can't initiate a payment
12531306

mutiny-core/src/nodemanager.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,20 @@ impl<S: MutinyStorage> NodeManager<S> {
16821682
Ok(invoice.into())
16831683
}
16841684

1685+
/// Creates a lightning offer. The amount should be in satoshis.
1686+
/// If no amount is provided, the offer will be created with no amount.
1687+
pub async fn create_offer(
1688+
&self,
1689+
from_node: &PublicKey,
1690+
amount: Option<u64>,
1691+
labels: Vec<String>,
1692+
) -> Result<Offer, MutinyError> {
1693+
let node = self.get_node(from_node).await?;
1694+
let offer = node.create_offer(amount, labels).await?;
1695+
1696+
Ok(offer)
1697+
}
1698+
16851699
/// Pays a lightning invoice from the selected node.
16861700
/// An amount should only be provided if the invoice does not have an amount.
16871701
/// The amount should be in satoshis.

mutiny-wasm/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,27 @@ impl MutinyWallet {
646646
.into())
647647
}
648648

649+
/// Creates a lightning offer. The amount should be in satoshis.
650+
/// If no amount is provided, the offer will be created with no amount.
651+
#[wasm_bindgen]
652+
pub async fn create_offer(
653+
&self,
654+
from_node: String,
655+
amount: Option<u64>,
656+
labels: &JsValue, /* Vec<String> */
657+
) -> Result<String, MutinyJsError> {
658+
let from_node = PublicKey::from_str(&from_node)?;
659+
let labels: Vec<String> = labels
660+
.into_serde()
661+
.map_err(|_| MutinyJsError::InvalidArgumentsError)?;
662+
Ok(self
663+
.inner
664+
.node_manager
665+
.create_offer(&from_node, amount, labels)
666+
.await?
667+
.to_string())
668+
}
669+
649670
/// Pays a lightning invoice from the selected node.
650671
/// An amount should only be provided if the invoice does not have an amount.
651672
/// The amount should be in satoshis.

0 commit comments

Comments
 (0)