From 9894ea52508bf267f57a8552a78d44e5cc411a94 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Fri, 23 Aug 2024 14:12:04 -0400 Subject: [PATCH] Add dummy endpoint to sign transaction --- Cargo.lock | 1 + firefly-cardanosigner/Cargo.toml | 1 + firefly-cardanosigner/src/main.rs | 11 +++++-- firefly-cardanosigner/src/routes.rs | 29 ++++++++++++++++++ firefly-server/src/error.rs | 46 +++++++++++++++++++++++++++++ firefly-server/src/lib.rs | 1 + 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 firefly-cardanosigner/src/routes.rs create mode 100644 firefly-server/src/error.rs diff --git a/Cargo.lock b/Cargo.lock index f94561e..52ef2d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -419,6 +419,7 @@ dependencies = [ "axum", "clap", "firefly-server", + "schemars", "serde", "tokio", ] diff --git a/firefly-cardanosigner/Cargo.toml b/firefly-cardanosigner/Cargo.toml index 8f4ded4..8613ac0 100644 --- a/firefly-cardanosigner/Cargo.toml +++ b/firefly-cardanosigner/Cargo.toml @@ -10,5 +10,6 @@ axum = "0.7" anyhow = "1" clap = { version = "4", features = ["derive"] } firefly-server = { path = "../firefly-server" } +schemars = "0.8" serde = "1" tokio = { version = "1", features = ["full"] } \ No newline at end of file diff --git a/firefly-cardanosigner/src/main.rs b/firefly-cardanosigner/src/main.rs index d000e62..5613506 100644 --- a/firefly-cardanosigner/src/main.rs +++ b/firefly-cardanosigner/src/main.rs @@ -1,12 +1,17 @@ use std::path::PathBuf; -use aide::axum::{routing::get, ApiRouter, IntoApiResponse}; +use aide::axum::{ + routing::{get, post}, + ApiRouter, IntoApiResponse, +}; use anyhow::Result; use axum::Json; use clap::Parser; use config::load_config; +use routes::sign_transaction; mod config; +mod routes; async fn health() -> impl IntoApiResponse { Json("Hello, world!") @@ -26,6 +31,8 @@ async fn main() -> Result<()> { let config_file = args.config_file.as_deref(); let config = load_config(config_file)?; - let router = ApiRouter::new().api_route("/api/health", get(health)); + let router = ApiRouter::new() + .api_route("/api/health", get(health)) + .api_route("/api/sign", post(sign_transaction)); firefly_server::server::serve(&config.api, router).await } diff --git a/firefly-cardanosigner/src/routes.rs b/firefly-cardanosigner/src/routes.rs new file mode 100644 index 0000000..8d36aae --- /dev/null +++ b/firefly-cardanosigner/src/routes.rs @@ -0,0 +1,29 @@ +use axum::Json; +use firefly_server::error::{ApiError, ApiResult}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, JsonSchema)] +pub struct SignTransactionRequest { + /// The address of the key to sign the transaction with. + address: String, + /// The raw CBOR-encoded transaction to sign. + transaction: String, +} + +#[derive(Serialize, JsonSchema)] +pub struct SignTransactionResponse { + /// The CBOR-encoded signed transaction. + transaction: String, +} + +pub async fn sign_transaction( + Json(req): Json, +) -> ApiResult> { + if req.address.is_empty() { + return Err(ApiError::not_found("No address provided")); + } + Ok(Json(SignTransactionResponse { + transaction: req.transaction, + })) +} diff --git a/firefly-server/src/error.rs b/firefly-server/src/error.rs new file mode 100644 index 0000000..2a5cf39 --- /dev/null +++ b/firefly-server/src/error.rs @@ -0,0 +1,46 @@ +use aide::OperationOutput; +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, +}; + +pub struct ApiError { + status: StatusCode, + message: String, +} + +impl ApiError { + pub fn new(status: impl Into, message: impl Into) -> Self { + Self { + status: status.into(), + message: message.into(), + } + } + pub fn not_found(message: impl Into) -> Self { + Self::new(StatusCode::NOT_FOUND, message) + } +} + +impl IntoResponse for ApiError { + fn into_response(self) -> Response { + (self.status, self.message).into_response() + } +} + +impl From for ApiError +where + E: Into, +{ + fn from(value: E) -> Self { + Self { + status: StatusCode::INTERNAL_SERVER_ERROR, + message: value.into().to_string(), + } + } +} + +impl OperationOutput for ApiError { + type Inner = Self; +} + +pub type ApiResult = std::result::Result; diff --git a/firefly-server/src/lib.rs b/firefly-server/src/lib.rs index 6d51ea9..55e21d7 100644 --- a/firefly-server/src/lib.rs +++ b/firefly-server/src/lib.rs @@ -1,2 +1,3 @@ pub mod config; +pub mod error; pub mod server;