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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
extern crate exonum_jsonrpc;
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate log;
extern crate failure;
#[macro_use] 
extern crate failure_derive;
#[macro_use] 
extern crate display_derive;

use std::collections::BTreeMap;
use std::io;

use serde::{Serialize, Deserialize};
use serde_json::value::Value;

use exonum_jsonrpc::client::Client as RpcClient;
pub use exonum_jsonrpc::error::Error as RpcError;

#[derive(Fail, Debug, Display)]
pub enum Error {
    #[display(fmt = "No information. {}", _0)]
    NoInformation(String),
    #[display(fmt = "Memory pool error. {}", _0)]
    Memory(String),
    #[display(fmt = "Transaction is incorrect. {}", _0)]
    TransactionIncorrect(String),
    #[display(fmt = "Transaction rejected. {}", _0)]
    TransactionRejected(String),
    #[display(fmt = "Insufficient funds.")]
    InsufficientFunds,
    #[display(fmt = "Transaction already in chain.")]
    TransactionAlreadyInChain,
    #[display(fmt = "{}", _0)]
    Rpc(RpcError),
    #[display(fmt = "{}", _0)]
    Other(io::Error)
}

pub type Result<T> = ::std::result::Result<T, Error>;
pub type Params = Vec<Value>;

impl Error {
    pub fn incorrect_transaction<S: Into<String>>(s: S) -> Error {
        Error::TransactionIncorrect(s.into())
    }
}

impl From<RpcError> for Error {
    fn from(e: RpcError) -> Error {
        match e {
            exonum_jsonrpc::Error::Rpc(value) => {
                if let Some(code) = value.pointer("/code").and_then(Value::as_i64) {
                    let msg = value
                        .pointer("/message")
                        .and_then(Value::as_str)
                        .unwrap_or_else(|| "")
                        .into();

                    match code {
                        -5 => return Error::NoInformation(msg),
                        -6 => return Error::InsufficientFunds,
                        -7 => return Error::Memory(msg),
                        -25 => return Error::TransactionIncorrect(msg),
                        -26 => return Error::TransactionRejected(msg),
                        -27 => return Error::TransactionAlreadyInChain,
                        _ => {}
                    }
                }
                Error::Rpc(RpcError::Rpc(value))
            }
            e => Error::Rpc(e),
        }
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Other(e)
    }
}

pub struct Client {
    inner: RpcClient,
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("BitcoinRpcClient").finish()
    }
}

#[derive(Clone, Deserialize, Debug)]
pub struct Info {
    pub version: u32,
    pub protocolversion: u32,
    pub walletversion: u32,
    pub balance: f64,
    pub blocks: u64,
    pub timeoffset: u64,
    pub connections: u32,
    pub proxy: String,
    pub difficulty: f64,
    pub testnet: bool,
    pub keypoololdest: u64,
    pub keypoolsize: u64,
    pub paytxfee: f64,
    pub relayfee: f64,
    pub errors: String,
}

#[derive(Clone, Deserialize, Debug)]
pub struct AddressInfo {
    pub isvalid: bool,
    pub address: String,
    #[serde(rename = "scriptPubKey")]
    pub script_pubkey: String,
    pub ismine: bool,
    pub iswatchonly: bool,
    pub isscript: bool,
    pub pubkey: String,
    pub iscompressed: bool,
    pub account: Option<String>,
    pub hdkeypath: String,
    pub hdmasterkeyid: String,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct MultiSig {
    pub address: String,
    #[serde(rename = "redeemScript")]
    pub redeem_script: String,
}

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

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct ScriptPubKey {
    pub asm: String,
    pub hex: String,
    #[serde(rename = "reqSigs")]
    pub req_sigs: Option<u64>,
    #[serde(rename = "type")]
    pub key_type: String,
    pub addresses: Option<Vec<String>>,
}

// TODO use TxIn from bitcoin crate
#[derive(Clone, Deserialize, Debug)]
pub struct TxIn {
    pub txid: String,
    pub vout: u32,
    #[serde(rename = "scriptSig")]
    pub script_sig: ScriptSig,
    pub sequence: u64,
    pub txinwitness: Option<Vec<String>>,
}

#[derive(Clone, Deserialize, Debug)]
pub struct TxOut {
    pub value: f64,
    pub n: u32,
    #[serde(rename = "scriptPubKey")]
    pub script_pubkey: ScriptPubKey,
}

#[derive(Clone, Deserialize, Debug)]
pub struct RawTransactionInfo {
    pub hex: Option<String>,
    pub txid: String,
    pub hash: String,
    pub size: u64,
    pub vsize: u64,
    pub version: u32,
    pub locktime: u32,
    pub vin: Vec<TxIn>,
    pub vout: Vec<TxOut>,
    pub confirmations: Option<u64>,
}

#[derive(Clone, Deserialize, Debug)]
pub struct UnspentTransactionInfo {
    pub txid: String,
    pub vout: u32,
    pub address: String,
    pub account: String,
    #[serde(rename = "scriptPubKey")]
    pub script_pubkey: String,
    #[serde(rename = "redeemScript")]
    pub redeem_script: Option<String>,
    pub amount: f64,
    pub confirmations: u64,
    pub spendable: bool,
    pub solvable: bool,
}

#[derive(Clone, Serialize, Debug)]
pub struct DependentOutput {
    pub txid: String,
    pub vout: u32,
    #[serde(rename = "scriptPubKey")]
    pub script_pubkey: String,
    #[serde(rename = "redeemScript")]
    pub redeem_script: String,
}

#[derive(Clone, Deserialize, Debug)]
pub struct SignTxOutput {
    pub hex: String,
    pub complete: bool,
}

#[derive(Clone, Serialize, Debug)]
pub struct TransactionInput {
    pub txid: String,
    pub vout: u32,
    pub sequence: Option<u64>,
}

#[derive(Clone, Deserialize, Debug)]
pub struct TransactionOutput {
    pub address: String,
    pub value: String,
}

#[derive(Clone, Deserialize, Debug)]
pub struct TransactionInfo {
    pub address: Option<String>,
    pub vout: u32,
    pub confirmations: u64,
    pub txid: String,
    pub abandoned: Option<bool>,
    pub time: u64,
}

#[derive(Debug)]
struct RpcRequest {
    method: String,
    params: Params,
    response: Result<Value>,
}

impl Client {
    pub fn new<S>(url: S, user: Option<String>, password: Option<String>) -> Client
    where
        S: Into<String>,
    {
        Client { inner: RpcClient::new(url.into(), user.map(Into::into), password.map(Into::into)) }
    }

    pub fn url(&self) -> &str {
        self.inner.url()
    }
    pub fn password(&self) -> &Option<String> {
        self.inner.password()
    }
    pub fn username(&self) -> &Option<String> {
        self.inner.username()
    }

    fn request<T>(&self, method: &str, params: Params) -> Result<T>
    where
        for<'de> T: Deserialize<'de>,
    {
        let request = self.inner.build_request(method.into(), params);
        let response = self.inner.send_request(&request)?;
        trace!(
            "{:#?}",
            RpcRequest {
                method: request.method.clone(),
                params: request.params.clone(),
                response: response.clone().into_result::<Value>().map_err(Error::from),
            }
        );
        response.into_result::<T>().map_err(Error::from)
    }
}

// public api part
impl Client {
    pub fn getinfo(&self) -> Result<Info> {
        self.request("getinfo", Vec::new())
    }

    pub fn getnewaddress(&self) -> Result<String> {
        self.request("getnewaddress", vec![])
    }

    pub fn validateaddress(&self, addr: &str) -> Result<AddressInfo> {
        self.request("validateaddress", vec![Value::String(addr.to_owned())])
    }

    pub fn createmultisig<V: AsRef<[String]>>(&self, signs: u8, addrs: V) -> Result<MultiSig> {
        let n = serde_json::to_value(signs).unwrap();
        let addrs = serde_json::to_value(addrs.as_ref()).unwrap();
        self.request("createmultisig", vec![n, addrs])
    }

    pub fn sendtoaddress(&self, addr: &str, amount: &str) -> Result<String> {
        let params = vec![
            serde_json::to_value(addr).unwrap(),
            serde_json::to_value(amount).unwrap(),
        ];
        self.request("sendtoaddress", params)
    }

    pub fn getrawtransaction(&self, txid: &str) -> Result<String> {
        let params = json!([txid, 0]).as_array().cloned().unwrap();
        self.request("getrawtransaction", params)
    }

    pub fn getrawtransaction_verbose(&self, txid: &str) -> Result<RawTransactionInfo> {
        let params = json!([txid, 1]).as_array().cloned().unwrap();
        self.request("getrawtransaction", params)
    }

    pub fn createrawtransaction<T, O>(
        &self,
        transactions: T,
        outputs: O,
        data: Option<String>,
    ) -> Result<String>
    where
        T: AsRef<[TransactionInput]>,
        O: AsRef<[TransactionOutput]>,
    {
        let mut map = BTreeMap::new();
        map.extend(outputs.as_ref().iter().map(|x| {
            (x.address.clone(), x.value.clone())
        }));
        if let Some(data) = data {
            map.insert("data".into(), data);
        }

        let params = json!([transactions.as_ref(), map])
            .as_array()
            .cloned()
            .unwrap();
        self.request("createrawtransaction", params)
    }

    pub fn dumpprivkey(&self, pub_key: &str) -> Result<String> {
        let params = json!([pub_key]).as_array().cloned().unwrap();
        self.request("dumpprivkey", params)
    }

    pub fn signrawtransaction<O, K>(
        &self,
        txhex: &str,
        outputs: O,
        priv_keys: K,
    ) -> Result<SignTxOutput>
    where
        O: AsRef<[DependentOutput]>,
        K: AsRef<[String]>,
    {
        let params = json!([txhex, outputs.as_ref(), priv_keys.as_ref()])
            .as_array()
            .cloned()
            .unwrap();
        self.request("signrawtransaction", params)
    }

    pub fn sendrawtransaction(&self, txhex: &str) -> Result<String> {
        self.request(
            "sendrawtransaction",
            vec![serde_json::to_value(txhex).unwrap()],
        )
    }

    pub fn decoderawtransaction(&self, txhex: &str) -> Result<RawTransactionInfo> {
        self.request(
            "decoderawtransaction",
            vec![serde_json::to_value(txhex).unwrap()],
        )
    }

    pub fn addwitnessaddress(&self, addr: &str) -> Result<String> {
        self.request(
            "addwitnessaddress",
            vec![serde_json::to_value(addr).unwrap()],
        )
    }

    pub fn listtransactions(
        &self,
        count: u32,
        from: u32,
        include_watch_only: bool,
    ) -> Result<Vec<TransactionInfo>> {
        let params = json!(["*", count, from, include_watch_only])
            .as_array()
            .cloned()
            .unwrap();
        self.request("listtransactions", params)
    }

    pub fn listunspent<V: AsRef<str> + Serialize>(
        &self,
        min_confirmations: u32,
        max_confirmations: u32,
        addresses: &[V],
    ) -> Result<Vec<UnspentTransactionInfo>> {
        let params = json!([min_confirmations, max_confirmations, addresses])
            .as_array()
            .cloned()
            .unwrap();
        self.request("listunspent", params)
    }

    pub fn importaddress(&self, addr: &str, label: &str, rescan: bool, p2sh: bool) -> Result<()> {
        let params = json!([addr, label, rescan, p2sh])
            .as_array()
            .cloned()
            .unwrap();
        // special case for decode {"result":null}
        let r: Result<Option<bool>> = self.request("importaddress", params);
        match r {
            Ok(_) |
            Err(Error::Rpc(RpcError::NoErrorOrResult)) => Ok(()),
            Err(e) => Err(e),
        }
    }

    pub fn generate(&self, nblocks: u64, maxtries: u64) -> Result<Vec<String>> {
        let params = json!([nblocks, maxtries]).as_array().cloned().unwrap();
        self.request("generate", params)
    }

    pub fn generatetoaddress(
        &self,
        nblocks: u64,
        addr: &str,
        maxtries: u64,
    ) -> Result<Vec<String>> {
        let params = json!([nblocks, addr, maxtries])
            .as_array()
            .cloned()
            .unwrap();
        self.request("generatetoaddress", params)
    }

    pub fn stop(&self) -> Result<String> {
        self.request("stop", vec![])
    }

    pub fn getreceivedbyaddress(&self, addr: &str, minconf: u64) -> Result<f64> {
        let params = json!([addr, minconf]).as_array().cloned().unwrap();
        self.request("getreceivedbyaddress", params)
    }

    pub fn getblockcount(&self) -> Result<u64> {
        self.request("getblockcount", vec![])
    }

    pub fn getbestblockhash(&self) -> Result<String> {
        self.request("getbestblockhash", vec![])
    }

    pub fn getblockhash(&self, height: u64) -> Result<String> {
        let params = json!([height])
            .as_array()
            .cloned()
            .unwrap();
        self.request("getblockhash", params)
    }

    pub fn getblock<S: AsRef<str> + Serialize>(&self, hash: S) -> Result<String> {
        let params = json!([hash.as_ref(), 0])
            .as_array()
            .cloned()
            .unwrap();
        self.request("getblock", params)
    }
}