forked from Blockstream/gdk
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
1298 lines (1146 loc) · 47.1 KB
/
lib.rs
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mod store;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate gdk_common;
use log::{debug, info, trace, warn};
use serde_json::Value;
pub mod error;
pub mod headers;
pub mod interface;
pub mod pin;
pub mod spv;
use crate::error::Error;
use crate::interface::{ElectrumUrl, WalletCtx};
use crate::store::*;
use bitcoin::hashes::{hex::FromHex, sha256, Hash};
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
use bitcoin::util::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey};
use bitcoin::{BlockHash, Script, Txid};
use electrum_client::GetHistoryRes;
use gdk_common::be::*;
use gdk_common::mnemonic::Mnemonic;
use gdk_common::model::*;
use gdk_common::network::Network;
use gdk_common::password::Password;
use gdk_common::session::Session;
use gdk_common::wally::{
self, asset_blinding_key_from_seed, asset_blinding_key_to_ec_private_key, asset_unblind,
MasterBlindingKey,
};
use elements::confidential::{self, Asset, Nonce};
use gdk_common::{ElementsNetwork, NetworkId};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use std::time::{Duration, Instant};
use crate::headers::bitcoin::HeadersChain;
use crate::headers::liquid::Verifier;
use crate::headers::ChainOrVerifier;
use crate::pin::PinManager;
use crate::spv::SpvCrossValidator;
use aes::Aes256;
use bitcoin::blockdata::constants::DIFFCHANGE_INTERVAL;
use block_modes::block_padding::Pkcs7;
use block_modes::BlockMode;
use block_modes::Cbc;
use electrum_client::{Client, ElectrumApi};
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::sync::{mpsc, Arc, RwLock};
use std::thread::JoinHandle;
const CROSS_VALIDATION_RATE: u8 = 4; // Once every 4 thread loop runs, or roughly 28 seconds
type Aes256Cbc = Cbc<Aes256, Pkcs7>;
pub struct Syncer {
pub store: Store,
pub master_blinding: Option<MasterBlindingKey>,
pub network: Network,
}
pub struct Tipper {
pub store: Store,
pub network: Network,
}
pub struct Headers {
pub store: Store,
pub checker: ChainOrVerifier,
pub cross_validator: Option<SpvCrossValidator>,
}
#[derive(Clone)]
pub struct NativeNotif(
pub Option<(extern "C" fn(*const libc::c_void, *const GDKRUST_json), *const libc::c_void)>,
);
unsafe impl Send for NativeNotif {}
pub struct Closer {
pub senders: Vec<Sender<()>>,
pub handles: Vec<JoinHandle<()>>,
}
impl Closer {
pub fn close(&mut self) -> Result<(), Error> {
while let Some(sender) = self.senders.pop() {
sender.send(())?;
}
while let Some(handle) = self.handles.pop() {
handle.join().expect("Couldn't join on the associated thread");
}
Ok(())
}
}
pub struct ElectrumSession {
pub data_root: String,
pub network: Network,
pub url: ElectrumUrl,
pub wallet: Option<WalletCtx>,
pub notify: NativeNotif,
pub closer: Closer,
pub state: State,
}
#[derive(Debug, PartialEq)]
pub enum State {
Disconnected,
Connected,
Logged,
}
fn notify(notif: NativeNotif, data: Value) {
info!("push notification: {:?}", data);
if let Some((handler, self_context)) = notif.0 {
// TODO check the native pointer is still alive
handler(self_context, GDKRUST_json::new(data));
} else {
warn!("no registered handler to receive notification");
}
}
fn notify_block(notif: NativeNotif, height: u32) {
let data = json!({"block":{"block_height":height},"event":"block"});
notify(notif, data);
}
fn notify_settings(notif: NativeNotif, settings: &Settings) {
let data = json!({"settings":settings,"event":"settings"});
notify(notif, data);
}
fn notify_fee(notif: NativeNotif, fees: &[FeeEstimate]) {
let data = json!({"fees":fees,"event":"fees"});
notify(notif, data);
}
fn determine_electrum_url(
url: &Option<String>,
tls: Option<bool>,
validate_domain: Option<bool>,
) -> Result<ElectrumUrl, Error> {
let url = url.as_ref().ok_or_else(|| Error::Generic("network url is missing".into()))?;
if url == "" {
return Err(Error::Generic("network url is empty".into()));
}
if tls.unwrap_or(false) {
Ok(ElectrumUrl::Tls(url.into(), validate_domain.unwrap_or(false)))
} else {
Ok(ElectrumUrl::Plaintext(url.into()))
}
}
pub fn determine_electrum_url_from_net(network: &Network) -> Result<ElectrumUrl, Error> {
determine_electrum_url(&network.electrum_url, network.tls, network.validate_domain)
}
impl ElectrumSession {
pub fn new_session(network: Network, db_root: &str, url: ElectrumUrl) -> Result<Self, Error> {
Ok(Self::create_session(network, db_root, url))
}
}
impl ElectrumSession {
pub fn create_session(network: Network, db_root: &str, url: ElectrumUrl) -> Self {
Self {
data_root: db_root.to_string(),
network,
url,
wallet: None,
notify: NativeNotif(None),
closer: Closer {
senders: vec![],
handles: vec![],
},
state: State::Disconnected,
}
}
pub fn get_wallet(&self) -> Result<&WalletCtx, Error> {
self.wallet.as_ref().ok_or_else(|| Error::Generic("wallet not initialized".into()))
}
pub fn get_wallet_mut(&mut self) -> Result<&mut WalletCtx, Error> {
self.wallet.as_mut().ok_or_else(|| Error::Generic("wallet not initialized".into()))
}
}
fn try_get_fee_estimates(client: &Client) -> Result<Vec<FeeEstimate>, Error> {
let relay_fee = (client.relay_fee()? * 100_000_000.0) as u64;
let blocks: Vec<usize> = (1..25).collect();
// max is covering a rounding errors in production electrs which sometimes cause a fee
// estimates lower than relay fee
let mut estimates: Vec<FeeEstimate> = client
.batch_estimate_fee(blocks)?
.iter()
.map(|e| FeeEstimate(relay_fee.max((*e * 100_000_000.0) as u64)))
.collect();
estimates.insert(0, FeeEstimate(relay_fee));
Ok(estimates)
}
fn make_txlist_item(tx: &TransactionMeta) -> TxListItem {
let type_ = tx.type_.clone();
let len = tx.hex.len() / 2;
let fee_rate = (tx.fee as f64 / len as f64) as u64;
let addressees = tx
.create_transaction
.as_ref()
.unwrap()
.addressees
.iter()
.map(|e| e.address.clone())
.collect();
TxListItem {
block_height: tx.height.unwrap_or_default(),
created_at: tx.created_at.clone(),
type_,
memo: tx.create_transaction.as_ref().and_then(|c| c.memo.clone()).unwrap_or("".to_string()),
txhash: tx.txid.clone(),
transaction_size: len,
transaction: tx.hex.clone(), // FIXME
satoshi: tx.satoshi.clone(),
rbf_optin: tx.rbf_optin, // TODO: TransactionMeta -> TxListItem rbf_optin
cap_cpfp: false, // TODO: TransactionMeta -> TxListItem cap_cpfp
can_rbf: false, // TODO: TransactionMeta -> TxListItem can_rbf
has_payment_request: false, // TODO: TransactionMeta -> TxListItem has_payment_request
server_signed: false, // TODO: TransactionMeta -> TxListItem server_signed
user_signed: tx.user_signed,
spv_verified: tx.spv_verified.to_string(),
instant: false,
fee: tx.fee,
fee_rate,
addressees, // notice the extra "e" -- its intentional
inputs: vec![], // tx.input.iter().map(format_gdk_input).collect(),
outputs: vec![], //tx.output.iter().map(format_gdk_output).collect(),
transaction_vsize: len, //TODO
transaction_weight: len, //TODO
}
}
impl Session<Error> for ElectrumSession {
// type Value = ElectrumSession;
fn destroy_session(&mut self) -> Result<(), Error> {
self.wallet = None;
Ok(())
}
fn poll_session(&self) -> Result<(), Error> {
Err(Error::Generic("implementme: ElectrumSession poll_session".into()))
}
fn connect(&mut self, net_params: &Value) -> Result<(), Error> {
info!("connect network:{:?} state:{:?}", self.network, self.state);
if self.state == State::Disconnected {
if self.data_root == "" {
self.data_root = net_params["state_dir"]
.as_str()
.map(|x| x.to_string())
.unwrap_or_else(|| "".into());
info!("setting db_root to {:?}", self.data_root);
}
let mnemonic = match self.get_mnemonic() {
Ok(mnemonic) => Some(mnemonic.clone()),
Err(_) => None,
};
match mnemonic {
Some(mnemonic) => self.login(&mnemonic, None).map(|_| ())?,
None => self.state = State::Connected,
}
}
Ok(())
}
fn disconnect(&mut self) -> Result<(), Error> {
info!("disconnect state:{:?}", self.state);
info!("disconnect STATUS block:{:?} tx:{}", self.block_status()?, self.tx_status()?);
if self.state != State::Disconnected {
self.closer.close()?;
self.state = State::Disconnected;
}
Ok(())
}
fn login_with_pin(
&mut self,
pin: String,
details: PinGetDetails,
) -> Result<Vec<Notification>, Error> {
let manager = PinManager::new()?;
let client_key = SecretKey::from_slice(&hex::decode(&details.pin_identifier)?)?;
let server_key = manager.get_pin(pin.as_bytes(), &client_key)?;
let iv = hex::decode(&details.salt)?;
let decipher = Aes256Cbc::new_var(&server_key[..], &iv).unwrap();
let mnemonic = decipher.decrypt_vec(&hex::decode(&details.encrypted_data)?)?;
let mnemonic = std::str::from_utf8(&mnemonic).unwrap().to_string();
let mnemonic = Mnemonic::from(mnemonic);
self.login(&mnemonic, None)
}
fn login(
&mut self,
mnemonic: &Mnemonic,
password: Option<Password>,
) -> Result<Vec<Notification>, Error> {
info!("login {:?} {:?}", self.network, self.state);
if self.state == State::Logged {
return Ok(vec![]);
}
// TODO: passphrase?
let mnem_str = mnemonic.clone().get_mnemonic_str();
let seed = wally::bip39_mnemonic_to_seed(
&mnem_str,
&password.map(|p| p.get_password_str()).unwrap_or_default(),
)
.ok_or(Error::InvalidMnemonic)?;
let secp = Secp256k1::new();
let xprv =
ExtendedPrivKey::new_master(bitcoin::network::constants::Network::Testnet, &seed)?;
// BIP44: m / purpose' / coin_type' / account' / change / address_index
// coin_type = 0 bitcoin, 1 testnet, 1776 liquid bitcoin as defined in https://github.com/satoshilabs/slips/blob/master/slip-0044.md
// slip44 suggest 1 for every testnet, so we are using it also for regtest
let coin_type: u32 = match self.network.id() {
NetworkId::Bitcoin(bitcoin_network) => match bitcoin_network {
bitcoin::Network::Bitcoin => 0,
bitcoin::Network::Testnet => 1,
bitcoin::Network::Regtest => 1,
},
NetworkId::Elements(elements_network) => match elements_network {
ElementsNetwork::Liquid => 1776,
ElementsNetwork::ElementsRegtest => 1,
},
};
// since we use P2WPKH-nested-in-P2SH it is 49 https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
let path_string = format!("m/49'/{}'/0'", coin_type);
info!("Using derivation path {}/0|1/*", path_string);
let path = DerivationPath::from_str(&path_string)?;
let xprv = xprv.derive_priv(&secp, &path)?;
let xpub = ExtendedPubKey::from_private(&secp, &xprv);
let wallet_desc = format!("{}{:?}", xpub, self.network);
let wallet_id = hex::encode(sha256::Hash::hash(wallet_desc.as_bytes()));
let sync_interval = self.network.sync_interval.unwrap_or(7);
let master_blinding = if self.network.liquid {
Some(asset_blinding_key_from_seed(&seed))
} else {
None
};
let mut path: PathBuf = self.data_root.as_str().into();
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
path.push(wallet_id);
info!("Store root path: {:?}", path);
let store = match self.get_wallet() {
Ok(wallet) => wallet.store.clone(),
Err(_) => Arc::new(RwLock::new(StoreMeta::new(
&path,
xpub,
master_blinding.clone(),
self.network.id(),
)?)),
};
let estimates = store.read()?.fee_estimates().clone();
notify_fee(self.notify.clone(), &estimates);
let mut tip_height = store.read()?.cache.tip.0;
notify_block(self.notify.clone(), tip_height);
info!("building client");
if let Ok(fee_client) = self.url.build_client() {
info!("building built end");
let fee_store = store.clone();
thread::spawn(move || {
match try_get_fee_estimates(&fee_client) {
Ok(fee_estimates) => {
fee_store.write().unwrap().cache.fee_estimates = fee_estimates
}
Err(e) => warn!("can't update fee estimates {:?}", e),
};
});
}
if self.network.spv_enabled.unwrap_or(false) {
let checker = match self.network.id() {
NetworkId::Bitcoin(network) => {
let mut path: PathBuf = self.data_root.as_str().into();
path.push(format!("headers_chain_{}", network));
ChainOrVerifier::Chain(HeadersChain::new(path, network)?)
}
NetworkId::Elements(network) => {
let verifier = Verifier::new(network);
ChainOrVerifier::Verifier(verifier)
}
};
let cross_validator = SpvCrossValidator::from_network(&self.network)?;
let mut headers = Headers {
store: store.clone(),
checker,
cross_validator,
};
let headers_url = self.url.clone();
let (close_headers, r) = channel();
self.closer.senders.push(close_headers);
let chunk_size = DIFFCHANGE_INTERVAL as usize;
let headers_handle = thread::spawn(move || {
info!("starting headers thread");
let mut round = 0u8;
'outer: loop {
if wait_or_close(&r, sync_interval) {
info!("closing headers thread");
break;
}
if let Ok(client) = headers_url.build_client() {
loop {
if r.try_recv().is_ok() {
info!("closing headers thread");
break 'outer;
}
match headers.ask(chunk_size, &client) {
Ok(headers_found) => {
if headers_found < chunk_size {
break;
} else {
info!("headers found: {}", headers_found);
}
}
Err(Error::InvalidHeaders) => {
warn!("invalid headers");
// this should handle reorgs and also broke IO writes update
headers.store.write().unwrap().cache.txs_verif.clear();
if let Err(e) = headers.remove(144) {
warn!("failed removing headers: {:?}", e);
break;
}
// XXX clear affected blocks/txs more surgically?
}
Err(e) => {
warn!("error while asking headers {}", e);
thread::sleep(Duration::from_millis(500));
}
}
}
match headers.get_proofs(&client) {
Ok(found) => {
if found > 0 {
info!("found proof {}", found)
}
}
Err(e) => warn!("error in getting proofs {:?}", e),
}
if round % CROSS_VALIDATION_RATE == 0 {
headers.cross_validate();
}
round = round.wrapping_add(1);
}
}
});
self.closer.handles.push(headers_handle);
}
let syncer = Syncer {
store: store.clone(),
master_blinding: master_blinding.clone(),
network: self.network.clone(),
};
let tipper = Tipper {
store: store.clone(),
network: self.network.clone(),
};
if self.wallet.is_none() {
let wallet = WalletCtx::new(
store,
mnemonic.clone(),
self.network.clone(),
xprv,
xpub,
master_blinding,
)?;
self.wallet = Some(wallet);
}
info!("login STATUS block:{:?} tx:{}", self.block_status()?, self.tx_status()?);
let notify_blocks = self.notify.clone();
let (close_tipper, r) = channel();
self.closer.senders.push(close_tipper);
let tipper_url = self.url.clone();
let tipper_handle = thread::spawn(move || {
info!("starting tipper thread");
loop {
if let Ok(client) = tipper_url.build_client() {
match tipper.tip(&client) {
Ok(current_tip) => {
if tip_height != current_tip {
tip_height = current_tip;
info!("tip is {:?}", tip_height);
notify_block(notify_blocks.clone(), tip_height);
}
}
Err(e) => {
warn!("exception in tipper {:?}", e);
}
}
}
if wait_or_close(&r, sync_interval) {
info!("closing tipper thread {:?}", tip_height);
break;
}
}
});
self.closer.handles.push(tipper_handle);
let (close_syncer, r) = channel();
self.closer.senders.push(close_syncer);
let notify_txs = self.notify.clone();
let syncer_url = self.url.clone();
let syncer_handle = thread::spawn(move || {
info!("starting syncer thread");
loop {
match syncer_url.build_client() {
Ok(client) => match syncer.sync(&client) {
Ok(new_txs) => {
if new_txs {
info!("there are new transactions");
let mockup_json = json!({"event":"transaction","transaction":{"subaccounts":[0]}});
notify(notify_txs.clone(), mockup_json);
}
}
Err(e) => warn!("Error during sync, {:?}", e),
},
Err(e) => warn!("Can't build client {:?}", e),
}
if wait_or_close(&r, sync_interval) {
info!("closing syncer thread");
break;
}
}
});
self.closer.handles.push(syncer_handle);
notify_settings(self.notify.clone(), &self.get_settings()?);
self.state = State::Logged;
Ok(vec![])
}
fn get_receive_address(&self, addr_details: &Value) -> Result<AddressPointer, Error> {
debug!("get_receive_address {:?}", addr_details);
let w = self.get_wallet()?;
let a = w.get_address()?;
debug!("get_address {:?}", a);
Ok(a)
}
fn set_pin(&self, details: &PinSetDetails) -> Result<PinGetDetails, Error> {
let manager = PinManager::new()?;
let client_key = SecretKey::new(&mut thread_rng());
let server_key = manager.set_pin(details.pin.as_bytes(), &client_key)?;
let iv = thread_rng().gen::<[u8; 16]>();
let cipher = Aes256Cbc::new_var(&server_key[..], &iv).unwrap();
let encrypted = cipher.encrypt_vec(details.mnemonic.as_bytes());
let result = PinGetDetails {
salt: hex::encode(&iv),
encrypted_data: hex::encode(&encrypted),
pin_identifier: hex::encode(&client_key[..]),
};
Ok(result)
}
fn get_subaccounts(&self) -> Result<Vec<Subaccount>, Error> {
// TODO configurable confs?
let index = 0;
let confs = 0;
let subaccount_fake = self.get_subaccount(index, confs)?;
Ok(vec![subaccount_fake])
}
fn get_subaccount(&self, index: u32, num_confs: u32) -> Result<Subaccount, Error> {
if index != 0 {
return Err(Error::InvalidSubaccount(index));
}
let balance = self.get_balance(num_confs, Some(index))?;
let mut opt = GetTransactionsOpt::default();
opt.count = 1;
let txs = self.get_transactions(&opt)?;
let subaccounts_fake = Subaccount {
type_: "electrum".into(),
name: "Single sig wallet".into(),
has_transactions: !txs.0.is_empty(),
satoshi: balance,
};
Ok(subaccounts_fake)
}
fn get_transactions(&self, opt: &GetTransactionsOpt) -> Result<TxsResult, Error> {
let txs = self.get_wallet()?.list_tx(opt)?.iter().map(make_txlist_item).collect();
Ok(TxsResult(txs))
}
fn get_transaction_details(&self, _txid: &str) -> Result<Value, Error> {
Err(Error::Generic("implementme: ElectrumSession get_transaction_details".into()))
}
fn get_balance(&self, _num_confs: u32, _subaccount: Option<u32>) -> Result<Balances, Error> {
self.get_wallet()?.balance()
}
fn set_transaction_memo(&self, txid: &str, memo: &str) -> Result<(), Error> {
let txid = Txid::from_hex(txid)?;
if memo.len() > 1024 {
return Err(Error::Generic("Too long memo (max 1024)".into()));
}
self.get_wallet()?.store.write()?.insert_memo(txid, memo)?;
Ok(())
}
fn create_transaction(
&mut self,
tx_req: &mut CreateTransaction,
) -> Result<TransactionMeta, Error> {
info!("electrum create_transaction {:#?}", tx_req);
self.get_wallet()?.create_tx(tx_req)
}
fn sign_transaction(&self, create_tx: &TransactionMeta) -> Result<TransactionMeta, Error> {
info!("electrum sign_transaction {:#?}", create_tx);
self.get_wallet()?.sign(create_tx)
}
fn send_transaction(&mut self, tx: &TransactionMeta) -> Result<String, Error> {
info!("electrum send_transaction {:#?}", tx);
let client = self.url.build_client()?;
let tx_bytes = hex::decode(&tx.hex)?;
let txid = client.transaction_broadcast_raw(&tx_bytes)?;
Ok(format!("{}", txid))
}
fn broadcast_transaction(&mut self, tx_hex: &str) -> Result<String, Error> {
let transaction = BETransaction::from_hex(&tx_hex, self.network.id())?;
info!("broadcast_transaction {:#?}", transaction.txid());
let client = self.url.build_client()?;
let hex = hex::decode(tx_hex)?;
let txid = client.transaction_broadcast_raw(&hex)?;
Ok(format!("{}", txid))
}
/// The estimates are returned as an array of 25 elements. Each element is
/// an integer representing the fee estimate expressed as satoshi per 1000
/// bytes. The first element is the minimum relay fee as returned by the
/// network, while the remaining elements are the current estimates to use
/// for a transaction to confirm from 1 to 24 blocks.
fn get_fee_estimates(&mut self) -> Result<Vec<FeeEstimate>, Error> {
let min_fee = match self.network.id() {
NetworkId::Bitcoin(_) => 1000,
NetworkId::Elements(_) => 100,
};
let fee_estimates = try_get_fee_estimates(&self.url.build_client()?)
.unwrap_or_else(|_| vec![FeeEstimate(min_fee); 25]);
self.get_wallet()?.store.write()?.cache.fee_estimates = fee_estimates.clone();
Ok(fee_estimates)
//TODO better implement default
}
fn get_mnemonic(&self) -> Result<&Mnemonic, Error> {
self.get_wallet().map(|wallet| wallet.get_mnemonic())
}
fn get_settings(&self) -> Result<Settings, Error> {
Ok(self.get_wallet()?.get_settings()?)
}
fn change_settings(&mut self, settings: &Settings) -> Result<(), Error> {
self.get_wallet()?.change_settings(settings)?;
notify_settings(self.notify.clone(), settings);
Ok(())
}
fn get_available_currencies(&self) -> Result<Value, Error> {
Ok(json!({ "all": [ "USD" ], "per_exchange": { "BITFINEX": [ "USD" ] } }))
// TODO implement
}
fn refresh_assets(&self, details: &RefreshAssets) -> Result<Value, Error> {
info!("refresh_assets {:?}", details);
if !(details.icons || details.assets) {
return Err(Error::Generic(
"cannot call refresh assets with both icons and assets false".to_string(),
));
}
let mut assets = Value::Null;
let mut icons = Value::Null;
let mut assets_last_modified = String::new();
let mut icons_last_modified = String::new();
if details.refresh {
let (tx_assets, rx_assets) = mpsc::channel();
if details.assets {
let registry_policy = self
.network
.policy_asset
.clone()
.ok_or_else(|| Error::Generic("policy assets not available".into()))?;
let last_modified =
self.get_wallet()?.store.read()?.cache.assets_last_modified.clone();
let base_url = self.network.registry_base_url()?;
thread::spawn(move || {
match call_assets(base_url, registry_policy, last_modified) {
Ok(p) => tx_assets.send(Some(p)),
Err(_) => tx_assets.send(None),
}
});
}
let (tx_icons, rx_icons) = mpsc::channel();
if details.icons {
let last_modified =
self.get_wallet()?.store.read()?.cache.icons_last_modified.clone();
let base_url = self.network.registry_base_url()?;
thread::spawn(move || match call_icons(base_url, last_modified) {
Ok(p) => tx_icons.send(Some(p)),
Err(_) => tx_icons.send(None),
});
}
if details.assets {
if let Ok(Some(assets_recv)) = rx_assets.recv() {
assets = assets_recv.0;
assets_last_modified = assets_recv.1;
}
}
if details.icons {
if let Ok(Some(icons_recv)) = rx_icons.recv() {
icons = icons_recv.0;
icons_last_modified = icons_recv.1;
}
}
let mut store_write = self.get_wallet()?.store.write()?;
if let Value::Object(_) = icons {
store_write.write_asset_icons(&icons)?;
store_write.cache.icons_last_modified = icons_last_modified;
}
if let Value::Object(_) = assets {
store_write.write_asset_registry(&assets)?;
store_write.cache.assets_last_modified = assets_last_modified;
}
}
let mut map = serde_json::Map::new();
if details.assets {
let assets_not_null = match assets {
Value::Object(_) => assets,
_ => self
.get_wallet()?
.store
.read()?
.read_asset_registry()?
.ok_or_else(|| Error::Generic("assets registry not available".into()))?,
};
map.insert("assets".to_string(), assets_not_null);
}
if details.icons {
let icons_not_null = match icons {
Value::Object(_) => icons,
_ => self
.get_wallet()?
.store
.read()?
.read_asset_icons()?
.ok_or_else(|| Error::Generic("icon registry not available".into()))?,
};
map.insert("icons".to_string(), icons_not_null);
}
Ok(Value::Object(map))
}
fn block_status(&self) -> Result<(u32, BlockHash), Error> {
let tip = self.get_wallet()?.get_tip()?;
info!("tip={:?}", tip);
Ok(tip)
}
fn tx_status(&self) -> Result<u64, Error> {
let mut opt = GetTransactionsOpt::default();
opt.count = 100;
let txs = self.get_wallet()?.list_tx(&opt)?;
let mut hasher = DefaultHasher::new();
for tx in txs.iter() {
std::hash::Hash::hash(&tx.txid, &mut hasher);
}
let status = hasher.finish();
info!("txs.len={} status={}", txs.len(), status);
Ok(status)
}
fn get_unspent_outputs(&self, _details: &Value) -> Result<GetUnspentOutputs, Error> {
let mut unspent_outputs: HashMap<String, Vec<UnspentOutput>> = HashMap::new();
for (outpoint, info) in self.get_wallet()?.utxos()?.iter() {
let cur = UnspentOutput::new(outpoint, info);
(*unspent_outputs.entry(info.asset.clone()).or_insert(vec![])).push(cur);
}
Ok(GetUnspentOutputs(unspent_outputs))
}
}
impl ElectrumSession {
pub fn export_cache(&self) -> Result<RawCache, Error> {
self.get_wallet()?.store.read()?.export_cache()
}
}
fn call_icons(base_url: String, last_modified: String) -> Result<(Value, String), Error> {
// TODO gzip encoding
let url = format!("{}/{}", base_url, "icons.json");
info!("START call_icons {}", &url);
let icons_response = ureq::get(&url)
.timeout_connect(15_000)
.timeout_read(15_000)
.set("If-Modified-Since", &last_modified)
.call();
let status = icons_response.status();
info!("call_icons {} returns {}", &url, status);
let last_modified = icons_response.header("Last-Modified").unwrap_or_default().to_string();
let value = icons_response.into_json()?;
info!("END call_icons {} {}", &url, status);
Ok((value, last_modified))
}
fn call_assets(
base_url: String,
registry_policy: String,
last_modified: String,
) -> Result<(Value, String), Error> {
// TODO add gzip encoding
let url = format!("{}/{}", base_url, "index.json");
info!("START call_assets {}", &url);
let assets_response = ureq::get(&url)
.timeout_connect(15_000)
.timeout_read(15_000)
.set("If-Modified-Since", &last_modified)
.call();
let status = assets_response.status();
info!("call_assets {} returns {}", url, status);
let last_modified = assets_response.header("Last-Modified").unwrap_or_default().to_string();
let mut assets = assets_response.into_json()?;
assets[registry_policy] =
json!({"asset_id": ®istry_policy, "name": "Liquid Bitcoin", "ticker": "L-BTC"});
info!("END call_assets {} {}", &url, status);
Ok((assets, last_modified))
}
impl Tipper {
pub fn tip(&self, client: &Client) -> Result<u32, Error> {
let header = client.block_headers_subscribe_raw()?;
let height = header.height as u32;
let tip_height = self.store.read()?.cache.tip.0;
if height != tip_height {
let hash = BEBlockHeader::deserialize(&header.header, self.network.id())?.block_hash();
info!("saving in store new tip {:?}", (height, hash));
self.store.write()?.cache.tip = (height, hash);
}
Ok(height)
}
}
impl Headers {
pub fn ask(&mut self, chunk_size: usize, client: &Client) -> Result<usize, Error> {
if let ChainOrVerifier::Chain(chain) = &mut self.checker {
info!("asking headers, current height:{} chunk_size:{} ", chain.height(), chunk_size);
let headers = client.block_headers(chain.height() as usize + 1, chunk_size)?.headers;
let len = headers.len();
chain.push(headers)?;
Ok(len)
} else {
// Liquid doesn't need to download the header's chain
Ok(0)
}
}
pub fn get_proofs(&mut self, client: &Client) -> Result<usize, Error> {
let store_read = self.store.read()?;
// find unconfirmed transactions that were previously confirmed and had
// their SPV validation cached, to be cleared below
let remove_proof: Vec<Txid> = store_read
.cache
.heights
.iter()
.filter(|(t, h)| h.is_none() && store_read.cache.txs_verif.get(*t).is_some())
.map(|(t, _)| t.clone())
.collect();
// find confirmed transactions with no SPV validation cache
let needs_proof: Vec<(Txid, u32)> = store_read
.cache
.heights
.iter()
.filter_map(|(t, h_opt)| Some((t, (*h_opt)?)))
.filter(|(t, _)| store_read.cache.txs_verif.get(*t).is_none())
.map(|(t, h)| (t.clone(), h))
.collect();
drop(store_read);
let mut txs_verified = HashMap::new();
for (txid, height) in needs_proof {
let verified = match client.transaction_get_merkle(&txid, height as usize) {
Ok(proof) => match &self.checker {
ChainOrVerifier::Chain(chain) => {
chain.verify_tx_proof(&txid, height, proof).is_ok()
}
ChainOrVerifier::Verifier(verifier) => {
if let Some(BEBlockHeader::Elements(header)) =
self.store.read()?.cache.headers.get(&height)
{
verifier.verify_tx_proof(&txid, proof, &header).is_ok()
} else {
false
}
}
},
Err(e) => {
warn!("failed fetching merkle inclusion proof for {}: {:?}", txid, e);
false
}
};
if verified {
info!("proof for {} verified!", txid);
txs_verified.insert(txid, SPVVerifyResult::Verified);
} else {
warn!("proof for {} not verified!", txid);
txs_verified.insert(txid, SPVVerifyResult::NotVerified);
}
}
let proofs_done = txs_verified.len();
let mut store_write = self.store.write()?;
store_write.cache.txs_verif.extend(txs_verified);
for txid in remove_proof {
store_write.cache.txs_verif.remove(&txid);
}
Ok(proofs_done)
}
pub fn remove(&mut self, headers: u32) -> Result<(), Error> {