diff --git a/fedimint-clientd/src/router/handlers/fedimint/mint/encode_notes.rs b/fedimint-clientd/src/router/handlers/fedimint/mint/encode_notes.rs index d72d853..17957e4 100644 --- a/fedimint-clientd/src/router/handlers/fedimint/mint/encode_notes.rs +++ b/fedimint-clientd/src/router/handlers/fedimint/mint/encode_notes.rs @@ -3,16 +3,18 @@ use std::str::FromStr; use anyhow::anyhow; use axum::http::StatusCode; use axum::Json; +use fedimint_core::config::FederationIdPrefix; use fedimint_mint_client::OOBNotes; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; +use super::OOBNotesJson; use crate::error::AppError; #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct EncodeRequest { - pub notes_json: Value, + pub notes_json_str: String, } #[derive(Debug, Serialize)] @@ -22,9 +24,11 @@ pub struct EncodeResponse { } async fn _encode_notes(req: EncodeRequest) -> Result { - let notes_str = req.notes_json.to_string(); - let notes = OOBNotes::from_str(¬es_str) + let notes = serde_json::from_str::(&req.notes_json_str) .map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid notes: {}", e)))?; + let prefix = FederationIdPrefix::from_str(¬es.federation_id_prefix) + .map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid prefix: {}", e)))?; + let notes = OOBNotes::new(prefix, notes.notes); Ok(EncodeResponse { notes }) } diff --git a/fedimint-clientd/src/router/handlers/fedimint/mint/mod.rs b/fedimint-clientd/src/router/handlers/fedimint/mint/mod.rs index fdbc11c..ee92920 100644 --- a/fedimint-clientd/src/router/handlers/fedimint/mint/mod.rs +++ b/fedimint-clientd/src/router/handlers/fedimint/mint/mod.rs @@ -1,3 +1,7 @@ +use fedimint_core::TieredMulti; +use fedimint_mint_client::SpendableNote; +use serde::{Deserialize, Serialize}; + pub mod combine; pub mod decode_notes; pub mod encode_notes; @@ -5,3 +9,9 @@ pub mod reissue; pub mod spend; pub mod split; pub mod validate; + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct OOBNotesJson { + federation_id_prefix: String, + notes: TieredMulti, +} diff --git a/wrappers/fedimint-ts/FedimintClient.ts b/wrappers/fedimint-ts/FedimintClient.ts index 60dea54..bc3fedc 100644 --- a/wrappers/fedimint-ts/FedimintClient.ts +++ b/wrappers/fedimint-ts/FedimintClient.ts @@ -35,6 +35,11 @@ import type { OnchainDepositAddressResponse, OnchainWithdrawRequest, OnchainWithdrawResponse, + MintDecodeNotesRequest, + MintEncodeNotesRequest, + NotesJson, + MintEncodeNotesResponse, + MintDecodeNotesResponse, } from "./types"; type FedimintResponse = Promise; @@ -404,6 +409,39 @@ class FedimintClient { * A module for interacting with an ecash mint */ public mint = { + /** + * Decodes hex encoded binary ecash notes to json + */ + decodeNotes: async ( + notes: string + ): FedimintResponse => { + const request: MintDecodeNotesRequest = { + notes, + }; + + return await this.post( + "/mint/decode-notes", + request + ); + }, + + /** + * Encodes json notes to hex encoded binary notes + */ + encodeNotes: async ( + notesJson: NotesJson + ): FedimintResponse => { + const request: MintEncodeNotesRequest = { + notesJsonStr: JSON.stringify(notesJson), + }; + console.log("request: ", request); + + return await this.post( + "/mint/encode-notes", + request + ); + }, + /** * Reissues an ecash note */ diff --git a/wrappers/fedimint-ts/test.ts b/wrappers/fedimint-ts/test.ts index 1306829..d1b7c53 100644 --- a/wrappers/fedimint-ts/test.ts +++ b/wrappers/fedimint-ts/test.ts @@ -160,6 +160,14 @@ async function main() { { amountMsat: 3000, allowOverpay: true, timeout: 1000 }, data ); + // `/v2/mint/decode-notes` + logMethod("/v2/mint/decode-notes"); + data = await fedimintClient.mint.decodeNotes(mintData.notes); + logInputAndOutput({ notes: mintData.notes }, data); + // `/v2/mint/encode-notes` + logMethod("/v2/mint/encode-notes"); + data = await fedimintClient.mint.encodeNotes(data.notesJson); + logInputAndOutput({ notesJson: data.notesJson }, data); // `/v2/mint/validate` logMethod("/v2/mint/validate"); data = await fedimintClient.mint.validate(mintData.notes); diff --git a/wrappers/fedimint-ts/types.ts b/wrappers/fedimint-ts/types.ts index 262d413..88ada07 100644 --- a/wrappers/fedimint-ts/types.ts +++ b/wrappers/fedimint-ts/types.ts @@ -180,6 +180,22 @@ interface TieredMulti { [amount: number]: T[]; } +interface MintDecodeNotesRequest { + notes: string; +} + +interface MintDecodeNotesResponse { + notesJson: NotesJson; +} + +interface MintEncodeNotesRequest { + notesJsonStr: string; +} + +interface MintEncodeNotesResponse { + notes: string; +} + interface MintReissueRequest { notes: string; } @@ -224,6 +240,16 @@ interface MintCombineResponse { notes: string; } +interface NotesJson { + federation_id_prefix: string; + notes: { + [denomination: string]: Array<{ + signature: string; + spend_key: string; + }>; + }; +} + export type { Tiered, TieredSummary, @@ -254,7 +280,12 @@ export type { LnPayResponse, LnAwaitPayRequest, Gateway, + NotesJson, SwitchGatewayRequest, + MintDecodeNotesRequest, + MintDecodeNotesResponse, + MintEncodeNotesRequest, + MintEncodeNotesResponse, MintReissueRequest, MintReissueResponse, MintSpendRequest,