lnbot 1.0.0

Official Rust SDK for LnBot — Bitcoin for AI Agents. Send and receive sats over Lightning.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Request and response types for the LnBot API.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Account
// ---------------------------------------------------------------------------

/// Response from registering a new account.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RegisterResponse {
    pub user_id: String,
    pub primary_key: String,
    pub secondary_key: String,
    pub recovery_passphrase: String,
}

/// Response from the `/v1/me` identity endpoint.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MeResponse {
    pub user_id: String,
    pub key_name: Option<String>,
    pub wallet_id: Option<String>,
}

// ---------------------------------------------------------------------------
// Wallet
// ---------------------------------------------------------------------------

/// A wallet's current state.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WalletResponse {
    pub wallet_id: String,
    pub name: String,
    pub balance: i64,
    pub on_hold: i64,
    pub available: i64,
}

/// Parameters for updating a wallet.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateWalletRequest {
    pub name: String,
}

impl UpdateWalletRequest {
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }
}

/// Response from creating a new wallet.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CreateWalletResponse {
    pub wallet_id: String,
    pub name: String,
    pub address: String,
}

/// A wallet in the list returned by `/v1/wallets`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WalletListItem {
    pub wallet_id: String,
    pub name: String,
}

// ---------------------------------------------------------------------------
// Wallet Key
// ---------------------------------------------------------------------------

/// Response from creating or rotating a wallet key.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WalletKeyResponse {
    pub key: String,
}

/// Response from getting wallet key info.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WalletKeyInfoResponse {
    pub hint: String,
    pub created_at: Option<String>,
    pub last_used_at: Option<String>,
}

// ---------------------------------------------------------------------------
// API Keys
// ---------------------------------------------------------------------------

/// Response from rotating an API key.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RotateApiKeyResponse {
    pub key: String,
    pub name: String,
}

// ---------------------------------------------------------------------------
// Invoices
// ---------------------------------------------------------------------------

/// Status of an invoice.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum InvoiceStatus {
    Pending,
    Settled,
    Expired,
    #[serde(other)]
    Unknown,
}

/// Parameters for creating a new invoice.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateInvoiceRequest {
    pub amount: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memo: Option<String>,
}

impl CreateInvoiceRequest {
    /// Creates a new invoice request for the given amount in sats.
    pub fn new(amount: i64) -> Self {
        Self {
            amount,
            reference: None,
            memo: None,
        }
    }

    /// Sets the memo (description) for the invoice.
    #[must_use]
    pub fn memo(mut self, memo: impl Into<String>) -> Self {
        self.memo = Some(memo.into());
        self
    }

    /// Sets an external reference for the invoice.
    #[must_use]
    pub fn reference(mut self, reference: impl Into<String>) -> Self {
        self.reference = Some(reference.into());
        self
    }
}

/// An invoice returned by the API.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InvoiceResponse {
    pub number: i32,
    pub status: InvoiceStatus,
    pub amount: i64,
    pub bolt11: String,
    pub reference: Option<String>,
    pub memo: Option<String>,
    pub preimage: Option<String>,
    pub tx_number: Option<i32>,
    pub created_at: Option<String>,
    pub settled_at: Option<String>,
    pub expires_at: Option<String>,
}

/// Parameters for creating an invoice for a specific wallet (unauthenticated).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateInvoiceForWalletRequest {
    pub wallet_id: String,
    pub amount: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl CreateInvoiceForWalletRequest {
    pub fn new(wallet_id: impl Into<String>, amount: i64) -> Self {
        Self {
            wallet_id: wallet_id.into(),
            amount,
            reference: None,
            comment: None,
        }
    }

    #[must_use]
    pub fn reference(mut self, reference: impl Into<String>) -> Self {
        self.reference = Some(reference.into());
        self
    }

    #[must_use]
    pub fn comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(comment.into());
        self
    }
}

/// Parameters for creating an invoice for a Lightning address (unauthenticated).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateInvoiceForAddressRequest {
    pub address: String,
    pub amount: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

impl CreateInvoiceForAddressRequest {
    pub fn new(address: impl Into<String>, amount: i64) -> Self {
        Self {
            address: address.into(),
            amount,
            tag: None,
            comment: None,
        }
    }

    #[must_use]
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tag = Some(tag.into());
        self
    }

    #[must_use]
    pub fn comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(comment.into());
        self
    }
}

/// An invoice created via wallet ID or Lightning address.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AddressInvoiceResponse {
    pub bolt11: String,
    pub amount: i64,
    pub expires_at: Option<String>,
}

/// Pagination parameters for list endpoints.
#[derive(Debug, Clone, Default)]
pub struct ListParams {
    pub limit: Option<i32>,
    pub after: Option<i32>,
}

impl ListParams {
    #[must_use]
    pub fn limit(mut self, limit: i32) -> Self {
        self.limit = Some(limit);
        self
    }

    #[must_use]
    pub fn after(mut self, after: i32) -> Self {
        self.after = Some(after);
        self
    }
}

/// Type of an invoice SSE event.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum InvoiceEventType {
    Settled,
    Expired,
    Unknown(String),
}

impl From<&str> for InvoiceEventType {
    fn from(s: &str) -> Self {
        match s {
            "settled" => Self::Settled,
            "expired" => Self::Expired,
            other => Self::Unknown(other.to_string()),
        }
    }
}

/// A real-time invoice event from the SSE stream.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct InvoiceEvent {
    pub event: InvoiceEventType,
    pub data: InvoiceResponse,
}

// ---------------------------------------------------------------------------
// Payments
// ---------------------------------------------------------------------------

/// Status of a payment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum PaymentStatus {
    Pending,
    Processing,
    Settled,
    Failed,
    #[serde(other)]
    Unknown,
}

/// Parameters for creating a new payment.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreatePaymentRequest {
    pub target: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub idempotency_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_fee: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
}

impl CreatePaymentRequest {
    pub fn new(target: impl Into<String>) -> Self {
        Self {
            target: target.into(),
            amount: None,
            idempotency_key: None,
            max_fee: None,
            reference: None,
        }
    }

    #[must_use]
    pub fn amount(mut self, amount: i64) -> Self {
        self.amount = Some(amount);
        self
    }

    #[must_use]
    pub fn idempotency_key(mut self, key: impl Into<String>) -> Self {
        self.idempotency_key = Some(key.into());
        self
    }

    #[must_use]
    pub fn max_fee(mut self, fee: i64) -> Self {
        self.max_fee = Some(fee);
        self
    }

    #[must_use]
    pub fn reference(mut self, reference: impl Into<String>) -> Self {
        self.reference = Some(reference.into());
        self
    }
}

/// A payment returned by the API.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PaymentResponse {
    pub number: i32,
    pub status: PaymentStatus,
    pub amount: i64,
    pub max_fee: i64,
    pub service_fee: i64,
    pub actual_fee: Option<i64>,
    pub address: String,
    pub reference: Option<String>,
    pub preimage: Option<String>,
    pub tx_number: Option<i32>,
    pub failure_reason: Option<String>,
    pub created_at: Option<String>,
    pub settled_at: Option<String>,
}

/// Response from resolving a payment target.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ResolveTargetResponse {
    pub target: String,
    #[serde(rename = "type")]
    pub target_type: String,
    pub amount: Option<i64>,
    pub description: Option<String>,
}

/// Type of a payment SSE event.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PaymentEventType {
    Settled,
    Failed,
    Unknown(String),
}

impl From<&str> for PaymentEventType {
    fn from(s: &str) -> Self {
        match s {
            "settled" => Self::Settled,
            "failed" => Self::Failed,
            other => Self::Unknown(other.to_string()),
        }
    }
}

/// A real-time payment event from the SSE stream.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PaymentEvent {
    pub event: PaymentEventType,
    pub data: PaymentResponse,
}

// ---------------------------------------------------------------------------
// Addresses
// ---------------------------------------------------------------------------

/// Parameters for creating a new Lightning address.
#[derive(Debug, Clone, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CreateAddressRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address: Option<String>,
}

/// A Lightning address returned by the API.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AddressResponse {
    pub address: String,
    pub generated: bool,
    pub cost: i64,
    pub created_at: Option<String>,
}

/// Parameters for transferring a Lightning address to another wallet.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TransferAddressRequest {
    pub target_wallet_key: String,
}

impl TransferAddressRequest {
    pub fn new(target_wallet_key: impl Into<String>) -> Self {
        Self {
            target_wallet_key: target_wallet_key.into(),
        }
    }
}

/// Response from transferring a Lightning address.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransferAddressResponse {
    pub address: String,
    pub transferred_to: String,
}

// ---------------------------------------------------------------------------
// Transactions
// ---------------------------------------------------------------------------

/// Type of a transaction.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum TransactionType {
    Credit,
    Debit,
    #[serde(other)]
    Unknown,
}

/// A transaction returned by the API.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransactionResponse {
    pub number: i32,
    #[serde(rename = "type")]
    pub tx_type: TransactionType,
    pub amount: i64,
    pub balance_after: i64,
    pub network_fee: i64,
    pub service_fee: i64,
    pub payment_hash: Option<String>,
    pub preimage: Option<String>,
    pub reference: Option<String>,
    pub note: Option<String>,
    pub created_at: Option<String>,
}

// ---------------------------------------------------------------------------
// Webhooks
// ---------------------------------------------------------------------------

/// Parameters for creating a new webhook.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateWebhookRequest {
    pub url: String,
}

impl CreateWebhookRequest {
    pub fn new(url: impl Into<String>) -> Self {
        Self { url: url.into() }
    }
}

/// Response from creating a new webhook, including the signing secret.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CreateWebhookResponse {
    pub id: String,
    pub url: String,
    pub secret: String,
    pub created_at: Option<String>,
}

/// A webhook returned by the API.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WebhookResponse {
    pub id: String,
    pub url: String,
    pub active: bool,
    pub created_at: Option<String>,
}

// ---------------------------------------------------------------------------
// Wallet event stream
// ---------------------------------------------------------------------------

/// A real-time event from the wallet event stream.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WalletEvent {
    pub event: String,
    pub created_at: String,
    pub data: serde_json::Value,
}

// ---------------------------------------------------------------------------
// Backup / Restore
// ---------------------------------------------------------------------------

/// Response containing the wallet's recovery passphrase.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct RecoveryBackupResponse {
    pub passphrase: String,
}

/// Parameters for restoring a wallet from a recovery passphrase.
#[derive(Debug, Clone, Serialize)]
pub struct RecoveryRestoreRequest {
    pub passphrase: String,
}

impl RecoveryRestoreRequest {
    pub fn new(passphrase: impl Into<String>) -> Self {
        Self {
            passphrase: passphrase.into(),
        }
    }
}

/// Response from a wallet recovery.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RecoveryRestoreResponse {
    pub wallet_id: String,
    pub name: String,
    pub primary_key: String,
    pub secondary_key: String,
}

/// Response from beginning a passkey backup flow.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct BackupPasskeyBeginResponse {
    pub session_id: String,
    pub options: HashMap<String, serde_json::Value>,
}

/// Parameters for completing a passkey backup flow.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BackupPasskeyCompleteRequest {
    pub session_id: String,
    pub attestation: HashMap<String, serde_json::Value>,
}

/// Response from beginning a passkey restore flow.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RestorePasskeyBeginResponse {
    pub session_id: String,
    pub options: HashMap<String, serde_json::Value>,
}

/// Parameters for completing a passkey restore flow.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RestorePasskeyCompleteRequest {
    pub session_id: String,
    pub assertion: HashMap<String, serde_json::Value>,
}

/// Response from completing a passkey restore flow.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RestorePasskeyCompleteResponse {
    pub wallet_id: String,
    pub name: String,
    pub primary_key: String,
    pub secondary_key: String,
}

// ---------------------------------------------------------------------------
// L402
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateL402ChallengeRequest {
    pub amount: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expiry_seconds: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caveats: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct L402ChallengeResponse {
    pub macaroon: String,
    pub invoice: String,
    pub payment_hash: String,
    pub expires_at: String,
    pub www_authenticate: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifyL402Request {
    pub authorization: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifyL402Response {
    pub valid: bool,
    pub payment_hash: Option<String>,
    pub caveats: Option<Vec<String>>,
    pub error: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PayL402Request {
    pub www_authenticate: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_fee: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub wait: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout: Option<i32>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct L402PayResponse {
    pub authorization: Option<String>,
    pub payment_hash: String,
    pub preimage: Option<String>,
    pub amount: i64,
    pub fee: Option<i64>,
    pub payment_number: i32,
    pub status: String,
}