From 77d53c9b65473ac5909d045e91538655bb363bb5 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Thu, 1 Aug 2024 13:44:49 +0300 Subject: [PATCH] extend signing test Signed-off-by: onur-ozkan --- Cargo.lock | 1 + Cargo.toml | 1 + src/proxy/http/get.rs | 52 +++++++++++++++++++++---------------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c3d192a..2071262 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1062,6 +1062,7 @@ dependencies = [ "hyper-tls", "jemallocator", "jsonwebtoken", + "libp2p", "log", "once_cell", "proxy_signature", diff --git a/Cargo.toml b/Cargo.toml index 1421eaa..61158b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ tokio = { version = "1.12.0", default-features = false, features = ["macros", "r tokio-tungstenite = { version = "0.20.0", features = ["native-tls"] } # From our sources # proxy_signature = { git = "https://github.com/KomodoPlatform/komodo-defi-framework", rev = "0784f1a" } +libp2p = { git = "https://github.com/KomodoPlatform/rust-libp2p.git", tag = "k-0.52.4", default-features = false, features = ["identify"] } proxy_signature = { path = "/home/nimda/devspace/KomodoPlatform/komodo-defi-framework/mm2src/proxy_signature" } [target.x86_64-unknown-linux-gnu.dependencies] diff --git a/src/proxy/http/get.rs b/src/proxy/http/get.rs index d0a4ba9..3f18483 100644 --- a/src/proxy/http/get.rs +++ b/src/proxy/http/get.rs @@ -104,31 +104,40 @@ fn modify_request_uri(req: &mut Request, proxy_route: &ProxyRoute) -> Gene #[cfg(test)] mod tests { + use crate::proxy::X_AUTH_PAYLOAD; + use super::*; use hyper::header::HeaderName; use hyper::Method; + use libp2p::identity; + use proxy_signature::RawMessage; + + fn generate_ed25519_keypair(mut p2p_key: [u8; 32]) -> identity::Keypair { + let secret = identity::ed25519::SecretKey::try_from_bytes(&mut p2p_key) + .expect("Secret length is 32 bytes"); + let keypair = identity::ed25519::Keypair::from(secret); + identity::Keypair::from(keypair) + } #[tokio::test] - async fn test_parse_payload() { - let serialized_payload = serde_json::json!({ - "coin_ticker": "BTC", - "address": "dummy-value", - "timestamp_message": 1655320000, - "signature": "dummy-value", - }) - .to_string(); + async fn sign_serialize_and_send() { + let keypair = generate_ed25519_keypair([0; 32]); + let proxy_sign = + RawMessage::sign(&keypair, &Uri::from_static("http://example.com"), 0, 5).unwrap(); + let serialized_proxy_sign = serde_json::to_string(&proxy_sign).unwrap(); let req = Request::builder() .method(Method::GET) .header(header::ACCEPT, HeaderValue::from_static(APPLICATION_JSON)) .header( crate::proxy::X_AUTH_PAYLOAD, - HeaderValue::from_str(&serialized_payload).unwrap(), + HeaderValue::from_str(&serialized_proxy_sign).unwrap(), ) .body(Body::empty()) .unwrap(); - let (mut req, payload) = crate::proxy::parse_auth_header(req).await.unwrap(); + let (mut req, deserialized_proxy_sign) = + crate::proxy::parse_auth_header(req).await.unwrap(); let body_bytes = hyper::body::to_bytes(req.body_mut()).await.unwrap(); assert!( @@ -136,23 +145,14 @@ mod tests { "Body should be empty for GET methods" ); - let header_value = req.headers().get(header::ACCEPT).unwrap(); - - // let expected_payload = SignedMessage { - // coin_ticker: String::from("BTC"), - // address: String::from("dummy-value"), - // timestamp_message: 1655320000, - // signature: String::from("dummy-value"), - // }; - - // assert_eq!(payload, expected_payload); - // assert_eq!(header_value, APPLICATION_JSON); + assert_eq!(deserialized_proxy_sign, proxy_sign); + assert!(deserialized_proxy_sign.is_valid_message()); - // let additional_headers = &[ - // header::CONTENT_LENGTH, - // HeaderName::from_bytes(X_AUTH_PAYLOAD.as_bytes()).unwrap(), - // ]; - // remove_hop_by_hop_headers(&mut req, additional_headers).unwrap(); + let additional_headers = &[ + header::CONTENT_LENGTH, + HeaderName::from_bytes(X_AUTH_PAYLOAD.as_bytes()).unwrap(), + ]; + remove_hop_by_hop_headers(&mut req, additional_headers).unwrap(); } #[tokio::test]