Skip to content

Commit

Permalink
better dm handling - find invoices in dm
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Apr 12, 2024
1 parent b5be4c6 commit 863d092
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ debug/
target/
.vim/
.direnv
.idea

# These are backup files generated by rustfmt
**/*.rs.bk
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ payjoin = { version = "0.13.0", features = ["send", "base64"] }
bincode = "1.3.3"
hex-conservative = "0.1.1"
async-lock = "3.2.0"
bitcoin-waila = { git = "https://github.com/mutinywallet/bitcoin-waila", rev = "1d6c416fc0abaa2efa78e3dcf28450975d8f7bfe" }

fedimint-client = "0.3.0"
fedimint-core = "0.3.0"
Expand Down
66 changes: 48 additions & 18 deletions mutiny-core/src/nostr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1404,8 +1404,8 @@ impl<S: MutinyStorage> NostrManager<S> {
Ok(())
}

/// Handles an encrypted direct message. If it is an invoice we add it to our pending
/// invoice storage.
/// Handles an encrypted direct message. If it finds an invoice in the dm, we add it
/// to our pending invoice storage.
pub async fn handle_direct_message(
&self,
event: Event,
Expand Down Expand Up @@ -1443,23 +1443,39 @@ impl<S: MutinyStorage> NostrManager<S> {
}
}

// handle it like a pay invoice NWC request, to see if it is valid
let params = PayInvoiceRequestParams {
id: None,
invoice: decrypted,
amount: None,
};
let invoice: Bolt11Invoice = match check_valid_nwc_invoice(&params, invoice_handler).await {
Ok(Some(invoice)) => invoice,
Ok(None) => return Ok(()),
Err(msg) => {
log_debug!(self.logger, "Not adding DM'd invoice: {msg}");
// loop through dm to check for invoice
for word in decrypted.split_whitespace() {
// ignore word if too short
if word.len() > 15 {
let invoice_request_param = match bitcoin_waila::PaymentParams::from_str(word) {
// if not an invoice, go to next word in dm
Ok(param) => match param.invoice() {
Some(invoice) => PayInvoiceRequestParams {
id: None,
invoice: invoice.to_string(),
amount: None,
},
None => continue,
},
Err(_) => continue,
};

// handle it like a pay invoice NWC request, to see if it is valid
let invoice: Bolt11Invoice =
match check_valid_nwc_invoice(&invoice_request_param, invoice_handler).await {
Ok(Some(invoice)) => invoice,
Ok(None) => return Ok(()),
Err(msg) => {
log_debug!(self.logger, "Not adding DM'd invoice: {msg}");
return Ok(());
}
};
self.save_pending_nwc_invoice(None, event.id, event.pubkey, invoice, None)
.await?;

return Ok(());
}
};

self.save_pending_nwc_invoice(None, event.id, event.pubkey, invoice, None)
.await?;
}

Ok(())
}
Expand Down Expand Up @@ -2322,9 +2338,9 @@ mod test {
.unwrap()
.to_event(&user)
.unwrap();
block_on(nostr_manager.handle_direct_message(dm, &inv_handler)).unwrap();
let pending = nostr_manager.get_pending_nwc_invoices().unwrap();
assert!(pending.is_empty());
block_on(nostr_manager.handle_direct_message(dm, &inv_handler)).unwrap();

// create invoice
let secp = Secp256k1::new();
Expand Down Expand Up @@ -2371,6 +2387,20 @@ mod test {
.unwrap();
block_on(nostr_manager.handle_direct_message(dm, &inv_handler)).unwrap();
let pending = nostr_manager.get_pending_nwc_invoices().unwrap();
assert!(!pending.is_empty());

// valid invoice in dm along with message should be added
let dm = EventBuilder::encrypted_direct_msg(
&user,
nostr_keys.public_key(),
format!("invoice for you to pay {}", invoice),
None,
)
.unwrap()
.to_event(&user)
.unwrap();
block_on(nostr_manager.handle_direct_message(dm, &inv_handler)).unwrap();
let pending = nostr_manager.get_pending_nwc_invoices().unwrap();
assert!(!pending.is_empty())
}

Expand Down

0 comments on commit 863d092

Please sign in to comment.