rustywallet-electrum 0.2.0

Electrum protocol client for Bitcoin balance checking and UTXO fetching
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
//! Real-time subscriptions for Electrum protocol.
//!
//! This module provides subscription functionality for receiving
//! real-time updates about addresses and blockchain headers.

use std::collections::HashMap;
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::{broadcast, RwLock};

use crate::error::{ElectrumError, Result};
use crate::scripthash::address_to_scripthash;
use crate::transport::Transport;
use crate::types::ClientConfig;

/// Subscription event types.
#[derive(Debug, Clone)]
pub enum SubscriptionEvent {
    /// Address status changed (new transaction)
    AddressStatus(AddressStatusEvent),
    /// New block header received
    BlockHeader(BlockHeaderEvent),
    /// Connection status changed
    ConnectionStatus(ConnectionStatus),
}

/// Address status change event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddressStatusEvent {
    /// The address that changed
    pub address: String,
    /// The scripthash
    pub scripthash: String,
    /// New status hash (null if no history)
    pub status: Option<String>,
}

/// Block header event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockHeaderEvent {
    /// Block height
    pub height: u64,
    /// Block header hex
    pub hex: String,
}

/// Connection status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionStatus {
    /// Connected to server
    Connected,
    /// Disconnected from server
    Disconnected,
    /// Reconnecting to server
    Reconnecting,
}

/// Subscription manager for real-time updates.
pub struct SubscriptionManager {
    transport: Arc<Transport>,
    #[allow(dead_code)]
    config: ClientConfig,
    /// Active address subscriptions (scripthash -> address)
    address_subs: RwLock<HashMap<String, String>>,
    /// Whether header subscription is active
    header_sub_active: RwLock<bool>,
    /// Event broadcaster
    event_tx: broadcast::Sender<SubscriptionEvent>,
    /// Request ID counter
    request_id: std::sync::atomic::AtomicU64,
    /// Running flag
    running: RwLock<bool>,
}

impl SubscriptionManager {
    /// Create a new subscription manager.
    pub async fn new(config: ClientConfig) -> Result<Self> {
        let transport = Arc::new(Transport::connect(config.clone()).await?);
        let (event_tx, _) = broadcast::channel(1000);
        
        Ok(Self {
            transport,
            config,
            address_subs: RwLock::new(HashMap::new()),
            header_sub_active: RwLock::new(false),
            event_tx,
            request_id: std::sync::atomic::AtomicU64::new(1),
            running: RwLock::new(true),
        })
    }

    /// Get the next request ID.
    fn next_id(&self) -> u64 {
        self.request_id.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
    }

    /// Subscribe to receive events.
    pub fn subscribe(&self) -> broadcast::Receiver<SubscriptionEvent> {
        self.event_tx.subscribe()
    }

    /// Subscribe to address status changes.
    ///
    /// Returns the current status hash (or None if no history).
    pub async fn subscribe_address(&self, address: &str) -> Result<Option<String>> {
        let scripthash = address_to_scripthash(address)?;
        
        let id = self.next_id();
        let result = self.transport
            .request(id, "blockchain.scripthash.subscribe", vec![json!(scripthash)])
            .await?;

        // Store subscription
        let mut subs = self.address_subs.write().await;
        subs.insert(scripthash.clone(), address.to_string());

        // Parse status
        let status = result.as_str().map(|s| s.to_string());
        
        Ok(status)
    }

    /// Unsubscribe from address status changes.
    pub async fn unsubscribe_address(&self, address: &str) -> Result<bool> {
        let scripthash = address_to_scripthash(address)?;
        
        let id = self.next_id();
        let result = self.transport
            .request(id, "blockchain.scripthash.unsubscribe", vec![json!(scripthash)])
            .await?;

        // Remove subscription
        let mut subs = self.address_subs.write().await;
        subs.remove(&scripthash);

        Ok(result.as_bool().unwrap_or(false))
    }

    /// Subscribe to new block headers.
    ///
    /// Returns the current tip header.
    pub async fn subscribe_headers(&self) -> Result<BlockHeaderEvent> {
        let id = self.next_id();
        let result = self.transport
            .request(id, "blockchain.headers.subscribe", vec![])
            .await?;

        *self.header_sub_active.write().await = true;

        let height = result.get("height")
            .and_then(|h| h.as_u64())
            .ok_or_else(|| ElectrumError::InvalidResponse("Missing height".into()))?;
        
        let hex = result.get("hex")
            .and_then(|h| h.as_str())
            .unwrap_or("")
            .to_string();

        Ok(BlockHeaderEvent { height, hex })
    }

    /// Get all subscribed addresses.
    pub async fn subscribed_addresses(&self) -> Vec<String> {
        let subs = self.address_subs.read().await;
        subs.values().cloned().collect()
    }

    /// Check if headers subscription is active.
    pub async fn is_headers_subscribed(&self) -> bool {
        *self.header_sub_active.read().await
    }

    /// Get subscription count.
    pub async fn subscription_count(&self) -> usize {
        let subs = self.address_subs.read().await;
        let header_active = *self.header_sub_active.read().await;
        subs.len() + if header_active { 1 } else { 0 }
    }

    /// Broadcast an event to all subscribers.
    fn broadcast(&self, event: SubscriptionEvent) {
        let _ = self.event_tx.send(event);
    }

    /// Process a notification from the server.
    pub async fn process_notification(&self, method: &str, params: &[serde_json::Value]) -> Result<()> {
        match method {
            "blockchain.scripthash.subscribe" => {
                if params.len() >= 2 {
                    let scripthash = params[0].as_str().unwrap_or("").to_string();
                    let status = params[1].as_str().map(|s| s.to_string());
                    
                    let subs = self.address_subs.read().await;
                    if let Some(address) = subs.get(&scripthash) {
                        self.broadcast(SubscriptionEvent::AddressStatus(AddressStatusEvent {
                            address: address.clone(),
                            scripthash,
                            status,
                        }));
                    }
                }
            }
            "blockchain.headers.subscribe" => {
                if let Some(header) = params.first() {
                    let height = header.get("height")
                        .and_then(|h| h.as_u64())
                        .unwrap_or(0);
                    let hex = header.get("hex")
                        .and_then(|h| h.as_str())
                        .unwrap_or("")
                        .to_string();
                    
                    self.broadcast(SubscriptionEvent::BlockHeader(BlockHeaderEvent {
                        height,
                        hex,
                    }));
                }
            }
            _ => {}
        }
        
        Ok(())
    }

    /// Stop the subscription manager.
    pub async fn stop(&self) {
        *self.running.write().await = false;
    }

    /// Check if the manager is running.
    pub async fn is_running(&self) -> bool {
        *self.running.read().await
    }
}

/// Builder for subscription-enabled client.
pub struct SubscriptionClientBuilder {
    config: ClientConfig,
    addresses: Vec<String>,
    subscribe_headers: bool,
}

impl SubscriptionClientBuilder {
    /// Create a new builder.
    pub fn new(config: ClientConfig) -> Self {
        Self {
            config,
            addresses: Vec::new(),
            subscribe_headers: false,
        }
    }

    /// Add an address to subscribe to.
    pub fn subscribe_address(mut self, address: impl Into<String>) -> Self {
        self.addresses.push(address.into());
        self
    }

    /// Add multiple addresses to subscribe to.
    pub fn subscribe_addresses(mut self, addresses: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.addresses.extend(addresses.into_iter().map(|a| a.into()));
        self
    }

    /// Subscribe to block headers.
    pub fn subscribe_headers(mut self) -> Self {
        self.subscribe_headers = true;
        self
    }

    /// Build and connect the subscription client.
    pub async fn build(self) -> Result<SubscriptionClient> {
        let manager = SubscriptionManager::new(self.config).await?;
        
        // Subscribe to addresses
        for address in &self.addresses {
            manager.subscribe_address(address).await?;
        }
        
        // Subscribe to headers if requested
        if self.subscribe_headers {
            manager.subscribe_headers().await?;
        }
        
        Ok(SubscriptionClient { manager })
    }
}

/// Client with subscription support.
pub struct SubscriptionClient {
    manager: SubscriptionManager,
}

impl SubscriptionClient {
    /// Create a new subscription client.
    pub async fn new(config: ClientConfig) -> Result<Self> {
        let manager = SubscriptionManager::new(config).await?;
        Ok(Self { manager })
    }

    /// Get a builder for configuring subscriptions.
    pub fn builder(config: ClientConfig) -> SubscriptionClientBuilder {
        SubscriptionClientBuilder::new(config)
    }

    /// Subscribe to events.
    pub fn subscribe(&self) -> broadcast::Receiver<SubscriptionEvent> {
        self.manager.subscribe()
    }

    /// Subscribe to an address.
    pub async fn subscribe_address(&self, address: &str) -> Result<Option<String>> {
        self.manager.subscribe_address(address).await
    }

    /// Unsubscribe from an address.
    pub async fn unsubscribe_address(&self, address: &str) -> Result<bool> {
        self.manager.unsubscribe_address(address).await
    }

    /// Subscribe to block headers.
    pub async fn subscribe_headers(&self) -> Result<BlockHeaderEvent> {
        self.manager.subscribe_headers().await
    }

    /// Get all subscribed addresses.
    pub async fn subscribed_addresses(&self) -> Vec<String> {
        self.manager.subscribed_addresses().await
    }

    /// Get subscription count.
    pub async fn subscription_count(&self) -> usize {
        self.manager.subscription_count().await
    }

    /// Stop the client.
    pub async fn stop(&self) {
        self.manager.stop().await;
    }
}

/// Address watcher for monitoring specific addresses.
pub struct AddressWatcher {
    client: SubscriptionClient,
    addresses: Vec<String>,
}

impl AddressWatcher {
    /// Create a new address watcher.
    pub async fn new(config: ClientConfig, addresses: Vec<String>) -> Result<Self> {
        let client = SubscriptionClient::new(config).await?;
        
        for address in &addresses {
            client.subscribe_address(address).await?;
        }
        
        Ok(Self { client, addresses })
    }

    /// Subscribe to events.
    pub fn subscribe(&self) -> broadcast::Receiver<SubscriptionEvent> {
        self.client.subscribe()
    }

    /// Get watched addresses.
    pub fn addresses(&self) -> &[String] {
        &self.addresses
    }

    /// Add an address to watch.
    pub async fn watch(&mut self, address: impl Into<String>) -> Result<()> {
        let addr = address.into();
        self.client.subscribe_address(&addr).await?;
        self.addresses.push(addr);
        Ok(())
    }

    /// Stop watching an address.
    pub async fn unwatch(&mut self, address: &str) -> Result<()> {
        self.client.unsubscribe_address(address).await?;
        self.addresses.retain(|a| a != address);
        Ok(())
    }

    /// Stop the watcher.
    pub async fn stop(&self) {
        self.client.stop().await;
    }
}

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

    #[test]
    fn test_address_status_event() {
        let event = AddressStatusEvent {
            address: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa".to_string(),
            scripthash: "abc123".to_string(),
            status: Some("def456".to_string()),
        };
        
        assert_eq!(event.address, "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa");
        assert!(event.status.is_some());
    }

    #[test]
    fn test_block_header_event() {
        let event = BlockHeaderEvent {
            height: 800000,
            hex: "0100000000000000".to_string(),
        };
        
        assert_eq!(event.height, 800000);
    }

    #[test]
    fn test_connection_status() {
        assert_eq!(ConnectionStatus::Connected, ConnectionStatus::Connected);
        assert_ne!(ConnectionStatus::Connected, ConnectionStatus::Disconnected);
    }
}