-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlib.rs
355 lines (303 loc) · 11 KB
/
lib.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
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
mod error;
mod event;
pub mod model;
mod signer;
use std::rc::Rc;
use std::str::FromStr;
use crate::event::{EventListener, WasmEventListener};
use crate::model::*;
use anyhow::anyhow;
use breez_sdk_liquid::persist::Persister;
use breez_sdk_liquid::sdk::{LiquidSdk, LiquidSdkBuilder};
use breez_sdk_liquid::wallet::LiquidOnchainWallet;
use breez_sdk_liquid::PRODUCTION_BREEZSERVER_URL;
use log::Level;
use signer::{Signer, WasmSigner};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct BindingLiquidSdk {
sdk: Rc<LiquidSdk>,
}
#[wasm_bindgen(js_name = "connect")]
pub async fn connect(req: ConnectRequest) -> WasmResult<BindingLiquidSdk> {
let signer = Box::new(LiquidSdk::default_signer(&req.clone().into())?);
connect_inner(req.config, signer).await
}
#[wasm_bindgen(js_name = "connectWithSigner")]
pub async fn connect_with_signer(
req: ConnectWithSignerRequest,
signer: Signer,
) -> WasmResult<BindingLiquidSdk> {
let signer: Box<dyn breez_sdk_liquid::model::Signer> = Box::new(WasmSigner { signer });
connect_inner(req.config, signer).await
}
async fn connect_inner(
config: Config,
signer: Box<dyn breez_sdk_liquid::model::Signer>,
) -> WasmResult<BindingLiquidSdk> {
let config: breez_sdk_liquid::model::Config = config.into();
let signer = Rc::new(signer);
let mut sdk_builder = LiquidSdkBuilder::new(
config.clone(),
PRODUCTION_BREEZSERVER_URL.to_string(),
Rc::clone(&signer),
)?;
let persister = Rc::new(Persister::new_in_memory(
&config.working_dir,
config.network,
config.sync_enabled(),
config.asset_metadata.clone(),
)?);
let onchain_wallet = Rc::new(LiquidOnchainWallet::new_in_memory(
config,
Rc::clone(&persister),
signer,
)?);
sdk_builder.persister(persister);
sdk_builder.onchain_wallet(onchain_wallet);
let sdk = sdk_builder.build()?;
sdk.start().await?;
Ok(BindingLiquidSdk { sdk })
}
#[wasm_bindgen(js_name = "defaultConfig")]
pub fn default_config(network: LiquidNetwork, breez_api_key: Option<String>) -> WasmResult<Config> {
let config = match network {
LiquidNetwork::Mainnet => breez_sdk_liquid::model::Config::mainnet_esplora(breez_api_key),
LiquidNetwork::Testnet => breez_sdk_liquid::model::Config::testnet_esplora(breez_api_key),
LiquidNetwork::Regtest => breez_sdk_liquid::model::Config::regtest_esplora(),
};
Ok(config.into())
}
#[wasm_bindgen(js_name = "parseInvoice")]
pub fn parse_invoice(input: String) -> WasmResult<LNInvoice> {
Ok(LiquidSdk::parse_invoice(&input)?.into())
}
#[wasm_bindgen(js_name = "initLogger")]
pub fn init_logger(level: String) -> WasmResult<()> {
Ok(console_log::init_with_level(Level::from_str(&level)?)
.map_err(|_| anyhow!("Logger already created"))?)
}
#[wasm_bindgen]
impl BindingLiquidSdk {
#[wasm_bindgen(js_name = "getInfo")]
pub async fn get_info(&self) -> WasmResult<GetInfoResponse> {
Ok(self.sdk.get_info().await?.into())
}
#[wasm_bindgen(js_name = "signMessage")]
pub fn sign_message(&self, req: SignMessageRequest) -> WasmResult<SignMessageResponse> {
Ok(self.sdk.sign_message(&req.into())?.into())
}
#[wasm_bindgen(js_name = "checkMessage")]
pub fn check_message(&self, req: CheckMessageRequest) -> WasmResult<CheckMessageResponse> {
Ok(self.sdk.check_message(&req.into())?.into())
}
#[wasm_bindgen(js_name = "parse")]
pub async fn parse(&self, input: String) -> WasmResult<InputType> {
Ok(self.sdk.parse(&input).await?.into())
}
#[wasm_bindgen(js_name = "addEventListener")]
pub async fn add_event_listener(&self, listener: EventListener) -> WasmResult<String> {
Ok(self
.sdk
.add_event_listener(Box::new(WasmEventListener { listener }))
.await?)
}
#[wasm_bindgen(js_name = "removeEventListener")]
pub async fn remove_event_listener(&self, id: String) -> WasmResult<()> {
self.sdk.remove_event_listener(id).await?;
Ok(())
}
#[wasm_bindgen(js_name = "prepareSendPayment")]
pub async fn prepare_send_payment(
&self,
req: PrepareSendRequest,
) -> WasmResult<PrepareSendResponse> {
Ok(self.sdk.prepare_send_payment(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "sendPayment")]
pub async fn send_payment(&self, req: SendPaymentRequest) -> WasmResult<SendPaymentResponse> {
Ok(self.sdk.send_payment(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "prepareReceivePayment")]
pub async fn prepare_receive_payment(
&self,
req: PrepareReceiveRequest,
) -> WasmResult<PrepareReceiveResponse> {
Ok(self.sdk.prepare_receive_payment(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "receivePayment")]
pub async fn receive_payment(
&self,
req: ReceivePaymentRequest,
) -> WasmResult<ReceivePaymentResponse> {
Ok(self.sdk.receive_payment(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "fetchLightningLimits")]
pub async fn fetch_lightning_limits(&self) -> WasmResult<LightningPaymentLimitsResponse> {
Ok(self.sdk.fetch_lightning_limits().await?.into())
}
#[wasm_bindgen(js_name = "fetchOnchainLimits")]
pub async fn fetch_onchain_limits(&self) -> WasmResult<OnchainPaymentLimitsResponse> {
Ok(self.sdk.fetch_onchain_limits().await?.into())
}
#[wasm_bindgen(js_name = "preparePayOnchain")]
pub async fn prepare_pay_onchain(
&self,
req: PreparePayOnchainRequest,
) -> WasmResult<PreparePayOnchainResponse> {
Ok(self.sdk.prepare_pay_onchain(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "payOnchain")]
pub async fn pay_onchain(&self, req: PayOnchainRequest) -> WasmResult<SendPaymentResponse> {
Ok(self.sdk.pay_onchain(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "prepareBuyBitcoin")]
pub async fn prepare_buy_bitcoin(
&self,
req: PrepareBuyBitcoinRequest,
) -> WasmResult<PrepareBuyBitcoinResponse> {
Ok(self.sdk.prepare_buy_bitcoin(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "buyBitcoin")]
pub async fn buy_bitcoin(&self, req: BuyBitcoinRequest) -> WasmResult<String> {
Ok(self.sdk.buy_bitcoin(&req.into()).await?)
}
#[wasm_bindgen(js_name = "listPayments")]
pub async fn list_payments(&self, req: ListPaymentsRequest) -> WasmResult<Vec<Payment>> {
Ok(self
.sdk
.list_payments(&req.into())
.await?
.into_iter()
.map(|r| r.into())
.collect())
}
#[wasm_bindgen(js_name = "getPayment")]
pub async fn get_payment(&self, req: GetPaymentRequest) -> WasmResult<Option<Payment>> {
Ok(self.sdk.get_payment(&req.into()).await?.map(|r| r.into()))
}
#[wasm_bindgen(js_name = "fetchPaymentProposedFees")]
pub async fn fetch_payment_proposed_fees(
&self,
req: FetchPaymentProposedFeesRequest,
) -> WasmResult<FetchPaymentProposedFeesResponse> {
Ok(self
.sdk
.fetch_payment_proposed_fees(&req.into())
.await?
.into())
}
#[wasm_bindgen(js_name = "acceptPaymentProposedFees")]
pub async fn accept_payment_proposed_fees(
&self,
req: AcceptPaymentProposedFeesRequest,
) -> WasmResult<()> {
self.sdk.accept_payment_proposed_fees(&req.into()).await?;
Ok(())
}
#[wasm_bindgen(js_name = "prepareLnurlPay")]
pub async fn prepare_lnurl_pay(
&self,
req: PrepareLnUrlPayRequest,
) -> WasmResult<PrepareLnUrlPayResponse> {
Ok(self.sdk.prepare_lnurl_pay(req.into()).await?.into())
}
#[wasm_bindgen(js_name = "lnurlPay")]
pub async fn lnurl_pay(&self, req: LnUrlPayRequest) -> WasmResult<LnUrlPayResult> {
Ok(self.sdk.lnurl_pay(req.into()).await?.into())
}
#[wasm_bindgen(js_name = "lnurlWithdraw")]
pub async fn lnurl_withdraw(
&self,
req: LnUrlWithdrawRequest,
) -> WasmResult<LnUrlWithdrawResult> {
Ok(self.sdk.lnurl_withdraw(req.into()).await?.into())
}
#[wasm_bindgen(js_name = "lnurlAuth")]
pub async fn lnurl_auth(
&self,
req_data: LnUrlAuthRequestData,
) -> WasmResult<LnUrlCallbackStatus> {
Ok(self.sdk.lnurl_auth(req_data.into()).await?.into())
}
#[wasm_bindgen(js_name = "registerWebhook")]
pub async fn register_webhook(&self, webhook_url: String) -> WasmResult<()> {
self.sdk.register_webhook(webhook_url).await?;
Ok(())
}
#[wasm_bindgen(js_name = "unregisterWebhook")]
pub async fn unregister_webhook(&self) -> WasmResult<()> {
self.sdk.unregister_webhook().await?;
Ok(())
}
#[wasm_bindgen(js_name = "fetchFiatRates")]
pub async fn fetch_fiat_rates(&self) -> WasmResult<Vec<Rate>> {
Ok(self
.sdk
.fetch_fiat_rates()
.await?
.into_iter()
.map(|r| r.into())
.collect())
}
#[wasm_bindgen(js_name = "listFiatCurrencies")]
pub async fn list_fiat_currencies(&self) -> WasmResult<Vec<FiatCurrency>> {
Ok(self
.sdk
.list_fiat_currencies()
.await?
.into_iter()
.map(|r| r.into())
.collect())
}
#[wasm_bindgen(js_name = "listRefundables")]
pub async fn list_refundables(&self) -> WasmResult<Vec<RefundableSwap>> {
Ok(self
.sdk
.list_refundables()
.await?
.into_iter()
.map(|r| r.into())
.collect())
}
#[wasm_bindgen(js_name = "prepareRefund")]
pub async fn prepare_refund(
&self,
req: PrepareRefundRequest,
) -> WasmResult<PrepareRefundResponse> {
Ok(self.sdk.prepare_refund(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "refund")]
pub async fn refund(&self, req: RefundRequest) -> WasmResult<RefundResponse> {
Ok(self.sdk.refund(&req.into()).await?.into())
}
#[wasm_bindgen(js_name = "rescanOnchainSwaps")]
pub async fn rescan_onchain_swaps(&self) -> WasmResult<()> {
self.sdk.rescan_onchain_swaps().await?;
Ok(())
}
#[wasm_bindgen(js_name = "sync")]
pub async fn sync(&self) -> WasmResult<()> {
self.sdk.sync(false).await?;
Ok(())
}
#[wasm_bindgen(js_name = "recommendedFees")]
pub async fn recommended_fees(&self) -> WasmResult<RecommendedFees> {
Ok(self.sdk.recommended_fees().await?.into())
}
#[wasm_bindgen(js_name = "backup")]
pub fn backup(&self, req: BackupRequest) -> WasmResult<()> {
self.sdk.backup(req.into())?;
Ok(())
}
#[wasm_bindgen(js_name = "restore")]
pub fn restore(&self, req: RestoreRequest) -> WasmResult<()> {
self.sdk.restore(req.into())?;
Ok(())
}
#[wasm_bindgen(js_name = "disconnect")]
pub async fn disconnect(&self) -> WasmResult<()> {
self.sdk.disconnect().await?;
Ok(())
}
}