From 537834ca7829ee70d9a671b36b77a8718de3d584 Mon Sep 17 00:00:00 2001 From: Alex Lewin Date: Mon, 27 May 2024 16:17:40 -0400 Subject: [PATCH] feat: progress --- fedimint-nwc/src/database/db.rs | 8 +++--- fedimint-nwc/src/database/invoice.rs | 2 +- fedimint-nwc/src/main.rs | 37 +++++++++++--------------- fedimint-nwc/src/nwc.rs | 21 +++++++-------- fedimint-nwc/src/services/multimint.rs | 4 +-- fedimint-nwc/src/services/nostr.rs | 2 +- fedimint-nwc/src/state.rs | 3 +-- 7 files changed, 34 insertions(+), 43 deletions(-) diff --git a/fedimint-nwc/src/database/db.rs b/fedimint-nwc/src/database/db.rs index 01e0c6a..63b4802 100644 --- a/fedimint-nwc/src/database/db.rs +++ b/fedimint-nwc/src/database/db.rs @@ -68,7 +68,7 @@ impl Database { } pub fn add_payment(&self, invoice: Bolt11Invoice) -> Result<()> { - let payment_hash_encoded = hex::encode(invoice.payment_hash().to_vec()); + let payment_hash_encoded = hex::encode(invoice.payment_hash()); self.write_with(|dbtx| { let mut payments = dbtx.open_table(PAYMENTS_TABLE)?; let now = std::time::SystemTime::now() @@ -103,7 +103,7 @@ impl Database { } pub fn add_invoice(&self, invoice: &Bolt11Invoice) -> Result<()> { - let payment_hash_encoded = hex::encode(invoice.payment_hash().to_vec()); + let payment_hash_encoded = hex::encode(invoice.payment_hash()); let invoice = Invoice::from(invoice); self.write_with(|dbtx| { let mut invoices = dbtx.open_table(INVOICES_TABLE)?; @@ -129,8 +129,8 @@ impl Database { }) }) } else if let Some(bolt11) = params.invoice { - let invoice = Bolt11Invoice::from_str(&bolt11).map_err(|e| anyhow::Error::new(e))?; - let payment_hash_encoded = hex::encode(invoice.payment_hash().to_vec()); + let invoice = Bolt11Invoice::from_str(&bolt11).map_err(anyhow::Error::new)?; + let payment_hash_encoded = hex::encode(invoice.payment_hash()); self.read_with(|dbtx| { let invoices = dbtx.open_table(INVOICES_TABLE)?; invoices diff --git a/fedimint-nwc/src/database/invoice.rs b/fedimint-nwc/src/database/invoice.rs index 54b62a3..4074372 100644 --- a/fedimint-nwc/src/database/invoice.rs +++ b/fedimint-nwc/src/database/invoice.rs @@ -35,7 +35,7 @@ impl Invoice { } pub fn payment_hash(&self) -> String { - hex::encode(self.invoice.payment_hash().to_vec()) + hex::encode(self.invoice.payment_hash()) } pub fn description(&self) -> Option { diff --git a/fedimint-nwc/src/main.rs b/fedimint-nwc/src/main.rs index ddce05b..ee6b852 100644 --- a/fedimint-nwc/src/main.rs +++ b/fedimint-nwc/src/main.rs @@ -51,28 +51,23 @@ async fn event_loop(state: AppState) -> Result<()> { break; }, notification = notifications.recv() => { - match notification { - Ok(notification) => match notification { - RelayPoolNotification::Event { event, .. } => { - // Only handle nwc events - if event.kind == Kind::WalletConnectRequest - && event.pubkey == state.nostr_service.user_keys().public_key() - && event.verify().is_ok() { - info!("Received event: {}", event.as_json()); - state.handle_event(*event).await - } else { - error!("Invalid nwc event: {}", event.as_json()); - } - }, - RelayPoolNotification::Shutdown => { - info!("Relay pool shutdown"); - break; - }, - _ => { - error!("Unhandled relay pool notification: {notification:?}"); + if let Ok(notification) = notification { + if let RelayPoolNotification::Event { event, .. } = notification { + // Only handle nwc events + if event.kind == Kind::WalletConnectRequest + && event.pubkey == state.nostr_service.user_keys().public_key() + && event.verify().is_ok() { + info!("Received event: {}", event.as_json()); + state.handle_event(*event).await + } else { + error!("Invalid nwc event: {}", event.as_json()); } - }, - Err(_) => {}, + } else if let RelayPoolNotification::Shutdown = notification { + info!("Relay pool shutdown"); + break; + } else { + error!("Unhandled relay pool notification: {notification:?}"); + } } } } diff --git a/fedimint-nwc/src/nwc.rs b/fedimint-nwc/src/nwc.rs index 19100d9..d6d5c66 100644 --- a/fedimint-nwc/src/nwc.rs +++ b/fedimint-nwc/src/nwc.rs @@ -84,10 +84,10 @@ async fn handle_multiple_payments( let event_clone = event.clone(); let mm = state.multimint_service.clone(); let nostr = state.nostr_service.clone(); - let mut db = state.db.clone(); - spawn(async move { - handle_nwc_params(params, method, &event_clone, &mm, &nostr, &mut db).await - }) + let db = state.db.clone(); + spawn( + async move { handle_nwc_params(params, method, &event_clone, &mm, &nostr, &db).await }, + ) .await??; } Ok(()) @@ -117,7 +117,7 @@ async fn handle_nwc_params( }; match response_result { - Ok(response) => nostr.send_encrypted_response(&event, response, d_tag).await, + Ok(response) => nostr.send_encrypted_response(event, response, d_tag).await, Err(e) => { let error_response = Response { result_type: method, @@ -125,7 +125,7 @@ async fn handle_nwc_params( result: None, }; nostr - .send_encrypted_response(&event, error_response, d_tag) + .send_encrypted_response(event, error_response, d_tag) .await } } @@ -234,16 +234,13 @@ async fn handle_lookup_invoice( None => (None, None), }; - let preimage = match invoice.clone().preimage { - Some(preimage) => Some(hex::encode(preimage)), - None => None, - }; + let preimage = invoice.clone().preimage.map(hex::encode); let settled_at = invoice.settled_at(); let created_at = invoice.created_at(); let expires_at = invoice.expires_at(); let invoice_str = invoice.invoice.to_string(); - let amount = invoice.invoice.amount_milli_satoshis().unwrap_or(0) as u64; + let amount = invoice.invoice.amount_milli_satoshis().unwrap_or(0); Ok(Response { result_type: method, @@ -255,7 +252,7 @@ async fn handle_lookup_invoice( description_hash, preimage, payment_hash, - amount: amount, + amount, fees_paid: 0, created_at, expires_at, diff --git a/fedimint-nwc/src/services/multimint.rs b/fedimint-nwc/src/services/multimint.rs index 7db8c7c..cb7c614 100644 --- a/fedimint-nwc/src/services/multimint.rs +++ b/fedimint-nwc/src/services/multimint.rs @@ -59,7 +59,7 @@ impl MultiMintService { } Err(e) => { tracing::error!("Invalid federation invite code: {}", e); - Err(e.into()) + Err(e) } } } @@ -71,7 +71,7 @@ impl MultiMintService { ) -> Result { let federation_id = match federation_id { Some(id) => id, - None => match self.default_federation_id.clone() { + None => match self.default_federation_id { Some(id) => id, None => return Err(anyhow!("No default federation id set")), }, diff --git a/fedimint-nwc/src/services/nostr.rs b/fedimint-nwc/src/services/nostr.rs index c77e1c9..806e300 100644 --- a/fedimint-nwc/src/services/nostr.rs +++ b/fedimint-nwc/src/services/nostr.rs @@ -144,7 +144,7 @@ impl NostrService { Ok(()) } - pub async fn connect(&self) -> () { + pub async fn connect(&self) { self.client.connect().await } diff --git a/fedimint-nwc/src/state.rs b/fedimint-nwc/src/state.rs index 5b1e1b7..8370fb5 100644 --- a/fedimint-nwc/src/state.rs +++ b/fedimint-nwc/src/state.rs @@ -82,8 +82,7 @@ impl AppState { let event_id = event.id; self.active_requests.lock().await.insert(event_id); - match tokio::time::timeout(Duration::from_secs(60), handle_nwc_request(&self, event)).await - { + match tokio::time::timeout(Duration::from_secs(60), handle_nwc_request(self, event)).await { Ok(Ok(_)) => {} Ok(Err(e)) => error!("Error processing request: {e}"), Err(e) => error!("Timeout error: {e}"),