Skip to content

Commit

Permalink
Add first simple test and remove examples for now
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 3, 2023
1 parent f0dfa46 commit f8deebe
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 70 deletions.
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ axum = { version = "0.7.0", default-features = false, optional = true }
reqwest = { version = "0.11.0", default-features = false, optional = true }

[dev-dependencies]
axum = { version = "0.7.0", features = ["tokio", "http1"], default-features = false }
reqwest = { version = "0.11.0", features = ["rustls-tls-webpki-roots"], default-features = false }
tokio = { version = "1.0.0", features = ["rt", "macros"], default-features = false }
tokio = { version = "1.0.0", features = ["macros"], default-features = false }

[features]
client = ["dep:reqwest"]
Expand All @@ -41,10 +42,6 @@ perf = "deny"
style = "deny"
suspicious = "deny"

[[example]]
[[test]]
name = "lud06"
required-features = ["client"]

[[example]]
name = "lud16"
required-features = ["client"]
required-features = ["client", "server"]
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ This library works as a toolkit so you can serve and make your LNURL requests wi

## Current support

- [LUD-01](https://github.com/lnurl/luds/blob/luds/01.md): 🆘 core ⚠️ client 🆘 server ⚠️ example 🆘 test
- [LUD-02](https://github.com/lnurl/luds/blob/luds/02.md): 🆘 core ⚠️ client 🆘 server 🆘 example 🆘 test
- [LUD-03](https://github.com/lnurl/luds/blob/luds/03.md): 🆘 core ⚠️ client 🆘 server 🆘 example 🆘 test
- [LUD-04](https://github.com/lnurl/luds/blob/luds/04.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-05](https://github.com/lnurl/luds/blob/luds/05.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-06](https://github.com/lnurl/luds/blob/luds/06.md): 🆘 core ⚠️ client 🆘 server ⚠️ example 🆘 test
- [LUD-07](https://github.com/lnurl/luds/blob/luds/07.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-08](https://github.com/lnurl/luds/blob/luds/08.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-09](https://github.com/lnurl/luds/blob/luds/09.md): 🆘 core ⚠️ client 🆘 server 🆘 example 🆘 test
- [LUD-10](https://github.com/lnurl/luds/blob/luds/10.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-11](https://github.com/lnurl/luds/blob/luds/11.md): 🆘 core ⚠️ client 🆘 server 🆘 example 🆘 test
- [LUD-12](https://github.com/lnurl/luds/blob/luds/12.md): 🆘 core ⚠️ client 🆘 server 🆘 example 🆘 test
- [LUD-13](https://github.com/lnurl/luds/blob/luds/13.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-14](https://github.com/lnurl/luds/blob/luds/14.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-15](https://github.com/lnurl/luds/blob/luds/15.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-16](https://github.com/lnurl/luds/blob/luds/16.md): 🆘 core ⚠️ client 🆘 server ⚠️ example 🆘 test
- [LUD-17](https://github.com/lnurl/luds/blob/luds/17.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-18](https://github.com/lnurl/luds/blob/luds/18.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-19](https://github.com/lnurl/luds/blob/luds/19.md): 🆘 core 🆘 client 🆘 server 🆘 example 🆘 test
- [LUD-20](https://github.com/lnurl/luds/blob/luds/20.md): 🆘 core ⚠️ client 🆘 server 🆘 example 🆘 test
- [LUD-01](https://github.com/lnurl/luds/blob/luds/01.md): ⚠️ core ⚠️ client ⚠️ server 🆘 tests
- [LUD-02](https://github.com/lnurl/luds/blob/luds/02.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-03](https://github.com/lnurl/luds/blob/luds/03.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-04](https://github.com/lnurl/luds/blob/luds/04.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-05](https://github.com/lnurl/luds/blob/luds/05.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-06](https://github.com/lnurl/luds/blob/luds/06.md): core ⚠️ client ⚠️ server ⚠️ tests
- [LUD-07](https://github.com/lnurl/luds/blob/luds/07.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-08](https://github.com/lnurl/luds/blob/luds/08.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-09](https://github.com/lnurl/luds/blob/luds/09.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-10](https://github.com/lnurl/luds/blob/luds/10.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-11](https://github.com/lnurl/luds/blob/luds/11.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-12](https://github.com/lnurl/luds/blob/luds/12.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-13](https://github.com/lnurl/luds/blob/luds/13.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-14](https://github.com/lnurl/luds/blob/luds/14.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-15](https://github.com/lnurl/luds/blob/luds/15.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [LUD-16](https://github.com/lnurl/luds/blob/luds/16.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-17](https://github.com/lnurl/luds/blob/luds/17.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [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

- ###### Soon. ™

Expand Down
19 changes: 0 additions & 19 deletions examples/lud06.rs

This file was deleted.

16 changes: 0 additions & 16 deletions examples/lud16.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ pub enum Query<'a> {
#[derive(Clone, Debug)]
pub struct PayRequest<'a> {
client: &'a reqwest::Client,
core: crate::core::pay_request::PayRequest,
pub core: crate::core::pay_request::PayRequest,
}

#[derive(Clone, Debug)]
pub struct ChannelRequest<'a> {
client: &'a reqwest::Client,
core: crate::core::channel_request::ChannelRequest,
pub core: crate::core::channel_request::ChannelRequest,
}

#[derive(Clone, Debug)]
pub struct WithdrawalRequest<'a> {
client: &'a reqwest::Client,
core: crate::core::withdrawal_request::WithdrawalRequest,
pub core: crate::core::withdrawal_request::WithdrawalRequest,
}

impl PayRequest<'_> {
Expand Down
12 changes: 7 additions & 5 deletions src/core/pay_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const TAG: &str = "payRequest";

#[derive(Clone, Debug)]
pub struct PayRequest {
callback: crate::serde::Url,
pub callback: url::Url,
pub short_description: String,
pub long_description: Option<String>,
pub success_action: Option<SuccessAction>,
Expand Down Expand Up @@ -78,7 +78,7 @@ impl std::str::FromStr for PayRequest {
});

Ok(PayRequest {
callback: p.callback,
callback: p.callback.0,
min: p.min_sendable,
max: p.max_sendable,
short_description,
Expand Down Expand Up @@ -130,8 +130,9 @@ impl std::fmt::Display for PayRequest {
});

f.write_str(&miniserde::json::to_string(&ser::QueryResponse {
tag: TAG,
metadata,
callback: &self.callback,
callback: &crate::serde::Url(self.callback.clone()),
min_sendable: self.min,
max_sendable: self.max,
comment_allowed: self.comment_size,
Expand All @@ -146,7 +147,7 @@ impl PayRequest {
/// Returns errors on network or deserialization failures.
#[must_use]
pub fn callback(mut self, comment: &str, millisatoshis: u64) -> url::Url {
self.callback.0.query_pairs_mut().extend_pairs(
self.callback.query_pairs_mut().extend_pairs(
[
(!comment.is_empty()).then_some(("comment", comment)),
Some(("amount", &millisatoshis.to_string())),
Expand All @@ -155,7 +156,7 @@ impl PayRequest {
.flatten(),
);

self.callback.0
self.callback
}
}

Expand Down Expand Up @@ -186,6 +187,7 @@ mod ser {

#[derive(Serialize)]
pub(super) struct QueryResponse<'a> {
pub tag: &'static str,
pub metadata: String,
pub callback: &'a Url,
#[serde(rename = "minSendable")]
Expand Down
4 changes: 4 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use axum::{http::StatusCode, routing::get, Router};
use std::future::Future;

pub fn builder<P>() -> Server<P> {
Server { pay_request: None }
}

pub struct Server<P> {
pub pay_request: Option<P>,
}
Expand Down
65 changes: 65 additions & 0 deletions tests/lud06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#[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 = {
let mut router = lnurlkit::server::builder();

router.pay_request = Some(move || {
let callback = callback_url.clone();

async {
Ok(lnurlkit::core::pay_request::PayRequest {
callback,
short_description: String::from("today i become death"),
long_description: Some(String::from("the destroyer of worlds")),
success_action: None,
jpeg: None,
png: None,
comment_size: 0,
min: 314,
max: 315,
})
}
});

router.build()
};

tokio::spawn(async move {
axum::serve(listener, router).await.expect("serve");
});

let client = lnurlkit::client::Client::default();

let lnurl = bech32::encode(
"lnurl",
bech32::ToBase32::to_base32(&query_url),
bech32::Variant::Bech32,
)
.expect("lnurl");

let queried = client.query(&lnurl).await.expect("query");

println!("{queried:?}");

let lnurlkit::client::Query::PayRequest(pr) = queried else {
panic!("not pay request");
};

assert_eq!(pr.core.min, 314);
assert_eq!(pr.core.max, 315);
assert_eq!(pr.core.short_description, "today i become death");
assert_eq!(pr.core.long_description.unwrap(), "the destroyer of worlds");

// let invoice = pr.callback("comment", 123000).await.expect("callback");

// println!("{invoice:?}");
}

0 comments on commit f8deebe

Please sign in to comment.