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
use exonum::crypto::Hash;
use bitcoin::util::address::Address;
use exonum_bitcoinrpc as bitcoin_rpc;
use failure;
use hex::FromHex;
use serde_derive::{Deserialize, Serialize};
use crate::btc::Transaction;
#[derive(Debug, Clone, PartialEq)]
pub struct TransactionInfo {
pub content: Transaction,
pub confirmations: u64,
}
pub trait BtcRelay: Send + Sync + ::std::fmt::Debug {
fn send_to_address(&self, addr: &Address, satoshis: u64)
-> Result<Transaction, failure::Error>;
fn transaction_info(&self, id: &Hash) -> Result<Option<TransactionInfo>, failure::Error>;
fn send_transaction(&self, transaction: &Transaction) -> Result<Hash, failure::Error>;
fn watch_address(&self, addr: &Address, rescan: bool) -> Result<(), failure::Error>;
fn config(&self) -> BitcoinRpcConfig;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct BitcoinRpcConfig {
pub host: String,
pub username: Option<String>,
pub password: Option<String>,
}
const SATOSHI_DIVISOR: f64 = 100_000_000.0;
#[derive(Debug)]
pub struct BitcoinRpcClient(bitcoin_rpc::Client);
impl BitcoinRpcClient {
pub fn new(config: BitcoinRpcConfig) -> Self {
let inner = bitcoin_rpc::Client::new(config.host, config.username, config.password);
BitcoinRpcClient(inner)
}
}
impl From<BitcoinRpcConfig> for BitcoinRpcClient {
fn from(cfg: BitcoinRpcConfig) -> Self {
Self::new(cfg)
}
}
impl From<BitcoinRpcClient> for Box<dyn BtcRelay> {
fn from(client: BitcoinRpcClient) -> Self {
Box::new(client) as Self
}
}
impl BtcRelay for BitcoinRpcClient {
fn send_to_address(
&self,
addr: &Address,
satoshis: u64,
) -> Result<Transaction, failure::Error> {
let amount = satoshis as f64 / SATOSHI_DIVISOR;
let txid = self
.0
.sendtoaddress(&addr.to_string(), &amount.to_string())?;
let tx_hex = self.0.getrawtransaction(&txid)?;
Transaction::from_hex(tx_hex).map_err(From::from)
}
fn transaction_info(&self, id: &Hash) -> Result<Option<TransactionInfo>, failure::Error> {
let txid = id.to_hex();
let txinfo = match self.0.getrawtransaction_verbose(&txid) {
Ok(info) => info,
Err(bitcoin_rpc::Error::NoInformation(_)) => return Ok(None),
Err(e) => Err(e)?,
};
let tx_hex = txinfo
.hex
.ok_or_else(|| bitcoin_rpc::Error::NoInformation(txid))?;
let content = Transaction::from_hex(tx_hex)?;
let confirmations = txinfo.confirmations.unwrap_or_default();
Ok(Some(TransactionInfo {
content,
confirmations,
}))
}
fn send_transaction(&self, transaction: &Transaction) -> Result<Hash, failure::Error> {
let txid = transaction.id();
self.0.sendrawtransaction(&transaction.to_string())?;
Ok(txid)
}
fn watch_address(&self, addr: &Address, rescan: bool) -> Result<(), failure::Error> {
self.0
.importaddress(&addr.to_string(), "multisig", false, rescan)
.map_err(From::from)
}
fn config(&self) -> BitcoinRpcConfig {
BitcoinRpcConfig {
host: self.0.url().to_string(),
username: self.0.username().clone(),
password: self.0.password().clone(),
}
}
}