forked from wizardsardine/liana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
199 lines (171 loc) · 5.64 KB
/
mod.rs
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
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::iter::FromIterator;
use liana::commands::{CoinStatus, CreateRecoveryResult};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::{error, info};
pub mod error;
pub mod jsonrpc;
use liana::{
commands::LabelItem,
config::Config,
miniscript::bitcoin::{address, psbt::Psbt, Address, OutPoint, Txid},
};
use super::{model::*, Daemon, DaemonError};
pub trait Client {
type Error: Into<DaemonError> + Debug;
fn request<S: Serialize + Debug, D: DeserializeOwned + Debug>(
&self,
method: &str,
params: Option<S>,
) -> Result<D, Self::Error>;
}
#[derive(Debug, Clone)]
pub struct Lianad<C: Client> {
client: C,
}
impl<C: Client> Lianad<C> {
pub fn new(client: C) -> Lianad<C> {
Lianad { client }
}
/// Generic call function for RPC calls.
fn call<T: Serialize + Debug, U: DeserializeOwned + Debug>(
&self,
method: &str,
input: Option<T>,
) -> Result<U, DaemonError> {
info!("{}", method);
self.client.request(method, input).map_err(|e| {
error!("method {} failed: {:?}", method, e);
e.into()
})
}
}
impl<C: Client + Debug> Daemon for Lianad<C> {
fn is_external(&self) -> bool {
true
}
fn config(&self) -> Option<&Config> {
None
}
fn is_alive(&self) -> Result<(), DaemonError> {
Ok(())
}
fn stop(&self) -> Result<(), DaemonError> {
unreachable!("GUI should not ask external client to stop")
}
fn get_info(&self) -> Result<GetInfoResult, DaemonError> {
self.call("getinfo", Option::<Request>::None)
}
fn get_new_address(&self) -> Result<GetAddressResult, DaemonError> {
self.call("getnewaddress", Option::<Request>::None)
}
fn list_coins(
&self,
statuses: &[CoinStatus],
outpoints: &[OutPoint],
) -> Result<ListCoinsResult, DaemonError> {
self.call(
"listcoins",
Some(vec![
json!(statuses.iter().map(|s| s.to_arg()).collect::<Vec<&str>>()),
json!(outpoints),
]),
)
}
fn list_spend_txs(&self) -> Result<ListSpendResult, DaemonError> {
self.call("listspendtxs", Option::<Request>::None)
}
fn create_spend_tx(
&self,
coins_outpoints: &[OutPoint],
destinations: &HashMap<Address<address::NetworkUnchecked>, u64>,
feerate_vb: u64,
change_address: Option<Address<address::NetworkUnchecked>>,
) -> Result<CreateSpendResult, DaemonError> {
let mut input = vec![
json!(destinations),
json!(coins_outpoints),
json!(feerate_vb),
];
if let Some(change_address) = change_address {
input.push(json!(change_address));
}
self.call("createspend", Some(input))
}
fn rbf_psbt(
&self,
txid: &Txid,
is_cancel: bool,
feerate_vb: Option<u64>,
) -> Result<CreateSpendResult, DaemonError> {
let mut input = vec![json!(txid.to_string()), json!(is_cancel)];
if let Some(feerate_vb) = feerate_vb {
input.push(json!(feerate_vb));
}
self.call("rbfpsbt", Some(input))
}
fn update_spend_tx(&self, psbt: &Psbt) -> Result<(), DaemonError> {
let spend_tx = psbt.to_string();
let _res: serde_json::value::Value = self.call("updatespend", Some(vec![spend_tx]))?;
Ok(())
}
fn delete_spend_tx(&self, txid: &Txid) -> Result<(), DaemonError> {
let _res: serde_json::value::Value =
self.call("delspendtx", Some(vec![txid.to_string()]))?;
Ok(())
}
fn broadcast_spend_tx(&self, txid: &Txid) -> Result<(), DaemonError> {
let _res: serde_json::value::Value =
self.call("broadcastspend", Some(vec![txid.to_string()]))?;
Ok(())
}
fn start_rescan(&self, t: u32) -> Result<(), DaemonError> {
let _res: serde_json::value::Value = self.call("startrescan", Some(vec![t]))?;
Ok(())
}
fn list_confirmed_txs(
&self,
start: u32,
end: u32,
limit: u64,
) -> Result<ListTransactionsResult, DaemonError> {
self.call(
"listconfirmed",
Some(vec![json!(start), json!(end), json!(limit)]),
)
}
fn list_txs(&self, txids: &[Txid]) -> Result<ListTransactionsResult, DaemonError> {
self.call("listtransactions", Some(vec![txids]))
}
fn create_recovery(
&self,
address: Address<address::NetworkUnchecked>,
feerate_vb: u64,
sequence: Option<u16>,
) -> Result<Psbt, DaemonError> {
let res: CreateRecoveryResult = self.call(
"createrecovery",
Some(vec![json!(address), json!(feerate_vb), json!(sequence)]),
)?;
Ok(res.psbt)
}
fn get_labels(
&self,
items: &HashSet<LabelItem>,
) -> Result<HashMap<String, String>, DaemonError> {
let items = items.iter().map(|a| a.to_string()).collect::<Vec<String>>();
let res: GetLabelsResult = self.call("getlabels", Some(vec![items]))?;
Ok(res.labels)
}
fn update_labels(&self, items: &HashMap<LabelItem, Option<String>>) -> Result<(), DaemonError> {
let labels: HashMap<String, Option<String>> =
HashMap::from_iter(items.iter().map(|(a, l)| (a.to_string(), l.clone())));
let _res: serde_json::value::Value = self.call("updatelabels", Some(vec![labels]))?;
Ok(())
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Request {}