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
//! bitcoin_rest - A Bitcoin Core REST API wrapper library for Rust.
//! 
//! This library calls the [Bitcoin Core's REST API endpoint](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md) and
//! converts them to [rust-bitcoin](https://github.com/rust-bitcoin/rust-bitcoin) objects.
//! 
//! For details, please see [Context](./struct.Context.html).

#[cfg(feature="softforks")]
use std::collections::HashMap;
pub use bytes;
pub use serde;
pub use reqwest;
pub use bitcoin;
use serde::{Deserialize, Serialize};
use bitcoin::hash_types::{BlockHash, Txid};
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::Decodable;

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

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

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

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

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

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

#[derive(Debug)]
pub enum Error {
    Reqwest(reqwest::Error),
    BitcoinEncodeError(bitcoin::consensus::encode::Error)
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Self::Reqwest(err)
    }
}

impl From<bitcoin::consensus::encode::Error> for Error {
    fn from(err: bitcoin::consensus::encode::Error) -> Self {
        Self::BitcoinEncodeError(err)
    }
}

/// `bitcoin_rest` context.
#[derive(Debug, Clone)]
pub struct Context {
    endpoint: String,
    client: reqwest::Client,
}

/// Create a new `bitcoin_rest` context.
///
/// The `endpoint` will be the string like "http://localhost:8332/rest"
/// (Note: this string is available via `bitcoin_rest::DEFAULT_ENDPOINT`).
pub fn new(endpoint: &str) -> Context {
    Context {
        endpoint: endpoint.to_string(),
        client: reqwest::Client::new(),
    }
}

impl Context {
    /// Call the REST endpoint and parse it as a JSON.
    pub async fn call_json<T: for<'de> Deserialize<'de>>(&self, path: &str) -> Result<T, reqwest::Error> {
        let url = format!("{}/{}.json", &self.endpoint, path);
        let result = self.client.get(url)
            .send().await?
            .json::<T>().await?;
        Ok(result)
    }
    /// Call the REST endpoint (binary).
    pub async fn call_bin(&self, path: &str) -> Result<bytes::Bytes, reqwest::Error> {
        let url = format!("{}/{}.bin", &self.endpoint, path);
        let result = self.client.get(url)
            .send().await?
            .bytes().await?;
        Ok(result)
    }
    /// Call the REST endpoint (hex).
    pub async fn call_hex(&self, path: &str) -> Result<String, reqwest::Error> {
        let url = format!("{}/{}.hex", &self.endpoint, path);
        let mut result = self.client.get(url)
            .send().await?
            .text().await?;
        // Trim last '\n'.
        result.pop();
        Ok(result)
    }
    /// Call the [/tx](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#transactions) endpoint.
    pub async fn tx(&self, txhash: &Txid) -> Result<Transaction, Error> {
        let result = self.call_bin(&["tx", &txhash.to_string()].join("/")).await?;
        Ok(Transaction::consensus_decode(result.as_ref())?)
    }
    /// Call the [/block](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#blocks) endpoint.
    pub async fn block(&self, blockhash: &BlockHash) -> Result<Block, Error> {
        let result = self.call_bin(&["block", &blockhash.to_string()].join("/")).await?;
        Ok(Block::consensus_decode(result.as_ref())?)
    }
    /// Call the [/block/notxdetails](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#blocks) endpoint.
    pub async fn block_notxdetails(&self, blockhash: &BlockHash) -> Result<BlockHeader, Error> {
        let result = self.call_bin(&["block", "notxdetails", &blockhash.to_string()].join("/")).await?;
        Ok(BlockHeader::consensus_decode(result.as_ref())?)
    }
    /// Call the [/headers](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#blockheaders) endpoint.
    pub async fn headers(&self, count: u32, blockhash: &BlockHash) -> Result<Vec<BlockHeader>, Error> {
        let result = self.call_bin(&["headers", &count.to_string(), &blockhash.to_string()].join("/")).await?;
        let mut ret = Vec::new();
        const BLOCK_HEADER_SIZE: usize = 80usize;
        let mut offset = 0;
        while offset < result.len() {
            ret.push(BlockHeader::consensus_decode(result[offset..(offset+BLOCK_HEADER_SIZE)].as_ref())?);
            offset += BLOCK_HEADER_SIZE;
        }
        Ok(ret)
    }
    /// Call the [/blockhashbyheight](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#blockhash-by-height) endpoint.
    pub async fn blockhashbyheight(&self, height: u32) -> Result<BlockHash, Error> {
        let result = self.call_bin(&["blockhashbyheight", &height.to_string()].join("/")).await?;
        Ok(BlockHash::consensus_decode(result.as_ref())?)
    }
    /// Call the [/chaininfo](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#chaininfo) endpoint.
    pub async fn chaininfo(&self) -> Result<ChainInfo, Error> {
        let result: ChainInfo = self.call_json("chaininfo").await?;
        Ok(result)
    }
    /// Call the [/getutxos](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md#query-utxo-set) endpoint.
    pub async fn getutxos(&self, checkmempool: bool, txids: &[Txid]) -> Result<UtxoData, Error> {
        let mut path = Vec::with_capacity(1 + if checkmempool { 0 } else { 1 } + txids.len());
        path.push("getutxos".to_string());
        if checkmempool {
            path.push("checkmempool".to_string());
        }
        for (i, txid) in txids.iter().enumerate() {
            path.push([txid.to_string(), i.to_string()].join("-"));
        }
        let result: UtxoData = self.call_json(&path.join("/")).await?;
        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use super::*;
    #[tokio::test]
    async fn reqwest_fail() {
        let rest = new("http://invalid-url");
        assert!(rest.blockhashbyheight(0).await.is_err());
    }
    struct Fixture {
        rest_env_name: &'static str,
        genesis_block_hash: &'static str,
        txid_coinbase_block1: &'static str,
    }
    async fn decode_fail(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        assert!(rest.blockhashbyheight(0xFFFFFFFF).await.is_err());
    }
    async fn tx(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let tx = rest.tx(&Txid::from_str(f.txid_coinbase_block1).unwrap()).await.unwrap();
        assert_eq!(tx.txid().to_string(), f.txid_coinbase_block1);
    }
    async fn block(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let blockid = BlockHash::from_str(f.genesis_block_hash).unwrap();
        let block = rest.block(&blockid).await.unwrap();
        assert_eq!(block.block_hash().to_string(), f.genesis_block_hash);
    }
    async fn block_notxdetails(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let blockid = BlockHash::from_str(f.genesis_block_hash).unwrap();
        let blockheader = rest.block_notxdetails(&blockid).await.unwrap();
        assert_eq!(blockheader.block_hash().to_string(), f.genesis_block_hash);
    }
    async fn headers(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let blockid = BlockHash::from_str(f.genesis_block_hash).unwrap();
        let headers = rest.headers(1, &blockid).await.unwrap();
        assert_eq!(headers[0].block_hash().to_string(), f.genesis_block_hash);
    }
    async fn chaininfo(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let chaininfo = rest.chaininfo().await.unwrap();
        assert_eq!(chaininfo.chain, "main");
    }
    async fn blockhashbyheight(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        assert_eq!(rest.blockhashbyheight(0).await.unwrap().to_string(), f.genesis_block_hash);
    }
    async fn blockhashbyheight_hex(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let blockhash_hex = rest.call_hex("blockhashbyheight/0").await.unwrap();
        let blockhash = BlockHash::from_str(&blockhash_hex).unwrap();
        assert_eq!(blockhash.to_string(), f.genesis_block_hash);
    }
    async fn utxos(f: &Fixture) {
        let test_endpoint = std::env::var(f.rest_env_name).unwrap_or(DEFAULT_ENDPOINT.to_string());
        let rest = new(&test_endpoint);
        let utxos = rest.getutxos(true, &vec![
            Txid::from_str(f.txid_coinbase_block1).unwrap(),
        ]).await.unwrap();
        assert!(utxos.chain_height > 0);
    }
    const BTC: Fixture = Fixture {
        rest_env_name: "BITCOIN_REST_ENDPOINT",
        genesis_block_hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
        txid_coinbase_block1: "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
    };
    #[tokio::test] async fn btc_decode_fail          () { decode_fail          (&BTC).await; }
    #[tokio::test] async fn btc_tx                   () { tx                   (&BTC).await; }
    #[tokio::test] async fn btc_block                () { block                (&BTC).await; }
    #[tokio::test] async fn btc_block_notxdetails    () { block_notxdetails    (&BTC).await; }
    #[tokio::test] async fn btc_headers              () { headers              (&BTC).await; }
    #[tokio::test] async fn btc_chaininfo            () { chaininfo            (&BTC).await; }
    #[tokio::test] async fn btc_blockhashbyheight    () { blockhashbyheight    (&BTC).await; }
    #[tokio::test] async fn btc_blockhashbyheight_hex() { blockhashbyheight_hex(&BTC).await; }
    #[tokio::test] async fn btc_utxos                () { utxos                (&BTC).await; }
    const MONA: Fixture = Fixture {
        rest_env_name: "MONACOIN_REST_ENDPOINT",
        genesis_block_hash: "ff9f1c0116d19de7c9963845e129f9ed1bfc0b376eb54fd7afa42e0d418c8bb6",
        txid_coinbase_block1: "10067abeabcd96a1261bc542b16d686d083308304923d74cb8f3bab4209cc3b9",
    };
    #[tokio::test] async fn mona_decode_fail      () { decode_fail      (&MONA).await; }
    #[tokio::test] async fn mona_tx               () { tx               (&MONA).await; }
    #[tokio::test] async fn mona_block            () { block            (&MONA).await; }
    #[tokio::test] async fn mona_block_notxdetails() { block_notxdetails(&MONA).await; }
    #[tokio::test] async fn mona_headers          () { headers          (&MONA).await; }
    #[cfg(not(feature="softforks"))]
    #[tokio::test] async fn mona_chaininfo        () { chaininfo        (&MONA).await; }
    //#[tokio::test] async fn mona_blockhashbyheight() { blockhashbyheight(&MONA).await; }
    #[tokio::test] async fn mona_utxos            () { utxos            (&MONA).await; }
}