Skip to content

Commit

Permalink
feat: fedimint 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 20, 2024
1 parent 7d21095 commit 3f8cfbb
Show file tree
Hide file tree
Showing 31 changed files with 763 additions and 520 deletions.
728 changes: 450 additions & 278 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions fedimint-clientd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ serde_json = "1.0.108"
tokio = { version = "1.34.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
fedimint-client = "0.2.2"
fedimint-core = "0.2.2"
fedimint-wallet-client = "0.2.2"
fedimint-mint-client = "0.2.2"
fedimint-ln-client = "0.2.2"
fedimint-rocksdb = "0.2.2"
fedimint-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.0" }
fedimint-core = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.0" }
fedimint-wallet-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.0" }
fedimint-mint-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.0" }
fedimint-ln-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.0" }
fedimint-rocksdb = { git = "https://github.com/fedimint/fedimint", tag = "v0.3.0-rc.0" }
url = "2.5.0"
lazy_static = "1.4.0"
async-utility = "0.2.0"
tower-http = { version = "0.5.0", features = ["cors", "auth", "trace"] }
tower-http = { version = "0.5.2", features = ["cors", "auth", "trace"] }
bitcoin = "0.29.2"
itertools = "0.12.0"
lnurl-rs = { version = "0.4.0", features = ["async"], default-features = false }
reqwest = "0.11.23"
lnurl-rs = { version = "0.4.1", features = ["async"], default-features = false }
reqwest = "0.11.27"
lightning-invoice = { version = "0.26.0", features = ["serde"] }
bitcoin_hashes = "0.11.0"
bitcoin_hashes = "0.13.0"
time = { version = "0.3.25", features = ["formatting"] }
chrono = "0.4.31"
futures-util = "0.3.30"
clap = { version = "4.4.13", features = ["derive", "env"] }
multimint = "0.1.7"
multimint = { version = "0.1.12", path = "../multimint" }
axum-otel-metrics = "0.8.0"

[patch.crates-io]
Expand Down
9 changes: 4 additions & 5 deletions fedimint-clientd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ async fn main() -> Result<()> {

match InviteCode::from_str(&cli.invite_code) {
Ok(invite_code) => {
let federation_id = state.multimint.register_new(invite_code, manual_secret).await?;
let federation_id = state
.multimint
.register_new(invite_code, manual_secret)
.await?;
info!("Created client for federation id: {:?}", federation_id);
if cli.mode == Mode::Cashu {
state.cashu_mint = Some(federation_id);
Expand Down Expand Up @@ -210,10 +213,6 @@ fn fedimint_v2_rest() -> Router<AppState> {
.route(
"/list-gateways",
post(fedimint::ln::list_gateways::handle_rest),
)
.route(
"/switch-gateway",
post(fedimint::ln::switch_gateway::handle_rest),
);

let wallet_router = Router::new()
Expand Down
6 changes: 3 additions & 3 deletions fedimint-clientd/src/router/handlers/cashu/melt/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::anyhow;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::Amount;
use fedimint_ln_client::{LightningClientModule, OutgoingLightningPayment};
Expand Down Expand Up @@ -64,7 +64,7 @@ pub async fn handle_method(
}

pub async fn melt_bolt11(
client: ClientArc,
client: ClientHandleArc,
request: String,
amount_msat: Amount,
) -> Result<PostMeltQuoteMethodResponse, AppError> {
Expand Down Expand Up @@ -108,7 +108,7 @@ pub async fn melt_bolt11(
}

async fn melt_onchain(
client: ClientArc,
client: ClientHandleArc,
request: String,
amount_sat: bitcoin::Amount,
) -> Result<PostMeltQuoteMethodResponse, AppError> {
Expand Down
6 changes: 3 additions & 3 deletions fedimint-clientd/src/router/handlers/cashu/mint/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::anyhow;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::time::now;
use fedimint_core::Amount;
Expand Down Expand Up @@ -60,7 +60,7 @@ const DEFAULT_MINT_EXPIRY_OFFSET: u64 = 3600;
const DEFAULT_MINT_DESCRIPTION: &str = "Cashu mint operation";

pub async fn mint_bolt11(
client: ClientArc,
client: ClientHandleArc,
amount_msat: Amount,
) -> Result<PostMintQuoteMethodResponse, AppError> {
let lightning_module = client.get_first_module::<LightningClientModule>();
Expand All @@ -87,7 +87,7 @@ pub async fn mint_bolt11(
}

async fn mint_onchain(
client: ClientArc,
client: ClientHandleArc,
_amount_sat: Amount,
) -> Result<PostMintQuoteMethodResponse, AppError> {
let wallet_client = client.get_first_module::<WalletClientModule>();
Expand Down
4 changes: 2 additions & 2 deletions fedimint-clientd/src/router/handlers/fedimint/admin/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::backup::Metadata;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use serde::Deserialize;
use serde_json::{json, Value};
Expand All @@ -20,7 +20,7 @@ pub struct BackupRequest {
pub federation_id: FederationId,
}

async fn _backup(client: ClientArc, req: BackupRequest) -> Result<(), AppError> {
async fn _backup(client: ClientHandleArc, req: BackupRequest) -> Result<(), AppError> {
client
.backup_to_federation(Metadata::from_json_serialized(req.metadata))
.await
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
use std::collections::HashMap;

use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use multimint::MultiMint;
use serde::Deserialize;
use serde_json::{json, Value};

use crate::error::AppError;
use crate::state::AppState;

async fn _discover_version(multimint: MultiMint) -> Result<Value, AppError> {
#[derive(Debug, Clone, Deserialize)]
pub struct DiscoverVersionRequest {
threshold: Option<usize>,
}

async fn _discover_version(multimint: MultiMint, threshold: Option<usize>) -> Result<Value, AppError> {
let mut api_versions = HashMap::new();
for (id, client) in multimint.clients.lock().await.iter() {
api_versions.insert(
*id,
json!({"version" : client.discover_common_api_version().await?}),
json!({"version" : client.discover_common_api_version(threshold).await?}),
);
}
Ok(json!(api_versions))
}

pub async fn handle_ws(state: AppState) -> Result<Value, AppError> {
let version = _discover_version(state.multimint).await?;
pub async fn handle_ws(state: AppState, v: Value) -> Result<Value, AppError> {
let v = serde_json::from_value::<DiscoverVersionRequest>(v).map_err(|e| {
AppError::new(
StatusCode::BAD_REQUEST,
anyhow::anyhow!("Invalid request: {}", e),
)
})?;
let version = _discover_version(state.multimint, v.threshold).await?;
let version_json = json!(version);
Ok(version_json)
}

#[axum_macros::debug_handler]
pub async fn handle_rest(State(state): State<AppState>) -> Result<Json<Value>, AppError> {
let version = _discover_version(state.multimint).await?;
pub async fn handle_rest(
State(state): State<AppState>,
Json(req): Json<DiscoverVersionRequest>,
) -> Result<Json<Value>, AppError> {
let version = _discover_version(state.multimint, req.threshold).await?;
Ok(Json(version))
}
5 changes: 3 additions & 2 deletions fedimint-clientd/src/router/handlers/fedimint/admin/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ async fn _join(mut multimint: MultiMint, req: JoinRequest) -> Result<JoinRespons
match env::var("FEDIMINT_CLIENTD_MANUAL_SECRET") {
Ok(secret) => Some(secret),
Err(_) => {
return Err(anyhow!("FEDIMINT_CLIENTD_MANUAL_SECRET must be set to join with manual secret"))
return Err(anyhow!(
"FEDIMINT_CLIENTD_MANUAL_SECRET must be set to join with manual secret"
))
}
}
} else {
None
};


let _ = multimint
.register_new(req.invite_code.clone(), manual_secret)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::UNIX_EPOCH;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::core::OperationId;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct OperationOutput {
}

async fn _list_operations(
client: ClientArc,
client: ClientHandleArc,
req: ListOperationsRequest,
) -> Result<Value, AppError> {
const ISO8601_CONFIG: iso8601::EncodedConfig = iso8601::Config::DEFAULT
Expand Down
4 changes: 2 additions & 2 deletions fedimint-clientd/src/router/handlers/fedimint/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub mod list_operations;
pub mod module;
pub mod restore;

use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_mint_client::MintClientModule;
use fedimint_wallet_client::WalletClientModule;
use info::InfoResponse;

pub async fn get_note_summary(client: &ClientArc) -> anyhow::Result<InfoResponse> {
pub async fn get_note_summary(client: &ClientHandleArc) -> anyhow::Result<InfoResponse> {
let mint_client = client.get_first_module::<MintClientModule>();
let wallet_client = client.get_first_module::<WalletClientModule>();
let summary = mint_client
Expand Down
4 changes: 2 additions & 2 deletions fedimint-clientd/src/router/handlers/fedimint/admin/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::anyhow;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::core::{ModuleInstanceId, ModuleKind};
use serde::Deserialize;
Expand All @@ -25,7 +25,7 @@ pub struct ModuleRequest {
pub federation_id: FederationId,
}

async fn _module(_client: ClientArc, _req: ModuleRequest) -> Result<(), AppError> {
async fn _module(_client: ClientHandleArc, _req: ModuleRequest) -> Result<(), AppError> {
// TODO: Figure out how to impl this
Err(AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use anyhow::anyhow;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use serde_json::{json, Value};

use crate::error::AppError;
use crate::state::AppState;

async fn _restore(_client: ClientArc, _v: Value) -> Result<(), AppError> {
async fn _restore(_client: ClientHandleArc, _v: Value) -> Result<(), AppError> {
// TODO: unimplemented in cli
Err(AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::anyhow;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::core::OperationId;
use fedimint_ln_client::{LightningClientModule, LnReceiveState};
Expand All @@ -24,7 +24,7 @@ pub struct AwaitInvoiceRequest {
}

async fn _await_invoice(
client: ClientArc,
client: ClientHandleArc,
req: AwaitInvoiceRequest,
) -> Result<InfoResponse, AppError> {
let lightning_module = &client.get_first_module::<LightningClientModule>();
Expand Down
7 changes: 5 additions & 2 deletions fedimint-clientd/src/router/handlers/fedimint/ln/await_pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Context};
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::core::OperationId;
use fedimint_ln_client::{LightningClientModule, PayType};
Expand All @@ -21,7 +21,10 @@ pub struct AwaitLnPayRequest {
pub federation_id: FederationId,
}

async fn _await_pay(client: ClientArc, req: AwaitLnPayRequest) -> Result<LnPayResponse, AppError> {
async fn _await_pay(
client: ClientHandleArc,
req: AwaitLnPayRequest,
) -> Result<LnPayResponse, AppError> {
let lightning_module = client.get_first_module::<LightningClientModule>();
let ln_pay_details = lightning_module
.get_ln_pay_details_for(req.operation_id)
Expand Down
37 changes: 32 additions & 5 deletions fedimint-clientd/src/router/handlers/fedimint/ln/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use anyhow::anyhow;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use fedimint_client::ClientArc;
use fedimint_client::ClientHandleArc;
use fedimint_core::config::FederationId;
use fedimint_core::core::OperationId;
use fedimint_core::Amount;
use fedimint_ln_client::LightningClientModule;
use lightning_invoice::{Bolt11InvoiceDescription, Description};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tracing::error;

use crate::error::AppError;
use crate::state::AppState;
Expand All @@ -29,12 +31,37 @@ pub struct LnInvoiceResponse {
pub invoice: String,
}

async fn _invoice(client: ClientArc, req: LnInvoiceRequest) -> Result<LnInvoiceResponse, AppError> {
async fn _invoice(
client: ClientHandleArc,
req: LnInvoiceRequest,
) -> Result<LnInvoiceResponse, AppError> {
let lightning_module = client.get_first_module::<LightningClientModule>();
lightning_module.select_active_gateway().await?;
let gateway_id = match lightning_module.list_gateways().await.first() {
Some(gateway_announcement) => gateway_announcement.info.gateway_id,
None => {
error!("No gateways available");
return Err(AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
anyhow!("No gateways available"),
))
}
};
let gateway = lightning_module.select_gateway(&gateway_id).await.ok_or_else(|| {
error!("Failed to select gateway");
AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
anyhow!("Failed to select gateway"),
)
})?;

let (operation_id, invoice) = lightning_module
.create_bolt11_invoice(req.amount_msat, req.description, req.expiry_time, ())
let (operation_id, invoice, _) = lightning_module
.create_bolt11_invoice(
req.amount_msat,
Bolt11InvoiceDescription::Direct(&Description::new(req.description)?),
req.expiry_time,
(),
Some(gateway),
)
.await?;
Ok(LnInvoiceResponse {
operation_id,
Expand Down
Loading

0 comments on commit 3f8cfbb

Please sign in to comment.