Skip to content

Commit

Permalink
Reorganize EVERYTHING
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 3, 2023
1 parent 3e7b851 commit 94c5113
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 406 deletions.
16 changes: 15 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "lnurlkit"
version = "0.1.0"
edition = "2021"
rust-version = "1.60"
authors = ["Lucas Sunsi Abreu <lsunsi@pm.me>"]
description = "All you need to support LNURL and nothing more"
categories = ["finance", "web-programming", "web-programming::http-client", "api-bindings", "asynchronous"]
Expand All @@ -15,13 +16,18 @@ readme = "README.md"
base64 = { version = "0.21.5", features = ["std"], default-features = false }
bech32 = { version = "0.9.0", default-features = false }
miniserde = { version = "0.1.0", default-features = false }
reqwest = { version = "0.11.0", default-features = false }
url = { version = "2.5.0", default-features = false }

reqwest = { version = "0.11.0", default-features = false, optional = true }

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

[features]
client = ["dep:reqwest"]
server = []

[lints.rust]
warnings = "deny"

Expand All @@ -33,3 +39,11 @@ pedantic = "deny"
perf = "deny"
style = "deny"
suspicious = "deny"

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

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

¹ _Soon to be. Currently it's still on early stages and should be considered experimental._

## Current client support

- [LUD-01](https://github.com/lnurl/luds/blob/luds/01.md): ✅ implementation ⚠️ example 🆘 test
- [LUD-02](https://github.com/lnurl/luds/blob/luds/02.md): ✅ implementation 🆘 example 🆘 test
- [LUD-03](https://github.com/lnurl/luds/blob/luds/03.md): ✅ implementation 🆘 example 🆘 test
- [LUD-04](https://github.com/lnurl/luds/blob/luds/04.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-05](https://github.com/lnurl/luds/blob/luds/05.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-06](https://github.com/lnurl/luds/blob/luds/06.md): ✅ implementation ⚠️ example 🆘 test
- [LUD-07](https://github.com/lnurl/luds/blob/luds/07.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-08](https://github.com/lnurl/luds/blob/luds/08.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-09](https://github.com/lnurl/luds/blob/luds/09.md): ✅ implementation 🆘 example 🆘 test
- [LUD-10](https://github.com/lnurl/luds/blob/luds/10.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-11](https://github.com/lnurl/luds/blob/luds/11.md): ✅ implementation 🆘 example 🆘 test
- [LUD-12](https://github.com/lnurl/luds/blob/luds/12.md): ✅ implementation 🆘 example 🆘 test
- [LUD-13](https://github.com/lnurl/luds/blob/luds/13.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-14](https://github.com/lnurl/luds/blob/luds/14.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-15](https://github.com/lnurl/luds/blob/luds/15.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-16](https://github.com/lnurl/luds/blob/luds/16.md): ✅ implementation ⚠️ example 🆘 test
- [LUD-17](https://github.com/lnurl/luds/blob/luds/17.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-18](https://github.com/lnurl/luds/blob/luds/18.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-19](https://github.com/lnurl/luds/blob/luds/19.md): 🆘 implementation 🆘 example 🆘 test
- [LUD-20](https://github.com/lnurl/luds/blob/luds/20.md): ✅ implementation 🆘 example 🆘 test

## Current server support
## 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

- ###### Soon. ™

Expand Down
9 changes: 3 additions & 6 deletions examples/pay_request.rs → examples/lud06.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[tokio::main(flavor = "current_thread")]
async fn main() {
let client = lnurlkit::Lnurl::default();
let client = lnurlkit::client::Client::default();

let queried = client
.query("lnurl1dp68gurn8ghj7cnfwpsjuctswqhjuam9d3kz66mwdamkutmvde6hymrs9a4k2mn4cdry4t")
Expand All @@ -9,14 +9,11 @@ async fn main() {

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

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

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

println!("{invoice:?}");
}
15 changes: 8 additions & 7 deletions examples/lud16.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#[tokio::main(flavor = "current_thread")]
async fn main() {
let client = lnurlkit::Lnurl::default();
let client = lnurlkit::client::Client::default();

let pr = client.address("kenu@bipa.app").await.expect("address");
let queried = client.query("kenu@bipa.app").await.expect("address");

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

let invoice = pr
.generate_invoice("comment", 123000)
.await
.expect("callback");
let lnurlkit::client::Query::PayRequest(pr) = queried else {
panic!("not pay request");
};

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

println!("{invoice:?}");
}
68 changes: 0 additions & 68 deletions src/channel_request.rs

This file was deleted.

127 changes: 127 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#[derive(Clone, Default)]
pub struct Client(reqwest::Client);

impl Client {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn query(&self, s: &str) -> Result<Query, &'static str> {
let url = crate::core::resolve(s)?;

let client = &self.0;
let response = client.get(url).send().await.map_err(|_| "request failed")?;
let text = response.text().await.map_err(|_| "body failed")?;

text.parse::<crate::core::Query>()
.map_err(|_| "parse failed")
.map(|query| match query {
crate::core::Query::PayRequest(core) => {
Query::PayRequest(PayRequest { client, core })
}
crate::core::Query::ChannelRequest(core) => {
Query::ChannelRequest(ChannelRequest { client, core })
}
crate::core::Query::WithdrawalRequest(core) => {
Query::WithdrawalRequest(WithdrawalRequest { client, core })
}
})
}
}

#[derive(Clone, Debug)]
pub enum Query<'a> {
PayRequest(PayRequest<'a>),
ChannelRequest(ChannelRequest<'a>),
WithdrawalRequest(WithdrawalRequest<'a>),
}

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

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

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

impl PayRequest<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback(
self,
comment: &str,
millisatoshis: u64,
) -> Result<crate::core::pay_request::CallbackResponse, &'static str> {
let callback = self.core.callback(comment, millisatoshis);

let response = self
.client
.get(callback)
.send()
.await
.map_err(|_| "request failed")?;

let text = response.text().await.map_err(|_| "body failed")?;

text.parse().map_err(|_| "parse failed")
}
}

impl ChannelRequest<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback_accept(self, remoteid: &str, private: bool) -> Result<(), &'static str> {
let callback = self.core.callback_accept(remoteid, private);

self.client
.get(callback)
.send()
.await
.map_err(|_| "request failed")?;

Ok(())
}

/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback_cancel(self, remoteid: &str) -> Result<(), &'static str> {
let callback = self.core.callback_cancel(remoteid);

self.client
.get(callback)
.send()
.await
.map_err(|_| "request failed")?;

Ok(())
}
}

impl WithdrawalRequest<'_> {
/// # Errors
///
/// Returns errors on network or deserialization failures.
pub async fn callback(self, pr: &str) -> Result<(), &'static str> {
let callback = self.core.callback(pr);

self.client
.get(callback)
.send()
.await
.map_err(|_| "request failed")?;

Ok(())
}
}
Loading

0 comments on commit 94c5113

Please sign in to comment.