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
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)]
#[serde(rename_all = "camelCase")]
pub struct ScriptPubKey {
    asm: String,
    hex: String,
    #[serde(default)]
    req_sigs: u32,
    #[serde(rename="type")]
    type_: String,
    #[serde(default)]
    addresses: Vec<String>,
}

#[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() }
}

#[cfg(test)]
mod tests {
    use super::*;
    const TEST_ENDPOINT: &str = DEFAULT_ENDPOINT;
    const GENESIS_BLOCK_HASH: &str = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f";
    const TXID_COINBASE_BLOCK1: &str = "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098";
    #[tokio::test]
    async fn tx() {
        let rest = new(TEST_ENDPOINT);
        let tx = rest.tx(bitcoin::hash_types::Txid::from_str(TXID_COINBASE_BLOCK1).unwrap()).await.unwrap();
        assert_eq!(tx.txid().to_string(), TXID_COINBASE_BLOCK1);
    }
    #[tokio::test]
    async fn block() {
        let rest = new(TEST_ENDPOINT);
        let blockid = bitcoin::hash_types::BlockHash::from_str(GENESIS_BLOCK_HASH).unwrap();
        let block = rest.block(blockid).await.unwrap();
        assert_eq!(block.block_hash().to_string(), GENESIS_BLOCK_HASH);
    }
    #[tokio::test]
    async fn block_notxdetails() {
        let rest = new(TEST_ENDPOINT);
        let blockid = bitcoin::hash_types::BlockHash::from_str(GENESIS_BLOCK_HASH).unwrap();
        let blockheader = rest.block_notxdetails(blockid).await.unwrap();
        assert_eq!(blockheader.block_hash().to_string(), GENESIS_BLOCK_HASH);
    }
    #[tokio::test]
    async fn headers() {
        let rest = new(TEST_ENDPOINT);
        let blockid = bitcoin::hash_types::BlockHash::from_str(GENESIS_BLOCK_HASH).unwrap();
        let headers = rest.headers(1, blockid).await.unwrap();
        assert_eq!(headers[0].block_hash().to_string(), GENESIS_BLOCK_HASH);
    }
    #[tokio::test]
    async fn chaininfo() {
        let rest = new(TEST_ENDPOINT);
        let chaininfo = rest.chaininfo().await.unwrap();
        assert_eq!(chaininfo.chain, "main");
    }
    #[tokio::test]
    async fn blockhashbyheight() {
        let rest = new(TEST_ENDPOINT);
        assert_eq!(rest.blockhashbyheight(0).await.unwrap().to_string(), GENESIS_BLOCK_HASH);
    }
    #[tokio::test]
    async fn utxos() {
        let rest = new(TEST_ENDPOINT);
        let utxos = rest.getutxos(true, &vec![
            bitcoin::hash_types::Txid::from_str(TXID_COINBASE_BLOCK1).unwrap(),
        ]).await.unwrap();
        assert!(utxos.chain_height > 0);
    }
}