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

Commit 131bab4

Browse files
committed
Respond to invalid nwc commands properly
1 parent d1dda77 commit 131bab4

1 file changed

Lines changed: 75 additions & 7 deletions

File tree

mutiny-core/src/nostr/nwc.rs

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,18 @@ impl NostrWalletConnect {
367367
Ok(())
368368
}
369369

370-
fn get_skipped_error_event(&self, event: &Event, message: String) -> anyhow::Result<Event> {
370+
fn get_skipped_error_event(
371+
&self,
372+
event: &Event,
373+
error_code: ErrorCode,
374+
message: String,
375+
) -> anyhow::Result<Event> {
371376
let server_key = self.server_key.secret_key()?;
372377
let client_pubkey = self.client_key.public_key();
373378
let content = Response {
374379
result_type: Method::PayInvoice,
375380
error: Some(NIP47Error {
376-
code: ErrorCode::Other,
381+
code: error_code,
377382
message,
378383
}),
379384
result: None,
@@ -416,11 +421,32 @@ impl NostrWalletConnect {
416421
let server_key = self.server_key.secret_key()?;
417422

418423
let decrypted = decrypt(&server_key, &client_pubkey, &event.content)?;
419-
let req: Request = Request::from_json(decrypted)?;
424+
let req: Request = match Request::from_json(decrypted) {
425+
Ok(req) => req,
426+
Err(e) => {
427+
log_warn!(
428+
nostr_manager.logger,
429+
"Failed to parse request: {e}, skipping..."
430+
);
431+
return self
432+
.get_skipped_error_event(
433+
&event,
434+
ErrorCode::NotImplemented,
435+
"Failed to parse request.".to_string(),
436+
)
437+
.map(Some);
438+
}
439+
};
420440

421441
// only respond to pay invoice requests
422442
if req.method != Method::PayInvoice {
423-
return Ok(None);
443+
return self
444+
.get_skipped_error_event(
445+
&event,
446+
ErrorCode::NotImplemented,
447+
"Command is not supported.".to_string(),
448+
)
449+
.map(Some);
424450
}
425451

426452
let invoice = match req.params {
@@ -432,7 +458,11 @@ impl NostrWalletConnect {
432458
// if the invoice has expired, skip it
433459
if invoice.would_expire(utils::now()) {
434460
return self
435-
.get_skipped_error_event(&event, "Invoice expired".to_string())
461+
.get_skipped_error_event(
462+
&event,
463+
ErrorCode::Other,
464+
"Invoice expired".to_string(),
465+
)
436466
.map(Some);
437467
}
438468

@@ -443,7 +473,11 @@ impl NostrWalletConnect {
443473
"NWC Invoice amount not set, cannot pay: {invoice}"
444474
);
445475
return self
446-
.get_skipped_error_event(&event, "Invoice amount not set".to_string())
476+
.get_skipped_error_event(
477+
&event,
478+
ErrorCode::Other,
479+
"Invoice amount not set".to_string(),
480+
)
447481
.map(Some);
448482
}
449483

@@ -459,6 +493,7 @@ impl NostrWalletConnect {
459493
return self
460494
.get_skipped_error_event(
461495
&event,
496+
ErrorCode::Other,
462497
"Paying hodl invoices disabled".to_string(),
463498
)
464499
.map(Some);
@@ -1147,6 +1182,7 @@ mod wasm_test {
11471182
use bitcoin::Network;
11481183
use mockall::predicate::eq;
11491184
use nostr::key::SecretKey;
1185+
use serde_json::json;
11501186
use std::sync::{atomic::AtomicBool, Arc};
11511187
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
11521188

@@ -1259,6 +1295,31 @@ mod wasm_test {
12591295
assert_eq!(result.unwrap(), None);
12601296
check_no_pending_invoices(&storage);
12611297

1298+
// test unknown command
1299+
let event = {
1300+
let req = json!({"method": "fake_command", "params": {}});
1301+
1302+
let encrypted = encrypt(&uri.secret, &uri.public_key, req.to_string()).unwrap();
1303+
let p_tag = Tag::PublicKey {
1304+
public_key: uri.public_key,
1305+
relay_url: None,
1306+
alias: None,
1307+
};
1308+
EventBuilder::new(Kind::WalletConnectRequest, encrypted, [p_tag])
1309+
.to_event(&Keys::new(uri.secret))
1310+
.unwrap()
1311+
};
1312+
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
1313+
check_nwc_error_response(
1314+
result.unwrap().unwrap(),
1315+
&uri.secret,
1316+
NIP47Error {
1317+
code: ErrorCode::NotImplemented,
1318+
message: "Failed to parse request.".to_string(),
1319+
},
1320+
);
1321+
check_no_pending_invoices(&storage);
1322+
12621323
// test unexpected command
12631324
let event = {
12641325
let req = Request {
@@ -1277,7 +1338,14 @@ mod wasm_test {
12771338
.unwrap()
12781339
};
12791340
let result = nwc.handle_nwc_request(event, &node, &nostr_manager).await;
1280-
assert_eq!(result.unwrap(), None);
1341+
check_nwc_error_response(
1342+
result.unwrap().unwrap(),
1343+
&uri.secret,
1344+
NIP47Error {
1345+
code: ErrorCode::NotImplemented,
1346+
message: "Command is not supported.".to_string(),
1347+
},
1348+
);
12811349
check_no_pending_invoices(&storage);
12821350

12831351
// test invalid invoice

0 commit comments

Comments
 (0)