pub mod handlers;
pub mod types;
use anyhow::Result;
use axum::extract::{DefaultBodyLimit, Request};
use axum::http::{HeaderMap, StatusCode};
use axum::middleware::{self, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use bsv_wallet_toolbox::{Chain, Services, StorageSqlx, Wallet};
use serde_json::json;
use std::net::SocketAddr;
use std::sync::Arc;
use tower_http::cors::CorsLayer;
use tower_http::trace::TraceLayer;
use handlers::WalletState;
pub type SpendingLock = Arc<tokio::sync::Mutex<()>>;
#[derive(Clone)]
#[allow(dead_code)]
pub struct TlsConfig {
pub cert_path: String,
pub key_path: String,
}
#[derive(Clone)]
pub struct ServerConfig {
pub auth_token: Option<String>,
pub tls: Option<TlsConfig>,
pub chain: Chain,
pub bind_addr: std::net::IpAddr,
pub callback_token: Option<String>,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
auth_token: None,
tls: None,
chain: Chain::Main,
bind_addr: std::net::IpAddr::from([127, 0, 0, 1]),
callback_token: None,
}
}
}
async fn auth_middleware(headers: HeaderMap, request: Request, next: Next) -> Response {
if request.uri().path() == "/arc-callback" {
return next.run(request).await;
}
let token = request
.extensions()
.get::<ServerConfig>()
.and_then(|c| c.auth_token.clone());
if let Some(expected) = token {
let provided = headers
.get("authorization")
.and_then(|v| v.to_str().ok())
.and_then(|v| v.strip_prefix("Bearer "));
match provided {
Some(t) if t == expected => {}
_ => {
return (
StatusCode::UNAUTHORIZED,
Json(json!({"code": "UNAUTHORIZED", "message": "Invalid or missing bearer token"})),
)
.into_response();
}
}
}
next.run(request).await
}
pub fn make_router(wallet: WalletState, config: ServerConfig) -> Router {
let cors = CorsLayer::very_permissive();
let cfg = config.clone();
let spending_lock: SpendingLock = Arc::new(tokio::sync::Mutex::new(()));
let verifier = crate::broadcast_verify::BroadcastVerifier::from_env(config.chain);
Router::new()
.route(
"/isAuthenticated",
get(handlers::is_authenticated).post(handlers::is_authenticated),
)
.route("/getPublicKey", post(handlers::get_public_key))
.route("/createSignature", post(handlers::create_signature))
.route("/createAction", post(handlers::create_action))
.route("/internalizeAction", post(handlers::internalize_action))
.route(
"/getHeight",
get(handlers::get_height).post(handlers::get_height),
)
.route(
"/getNetwork",
get(handlers::get_network).post(handlers::get_network),
)
.route(
"/getVersion",
get(handlers::get_version).post(handlers::get_version),
)
.route(
"/waitForAuthentication",
get(handlers::wait_for_authentication).post(handlers::wait_for_authentication),
)
.route("/getHeaderForHeight", post(handlers::get_header_for_height))
.route("/verifySignature", post(handlers::verify_signature))
.route("/encrypt", post(handlers::encrypt))
.route("/decrypt", post(handlers::decrypt))
.route("/createHmac", post(handlers::create_hmac))
.route("/verifyHmac", post(handlers::verify_hmac))
.route("/signAction", post(handlers::sign_action))
.route("/abortAction", post(handlers::abort_action))
.route("/listActions", post(handlers::list_actions))
.route("/listOutputs", post(handlers::list_outputs))
.route("/relinquishOutput", post(handlers::relinquish_output))
.route("/acquireCertificate", post(handlers::acquire_certificate))
.route("/listCertificates", post(handlers::list_certificates))
.route("/proveCertificate", post(handlers::prove_certificate))
.route(
"/relinquishCertificate",
post(handlers::relinquish_certificate),
)
.route(
"/discoverByIdentityKey",
post(handlers::discover_by_identity_key),
)
.route(
"/discoverByAttributes",
post(handlers::discover_by_attributes),
)
.route(
"/revealCounterpartyKeyLinkage",
post(handlers::reveal_counterparty_key_linkage),
)
.route(
"/revealSpecificKeyLinkage",
post(handlers::reveal_specific_key_linkage),
)
.route("/arc-callback", post(arc_callback))
.layer(cors)
.layer(middleware::from_fn(auth_middleware))
.layer(axum::Extension(cfg))
.layer(axum::Extension(spending_lock))
.layer(axum::Extension(verifier))
.layer(TraceLayer::new_for_http())
.layer(DefaultBodyLimit::max(50 * 1024 * 1024))
.with_state(wallet)
}
async fn arc_callback(
axum::extract::State(wallet): axum::extract::State<WalletState>,
request: Request,
) -> Response {
let expected = request
.extensions()
.get::<ServerConfig>()
.and_then(|c| c.callback_token.clone());
let Some(expected) = expected else {
return (
StatusCode::NOT_FOUND,
Json(json!({"code": "CALLBACK_DISABLED", "message": "no callback token configured"})),
)
.into_response();
};
let headers = request.headers().clone();
let provided = headers
.get("authorization")
.and_then(|v| v.to_str().ok())
.and_then(|v| v.strip_prefix("Bearer "))
.map(str::to_string)
.or_else(|| {
headers
.get("x-callbacktoken")
.and_then(|v| v.to_str().ok())
.map(str::to_string)
});
if provided.as_deref() != Some(expected.as_str()) {
tracing::warn!(
has_authorization = headers.contains_key("authorization"),
has_x_callbacktoken = headers.contains_key("x-callbacktoken"),
"arc-callback: rejected POST with invalid/missing callback token"
);
return (
StatusCode::UNAUTHORIZED,
Json(json!({"code": "UNAUTHORIZED", "message": "invalid or missing callback token"})),
)
.into_response();
}
let bytes = match axum::body::to_bytes(request.into_body(), 1_000_000).await {
Ok(b) => b,
Err(_) => {
return (
StatusCode::PAYLOAD_TOO_LARGE,
Json(json!({"code": "TOO_LARGE", "message": "payload too large"})),
)
.into_response();
}
};
let payload: serde_json::Value = match serde_json::from_slice(&bytes) {
Ok(v) => v,
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(json!({"code": "BAD_JSON", "message": e.to_string()})),
)
.into_response();
}
};
match crate::arc_ingest::ingest_arc_payload(wallet.storage(), &payload).await {
Ok(action) => (
StatusCode::OK,
Json(json!({"ok": true, "action": format!("{:?}", action)})),
)
.into_response(),
Err(e) => (
StatusCode::BAD_REQUEST,
Json(json!({"code": "BAD_PAYLOAD", "message": e.to_string()})),
)
.into_response(),
}
}
pub async fn run(wallet: WalletState, port: u16, config: ServerConfig) -> Result<()> {
let tls = config.tls.clone();
let bind_addr = config.bind_addr;
let app = make_router(wallet, config);
let addr = SocketAddr::from((bind_addr, port));
#[cfg(feature = "tls")]
if let Some(tls_cfg) = tls {
use axum_server::tls_rustls::RustlsConfig;
let rustls = RustlsConfig::from_pem_file(&tls_cfg.cert_path, &tls_cfg.key_path).await?;
tracing::info!("HTTPS server listening on {addr}");
eprintln!("HTTPS server listening on {addr}");
axum_server::bind_rustls(addr, rustls)
.serve(app.into_make_service())
.await?;
return Ok(());
}
#[cfg(not(feature = "tls"))]
if tls.is_some() {
anyhow::bail!("TLS requested but binary was built without `--features tls`");
}
tracing::info!("HTTP server listening on {addr}");
eprintln!("HTTP server listening on {addr}");
let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
pub fn make_wallet_state(wallet: Wallet<StorageSqlx, Services>) -> WalletState {
Arc::new(wallet)
}
#[allow(dead_code)] pub fn make_wallet_state_from_arc(wallet: Arc<Wallet<StorageSqlx, Services>>) -> WalletState {
wallet
}
async fn shutdown_signal() {
tokio::signal::ctrl_c()
.await
.expect("failed to listen for ctrl-c");
eprintln!("\nShutting down...");
}