Skip to content

Commit

Permalink
Add dummy endpoint to sign transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Aug 23, 2024
1 parent 4c5de94 commit 9894ea5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions firefly-cardanosigner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
11 changes: 9 additions & 2 deletions firefly-cardanosigner/src/main.rs
Original file line number Diff line number Diff line change
@@ -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!")
Expand All @@ -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
}
29 changes: 29 additions & 0 deletions firefly-cardanosigner/src/routes.rs
Original file line number Diff line number Diff line change
@@ -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<SignTransactionRequest>,
) -> ApiResult<Json<SignTransactionResponse>> {
if req.address.is_empty() {
return Err(ApiError::not_found("No address provided"));
}
Ok(Json(SignTransactionResponse {
transaction: req.transaction,
}))
}
46 changes: 46 additions & 0 deletions firefly-server/src/error.rs
Original file line number Diff line number Diff line change
@@ -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<StatusCode>, message: impl Into<String>) -> Self {
Self {
status: status.into(),
message: message.into(),
}
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(StatusCode::NOT_FOUND, message)
}
}

impl IntoResponse for ApiError {
fn into_response(self) -> Response {
(self.status, self.message).into_response()
}
}

impl<E> From<E> for ApiError
where
E: Into<anyhow::Error>,
{
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<T, E = ApiError> = std::result::Result<T, E>;
1 change: 1 addition & 0 deletions firefly-server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod config;
pub mod error;
pub mod server;

0 comments on commit 9894ea5

Please sign in to comment.