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
use std::str::FromStr;
use std::collections::HashMap;
use serde::Deserialize;
use bitcoin::consensus::Decodable;

pub const DEFAULT_ENDPOINT: &str = "http://localhost:8332/rest/";

#[derive(Debug, Deserialize)]
pub struct BlockHashByHeight {
    blockhash: String,
}

#[derive(Debug, Deserialize)]
pub struct ScriptSig {
    asm: String,
    hex: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Vin {
    txid: String,
    vout: u32,
    script_sig: ScriptSig,
    sequence: u32,
    txinwitness: Vec<String>,
}

impl Vin {
    pub fn to_vin(&self) -> Result<bitcoin::blockdata::transaction::TxIn, Box<dyn std::error::Error>> {
        Ok(bitcoin::blockdata::transaction::TxIn{
            previous_output: bitcoin::blockdata::transaction::OutPoint{
                txid: bitcoin::hash_types::Txid::from_str(&self.txid).unwrap(),
                vout: self.vout,
            },
            script_sig: bitcoin::blockdata::script::Script::from_str(&self.script_sig.hex).unwrap(),
            sequence: self.sequence,
            witness: self.txinwitness.iter().map(|witness| {
                let mut buf: Vec<u8> = Vec::new();
                buf.resize(witness.len() / 2, 0);
                hex::decode_to_slice(witness, &mut buf).unwrap();
                buf
            }).collect(),
        })
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptPubKey {
    asm: String,
    hex: String,
    req_sigs: u32,
    #[serde(rename="type")]
    type_: String,
    addresses: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Vout {
    value: f64,
    script_pub_key: ScriptPubKey,
}

impl Vout {
    pub fn to_vout(&self) -> Result<bitcoin::blockdata::transaction::TxOut, Box<dyn std::error::Error>> {
        Ok(bitcoin::blockdata::transaction::TxOut{
            value: (1e8 * self.value) as u64,
            script_pubkey: bitcoin::blockdata::script::Script::from_str(&self.script_pub_key.hex).unwrap(),
        })
    }
}

#[derive(Debug, Deserialize)]
pub struct Tx {
    version: i32,
    locktime: u32,
    vin: Vec<Vin>,
    vout: Vec<Vout>,
}

impl Tx {
    pub fn to_transaction(&self) -> Result<bitcoin::blockdata::transaction::Transaction, Box<dyn std::error::Error>> {
        let vins = self.vin.iter().map(|vin| vin.to_vin()).collect::<Result<Vec<_>, _>>()?;
        let vouts = self.vout.iter().map(|vout| vout.to_vout()).collect::<Result<Vec<_>, _>>()?;
        Ok(bitcoin::blockdata::transaction::Transaction{
            version: self.version,
            lock_time: self.locktime,
            input: vins,
            output: vouts,
        })
    }
}

#[derive(Debug, Deserialize)]
pub struct BlockHeader {
    version: i32,
    previousblockhash: String,
    merkleroot: String,
    time: u32,
    bits: String,
    nonce: u32,
}

macro_rules! to_block_header {
    ($self: expr) => {
        bitcoin::blockdata::block::BlockHeader{
            version: $self.version,
            prev_blockhash: bitcoin::hash_types::BlockHash::from_str(&$self.previousblockhash)?,
            merkle_root: bitcoin::hash_types::TxMerkleNode::from_str(&$self.merkleroot)?,
            time: $self.time,
            bits: u32::from_str_radix(&$self.bits, 16)?,
            nonce: $self.nonce,
        }
    }
}

impl BlockHeader {
    pub fn to_block_header(&self) -> Result<bitcoin::blockdata::block::BlockHeader, Box<dyn std::error::Error>> {
        Ok(to_block_header!(self))
    }
}

#[derive(Debug, Deserialize)]
pub struct Block {
    version: i32,
    previousblockhash: String,
    merkleroot: String,
    time: u32,
    bits: String,
    nonce: u32,
    tx: Vec<Tx>,
}

impl Block {
    pub fn to_block_header(&self) -> Result<bitcoin::blockdata::block::BlockHeader, Box<dyn std::error::Error>> {
        Ok(to_block_header!(self))
    }
    pub fn to_block(&self) -> Result<bitcoin::blockdata::block::Block, Box<dyn std::error::Error>> {
        let blockheader = self.to_block_header()?;
        let transactions = self.tx.iter().map(|tx| tx.to_transaction()).collect::<Result<Vec<_>, _>>()?;
        Ok(bitcoin::blockdata::block::Block{
            header: blockheader,
            txdata: transactions,
        })
    }
}

#[derive(Debug, Deserialize)]
pub struct Softfork {
    #[serde(rename="type")]
    type_: String,
    active: bool,
    height: u32,
}

#[derive(Debug, Deserialize)]
pub struct ChainInfo {
    chain: String,
    blocks: u32,
    headers: u32,
    bestblockhash: String,
    difficulty: f64,
    mediantime: u32,
    verificationprogress: f64,
    chainwork: String,
    pruned: bool,
    #[serde(default)]
    pruneheight: u32,
    softforks: HashMap<String, Softfork>,
    warnings: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Utxo {
    height: u32,
    value: f64,
    script_pub_key: ScriptPubKey,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UtxoData {
    chain_height: u32,
    chaintip_hash: String,
    bitmap: String,
    utxos: Vec<Utxo>,
}

pub struct Context {
    endpoint: String,
}

impl Context {
    /// Call the REST endpoint.
    pub async fn call_json<T: for<'de> Deserialize<'de>>(&self, path: &str) -> Result<T, Box<dyn std::error::Error>> {
        let url = String::new() + &self.endpoint + path + ".json";
        let result = reqwest::get(url)
            .await?
            .json::<T>()
            .await?;
        Ok(result)
    }
    pub async fn call_bin(&self, path: &str) -> Result<bytes::Bytes, Box<dyn std::error::Error>> {
        let url = String::new() + &self.endpoint + path + ".bin";
        let result = reqwest::get(url)
            .await?
            .bytes()
            .await?;
        Ok(result)
    }
    pub async fn call_hex(&self, path: &str) -> Result<String, Box<dyn std::error::Error>> {
        let url = String::new() + &self.endpoint + path + ".hex";
        let mut result = reqwest::get(url)
            .await?
            .text()
            .await?;
        // Trim last '\n'.
        result.pop();
        Ok(result)
    }
    pub async fn tx(&self, txhash: bitcoin::hash_types::Txid)
        -> Result<bitcoin::blockdata::transaction::Transaction, Box<dyn std::error::Error>> {
        let path = String::from("tx/") + &txhash.to_string();
        let result = self.call_bin(&path).await?;
        Ok(bitcoin::blockdata::transaction::Transaction::consensus_decode(result.as_ref())?)
    }
    pub async fn block(&self, blockhash: bitcoin::hash_types::BlockHash) ->
        Result<bitcoin::blockdata::block::Block, Box<dyn std::error::Error>> {
        let path = String::from("block/") + &blockhash.to_string();
        let result = self.call_bin(&path).await?;
        Ok(bitcoin::blockdata::block::Block::consensus_decode(result.as_ref())?)
    }
    pub async fn block_notxdetails(&self, blockhash: bitcoin::hash_types::BlockHash) ->
        Result<bitcoin::blockdata::block::BlockHeader, Box<dyn std::error::Error>> {
        let path = String::from("block/notxdetails/") + &blockhash.to_string();
        let result = self.call_bin(&path).await?;
        Ok(bitcoin::blockdata::block::BlockHeader::consensus_decode(result.as_ref())?)
    }
    pub async fn headers(&self, count: u32, blockhash: bitcoin::hash_types::BlockHash) ->
        Result<Vec<bitcoin::blockdata::block::BlockHeader>, Box<dyn std::error::Error>> {
        let path = String::from("headers/") + &count.to_string() + "/" + &blockhash.to_string();
        let result = self.call_bin(&path).await?;
        let mut ret = Vec::new();
        for i in 0..count {
            let begin = (i as usize) * 80usize;
            let end = ((i + 1) as usize) * 80usize;
            ret.push(bitcoin::blockdata::block::BlockHeader::consensus_decode(result.slice(begin .. end).as_ref())?);
        }
        Ok(ret)
    }
    pub async fn blockhashbyheight(&self, height: u32) -> Result<bitcoin::hash_types::BlockHash, Box<dyn std::error::Error>> {
        let path = String::from("blockhashbyheight/") + &height.to_string();
        let result = self.call_hex(&path).await?;
        Ok(bitcoin::hash_types::BlockHash::from_str(&result)?)
    }
    pub async fn chaininfo(&self) -> Result<ChainInfo, Box<dyn std::error::Error>> {
        let result: ChainInfo = self.call_json("chaininfo").await?;
        Ok(result)
    }
    pub async fn getutxos(&self, checkmempool: bool, txids: &Vec<bitcoin::hash_types::Txid>) ->
        Result<UtxoData, Box<dyn std::error::Error>> {
        let mut url = String::from("getutxos/");
        if checkmempool {
            url += "checkmempool/"
        }
        for i in 0..txids.len() {
            url += &(txids[i].to_string() + "-" + &i.to_string());
        }
        let result: UtxoData = self.call_json(&url).await?;
        Ok(result)
    }
}

pub fn new(endpoint: &str) -> Context {
    Context{ endpoint: endpoint.to_string() }
}