Skip to content

Commit

Permalink
feat: encode and decode notes to / from json
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 23, 2024
1 parent 7fd8588 commit ec2384e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fedimint-clientd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ async fn main() -> Result<()> {
/// - `/fedimint/v2/onchain/withdraw`: Withdraw funds from the federation.
fn fedimint_v2_rest() -> Router<AppState> {
let mint_router = Router::new()
.route(
"/decode-notes",
post(fedimint::mint::decode_notes::handle_rest),
)
.route(
"/encode-notes",
post(fedimint::mint::encode_notes::handle_rest),
)
.route("/reissue", post(fedimint::mint::reissue::handle_rest))
.route("/spend", post(fedimint::mint::spend::handle_rest))
.route("/validate", post(fedimint::mint::validate::handle_rest))
Expand Down
43 changes: 43 additions & 0 deletions fedimint-clientd/src/router/handlers/fedimint/mint/decode_notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use anyhow::anyhow;
use axum::http::StatusCode;
use axum::Json;
use fedimint_mint_client::OOBNotes;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::error::AppError;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DecodeRequest {
pub notes: OOBNotes,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DecodeResponse {
pub notes_json: Value,
}

async fn _decode_notes(req: DecodeRequest) -> Result<DecodeResponse, AppError> {
let notes_json = req
.notes
.notes_json()
.map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid notes: {}", e)))?;

Ok(DecodeResponse { notes_json })
}

pub async fn handle_ws(v: Value) -> Result<Value, AppError> {
let v = serde_json::from_value::<DecodeRequest>(v)
.map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid request: {}", e)))?;
let decode = _decode_notes(v).await?;
let decode_json = json!(decode);
Ok(decode_json)
}

#[axum_macros::debug_handler]
pub async fn handle_rest(Json(req): Json<DecodeRequest>) -> Result<Json<DecodeResponse>, AppError> {
let decode = _decode_notes(req).await?;
Ok(Json(decode))
}
44 changes: 44 additions & 0 deletions fedimint-clientd/src/router/handlers/fedimint/mint/encode_notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::str::FromStr;

use anyhow::anyhow;
use axum::http::StatusCode;
use axum::Json;
use fedimint_mint_client::OOBNotes;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::error::AppError;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EncodeRequest {
pub notes_json: Value,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EncodeResponse {
pub notes: OOBNotes,
}

async fn _encode_notes(req: EncodeRequest) -> Result<EncodeResponse, AppError> {
let notes_str = req.notes_json.to_string();
let notes = OOBNotes::from_str(&notes_str)
.map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid notes: {}", e)))?;

Ok(EncodeResponse { notes })
}

pub async fn handle_ws(v: Value) -> Result<Value, AppError> {
let v = serde_json::from_value::<EncodeRequest>(v)
.map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid request: {}", e)))?;
let encode = _encode_notes(v).await?;
let encode_json = json!(encode);
Ok(encode_json)
}

#[axum_macros::debug_handler]
pub async fn handle_rest(Json(req): Json<EncodeRequest>) -> Result<Json<EncodeResponse>, AppError> {
let decode = _encode_notes(req).await?;
Ok(Json(decode))
}
2 changes: 2 additions & 0 deletions fedimint-clientd/src/router/handlers/fedimint/mint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod combine;
pub mod decode_notes;
pub mod encode_notes;
pub mod reissue;
pub mod spend;
pub mod split;
Expand Down
8 changes: 8 additions & 0 deletions fedimint-clientd/src/router/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum JsonRpcMethod {
AdminModule,
AdminRestore,
AdminListOperations,
MintDecodeNotes,
MintEncodeNotes,
MintReissue,
MintSpend,
MintValidate,
Expand Down Expand Up @@ -170,6 +172,12 @@ async fn match_method(req: JsonRpcRequest, state: AppState) -> Result<Value, App
JsonRpcMethod::AdminListOperations => {
handlers::fedimint::admin::list_operations::handle_ws(state.clone(), req.params).await
}
JsonRpcMethod::MintDecodeNotes => {
handlers::fedimint::mint::decode_notes::handle_ws(req.params).await
}
JsonRpcMethod::MintEncodeNotes => {
handlers::fedimint::mint::encode_notes::handle_ws(req.params).await
}
JsonRpcMethod::MintReissue => {
handlers::fedimint::mint::reissue::handle_ws(state.clone(), req.params).await
}
Expand Down

0 comments on commit ec2384e

Please sign in to comment.