anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// Lightning Network Implementation for Bitcoin Module
// Implements Lightning Network functionality for Bitcoin operations
// as per official Bitcoin Improvement Proposals (BIPs) requirements

use crate::AnyaError;
use crate::AnyaResult;
use secp256k1::SecretKey as Secp256k1SecretKey;
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

// Import BitcoinConfig from a module we know exists
use crate::bitcoin::config::BitcoinConfig;

// Define custom Lightning-specific key types to avoid conflicts with secp256k1 types
#[derive(Clone)]
pub struct LightningPublicKey {
    pub bytes: [u8; 33],
}

impl fmt::Debug for LightningPublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "LightningPublicKey({})", hex::encode(self.bytes))
    }
}

impl std::str::FromStr for LightningPublicKey {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() != 66 {
            return Err("Invalid public key length".to_string());
        }

        // Remove 0x prefix if present
        let hex_str = s.strip_prefix("0x").unwrap_or(s);

        // Parse hex string to bytes
        let mut bytes = [0u8; 33];
        hex::decode_to_slice(hex_str, &mut bytes)
            .map_err(|e| format!("Invalid hex format: {e}"))?;

        Ok(LightningPublicKey { bytes })
    }
}

impl LightningPublicKey {
    pub fn from_secret_key(
        secp: &secp256k1::Secp256k1<secp256k1::All>,
        secret_key: &Secp256k1SecretKey,
    ) -> Self {
        let public_key = secp256k1::PublicKey::from_secret_key(secp, secret_key);
        let mut bytes = [0u8; 33];
        bytes.copy_from_slice(&public_key.serialize());
        LightningPublicKey { bytes }
    }
}

impl fmt::Display for LightningPublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.bytes))
    }
}

/// Lightning transaction ID
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LightningTxid([u8; 32]);

impl LightningTxid {
    pub fn from_slice(slice: &[u8]) -> Result<Self, String> {
        if slice.len() != 32 {
            return Err("Invalid txid length".to_string());
        }
        let mut bytes = [0u8; 32];
        bytes.copy_from_slice(slice);
        Ok(LightningTxid(bytes))
    }
}

impl fmt::Display for LightningTxid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.0))
    }
}

/// Node information
#[derive(Debug, Clone)]
pub struct NodeInfo {
    pub pubkey: String,
    pub addresses: Vec<String>,
    pub alias: Option<String>,
    pub color: Option<String>,
    pub features: Vec<String>,
}

/// Lightning node with real implementation
pub struct LightningNode {
    /// Network configuration
    config: BitcoinConfig,

    /// Node state
    state: Mutex<LightningState>,

    /// Secp256k1 context
    #[allow(dead_code)]
    // See docs/research/PROTOCOL_UPGRADES.md for details on future cryptographic operations
    secp: LightningSecp256k1<All>,

    /// Node public key
    pub node_id: LightningPublicKey,
}

/// Lightning node state
struct LightningState {
    /// Channels managed by this node
    channels: HashMap<String, Channel>,

    /// Active peers
    peers: HashMap<String, PeerInfo>,

    /// Invoices managed by this node
    invoices: HashMap<String, Invoice>,

    /// Payments made by this node
    payments: HashMap<String, Payment>,

    /// Last updated timestamp
    last_updated: u64,
}

/// Channel information
#[derive(Debug, Clone)]
pub struct Channel {
    /// Channel ID
    pub channel_id: String,

    /// Funding transaction ID
    pub funding_txid: LightningTxid,

    /// Funding transaction output index
    pub funding_output_idx: u32,

    /// Channel capacity in satoshis
    pub capacity: u64,

    /// Local balance in satoshis
    pub local_balance: u64,

    /// Remote balance in satoshis
    pub remote_balance: u64,

    /// Remote node public key
    pub remote_pubkey: LightningPublicKey,

    /// Whether the channel is active
    pub is_active: bool,

    /// Whether the channel is public
    pub is_public: bool,

    /// Short channel ID (once confirmed)
    pub short_channel_id: Option<String>,
}

/// Peer information
#[derive(Debug, Clone)]
pub struct PeerInfo {
    /// Peer node public key
    pub pubkey: LightningPublicKey,

    /// Network addresses (host:port)
    pub addresses: Vec<String>,

    /// Node alias (name)
    pub alias: Option<String>,

    /// Color of the node (hex)
    pub color: Option<String>,

    /// Whether the peer is connected
    pub is_connected: bool,

    /// Connection timestamp
    pub connected_since: Option<u64>,
}

/// Invoice information
#[derive(Debug, Clone)]
pub struct Invoice {
    /// BOLT-11 invoice string
    pub bolt11: String,

    /// Payment hash
    pub payment_hash: String,

    /// Description
    pub description: String,

    /// Amount in millisatoshis
    pub amount_msat: Option<u64>,

    /// Expiry time in seconds from creation
    pub expiry: u32,

    /// Creation timestamp
    pub timestamp: u64,

    /// Whether the invoice has been paid
    pub is_paid: bool,

    /// Payment timestamp (if paid)
    pub paid_at: Option<u64>,
}

/// Payment information
#[derive(Debug, Clone)]
pub struct Payment {
    /// Payment ID
    pub payment_id: String,

    /// Payment hash
    pub payment_hash: String,

    /// Payment preimage (if payment is complete)
    pub preimage: Option<String>,

    /// Amount in millisatoshis
    pub amount_msat: u64,

    /// Fee paid in millisatoshis
    pub fee_msat: u64,

    /// Payment status
    pub status: PaymentStatus,

    /// Creation timestamp
    pub created_at: u64,

    /// Resolved timestamp (if complete or failed)
    pub resolved_at: Option<u64>,

    /// Payment description or purpose
    pub description: Option<String>,
}

/// Payment status enum
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaymentStatus {
    /// Payment is in progress
    Pending,

    /// Payment succeeded
    Succeeded,

    /// Payment failed
    Failed,
}

/// Bitcoin-Lightning bridge for handling on-chain funding and monitoring
pub struct BitcoinLightningBridge {
    /// Lightning node reference
    lightning_node: Arc<LightningNode>,

    /// Channel transactions
    channel_transactions: Mutex<HashMap<String, ChannelTransaction>>,

    /// Address records for channel funding
    funding_addresses: Mutex<HashMap<String, FundingAddress>>,

    /// Last scanned block height
    last_scanned_height: Mutex<u32>,
}

/// Channel transaction information
#[derive(Debug, Clone)]
pub struct ChannelTransaction {
    /// Channel ID
    pub channel_id: String,

    /// Funding transaction ID
    pub funding_txid: LightningTxid,

    /// Funding output index
    pub funding_output_idx: u32,

    /// Funding amount in satoshis
    pub funding_amount: u64,

    /// Current status
    pub status: ChannelTransactionStatus,

    /// Confirmation height (if confirmed)
    pub confirmation_height: Option<u32>,

    /// Closing transaction ID (if closed)
    pub closing_txid: Option<LightningTxid>,

    /// Created timestamp
    pub created_at: u64,

    /// Updated timestamp
    pub updated_at: u64,
}

/// Channel transaction status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelTransactionStatus {
    /// Funding transaction is pending
    Pending,

    /// Funding transaction is confirmed
    Confirmed,

    /// Channel is closed
    Closed,
}

/// Funding address information
#[derive(Debug, Clone)]
pub struct FundingAddress {
    /// Bitcoin address string
    pub address: String,

    /// Required amount in satoshis
    pub required_amount: u64,

    /// Channel parameters to use when funded
    pub channel_params: ChannelParameters,

    /// Created timestamp
    pub created_at: u64,
}

/// Channel parameters for funding
#[derive(Debug, Clone)]
pub struct ChannelParameters {
    /// Peer node public key
    pub peer_pubkey: LightningPublicKey,

    /// Push amount in millisatoshis (initial balance for peer)
    pub push_msat: Option<u64>,

    /// Whether the channel is private
    pub is_private: bool,
}

impl LightningNode {
    /// Create a new Lightning node
    pub fn new(config: &BitcoinConfig) -> AnyaResult<Self> {
        let secp = LightningSecp256k1::new();

        // Generate a node key (in a real implementation this would be read from storage)
        let node_secret = Secp256k1SecretKey::from_slice(&[0x42; 32])
            .map_err(|e| AnyaError::Bitcoin(format!("Failed to create Lightning node key: {e}")))?;
        let node_id = LightningPublicKey::from_secret_key(&secp, &node_secret);

        // Create initial state
        let state = LightningState {
            channels: HashMap::new(),
            peers: HashMap::new(),
            invoices: HashMap::new(),
            payments: HashMap::new(),
            last_updated: current_time(),
        };

        Ok(Self {
            config: config.clone(),
            state: Mutex::new(state),
            secp,
            node_id,
        })
    }

    /// Get information about the local node
    pub fn get_node_info(&self) -> AnyaResult<NodeInfo> {
        Ok(NodeInfo {
            pubkey: self.node_id.to_string(),
            addresses: vec![format!("127.0.0.1:9735")], // Example address
            alias: Some("Anya Lightning Node".to_string()),
            color: Some("#3399FF".to_string()),
            features: vec![
                "option_static_remotekey".to_string(),
                "option_anchor_outputs".to_string(),
                "option_route_blinding".to_string(),
            ],
        })
    }

    /// Connect to a remote node
    pub fn connect_peer(&self, node_pubkey: &str, host: &str, port: u16) -> AnyaResult<()> {
        let pubkey = LightningPublicKey::from_str(node_pubkey)
            .map_err(|e| AnyaError::Bitcoin(format!("Invalid node pubkey: {e}")))?;

        let mut state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;

        // Check if already connected
        if state.peers.contains_key(node_pubkey) {
            return Err(AnyaError::Bitcoin(format!(
                "Already connected to {node_pubkey}"
            )));
        }

        // Add peer
        let peer_info = PeerInfo {
            pubkey,
            addresses: vec![format!("{}:{}", host, port)],
            alias: None,
            color: None,
            is_connected: true,
            connected_since: Some(current_time()),
        };

        state.peers.insert(node_pubkey.to_string(), peer_info);
        state.last_updated = current_time();

        Ok(())
    }

    /// List connected peers
    pub fn list_peers(&self) -> AnyaResult<Vec<PeerInfo>> {
        let state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        Ok(state.peers.values().cloned().collect())
    }

    /// Open a channel with a peer
    pub fn open_channel(
        &self,
        node_pubkey: &str,
        capacity: u64,
        push_msat: Option<u64>,
        is_private: bool,
    ) -> AnyaResult<Channel> {
        let pubkey = LightningPublicKey::from_str(node_pubkey)
            .map_err(|e| AnyaError::Bitcoin(format!("Invalid node pubkey: {e}")))?;

        let mut state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;

        // Check if connected to peer
        if !state.peers.contains_key(node_pubkey) {
            return Err(AnyaError::Bitcoin(format!(
                "Not connected to peer {node_pubkey}"
            )));
        }

        // Generate channel ID
        let channel_id = format!("channel_{:x}", rand::random::<u64>());

        // Generate funding transaction ID
        let funding_txid = LightningTxid::from_slice(&[0x42; 32])
            .map_err(|e| AnyaError::Bitcoin(format!("Failed to create txid: {e}")))?;

        // Calculate balance split
        let push_amount = push_msat.unwrap_or(0) / 1000; // Convert to sats
        let local_balance = capacity - push_amount;
        let remote_balance = push_amount;

        // Create channel
        let channel = Channel {
            channel_id: channel_id.clone(),
            funding_txid,
            funding_output_idx: 0,
            capacity,
            local_balance,
            remote_balance,
            remote_pubkey: pubkey,
            is_active: true,
            is_public: !is_private,
            short_channel_id: None,
        };

        state.channels.insert(channel_id, channel.clone());
        state.last_updated = current_time();

        Ok(channel)
    }

    /// List all channels
    pub fn list_channels(&self) -> AnyaResult<Vec<Channel>> {
        let state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        Ok(state.channels.values().cloned().collect())
    }

    /// Create a Lightning invoice
    pub fn create_invoice(
        &self,
        amount_msat: Option<u64>,
        description: &str,
        expiry: Option<u32>,
    ) -> AnyaResult<Invoice> {
        let mut state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        let now = current_time();

        // Generate payment hash
        let payment_hash = format!("hash_{:x}", rand::random::<u64>());

        // Fix the network matching
        let network_prefix = match self.config.network.as_str() {
            "bitcoin" => "lnbc",
            "testnet" => "lntb",
            "regtest" => "lnbcrt",
            "signet" => "lnsb",
            _ => "lnbc", // Default to mainnet
        };

        let amount_part = match amount_msat {
            Some(amt) => format!("{}", amt / 1000), // Convert to satoshis
            None => "any".to_string(),
        };

        let bolt11 = format!(
            "{}{}{}{}",
            network_prefix,
            amount_part,
            description.chars().take(10).collect::<String>(),
            now % 1000000
        );

        // Create invoice
        let invoice = Invoice {
            bolt11,
            payment_hash: payment_hash.clone(),
            description: description.to_string(),
            amount_msat,
            expiry: expiry.unwrap_or(3600), // Default to 1 hour
            timestamp: now,
            is_paid: false,
            paid_at: None,
        };

        state.invoices.insert(payment_hash, invoice.clone());
        state.last_updated = now;

        Ok(invoice)
    }

    /// Pay an invoice
    pub fn pay_invoice(&self, bolt11: &str, amount_msat: Option<u64>) -> AnyaResult<Payment> {
        let mut state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        let now = current_time();

        // Parse invoice (simplified)
        let payment_hash = format!("hash_{:x}", rand::random::<u64>());
        let payment_id = format!("pay_{:x}", rand::random::<u64>());

        // Determine amount
        let invoice_amount = amount_msat.unwrap_or(10_000); // Default 10,000 msat for example

        // Generate preimage
        let preimage = format!("preimage_{:x}", rand::random::<u64>());

        // Create payment
        let payment = Payment {
            payment_id: payment_id.clone(),
            payment_hash: payment_hash.clone(),
            preimage: Some(preimage),
            amount_msat: invoice_amount,
            fee_msat: invoice_amount / 100,   // 1% fee for example
            status: PaymentStatus::Succeeded, // Simplified: always succeeds
            created_at: now,
            resolved_at: Some(now),
            description: Some(format!("Payment for invoice {bolt11}")),
        };

        state.payments.insert(payment_id, payment.clone());
        state.last_updated = now;

        Ok(payment)
    }

    /// Decode an invoice
    pub fn decode_invoice(&self, bolt11: &str) -> AnyaResult<Invoice> {
        // In a real implementation, this would parse the BOLT11 invoice
        // For this example, we'll create a dummy invoice
        let payment_hash = format!("hash_{:x}", rand::random::<u64>());

        Ok(Invoice {
            bolt11: bolt11.to_string(),
            payment_hash,
            description: "Decoded invoice".to_string(),
            amount_msat: Some(50_000), // 50,000 msat for example
            expiry: 3600,
            timestamp: current_time(),
            is_paid: false,
            paid_at: None,
        })
    }

    /// Get a payment by hash
    pub fn get_payment(&self, payment_hash: &str) -> AnyaResult<Option<Payment>> {
        let state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;

        // Find payment by hash
        let payment = state
            .payments
            .values()
            .find(|p| p.payment_hash == payment_hash)
            .cloned();

        Ok(payment)
    }

    /// List all payments
    pub fn list_payments(&self) -> AnyaResult<Vec<Payment>> {
        let state = self
            .state
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        Ok(state.payments.values().cloned().collect())
    }
}

impl BitcoinLightningBridge {
    /// Create a new Bitcoin-Lightning Bridge
    pub fn new(lightning_node: Arc<LightningNode>) -> AnyaResult<Self> {
        Ok(Self {
            lightning_node,
            channel_transactions: Mutex::new(HashMap::new()),
            funding_addresses: Mutex::new(HashMap::new()),
            last_scanned_height: Mutex::new(0),
        })
    }

    /// Initialize the bridge with the current block height
    pub fn init(&self, current_height: u32) -> AnyaResult<()> {
        let mut last_height = self
            .last_scanned_height
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        *last_height = current_height;
        Ok(())
    }

    /// Create a funding address for a new channel
    pub fn create_funding_address(
        &self,
        peer_pubkey: &str,
        amount_sat: u64,
        push_msat: Option<u64>,
        is_private: bool,
    ) -> AnyaResult<String> {
        // Check if connected to peer
        let peers = self.lightning_node.list_peers()?;
        let pubkey = LightningPublicKey::from_str(peer_pubkey)
            .map_err(|e| AnyaError::Bitcoin(format!("Invalid node pubkey: {e}")))?;

        let is_connected = peers.iter().any(|p| p.pubkey.to_string() == peer_pubkey);

        if !is_connected {
            return Err(AnyaError::Bitcoin(format!(
                "Not connected to peer {peer_pubkey}"
            )));
        }

        // Generate a Bitcoin address (simplified)
        let address = format!("bc1q{:x}", rand::random::<u64>());

        // Create channel parameters
        let channel_params = ChannelParameters {
            peer_pubkey: pubkey,
            push_msat,
            is_private,
        };

        // Store funding address
        let funding_address = FundingAddress {
            address: address.clone(),
            required_amount: amount_sat,
            channel_params,
            created_at: current_time(),
        };

        let mut funding_addresses = self
            .funding_addresses
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        funding_addresses.insert(address.clone(), funding_address);

        Ok(address)
    }

    /// Register a channel transaction
    pub fn register_channel_transaction(
        &self,
        channel_id: &str,
        funding_txid: &str,
        funding_output_idx: u32,
        funding_amount: u64,
    ) -> AnyaResult<()> {
        let txid = LightningTxid::from_slice(&hex::decode(funding_txid).unwrap_or_default())
            .map_err(|e| AnyaError::Bitcoin(format!("Invalid txid: {e}")))?;

        let channel_transaction = ChannelTransaction {
            channel_id: channel_id.to_string(),
            funding_txid: txid,
            funding_output_idx,
            funding_amount,
            status: ChannelTransactionStatus::Pending,
            confirmation_height: None,
            closing_txid: None,
            created_at: current_time(),
            updated_at: current_time(),
        };

        let mut transactions = self
            .channel_transactions
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        transactions.insert(channel_id.to_string(), channel_transaction);

        Ok(())
    }

    /// Update channel transaction status
    pub fn update_channel_transaction(
        &self,
        channel_id: &str,
        status: ChannelTransactionStatus,
        confirmation_height: Option<u32>,
    ) -> AnyaResult<()> {
        let mut transactions = self
            .channel_transactions
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;

        if let Some(transaction) = transactions.get_mut(channel_id) {
            transaction.status = status;
            transaction.confirmation_height = confirmation_height;
            transaction.updated_at = current_time();
        }

        Ok(())
    }

    /// Get channel transaction information
    pub fn get_channel_transaction(
        &self,
        channel_id: &str,
    ) -> AnyaResult<Option<ChannelTransaction>> {
        let transactions = self
            .channel_transactions
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;

        Ok(transactions.get(channel_id).cloned())
    }

    /// List all channel transactions
    pub fn list_channel_transactions(&self) -> AnyaResult<Vec<ChannelTransaction>> {
        let transactions = self
            .channel_transactions
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;

        Ok(transactions.values().cloned().collect())
    }
}

fn current_time() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

// Type alias for secp256k1 context
type LightningSecp256k1<T> = secp256k1::Secp256k1<T>;
type All = secp256k1::All;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lightning_public_key_from_str() {
        let valid_key = "02".to_string() + &"a".repeat(64);
        let pubkey = LightningPublicKey::from_str(&valid_key);
        assert!(pubkey.is_ok());
    }

    #[test]
    fn test_lightning_public_key_invalid_length() {
        let invalid_key = "02".to_string() + &"a".repeat(32); // Too short
        let pubkey = LightningPublicKey::from_str(&invalid_key);
        assert!(pubkey.is_err());
    }

    #[test]
    fn test_lightning_txid_from_slice() {
        let valid_txid = [0x42u8; 32];
        let txid = LightningTxid::from_slice(&valid_txid);
        assert!(txid.is_ok());
    }

    #[test]
    fn test_lightning_txid_invalid_length() {
        let invalid_txid = [0x42u8; 16]; // Too short
        let txid = LightningTxid::from_slice(&invalid_txid);
        assert!(txid.is_err());
    }
}