Skip to content

Commit

Permalink
Attempt at LUD-16
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 2, 2023
1 parent 8610509 commit 3e7b851
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This library works as a toolkit so you can serve and make your LNURL requests wi
- [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-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
Expand Down
15 changes: 15 additions & 0 deletions examples/lud16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[tokio::main(flavor = "current_thread")]
async fn main() {
let client = lnurlkit::Lnurl::default();

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

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

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

println!("{invoice:?}");
}
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ impl Lnurl {

Ok(query)
}

/// # Errors
///
/// Will return error in case `address` is not a valid lnaddress,
/// when request or parsing fails, basically anything that goes bad.
pub async fn address(&self, address: &str) -> Result<pay_request::PayRequest, &'static str> {
let (identifier, domain) = address.split_once('@').ok_or("split failed")?;
let url = url::Url::parse(&format!("https://{domain}/.well-known/lnurlp/{identifier}"))
.map_err(|_| "bad url")?;

let response = self.0.get(url).send().await.map_err(|_| "request failed")?;
let body = response.text().await.map_err(|_| "body failed")?;
let query = pay_request::build(&body, &self.0).map_err(|_| "deserialize data failed")?;

Ok(query)
}
}

#[derive(Debug)]
Expand Down

0 comments on commit 3e7b851

Please sign in to comment.