Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More safely handle invalid NWC events #1129

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion mutiny-core/src/nostr/nwc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ impl NostrWalletConnect {
) -> anyhow::Result<Option<Event>> {
let invoice = match params.payment_hash {
Some(payment_hash) => {
let hash: [u8; 32] = FromHex::from_hex(&payment_hash).expect("invalid hash");
let hash: [u8; 32] = FromHex::from_hex(&payment_hash)
.map_err(|e| anyhow!("Failed to parse payment_hash {payment_hash}: {e}"))?;
node.lookup_payment(&hash).await
}
None => match params.bolt11 {
Expand Down Expand Up @@ -2386,5 +2387,31 @@ mod wasm_test {
assert_eq!(result.transaction_type, Some(TransactionType::Incoming));
assert_eq!(result.preimage, None);
assert_ne!(result.created_at, 0); // make sure we properly set this

// test invalid invoice
let event = sign_nwc_request(
&uri,
Request::lookup_invoice(LookupInvoiceRequestParams {
payment_hash: None,
bolt11: Some("invalid invoice".to_string()),
}),
);
let result = nwc
.handle_nwc_request(event.clone(), &node, &nostr_manager)
.await;
assert!(result.is_err());

// test invalid payment_hash
let event = sign_nwc_request(
&uri,
Request::lookup_invoice(LookupInvoiceRequestParams {
payment_hash: Some("invalid payment_hash".to_string()),
bolt11: None,
}),
);
let result = nwc
.handle_nwc_request(event.clone(), &node, &nostr_manager)
.await;
assert!(result.is_err());
}
}
Loading