Przeglądaj źródła

chore: remove ldk node unwraps

thesimplekid 1 miesiąc temu
rodzic
commit
c4e83b96de

+ 1 - 0
crates/cdk-common/src/database/mint/test/mod.rs

@@ -2,6 +2,7 @@
 //!
 //! This set is generic and checks the default and expected behaviour for a mint database
 //! implementation
+#![allow(clippy::unwrap_used)]
 use std::str::FromStr;
 use std::sync::atomic::{AtomicU64, Ordering};
 use std::time::{SystemTime, UNIX_EPOCH};

+ 11 - 11
crates/cdk-ldk-node/src/web/handlers/channels.rs

@@ -41,7 +41,7 @@ pub async fn channels_page(State(_state): State<AppState>) -> Result<Response, S
         .status(StatusCode::FOUND)
         .header("Location", "/balance")
         .body(Body::empty())
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 pub async fn open_channel_page(State(state): State<AppState>) -> Result<Html<String>, StatusCode> {
@@ -112,7 +112,7 @@ pub async fn post_open_channel(
                 .body(Body::from(
                     layout_with_status("Open Channel Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -132,7 +132,7 @@ pub async fn post_open_channel(
                 .body(Body::from(
                     layout_with_status("Open Channel Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -156,7 +156,7 @@ pub async fn post_open_channel(
             .body(Body::from(
                 layout_with_status("Open Channel Error", content, true).into_string(),
             ))
-            .unwrap());
+            .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
     }
 
     // Then open the channel
@@ -214,7 +214,7 @@ pub async fn post_open_channel(
         .body(Body::from(
             layout_with_status("Open Channel Result", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 pub async fn close_channel_page(
@@ -380,7 +380,7 @@ pub async fn post_close_channel(
                 .body(Body::from(
                     layout_with_status("Close Channel Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -400,7 +400,7 @@ pub async fn post_close_channel(
                 .body(Body::from(
                     layout_with_status("Close Channel Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -451,7 +451,7 @@ pub async fn post_close_channel(
         .body(Body::from(
             layout_with_status("Close Channel Result", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 pub async fn post_force_close_channel(
@@ -483,7 +483,7 @@ pub async fn post_force_close_channel(
                 .body(Body::from(
                     layout_with_status("Force Close Channel Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -503,7 +503,7 @@ pub async fn post_force_close_channel(
                 .body(Body::from(
                     layout_with_status("Force Close Channel Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -556,5 +556,5 @@ pub async fn post_force_close_channel(
         .body(Body::from(
             layout_with_status("Force Close Channel Result", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }

+ 5 - 4
crates/cdk-ldk-node/src/web/handlers/invoices.rs

@@ -151,7 +151,8 @@ pub async fn post_create_bolt11(
             Ok(desc) => Bolt11InvoiceDescription::Direct(desc),
             Err(_) => {
                 // Fallback to a minimal valid description
-                let desc = Description::new(" ".to_string()).unwrap();
+                let desc =
+                    Description::new(" ".to_string()).expect("single space is valid description");
                 Bolt11InvoiceDescription::Direct(desc)
             }
         }
@@ -175,7 +176,7 @@ pub async fn post_create_bolt11(
                     .body(Body::from(
                         layout_with_status(" Error", content, true).into_string(),
                     ))
-                    .unwrap());
+                    .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
             }
         }
     };
@@ -239,7 +240,7 @@ pub async fn post_create_bolt11(
         .body(Body::from(
             layout_with_status("BOLT11 Invoice Created", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 pub async fn post_create_bolt12(
@@ -326,5 +327,5 @@ pub async fn post_create_bolt12(
         .body(Body::from(
             layout_with_status("BOLT12 Offer Created", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }

+ 6 - 6
crates/cdk-ldk-node/src/web/handlers/onchain.rs

@@ -234,7 +234,7 @@ pub async fn post_send_onchain(
         .status(StatusCode::FOUND)
         .header("Location", format!("/onchain/confirm?{}", encoded_form))
         .body(Body::empty())
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 pub async fn onchain_confirm_page(
@@ -264,7 +264,7 @@ pub async fn onchain_confirm_page(
                 .body(Body::from(
                     layout_with_status("Send On-chain Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -290,7 +290,7 @@ pub async fn onchain_confirm_page(
                 .body(Body::from(
                     layout_with_status("Send On-chain Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
         (amount, false)
     };
@@ -364,7 +364,7 @@ pub async fn onchain_confirm_page(
         .body(Body::from(
             layout_with_status("Confirm Transaction", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 async fn execute_onchain_transaction(
@@ -397,7 +397,7 @@ async fn execute_onchain_transaction(
                 .body(Body::from(
                     layout_with_status("Send On-chain Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -476,5 +476,5 @@ async fn execute_onchain_transaction(
         .body(Body::from(
             layout_with_status("Send On-chain Result", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }

+ 7 - 7
crates/cdk-ldk-node/src/web/handlers/payments.rs

@@ -377,7 +377,7 @@ pub async fn post_pay_bolt11(
                 .body(Body::from(
                     layout_with_status("Payment Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -421,7 +421,7 @@ pub async fn post_pay_bolt11(
                 .body(Body::from(
                     layout_with_status("Payment Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -508,7 +508,7 @@ pub async fn post_pay_bolt11(
         .body(Body::from(
             layout_with_status("Payment Result", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }
 
 pub async fn post_pay_bolt12(
@@ -531,7 +531,7 @@ pub async fn post_pay_bolt12(
                 .body(Body::from(
                     layout_with_status("Payment Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -565,7 +565,7 @@ pub async fn post_pay_bolt12(
                         .body(Body::from(
                             layout_with_status("Payment Error", content, true).into_string(),
                         ))
-                        .unwrap());
+                        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
                 }
             };
             let amount_msats = amount_btc * 1_000;
@@ -599,7 +599,7 @@ pub async fn post_pay_bolt12(
                 .body(Body::from(
                     layout_with_status("Payment Error", content, true).into_string(),
                 ))
-                .unwrap());
+                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?);
         }
     };
 
@@ -693,5 +693,5 @@ pub async fn post_pay_bolt12(
         .body(Body::from(
             layout_with_status("Payment Result", content, true).into_string(),
         ))
-        .unwrap())
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?)
 }

+ 12 - 0
crates/cdk-ldk-node/src/web/handlers/utils.rs

@@ -1,10 +1,22 @@
 use std::sync::Arc;
 
+use axum::body::Body;
+use axum::http::{Response, StatusCode};
 use ldk_node::payment::PaymentDirection;
 use serde::Deserialize;
 
 use crate::CdkLdkNode;
 
+/// Build a response, converting builder errors to INTERNAL_SERVER_ERROR
+pub fn build_response(
+    builder: axum::http::response::Builder,
+    body: Body,
+) -> Result<Response<Body>, StatusCode> {
+    builder
+        .body(body)
+        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
+}
+
 #[derive(Clone)]
 pub struct AppState {
     pub node: Arc<CdkLdkNode>,

+ 7 - 2
crates/cdk-ldk-node/src/web/static_files.rs

@@ -29,12 +29,17 @@ pub async fn static_handler(Path(path): Path<String>) -> impl IntoResponse {
         Some(content) => {
             let content_type = get_content_type(cleaned_path);
             let mut headers = HeaderMap::new();
-            headers.insert(header::CONTENT_TYPE, content_type.parse().unwrap());
+            headers.insert(
+                header::CONTENT_TYPE,
+                content_type.parse().expect("valid content type"),
+            );
 
             // Add cache headers for static assets
             headers.insert(
                 header::CACHE_CONTROL,
-                "public, max-age=31536000".parse().unwrap(),
+                "public, max-age=31536000"
+                    .parse()
+                    .expect("valid cache control"),
             );
 
             (headers, content.data).into_response()

+ 2 - 0
crates/cdk-lnd/build.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unwrap_used)]
+
 fn main() -> Result<(), Box<dyn std::error::Error>> {
     println!("cargo:rerun-if-changed=src/proto/lnrpc.proto");
     println!("cargo:rerun-if-changed=src/proto/routerrpc.proto");

+ 2 - 0
crates/cdk-mint-rpc/build.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unwrap_used)]
+
 fn main() -> Result<(), Box<dyn std::error::Error>> {
     println!("cargo:rerun-if-changed=src/proto/cdk-mint-rpc.proto");
 

+ 2 - 0
crates/cdk-mintd/build.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unwrap_used)]
+
 fn main() {
     // Check that at least one database feature is enabled
     let has_database = cfg!(feature = "sqlite") || cfg!(feature = "postgres");

+ 2 - 0
crates/cdk-payment-processor/build.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unwrap_used)]
+
 fn main() -> Result<(), Box<dyn std::error::Error>> {
     println!("cargo:rerun-if-changed=src/proto/payment_processor.proto");
     tonic_build::configure()

+ 3 - 1
crates/cdk-signatory/build.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unwrap_used)]
+
 fn main() {
     println!("cargo:rerun-if-changed=src/proto/signatory.proto");
 
@@ -7,5 +9,5 @@ fn main() {
         .type_attribute(".", "#[allow(missing_docs)]")
         .field_attribute(".", "#[allow(missing_docs)]")
         .compile_protos(&["src/proto/signatory.proto"], &["src/proto"])
-        .unwrap();
+        .expect("valid proto");
 }

+ 2 - 0
crates/cdk-sql-common/build.rs

@@ -1,3 +1,5 @@
+#![allow(clippy::unwrap_used)]
+
 use std::cmp::Ordering;
 use std::env;
 use std::fs::{self, File};