Skip to content

Commit

Permalink
Merge branch 'trunk' of github.com:SatoshiPortal/boltz-rust into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
i5hi committed Oct 5, 2024
2 parents 65ab179 + 928a587 commit 869d9e4
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/util/lnurl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::error::Error;
use lightning_invoice::Bolt11Invoice;
use lnurl::lightning_address::LightningAddress;
use lnurl::pay::LnURLPayInvoice;
use lnurl::withdraw::WithdrawalResponse;
use lnurl::{lnurl::LnUrl, Builder, LnUrlResponse};
use std::cmp::max;
use std::str::FromStr;

pub fn validate_lnurl(string: &str) -> bool {
Expand Down Expand Up @@ -52,6 +55,36 @@ pub fn fetch_invoice(address: &str, amount_msats: u64) -> Result<String, Error>
}
}

pub fn create_withdraw_response(voucher: &str) -> Result<WithdrawalResponse, Error> {
let lnurl = LnUrl::from_str(&*voucher.to_lowercase())
.map_err(|_| Error::Generic("Invalid LNURL".to_string()))?;

let client = Builder::default()
.build_blocking()
.map_err(|e| Error::Generic(e.to_string()))?;

let res = client
.make_request(&lnurl.url)
.map_err(|e| Error::HTTP(e.to_string()))?;

match res {
LnUrlResponse::LnUrlWithdrawResponse(withdraw) => Ok(withdraw),
_ => Err(Error::Generic("Unexpected response type".to_string())),
}
}

pub fn process_withdrawal(withdraw: &WithdrawalResponse, invoice: &str) -> Result<String, Error> {
let client = Builder::default()
.build_blocking()
.map_err(|e| Error::Generic(e.to_string()))?;

let withdraw_result = client
.do_withdrawal(withdraw, invoice)
.map_err(|e| Error::HTTP(e.to_string()))?;

Ok("Withdrawal successful".to_string())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -87,4 +120,41 @@ mod tests {
assert!(validate_lnurl(email_lnurl));
test_address(email_lnurl, amount_msats, "Lightning Address");
}

#[ignore = "Requires using an new lnurl-w voucher and invoice to match the max_withdrawble amount"]
#[test]
fn test_process_withdrawal() {
let invoice = "lnbc5m1pnszpmwpp5vdu4qrghzq4c3uzvll0d82aa8vw2xtywukwgq5jncwwk7av7rdcscqpjsp5pav6wyrk0zaqc6gyfr4048qmnfj7h7ydpul5ds4dmqj4xam679zq9q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgqdqqmqz9gxqyjw5qrzjqwryaup9lh50kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glcllm8u4a8gvusysqqqqlgqqqqqeqqjq6g9v7ejekz6uxqqmjjuaaa2s63nzx3d4n9pu8m6h68nmh7rgprky4pn5qae9878q5wpg72p66djy7ywsa7v4mfecdmnyj38etln394cqqzhnzt";
let voucher = "LNURL1DP68GURN8GHJ7ER9D4HJUMRWVF5HGUEWVDHK6TMHD96XSERJV9MJ7CTSDYHHVVF0D3H82UNV9AZXY56N89F5CDFJW34K63N2GEJXK5N2VD2K6TMRWARKJVN8D565WCNNDFT85WR42FP5GUN2VSDZTX2W";
assert!(validate_lnurl(voucher));
let withdraw_response = match create_withdraw_response(voucher) {
Ok(response) => response,
Err(e) => {
println!("Failed to create withdraw response: {:?}", e);
return;
}
};

let invoice_amount = match Bolt11Invoice::from_str(invoice) {
Ok(invoice) => invoice.amount_milli_satoshis().unwrap() / 1000,
Err(e) => {
println!("Failed to parse invoice: {:?}", e);
return;
}
};

assert!(
invoice_amount <= withdraw_response.max_withdrawable,
"Invoice of {} exceeds max withdrawable {} sats",
invoice_amount,
withdraw_response.max_withdrawable
);
println!("Successfully created withdraw response");
let result = process_withdrawal(&withdraw_response, invoice);

assert!(result.is_ok(), "Withdrawal failed: {:?}", result.err());
assert_eq!(result.unwrap(), "Withdrawal successful");

println!("Withdrawal test passed successfully");
}
}

0 comments on commit 869d9e4

Please sign in to comment.