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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
//! Electrum client
//!
//! This module contains definitions of all the complex data structures that are returned by calls

use std::collections::{BTreeMap, VecDeque};
use std::io::{self, BufRead, BufReader, Read, Write};
use std::net::{TcpStream, ToSocketAddrs};

#[allow(unused_imports)]
use log::{debug, error, info, trace};

use bitcoin::blockdata::block;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode::{deserialize, serialize};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::{Script, Txid};

#[cfg(feature = "use-openssl")]
use openssl::ssl::{SslConnector, SslMethod, SslStream, SslVerifyMode};
#[cfg(all(
    any(feature = "default", feature = "use-rustls"),
    not(feature = "use-openssl")
))]
use rustls::{ClientConfig, ClientSession, StreamOwned};

#[cfg(any(feature = "default", feature = "proxy"))]
use socks::{Socks5Stream, ToTargetAddr};

use stream::ClonableStream;

use batch::Batch;
use types::*;

macro_rules! impl_batch_call {
    ( $self:expr, $data:expr, $call:ident ) => {{
        let mut batch = Batch::default();
        for i in $data {
            batch.$call(i);
        }

        let resp = $self.batch_call(batch)?;
        let mut answer = Vec::new();

        for x in resp {
            answer.push(serde_json::from_value(x)?);
        }

        Ok(answer)
    }};
}

/// A trait for [`ToSocketAddrs`](https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html) that
/// can also be turned into a domain. Used when an SSL client needs to validate the server's
/// certificate.
pub trait ToSocketAddrsDomain: ToSocketAddrs {
    /// Returns the domain, if present
    fn domain(&self) -> Option<&str> {
        None
    }
}

impl ToSocketAddrsDomain for &str {
    fn domain(&self) -> Option<&str> {
        self.splitn(2, ':').next()
    }
}

impl ToSocketAddrsDomain for (&str, u16) {
    fn domain(&self) -> Option<&str> {
        self.0.domain()
    }
}

/// Instance of an Electrum client
///
/// A `Client` maintains a constant connection with an Electrum server and exposes methods to
/// interact with it. It can also subscribe and receive notifictations from the server about new
/// blocks or activity on a specific *scriptPubKey*.
///
/// The `Client` is modeled in such a way that allows the external caller to have full control over
/// its functionality: no threads or tasks are spawned internally to monitor the state of the
/// connection. This allows the caller to control its behavior through some *polling* functions,
/// and ultimately makes the library more lightweight and easier to embed into existing
/// projects.
///
/// More transport methods can be used by manually creating an instance of this struct with an
/// arbitray `S` type.
#[derive(Debug)]
pub struct Client<S>
where
    S: Read + Write,
{
    stream: ClonableStream<S>,
    buf_reader: BufReader<ClonableStream<S>>,

    headers: VecDeque<HeaderNotification>,
    script_notifications: BTreeMap<ScriptHash, VecDeque<ScriptStatus>>,

    #[cfg(feature = "debug-calls")]
    calls: usize,
}

impl<S> From<S> for Client<S>
where
    S: Read + Write,
{
    fn from(stream: S) -> Self {
        let stream: ClonableStream<_> = stream.into();

        Self {
            buf_reader: BufReader::new(stream.clone()),
            stream,
            headers: VecDeque::new(),
            script_notifications: BTreeMap::new(),

            #[cfg(feature = "debug-calls")]
            calls: 0,
        }
    }
}

/// Transport type used to establish a plaintext TCP connection with the server
pub type ElectrumPlaintextStream = TcpStream;
impl Client<ElectrumPlaintextStream> {
    /// Creates a new plaintext client and tries to connect to `socket_addr`.
    pub fn new<A: ToSocketAddrs>(socket_addr: A) -> Result<Self, Error> {
        let stream = TcpStream::connect(socket_addr)?;

        Ok(stream.into())
    }
}

#[cfg(feature = "use-openssl")]
/// Transport type used to establish an OpenSSL TLS encrypted/authenticated connection with the server
pub type ElectrumSslStream = SslStream<TcpStream>;
#[cfg(feature = "use-openssl")]
impl Client<ElectrumSslStream> {
    /// Creates a new SSL client and tries to connect to `socket_addr`. Optionally, if
    /// `validate_domain` is `true`, validate the server's certificate.
    pub fn new_ssl<A: ToSocketAddrsDomain>(
        socket_addr: A,
        validate_domain: bool,
    ) -> Result<Self, Error> {
        let mut builder =
            SslConnector::builder(SslMethod::tls()).map_err(Error::InvalidSslMethod)?;
        // TODO: support for certificate pinning
        if validate_domain {
            socket_addr.domain().ok_or(Error::MissingDomain)?;
        } else {
            builder.set_verify(SslVerifyMode::NONE);
        }
        let connector = builder.build();

        let domain = socket_addr.domain().unwrap_or("NONE").to_string();
        let stream = TcpStream::connect(socket_addr)?;
        let stream = connector
            .connect(&domain, stream)
            .map_err(Error::SslHandshakeError)?;

        Ok(stream.into())
    }
}

#[cfg(all(
    any(feature = "default", feature = "use-rustls"),
    not(feature = "use-openssl")
))]
mod danger {
    use rustls;
    use webpki;

    pub struct NoCertificateVerification {}

    impl rustls::ServerCertVerifier for NoCertificateVerification {
        fn verify_server_cert(
            &self,
            _roots: &rustls::RootCertStore,
            _presented_certs: &[rustls::Certificate],
            _dns_name: webpki::DNSNameRef<'_>,
            _ocsp: &[u8],
        ) -> Result<rustls::ServerCertVerified, rustls::TLSError> {
            Ok(rustls::ServerCertVerified::assertion())
        }
    }
}

#[cfg(all(
    any(feature = "default", feature = "use-rustls"),
    not(feature = "use-openssl")
))]
/// Transport type used to establish a Rustls TLS encrypted/authenticated connection with the server
pub type ElectrumSslStream = StreamOwned<ClientSession, TcpStream>;
#[cfg(all(
    any(feature = "default", feature = "use-rustls"),
    not(feature = "use-openssl")
))]
impl Client<ElectrumSslStream> {
    /// Creates a new SSL client and tries to connect to `socket_addr`. Optionally, if
    /// `validate_domain` is `true`, validate the server's certificate.
    pub fn new_ssl<A: ToSocketAddrsDomain>(
        socket_addr: A,
        validate_domain: bool,
    ) -> Result<Self, Error> {
        let mut config = ClientConfig::new();
        if validate_domain {
            socket_addr.domain().ok_or(Error::MissingDomain)?;

            // TODO: cert pinning
            config
                .root_store
                .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
        } else {
            config
                .dangerous()
                .set_certificate_verifier(std::sync::Arc::new(danger::NoCertificateVerification {}))
        }

        let domain = socket_addr.domain().unwrap_or("NONE").to_string();
        let tcp_stream = TcpStream::connect(socket_addr)?;
        let session = ClientSession::new(
            &std::sync::Arc::new(config),
            webpki::DNSNameRef::try_from_ascii_str(&domain)
                .map_err(|_| Error::InvalidDNSNameError(domain.clone()))?,
        );
        let stream = StreamOwned::new(session, tcp_stream);

        Ok(stream.into())
    }
}

#[cfg(any(feature = "default", feature = "proxy"))]
/// Transport type used to establish a connection to a server through a socks proxy
pub type ElectrumProxyStream = Socks5Stream;
#[cfg(any(feature = "default", feature = "proxy"))]
impl Client<ElectrumProxyStream> {
    /// Creates a new socks client and tries to connect to `target_addr` using `proxy_addr` as an
    /// unauthenticated socks proxy server. The DNS resolution of `target_addr`, if required, is done
    /// through the proxy. This allows to specify, for instance, `.onion` addresses.
    pub fn new_proxy<A: ToSocketAddrs, T: ToTargetAddr>(
        target_addr: T,
        proxy_addr: A,
    ) -> Result<Self, Error> {
        // TODO: support proxy credentials
        let stream = Socks5Stream::connect(proxy_addr, target_addr)?;

        Ok(stream.into())
    }
}

impl<S: Read + Write> Client<S> {
    fn call(&mut self, req: Request) -> Result<serde_json::Value, Error> {
        let mut raw = serde_json::to_vec(&req)?;
        trace!("==> {}", String::from_utf8_lossy(&raw));

        raw.extend_from_slice(b"\n");
        self.stream.write_all(&raw)?;
        self.stream.flush()?;

        self.increment_calls();

        let mut resp = loop {
            let raw = self.recv()?;
            let mut resp: serde_json::Value = serde_json::from_slice(&raw)?;

            match resp["method"].take().as_str() {
                Some(ref method) if method == &req.method => break resp,
                Some(ref method) => self.handle_notification(method, resp["result"].take())?,
                _ => break resp,
            };
        };

        if let Some(err) = resp.get("error") {
            return Err(Error::Protocol(err.clone()));
        }

        Ok(resp["result"].take())
    }

    /// Execute a queue of calls stored in a [`Batch`](../batch/struct.Batch.html) struct. Returns
    /// `Ok()` **only if** all of the calls are successful. The order of the JSON `Value`s returned
    /// reflects the order in which the calls were made on the `Batch` struct.
    pub fn batch_call(&mut self, batch: Batch) -> Result<Vec<serde_json::Value>, Error> {
        let mut id_map = BTreeMap::new();
        let mut raw = Vec::new();
        let mut answer = Vec::new();

        for (i, (method, params)) in batch.into_iter().enumerate() {
            let req = Request::new_id(i, &method, params);

            raw.append(&mut serde_json::to_vec(&req)?);
            raw.extend_from_slice(b"\n");

            id_map.insert(req.id, method);
        }

        trace!("==> {}", String::from_utf8_lossy(&raw));

        self.stream.write_all(&raw)?;
        self.stream.flush()?;

        self.increment_calls();

        while answer.len() < id_map.len() {
            let raw = self.recv()?;
            let mut resp: serde_json::Value = serde_json::from_slice(&raw)?;

            let resp = match resp["id"].as_u64() {
                Some(id) if id_map.contains_key(&(id as usize)) => resp,
                _ => {
                    self.handle_notification(
                        resp["method"].take().as_str().unwrap_or(""),
                        resp["result"].take(),
                    )?;
                    continue;
                }
            };

            if let Some(err) = resp.get("error") {
                return Err(Error::Protocol(err.clone()));
            }

            answer.push(resp.clone());
        }

        answer.sort_by(|a, b| a["id"].as_u64().partial_cmp(&b["id"].as_u64()).unwrap());

        let answer = answer.into_iter().map(|mut x| x["result"].take()).collect();
        Ok(answer)
    }

    fn recv(&mut self) -> io::Result<Vec<u8>> {
        let mut resp = String::new();
        self.buf_reader.read_line(&mut resp)?;

        trace!("<== {}", resp);

        Ok(resp.as_bytes().to_vec())
    }

    fn handle_notification(
        &mut self,
        method: &str,
        result: serde_json::Value,
    ) -> Result<(), Error> {
        match method {
            "blockchain.headers.subscribe" => {
                self.headers.push_back(serde_json::from_value(result)?)
            }
            "blockchain.scripthash.subscribe" => {
                let unserialized: ScriptNotification = serde_json::from_value(result)?;

                let queue = self
                    .script_notifications
                    .get_mut(&unserialized.scripthash)
                    .ok_or_else(|| Error::NotSubscribed(unserialized.scripthash))?;

                queue.push_back(unserialized.status);
            }
            _ => info!("received unknown notification for method `{}`", method),
        }

        Ok(())
    }

    /// Tries to read from the read buffer if any notifications were received since the last call
    /// or `poll`, and processes them
    pub fn poll(&mut self) -> Result<(), Error> {
        // try to pull data from the stream
        self.buf_reader.fill_buf()?;

        while !self.buf_reader.buffer().is_empty() {
            let raw = self.recv()?;
            let mut resp: serde_json::Value = serde_json::from_slice(&raw)?;

            match resp["method"].take().as_str() {
                Some(ref method) => self.handle_notification(method, resp["params"].take())?,
                _ => continue,
            }
        }

        Ok(())
    }

    /// Subscribes to notifications for new block headers, by sending a `blockchain.headers.subscribe` call.
    pub fn block_headers_subscribe(&mut self) -> Result<HeaderNotification, Error> {
        let req = Request::new("blockchain.headers.subscribe", vec![]);
        let value = self.call(req)?;

        Ok(serde_json::from_value(value)?)
    }

    /// Tries to pop one queued notification for a new block header that we might have received.
    /// Returns `None` if there are no items in the queue.
    pub fn block_headers_poll(&mut self) -> Result<Option<HeaderNotification>, Error> {
        self.poll()?;

        Ok(self.headers.pop_front())
    }

    /// Gets the block header for height `height`.
    pub fn block_header(&mut self, height: usize) -> Result<block::BlockHeader, Error> {
        let req = Request::new("blockchain.block.header", vec![Param::Usize(height)]);
        let result = self.call(req)?;

        Ok(deserialize(&Vec::<u8>::from_hex(
            result
                .as_str()
                .ok_or_else(|| Error::InvalidResponse(result.clone()))?,
        )?)?)
    }

    /// Tries to fetch `count` block headers starting from `start_height`.
    pub fn block_headers(
        &mut self,
        start_height: usize,
        count: usize,
    ) -> Result<GetHeadersRes, Error> {
        let req = Request::new(
            "blockchain.block.headers",
            vec![Param::Usize(start_height), Param::Usize(count)],
        );
        let result = self.call(req)?;

        let mut deserialized: GetHeadersRes = serde_json::from_value(result)?;
        for i in 0..deserialized.count {
            let (start, end) = (i * 80, (i + 1) * 80);
            deserialized
                .headers
                .push(deserialize(&deserialized.raw_headers[start..end])?);
        }
        deserialized.raw_headers.clear();

        Ok(deserialized)
    }

    /// Estimates the fee required in **Satoshis per kilobyte** to confirm a transaction in `number` blocks.
    pub fn estimate_fee(&mut self, number: usize) -> Result<f64, Error> {
        let req = Request::new("blockchain.estimatefee", vec![Param::Usize(number)]);
        let result = self.call(req)?;

        result
            .as_f64()
            .ok_or_else(|| Error::InvalidResponse(result.clone()))
    }

    /// Returns the minimum accepted fee by the server's node in **Bitcoin, not Satoshi**.
    pub fn relay_fee(&mut self) -> Result<f64, Error> {
        let req = Request::new("blockchain.relayfee", vec![]);
        let result = self.call(req)?;

        result
            .as_f64()
            .ok_or_else(|| Error::InvalidResponse(result.clone()))
    }

    /// Subscribes to notifications for activity on a specific *scriptPubKey*.
    ///
    /// Returns a [`ScriptStatus`](../types/type.ScriptStatus.html) when successful that represents
    /// the current status for the requested script.
    ///
    /// Returns [`Error::AlreadySubscribed`](../types/enum.Error.html#variant.AlreadySubscribed) if
    /// already subscribed to the same script.
    pub fn script_subscribe(&mut self, script: &Script) -> Result<ScriptStatus, Error> {
        let script_hash = script.to_electrum_scripthash();

        if self.script_notifications.contains_key(&script_hash) {
            return Err(Error::AlreadySubscribed(script_hash));
        }

        self.script_notifications
            .insert(script_hash.clone(), VecDeque::new());

        let req = Request::new(
            "blockchain.scripthash.subscribe",
            vec![Param::String(script_hash.to_hex())],
        );
        let value = self.call(req)?;

        Ok(serde_json::from_value(value)?)
    }

    /// Subscribes to notifications for activity on a specific *scriptPubKey*.
    ///
    /// Returns a `bool` with the server response when successful.
    ///
    /// Returns [`Error::NotSubscribed`](../types/enum.Error.html#variant.NotSubscribed) if
    /// not subscribed to the script.
    pub fn script_unsubscribe(&mut self, script: &Script) -> Result<bool, Error> {
        let script_hash = script.to_electrum_scripthash();

        if !self.script_notifications.contains_key(&script_hash) {
            return Err(Error::NotSubscribed(script_hash));
        }

        let req = Request::new(
            "blockchain.scripthash.unsubscribe",
            vec![Param::String(script_hash.to_hex())],
        );
        let value = self.call(req)?;
        let answer = serde_json::from_value(value)?;

        self.script_notifications.remove(&script_hash);

        Ok(answer)
    }

    /// Tries to pop one queued notification for a the requested script. Returns `None` if there are no items in the queue.
    pub fn script_poll(&mut self, script: &Script) -> Result<Option<ScriptStatus>, Error> {
        self.poll()?;

        let script_hash = script.to_electrum_scripthash();

        match self.script_notifications.get_mut(&script_hash) {
            None => Err(Error::NotSubscribed(script_hash)),
            Some(queue) => Ok(queue.pop_front()),
        }
    }

    /// Returns the balance for a *scriptPubKey*
    pub fn script_get_balance(&mut self, script: &Script) -> Result<GetBalanceRes, Error> {
        let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
        let req = Request::new("blockchain.scripthash.get_balance", params);
        let result = self.call(req)?;

        Ok(serde_json::from_value(result)?)
    }
    /// Batch version of [`script_get_balance`](#method.script_get_balance).
    ///
    /// Takes a list of scripts and returns a list of balance responses.
    pub fn batch_script_get_balance<'s, I>(
        &mut self,
        scripts: I,
    ) -> Result<Vec<GetBalanceRes>, Error>
    where
        I: IntoIterator<Item = &'s Script>,
    {
        impl_batch_call!(self, scripts, script_get_balance)
    }

    /// Returns the history for a *scriptPubKey*
    pub fn script_get_history(&mut self, script: &Script) -> Result<Vec<GetHistoryRes>, Error> {
        let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
        let req = Request::new("blockchain.scripthash.get_history", params);
        let result = self.call(req)?;

        Ok(serde_json::from_value(result)?)
    }
    /// Batch version of [`script_get_history`](#method.script_get_history).
    ///
    /// Takes a list of scripts and returns a list of history responses.
    pub fn batch_script_get_history<'s, I>(
        &mut self,
        scripts: I,
    ) -> Result<Vec<Vec<GetHistoryRes>>, Error>
    where
        I: IntoIterator<Item = &'s Script>,
    {
        impl_batch_call!(self, scripts, script_get_history)
    }

    /// Returns the list of unspent outputs for a *scriptPubKey*
    pub fn script_list_unspent(&mut self, script: &Script) -> Result<Vec<ListUnspentRes>, Error> {
        let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
        let req = Request::new("blockchain.scripthash.listunspent", params);
        let result = self.call(req)?;

        Ok(serde_json::from_value(result)?)
    }
    /// Batch version of [`script_list_unspent`](#method.script_list_unspent).
    ///
    /// Takes a list of scripts and returns a list of a list of utxos.
    pub fn batch_script_list_unspent<'s, I>(
        &mut self,
        scripts: I,
    ) -> Result<Vec<Vec<ListUnspentRes>>, Error>
    where
        I: IntoIterator<Item = &'s Script>,
    {
        impl_batch_call!(self, scripts, script_list_unspent)
    }

    /// Gets the raw transaction with `txid`. Returns an error if not found.
    pub fn transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
        let params = vec![Param::String(txid.to_hex())];
        let req = Request::new("blockchain.transaction.get", params);
        let result = self.call(req)?;

        Ok(deserialize(&Vec::<u8>::from_hex(
            result
                .as_str()
                .ok_or_else(|| Error::InvalidResponse(result.clone()))?,
        )?)?)
    }
    /// Batch version of [`transaction_get`](#method.transaction_get).
    ///
    /// Takes a list of `txids` and returns a list of transactions.
    pub fn batch_transaction_get<'t, I>(
        &mut self,
        txids: Vec<&Txid>,
    ) -> Result<Vec<Transaction>, Error>
    where
        I: IntoIterator<Item = &'t Txid>,
    {
        impl_batch_call!(self, txids, transaction_get)
    }

    /// Broadcasts a transaction to the network.
    pub fn transaction_broadcast(&mut self, tx: &Transaction) -> Result<Txid, Error> {
        let buffer: Vec<u8> = serialize(tx);
        let params = vec![Param::String(buffer.to_hex())];
        let req = Request::new("blockchain.transaction.broadcast", params);
        let result = self.call(req)?;

        Ok(serde_json::from_value(result)?)
    }

    /// Returns the merkle path for the transaction `txid` confirmed in the block at `height`.
    pub fn transaction_get_merkle(
        &mut self,
        txid: &Txid,
        height: usize,
    ) -> Result<GetMerkleRes, Error> {
        let params = vec![Param::String(txid.to_hex()), Param::Usize(height)];
        let req = Request::new("blockchain.transaction.get_merkle", params);
        let result = self.call(req)?;

        Ok(serde_json::from_value(result)?)
    }

    /// Returns the capabilities of the server.
    pub fn server_features(&mut self) -> Result<ServerFeaturesRes, Error> {
        let req = Request::new("server.features", vec![]);
        let result = self.call(req)?;

        Ok(serde_json::from_value(result)?)
    }

    #[cfg(feature = "debug-calls")]
    /// Returns the number of network calls made since the creation of the client.
    pub fn calls_made(&self) -> usize {
        self.calls
    }

    #[inline]
    #[cfg(feature = "debug-calls")]
    fn increment_calls(&mut self) {
        self.calls += 1;
    }

    #[inline]
    #[cfg(not(feature = "debug-calls"))]
    fn increment_calls(&self) {}
}

#[cfg(test)]
mod test {
    use std::fs::File;
    use std::io::Read;
    use test_stream::TestStream;

    use client::Client;

    impl Client<TestStream> {
        pub fn new_test(file: File) -> Self {
            TestStream::new(file).into()
        }
    }

    macro_rules! impl_test_prelude {
        ( $testcase:expr ) => {{
            let data_in = File::open(format!("./test_data/{}.in", $testcase)).unwrap();
            Client::new_test(data_in)
        }};
    }

    macro_rules! impl_test_conclusion {
        ( $testcase:expr, $stream:expr ) => {
            let mut data_out = File::open(format!("./test_data/{}.out", $testcase)).unwrap();
            let mut buffer = Vec::new();
            data_out.read_to_end(&mut buffer).unwrap();
            let stream_buffer = $stream.stream().lock().unwrap().buffer.clone();

            assert_eq!(
                stream_buffer,
                buffer,
                "Expecting `{}`, got `{}`",
                String::from_utf8_lossy(&buffer.to_vec()),
                String::from_utf8_lossy(&stream_buffer)
            );
        };
    }

    #[test]
    fn test_server_features_simple() {
        let test_case = "server_features_simple";
        let mut client = impl_test_prelude!(test_case);

        let resp = client.server_features().unwrap();
        assert_eq!(resp.server_version, "ElectrumX 1.0.17");
        assert_eq!(
            resp.genesis_hash,
            [
                0x00, 0x00, 0x00, 0x00, 0x09, 0x33, 0xEA, 0x01, 0xAD, 0x0E, 0xE9, 0x84, 0x20, 0x97,
                0x79, 0xBA, 0xAE, 0xC3, 0xCE, 0xD9, 0x0F, 0xA3, 0xF4, 0x08, 0x71, 0x95, 0x26, 0xF8,
                0xD7, 0x7F, 0x49, 0x43
            ]
        );
        assert_eq!(resp.protocol_min, "1.0");
        assert_eq!(resp.protocol_max, "1.0");
        assert_eq!(resp.hash_function, Some("sha256".into()));
        assert_eq!(resp.pruning, None);

        impl_test_conclusion!(test_case, client.stream);
    }
    #[test]
    fn test_relay_fee() {
        let test_case = "relay_fee";
        let mut client = impl_test_prelude!(test_case);

        let resp = client.relay_fee().unwrap();
        assert_eq!(resp, 123.4);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_estimate_fee() {
        let test_case = "estimate_fee";
        let mut client = impl_test_prelude!(test_case);

        let resp = client.estimate_fee(10).unwrap();
        assert_eq!(resp, 10.0);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_block_header() {
        let test_case = "block_header";
        let mut client = impl_test_prelude!(test_case);

        let resp = client.block_header(500).unwrap();
        assert_eq!(resp.version, 536870912);
        assert_eq!(resp.time, 1578166214);
        assert_eq!(resp.nonce, 0);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_block_headers() {
        let test_case = "block_headers";
        let mut client = impl_test_prelude!(test_case);

        let resp = client.block_headers(100, 4).unwrap();
        assert_eq!(resp.count, 4);
        assert_eq!(resp.max, 2016);
        assert_eq!(resp.headers.len(), 4);

        assert_eq!(resp.headers[0].time, 1563694949);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_script_get_balance() {
        use std::str::FromStr;

        let test_case = "script_get_balance";
        let mut client = impl_test_prelude!(test_case);

        let addr = bitcoin::Address::from_str("2N1xJCxBUXTDs6y8Sydz3axhAiXrrQwcosi").unwrap();
        let resp = client.script_get_balance(&addr.script_pubkey()).unwrap();
        assert_eq!(resp.confirmed, 0);
        assert_eq!(resp.unconfirmed, 130000000);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_script_get_history() {
        use std::str::FromStr;

        use bitcoin::hashes::hex::FromHex;
        use bitcoin::Txid;

        let test_case = "script_get_history";
        let mut client = impl_test_prelude!(test_case);

        let addr = bitcoin::Address::from_str("2N1xJCxBUXTDs6y8Sydz3axhAiXrrQwcosi").unwrap();
        let resp = client.script_get_history(&addr.script_pubkey()).unwrap();
        assert_eq!(resp.len(), 2);
        assert_eq!(
            resp[0].tx_hash,
            Txid::from_hex("a1aa2b52fb79641f918d44a27f51781c3c0c49f7ee0e4b14dbb37c722853f046")
                .unwrap()
        );

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_script_list_unspent() {
        use std::str::FromStr;

        use bitcoin::hashes::hex::FromHex;
        use bitcoin::Txid;

        let test_case = "script_list_unspent";
        let mut client = impl_test_prelude!(test_case);

        let addr = bitcoin::Address::from_str("2N1xJCxBUXTDs6y8Sydz3axhAiXrrQwcosi").unwrap();
        let resp = client.script_list_unspent(&addr.script_pubkey()).unwrap();
        assert_eq!(resp.len(), 2);
        assert_eq!(resp[0].value, 30000000);
        assert_eq!(resp[0].height, 0);
        assert_eq!(resp[0].tx_pos, 1);
        assert_eq!(
            resp[0].tx_hash,
            Txid::from_hex("a1aa2b52fb79641f918d44a27f51781c3c0c49f7ee0e4b14dbb37c722853f046")
                .unwrap()
        );

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_batch_script_list_unspent() {
        use std::str::FromStr;

        let test_case = "batch_script_list_unspent";
        let mut client = impl_test_prelude!(test_case);

        let script_1 = bitcoin::Address::from_str("2N1xJCxBUXTDs6y8Sydz3axhAiXrrQwcosi")
            .unwrap()
            .script_pubkey();
        let script_2 = bitcoin::Address::from_str("2MyEi7dbTfQxo1M4hJaAzA2tgEJFQhYv5Au")
            .unwrap()
            .script_pubkey();

        let resp = client
            .batch_script_list_unspent(vec![&script_1, &script_2])
            .unwrap();
        assert_eq!(resp.len(), 2);
        assert_eq!(resp[0].len(), 2);
        assert_eq!(resp[1].len(), 1);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_transaction_get() {
        use bitcoin::hashes::hex::FromHex;
        use bitcoin::Txid;

        let test_case = "transaction_get";
        let mut client = impl_test_prelude!(test_case);

        let resp = client
            .transaction_get(
                &Txid::from_hex("a1aa2b52fb79641f918d44a27f51781c3c0c49f7ee0e4b14dbb37c722853f046")
                    .unwrap(),
            )
            .unwrap();
        assert_eq!(resp.version, 2);
        assert_eq!(resp.lock_time, 1376);

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_transaction_broadcast() {
        use bitcoin::consensus::deserialize;
        use bitcoin::hashes::hex::FromHex;
        use bitcoin::Txid;

        let test_case = "transaction_broadcast";
        let mut client = impl_test_prelude!(test_case);

        let buf = Vec::<u8>::from_hex("02000000000101f6cd5873d669cc2de550453623d9d10ed5b5ba906d81160ee3ab853ebcfffa0c0100000000feffffff02e22f82000000000017a914e229870f3af1b1a3aefc3452a4d2939b443e6eba8780c3c9010000000017a9145f859501ff79211aeb972633b782743dd3b31dab8702473044022046ff3b0618107e08bd25fb753e31542b8c23575d7e9faf43dd17f59727cfb9c902200a4f3837105808d810de01fcd63fb18e66a69026090dc72b66840d41e55c6bf3012103e531113bbca998f8d164235e3395db336d3ba03552d1bfaa83fd7cffe6e5c6c960050000").unwrap();
        let tx: bitcoin::Transaction = deserialize(&buf).unwrap();

        let resp = client.transaction_broadcast(&tx).unwrap();
        assert_eq!(
            resp,
            Txid::from_hex("a1aa2b52fb79641f918d44a27f51781c3c0c49f7ee0e4b14dbb37c722853f046")
                .unwrap()
        );

        impl_test_conclusion!(test_case, client.stream);
    }

    #[test]
    fn test_transaction_get_merkle() {
        use bitcoin::hashes::hex::FromHex;
        use bitcoin::Txid;

        let test_case = "transaction_get_merkle";
        let mut client = impl_test_prelude!(test_case);

        let resp = client
            .transaction_get_merkle(
                &Txid::from_hex("2d64851151550e8c4d337f335ee28874401d55b358a66f1bafab2c3e9f48773d")
                    .unwrap(),
                1234,
            )
            .unwrap();
        assert_eq!(resp.block_height, 450538);
        assert_eq!(resp.pos, 710);
        assert_eq!(resp.merkle.len(), 11);
        assert_eq!(
            resp.merkle[0],
            [
                0x71, 0x3D, 0x6C, 0x7E, 0x6C, 0xE7, 0xBB, 0xEA, 0x70, 0x8D, 0x61, 0x16, 0x22, 0x31,
                0xEA, 0xA8, 0xEC, 0xB3, 0x1C, 0x4C, 0x5D, 0xD8, 0x4F, 0x81, 0xC2, 0x04, 0x09, 0xA9,
                0x00, 0x69, 0xCB, 0x24
            ]
        );

        impl_test_conclusion!(test_case, client.stream);
    }
}