Skip to main content

fedimint_gateway_ui/
lib.rs

1mod bitcoin;
2mod connect_fed;
3mod federation;
4mod general;
5mod lightning;
6mod mnemonic;
7mod payment_summary;
8mod setup;
9
10use std::collections::BTreeMap;
11use std::fmt::Display;
12use std::sync::Arc;
13
14use ::bitcoin::{Address, Txid};
15use async_trait::async_trait;
16use axum::body::Body;
17use axum::extract::{Query, State};
18use axum::http::header;
19use axum::response::{Html, IntoResponse, Redirect, Response};
20use axum::routing::{get, post};
21use axum::{Form, Router};
22use axum_extra::extract::CookieJar;
23use axum_extra::extract::cookie::{Cookie, SameSite};
24use fedimint_core::bitcoin::Network;
25use fedimint_core::config::FederationId;
26use fedimint_core::invite_code::InviteCode;
27use fedimint_core::secp256k1::serde::Deserialize;
28use fedimint_core::task::TaskGroup;
29use fedimint_core::{PeerId, TieredCounts};
30use fedimint_gateway_common::{
31    ChainSource, CloseChannelsWithPeerRequest, CloseChannelsWithPeerResponse, ConnectFedPayload,
32    CreateInvoiceForOperatorPayload, CreateOfferPayload, CreateOfferResponse,
33    DepositAddressPayload, FederationInfo, GatewayBalances, GatewayInfo, LeaveFedPayload,
34    LightningMode, ListTransactionsPayload, ListTransactionsResponse, MnemonicResponse,
35    OpenChannelRequest, PayInvoiceForOperatorPayload, PayOfferPayload, PayOfferResponse,
36    PaymentLogPayload, PaymentLogResponse, PaymentSummaryPayload, PaymentSummaryResponse,
37    ReceiveEcashPayload, ReceiveEcashResponse, SendOnchainRequest, SetFeesPayload,
38    SetMnemonicPayload, SpendEcashPayload, SpendEcashResponse, WithdrawPayload,
39    WithdrawPreviewPayload, WithdrawPreviewResponse, WithdrawResponse,
40};
41use fedimint_ln_common::contracts::Preimage;
42use fedimint_logging::LOG_GATEWAY_UI;
43use fedimint_ui_common::assets::WithStaticRoutesExt;
44use fedimint_ui_common::auth::UserAuth;
45use fedimint_ui_common::{
46    LOGIN_ROUTE, LoginInput, ROOT_ROUTE, UiState, dashboard_layout,
47    login_form as render_login_form, single_card_layout,
48};
49use lightning_invoice::Bolt11Invoice;
50use maud::html;
51use tracing::debug;
52
53use crate::connect_fed::connect_federation_handler;
54use crate::federation::{
55    deposit_address_handler, leave_federation_handler, receive_ecash_handler, set_fees_handler,
56    spend_ecash_handler, withdraw_confirm_handler, withdraw_preview_handler,
57};
58use crate::lightning::{
59    channels_fragment_handler, close_channel_handler, create_bolt11_invoice_handler,
60    create_receive_invoice_handler, detect_payment_type_handler, generate_receive_address_handler,
61    open_channel_handler, pay_bolt11_invoice_handler, pay_unified_handler,
62    payments_fragment_handler, send_onchain_handler, set_channel_fees_handler,
63    transactions_fragment_handler, wallet_fragment_handler,
64};
65use crate::mnemonic::{mnemonic_iframe_handler, mnemonic_reveal_handler};
66use crate::payment_summary::payment_log_fragment_handler;
67use crate::setup::{create_wallet_handler, recover_wallet_form, recover_wallet_handler};
68pub type DynGatewayApi<E> = Arc<dyn IAdminGateway<Error = E> + Send + Sync + 'static>;
69
70pub(crate) const OPEN_CHANNEL_ROUTE: &str = "/ui/channels/open";
71pub(crate) const CLOSE_CHANNEL_ROUTE: &str = "/ui/channels/close";
72pub(crate) const SET_CHANNEL_FEES_ROUTE: &str = "/ui/channels/fees";
73pub(crate) const CHANNEL_FRAGMENT_ROUTE: &str = "/ui/channels/fragment";
74pub(crate) const LEAVE_FEDERATION_ROUTE: &str = "/ui/federations/{id}/leave";
75pub(crate) const CONNECT_FEDERATION_ROUTE: &str = "/ui/federations/join";
76pub(crate) const SET_FEES_ROUTE: &str = "/ui/federation/set-fees";
77pub(crate) const SEND_ONCHAIN_ROUTE: &str = "/ui/wallet/send";
78pub(crate) const WALLET_FRAGMENT_ROUTE: &str = "/ui/wallet/fragment";
79pub(crate) const LN_ONCHAIN_ADDRESS_ROUTE: &str = "/ui/wallet/receive";
80pub(crate) const DEPOSIT_ADDRESS_ROUTE: &str = "/ui/federations/deposit-address";
81pub(crate) const PAYMENTS_FRAGMENT_ROUTE: &str = "/ui/payments/fragment";
82pub(crate) const CREATE_BOLT11_INVOICE_ROUTE: &str = "/ui/payments/receive/bolt11";
83pub(crate) const CREATE_RECEIVE_INVOICE_ROUTE: &str = "/ui/payments/receive";
84pub(crate) const PAY_BOLT11_INVOICE_ROUTE: &str = "/ui/payments/send/bolt11";
85pub(crate) const PAY_UNIFIED_ROUTE: &str = "/ui/payments/send";
86pub(crate) const DETECT_PAYMENT_TYPE_ROUTE: &str = "/ui/payments/detect";
87pub(crate) const TRANSACTIONS_FRAGMENT_ROUTE: &str = "/ui/transactions/fragment";
88pub(crate) const RECEIVE_ECASH_ROUTE: &str = "/ui/federations/receive";
89pub(crate) const STOP_GATEWAY_ROUTE: &str = "/ui/stop";
90pub(crate) const WITHDRAW_PREVIEW_ROUTE: &str = "/ui/federations/withdraw-preview";
91pub(crate) const WITHDRAW_CONFIRM_ROUTE: &str = "/ui/federations/withdraw-confirm";
92pub(crate) const SPEND_ECASH_ROUTE: &str = "/ui/federations/spend";
93pub(crate) const PAYMENT_LOG_ROUTE: &str = "/ui/payment-log";
94pub(crate) const CREATE_WALLET_ROUTE: &str = "/ui/wallet/create";
95pub(crate) const RECOVER_WALLET_ROUTE: &str = "/ui/wallet/recover";
96pub(crate) const MNEMONIC_IFRAME_ROUTE: &str = "/ui/mnemonic/iframe";
97pub(crate) const EXPORT_INVITE_CODES_ROUTE: &str = "/ui/export-invite-codes";
98
99#[derive(Default, Deserialize)]
100pub struct DashboardQuery {
101    pub success: Option<String>,
102    pub ui_error: Option<String>,
103    pub show_export_reminder: Option<bool>,
104}
105
106fn redirect_success(msg: String) -> impl IntoResponse {
107    let encoded: String = url::form_urlencoded::byte_serialize(msg.as_bytes()).collect();
108    Redirect::to(&format!("/?success={}", encoded))
109}
110
111pub(crate) fn redirect_success_with_export_reminder(msg: String) -> impl IntoResponse {
112    let encoded: String = url::form_urlencoded::byte_serialize(msg.as_bytes()).collect();
113    Redirect::to(&format!("/?success={}&show_export_reminder=true", encoded))
114}
115
116fn redirect_error(msg: String) -> impl IntoResponse {
117    let encoded: String = url::form_urlencoded::byte_serialize(msg.as_bytes()).collect();
118    Redirect::to(&format!("/?ui_error={}", encoded))
119}
120
121pub fn is_allowed_setup_route(path: &str) -> bool {
122    path == ROOT_ROUTE
123        || path == LOGIN_ROUTE
124        || path.starts_with("/assets/")
125        || path == CREATE_WALLET_ROUTE
126        || path == RECOVER_WALLET_ROUTE
127}
128
129#[async_trait]
130pub trait IAdminGateway {
131    type Error;
132
133    async fn handle_get_info(&self) -> Result<GatewayInfo, Self::Error>;
134
135    async fn handle_list_channels_msg(
136        &self,
137    ) -> Result<Vec<fedimint_gateway_common::ChannelInfo>, Self::Error>;
138
139    async fn handle_payment_summary_msg(
140        &self,
141        PaymentSummaryPayload {
142            start_millis,
143            end_millis,
144        }: PaymentSummaryPayload,
145    ) -> Result<PaymentSummaryResponse, Self::Error>;
146
147    async fn handle_leave_federation(
148        &self,
149        payload: LeaveFedPayload,
150    ) -> Result<FederationInfo, Self::Error>;
151
152    async fn handle_connect_federation(
153        &self,
154        payload: ConnectFedPayload,
155    ) -> Result<FederationInfo, Self::Error>;
156
157    async fn handle_set_fees_msg(&self, payload: SetFeesPayload) -> Result<(), Self::Error>;
158
159    async fn handle_mnemonic_msg(&self) -> Result<MnemonicResponse, Self::Error>;
160
161    async fn handle_open_channel_msg(
162        &self,
163        payload: OpenChannelRequest,
164    ) -> Result<Txid, Self::Error>;
165
166    async fn handle_close_channels_with_peer_msg(
167        &self,
168        payload: CloseChannelsWithPeerRequest,
169    ) -> Result<CloseChannelsWithPeerResponse, Self::Error>;
170
171    async fn handle_set_channel_fees_msg(
172        &self,
173        payload: fedimint_gateway_common::SetChannelFeesRequest,
174    ) -> Result<(), Self::Error>;
175
176    async fn handle_get_balances_msg(&self) -> Result<GatewayBalances, Self::Error>;
177
178    async fn handle_send_onchain_msg(
179        &self,
180        payload: SendOnchainRequest,
181    ) -> Result<Txid, Self::Error>;
182
183    async fn handle_get_ln_onchain_address_msg(&self) -> Result<Address, Self::Error>;
184
185    async fn handle_deposit_address_msg(
186        &self,
187        payload: DepositAddressPayload,
188    ) -> Result<Address, Self::Error>;
189
190    async fn handle_receive_ecash_msg(
191        &self,
192        payload: ReceiveEcashPayload,
193    ) -> Result<ReceiveEcashResponse, Self::Error>;
194
195    async fn handle_create_invoice_for_operator_msg(
196        &self,
197        payload: CreateInvoiceForOperatorPayload,
198    ) -> Result<Bolt11Invoice, Self::Error>;
199
200    async fn handle_pay_invoice_for_operator_msg(
201        &self,
202        payload: PayInvoiceForOperatorPayload,
203    ) -> Result<Preimage, Self::Error>;
204
205    async fn handle_list_transactions_msg(
206        &self,
207        payload: ListTransactionsPayload,
208    ) -> Result<ListTransactionsResponse, Self::Error>;
209
210    async fn handle_spend_ecash_msg(
211        &self,
212        payload: SpendEcashPayload,
213    ) -> Result<SpendEcashResponse, Self::Error>;
214
215    async fn handle_shutdown_msg(&self, task_group: TaskGroup) -> Result<(), Self::Error>;
216
217    fn get_task_group(&self) -> TaskGroup;
218
219    async fn handle_withdraw_msg(
220        &self,
221        payload: WithdrawPayload,
222    ) -> Result<WithdrawResponse, Self::Error>;
223
224    async fn handle_withdraw_preview_msg(
225        &self,
226        payload: WithdrawPreviewPayload,
227    ) -> Result<WithdrawPreviewResponse, Self::Error>;
228
229    async fn handle_payment_log_msg(
230        &self,
231        payload: PaymentLogPayload,
232    ) -> Result<PaymentLogResponse, Self::Error>;
233
234    async fn handle_export_invite_codes(
235        &self,
236    ) -> BTreeMap<FederationId, BTreeMap<PeerId, (String, InviteCode)>>;
237
238    fn get_password_hash(&self) -> String;
239
240    fn gatewayd_version(&self) -> String;
241
242    async fn get_chain_source(&self) -> (ChainSource, Network);
243
244    fn lightning_mode(&self) -> LightningMode;
245
246    async fn is_configured(&self) -> bool;
247
248    async fn handle_set_mnemonic_msg(&self, payload: SetMnemonicPayload)
249    -> Result<(), Self::Error>;
250
251    async fn handle_create_offer_for_operator_msg(
252        &self,
253        payload: CreateOfferPayload,
254    ) -> Result<CreateOfferResponse, Self::Error>;
255
256    async fn handle_pay_offer_for_operator_msg(
257        &self,
258        payload: PayOfferPayload,
259    ) -> Result<PayOfferResponse, Self::Error>;
260
261    async fn handle_get_note_summary_msg(
262        &self,
263        federation_id: &FederationId,
264    ) -> Result<TieredCounts, Self::Error>;
265}
266
267async fn login_form_handler<E>(
268    State(_state): State<UiState<DynGatewayApi<E>>>,
269) -> impl IntoResponse {
270    Html(single_card_layout("Enter Password", render_login_form(None)).into_string())
271}
272
273// Dashboard login submit handler
274async fn login_submit<E>(
275    State(state): State<UiState<DynGatewayApi<E>>>,
276    jar: CookieJar,
277    Form(input): Form<LoginInput>,
278) -> impl IntoResponse {
279    if let Ok(verify) = bcrypt::verify(&input.password, &state.api.get_password_hash())
280        && verify
281    {
282        let mut cookie = Cookie::new(state.auth_cookie_name.clone(), state.auth_cookie_value);
283        cookie.set_path(ROOT_ROUTE);
284
285        cookie.set_http_only(true);
286        cookie.set_same_site(Some(SameSite::Lax));
287
288        let jar = jar.add(cookie);
289        return (jar, [("HX-Redirect", "/")]).into_response();
290    }
291
292    Html(render_login_form(Some("The password is invalid")).into_string()).into_response()
293}
294
295async fn dashboard_view<E>(
296    State(state): State<UiState<DynGatewayApi<E>>>,
297    _auth: UserAuth,
298    Query(msg): Query<DashboardQuery>,
299) -> impl IntoResponse
300where
301    E: std::fmt::Display,
302{
303    // If gateway is not configured, show setup view instead of dashboard
304    if !state.api.is_configured().await {
305        return setup::setup_view(State(state), Query(msg))
306            .await
307            .into_response();
308    }
309
310    let gatewayd_version = state.api.gatewayd_version();
311    debug!(target: LOG_GATEWAY_UI, "Getting gateway info...");
312    let gateway_info = match state.api.handle_get_info().await {
313        Ok(info) => info,
314        Err(err) => {
315            let content = html! {
316                div class="alert alert-danger mt-4" {
317                    strong { "Failed to fetch gateway info: " }
318                    (err.to_string())
319                }
320            };
321            return Html(dashboard_layout(content, &gatewayd_version, None).into_string())
322                .into_response();
323        }
324    };
325
326    let content = html! {
327
328       (federation::scripts())
329
330        @if let Some(success) = msg.success {
331            div class="alert alert-success mt-2 d-flex justify-content-between align-items-center" {
332                span {
333                    (success)
334                    @if msg.show_export_reminder.unwrap_or(false) {
335                        " "
336                        a href=(EXPORT_INVITE_CODES_ROUTE) { "Export your invite codes for backup." }
337                    }
338                }
339                a href=(ROOT_ROUTE)
340                class="ms-3 text-decoration-none text-dark fw-bold"
341                style="font-size: 1.5rem; line-height: 1; cursor: pointer;"
342                { "×" }
343            }
344        }
345        @if let Some(error) = msg.ui_error {
346            div class="alert alert-danger mt-2 d-flex justify-content-between align-items-center" {
347                span { (error) }
348                a href=(ROOT_ROUTE)
349                class="ms-3 text-decoration-none text-dark fw-bold"
350                style="font-size: 1.5rem; line-height: 1; cursor: pointer;"
351                { "×" }
352            }
353        }
354
355        div class="row mt-4" {
356            div class="col-md-12 text-end" {
357                a href=(EXPORT_INVITE_CODES_ROUTE) class="btn btn-outline-primary me-2" {
358                    "Export Invite Codes"
359                }
360                form action=(STOP_GATEWAY_ROUTE) method="post" style="display: inline;" {
361                    button class="btn btn-outline-danger" type="submit"
362                        onclick="return confirm('Are you sure you want to safely stop the gateway? The gateway will wait for outstanding payments and then shutdown.');"
363                    {
364                        "Safely Stop Gateway"
365                    }
366                }
367            }
368        }
369
370        div class="row gy-4" {
371            div class="col-md-6" {
372                (general::render(&gateway_info))
373            }
374            div class="col-md-6" {
375                (payment_summary::render(&state.api, &gateway_info.federations).await)
376            }
377        }
378
379        div class="row gy-4 mt-2" {
380            div class="col-md-6" {
381                (bitcoin::render(&state.api).await)
382            }
383            div class="col-md-6" {
384                (mnemonic::render())
385            }
386        }
387
388        div class="row gy-4 mt-2" {
389            div class="col-md-12" {
390                (lightning::render(&gateway_info, &state.api).await)
391            }
392        }
393
394        div class="row gy-4 mt-2" {
395            div class="col-md-12" {
396                (connect_fed::render(&gateway_info.gateway_state))
397            }
398        }
399
400        @let invite_codes = state.api.handle_export_invite_codes().await;
401        @let empty_map = BTreeMap::new();
402
403        @for fed in &gateway_info.federations {
404            @let fed_codes = invite_codes.get(&fed.federation_id).unwrap_or(&empty_map);
405            @let note_summary = state.api.handle_get_note_summary_msg(&fed.federation_id).await;
406            (federation::render(fed, fed_codes, &note_summary))
407        }
408    };
409
410    Html(dashboard_layout(content, &gatewayd_version, None).into_string()).into_response()
411}
412
413async fn stop_gateway_handler<E>(
414    State(state): State<UiState<DynGatewayApi<E>>>,
415    _auth: UserAuth,
416) -> impl IntoResponse
417where
418    E: std::fmt::Display,
419{
420    match state
421        .api
422        .handle_shutdown_msg(state.api.get_task_group())
423        .await
424    {
425        Ok(_) => redirect_success("Gateway is safely shutting down...".to_string()).into_response(),
426        Err(err) => redirect_error(format!("Failed to stop gateway: {err}")).into_response(),
427    }
428}
429
430async fn export_invite_codes_handler<E>(
431    State(state): State<UiState<DynGatewayApi<E>>>,
432    _auth: UserAuth,
433) -> impl IntoResponse
434where
435    E: std::fmt::Display,
436{
437    let invite_codes: BTreeMap<FederationId, Vec<InviteCode>> = state
438        .api
439        .handle_export_invite_codes()
440        .await
441        .into_iter()
442        .map(|(fed_id, peers)| {
443            let codes = peers.into_values().map(|(_, code)| code).collect();
444            (fed_id, codes)
445        })
446        .collect();
447    let json = match serde_json::to_string_pretty(&invite_codes) {
448        Ok(json) => json,
449        Err(err) => {
450            return Response::builder()
451                .status(500)
452                .body(Body::from(format!(
453                    "Failed to serialize invite codes: {err}"
454                )))
455                .expect("Failed to build error response");
456        }
457    };
458    let filename = "gateway-invite-codes.json";
459
460    Response::builder()
461        .header(header::CONTENT_TYPE, "application/json")
462        .header(
463            header::CONTENT_DISPOSITION,
464            format!("attachment; filename=\"{filename}\""),
465        )
466        .body(Body::from(json))
467        .expect("Failed to build response")
468}
469
470pub fn router<E: Display + Send + Sync + std::fmt::Debug + 'static>(
471    api: DynGatewayApi<E>,
472) -> Router {
473    let app = Router::new()
474        .route(ROOT_ROUTE, get(dashboard_view))
475        .route(LOGIN_ROUTE, get(login_form_handler).post(login_submit))
476        .route(OPEN_CHANNEL_ROUTE, post(open_channel_handler))
477        .route(CLOSE_CHANNEL_ROUTE, post(close_channel_handler))
478        .route(SET_CHANNEL_FEES_ROUTE, post(set_channel_fees_handler))
479        .route(CHANNEL_FRAGMENT_ROUTE, get(channels_fragment_handler))
480        .route(WALLET_FRAGMENT_ROUTE, get(wallet_fragment_handler))
481        .route(LEAVE_FEDERATION_ROUTE, post(leave_federation_handler))
482        .route(CONNECT_FEDERATION_ROUTE, post(connect_federation_handler))
483        .route(SET_FEES_ROUTE, post(set_fees_handler))
484        .route(SEND_ONCHAIN_ROUTE, post(send_onchain_handler))
485        .route(
486            LN_ONCHAIN_ADDRESS_ROUTE,
487            get(generate_receive_address_handler),
488        )
489        .route(DEPOSIT_ADDRESS_ROUTE, post(deposit_address_handler))
490        .route(SPEND_ECASH_ROUTE, post(spend_ecash_handler))
491        .route(RECEIVE_ECASH_ROUTE, post(receive_ecash_handler))
492        .route(PAYMENTS_FRAGMENT_ROUTE, get(payments_fragment_handler))
493        .route(
494            CREATE_BOLT11_INVOICE_ROUTE,
495            post(create_bolt11_invoice_handler),
496        )
497        .route(
498            CREATE_RECEIVE_INVOICE_ROUTE,
499            post(create_receive_invoice_handler),
500        )
501        .route(PAY_BOLT11_INVOICE_ROUTE, post(pay_bolt11_invoice_handler))
502        .route(PAY_UNIFIED_ROUTE, post(pay_unified_handler))
503        .route(DETECT_PAYMENT_TYPE_ROUTE, post(detect_payment_type_handler))
504        .route(
505            TRANSACTIONS_FRAGMENT_ROUTE,
506            get(transactions_fragment_handler),
507        )
508        .route(STOP_GATEWAY_ROUTE, post(stop_gateway_handler))
509        .route(EXPORT_INVITE_CODES_ROUTE, get(export_invite_codes_handler))
510        .route(WITHDRAW_PREVIEW_ROUTE, post(withdraw_preview_handler))
511        .route(WITHDRAW_CONFIRM_ROUTE, post(withdraw_confirm_handler))
512        .route(PAYMENT_LOG_ROUTE, get(payment_log_fragment_handler))
513        .route(CREATE_WALLET_ROUTE, post(create_wallet_handler))
514        .route(
515            RECOVER_WALLET_ROUTE,
516            get(recover_wallet_form).post(recover_wallet_handler),
517        )
518        .route(
519            MNEMONIC_IFRAME_ROUTE,
520            get(mnemonic_iframe_handler).post(mnemonic_reveal_handler),
521        )
522        .with_static_routes();
523
524    app.with_state(UiState::new(api))
525}