From 585e808dff1e6d2c2e6b3e75877bb5eee1495e51 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 2 Oct 2023 22:36:39 +0200 Subject: [PATCH 1/2] Add recursive directory and make all endpoints JSON --- src/subcommand/server.rs | 30 +++++++++++++++++++++++++++++- tests/server.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index df85e07657..1a1e05284b 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -162,6 +162,13 @@ impl Server { let router = Router::new() .route("/", get(Self::home)) + .route("/-/blockheight", get(Self::block_height)) + .route("/-/blockhash", get(Self::block_hash_json)) + .route( + "/-/blockhash/:height", + get(Self::block_hash_from_height_json), + ) + .route("/-/blocktime", get(Self::block_time)) .route("/block/:query", get(Self::block)) .route("/blockcount", get(Self::block_count)) .route("/blockheight", get(Self::block_height)) @@ -784,6 +791,15 @@ impl Server { ) } + async fn block_hash_json(Extension(index): Extension>) -> ServerResult> { + Ok(Json( + index + .block_hash(None)? + .ok_or_not_found(|| "blockhash")? + .to_string(), + )) + } + async fn block_hash_from_height( Extension(index): Extension>, Path(height): Path, @@ -796,6 +812,18 @@ impl Server { ) } + async fn block_hash_from_height_json( + Extension(index): Extension>, + Path(height): Path, + ) -> ServerResult> { + Ok(Json( + index + .block_hash(Some(height))? + .ok_or_not_found(|| "blockhash")? + .to_string(), + )) + } + async fn block_time(Extension(index): Extension>) -> ServerResult { Ok( index @@ -875,7 +903,7 @@ impl Server { ); headers.append( header::CONTENT_SECURITY_POLICY, - HeaderValue::from_static("default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime 'unsafe-eval' 'unsafe-inline' data: blob:"), + HeaderValue::from_static("default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/-/ 'unsafe-eval' 'unsafe-inline' data: blob:"), ); headers.insert( header::CACHE_CONTROL, diff --git a/tests/server.rs b/tests/server.rs index ff69555cae..647d303594 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -189,7 +189,7 @@ fn inscription_content() { .collect::>(), &[ "default-src 'self' 'unsafe-eval' 'unsafe-inline' data: blob:", - "default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime 'unsafe-eval' 'unsafe-inline' data: blob:", + "default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/-/ 'unsafe-eval' 'unsafe-inline' data: blob:", ] ); assert_eq!(response.bytes().unwrap(), "FOO"); @@ -325,3 +325,31 @@ fn missing_credentials() { .expected_stderr("error: no bitcoind rpc user specified\n") .run_and_extract_stdout(); } + +#[test] +fn all_endpoints_in_recursive_directory_return_json() { + let rpc_server = test_bitcoincore_rpc::spawn(); + create_wallet(&rpc_server); + + rpc_server.mine_blocks(2); + + let server = TestServer::spawn_with_args(&rpc_server, &[]); + + assert_eq!(server.request("/-/blockheight").json::().unwrap(), 2); + + assert_eq!(server.request("/-/blocktime").json::().unwrap(), 2); + + assert_eq!( + server.request("/-/blockhash").json::().unwrap(), + "70a93647a8d559c7e7ff2df9bd875f5b726a2ff8ca3562003d257df5a4c47ae2" + ); + + assert_eq!( + server.request("/-/blockhash/0").json::().unwrap(), + "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" + ); + + assert!(server.request("/blockhash").json::().is_err()); + + assert!(server.request("/blockhash/2").json::().is_err()); +} From 28a2039fdb04e709210d697fdd5f762af10379cb Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 30 Oct 2023 13:32:25 -0700 Subject: [PATCH 2/2] rename recursive directory --- src/subcommand/server.rs | 10 +++++----- tests/server.rs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index f7510e5e14..0fe115bec6 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -185,13 +185,13 @@ impl Server { let router = Router::new() .route("/", get(Self::home)) - .route("/-/blockheight", get(Self::block_height)) - .route("/-/blockhash", get(Self::block_hash_json)) + .route("/r/blockheight", get(Self::block_height)) + .route("/r/blockhash", get(Self::block_hash_json)) .route( - "/-/blockhash/:height", + "/r/blockhash/:height", get(Self::block_hash_from_height_json), ) - .route("/-/blocktime", get(Self::block_time)) + .route("/r/blocktime", get(Self::block_time)) .route("/block/:query", get(Self::block)) .route("/blockcount", get(Self::block_count)) .route("/blockheight", get(Self::block_height)) @@ -982,7 +982,7 @@ impl Server { ); headers.append( header::CONTENT_SECURITY_POLICY, - HeaderValue::from_static("default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/-/ 'unsafe-eval' 'unsafe-inline' data: blob:"), + HeaderValue::from_static("default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/r/ 'unsafe-eval' 'unsafe-inline' data: blob:"), ); headers.insert( header::CACHE_CONTROL, diff --git a/tests/server.rs b/tests/server.rs index 796f8ba97f..5be237a62e 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -184,7 +184,7 @@ fn inscription_content() { .collect::>(), &[ "default-src 'self' 'unsafe-eval' 'unsafe-inline' data: blob:", - "default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/-/ 'unsafe-eval' 'unsafe-inline' data: blob:", + "default-src *:*/content/ *:*/blockheight *:*/blockhash *:*/blockhash/ *:*/blocktime *:*/r/ 'unsafe-eval' 'unsafe-inline' data: blob:", ] ); assert_eq!(response.bytes().unwrap(), "FOO"); @@ -339,17 +339,17 @@ fn all_endpoints_in_recursive_directory_return_json() { let server = TestServer::spawn_with_args(&rpc_server, &[]); - assert_eq!(server.request("/-/blockheight").json::().unwrap(), 2); + assert_eq!(server.request("/r/blockheight").json::().unwrap(), 2); - assert_eq!(server.request("/-/blocktime").json::().unwrap(), 2); + assert_eq!(server.request("/r/blocktime").json::().unwrap(), 2); assert_eq!( - server.request("/-/blockhash").json::().unwrap(), + server.request("/r/blockhash").json::().unwrap(), "70a93647a8d559c7e7ff2df9bd875f5b726a2ff8ca3562003d257df5a4c47ae2" ); assert_eq!( - server.request("/-/blockhash/0").json::().unwrap(), + server.request("/r/blockhash/0").json::().unwrap(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" );