-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathlib.rs
2300 lines (2135 loc) · 78.9 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
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
//! # Rust Client for Bitcoin Core API
//!
//! This is a client library for the Bitcoin Core JSON-RPC API.
//!
#![crate_name = "bitcoincore_rpc_json"]
#![crate_type = "rlib"]
#![allow(deprecated)] // Because of `GetPeerInfoResultNetwork::Unroutable`.
pub extern crate bitcoin;
#[allow(unused)]
#[macro_use] // `macro_use` is needed for v1.24.0 compilation.
extern crate serde;
extern crate serde_json;
use std::collections::HashMap;
use bitcoin::address::NetworkUnchecked;
use bitcoin::block::Version;
use bitcoin::consensus::encode;
use bitcoin::hashes::hex::FromHex;
use bitcoin::hashes::sha256;
use bitcoin::{
bip158, bip32, Address, Amount, Network, PrivateKey, PublicKey, Script, ScriptBuf,
SignedAmount, Transaction,
};
use serde::de::Error as SerdeError;
use serde::{Deserialize, Serialize};
use std::fmt;
//TODO(stevenroose) consider using a Time type
/// A module used for serde serialization of bytes in hexadecimal format.
///
/// The module is compatible with the serde attribute.
pub mod serde_hex {
use bitcoin::hex::{DisplayHex, FromHex};
use serde::de::Error;
use serde::{Deserializer, Serializer};
pub fn serialize<S: Serializer>(b: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&b.to_lower_hex_string())
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let hex_str: String = ::serde::Deserialize::deserialize(d)?;
Ok(FromHex::from_hex(&hex_str).map_err(D::Error::custom)?)
}
pub mod opt {
use bitcoin::hex::{DisplayHex, FromHex};
use serde::de::Error;
use serde::{Deserializer, Serializer};
pub fn serialize<S: Serializer>(b: &Option<Vec<u8>>, s: S) -> Result<S::Ok, S::Error> {
match *b {
None => s.serialize_none(),
Some(ref b) => s.serialize_str(&b.to_lower_hex_string()),
}
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Vec<u8>>, D::Error> {
let hex_str: String = ::serde::Deserialize::deserialize(d)?;
Ok(Some(FromHex::from_hex(&hex_str).map_err(D::Error::custom)?))
}
}
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetNetworkInfoResultNetwork {
pub name: String,
pub limited: bool,
pub reachable: bool,
pub proxy: String,
pub proxy_randomize_credentials: bool,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetNetworkInfoResultAddress {
pub address: String,
pub port: usize,
pub score: usize,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetNetworkInfoResult {
pub version: usize,
pub subversion: String,
#[serde(rename = "protocolversion")]
pub protocol_version: usize,
#[serde(rename = "localservices")]
pub local_services: String,
#[serde(rename = "localrelay")]
pub local_relay: bool,
#[serde(rename = "timeoffset")]
pub time_offset: isize,
pub connections: usize,
/// The number of inbound connections
/// Added in Bitcoin Core v0.21
pub connections_in: Option<usize>,
/// The number of outbound connections
/// Added in Bitcoin Core v0.21
pub connections_out: Option<usize>,
#[serde(rename = "networkactive")]
pub network_active: bool,
pub networks: Vec<GetNetworkInfoResultNetwork>,
#[serde(rename = "relayfee", with = "bitcoin::amount::serde::as_btc")]
pub relay_fee: Amount,
#[serde(rename = "incrementalfee", with = "bitcoin::amount::serde::as_btc")]
pub incremental_fee: Amount,
#[serde(rename = "localaddresses")]
pub local_addresses: Vec<GetNetworkInfoResultAddress>,
pub warnings: StringOrStringArray,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AddMultiSigAddressResult {
pub address: Address<NetworkUnchecked>,
pub redeem_script: ScriptBuf,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct LoadWalletResult {
pub name: String,
pub warning: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct UnloadWalletResult {
pub warning: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct ListWalletDirResult {
pub wallets: Vec<ListWalletDirItem>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct ListWalletDirItem {
pub name: String,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetWalletInfoResult {
#[serde(rename = "walletname")]
pub wallet_name: String,
#[serde(rename = "walletversion")]
pub wallet_version: u32,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub balance: Amount,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub unconfirmed_balance: Amount,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub immature_balance: Amount,
#[serde(rename = "txcount")]
pub tx_count: usize,
#[serde(rename = "keypoololdest")]
pub keypool_oldest: Option<usize>,
#[serde(rename = "keypoolsize")]
pub keypool_size: usize,
#[serde(rename = "keypoolsize_hd_internal")]
pub keypool_size_hd_internal: usize,
pub unlocked_until: Option<u64>,
#[serde(rename = "paytxfee", with = "bitcoin::amount::serde::as_btc")]
pub pay_tx_fee: Amount,
#[serde(rename = "hdseedid")]
pub hd_seed_id: Option<bitcoin::bip32::XKeyIdentifier>,
pub private_keys_enabled: bool,
pub avoid_reuse: Option<bool>,
pub scanning: Option<ScanningDetails>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ScanningDetails {
Scanning {
duration: usize,
progress: f32,
},
/// The bool in this field will always be false.
NotScanning(bool),
}
impl Eq for ScanningDetails {}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetBlockResult {
pub hash: bitcoin::BlockHash,
pub confirmations: i32,
pub size: usize,
pub strippedsize: Option<usize>,
pub weight: usize,
pub height: usize,
pub version: i32,
#[serde(default, with = "crate::serde_hex::opt")]
pub version_hex: Option<Vec<u8>>,
pub merkleroot: bitcoin::hash_types::TxMerkleNode,
pub tx: Vec<bitcoin::Txid>,
pub time: usize,
pub mediantime: Option<usize>,
pub nonce: u32,
pub bits: String,
pub difficulty: f64,
#[serde(with = "crate::serde_hex")]
pub chainwork: Vec<u8>,
pub n_tx: usize,
pub previousblockhash: Option<bitcoin::BlockHash>,
pub nextblockhash: Option<bitcoin::BlockHash>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetBlockHeaderResult {
pub hash: bitcoin::BlockHash,
pub confirmations: i32,
pub height: usize,
pub version: Version,
#[serde(default, with = "crate::serde_hex::opt")]
pub version_hex: Option<Vec<u8>>,
#[serde(rename = "merkleroot")]
pub merkle_root: bitcoin::hash_types::TxMerkleNode,
pub time: usize,
#[serde(rename = "mediantime")]
pub median_time: Option<usize>,
pub nonce: u32,
pub bits: String,
pub difficulty: f64,
#[serde(with = "crate::serde_hex")]
pub chainwork: Vec<u8>,
pub n_tx: usize,
#[serde(rename = "previousblockhash")]
pub previous_block_hash: Option<bitcoin::BlockHash>,
#[serde(rename = "nextblockhash")]
pub next_block_hash: Option<bitcoin::BlockHash>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct GetBlockStatsResult {
#[serde(rename = "avgfee", with = "bitcoin::amount::serde::as_sat")]
pub avg_fee: Amount,
#[serde(rename = "avgfeerate", with = "bitcoin::amount::serde::as_sat")]
pub avg_fee_rate: Amount,
#[serde(rename = "avgtxsize")]
pub avg_tx_size: u32,
#[serde(rename = "blockhash")]
pub block_hash: bitcoin::BlockHash,
#[serde(rename = "feerate_percentiles")]
pub fee_rate_percentiles: FeeRatePercentiles,
pub height: u64,
pub ins: usize,
#[serde(rename = "maxfee", with = "bitcoin::amount::serde::as_sat")]
pub max_fee: Amount,
#[serde(rename = "maxfeerate", with = "bitcoin::amount::serde::as_sat")]
pub max_fee_rate: Amount,
#[serde(rename = "maxtxsize")]
pub max_tx_size: u32,
#[serde(rename = "medianfee", with = "bitcoin::amount::serde::as_sat")]
pub median_fee: Amount,
#[serde(rename = "mediantime")]
pub median_time: u64,
#[serde(rename = "mediantxsize")]
pub median_tx_size: u32,
#[serde(rename = "minfee", with = "bitcoin::amount::serde::as_sat")]
pub min_fee: Amount,
#[serde(rename = "minfeerate", with = "bitcoin::amount::serde::as_sat")]
pub min_fee_rate: Amount,
#[serde(rename = "mintxsize")]
pub min_tx_size: u32,
pub outs: usize,
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub subsidy: Amount,
#[serde(rename = "swtotal_size")]
pub sw_total_size: usize,
#[serde(rename = "swtotal_weight")]
pub sw_total_weight: usize,
#[serde(rename = "swtxs")]
pub sw_txs: usize,
pub time: u64,
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub total_out: Amount,
pub total_size: usize,
pub total_weight: usize,
#[serde(rename = "totalfee", with = "bitcoin::amount::serde::as_sat")]
pub total_fee: Amount,
pub txs: usize,
pub utxo_increase: i32,
pub utxo_size_inc: i32,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct GetBlockStatsResultPartial {
#[serde(
default,
rename = "avgfee",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub avg_fee: Option<Amount>,
#[serde(
default,
rename = "avgfeerate",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub avg_fee_rate: Option<Amount>,
#[serde(default, rename = "avgtxsize", skip_serializing_if = "Option::is_none")]
pub avg_tx_size: Option<u32>,
#[serde(default, rename = "blockhash", skip_serializing_if = "Option::is_none")]
pub block_hash: Option<bitcoin::BlockHash>,
#[serde(default, rename = "feerate_percentiles", skip_serializing_if = "Option::is_none")]
pub fee_rate_percentiles: Option<FeeRatePercentiles>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub height: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ins: Option<usize>,
#[serde(
default,
rename = "maxfee",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub max_fee: Option<Amount>,
#[serde(
default,
rename = "maxfeerate",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub max_fee_rate: Option<Amount>,
#[serde(default, rename = "maxtxsize", skip_serializing_if = "Option::is_none")]
pub max_tx_size: Option<u32>,
#[serde(
default,
rename = "medianfee",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub median_fee: Option<Amount>,
#[serde(default, rename = "mediantime", skip_serializing_if = "Option::is_none")]
pub median_time: Option<u64>,
#[serde(default, rename = "mediantxsize", skip_serializing_if = "Option::is_none")]
pub median_tx_size: Option<u32>,
#[serde(
default,
rename = "minfee",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub min_fee: Option<Amount>,
#[serde(
default,
rename = "minfeerate",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub min_fee_rate: Option<Amount>,
#[serde(default, rename = "mintxsize", skip_serializing_if = "Option::is_none")]
pub min_tx_size: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub outs: Option<usize>,
#[serde(
default,
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub subsidy: Option<Amount>,
#[serde(default, rename = "swtotal_size", skip_serializing_if = "Option::is_none")]
pub sw_total_size: Option<usize>,
#[serde(default, rename = "swtotal_weight", skip_serializing_if = "Option::is_none")]
pub sw_total_weight: Option<usize>,
#[serde(default, rename = "swtxs", skip_serializing_if = "Option::is_none")]
pub sw_txs: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub time: Option<u64>,
#[serde(
default,
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub total_out: Option<Amount>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub total_size: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub total_weight: Option<usize>,
#[serde(
default,
rename = "totalfee",
with = "bitcoin::amount::serde::as_sat::opt",
skip_serializing_if = "Option::is_none"
)]
pub total_fee: Option<Amount>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub txs: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub utxo_increase: Option<i32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub utxo_size_inc: Option<i32>,
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
pub struct FeeRatePercentiles {
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub fr_10th: Amount,
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub fr_25th: Amount,
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub fr_50th: Amount,
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub fr_75th: Amount,
#[serde(with = "bitcoin::amount::serde::as_sat")]
pub fr_90th: Amount,
}
#[derive(Clone)]
pub enum BlockStatsFields {
AverageFee,
AverageFeeRate,
AverageTxSize,
BlockHash,
FeeRatePercentiles,
Height,
Ins,
MaxFee,
MaxFeeRate,
MaxTxSize,
MedianFee,
MedianTime,
MedianTxSize,
MinFee,
MinFeeRate,
MinTxSize,
Outs,
Subsidy,
SegWitTotalSize,
SegWitTotalWeight,
SegWitTxs,
Time,
TotalOut,
TotalSize,
TotalWeight,
TotalFee,
Txs,
UtxoIncrease,
UtxoSizeIncrease,
}
impl BlockStatsFields {
fn get_rpc_keyword(&self) -> &str {
match *self {
BlockStatsFields::AverageFee => "avgfee",
BlockStatsFields::AverageFeeRate => "avgfeerate",
BlockStatsFields::AverageTxSize => "avgtxsize",
BlockStatsFields::BlockHash => "blockhash",
BlockStatsFields::FeeRatePercentiles => "feerate_percentiles",
BlockStatsFields::Height => "height",
BlockStatsFields::Ins => "ins",
BlockStatsFields::MaxFee => "maxfee",
BlockStatsFields::MaxFeeRate => "maxfeerate",
BlockStatsFields::MaxTxSize => "maxtxsize",
BlockStatsFields::MedianFee => "medianfee",
BlockStatsFields::MedianTime => "mediantime",
BlockStatsFields::MedianTxSize => "mediantxsize",
BlockStatsFields::MinFee => "minfee",
BlockStatsFields::MinFeeRate => "minfeerate",
BlockStatsFields::MinTxSize => "minfeerate",
BlockStatsFields::Outs => "outs",
BlockStatsFields::Subsidy => "subsidy",
BlockStatsFields::SegWitTotalSize => "swtotal_size",
BlockStatsFields::SegWitTotalWeight => "swtotal_weight",
BlockStatsFields::SegWitTxs => "swtxs",
BlockStatsFields::Time => "time",
BlockStatsFields::TotalOut => "total_out",
BlockStatsFields::TotalSize => "total_size",
BlockStatsFields::TotalWeight => "total_weight",
BlockStatsFields::TotalFee => "totalfee",
BlockStatsFields::Txs => "txs",
BlockStatsFields::UtxoIncrease => "utxo_increase",
BlockStatsFields::UtxoSizeIncrease => "utxo_size_inc",
}
}
}
impl fmt::Display for BlockStatsFields {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.get_rpc_keyword())
}
}
impl From<BlockStatsFields> for serde_json::Value {
fn from(bsf: BlockStatsFields) -> Self {
Self::from(bsf.to_string())
}
}
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetMiningInfoResult {
pub blocks: u32,
#[serde(rename = "currentblockweight")]
pub current_block_weight: Option<u64>,
#[serde(rename = "currentblocktx")]
pub current_block_tx: Option<usize>,
pub difficulty: f64,
#[serde(rename = "networkhashps")]
pub network_hash_ps: f64,
#[serde(rename = "pooledtx")]
pub pooled_tx: usize,
#[serde(deserialize_with = "deserialize_bip70_network")]
pub chain: Network,
pub warnings: StringOrStringArray,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetRawTransactionResultVinScriptSig {
pub asm: String,
#[serde(with = "crate::serde_hex")]
pub hex: Vec<u8>,
}
impl GetRawTransactionResultVinScriptSig {
pub fn script(&self) -> Result<ScriptBuf, encode::Error> {
Ok(ScriptBuf::from(self.hex.clone()))
}
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetRawTransactionResultVin {
pub sequence: u32,
/// The raw scriptSig in case of a coinbase tx.
#[serde(default, with = "crate::serde_hex::opt")]
pub coinbase: Option<Vec<u8>>,
/// Not provided for coinbase txs.
pub txid: Option<bitcoin::Txid>,
/// Not provided for coinbase txs.
pub vout: Option<u32>,
/// The scriptSig in case of a non-coinbase tx.
pub script_sig: Option<GetRawTransactionResultVinScriptSig>,
/// Not provided for coinbase txs.
#[serde(default, deserialize_with = "deserialize_hex_array_opt")]
pub txinwitness: Option<Vec<Vec<u8>>>,
}
impl GetRawTransactionResultVin {
/// Whether this input is from a coinbase tx.
/// The [txid], [vout] and [script_sig] fields are not provided
/// for coinbase transactions.
pub fn is_coinbase(&self) -> bool {
self.coinbase.is_some()
}
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetRawTransactionResultVoutScriptPubKey {
pub asm: String,
#[serde(with = "crate::serde_hex")]
pub hex: Vec<u8>,
pub req_sigs: Option<usize>,
#[serde(rename = "type")]
pub type_: Option<ScriptPubkeyType>,
// Deprecated in Bitcoin Core 22
#[serde(default)]
pub addresses: Vec<Address<NetworkUnchecked>>,
// Added in Bitcoin Core 22
#[serde(default)]
pub address: Option<Address<NetworkUnchecked>>,
}
impl GetRawTransactionResultVoutScriptPubKey {
pub fn script(&self) -> Result<ScriptBuf, encode::Error> {
Ok(ScriptBuf::from(self.hex.clone()))
}
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetRawTransactionResultVout {
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub value: Amount,
pub n: u32,
pub script_pub_key: GetRawTransactionResultVoutScriptPubKey,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetRawTransactionResult {
#[serde(rename = "in_active_chain")]
pub in_active_chain: Option<bool>,
#[serde(with = "crate::serde_hex")]
pub hex: Vec<u8>,
pub txid: bitcoin::Txid,
pub hash: bitcoin::Wtxid,
pub size: usize,
pub vsize: usize,
pub version: u32,
pub locktime: u32,
pub vin: Vec<GetRawTransactionResultVin>,
pub vout: Vec<GetRawTransactionResultVout>,
pub blockhash: Option<bitcoin::BlockHash>,
pub confirmations: Option<u32>,
pub time: Option<usize>,
pub blocktime: Option<usize>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetBlockFilterResult {
pub header: bitcoin::hash_types::FilterHash,
#[serde(with = "crate::serde_hex")]
pub filter: Vec<u8>,
}
impl GetBlockFilterResult {
/// Get the filter.
/// Note that this copies the underlying filter data. To prevent this,
/// use [into_filter] instead.
pub fn to_filter(&self) -> bip158::BlockFilter {
bip158::BlockFilter::new(&self.filter)
}
/// Convert the result in the filter type.
pub fn into_filter(self) -> bip158::BlockFilter {
bip158::BlockFilter {
content: self.filter,
}
}
}
impl GetRawTransactionResult {
/// Whether this tx is a coinbase tx.
pub fn is_coinbase(&self) -> bool {
self.vin.len() == 1 && self.vin[0].is_coinbase()
}
pub fn transaction(&self) -> Result<Transaction, encode::Error> {
Ok(encode::deserialize(&self.hex)?)
}
}
/// Enum to represent the BIP125 replaceable status for a transaction.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Bip125Replaceable {
Yes,
No,
Unknown,
}
/// Enum to represent the category of a transaction.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum GetTransactionResultDetailCategory {
Send,
Receive,
Generate,
Immature,
Orphan,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetTransactionResultDetail {
pub address: Option<Address<NetworkUnchecked>>,
pub category: GetTransactionResultDetailCategory,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub amount: SignedAmount,
pub label: Option<String>,
pub vout: u32,
#[serde(default, with = "bitcoin::amount::serde::as_btc::opt")]
pub fee: Option<SignedAmount>,
pub abandoned: Option<bool>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct WalletTxInfo {
pub confirmations: i32,
pub blockhash: Option<bitcoin::BlockHash>,
pub blockindex: Option<usize>,
pub blocktime: Option<u64>,
pub blockheight: Option<u32>,
pub txid: bitcoin::Txid,
pub time: u64,
pub timereceived: u64,
#[serde(rename = "bip125-replaceable")]
pub bip125_replaceable: Bip125Replaceable,
/// Conflicting transaction ids
#[serde(rename = "walletconflicts")]
pub wallet_conflicts: Vec<bitcoin::Txid>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetTransactionResult {
#[serde(flatten)]
pub info: WalletTxInfo,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub amount: SignedAmount,
#[serde(default, with = "bitcoin::amount::serde::as_btc::opt")]
pub fee: Option<SignedAmount>,
pub details: Vec<GetTransactionResultDetail>,
#[serde(with = "crate::serde_hex")]
pub hex: Vec<u8>,
}
impl GetTransactionResult {
pub fn transaction(&self) -> Result<Transaction, encode::Error> {
Ok(encode::deserialize(&self.hex)?)
}
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct ListTransactionResult {
#[serde(flatten)]
pub info: WalletTxInfo,
#[serde(flatten)]
pub detail: GetTransactionResultDetail,
pub trusted: Option<bool>,
pub comment: Option<String>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct ListSinceBlockResult {
pub transactions: Vec<ListTransactionResult>,
#[serde(default)]
pub removed: Vec<ListTransactionResult>,
pub lastblock: bitcoin::BlockHash,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTxOutResult {
pub bestblock: bitcoin::BlockHash,
pub confirmations: u32,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub value: Amount,
pub script_pub_key: GetRawTransactionResultVoutScriptPubKey,
pub coinbase: bool,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ListUnspentQueryOptions {
#[serde(
rename = "minimumAmount",
with = "bitcoin::amount::serde::as_btc::opt",
skip_serializing_if = "Option::is_none"
)]
pub minimum_amount: Option<Amount>,
#[serde(
rename = "maximumAmount",
with = "bitcoin::amount::serde::as_btc::opt",
skip_serializing_if = "Option::is_none"
)]
pub maximum_amount: Option<Amount>,
#[serde(rename = "maximumCount", skip_serializing_if = "Option::is_none")]
pub maximum_count: Option<usize>,
#[serde(
rename = "minimumSumAmount",
with = "bitcoin::amount::serde::as_btc::opt",
skip_serializing_if = "Option::is_none"
)]
pub minimum_sum_amount: Option<Amount>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ListUnspentResultEntry {
pub txid: bitcoin::Txid,
pub vout: u32,
pub address: Option<Address<NetworkUnchecked>>,
pub label: Option<String>,
pub redeem_script: Option<ScriptBuf>,
pub witness_script: Option<ScriptBuf>,
pub script_pub_key: ScriptBuf,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub amount: Amount,
pub confirmations: u32,
pub spendable: bool,
pub solvable: bool,
#[serde(rename = "desc")]
pub descriptor: Option<String>,
pub safe: bool,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ListReceivedByAddressResult {
#[serde(default, rename = "involvesWatchonly")]
pub involved_watch_only: bool,
pub address: Address<NetworkUnchecked>,
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub amount: Amount,
pub confirmations: u32,
pub label: String,
pub txids: Vec<bitcoin::Txid>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SignRawTransactionResultError {
pub txid: bitcoin::Txid,
pub vout: u32,
pub script_sig: ScriptBuf,
pub sequence: u32,
pub error: String,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SignRawTransactionResult {
#[serde(with = "crate::serde_hex")]
pub hex: Vec<u8>,
pub complete: bool,
pub errors: Option<Vec<SignRawTransactionResultError>>,
}
impl SignRawTransactionResult {
pub fn transaction(&self) -> Result<Transaction, encode::Error> {
Ok(encode::deserialize(&self.hex)?)
}
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct TestMempoolAcceptResult {
pub txid: bitcoin::Txid,
pub allowed: bool,
#[serde(rename = "reject-reason")]
pub reject_reason: Option<String>,
/// Virtual transaction size as defined in BIP 141 (only present when 'allowed' is true)
/// Added in Bitcoin Core v0.21
pub vsize: Option<u64>,
/// Transaction fees (only present if 'allowed' is true)
/// Added in Bitcoin Core v0.21
pub fees: Option<TestMempoolAcceptResultFees>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct TestMempoolAcceptResultFees {
/// Transaction fee in BTC
#[serde(with = "bitcoin::amount::serde::as_btc")]
pub base: Amount,
// unlike GetMempoolEntryResultFees, this only has the `base` fee
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Bip9SoftforkStatus {
Defined,
Started,
LockedIn,
Active,
Failed,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct Bip9SoftforkStatistics {
pub period: u32,
pub threshold: Option<u32>,
pub elapsed: u32,
pub count: u32,
pub possible: Option<bool>,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct Bip9SoftforkInfo {
pub status: Bip9SoftforkStatus,
pub bit: Option<u8>,
// Can be -1 for 0.18.x inactive ones.
pub start_time: i64,
pub timeout: u64,
pub since: u32,
pub statistics: Option<Bip9SoftforkStatistics>,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SoftforkType {
Buried,
Bip9,
#[serde(other)]
Other,
}
/// Status of a softfork
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct Softfork {
#[serde(rename = "type")]
pub type_: SoftforkType,
pub bip9: Option<Bip9SoftforkInfo>,
pub height: Option<u32>,
pub active: bool,
}
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ScriptPubkeyType {
Nonstandard,
Pubkey,
PubkeyHash,
ScriptHash,
MultiSig,
NullData,
Witness_v0_KeyHash,
Witness_v0_ScriptHash,
Witness_v1_Taproot,
Witness_Unknown,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetAddressInfoResultEmbedded {
pub address: Address<NetworkUnchecked>,
#[serde(rename = "scriptPubKey")]
pub script_pub_key: ScriptBuf,
#[serde(rename = "is_script")]
pub is_script: Option<bool>,
#[serde(rename = "is_witness")]
pub is_witness: Option<bool>,
pub witness_version: Option<u32>,
#[serde(with = "crate::serde_hex")]
pub witness_program: Vec<u8>,
pub script: Option<ScriptPubkeyType>,
/// The redeemscript for the p2sh address.
#[serde(default, with = "crate::serde_hex::opt")]
pub hex: Option<Vec<u8>>,
pub pubkeys: Option<Vec<PublicKey>>,
#[serde(rename = "sigsrequired")]
pub n_signatures_required: Option<usize>,
pub pubkey: Option<PublicKey>,
#[serde(rename = "is_compressed")]
pub is_compressed: Option<bool>,
pub label: Option<String>,
#[serde(rename = "hdkeypath")]
pub hd_key_path: Option<bip32::DerivationPath>,
#[serde(rename = "hdseedid")]
pub hd_seed_id: Option<bitcoin::bip32::XKeyIdentifier>,
#[serde(default)]
pub labels: Vec<GetAddressInfoResultLabel>,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum GetAddressInfoResultLabelPurpose {
Send,
Receive,
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum GetAddressInfoResultLabel {
Simple(String),
WithPurpose {
name: String,
purpose: GetAddressInfoResultLabelPurpose,
},
}
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetAddressInfoResult {
pub address: Address<NetworkUnchecked>,
#[serde(rename = "scriptPubKey")]
pub script_pub_key: ScriptBuf,
#[serde(rename = "ismine")]
pub is_mine: Option<bool>,
#[serde(rename = "iswatchonly")]
pub is_watchonly: Option<bool>,
#[serde(rename = "isscript")]
pub is_script: Option<bool>,
#[serde(rename = "iswitness")]
pub is_witness: Option<bool>,
pub witness_version: Option<u32>,
#[serde(default, with = "crate::serde_hex::opt")]
pub witness_program: Option<Vec<u8>>,
pub script: Option<ScriptPubkeyType>,
/// The redeemscript for the p2sh address.
#[serde(default, with = "crate::serde_hex::opt")]
pub hex: Option<Vec<u8>>,
pub pubkeys: Option<Vec<PublicKey>>,
#[serde(rename = "sigsrequired")]
pub n_signatures_required: Option<usize>,
pub pubkey: Option<PublicKey>,
/// Information about the address embedded in P2SH or P2WSH, if relevant and known.
pub embedded: Option<GetAddressInfoResultEmbedded>,
#[serde(rename = "is_compressed")]
pub is_compressed: Option<bool>,
pub timestamp: Option<u64>,
#[serde(rename = "hdkeypath")]
pub hd_key_path: Option<bip32::DerivationPath>,