1+ // Bitcoin Dev Kit
2+ // Written in 2020 by Alekos Filini <[email protected] > 3+ //
4+ // Copyright (c) 2020-2025 Bitcoin Dev Kit Developers
5+ //
6+ // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
7+ // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
9+ // You may not use this file except in accordance with one or both of these
10+ // licenses.
11+
112//! Structs from the Esplora API
213//!
314//! See: <https://github.com/Blockstream/esplora/blob/master/API.md>
@@ -6,116 +17,157 @@ use bitcoin::hash_types;
617use serde:: Deserialize ;
718
819pub use bitcoin:: consensus:: { deserialize, serialize} ;
20+ use bitcoin:: hash_types:: TxMerkleNode ;
921pub use bitcoin:: hex:: FromHex ;
1022pub use bitcoin:: {
11- absolute, block, transaction, Amount , Block , BlockHash , CompactTarget , OutPoint , Script ,
12- ScriptBuf , ScriptHash , Transaction , TxIn , TxOut , Txid , Weight , Witness ,
23+ absolute, block, transaction, Address , Amount , Block , BlockHash , CompactTarget , OutPoint ,
24+ Script , ScriptBuf , ScriptHash , Transaction , TxIn , TxOut , Txid , Weight , Witness ,
1325} ;
1426
27+ /// Information about a previous output.
1528#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
1629pub struct PrevOut {
30+ /// The value of the previous output, in satoshis.
1731 pub value : u64 ,
32+ /// The ScriptPubKey that the previous output is locked to, as a [`ScriptBuf`].
1833 pub scriptpubkey : ScriptBuf ,
1934}
2035
36+ /// Information about an input from a [`Transaction`].
2137#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
2238pub struct Vin {
39+ /// The [`Txid`] of the previous [`Transaction`] this input spends from.
2340 pub txid : Txid ,
41+ /// The output index of the previous output in the [`Transaction`] that created it.
2442 pub vout : u32 ,
25- // None if coinbase
43+ /// The previous output amount and ScriptPubKey.
44+ /// `None` if this is a coinbase input.
2645 pub prevout : Option < PrevOut > ,
46+ /// The ScriptSig authorizes spending this input.
2747 pub scriptsig : ScriptBuf ,
48+ /// The Witness that authorizes spending this input, if this is a SegWit spend.
2849 #[ serde( deserialize_with = "deserialize_witness" , default ) ]
2950 pub witness : Vec < Vec < u8 > > ,
51+ /// The sequence value for this input, used to set RBF and Locktime behavior.
3052 pub sequence : u32 ,
53+ /// Whether this is a coinbase input.
3154 pub is_coinbase : bool ,
3255}
3356
57+ /// Information about a [`Transaction`]s output.
3458#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
3559pub struct Vout {
60+ /// The value of the output, in satoshis.
3661 pub value : u64 ,
62+ /// The ScriptPubKey that the output is locked to, as a [`ScriptBuf`].
3763 pub scriptpubkey : ScriptBuf ,
3864}
3965
66+ /// The confirmation status of a [`Transaction`].
4067#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
4168pub struct TxStatus {
69+ /// Whether the [`Transaction`] is confirmed or not.
4270 pub confirmed : bool ,
71+ /// The block height the [`Transaction`] was confirmed in.
4372 pub block_height : Option < u32 > ,
73+ /// The [`BlockHash`] of the block the [`Transaction`] was confirmed in.
4474 pub block_hash : Option < BlockHash > ,
75+ /// The time that the block was mined at, as a UNIX timestamp.
76+ /// Note: this timestamp is set by the miner and may not reflect the exact time of mining.
4577 pub block_time : Option < u64 > ,
4678}
4779
80+ /// A Merkle inclusion proof for a transaction, given it's [`Txid`].
4881#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
4982pub struct MerkleProof {
83+ /// The height of the block the [`Transaction`] was confirmed in.
5084 pub block_height : u32 ,
85+ /// A list of transaction hashes the current hash is paired with,
86+ /// recursively, in order to trace up to obtain the Merkle root of the
87+ /// [`Block`], deepest pairing first.
5188 pub merkle : Vec < Txid > ,
89+ /// The 0-based index of the position of the [`Transaction`] in the
90+ /// ordered list of [`Transaction`]s in the [`Block`].
5291 pub pos : usize ,
5392}
5493
94+ /// The spend status of a [`TxOut`].
5595#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
5696pub struct OutputStatus {
97+ /// Whether the [`TxOut`] is spent or not.
5798 pub spent : bool ,
99+ /// The [`Txid`] that spent this [`TxOut`].
58100 pub txid : Option < Txid > ,
101+ /// The input index of this [`TxOut`] in the [`Transaction`] that spent it.
59102 pub vin : Option < u64 > ,
103+ /// Information about the [`Transaction`] that spent this [`TxOut`].
60104 pub status : Option < TxStatus > ,
61105}
62106
107+ /// Information about a [`Block`]s status.
63108#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
64109pub struct BlockStatus {
110+ /// Whether this [`Block`] belongs to the chain with the most
111+ /// Proof-of-Work (false for [`Block`]s that belong to a stale chain).
65112 pub in_best_chain : bool ,
113+ /// The height of this [`Block`].
66114 pub height : Option < u32 > ,
115+ /// The [`BlockHash`] of the [`Block`] that builds on top of this one.
67116 pub next_best : Option < BlockHash > ,
68117}
69118
119+ /// A [`Transaction`] in the format returned by Esplora.
70120#[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
71121pub struct Tx {
122+ /// The [`Txid`] of the [`Transaction`].
72123 pub txid : Txid ,
124+ /// The version number of the [`Transaction`].
73125 pub version : i32 ,
126+ /// The locktime of the [`Transaction`].
127+ /// Sets a time or height after which the [`Transaction`] can be mined.
74128 pub locktime : u32 ,
129+ /// The array of inputs in the [`Transaction`].
75130 pub vin : Vec < Vin > ,
131+ /// The array of outputs in the [`Transaction`].
76132 pub vout : Vec < Vout > ,
77- /// Transaction size in raw bytes (NOT virtual bytes).
133+ /// The [` Transaction`] size in raw bytes (NOT virtual bytes).
78134 pub size : usize ,
79- /// Transaction weight units.
135+ /// The [` Transaction`]'s weight units.
80136 pub weight : u64 ,
137+ /// The confirmation status of the [`Transaction`].
81138 pub status : TxStatus ,
139+ /// The fee amount paid by the [`Transaction`], in satoshis.
82140 pub fee : u64 ,
83141}
84142
85- #[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
86- pub struct BlockTime {
87- pub timestamp : u64 ,
88- pub height : u32 ,
89- }
90-
91143/// Information about a bitcoin [`Block`].
92144#[ derive( Debug , Clone , Deserialize ) ]
93145pub struct BlockInfo {
94- /// The block 's [`BlockHash`].
146+ /// The [`Block`] 's [`BlockHash`].
95147 pub id : BlockHash ,
96- /// The block 's height.
148+ /// The [`Block`] 's height.
97149 pub height : u32 ,
98- /// The block 's version.
150+ /// The [`Block`] 's version.
99151 pub version : block:: Version ,
100- /// The block's timestamp.
152+ /// The [`Block`]'s UNIX timestamp.
101153 pub timestamp : u64 ,
102- /// The block 's transaction count.
154+ /// The [`Block`] 's [`Transaction`] count.
103155 pub tx_count : u64 ,
104- /// The block 's size, in bytes.
156+ /// The [`Block`] 's size, in bytes.
105157 pub size : usize ,
106- /// The block 's weight.
158+ /// The [`Block`] 's weight.
107159 pub weight : u64 ,
108- /// The merkle root of the transactions in the block.
160+ /// The Merkle root of the transactions in the block.
109161 pub merkle_root : hash_types:: TxMerkleNode ,
110- /// The [`BlockHash`] of the previous block (`None` for the genesis block).
162+ /// The [`BlockHash`] of the previous [`Block`] (`None` for the genesis block).
111163 pub previousblockhash : Option < BlockHash > ,
112- /// The block 's MTP (Median Time Past).
164+ /// The [`Block`] 's MTP (Median Time Past).
113165 pub mediantime : u64 ,
114- /// The block 's nonce value.
166+ /// The [`Block`] 's nonce value.
115167 pub nonce : u32 ,
116- /// The block 's `bits` value as a [`CompactTarget`].
168+ /// The [`Block`] 's `bits` value as a [`CompactTarget`].
117169 pub bits : CompactTarget ,
118- /// The block 's difficulty target value.
170+ /// The [`Block`] 's difficulty target value.
119171 pub difficulty : f64 ,
120172}
121173
@@ -141,112 +193,127 @@ impl PartialEq for BlockInfo {
141193}
142194impl Eq for BlockInfo { }
143195
196+ /// Time-related information about a [`Block`].
197+ #[ derive( Deserialize , Clone , Debug , PartialEq , Eq ) ]
198+ pub struct BlockTime {
199+ /// The [`Block`]'s timestamp.
200+ pub timestamp : u64 ,
201+ /// The [`Block`]'s height.
202+ pub height : u32 ,
203+ }
204+
205+ /// Summary about a [`Block`].
144206#[ derive( Debug , Clone , Deserialize , PartialEq , Eq ) ]
145207pub struct BlockSummary {
208+ /// The [`Block`]'s hash.
146209 pub id : BlockHash ,
210+ /// The [`Block`]'s timestamp and height.
147211 #[ serde( flatten) ]
148212 pub time : BlockTime ,
149- /// Hash of the previous block, will be `None` for the genesis block.
150- pub previousblockhash : Option < bitcoin:: BlockHash > ,
151- pub merkle_root : bitcoin:: hash_types:: TxMerkleNode ,
213+ /// The [`BlockHash`] of the previous [`Block`] (`None` for the genesis [`Block`]).
214+ pub previousblockhash : Option < BlockHash > ,
215+ /// The Merkle root of the [`Block`]'s [`Transaction`]s.
216+ pub merkle_root : TxMerkleNode ,
152217}
153218
154- /// Address statistics, includes the address, and the utxo information for the address .
219+ /// Statistics about an [`Address`] .
155220#[ derive( Debug , Clone , Deserialize , PartialEq , Eq ) ]
156221pub struct AddressStats {
157- /// The address .
222+ /// The [`Address`] .
158223 pub address : String ,
159- /// The summary of transactions for this address, already on chain .
224+ /// The summary of confirmed [`Transaction`]s for this [`Address`] .
160225 pub chain_stats : AddressTxsSummary ,
161- /// The summary of transactions for this address, currently in the mempool .
226+ /// The summary of mempool [`Transaction`]s for this [`Address`] .
162227 pub mempool_stats : AddressTxsSummary ,
163228}
164229
165- /// Contains a summary of the transactions for an address .
230+ /// A summary of [`Transaction`]s in which an [`Address`] was involved .
166231#[ derive( Debug , Copy , Clone , PartialEq , Eq , Deserialize ) ]
167232pub struct AddressTxsSummary {
168- /// The number of funded transaction outputs .
233+ /// The number of funded [`TxOut`]s .
169234 pub funded_txo_count : u32 ,
170- /// The sum of the funded transaction outputs , in satoshis.
235+ /// The sum of the funded [`TxOut`]s , in satoshis.
171236 pub funded_txo_sum : u64 ,
172- /// The number of spent transaction outputs .
237+ /// The number of spent [`TxOut`]s .
173238 pub spent_txo_count : u32 ,
174- /// The sum of the spent transaction outputs , in satoshis.
239+ /// The sum of the spent [`TxOut`]s , in satoshis.
175240 pub spent_txo_sum : u64 ,
176- /// The total number of transactions .
241+ /// The total number of [`Transaction`]s .
177242 pub tx_count : u32 ,
178243}
179244
180245/// Statistics about a particular [`Script`] hash's confirmed and mempool transactions.
181246#[ derive( Debug , Copy , Clone , PartialEq , Eq , Deserialize ) ]
182247pub struct ScriptHashStats {
183- /// The summary of confirmed transactions for this [`Script`] hash.
248+ /// The summary of confirmed [`Transaction`]s for this [`Script`] hash.
184249 pub chain_stats : ScriptHashTxsSummary ,
185- /// The summary of mempool transactions for this [`Script`] hash.
250+ /// The summary of mempool [`Transaction`]s for this [`Script`] hash.
186251 pub mempool_stats : ScriptHashTxsSummary ,
187252}
188253
189- /// Contains a summary of the transactions for a particular [`Script`] hash.
254+ /// Contains a summary of the [`Transaction`]s for a particular [`Script`] hash.
190255pub type ScriptHashTxsSummary = AddressTxsSummary ;
191256
192- /// Information about an UTXO 's status: confirmation status,
257+ /// Information about a [`TxOut`] 's status: confirmation status,
193258/// confirmation height, confirmation block hash and confirmation block time.
194259#[ derive( Debug , Copy , Clone , PartialEq , Eq , Deserialize ) ]
195260pub struct UtxoStatus {
196- /// Whether or not the UTXO is confirmed.
261+ /// Whether or not the [`TxOut`] is confirmed.
197262 pub confirmed : bool ,
198- /// The block height in which the UTXO was confirmed.
263+ /// The block height in which the [`TxOut`] was confirmed.
199264 pub block_height : Option < u32 > ,
200- /// The block hash in which the UTXO was confirmed.
265+ /// The block hash in which the [`TxOut`] was confirmed.
201266 pub block_hash : Option < BlockHash > ,
202- /// The UNIX timestamp in which the UTXO was confirmed.
267+ /// The UNIX timestamp in which the [`TxOut`] was confirmed.
203268 pub block_time : Option < u64 > ,
204269}
205270
206- /// Information about an UTXO 's outpoint, confirmation status and value.
271+ /// Information about an [`TxOut`] 's outpoint, confirmation status and value.
207272#[ derive( Debug , Copy , Clone , PartialEq , Eq , Deserialize ) ]
208273pub struct Utxo {
209- /// The [`Txid`] of the transaction that created the UTXO .
274+ /// The [`Txid`] of the [`Transaction`] that created the [`TxOut`] .
210275 pub txid : Txid ,
211- /// The output index of the UTXO on the transaction that created the it.
276+ /// The output index of the [`TxOut`] in the [`Transaction`] that created it.
212277 pub vout : u32 ,
213- /// The confirmation status of the UTXO .
278+ /// The confirmation status of the [`TxOut`] .
214279 pub status : UtxoStatus ,
215- /// The value of the UTXO as an [`Amount`].
280+ /// The value of the [`TxOut`] as an [`Amount`].
216281 pub value : Amount ,
217282}
218283
219284/// Statistics about the mempool.
220285#[ derive( Clone , Debug , PartialEq , Deserialize ) ]
221286pub struct MempoolStats {
222- /// The number of transactions in the mempool.
287+ /// The number of [`Transaction`]s in the mempool.
223288 pub count : usize ,
224- /// The total size of mempool transactions in virtual bytes.
289+ /// The total size of mempool [`Transaction`]s, in virtual bytes.
225290 pub vsize : usize ,
226- /// The total fee paid by mempool transactions , in sats .
291+ /// The total fee paid by mempool [`Transaction`]s , in satoshis .
227292 pub total_fee : u64 ,
228293 /// The mempool's fee rate distribution histogram.
229294 ///
230295 /// An array of `(feerate, vsize)` tuples, where each entry's `vsize` is the total vsize
231- /// of transactions paying more than `feerate` but less than the previous entry's `feerate`
296+ /// of [`Transaction`]s paying more than `feerate` but less than the previous entry's `feerate`
232297 /// (except for the first entry, which has no upper bound).
233298 pub fee_histogram : Vec < ( f64 , usize ) > ,
234299}
235300
236301/// A [`Transaction`] that recently entered the mempool.
237302#[ derive( Clone , Debug , PartialEq , Eq , Deserialize ) ]
238303pub struct MempoolRecentTx {
239- /// Transaction ID as a [`Txid`].
304+ /// The [` Transaction`]'s ID, as a [`Txid`].
240305 pub txid : Txid ,
241- /// [`Amount`] of fees paid by the transaction, in satoshis.
306+ /// The [`Amount`] of fees paid by the transaction, in satoshis.
242307 pub fee : u64 ,
243- /// The transaction size, in virtual bytes.
308+ /// The [`Transaction`]'s size, in virtual bytes.
244309 pub vsize : usize ,
245- /// Combined [`Amount`] of the transaction , in satoshis.
310+ /// Combined [`Amount`] of the [`Transaction`] , in satoshis.
246311 pub value : u64 ,
247312}
248313
249314impl Tx {
315+ /// Convert a transaction from the format returned by Esplora into a `rust-bitcoin`
316+ /// [`Transaction`].
250317 pub fn to_tx ( & self ) -> Transaction {
251318 Transaction {
252319 version : transaction:: Version :: non_standard ( self . version ) ,
@@ -277,6 +344,7 @@ impl Tx {
277344 }
278345 }
279346
347+ /// Get the confirmation time from a [`Tx`].
280348 pub fn confirmation_time ( & self ) -> Option < BlockTime > {
281349 match self . status {
282350 TxStatus {
@@ -289,6 +357,7 @@ impl Tx {
289357 }
290358 }
291359
360+ /// Get a list of the [`Tx`]'s previous outputs.
292361 pub fn previous_outputs ( & self ) -> Vec < Option < TxOut > > {
293362 self . vin
294363 . iter ( )
@@ -302,10 +371,12 @@ impl Tx {
302371 . collect ( )
303372 }
304373
374+ /// Get the weight of a [`Tx`].
305375 pub fn weight ( & self ) -> Weight {
306376 Weight :: from_wu ( self . weight )
307377 }
308378
379+ /// Get the fee paid by a [`Tx`].
309380 pub fn fee ( & self ) -> Amount {
310381 Amount :: from_sat ( self . fee )
311382 }
0 commit comments