cory 0.1.2

Visual Bitcoin transaction ancestry explorer with embedded web UI and REST API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
mod auth;
mod error;
mod graph;
mod history;
mod labels;
mod limits;
mod static_files;

use std::collections::HashMap;
use std::sync::Arc;

use axum::extract::DefaultBodyLimit;
use axum::routing::{any, delete, get, post};
use axum::{Json, Router};
use tokio::sync::RwLock;
use tower_http::cors::{AllowOrigin, CorsLayer};

use cory_core::cache::Cache;
use cory_core::labels::LabelStore;
use cory_core::rpc::BitcoinRpc;
use cory_core::types::GraphLimits;

// ==============================================================================
// Application State
// ==============================================================================

pub struct AppState {
    pub rpc: Arc<dyn BitcoinRpc>,
    pub cache: Arc<Cache>,
    pub labels: Arc<RwLock<LabelStore>>,
    pub api_token: String,
    pub default_limits: GraphLimits,
    pub rpc_concurrency: usize,
    pub network: bitcoin::Network,
    pub history: Arc<RwLock<HashMap<String, String>>>,
}

type SharedState = Arc<AppState>;

// ==============================================================================
// Router
// ==============================================================================

pub fn build_router(state: AppState, origin: &str) -> Router {
    // Only reflect the allowed origin when the request's Origin header
    // actually matches. Otherwise, omit the header entirely so browsers
    // get a clean CORS rejection instead of a mismatched origin value.
    let allowed: axum::http::HeaderValue = origin.parse().expect("valid origin header value");
    let cors = CorsLayer::new()
        .allow_origin(AllowOrigin::predicate({
            let allowed = allowed.clone();
            move |request_origin: &axum::http::HeaderValue, _| *request_origin == allowed
        }))
        .allow_methods([
            axum::http::Method::GET,
            axum::http::Method::POST,
            axum::http::Method::DELETE,
            axum::http::Method::OPTIONS,
        ])
        .allow_headers([
            axum::http::header::CONTENT_TYPE,
            axum::http::header::HeaderName::from_static("x-api-token"),
        ]);

    let shared = Arc::new(state);

    let public_api = Router::new()
        .route("/api/v1/health", get(health))
        .route("/api/v1/limits", get(limits::get_limits));

    // Label mutation routes get a 2 MB body limit to prevent abuse via
    // oversized import payloads. Graph and other routes use Axum's default.
    const LABEL_BODY_LIMIT: usize = 2 * 1024 * 1024;

    let label_routes = Router::new()
        .route(
            "/api/v1/label",
            get(labels::list_label_files).post(labels::create_or_import_local_label_file),
        )
        .route(
            "/api/v1/label/{file_id}",
            post(labels::upsert_or_replace_local_label_file)
                .delete(labels::delete_local_label_file),
        )
        .route(
            "/api/v1/label/{file_id}/entry",
            delete(labels::delete_local_label_entry),
        )
        .route(
            "/api/v1/label/{file_id}/export",
            get(labels::export_local_label_file),
        )
        .layer(DefaultBodyLimit::max(LABEL_BODY_LIMIT));

    let protected_api = Router::new()
        .route("/api/v1/graph/tx/{txid}", get(graph::get_graph))
        .route("/api/v1/history", get(history::get_history))
        .route("/api/v1/labels.zip", get(labels::zip_browser_labels))
        .merge(label_routes);

    Router::new()
        .merge(public_api)
        .merge(protected_api)
        .route("/api", any(api_not_found))
        .route("/api/{*path}", any(api_not_found))
        .fallback(static_files::static_files)
        .layer(cors)
        .with_state(shared)
}

async fn health() -> Json<serde_json::Value> {
    Json(serde_json::json!({ "status": "ok" }))
}

async fn api_not_found() -> error::AppError {
    error::AppError::NotFound("API route not found".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use axum::body::{to_bytes, Body};
    use axum::http::{Request, StatusCode};
    use bitcoin::hashes::Hash;
    use bitcoin::{Amount, ScriptBuf, Txid};
    use cory_core::error::{CoreError, RpcError};
    use cory_core::types::{ScriptType, TxInput, TxNode, TxOutput};
    use tower::ServiceExt;

    #[derive(Clone, Copy)]
    enum FakeRpcMode {
        Ok,
        NotFound,
        InvalidTxData,
        RpcFailure,
    }

    struct FakeRpc {
        mode: FakeRpcMode,
    }

    #[async_trait]
    impl BitcoinRpc for FakeRpc {
        async fn get_transaction(&self, txid: &Txid) -> Result<TxNode, CoreError> {
            match self.mode {
                FakeRpcMode::Ok => Ok(sample_tx(*txid)),
                FakeRpcMode::NotFound => Err(CoreError::TxNotFound(*txid)),
                FakeRpcMode::InvalidTxData => {
                    Err(CoreError::InvalidTxData("invalid tx fixture".to_string()))
                }
                FakeRpcMode::RpcFailure => Err(CoreError::Rpc(RpcError::ServerError {
                    code: -28,
                    message: "Loading block index...".to_string(),
                })),
            }
        }

        async fn get_tx_out(
            &self,
            _txid: &Txid,
            _vout: u32,
        ) -> Result<Option<TxOutput>, CoreError> {
            Ok(None)
        }

        async fn get_blockchain_info(&self) -> Result<cory_core::rpc::ChainInfo, CoreError> {
            Ok(cory_core::rpc::ChainInfo {
                chain: "regtest".to_string(),
                blocks: 1,
                best_block_hash: bitcoin::BlockHash::all_zeros(),
                pruned: false,
            })
        }
    }

    fn sample_tx(txid: Txid) -> TxNode {
        TxNode {
            txid,
            version: 2,
            locktime: 0,
            size: 100,
            vsize: 100,
            weight: 400,
            block_hash: None,
            block_height: None,
            inputs: vec![TxInput {
                prevout: None,
                sequence: 0xFFFF_FFFF,
                value: None,
                script_type: None,
            }],
            outputs: vec![TxOutput {
                value: Amount::from_sat(1_000),
                script_pub_key: ScriptBuf::new(),
                script_type: ScriptType::Unknown,
            }],
        }
    }

    fn test_router(mode: FakeRpcMode) -> Router {
        test_router_with_limits(mode, GraphLimits::default())
    }

    fn test_router_with_limits(mode: FakeRpcMode, default_limits: GraphLimits) -> Router {
        let state = AppState {
            rpc: Arc::new(FakeRpc { mode }),
            cache: Arc::new(Cache::with_capacity(100, 100)),
            labels: Arc::new(RwLock::new(LabelStore::new())),
            api_token: "test-token".to_string(),
            default_limits,
            rpc_concurrency: 4,
            network: bitcoin::Network::Regtest,
            history: Arc::new(RwLock::new(HashMap::new())),
        };
        build_router(state, "http://127.0.0.1:3080")
    }

    fn txid_str(byte: u8) -> String {
        Txid::from_slice(&[byte; 32])
            .expect("test txid bytes must parse")
            .to_string()
    }

    async fn response_body_json(resp: axum::response::Response) -> serde_json::Value {
        let bytes = to_bytes(resp.into_body(), 1024 * 1024)
            .await
            .expect("response body must be readable");
        serde_json::from_slice(&bytes).expect("response body must be valid JSON")
    }

    #[tokio::test]
    async fn unknown_api_route_returns_json_404() {
        let router = test_router(FakeRpcMode::Ok);
        let response = router
            .oneshot(
                Request::builder()
                    .uri("/api/v1/does-not-exist")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        let json = response_body_json(response).await;
        assert_eq!(
            json.get("error").and_then(serde_json::Value::as_str),
            Some("API route not found")
        );
    }

    #[tokio::test]
    async fn limits_endpoint_exposes_hard_configured_and_effective_values() {
        let router = test_router_with_limits(
            FakeRpcMode::Ok,
            GraphLimits {
                max_depth: 5_000,
                max_nodes: 80_000,
                max_edges: 300_000,
            },
        );
        let response = router
            .oneshot(
                Request::builder()
                    .uri("/api/v1/limits")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::OK);
        let json = response_body_json(response).await;

        assert_eq!(
            json.get("hard_max_depth")
                .and_then(serde_json::Value::as_u64),
            Some(limits::HARD_MAX_DEPTH as u64)
        );
        assert_eq!(
            json.get("configured_default_depth")
                .and_then(serde_json::Value::as_u64),
            Some(5_000)
        );
        assert_eq!(
            json.get("effective_default_depth")
                .and_then(serde_json::Value::as_u64),
            Some(limits::HARD_MAX_DEPTH as u64)
        );

        assert_eq!(
            json.get("hard_max_nodes")
                .and_then(serde_json::Value::as_u64),
            Some(limits::HARD_MAX_NODES as u64)
        );
        assert_eq!(
            json.get("configured_default_nodes")
                .and_then(serde_json::Value::as_u64),
            Some(80_000)
        );
        assert_eq!(
            json.get("effective_default_nodes")
                .and_then(serde_json::Value::as_u64),
            Some(limits::HARD_MAX_NODES as u64)
        );

        assert_eq!(
            json.get("hard_max_edges")
                .and_then(serde_json::Value::as_u64),
            Some(limits::HARD_MAX_EDGES as u64)
        );
        assert_eq!(
            json.get("configured_default_edges")
                .and_then(serde_json::Value::as_u64),
            Some(300_000)
        );
        assert_eq!(
            json.get("effective_default_edges")
                .and_then(serde_json::Value::as_u64),
            Some(limits::HARD_MAX_EDGES as u64)
        );
    }

    #[tokio::test]
    async fn graph_zero_limits_return_bad_request() {
        let router = test_router(FakeRpcMode::Ok);
        let url = format!("/api/v1/graph/tx/{}?max_nodes=0", txid_str(1));
        let response = router
            .oneshot(
                Request::builder()
                    .uri(url)
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        let json = response_body_json(response).await;
        assert_eq!(
            json.get("error").and_then(serde_json::Value::as_str),
            Some("max_nodes must be at least 1")
        );
    }

    #[tokio::test]
    async fn graph_limit_above_configured_max_returns_bad_request() {
        let router = test_router_with_limits(
            FakeRpcMode::Ok,
            GraphLimits {
                max_depth: 2,
                max_nodes: 500,
                max_edges: 2000,
            },
        );
        let url = format!("/api/v1/graph/tx/{}?max_depth={}", txid_str(1), 3);
        let response = router
            .oneshot(
                Request::builder()
                    .uri(url)
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        let json = response_body_json(response).await;
        assert_eq!(
            json.get("error").and_then(serde_json::Value::as_str),
            Some("max_depth must be at most 2")
        );
    }

    #[tokio::test]
    async fn graph_tx_not_found_maps_to_404() {
        let router = test_router(FakeRpcMode::NotFound);
        let response = router
            .oneshot(
                Request::builder()
                    .uri(format!("/api/v1/graph/tx/{}", txid_str(2)))
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn graph_invalid_tx_data_maps_to_400() {
        let router = test_router(FakeRpcMode::InvalidTxData);
        let response = router
            .oneshot(
                Request::builder()
                    .uri(format!("/api/v1/graph/tx/{}", txid_str(3)))
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn graph_rpc_failure_maps_to_502() {
        let router = test_router(FakeRpcMode::RpcFailure);
        let response = router
            .oneshot(
                Request::builder()
                    .uri(format!("/api/v1/graph/tx/{}", txid_str(4)))
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
    }

    #[tokio::test]
    async fn graph_request_records_history_by_default() {
        let router = test_router(FakeRpcMode::Ok);
        let txid = txid_str(5);

        let graph_response = router
            .clone()
            .oneshot(
                Request::builder()
                    .uri(format!("/api/v1/graph/tx/{txid}"))
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");
        assert_eq!(graph_response.status(), StatusCode::OK);

        let history_response = router
            .oneshot(
                Request::builder()
                    .uri("/api/v1/history")
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");
        assert_eq!(history_response.status(), StatusCode::OK);

        let json = response_body_json(history_response).await;
        let entries = json
            .get("entries")
            .and_then(serde_json::Value::as_array)
            .expect("history response must include entries array");
        assert_eq!(entries.len(), 1);
        assert_eq!(
            entries[0]
                .get("txid")
                .and_then(serde_json::Value::as_str)
                .expect("history entry must include txid"),
            txid
        );
    }

    #[tokio::test]
    async fn graph_request_can_skip_history_recording() {
        let router = test_router(FakeRpcMode::Ok);
        let txid = txid_str(6);

        let graph_response = router
            .clone()
            .oneshot(
                Request::builder()
                    .uri(format!("/api/v1/graph/tx/{txid}?record_history=false"))
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");
        assert_eq!(graph_response.status(), StatusCode::OK);

        let history_response = router
            .oneshot(
                Request::builder()
                    .uri("/api/v1/history")
                    .header("x-api-token", "test-token")
                    .body(Body::empty())
                    .expect("request must build"),
            )
            .await
            .expect("router should serve request");
        assert_eq!(history_response.status(), StatusCode::OK);

        let json = response_body_json(history_response).await;
        let entries = json
            .get("entries")
            .and_then(serde_json::Value::as_array)
            .expect("history response must include entries array");
        assert!(entries.is_empty());
    }
}