cdk-ldk-node 0.16.0-rc.2

CDK ln backend for cdk-ldk-node
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::collections::HashMap;
use std::str::FromStr;

use axum::body::Body;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::{Html, Response};
use axum::Form;
use ldk_node::bitcoin::Address;
use maud::html;
use serde::{Deserialize, Serialize};

use crate::web::handlers::utils::deserialize_optional_u64;
use crate::web::handlers::AppState;
use crate::web::templates::{
    error_message, form_card, format_sats_as_btc, info_card, is_node_running, layout_with_status,
    success_message,
};

#[derive(Deserialize, Serialize)]
pub struct SendOnchainActionForm {
    address: String,
    #[serde(deserialize_with = "deserialize_optional_u64")]
    amount_sat: Option<u64>,
    send_action: String,
}

#[derive(Deserialize)]
pub struct ConfirmOnchainForm {
    address: String,
    amount_sat: Option<u64>,
    send_action: String,
    confirmed: Option<String>,
}

pub async fn get_new_address(State(state): State<AppState>) -> Result<Html<String>, StatusCode> {
    let address_result = state.node.inner.onchain_payment().new_address();

    let content = match address_result {
        Ok(address) => {
            html! {
                div class="card" {
                    h2 style="font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5; padding-bottom: 1rem; border-bottom: 1px solid hsl(var(--border)); margin-bottom: 0;" { "Bitcoin Address" }
                    div class="address-display" style="margin-top: 1.5rem;" {
                        div class="address-container" {
                            span class="address-text" { (address.to_string()) }
                        }
                    }
                }
                div class="card" {
                    div style="display: flex; justify-content: space-between; gap: 1rem;" {
                        a href="/onchain" { button class="button-secondary" { "Back" } }
                        form method="post" action="/onchain/new-address" style="display: inline;" {
                            button class="button-primary" type="submit" { "Generate Another Address" }
                        }
                    }
                }
            }
        }
        Err(e) => {
            html! {
                (error_message(&format!("Failed to generate address: {e}")))
                div class="card" {
                    a href="/onchain" { button class="button-primary" { "← Back to On-chain" } }
                }
            }
        }
    };

    let is_running = is_node_running(&state.node.inner);
    Ok(Html(
        layout_with_status("New Address", content, is_running).into_string(),
    ))
}

pub async fn onchain_page(
    State(state): State<AppState>,
    query: Query<HashMap<String, String>>,
) -> Result<Html<String>, StatusCode> {
    let balances = state.node.inner.list_balances();
    let action = query
        .get("action")
        .map(|s| s.as_str())
        .unwrap_or("overview");

    let mut content = html! {
        h2 style="text-align: center; margin-bottom: 3rem;" { "On-chain" }

        // On-chain Balance with action buttons in header
        div class="card" {
            div style="display: flex; justify-content: space-between; align-items: center; padding-bottom: 1rem; border-bottom: 1px solid hsl(var(--border)); margin-bottom: 0;" {
                h2 style="font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5; margin: 0;" { "On-chain Balance" }
                div style="display: flex; gap: 0.5rem;" {
                    a href="/onchain?action=send" style="text-decoration: none;" {
                        button class="button-outline" style="padding: 0.5rem 1rem; font-size: 0.875rem;" { "Send" }
                    }
                    a href="/onchain?action=receive" style="text-decoration: none;" {
                        button class="button-outline" style="padding: 0.5rem 1rem; font-size: 0.875rem;" { "Receive" }
                    }
                }
            }
            div class="metrics-container" style="margin-top: 1.5rem;" {
                div class="metric-card" {
                    div class="metric-value" { (format_sats_as_btc(balances.total_onchain_balance_sats)) }
                    div class="metric-label" { "Total Balance" }
                }
                div class="metric-card" {
                    div class="metric-value" { (format_sats_as_btc(balances.spendable_onchain_balance_sats)) }
                    div class="metric-label" { "Spendable Balance" }
                }
            }
        }
    };

    match action {
        "send" => {
            content = html! {
                h2 style="text-align: center; margin-bottom: 3rem;" { "On-chain" }

                // Send form above balance
                (form_card(
                    "Send On-chain Payment",
                    html! {
                        form method="post" action="/onchain/send" {
                            div class="form-group" {
                                label for="address" { "Recipient Address" }
                                input type="text" id="address" name="address" required placeholder="bc1..." {}
                            }
                            div class="form-group" {
                                label for="amount_sat" { "Amount (sats)" }
                                input type="number" id="amount_sat" name="amount_sat" placeholder="0" {}
                            }
                            input type="hidden" id="send_action" name="send_action" value="send" {}
                            div style="display: flex; justify-content: space-between; gap: 1rem; margin-top: 2rem;" {
                                a href="/onchain" { button type="button" class="button-secondary" { "Cancel" } }
                                div style="display: flex; gap: 0.5rem;" {
                                    button type="submit" onclick="document.getElementById('send_action').value='send'" { "Send Payment" }
                                    button type="submit" onclick="document.getElementById('send_action').value='send_all'; document.getElementById('amount_sat').value=''" { "Send All" }
                                }
                            }
                        }
                    }
                ))

                // On-chain Balance with action buttons in header
                div class="card" {
                    div style="display: flex; justify-content: space-between; align-items: center; padding-bottom: 1rem; border-bottom: 1px solid hsl(var(--border)); margin-bottom: 0;" {
                        h2 style="font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5; margin: 0;" { "On-chain Balance" }
                        div style="display: flex; gap: 0.5rem;" {
                            a href="/onchain?action=send" style="text-decoration: none;" {
                                button class="button-outline" style="padding: 0.5rem 1rem; font-size: 0.875rem;" { "Send" }
                            }
                            a href="/onchain?action=receive" style="text-decoration: none;" {
                                button class="button-outline" style="padding: 0.5rem 1rem; font-size: 0.875rem;" { "Receive" }
                            }
                        }
                    }
                    div class="metrics-container" style="margin-top: 1.5rem;" {
                        div class="metric-card" {
                            div class="metric-value" { (format_sats_as_btc(balances.total_onchain_balance_sats)) }
                            div class="metric-label" { "Total Balance" }
                        }
                        div class="metric-card" {
                            div class="metric-value" { (format_sats_as_btc(balances.spendable_onchain_balance_sats)) }
                            div class="metric-label" { "Spendable Balance" }
                        }
                    }
                }
            };
        }
        "receive" => {
            content = html! {
                h2 style="text-align: center; margin-bottom: 3rem;" { "On-chain" }

                // Generate address form above balance
                (form_card(
                    "Generate New Address",
                    html! {
                        form method="post" action="/onchain/new-address" {
                            p style="margin-bottom: 2rem;" { "Click the button below to generate a new Bitcoin address for receiving on-chain payments." }
                            div style="display: flex; justify-content: space-between; gap: 1rem;" {
                                a href="/onchain" { button type="button" class="button-secondary" { "Cancel" } }
                                button class="button-primary" type="submit" { "Generate New Address" }
                            }
                        }
                    }
                ))

                // On-chain Balance with action buttons in header
                div class="card" {
                    div style="display: flex; justify-content: space-between; align-items: center; padding-bottom: 1rem; border-bottom: 1px solid hsl(var(--border)); margin-bottom: 0;" {
                        h2 style="font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5; margin: 0;" { "On-chain Balance" }
                        div style="display: flex; gap: 0.5rem;" {
                            a href="/onchain?action=send" style="text-decoration: none;" {
                                button class="button-outline" style="padding: 0.5rem 1rem; font-size: 0.875rem;" { "Send" }
                            }
                            a href="/onchain?action=receive" style="text-decoration: none;" {
                                button class="button-outline" style="padding: 0.5rem 1rem; font-size: 0.875rem;" { "Receive" }
                            }
                        }
                    }
                    div class="metrics-container" style="margin-top: 1.5rem;" {
                        div class="metric-card" {
                            div class="metric-value" { (format_sats_as_btc(balances.total_onchain_balance_sats)) }
                            div class="metric-label" { "Total Balance" }
                        }
                        div class="metric-card" {
                            div class="metric-value" { (format_sats_as_btc(balances.spendable_onchain_balance_sats)) }
                            div class="metric-label" { "Spendable Balance" }
                        }
                    }
                }
            };
        }
        _ => {
            // Show overview with just the balance and quick actions at the top
        }
    }

    let is_running = is_node_running(&state.node.inner);
    Ok(Html(
        layout_with_status("On-chain", content, is_running).into_string(),
    ))
}

pub async fn post_send_onchain(
    State(_state): State<AppState>,
    Form(form): Form<SendOnchainActionForm>,
) -> Result<Response, StatusCode> {
    let encoded_form =
        serde_urlencoded::to_string(&form).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    Response::builder()
        .status(StatusCode::FOUND)
        .header("Location", format!("/onchain/confirm?{}", encoded_form))
        .body(Body::empty())
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}

pub async fn onchain_confirm_page(
    State(state): State<AppState>,
    query: Query<ConfirmOnchainForm>,
) -> Result<Response, StatusCode> {
    let form = query.0;

    // If user confirmed, execute the transaction
    if form.confirmed.as_deref() == Some("true") {
        return execute_onchain_transaction(State(state), form).await;
    }

    // Validate address
    let _address = match Address::from_str(&form.address) {
        Ok(addr) => addr,
        Err(e) => {
            let content = html! {
                (error_message(&format!("Invalid address: {e}")))
                div class="card" {
                    a href="/onchain?action=send" { button { "← Back" } }
                }
            };
            return Response::builder()
                .status(StatusCode::BAD_REQUEST)
                .header("content-type", "text/html")
                .body(Body::from(
                    layout_with_status("Send On-chain Error", content, true).into_string(),
                ))
                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
        }
    };

    let balances = state.node.inner.list_balances();
    let spendable_balance = balances.spendable_onchain_balance_sats;

    // Calculate transaction details
    let (amount_to_send, is_send_all) = if form.send_action == "send_all" {
        (spendable_balance, true)
    } else {
        let amount = form.amount_sat.unwrap_or(0);
        if amount > spendable_balance {
            let content = html! {
                (error_message(&format!("Insufficient funds. Requested: {}, Available: {}",
                    format_sats_as_btc(amount), format_sats_as_btc(spendable_balance))))
                div class="card" {
                    a href="/onchain?action=send" { button { "← Back" } }
                }
            };
            return Response::builder()
                .status(StatusCode::BAD_REQUEST)
                .header("content-type", "text/html")
                .body(Body::from(
                    layout_with_status("Send On-chain Error", content, true).into_string(),
                ))
                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
        }
        (amount, false)
    };

    let confirmation_url = if form.send_action == "send_all" {
        format!(
            "/onchain/confirm?address={}&send_action={}&confirmed=true",
            urlencoding::encode(&form.address),
            form.send_action
        )
    } else {
        format!(
            "/onchain/confirm?address={}&amount_sat={}&send_action={}&confirmed=true",
            urlencoding::encode(&form.address),
            form.amount_sat.unwrap_or(0),
            form.send_action
        )
    };

    let content = html! {
        h2 style="text-align: center; margin-bottom: 3rem;" { "Confirm On-chain Transaction" }

        @if is_send_all {
            div class="card send-all-notice" {
                h3 { "Send All Notice" }
                p {
                    "This transaction will send all available funds to the recipient address. Network fees will be deducted from the total amount automatically."
                }
            }
        }

        // Transaction Details Card
        div class="card" {
            h2 style="font-size: 0.875rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.5; padding-bottom: 1rem; border-bottom: 1px solid hsl(var(--border)); margin-bottom: 0;" { "Transaction Details" }
            div class="transaction-details" style="margin-top: 1.5rem;" {
                div class="detail-row" {
                    span class="detail-label" { "Recipient Address:" }
                    span class="detail-value" { (form.address.clone()) }
                }
                div class="detail-row" {
                    span class="detail-label" { "Amount to Send:" }
                    span class="detail-value-amount" {
                        (if is_send_all {
                            format!("{} (All available funds)", format_sats_as_btc(amount_to_send))
                        } else {
                            format_sats_as_btc(amount_to_send)
                        })
                    }
                }
                div class="detail-row" {
                    span class="detail-label" { "Current Spendable Balance:" }
                    span class="detail-value-amount" { (format_sats_as_btc(spendable_balance)) }
                }
            }

            div style="display: flex; justify-content: space-between; gap: 1rem; margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid hsl(var(--border));" {
                a href="/onchain?action=send" {
                    button type="button" class="button-secondary" { "Cancel" }
                }
                a href=(confirmation_url) {
                    button class="button-primary" {
                        "Confirm"
                    }
                }
            }
        }
    };

    Response::builder()
        .header("content-type", "text/html")
        .body(Body::from(
            layout_with_status("Confirm Transaction", content, true).into_string(),
        ))
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}

async fn execute_onchain_transaction(
    State(state): State<AppState>,
    form: ConfirmOnchainForm,
) -> Result<Response, StatusCode> {
    tracing::info!(
        "Web interface: Executing on-chain transaction to address={}, send_action={}, amount_sat={:?}",
        form.address,
        form.send_action,
        form.amount_sat
    );

    let address = match Address::from_str(&form.address) {
        Ok(addr) => addr,
        Err(e) => {
            tracing::warn!(
                "Web interface: Invalid address for on-chain transaction: {}",
                e
            );
            let content = html! {
                (error_message(&format!("Invalid address: {e}")))
                div class="card" {
                    a href="/onchain" { button { "← Back" } }
                }
            };
            return Response::builder()
                .status(StatusCode::BAD_REQUEST)
                .header("content-type", "text/html")
                .body(Body::from(
                    layout_with_status("Send On-chain Error", content, true).into_string(),
                ))
                .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
        }
    };

    // Handle send all action
    let txid_result = if form.send_action == "send_all" {
        tracing::info!(
            "Web interface: Sending all available funds to {}",
            form.address
        );
        state.node.inner.onchain_payment().send_all_to_address(
            address.assume_checked_ref(),
            false,
            None,
        )
    } else {
        let amount_sats = form.amount_sat.ok_or(StatusCode::BAD_REQUEST)?;
        tracing::info!(
            "Web interface: Sending {} sats to {}",
            amount_sats,
            form.address
        );
        state.node.inner.onchain_payment().send_to_address(
            address.assume_checked_ref(),
            amount_sats,
            None,
        )
    };

    let content = match txid_result {
        Ok(txid) => {
            if form.send_action == "send_all" {
                tracing::info!(
                    "Web interface: Successfully sent all available funds, txid={}",
                    txid
                );
            } else {
                tracing::info!(
                    "Web interface: Successfully sent {} sats, txid={}",
                    form.amount_sat.unwrap_or(0),
                    txid
                );
            }
            let amount = form.amount_sat;
            html! {
                        (success_message("Transaction sent successfully!"))
                        (info_card(
                            "Transaction Details",
                            vec![
                                ("Transaction ID", txid.to_string()),
                                ("Amount", if form.send_action == "send_all" {
                                    format!("{} (All available funds)", format_sats_as_btc(amount.unwrap_or(0)))
                                } else {
                                    format_sats_as_btc(form.amount_sat.unwrap_or(0))
                                }),
                                ("Recipient", form.address),
                            ]
                        ))
                        div class="card" {
                            a href="/onchain" { button { "← Back to On-chain" } }
                        }
            }
        }
        Err(e) => {
            tracing::error!("Web interface: Failed to send on-chain transaction: {}", e);
            html! {
                (error_message(&format!("Failed to send payment: {e}")))
                div class="card" {
                    a href="/onchain" { button { "← Try Again" } }
                }
            }
        }
    };

    Response::builder()
        .header("content-type", "text/html")
        .body(Body::from(
            layout_with_status("Send On-chain Result", content, true).into_string(),
        ))
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}