diff --git a/Cargo.toml b/Cargo.toml index 095bf55..d6b89c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,3 +84,7 @@ required-features = ["client", "server"] [[test]] name = "lud16" required-features = ["client", "server"] + +[[test]] +name = "lud21" +required-features = ["client", "server"] diff --git a/README.md b/README.md index 51052d5..c9862ac 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ This library works as a toolkit so you can serve and make your LNURL requests wi - [LUD-18](https://github.com/lnurl/luds/blob/luds/18.md): πŸ†˜ core πŸ†˜ client πŸ†˜ server πŸ†˜ tests - [LUD-19](https://github.com/lnurl/luds/blob/luds/19.md): πŸ†˜ core πŸ†˜ client πŸ†˜ server πŸ†˜ tests - [LUD-20](https://github.com/lnurl/luds/blob/luds/20.md): βœ… core βœ… client βœ… server ⚠️ tests -- [LUD-21 *proposal*](https://github.com/lnurl/luds/blob/8580e3c8cbfd8fc95a6c0e5f7fcb5b048a0d5b61/21.md): ⚠️ core ⚠️ client ⚠️ server πŸ†˜ tests +- [LUD-21 *proposal*](https://github.com/lnurl/luds/blob/8580e3c8cbfd8fc95a6c0e5f7fcb5b048a0d5b61/21.md): βœ… core βœ… client βœ… server βœ… tests ###### Soon. β„’ diff --git a/tests/lud21.rs b/tests/lud21.rs new file mode 100644 index 0000000..681d1a9 --- /dev/null +++ b/tests/lud21.rs @@ -0,0 +1,106 @@ +#[tokio::test] +async fn test() { + let listener = tokio::net::TcpListener::bind("0.0.0.0:0") + .await + .expect("net"); + + let addr = listener.local_addr().expect("addr"); + + let query_url = format!("http://{addr}/lnurlp"); + let callback_url = url::Url::parse(&format!("http://{addr}/lnurlp/callback")).expect("url"); + + let router = lnurlkit::Server::default() + .pay_request( + move |_| { + let callback = callback_url.clone(); + async { + Ok(lnurlkit::pay::server::Entrypoint { + callback, + short_description: String::from("today i become death"), + long_description: Some(String::from("the destroyer of worlds")), + jpeg: None, + png: None, + comment_size: None, + min: 314, + max: 315, + identifier: None, + email: None, + currencies: Some(vec![ + lnurlkit::pay::Currency { + code: String::from("BRL"), + name: String::from("Reais"), + symbol: String::from("R$"), + decimals: 2, + multiplier: 314.15, + convertible: true, + }, + lnurlkit::pay::Currency { + code: String::from("USD"), + name: String::from("DΓ³lar"), + symbol: String::from("$"), + decimals: 3, + multiplier: 123.321, + convertible: false, + }, + ]), + }) + } + }, + |req: lnurlkit::pay::server::Callback| async move { + Ok(lnurlkit::pay::server::CallbackResponse { + pr: format!("pierre:{:?}:{:?}", req.amount, req.convert), + disposable: false, + success_action: None, + }) + }, + ) + .build(); + + tokio::spawn(async move { + axum::serve(listener, router).await.expect("serve"); + }); + + let client = lnurlkit::Client::default(); + + let lnurl = bech32::encode( + "lnurl", + bech32::ToBase32::to_base32(&query_url), + bech32::Variant::Bech32, + ) + .expect("lnurl"); + + let queried = client.entrypoint(&lnurl).await.expect("query"); + let lnurlkit::client::Entrypoint::Pay(pr) = queried else { + panic!("not pay request"); + }; + + let currencies = pr.core.currencies.as_ref().unwrap(); + + assert_eq!(currencies[0].code, "BRL"); + assert_eq!(currencies[0].name, "Reais"); + assert_eq!(currencies[0].symbol, "R$"); + assert_eq!(currencies[0].decimals, 2); + assert!((currencies[0].multiplier - 314.15).abs() < f64::EPSILON); + assert!(currencies[0].convertible); + + assert_eq!(currencies[1].code, "USD"); + assert_eq!(currencies[1].name, "DΓ³lar"); + assert_eq!(currencies[1].symbol, "$"); + assert_eq!(currencies[1].decimals, 3); + assert!((currencies[1].multiplier - 123.321).abs() < f64::EPSILON); + assert!(!currencies[1].convertible); + + let invoice = pr + .invoice( + &lnurlkit::pay::Amount::Currency(String::from("USD"), 314), + None, + Some("BRL"), + ) + .await + .expect("callback"); + + assert_eq!( + &invoice.pr as &str, + "pierre:Currency(\"USD\", 314):Some(\"BRL\")" + ); +}