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
use bitcoin::blockdata::block;
use bitcoin::hashes::hex::FromHex;
use bitcoin::hashes::{sha256, Hash};
use bitcoin::{Script, Txid};
use serde::{de, Deserialize, Serialize};
static JSONRPC_2_0: &str = "2.0";
#[derive(Serialize, Clone)]
#[serde(untagged)]
pub enum Param {
U32(u32),
Usize(usize),
String(String),
Bool(bool),
}
#[derive(Serialize, Clone)]
pub struct Request<'a> {
jsonrpc: &'static str,
pub id: usize,
pub method: &'a str,
pub params: Vec<Param>,
}
impl<'a> Request<'a> {
pub fn new(method: &'a str, params: Vec<Param>) -> Self {
Self {
id: 0,
jsonrpc: JSONRPC_2_0,
method,
params,
}
}
pub fn new_id(id: usize, method: &'a str, params: Vec<Param>) -> Self {
let mut instance = Self::new(method, params);
instance.id = id;
instance
}
}
pub type ScriptHash = [u8; 32];
pub type ScriptStatus = [u8; 32];
pub trait ToElectrumScriptHash {
fn to_electrum_scripthash(&self) -> ScriptHash;
}
impl ToElectrumScriptHash for Script {
fn to_electrum_scripthash(&self) -> ScriptHash {
let mut result = sha256::Hash::hash(self.as_bytes()).into_inner();
result.reverse();
result
}
}
fn from_hex<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: FromHex,
D: de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
T::from_hex(&s).map_err(de::Error::custom)
}
fn from_hex_array<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
T: FromHex + std::fmt::Debug,
D: de::Deserializer<'de>,
{
let arr = Vec::<String>::deserialize(deserializer)?;
let results: Vec<Result<T, _>> = arr
.into_iter()
.map(|s| T::from_hex(&s).map_err(de::Error::custom))
.collect();
let mut answer = Vec::new();
for x in results.into_iter() {
answer.push(x?);
}
Ok(answer)
}
fn from_hex_header<'de, D>(deserializer: D) -> Result<block::BlockHeader, D::Error>
where
D: de::Deserializer<'de>,
{
use bitcoin::consensus::deserialize;
let vec: Vec<u8> = from_hex(deserializer)?;
deserialize(&vec).map_err(de::Error::custom)
}
#[derive(Debug, Deserialize)]
pub struct GetHistoryRes {
pub height: i32,
pub tx_hash: Txid,
pub fee: Option<u64>,
}
#[derive(Debug, Deserialize)]
pub struct ListUnspentRes {
pub height: usize,
pub tx_hash: Txid,
pub tx_pos: usize,
pub value: u64,
}
#[derive(Debug, Deserialize)]
pub struct ServerFeaturesRes {
pub server_version: String,
#[serde(deserialize_with = "from_hex")]
pub genesis_hash: [u8; 32],
pub protocol_min: String,
pub protocol_max: String,
pub hash_function: Option<String>,
pub pruning: Option<i64>,
}
#[derive(Debug, Deserialize)]
pub struct GetHeadersRes {
pub max: usize,
pub count: usize,
#[serde(rename(deserialize = "hex"), deserialize_with = "from_hex")]
pub raw_headers: Vec<u8>,
#[serde(skip)]
pub headers: Vec<block::BlockHeader>,
}
#[derive(Debug, Deserialize)]
pub struct GetBalanceRes {
pub confirmed: u64,
pub unconfirmed: u64,
}
#[derive(Debug, Deserialize)]
pub struct GetMerkleRes {
pub block_height: usize,
pub pos: usize,
#[serde(deserialize_with = "from_hex_array")]
pub merkle: Vec<[u8; 32]>,
}
#[derive(Debug, Deserialize)]
pub struct HeaderNotification {
pub height: usize,
#[serde(rename = "hex", deserialize_with = "from_hex_header")]
pub header: block::BlockHeader,
}
#[derive(Debug, Deserialize)]
pub struct ScriptNotification {
pub scripthash: ScriptHash,
pub status: ScriptStatus,
}
#[derive(Debug)]
pub enum Error {
IOError(std::io::Error),
JSON(serde_json::error::Error),
Hex(bitcoin::hashes::hex::Error),
Protocol(serde_json::Value),
Bitcoin(bitcoin::consensus::encode::Error),
AlreadySubscribed(ScriptHash),
NotSubscribed(ScriptHash),
InvalidResponse(serde_json::Value),
Message(String),
InvalidDNSNameError(String),
MissingDomain,
#[cfg(feature = "use-openssl")]
InvalidSslMethod(openssl::error::ErrorStack),
#[cfg(feature = "use-openssl")]
SslHandshakeError(openssl::ssl::HandshakeError<std::net::TcpStream>),
}
macro_rules! impl_error {
( $from:ty, $to:ident ) => {
impl std::convert::From<$from> for Error {
fn from(err: $from) -> Self {
Error::$to(err)
}
}
};
}
impl_error!(std::io::Error, IOError);
impl_error!(serde_json::Error, JSON);
impl_error!(bitcoin::hashes::hex::Error, Hex);
impl_error!(bitcoin::consensus::encode::Error, Bitcoin);