-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: encode and decode notes to / from json
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
fedimint-clientd/src/router/handlers/fedimint/mint/decode_notes.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
fedimint-clientd/src/router/handlers/fedimint/mint/encode_notes.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¬es_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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters