Skip to main content

fedimint_server_ui/
setup.rs

1use std::collections::BTreeSet;
2
3use axum::Router;
4use axum::extract::State;
5use axum::response::{Html, IntoResponse, Redirect};
6use axum::routing::{get, post};
7use axum_extra::extract::Form;
8use axum_extra::extract::cookie::CookieJar;
9use fedimint_core::core::ModuleKind;
10use fedimint_core::module::ApiAuth;
11use fedimint_server_core::setup_ui::DynSetupApi;
12use fedimint_ui_common::assets::WithStaticRoutesExt;
13use fedimint_ui_common::auth::UserAuth;
14use fedimint_ui_common::{
15    CONNECTIVITY_CHECK_ROUTE, LOGIN_ROUTE, LoginInput, ROOT_ROUTE, UiState,
16    connectivity_check_handler, copiable_text, login_form, login_submit_response,
17    single_card_layout,
18};
19use maud::{Markup, PreEscaped, html};
20use qrcode::QrCode;
21use serde::Deserialize;
22
23// Setup route constants
24pub const FEDERATION_SETUP_ROUTE: &str = "/federation_setup";
25pub const ADD_SETUP_CODE_ROUTE: &str = "/add_setup_code";
26pub const RESET_SETUP_CODES_ROUTE: &str = "/reset_setup_codes";
27pub const START_DKG_ROUTE: &str = "/start_dkg";
28
29#[derive(Debug, Deserialize)]
30pub(crate) struct SetupInput {
31    pub password: String,
32    pub name: String,
33    #[serde(default)]
34    pub is_lead: bool,
35    pub federation_name: String,
36    #[serde(default)]
37    pub federation_size: String,
38    #[serde(default)] // will not be sent if disabled
39    pub enable_base_fees: bool,
40    #[serde(default)] // list of enabled module kinds
41    pub enabled_modules: Vec<String>,
42}
43
44#[derive(Debug, Deserialize)]
45pub(crate) struct PeerInfoInput {
46    pub peer_info: String,
47}
48
49fn peer_list_section(
50    connected_peers: &[String],
51    federation_size: Option<u32>,
52    cfg_federation_name: &Option<String>,
53    cfg_base_fees_disabled: Option<bool>,
54    cfg_enabled_modules: &Option<BTreeSet<ModuleKind>>,
55    error: Option<&str>,
56) -> Markup {
57    let total_guardians = connected_peers.len() + 1;
58    let can_start_dkg = federation_size
59        .map(|expected| total_guardians == expected as usize)
60        .unwrap_or(false);
61
62    html! {
63        div id="peer-list-section" {
64            @if let Some(expected) = federation_size {
65                p { (format!("{total_guardians} of {expected} guardians connected.")) }
66            } @else {
67                p { "Add setup code for every other guardian." }
68            }
69
70            @if !connected_peers.is_empty() {
71                ul class="list-group mb-2" {
72                    @for peer in connected_peers {
73                        li class="list-group-item" { (peer) }
74                    }
75                }
76
77                form id="reset-form" method="post" action=(RESET_SETUP_CODES_ROUTE) class="d-none" {}
78                div class="text-center mb-4" {
79                    button type="button" class="btn btn-link text-danger text-decoration-none p-0" onclick="if(confirm('Are you sure you want to reset all guardians?')){document.getElementById('reset-form').submit();}" {
80                        "Reset Guardians"
81                    }
82                }
83            }
84
85            @if can_start_dkg {
86                // All guardians connected — show confirm form
87                @let has_settings = cfg_federation_name.is_some()
88                    || federation_size.is_some()
89                    || cfg_base_fees_disabled.is_some()
90                    || cfg_enabled_modules.is_some();
91
92                form id="start-dkg-form" hx-post=(START_DKG_ROUTE) hx-target="#peer-list-section" hx-swap="outerHTML" {
93                    @if let Some(error) = error {
94                        div class="alert alert-danger mb-3" { (error) }
95                    }
96                    button type="submit" class="btn btn-warning w-100 py-2" { "Confirm" }
97                }
98
99                @if has_settings {
100                    p class="text-muted mt-3 mb-0" style="font-size: 0.85rem;" {
101                        @if let Some(name) = cfg_federation_name {
102                            (name) " federation has been configured"
103                        } @else {
104                            "The federation has been configured"
105                        }
106                        @if let Some(disabled) = cfg_base_fees_disabled {
107                            " with base fees "
108                            @if disabled { "disabled" } @else { "enabled" }
109                        }
110                        @if let Some(modules) = cfg_enabled_modules {
111                            " and modules "
112                            (modules.iter().map(|m| m.as_str().to_owned()).collect::<Vec<_>>().join(", "))
113                        }
114                        "."
115                    }
116                }
117            } @else {
118                // Still collecting — show add guardian form
119                form id="add-setup-code-form" hx-post=(ADD_SETUP_CODE_ROUTE) hx-target="#peer-list-section" hx-swap="outerHTML" {
120                    div class="mb-3" {
121                        div class="input-group" {
122                            input type="text" class="form-control" id="peer_info" name="peer_info"
123                                placeholder="Paste Setup Code" required;
124                            button type="button" class="btn btn-outline-secondary" onclick="startQrScanner()" title="Scan QR Code" {
125                                i class="bi bi-qr-code-scan" {}
126                            }
127                        }
128                    }
129
130                    @if let Some(error) = error {
131                        div class="alert alert-danger mb-3" { (error) }
132                    }
133                    button type="submit" class="btn btn-primary w-100 py-2" { "Add Guardian" }
134                }
135            }
136        }
137    }
138}
139
140fn setup_form_content(
141    available_modules: &BTreeSet<ModuleKind>,
142    default_modules: &BTreeSet<ModuleKind>,
143    error: Option<&str>,
144) -> Markup {
145    html! {
146        form id="setup-form" hx-post=(ROOT_ROUTE) hx-target="#setup-form" hx-swap="outerHTML" {
147            style {
148                r#"
149                .toggle-content {
150                    display: none;
151                }
152
153                .toggle-control:checked ~ .toggle-content {
154                    display: block;
155                }
156
157                #base-fees-warning {
158                    display: block;
159                }
160
161                .form-check:has(#enable_base_fees:checked) + #base-fees-warning {
162                    display: none;
163                }
164
165                .accordion-button {
166                    background-color: #f8f9fa;
167                }
168
169                .accordion-button:not(.collapsed) {
170                    background-color: #f8f9fa;
171                    box-shadow: none;
172                }
173
174                .accordion-button:focus {
175                    box-shadow: none;
176                }
177
178                #modules-warning {
179                    display: none;
180                }
181
182                #modules-list:has(.form-check-input:not(:checked)) ~ #modules-warning {
183                    display: block;
184                }
185                "#
186            }
187
188            div class="form-group mb-4" {
189                input type="text" class="form-control" id="name" name="name" placeholder="Your Guardian Name" required;
190            }
191
192            div class="form-group mb-4" {
193                input type="password" class="form-control" id="password" name="password" placeholder="Your Password" required;
194            }
195
196            div class="alert alert-warning mb-3" style="font-size: 0.875rem;" {
197                "Exactly one guardian must set the global config."
198            }
199
200            div class="form-group mb-4" {
201                input type="checkbox" class="form-check-input toggle-control" id="is_lead" name="is_lead" value="true";
202
203                label class="form-check-label ms-2" for="is_lead" {
204                    "Set the global config"
205                }
206
207                div class="toggle-content mt-3" {
208                    input type="text" class="form-control" id="federation_name" name="federation_name" placeholder="Federation Name";
209
210                    div class="form-group mt-3" {
211                        label class="form-label" for="federation_size" {
212                            "Total number of guardians (including you)"
213                        }
214                        select class="form-select" id="federation_size" name="federation_size" {
215                            option value="" selected disabled { "Federation Size" }
216                            option value="1" { "1 — Testing" }
217                            option value="4" { "4 — Recommended" }
218                            option value="5" { "5" }
219                            option value="6" { "6" }
220                            option value="7" { "7 — Recommended" }
221                            option value="8" { "8" }
222                            option value="9" { "9" }
223                            option value="10" { "10 — Recommended" }
224                            option value="11" { "11" }
225                            option value="12" { "12" }
226                            option value="13" { "13 — Recommended" }
227                            option value="14" { "14" }
228                            option value="15" { "15" }
229                            option value="16" { "16 — Recommended" }
230                            option value="17" { "17" }
231                            option value="18" { "18" }
232                            option value="19" { "19 — Recommended" }
233                            option value="20" { "20" }
234                        }
235                    }
236
237                    div class="form-check mt-3" {
238                        input type="checkbox" class="form-check-input" id="enable_base_fees" name="enable_base_fees" checked value="true";
239
240                        label class="form-check-label" for="enable_base_fees" {
241                            "Enable base fees for this federation"
242                        }
243                    }
244
245                    div id="base-fees-warning" class="alert alert-warning mt-2" style="font-size: 0.875rem;" {
246                        strong { "Warning: " }
247                        "Base fees discourage spam and wasting storage space. The typical fee is only 1-3 sats per transaction, regardless of the value transferred. We recommend enabling the base fee and it cannot be changed later."
248                    }
249
250                    div class="accordion mt-3" id="modulesAccordion" {
251                        div class="accordion-item" {
252                            h2 class="accordion-header" {
253                                button class="accordion-button collapsed" type="button"
254                                    data-bs-toggle="collapse" data-bs-target="#modulesConfig"
255                                    aria-expanded="false" aria-controls="modulesConfig" {
256                                    "Advanced: Configure Enabled Modules"
257                                }
258                            }
259                            div id="modulesConfig" class="accordion-collapse collapse" data-bs-parent="#modulesAccordion" {
260                                div class="accordion-body" {
261                                    div id="modules-list" {
262                                        @for kind in available_modules {
263                                            div class="form-check" {
264                                                input type="checkbox" class="form-check-input"
265                                                    id=(format!("module_{}", kind.as_str()))
266                                                    name="enabled_modules"
267                                                    value=(kind.as_str())
268                                                    checked[default_modules.contains(kind)];
269
270                                                label class="form-check-label" for=(format!("module_{}", kind.as_str())) {
271                                                    (kind.as_str())
272                                                    @if !default_modules.contains(kind) {
273                                                        span class="badge bg-warning text-dark ms-2" { "experimental" }
274                                                    }
275                                                }
276                                            }
277                                        }
278                                    }
279
280                                    div id="modules-warning" class="alert alert-warning mt-2 mb-0" style="font-size: 0.875rem;" {
281                                        "Only modify this if you know what you are doing. Disabled modules cannot be enabled later."
282                                    }
283                                }
284                            }
285                        }
286                    }
287                }
288            }
289
290            @if let Some(error) = error {
291                div class="alert alert-danger mb-3" { (error) }
292            }
293            button type="submit" class="btn btn-primary w-100 py-2" { "Confirm" }
294        }
295    }
296}
297
298// GET handler for the /setup route (display the setup form)
299async fn setup_form(State(state): State<UiState<DynSetupApi>>) -> impl IntoResponse {
300    if state.api.setup_code().await.is_some() {
301        return Redirect::to(FEDERATION_SETUP_ROUTE).into_response();
302    }
303
304    let available_modules = state.api.available_modules();
305    let default_modules = state.api.default_modules();
306    let content = setup_form_content(&available_modules, &default_modules, None);
307
308    Html(single_card_layout("Guardian Setup", content).into_string()).into_response()
309}
310
311// POST handler for the /setup route (process the setup form)
312async fn setup_submit(
313    State(state): State<UiState<DynSetupApi>>,
314    Form(input): Form<SetupInput>,
315) -> impl IntoResponse {
316    let available_modules = state.api.available_modules();
317    let default_modules = state.api.default_modules();
318
319    // Only use these settings if is_lead is true
320    let federation_name = if input.is_lead {
321        Some(input.federation_name)
322    } else {
323        None
324    };
325
326    let disable_base_fees = if input.is_lead {
327        Some(!input.enable_base_fees)
328    } else {
329        None
330    };
331
332    let enabled_modules = if input.is_lead {
333        let enabled: BTreeSet<ModuleKind> = input
334            .enabled_modules
335            .into_iter()
336            .map(|s| ModuleKind::clone_from_str(&s))
337            .collect();
338
339        Some(enabled)
340    } else {
341        None
342    };
343
344    let federation_size = if input.is_lead {
345        let s = input.federation_size.trim();
346        if s.is_empty() {
347            None
348        } else {
349            match s.parse::<u32>() {
350                Ok(size) => Some(size),
351                Err(_) => {
352                    return Html(
353                        setup_form_content(
354                            &available_modules,
355                            &default_modules,
356                            Some("Invalid federation size"),
357                        )
358                        .into_string(),
359                    )
360                    .into_response();
361                }
362            }
363        }
364    } else {
365        None
366    };
367
368    match state
369        .api
370        .set_local_parameters(
371            ApiAuth::new(input.password),
372            input.name,
373            federation_name,
374            disable_base_fees,
375            enabled_modules,
376            federation_size,
377        )
378        .await
379    {
380        Ok(_) => (
381            [("HX-Redirect", FEDERATION_SETUP_ROUTE)],
382            Html(String::new()),
383        )
384            .into_response(),
385        Err(e) => Html(
386            setup_form_content(&available_modules, &default_modules, Some(&e.to_string()))
387                .into_string(),
388        )
389        .into_response(),
390    }
391}
392
393// GET handler for the /login route (display the login form)
394async fn login_form_handler(State(state): State<UiState<DynSetupApi>>) -> impl IntoResponse {
395    if state.api.setup_code().await.is_none() {
396        return Redirect::to(ROOT_ROUTE).into_response();
397    }
398
399    Html(single_card_layout("Enter Password", login_form(None)).into_string()).into_response()
400}
401
402// POST handler for the /login route (authenticate and set session cookie)
403async fn login_submit(
404    State(state): State<UiState<DynSetupApi>>,
405    jar: CookieJar,
406    Form(input): Form<LoginInput>,
407) -> impl IntoResponse {
408    let auth = match state.api.auth().await {
409        Some(auth) => auth,
410        None => return Redirect::to(ROOT_ROUTE).into_response(),
411    };
412
413    login_submit_response(
414        auth,
415        state.auth_cookie_name,
416        state.auth_cookie_value,
417        jar,
418        input,
419    )
420    .into_response()
421}
422
423// GET handler for the /federation-setup route (main federation management page)
424async fn federation_setup(
425    State(state): State<UiState<DynSetupApi>>,
426    _auth: UserAuth,
427) -> impl IntoResponse {
428    let our_connection_info = state
429        .api
430        .setup_code()
431        .await
432        .expect("Successful authentication ensures that the local parameters have been set");
433
434    let connected_peers = state.api.connected_peers().await;
435    let federation_size = state.api.federation_size().await;
436    let cfg_federation_name = state.api.cfg_federation_name().await;
437    let cfg_base_fees_disabled = state.api.cfg_base_fees_disabled().await;
438    let cfg_enabled_modules = state.api.cfg_enabled_modules().await;
439
440    let content = html! {
441        p { "Share this with your fellow guardians." }
442
443        @let qr_svg = QrCode::new(&our_connection_info)
444            .expect("Failed to generate QR code")
445            .render::<qrcode::render::svg::Color>()
446            .build();
447
448        div class="text-center mb-3" {
449            div class="border rounded p-2 bg-white d-inline-block" style="width: 250px; max-width: 100%;" {
450                div style="width: 100%; height: auto; overflow: hidden;" {
451                    (PreEscaped(format!(r#"<div style="width: 100%; height: auto;">{}</div>"#,
452                        qr_svg.replace("width=", "data-width=")
453                              .replace("height=", "data-height=")
454                              .replace("<svg", r#"<svg style="width: 100%; height: auto; display: block;""#))))
455                }
456            }
457        }
458
459        div class="mb-4" {
460            (copiable_text(&our_connection_info))
461        }
462
463        (peer_list_section(&connected_peers, federation_size, &cfg_federation_name, cfg_base_fees_disabled, &cfg_enabled_modules, None))
464
465        // QR Scanner Modal
466        div class="modal fade" id="qrScannerModal" tabindex="-1" aria-labelledby="qrScannerModalLabel" aria-hidden="true" {
467            div class="modal-dialog modal-dialog-centered" {
468                div class="modal-content" {
469                    div class="modal-header" {
470                        h5 class="modal-title" id="qrScannerModalLabel" { "Scan Setup Code" }
471                        button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" {}
472                    }
473                    div class="modal-body" {
474                        div id="qr-reader" style="width: 100%;" {}
475                        div id="qr-reader-error" class="alert alert-danger mt-3 d-none" {}
476                    }
477                    div class="modal-footer" {
478                        button type="button" class="btn btn-secondary" data-bs-dismiss="modal" { "Cancel" }
479                    }
480                }
481            }
482        }
483
484        script src="/assets/html5-qrcode.min.js" {}
485
486        // QR Scanner JavaScript
487        script {
488            (PreEscaped(r#"
489            var html5QrCode = null;
490            var qrScannerModal = null;
491
492            function startQrScanner() {
493                // Check for Flutter override hook
494                if (typeof window.fedimintQrScannerOverride === 'function') {
495                    window.fedimintQrScannerOverride(function(result) {
496                        if (result) {
497                            document.getElementById('peer_info').value = result;
498                        }
499                    });
500                    return;
501                }
502
503                var modalEl = document.getElementById('qrScannerModal');
504                qrScannerModal = new bootstrap.Modal(modalEl);
505
506                // Reset error message
507                var errorEl = document.getElementById('qr-reader-error');
508                errorEl.classList.add('d-none');
509                errorEl.textContent = '';
510
511                qrScannerModal.show();
512
513                // Wait for modal to be shown before starting camera
514                modalEl.addEventListener('shown.bs.modal', function onShown() {
515                    modalEl.removeEventListener('shown.bs.modal', onShown);
516                    initializeScanner();
517                });
518
519                // Clean up when modal is hidden
520                modalEl.addEventListener('hidden.bs.modal', function onHidden() {
521                    modalEl.removeEventListener('hidden.bs.modal', onHidden);
522                    stopQrScanner();
523                });
524            }
525
526            function initializeScanner() {
527                html5QrCode = new Html5Qrcode("qr-reader");
528
529                var config = {
530                    fps: 10,
531                    qrbox: { width: 250, height: 250 },
532                    aspectRatio: 1.0
533                };
534
535                html5QrCode.start(
536                    { facingMode: "environment" },
537                    config,
538                    function(decodedText, decodedResult) {
539                        // Success - populate input and close modal
540                        document.getElementById('peer_info').value = decodedText;
541                        qrScannerModal.hide();
542                    },
543                    function(errorMessage) {
544                        // Ignore scan errors (happens constantly while searching)
545                    }
546                ).catch(function(err) {
547                    var errorEl = document.getElementById('qr-reader-error');
548                    errorEl.textContent = 'Unable to access camera: ' + err;
549                    errorEl.classList.remove('d-none');
550                });
551            }
552
553            function stopQrScanner() {
554                if (html5QrCode && html5QrCode.isScanning) {
555                    html5QrCode.stop().catch(function(err) {
556                        console.error('Error stopping scanner:', err);
557                    });
558                }
559            }
560            "#))
561        }
562    };
563
564    Html(single_card_layout("Federation Setup", content).into_string()).into_response()
565}
566
567// POST handler for adding peer connection info
568async fn post_add_setup_code(
569    State(state): State<UiState<DynSetupApi>>,
570    _auth: UserAuth,
571    Form(input): Form<PeerInfoInput>,
572) -> impl IntoResponse {
573    let error = state.api.add_peer_setup_code(input.peer_info).await.err();
574
575    let connected_peers = state.api.connected_peers().await;
576    let federation_size = state.api.federation_size().await;
577    let cfg_federation_name = state.api.cfg_federation_name().await;
578    let cfg_base_fees_disabled = state.api.cfg_base_fees_disabled().await;
579    let cfg_enabled_modules = state.api.cfg_enabled_modules().await;
580
581    Html(
582        peer_list_section(
583            &connected_peers,
584            federation_size,
585            &cfg_federation_name,
586            cfg_base_fees_disabled,
587            &cfg_enabled_modules,
588            error.as_ref().map(|e| e.to_string()).as_deref(),
589        )
590        .into_string(),
591    )
592    .into_response()
593}
594
595// POST handler for starting the DKG process
596async fn post_start_dkg(
597    State(state): State<UiState<DynSetupApi>>,
598    _auth: UserAuth,
599) -> impl IntoResponse {
600    let our_connection_info = state.api.setup_code().await;
601
602    match state.api.start_dkg().await {
603        Ok(()) => {
604            let content = html! {
605                @if let Some(ref info) = our_connection_info {
606                    p { "Share with guardians who still need it." }
607                    div class="mb-4" {
608                        (copiable_text(info))
609                    }
610                }
611
612                div class="alert alert-info mb-3" {
613                    "All guardians need to confirm their settings. Once completed you will be redirected to the Dashboard."
614                }
615
616                // Poll until the dashboard is ready, then redirect
617                div
618                    hx-get=(ROOT_ROUTE)
619                    hx-trigger="every 2s"
620                    hx-swap="none"
621                    hx-on--after-request={
622                        "if (event.detail.xhr.status === 200) { window.location.href = '" (ROOT_ROUTE) "'; }"
623                    }
624                    style="display: none;"
625                {}
626
627                div class="text-center mt-4" {
628                    div class="spinner-border text-primary" role="status" {
629                        span class="visually-hidden" { "Loading..." }
630                    }
631                    p class="mt-2 text-muted" { "Waiting for federation setup to complete..." }
632                }
633            };
634
635            (
636                [("HX-Retarget", "body"), ("HX-Reswap", "innerHTML")],
637                Html(single_card_layout("DKG Started", content).into_string()),
638            )
639                .into_response()
640        }
641        Err(e) => {
642            let connected_peers = state.api.connected_peers().await;
643            let federation_size = state.api.federation_size().await;
644            let cfg_federation_name = state.api.cfg_federation_name().await;
645            let cfg_base_fees_disabled = state.api.cfg_base_fees_disabled().await;
646            let cfg_enabled_modules = state.api.cfg_enabled_modules().await;
647
648            Html(
649                peer_list_section(
650                    &connected_peers,
651                    federation_size,
652                    &cfg_federation_name,
653                    cfg_base_fees_disabled,
654                    &cfg_enabled_modules,
655                    Some(&e.to_string()),
656                )
657                .into_string(),
658            )
659            .into_response()
660        }
661    }
662}
663
664// POST handler for resetting peer connection info
665async fn post_reset_setup_codes(
666    State(state): State<UiState<DynSetupApi>>,
667    _auth: UserAuth,
668) -> impl IntoResponse {
669    state.api.reset_setup_codes().await;
670
671    Redirect::to(FEDERATION_SETUP_ROUTE).into_response()
672}
673
674pub fn router(api: DynSetupApi) -> Router {
675    Router::new()
676        .route(ROOT_ROUTE, get(setup_form).post(setup_submit))
677        .route(LOGIN_ROUTE, get(login_form_handler).post(login_submit))
678        .route(FEDERATION_SETUP_ROUTE, get(federation_setup))
679        .route(ADD_SETUP_CODE_ROUTE, post(post_add_setup_code))
680        .route(RESET_SETUP_CODES_ROUTE, post(post_reset_setup_codes))
681        .route(START_DKG_ROUTE, post(post_start_dkg))
682        .route(
683            CONNECTIVITY_CHECK_ROUTE,
684            get(connectivity_check_handler::<DynSetupApi>),
685        )
686        .with_static_routes()
687        .with_state(UiState::new(api))
688}