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
use crate::{
electrum::{
request::Request,
response::{
ErrorResponse, HistoryResult, Response, SHGetHistoryResponse, SHNotification,
SHSubscribeResponse, TxBroadcastResponse, TxGetResponse, TxGetResult,
},
types::ScriptHash,
},
raw_client::{self, Client as RawClient},
};
use bwk_backoff::Backoff;
use hex_conservative::FromHex;
use miniscript::bitcoin::{
consensus::{self, encode::serialize_hex, Decodable},
OutPoint, Script, ScriptBuf, Transaction, TxOut, Txid,
};
use std::{
collections::{BTreeMap, HashMap},
fmt::{Debug, Display},
sync::mpsc,
thread::{self},
time::Duration,
};
#[derive(Debug, Clone)]
pub enum Error {
Electrum(String),
TxParsing,
WrongResponse,
WrongOutPoint,
TxDoesNotExists,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Electrum(e) => write!(f, "{e:?}"),
Error::TxParsing => write!(f, "Fail to parse the transaction"),
Error::WrongResponse => write!(f, "Wrong response from electrum server"),
Error::WrongOutPoint => write!(f, "Requested outpoint did not exists"),
Error::TxDoesNotExists => write!(f, "Requested transaction did not exists"),
}
}
}
impl From<raw_client::Error> for Error {
fn from(value: raw_client::Error) -> Self {
Error::Electrum(format!("{value:?}"))
}
}
#[derive(Debug, Clone, Copy)]
pub enum CoinStatus {
Unconfirmed,
Confirmed,
Spend,
}
pub fn short_hash(s: &ScriptBuf) -> String {
let s = ScriptHash::new(s).to_string();
short_string(s)
}
pub fn short_string(s: String) -> String {
let head = 4;
let tail = 4;
if s.len() <= head + tail + 2 {
// No need to truncate if string is short
return s.to_string();
}
format!("{}..{}", &s[..head], &s[s.len() - tail..])
}
#[derive(Clone)]
pub enum CoinRequest {
Subscribe(Vec<ScriptBuf>),
History(Vec<ScriptBuf>),
Txs(Vec<Txid>),
Stop,
}
impl Debug for CoinRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Subscribe(vec) => {
let hashes: Vec<_> = vec.iter().map(short_hash).collect();
f.debug_tuple("Subscribe").field(&hashes).finish()
}
Self::History(vec) => {
let hashes: Vec<_> = vec.iter().map(short_hash).collect();
f.debug_tuple("History").field(&hashes).finish()
}
Self::Txs(arg0) => f.debug_tuple("Txs").field(arg0).finish(),
Self::Stop => write!(f, "Stop"),
}
}
}
#[derive(Clone)]
pub enum CoinResponse {
Status(BTreeMap<ScriptBuf, Option<String>>),
History(BTreeMap<ScriptBuf, Vec<(Txid, Option<u64> /* height */)>>),
Txs(Vec<Transaction>),
Stopped,
Error(String),
}
impl Debug for CoinResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Txs(vec) => {
let txids: Vec<_> = vec.iter().map(|tx| tx.compute_txid()).collect();
f.debug_tuple("Txs").field(&txids).finish()
}
Self::Status(map) => {
let statuses: Vec<_> = map
.iter()
.map(|(spk, status)| {
format!(
"{} => {:?}",
short_hash(spk),
status.as_ref().map(|st| short_string(st.to_string()))
)
})
.collect();
f.debug_tuple("Status").field(&statuses).finish()
}
Self::History(map) => {
let map: Vec<_> = map
.iter()
.map(|(spk, v)| {
let conf: Vec<_> =
v.iter().filter(|(_, height)| height.is_some()).collect();
format!(
"{} => conf: {}, total: {}",
short_hash(spk),
conf.len(),
v.len()
)
})
.collect();
f.debug_tuple("History").field(&map).finish()
}
Self::Stopped => write!(f, "Stopped"),
Self::Error(e) => write!(f, "Error({})", e),
}
}
}
#[derive(Debug)]
pub struct Client {
inner: RawClient,
index: HashMap<usize, Request>,
last_id: usize,
url: String,
port: u16,
}
impl Clone for Client {
fn clone(&self) -> Self {
Client::new(&self.url, self.port).unwrap()
}
}
impl Client {
/// Create a new electrum client.
///
/// # Arguments
/// * `address` - url/ip of the electrum server as String
/// * `port` - port of the electrum server
pub fn new(address: &str, port: u16) -> Result<Self, Error> {
let ssl = address.starts_with("ssl://");
let address = address.to_string().replace("ssl://", "");
let mut inner = RawClient::new_ssl_maybe(&address, port, ssl);
inner.try_connect()?;
Ok(Client {
inner,
index: HashMap::new(),
last_id: 0,
url: address,
port,
})
}
/// Create a new local electrum client: SSL certificate validation id disabled in
/// order to be used with self-signed certificates.
///
/// # Arguments
/// * `address` - url/ip of the electrum server as String
/// * `port` - port of the electrum server
pub fn new_local(address: &str, port: u16) -> Result<Self, Error> {
let ssl = address.starts_with("ssl://");
let address = address.to_string().replace("ssl://", "");
let mut inner = RawClient::new_ssl_maybe(&address, port, ssl).verif_certificate(false);
inner.try_connect()?;
Ok(Client {
inner,
index: HashMap::new(),
last_id: 0,
url: address,
port,
})
}
/// Generate a new request id
fn id(&mut self) -> usize {
self.last_id = self.last_id.wrapping_add(1);
self.last_id
}
fn register(&mut self, req: &mut Request) -> usize {
let id = self.id();
req.id = id;
self.index.insert(req.id, req.clone());
id
}
pub fn listen<RQ, RS>(self) -> (mpsc::Sender<RQ>, mpsc::Receiver<RS>)
where
RQ: Into<CoinRequest> + Debug + Send + 'static,
RS: From<CoinResponse> + Debug + Send + 'static,
{
let (sender, request) = mpsc::channel();
let (response, receiver) = mpsc::channel();
thread::spawn(move || self.listen_txs(response, request));
(sender, receiver)
}
fn listen_txs<RQ, RS>(mut self, send: mpsc::Sender<RS>, recv: mpsc::Receiver<RQ>)
where
RQ: Into<CoinRequest> + Debug + Send + 'static,
RS: From<CoinResponse> + Debug + Send + 'static,
{
log::debug!("Client::listen_txs()");
let mut req_id_spk_map = BTreeMap::new();
let mut watched_spks_sh = BTreeMap::<usize /* request_id */, ScriptHash>::new();
let mut sh_sbf_map = BTreeMap::<ScriptHash, ScriptBuf>::new();
let mut last_request = None;
fn responses_matches_requests(req: &[Request], resp: &[Response]) -> bool {
req.iter()
.all(|rq| resp.iter().any(|response| response.id() == Some(rq.id)))
}
let mut backoff = Backoff::new_ms(50);
loop {
let mut received = false;
// Handle requests from consumer
// NOTE: some server implementation (electrs for instance) will answer by an empty
// response if it receive a request while it has not yes sent its previous response
// so we need to make sure to not send a request before receiving the previous response
if last_request.is_none() {
match recv.try_recv() {
Ok(rq) => {
log::debug!("Client::listen_txs() recv request: {rq:#?}");
received = true;
let rq: CoinRequest = rq.into();
match rq {
CoinRequest::Subscribe(spks) => {
let mut batch = vec![];
for spk in spks {
let mut sub = Request::subscribe_sh(&spk);
let id = self.register(&mut sub);
log::debug!("Client::listen_txs() subscribe request: {sub:?}");
let sh = ScriptHash::new(&spk);
watched_spks_sh.insert(id, sh);
sh_sbf_map.insert(sh, spk);
batch.push(sub);
}
if !batch.is_empty() {
log::debug!(
"Client::listen_txs() last_request = {:?}",
batch.len()
);
last_request = Some(batch.clone());
let mut retry = 0usize;
while let Err(e) =
self.inner.try_send_batch(batch.iter().collect())
{
retry += 1;
if retry > 10 {
send.send(CoinResponse::Error(format!("electrum::Client::listen_txs() Fail to send bacth request: {:?}", e)).into()).expect("caller dropped");
}
thread::sleep(Duration::from_millis(50));
}
}
}
CoinRequest::History(sbfs) => {
let mut batch = vec![];
for spk in sbfs {
let mut history = Request::sh_get_history(&spk);
let id = self.register(&mut history);
log::debug!(
"Client::listen_txs() history request: {history:?}"
);
req_id_spk_map.insert(id, spk);
batch.push(history);
}
if !batch.is_empty() {
log::debug!(
"Client::listen_txs() last_request = {:?}",
batch.len()
);
last_request = Some(batch.clone());
let mut retry = 0usize;
while let Err(e) =
self.inner.try_send_batch(batch.iter().collect())
{
retry += 1;
if retry > 10 {
send.send(CoinResponse::Error(format!("electrum::Client::listen_txs() Fail to send bacth request: {:?}", e)).into()).expect("caller dropped");
}
thread::sleep(Duration::from_millis(50));
}
}
}
CoinRequest::Txs(txids) => {
let mut batch = vec![];
for txid in txids {
let mut tx = Request::tx_get(txid);
self.register(&mut tx);
log::debug!("Client::listen_txs() txs request: {tx:?}");
batch.push(tx);
}
if !batch.is_empty() {
log::debug!(
"Client::listen_txs() last_request = {:?}",
batch.len()
);
last_request = Some(batch.clone());
let mut retry = 0usize;
while let Err(e) =
self.inner.try_send_batch(batch.iter().collect())
{
retry += 1;
if retry > 10 && send.send(CoinResponse::Error(format!("electrum::Client::listen_txs() Fail to send bacth request: {:?}", e)).into()).is_err() {
// NOTE: caller has dropped the channel
// == Close request
return;
}
thread::sleep(Duration::from_millis(50));
}
}
}
CoinRequest::Stop => {
send.send(CoinResponse::Stopped.into()).unwrap();
return;
}
};
}
Err(e) => {
match e {
mpsc::TryRecvError::Empty => {}
mpsc::TryRecvError::Disconnected => {
// NOTE: caller has dropped the channel
// == Close request
return;
}
}
}
}
}
// Handle responses from electrum server
match self.inner.try_recv(&self.index) {
Ok(Some(r)) => {
log::debug!("Client::listen_txs() from electrum: {r:#?}");
let r_match = if let Some(req) = &last_request {
responses_matches_requests(req, &r)
} else {
false
};
if r_match {
last_request = None;
} else if let Some(last_req) = &last_request {
log::debug!("Client::listen_txs() request not match resend last request");
thread::sleep(Duration::from_millis(100));
self.inner
.try_send_batch(last_req.iter().collect())
.unwrap();
}
received = true;
let mut statuses = BTreeMap::new();
let mut txs = Vec::new();
// let mut txid_to_get = Vec::new();
let mut histories = BTreeMap::new();
for r in r {
match r {
Response::SHSubscribe(SHSubscribeResponse { result: status, id }) => {
let sh = watched_spks_sh.get(&id).expect("already inserted");
let sbf = sh_sbf_map.get(sh).expect("already inserted");
statuses.insert(sbf.clone(), status);
}
Response::SHNotification(SHNotification {
status: (sh, status),
..
}) => {
let sbf = sh_sbf_map.get(&sh).expect("already inserted");
statuses.insert(sbf.clone(), status);
}
Response::SHGetHistory(SHGetHistoryResponse { history, id }) => {
let spk =
req_id_spk_map.get(&id).expect("already inserted").clone();
req_id_spk_map.remove(&id);
let mut spk_hist = vec![];
for tx in history {
let HistoryResult { txid, height, .. } = tx;
let height = if height < 1 {
None
} else {
Some(height as u64)
};
spk_hist.push((txid, height));
}
histories.insert(spk, spk_hist);
}
Response::TxGet(TxGetResponse {
result: TxGetResult::Raw(raw_tx),
..
}) => {
let tx: Transaction =
// TODO: do not unwrap
consensus::encode::deserialize_hex(&raw_tx).unwrap();
txs.push(tx);
}
Response::Error(e) => {
if send
.send(CoinResponse::Error(e.to_string()).into())
.is_err()
{
// NOTE: caller has dropped the channel
// == Close request
return;
}
}
_ => {}
}
}
if !histories.is_empty() {
let rsp = CoinResponse::History(histories);
log::debug!("Client::listen_txs() send response: {rsp:#?}");
send.send(rsp.into()).unwrap();
}
if !statuses.is_empty() {
let rsp = CoinResponse::Status(statuses);
log::debug!("Client::listen_txs() send response: {rsp:#?}");
send.send(rsp.into()).unwrap();
}
// let mut txs = Vec::new();
if !txs.is_empty() {
let rsp = CoinResponse::Txs(txs);
log::debug!("Client::listen_txs() send response: {rsp:#?}");
send.send(rsp.into()).unwrap();
}
}
Ok(None) => {}
Err(e) => {
if send
.send(CoinResponse::Error(e.to_string()).into())
.is_err()
{
// NOTE: caller has dropped the channel
// == Close request
return;
}
}
}
if received {
continue;
}
backoff.snooze();
}
}
/// Try to get a transaction by its txid
///
/// # Errors
///
/// This function will return an error if:
/// - fail to send the request
/// - parsing response fails
/// - the response is not of expected type
/// - the transaction does not exists
pub fn get_tx(&mut self, txid: Txid) -> Result<Transaction, Error> {
let request = Request::tx_get(txid).id(self.id());
self.inner.try_send(&request)?;
let req_id = request.id;
self.index.insert(request.id, request);
let resp = match self.inner.recv(&self.index) {
Ok(r) => r,
Err(e) => {
self.index.remove(&req_id);
return Err(e.into());
}
};
for r in resp {
if let Response::TxGet(TxGetResponse {
id,
result: TxGetResult::Raw(res),
}) = r
{
if req_id == id {
self.index.remove(&req_id);
let raw_tx = match Vec::<u8>::from_hex(&res) {
Ok(raw) => raw,
Err(_) => {
return Err(Error::TxParsing);
}
};
let tx: Result<Transaction, _> =
Decodable::consensus_decode(&mut raw_tx.as_slice());
return tx.map_err(|_| Error::TxParsing);
}
} else if let Response::Error(ErrorResponse { id, .. }) = r {
if req_id == id {
self.index.remove(&req_id);
// NOTE: it's very likely if we receive an error response from the server
// it's because the txid does not match any Transaction, but maybe we can
// do a better handling of the error case (for this we need check if responses
// from all electrum server implementations are consistant).
return Err(Error::TxDoesNotExists);
}
}
}
self.index.remove(&req_id);
Err(Error::WrongResponse)
}
/// Get coins that pay to the given spk and their related transaction.
/// This method will make several calls to the electrum server:
/// - it will first request a list of all transactions txid that have
/// an output paying to the spk.
/// - it will then fetch all txs, store them and extract all the coins
/// that pay to the given spk.
/// - it will return a list of (TxOut, OutPoint) and a map of transactions.
///
/// # Errors
///
/// This function will return an error if:
/// - a call to the electrum server fail
#[allow(clippy::type_complexity)]
pub fn get_coins_at(
&mut self,
script: &Script,
) -> Result<(Vec<(TxOut, OutPoint)>, HashMap<Txid, Transaction>), Error> {
let mut txouts = Vec::new();
let mut transactions = HashMap::new();
let txs = self.get_coins_tx_at(script)?;
for txid in txs {
let tx = self.get_tx(txid)?;
for (i, txout) in tx.output.iter().enumerate() {
if *txout.script_pubkey == *script {
let outpoint = OutPoint {
txid,
vout: i as u32,
};
txouts.push((txout.clone(), outpoint));
}
}
transactions.insert(txid, tx);
}
Ok((txouts, transactions))
}
/// Get a list of txid of all transaction that have an output paying to the
/// given spk
///
/// # Errors
///
/// This function will return an error if:
/// - fail sending the request
/// - receive a wrong response
pub fn get_coins_tx_at(&mut self, script: &Script) -> Result<Vec<Txid>, Error> {
let request = Request::sh_get_history(script).id(self.id());
self.inner.try_send(&request)?;
let req_id = request.id;
self.index.insert(request.id, request);
let resp = match self.inner.recv(&self.index) {
Ok(r) => r,
Err(e) => {
self.index.remove(&req_id);
return Err(e.into());
}
};
for r in resp {
if let Response::SHGetHistory(SHGetHistoryResponse { id, history }) = r {
if req_id == id {
self.index.remove(&req_id);
let history: Vec<_> = history.into_iter().map(|r| r.txid).collect();
return Ok(history);
}
}
}
self.index.remove(&req_id);
Err(Error::WrongResponse)
}
/// Broadcast the given transaction.
///
/// # Errors
///
/// This function will return an error if:
/// - fail to send the request
/// - get a wrong response
pub fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
let raw_tx = serialize_hex(tx);
log::debug!("electrum::Client().broadcast(): {:?}", raw_tx);
let request = Request::tx_broadcast(raw_tx);
self.inner.try_send(&request)?;
let req_id = request.id;
self.index.insert(request.id, request);
let resp = match self.inner.recv(&self.index) {
Ok(r) => r,
Err(e) => {
self.index.remove(&req_id);
return Err(e.into());
}
};
log::debug!(
"electrum::Client().broadcast(): receive response: {:?}",
resp
);
for r in resp {
if let Response::TxBroadcast(TxBroadcastResponse { id, .. }) = r {
if req_id == id {
self.index.remove(&req_id);
return Ok(());
}
}
}
self.index.remove(&req_id);
Err(Error::WrongResponse)
}
/// Returns the URL of the electrum client.
///
/// # Returns
/// A `String` containing the URL of the electrum server.
pub fn url(&self) -> String {
self.url.clone()
}
/// Returns the port of the electrum client.
///
/// # Returns
/// A `u16` containing the port of the electrum server.
pub fn port(&self) -> u16 {
self.port
}
}