anya_core/bitcoin/adapters/
mod.rs

1use std::error::Error;
2// [AIR-3][AIS-3][BPC-3][RES-3] Bitcoin adapters module implementation
3// This follows official Bitcoin Improvement Proposals (BIPs) standards for hexagonal architecture
4use std::sync::Arc;
5// [AIR-3][AIS-3][BPC-3][RES-3] Removed unused import: async_trait::async_trait
6
7// [AIR-3][AIS-3][BPC-3][RES-3] Import Bitcoin interface types
8// This follows official Bitcoin Improvement Proposals (BIPs) standards for type consistency
9use crate::bitcoin::config::BitcoinConfig;
10use crate::bitcoin::interface::{
11    Address,
12    AddressType,
13    BitcoinImplementationType,
14    // [AIR-3][AIS-3][BPC-3][RES-3] Removed unused import: BitcoinError
15    BitcoinInterface,
16    BitcoinResult,
17    Block,
18    BlockHeader,
19    Transaction,
20};
21
22/// [AIR-3][AIS-3][BPC-3][RES-3] Bitcoin adapter for Bitcoin implementation
23#[allow(dead_code)]
24pub struct BitcoinAdapter {
25    /// Configuration
26    config: Arc<BitcoinConfig>,
27
28    /// Implementation
29    implementation: Arc<dyn BitcoinInterface>,
30}
31
32impl BitcoinAdapter {
33    /// Create a new Bitcoin adapter
34    pub async fn new(config: BitcoinConfig) -> Result<Self, Box<dyn Error>> {
35        let implementation = Arc::new(crate::bitcoin::rust::RustBitcoinImplementation::new(
36            &config,
37        )?) as Arc<dyn BitcoinInterface>;
38
39        Ok(Self {
40            config: Arc::new(config),
41            implementation,
42        })
43    }
44
45    /// Get the Bitcoin implementation
46    pub fn get_implementation(&self) -> Arc<dyn BitcoinInterface> {
47        self.implementation.clone()
48    }
49}
50
51/// Implementation of BitcoinInterface following hexagonal architecture pattern
52/// [AIR-3][AIS-3][BPC-3][RES-3] Using async_trait for async interface implementation
53#[async_trait::async_trait]
54impl BitcoinInterface for BitcoinAdapter {
55    /// [AIR-3][AIS-3][BPC-3][RES-3] Get transaction by ID
56    async fn get_transaction(&self, txid: &str) -> BitcoinResult<Transaction> {
57        self.implementation.get_transaction(txid).await
58    }
59
60    /// [AIR-3][AIS-3][BPC-3][RES-3] Get block by hash
61    async fn get_block(&self, hash: &str) -> BitcoinResult<Block> {
62        self.implementation.get_block(hash).await
63    }
64
65    /// [AIR-3][AIS-3][BPC-3][RES-3] Get current block height
66    async fn get_block_height(&self) -> BitcoinResult<u32> {
67        self.implementation.get_block_height().await
68    }
69
70    /// [AIR-3][AIS-3][BPC-3][RES-3] Generate address of specified type
71    async fn generate_address(&self, address_type: AddressType) -> BitcoinResult<Address> {
72        self.implementation.generate_address(address_type).await
73    }
74
75    /// [AIR-3][AIS-3][BPC-3][RES-3] Create transaction with outputs and fee rate
76    async fn create_transaction(
77        &self,
78        outputs: Vec<(String, u64)>,
79        fee_rate: u64,
80    ) -> BitcoinResult<Transaction> {
81        self.implementation
82            .create_transaction(outputs, fee_rate)
83            .await
84    }
85
86    /// [AIR-3][AIS-3][BPC-3][RES-3] Broadcast transaction to network
87    async fn broadcast_transaction(&self, transaction: &Transaction) -> BitcoinResult<String> {
88        self.implementation.broadcast_transaction(transaction).await
89    }
90
91    /// [AIR-3][AIS-3][BPC-3][RES-3] Get balance for address
92    async fn get_balance(&self, address: &Address) -> BitcoinResult<u64> {
93        self.implementation.get_balance(address).await
94    }
95
96    /// [AIR-3][AIS-3][BPC-3][RES-3] Estimate fee for target confirmation blocks
97    async fn estimate_fee(&self, target_blocks: u8) -> BitcoinResult<u64> {
98        self.implementation.estimate_fee(target_blocks).await
99    }
100
101    /// [AIR-3][AIS-3][BPC-3][RES-3] Get block header by hash
102    async fn get_block_header(&self, hash: &str) -> BitcoinResult<BlockHeader> {
103        self.implementation.get_block_header(hash).await
104    }
105
106    /// [AIR-3][AIS-3][BPC-3][RES-3] Verify merkle proof for transaction
107    async fn verify_merkle_proof(
108        &self,
109        tx_hash: &str,
110        block_header: &BlockHeader,
111    ) -> BitcoinResult<bool> {
112        self.implementation
113            .verify_merkle_proof(tx_hash, block_header)
114            .await
115    }
116
117    /// [AIR-3][AIS-3][BPC-3][RES-3] Send transaction to network
118    async fn send_transaction(&self, tx: &Transaction) -> BitcoinResult<String> {
119        self.implementation.send_transaction(tx).await
120    }
121
122    fn implementation_type(&self) -> BitcoinImplementationType {
123        self.implementation.implementation_type()
124    }
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[tokio::test]
132    async fn test_adapter_initialization() -> Result<(), Box<dyn Error>> {
133        let config = BitcoinConfig::default();
134        let adapter = BitcoinAdapter::new(config).await?;
135
136        // Check that we can get the implementation
137        let implementation = adapter.get_implementation();
138        assert_eq!(
139            implementation.implementation_type(),
140            BitcoinImplementationType::Rust
141        );
142
143        // Check the default implementation type
144        assert_eq!(
145            adapter.implementation_type(),
146            BitcoinImplementationType::Rust
147        );
148
149        // [BPC-3] Return success result
150        Ok(())
151    }
152}