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

Commit f8244f0

Browse files
authored
Merge pull request #78 from benthecarman/fix-invoice-creation
2 parents 48368ca + 8caf105 commit f8244f0

File tree

6 files changed

+50
-33
lines changed

6 files changed

+50
-33
lines changed

node-manager/src/background.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ impl BackgroundProcessor {
451451
{
452452
let stop_thread = Arc::new(AtomicBool::new(false));
453453
let stop_thread_clone = stop_thread.clone();
454-
let _ = spawn_local(async move {
455-
let _ = define_run_body!(
454+
spawn_local(async move {
455+
define_run_body!(
456456
persister,
457457
event_handler,
458458
chain_monitor,

node-manager/src/error.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bdk::esplora_client;
22
use lightning::ln::peer_handler::PeerHandleError;
3+
use lightning_invoice::payment::PaymentError;
34
use thiserror::Error;
45
use wasm_bindgen::JsValue;
56

@@ -114,6 +115,16 @@ impl From<PeerHandleError> for MutinyError {
114115
}
115116
}
116117

118+
impl From<PaymentError> for MutinyError {
119+
fn from(e: PaymentError) -> Self {
120+
match e {
121+
PaymentError::Invoice(_) => Self::InvoiceInvalid,
122+
PaymentError::Routing(_) => Self::RoutingFailed,
123+
PaymentError::Sending(_) => Self::RoutingFailed,
124+
}
125+
}
126+
}
127+
117128
impl From<MutinyStorageError> for bdk::Error {
118129
fn from(e: MutinyStorageError) -> Self {
119130
match e {
@@ -232,6 +243,16 @@ impl From<bitcoin::util::address::Error> for MutinyJsError {
232243
}
233244
}
234245

246+
impl From<PaymentError> for MutinyJsError {
247+
fn from(e: PaymentError) -> Self {
248+
match e {
249+
PaymentError::Invoice(_) => Self::InvoiceInvalid,
250+
PaymentError::Routing(_) => Self::RoutingFailed,
251+
PaymentError::Sending(_) => Self::RoutingFailed,
252+
}
253+
}
254+
}
255+
235256
impl From<esplora_client::Error> for MutinyJsError {
236257
fn from(_e: esplora_client::Error) -> Self {
237258
// This is most likely a chain access failure

node-manager/src/event.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(crate) struct PaymentInfo {
2121
pub secret: Option<[u8; 32]>,
2222
pub status: HTLCStatus,
2323
pub amt_msat: MillisatAmount,
24+
pub fee_paid_msat: Option<u64>,
2425
}
2526

2627
#[derive(Serialize, Deserialize)]
@@ -97,11 +98,11 @@ impl LdkEventHandler for EventHandler {
9798
.to_address();
9899

99100
let wallet_thread = self.wallet.clone();
100-
let channel_values_satoshis_thread = channel_value_satoshis.clone();
101+
let channel_values_satoshis_thread = *channel_value_satoshis;
101102
let channel_manager_thread = self.channel_manager.clone();
102103
let logger_thread = self.logger.clone();
103-
let temporary_channel_id_thread = temporary_channel_id.clone();
104-
let counterparty_node_id_thread = counterparty_node_id.clone();
104+
let temporary_channel_id_thread = *temporary_channel_id;
105+
let counterparty_node_id_thread = *counterparty_node_id;
105106
spawn_local(async move {
106107
let psbt = match wallet_thread
107108
.create_signed_psbt(addr, channel_values_satoshis_thread, None)
@@ -239,6 +240,7 @@ impl LdkEventHandler for EventHandler {
239240
secret: payment_secret,
240241
status: HTLCStatus::Succeeded,
241242
amt_msat: MillisatAmount(Some(*amount_msat)),
243+
fee_paid_msat: None,
242244
};
243245
match self
244246
.persister
@@ -261,6 +263,7 @@ impl LdkEventHandler for EventHandler {
261263
Event::PaymentSent {
262264
payment_preimage,
263265
payment_hash,
266+
fee_paid_msat,
264267
..
265268
} => {
266269
self.logger.log(&Record::new(
@@ -271,14 +274,15 @@ impl LdkEventHandler for EventHandler {
271274
0,
272275
));
273276

274-
match self.persister.read_payment_info(*payment_hash, true) {
277+
match self.persister.read_payment_info(*payment_hash, false) {
275278
Some(mut saved_payment_info) => {
276279
saved_payment_info.status = HTLCStatus::Succeeded;
277280
saved_payment_info.preimage = Some(payment_preimage.0);
281+
saved_payment_info.fee_paid_msat = *fee_paid_msat;
278282
match self.persister.persist_payment_info(
279283
*payment_hash,
280284
saved_payment_info,
281-
true,
285+
false,
282286
) {
283287
Ok(_) => (),
284288
Err(e) => {
@@ -359,13 +363,13 @@ impl LdkEventHandler for EventHandler {
359363
0,
360364
));
361365

362-
match self.persister.read_payment_info(*payment_hash, true) {
366+
match self.persister.read_payment_info(*payment_hash, false) {
363367
Some(mut saved_payment_info) => {
364368
saved_payment_info.status = HTLCStatus::Failed;
365369
match self.persister.persist_payment_info(
366370
*payment_hash,
367371
saved_payment_info,
368-
true,
372+
false,
369373
) {
370374
Ok(_) => (),
371375
Err(e) => {

node-manager/src/localstorage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl MutinyBrowserStorage {
3434
let data = serde_json::to_string(&value)?;
3535
// Only bother encrypting if a password is set
3636
if self.password.is_empty() {
37-
return Ok(LocalStorage::set(key, data)?);
37+
Ok(LocalStorage::set(key, data)?)
3838
} else {
3939
let ciphertext = encrypt(data.as_str(), self.password.as_str());
40-
return Ok(LocalStorage::set(key, ciphertext)?);
40+
Ok(LocalStorage::set(key, ciphertext)?)
4141
}
4242
}
4343

@@ -49,10 +49,10 @@ impl MutinyBrowserStorage {
4949
let data: String = LocalStorage::get(key)?;
5050
// Only bother decrypting if a password is set
5151
if self.password.is_empty() {
52-
return Ok(serde_json::from_str::<T>(data.as_str())?);
52+
Ok(serde_json::from_str::<T>(data.as_str())?)
5353
} else {
5454
let decrypted_data = decrypt(data.as_str(), self.password.as_str());
55-
return Ok(serde_json::from_str::<T>(decrypted_data.as_str())?);
55+
Ok(serde_json::from_str::<T>(decrypted_data.as_str())?)
5656
}
5757
}
5858

node-manager/src/node.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct Node {
9292
pub chain_monitor: Arc<ChainMonitor>,
9393
pub invoice_payer: Arc<InvoicePayer<EventHandler>>,
9494
network: Network,
95-
persister: Arc<MutinyNodePersister>,
95+
pub persister: Arc<MutinyNodePersister>,
9696
_background_processor: BackgroundProcessor,
9797
logger: Arc<MutinyLogger>,
9898
}
@@ -309,7 +309,7 @@ impl Node {
309309
route_hints: Vec<PhantomRouteHints>,
310310
) -> Result<Invoice, MutinyError> {
311311
let invoice = match create_phantom_invoice::<InMemorySigner, Arc<PhantomKeysManager>>(
312-
Some(amount_sat * 1),
312+
Some(amount_sat * 1_000),
313313
None,
314314
description,
315315
1500,
@@ -345,6 +345,7 @@ impl Node {
345345
secret: Some(invoice.payment_secret().0),
346346
status: HTLCStatus::Pending,
347347
amt_msat: MillisatAmount(Some(amount_sat * 1000)),
348+
fee_paid_msat: None,
348349
};
349350
match self
350351
.persister

node-manager/src/nodemanager.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct MutinyInvoice {
7878
payment_hash: String,
7979
preimage: Option<String>,
8080
pub amount_sats: Option<u64>,
81-
pub expire: Option<u64>,
81+
pub expire: u64,
8282
pub paid: bool,
8383
pub fees_paid: Option<u64>,
8484
pub is_send: bool,
@@ -114,13 +114,16 @@ impl From<Invoice> for MutinyInvoice {
114114
InvoiceDescription::Hash(_) => None,
115115
};
116116

117+
let timestamp = value.duration_since_epoch().as_secs();
118+
let expiry = timestamp + value.expiry_time().as_secs();
119+
117120
MutinyInvoice {
118121
bolt11: value.to_string(),
119122
description,
120123
payment_hash: value.payment_hash().to_owned().to_hex(),
121124
preimage: None,
122125
amount_sats: value.amount_milli_satoshis().map(|m| m / 1000),
123-
expire: None, // todo
126+
expire: expiry,
124127
paid: false,
125128
fees_paid: None, // todo
126129
is_send: false, // todo this could be bad
@@ -245,7 +248,7 @@ impl NodeManager {
245248
let id = node
246249
.keys_manager
247250
.get_node_id(Recipient::Node)
248-
.expect(format!("Failed to get node id for {}", node_item.0).as_str());
251+
.expect("Failed to get node id");
249252

250253
nodes_map.insert(id.to_hex(), Arc::new(node));
251254
}
@@ -513,6 +516,8 @@ impl NodeManager {
513516

514517
let invoice =
515518
Invoice::from_str(invoice_str.as_str()).map_err(|_| MutinyJsError::InvoiceInvalid)?;
519+
520+
// todo ensure payment hash is unique
516521
let pay_result = node.invoice_payer.pay_invoice(&invoice);
517522

518523
match pay_result {
@@ -544,23 +549,9 @@ impl NodeManager {
544549

545550
let amt_msats = amt_sats * 1000;
546551

547-
let chans = node.channel_manager.list_channels();
548-
let chan = chans.first().unwrap();
549-
let usable = node.channel_manager.list_usable_channels();
550-
551-
info!(
552-
"confirmations_required: {}",
553-
chan.confirmations_required.unwrap_or(69)
554-
);
555-
info!("is_channel_ready: {}", chan.is_channel_ready);
556-
info!("is_usable: {}", chan.is_usable);
557-
info!("is_public: {}", chan.is_public);
558-
info!("usable: {}", usable.len());
559-
560552
let _ = node
561553
.invoice_payer
562-
.pay_pubkey(node_id, preimage, amt_msats, 40)
563-
.unwrap();
554+
.pay_pubkey(node_id, preimage, amt_msats, 40)?;
564555

565556
Ok(info!("Keysend successful!"))
566557
}

0 commit comments

Comments
 (0)