forked from bitcoindevkit/rust-esplora-client
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.rs
196 lines (178 loc) · 5.52 KB
/
api.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
//! Structs from the Esplora API
//!
//! See: <https://github.com/Blockstream/esplora/blob/master/API.md>
use amplify::confinement::Confined;
use amplify::hex::FromHex;
use amplify::{confinement, Bytes32};
use bpstd::{
BlockHash, LockTime, Outpoint, ScriptPubkey, SeqNo, SigScript, Tx as Transaction, TxIn, TxOut,
TxVer, Txid, Witness,
};
use serde::Deserialize;
use serde_with::hex::Hex;
#[serde_as]
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct PrevOut {
pub value: u64,
#[serde_as(as = "Hex")]
pub scriptpubkey: ScriptPubkey,
}
#[serde_as]
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Vin {
pub txid: Txid,
pub vout: u32,
// None if coinbase
pub prevout: Option<PrevOut>,
#[serde_as(as = "Hex")]
pub scriptsig: SigScript,
#[serde(deserialize_with = "deserialize_witness", default)]
pub witness: Vec<Vec<u8>>,
pub sequence: u32,
pub is_coinbase: bool,
}
#[serde_as]
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Vout {
pub value: u64,
#[serde_as(as = "Hex")]
pub scriptpubkey: ScriptPubkey,
}
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct TxStatus {
pub confirmed: bool,
pub block_height: Option<u32>,
pub block_hash: Option<BlockHash>,
pub block_time: Option<u64>,
}
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct MerkleProof {
pub block_height: u32,
pub merkle: Vec<Txid>,
pub pos: usize,
}
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct OutputStatus {
pub spent: bool,
pub txid: Option<Txid>,
pub vin: Option<u64>,
pub status: Option<TxStatus>,
}
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct BlockStatus {
pub in_best_chain: bool,
pub height: Option<u32>,
pub next_best: Option<BlockHash>,
}
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Tx {
pub txid: Txid,
pub version: i32,
pub locktime: u32,
pub vin: Vec<Vin>,
pub vout: Vec<Vout>,
/// Transaction size in raw bytes (NOT virtual bytes).
pub size: u32,
/// Transaction weight units.
pub weight: u32,
pub status: TxStatus,
pub fee: u64,
}
#[derive(Deserialize, Clone, Debug)]
pub struct Utxo {
pub txid: Txid,
pub vout: Vec<Vout>,
pub value: u64,
pub status: TxStatus,
}
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct BlockTime {
pub timestamp: u64,
pub height: u32,
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct BlockSummary {
pub id: BlockHash,
#[serde(flatten)]
pub time: BlockTime,
/// Hash of the previous block, will be `None` for the genesis block.
pub previousblockhash: Option<BlockHash>,
pub merkle_root: Bytes32,
}
/// Address statistics, includes the address, and the utxo information for the address.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct AddressStats {
/// The address.
pub address: String,
/// The summary of transactions for this address, already on chain.
pub chain_stats: AddressTxsSummary,
/// The summary of transactions for this address, currently in the mempool.
pub mempool_stats: AddressTxsSummary,
}
/// Contains a summary of the transactions for an address.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize)]
pub struct AddressTxsSummary {
/// The number of funded transaction outputs.
pub funded_txo_count: u32,
/// The sum of the funded transaction outputs, in satoshis.
pub funded_txo_sum: u64,
/// The number of spent transaction outputs.
pub spent_txo_count: u32,
/// The sum of the spent transaction outputs, in satoshis.
pub spent_txo_sum: u64,
/// The total number of transactions.
pub tx_count: u32,
}
impl Tx {
pub fn to_tx(&self) -> Result<Transaction, confinement::Error> {
let inputs = self.vin.iter().cloned().map(|vin| TxIn {
prev_output: Outpoint::new(vin.txid, vin.vout),
sig_script: vin.scriptsig,
sequence: SeqNo::from_consensus_u32(vin.sequence),
witness: Witness::from_consensus_stack(vin.witness),
});
let outputs = self.vout.iter().cloned().map(|vout| TxOut {
value: vout.value.into(),
script_pubkey: vout.scriptpubkey,
});
Ok(Transaction {
version: TxVer::from_consensus_i32(self.version),
lock_time: LockTime::from_consensus_u32(self.locktime),
inputs: Confined::try_from_iter(inputs)?,
outputs: Confined::try_from_iter(outputs)?,
})
}
pub fn confirmation_time(&self) -> Option<BlockTime> {
match self.status {
TxStatus {
confirmed: true,
block_height: Some(height),
block_time: Some(timestamp),
..
} => Some(BlockTime { timestamp, height }),
_ => None,
}
}
pub fn previous_outputs(&self) -> Vec<Option<TxOut>> {
self.vin
.iter()
.cloned()
.map(|vin| {
vin.prevout.map(|po| TxOut {
script_pubkey: po.scriptpubkey,
value: po.value.into(),
})
})
.collect()
}
}
fn deserialize_witness<'de, D>(d: D) -> Result<Vec<Vec<u8>>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let list = Vec::<String>::deserialize(d)?;
list.into_iter()
.map(|hex_str| Vec::<u8>::from_hex(&hex_str))
.collect::<Result<Vec<Vec<u8>>, _>>()
.map_err(serde::de::Error::custom)
}