Skip to content

Commit

Permalink
feat: encode and decode notes working clientd and ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 23, 2024
1 parent ec2384e commit 461db57
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -22,9 +24,11 @@ pub struct EncodeResponse {
}

async fn _encode_notes(req: EncodeRequest) -> Result<EncodeResponse, AppError> {
let notes_str = req.notes_json.to_string();
let notes = OOBNotes::from_str(&notes_str)
let notes = serde_json::from_str::<OOBNotesJson>(&req.notes_json_str)
.map_err(|e| AppError::new(StatusCode::BAD_REQUEST, anyhow!("Invalid notes: {}", e)))?;
let prefix = FederationIdPrefix::from_str(&notes.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 })
}
Expand Down
10 changes: 10 additions & 0 deletions fedimint-clientd/src/router/handlers/fedimint/mint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use fedimint_core::TieredMulti;
use fedimint_mint_client::SpendableNote;
use serde::{Deserialize, Serialize};

pub mod combine;
pub mod decode_notes;
pub mod encode_notes;
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<SpendableNote>,
}
38 changes: 38 additions & 0 deletions wrappers/fedimint-ts/FedimintClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ import type {
OnchainDepositAddressResponse,
OnchainWithdrawRequest,
OnchainWithdrawResponse,
MintDecodeNotesRequest,
MintEncodeNotesRequest,
NotesJson,
MintEncodeNotesResponse,
MintDecodeNotesResponse,
} from "./types";

type FedimintResponse<T> = Promise<T>;
Expand Down Expand Up @@ -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<MintDecodeNotesResponse> => {
const request: MintDecodeNotesRequest = {
notes,
};

return await this.post<MintDecodeNotesResponse>(
"/mint/decode-notes",
request
);
},

/**
* Encodes json notes to hex encoded binary notes
*/
encodeNotes: async (
notesJson: NotesJson
): FedimintResponse<MintEncodeNotesResponse> => {
const request: MintEncodeNotesRequest = {
notesJsonStr: JSON.stringify(notesJson),
};
console.log("request: ", request);

return await this.post<MintEncodeNotesResponse>(
"/mint/encode-notes",
request
);
},

/**
* Reissues an ecash note
*/
Expand Down
8 changes: 8 additions & 0 deletions wrappers/fedimint-ts/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions wrappers/fedimint-ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ interface TieredMulti<T> {
[amount: number]: T[];
}

interface MintDecodeNotesRequest {
notes: string;
}

interface MintDecodeNotesResponse {
notesJson: NotesJson;
}

interface MintEncodeNotesRequest {
notesJsonStr: string;
}

interface MintEncodeNotesResponse {
notes: string;
}

interface MintReissueRequest {
notes: string;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -254,7 +280,12 @@ export type {
LnPayResponse,
LnAwaitPayRequest,
Gateway,
NotesJson,
SwitchGatewayRequest,
MintDecodeNotesRequest,
MintDecodeNotesResponse,
MintEncodeNotesRequest,
MintEncodeNotesResponse,
MintReissueRequest,
MintReissueResponse,
MintSpendRequest,
Expand Down

0 comments on commit 461db57

Please sign in to comment.