fedimint_wasm_tests/
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
356
#![deny(clippy::pedantic)]

use std::sync::Arc;

use anyhow::Result;
use fedimint_client::secret::{PlainRootSecretStrategy, RootSecretStrategy};
use fedimint_client::Client;
use fedimint_core::db::mem_impl::MemDatabase;
use fedimint_core::db::Database;
use fedimint_core::invite_code::InviteCode;
use fedimint_ln_client::{LightningClientInit, LightningClientModule};
use fedimint_mint_client::MintClientInit;
use fedimint_wallet_client::WalletClientInit;
use rand::thread_rng;

async fn load_or_generate_mnemonic(db: &Database) -> anyhow::Result<[u8; 64]> {
    Ok(
        if let Ok(s) = Client::load_decodable_client_secret(db).await {
            s
        } else {
            let secret = PlainRootSecretStrategy::random(&mut thread_rng());
            Client::store_encodable_client_secret(db, secret).await?;
            secret
        },
    )
}

async fn make_client_builder() -> Result<fedimint_client::ClientBuilder> {
    let mem_database = MemDatabase::default();
    let mut builder = fedimint_client::Client::builder(mem_database.into()).await?;
    builder.with_module(LightningClientInit::default());
    builder.with_module(MintClientInit);
    builder.with_module(WalletClientInit::default());
    builder.with_primary_module(1);

    Ok(builder)
}

async fn client(invite_code: &InviteCode) -> Result<fedimint_client::ClientHandleArc> {
    let client_config = fedimint_api_client::api::net::Connector::default()
        .download_from_invite_code(invite_code)
        .await?;
    let mut builder = make_client_builder().await?;
    let client_secret = load_or_generate_mnemonic(builder.db_no_decoders()).await?;
    builder.stopped();
    let client = builder
        .join(
            PlainRootSecretStrategy::to_root_secret(&client_secret),
            client_config.clone(),
            None,
        )
        .await
        .map(Arc::new)?;
    if let Ok(ln_client) = client.get_first_module::<LightningClientModule>() {
        let _ = ln_client.update_gateway_cache().await;
    }
    Ok(client)
}

mod faucet {
    pub async fn invite_code() -> anyhow::Result<String> {
        let resp = gloo_net::http::Request::get("http://localhost:15243/connect-string")
            .send()
            .await?;
        if resp.ok() {
            Ok(resp.text().await?)
        } else {
            anyhow::bail!(resp.text().await?);
        }
    }

    pub async fn pay_invoice(invoice: &str) -> anyhow::Result<()> {
        let resp = gloo_net::http::Request::post("http://localhost:15243/pay")
            .body(invoice)?
            .send()
            .await?;
        if resp.ok() {
            Ok(())
        } else {
            anyhow::bail!(resp.text().await?);
        }
    }

    pub async fn gateway_api() -> anyhow::Result<String> {
        let resp = gloo_net::http::Request::get("http://localhost:15243/gateway-api")
            .send()
            .await?;
        if resp.ok() {
            Ok(resp.text().await?)
        } else {
            anyhow::bail!(resp.text().await?);
        }
    }

    pub async fn generate_invoice(amt: u64) -> anyhow::Result<String> {
        let resp = gloo_net::http::Request::post("http://localhost:15243/invoice")
            .body(amt)?
            .send()
            .await?;
        if resp.ok() {
            Ok(resp.text().await?)
        } else {
            anyhow::bail!(resp.text().await?);
        }
    }
}

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
mod tests {
    use std::time::Duration;

    use anyhow::{anyhow, bail};
    use fedimint_client::derivable_secret::DerivableSecret;
    use fedimint_core::Amount;
    use fedimint_ln_client::{
        LightningClientModule, LnPayState, LnReceiveState, OutgoingLightningPayment, PayType,
    };
    use fedimint_ln_common::lightning_invoice::{Bolt11InvoiceDescription, Description};
    use fedimint_ln_common::LightningGateway;
    use fedimint_mint_client::{
        MintClientModule, ReissueExternalNotesState, SelectNotesWithAtleastAmount, SpendOOBState,
    };
    use futures::StreamExt;
    use wasm_bindgen_test::wasm_bindgen_test;

    use super::{client, faucet, Result};

    #[wasm_bindgen_test]
    async fn build_client() -> Result<()> {
        let _client = client(&faucet::invite_code().await?.parse()?).await?;
        Ok(())
    }

    async fn get_gateway(
        client: &fedimint_client::ClientHandleArc,
    ) -> anyhow::Result<LightningGateway> {
        let lightning_module = client.get_first_module::<LightningClientModule>()?;
        let gws = lightning_module.list_gateways().await;
        let gw_api = faucet::gateway_api().await?;
        let lnd_gw = gws
            .into_iter()
            .find(|x| x.info.api.to_string() == gw_api)
            .expect("no gateway with api");

        Ok(lnd_gw.info)
    }

    #[wasm_bindgen_test]
    async fn receive() -> Result<()> {
        let client = client(&faucet::invite_code().await?.parse()?).await?;
        client.start_executor().await;
        let ln_gateway = get_gateway(&client).await?;
        futures::future::try_join_all(
            (0..10)
                .map(|_| receive_once(client.clone(), Amount::from_sats(21), ln_gateway.clone())),
        )
        .await?;
        Ok(())
    }

    async fn receive_once(
        client: fedimint_client::ClientHandleArc,
        amount: Amount,
        gateway: LightningGateway,
    ) -> Result<()> {
        let lightning_module = client.get_first_module::<LightningClientModule>()?;
        let desc = Description::new("test".to_string())?;
        let (opid, invoice, _) = lightning_module
            .create_bolt11_invoice(
                amount,
                Bolt11InvoiceDescription::Direct(&desc),
                None,
                (),
                Some(gateway),
            )
            .await?;
        faucet::pay_invoice(&invoice.to_string()).await?;

        let mut updates = lightning_module
            .subscribe_ln_receive(opid)
            .await?
            .into_stream();
        while let Some(update) = updates.next().await {
            match update {
                LnReceiveState::Claimed => return Ok(()),
                LnReceiveState::Canceled { reason } => {
                    return Err(reason.into());
                }
                _ => {}
            }
        }
        Err(anyhow!("Lightning receive failed"))
    }

    // Tests that ChaCha20 crypto functions used for backup and recovery are
    // available in WASM at runtime. Related issue: https://github.com/fedimint/fedimint/issues/2843
    #[wasm_bindgen_test]
    async fn derive_chacha_key() {
        let root_secret = DerivableSecret::new_root(&[0x42; 32], &[0x2a; 32]);
        let key = root_secret.to_chacha20_poly1305_key();

        // Prevent optimization
        // FIXME: replace with `std::hint::black_box` once stabilized
        assert!(format!("key: {key:?}").len() > 8);
    }

    async fn pay_once(
        client: fedimint_client::ClientHandleArc,
        ln_gateway: LightningGateway,
    ) -> Result<(), anyhow::Error> {
        let lightning_module = client.get_first_module::<LightningClientModule>()?;
        let bolt11 = faucet::generate_invoice(11).await?;
        let OutgoingLightningPayment {
            payment_type,
            contract_id: _,
            fee: _,
        } = lightning_module
            .pay_bolt11_invoice(Some(ln_gateway), bolt11.parse()?, ())
            .await?;
        let PayType::Lightning(operation_id) = payment_type else {
            unreachable!("paying invoice over lightning");
        };
        let lightning_module = client.get_first_module::<LightningClientModule>()?;
        let mut updates = lightning_module
            .subscribe_ln_pay(operation_id)
            .await?
            .into_stream();
        loop {
            match updates.next().await {
                Some(LnPayState::Success { preimage: _ }) => {
                    break;
                }
                Some(LnPayState::Refunded { gateway_error }) => {
                    return Err(anyhow!("refunded {gateway_error}"));
                }
                None => return Err(anyhow!("Lightning send failed")),
                _ => {}
            }
        }
        Ok(())
    }

    #[wasm_bindgen_test]
    async fn receive_and_pay() -> Result<()> {
        let client = client(&faucet::invite_code().await?.parse()?).await?;
        client.start_executor().await;
        let ln_gateway = get_gateway(&client).await?;

        futures::future::try_join_all(
            (0..10)
                .map(|_| receive_once(client.clone(), Amount::from_sats(21), ln_gateway.clone())),
        )
        .await?;
        futures::future::try_join_all(
            (0..10).map(|_| pay_once(client.clone(), ln_gateway.clone())),
        )
        .await?;

        Ok(())
    }

    async fn send_and_recv_ecash_once(
        client: fedimint_client::ClientHandleArc,
    ) -> Result<(), anyhow::Error> {
        let mint = client.get_first_module::<MintClientModule>()?;
        let (_, notes) = mint
            .spend_notes_with_selector(
                &SelectNotesWithAtleastAmount,
                Amount::from_sats(11),
                Duration::from_secs(10000),
                false,
                (),
            )
            .await?;
        let operation_id = mint.reissue_external_notes(notes, ()).await?;
        let mut updates = mint
            .subscribe_reissue_external_notes(operation_id)
            .await?
            .into_stream();
        loop {
            match updates.next().await {
                Some(ReissueExternalNotesState::Done) => {
                    break;
                }
                Some(ReissueExternalNotesState::Failed(error)) => {
                    return Err(anyhow!("reissue failed {error}"));
                }
                None => return Err(anyhow!("reissue failed")),
                _ => {}
            }
        }
        Ok(())
    }

    async fn send_ecash_exact(
        client: fedimint_client::ClientHandleArc,
        amount: Amount,
    ) -> Result<(), anyhow::Error> {
        let mint = client.get_first_module::<MintClientModule>()?;
        'retry: loop {
            let (operation_id, notes) = mint
                .spend_notes_with_selector(
                    &SelectNotesWithAtleastAmount,
                    amount,
                    Duration::from_secs(10000),
                    false,
                    (),
                )
                .await?;
            if notes.total_amount() == amount {
                return Ok(());
            }
            mint.try_cancel_spend_notes(operation_id).await;
            let mut updates = mint
                .subscribe_spend_notes(operation_id)
                .await?
                .into_stream();
            while let Some(update) = updates.next().await {
                if update == SpendOOBState::UserCanceledSuccess {
                    continue 'retry;
                }
            }
            bail!("failed to cancel notes");
        }
    }

    #[wasm_bindgen_test]
    async fn test_ecash() -> Result<()> {
        let client = client(&faucet::invite_code().await?.parse()?).await?;
        client.start_executor().await;
        let ln_gateway = get_gateway(&client).await?;

        futures::future::try_join_all(
            (0..10)
                .map(|_| receive_once(client.clone(), Amount::from_sats(21), ln_gateway.clone())),
        )
        .await?;
        futures::future::try_join_all((0..10).map(|_| send_and_recv_ecash_once(client.clone())))
            .await?;
        Ok(())
    }

    #[wasm_bindgen_test]
    async fn test_ecash_exact() -> Result<()> {
        let client = client(&faucet::invite_code().await?.parse()?).await?;
        client.start_executor().await;
        let ln_gateway = get_gateway(&client).await?;

        receive_once(client.clone(), Amount::from_sats(100), ln_gateway).await?;
        futures::future::try_join_all(
            (0..3).map(|_| send_ecash_exact(client.clone(), Amount::from_sats(1))),
        )
        .await?;
        Ok(())
    }
}