use gl_client::lnurl::models as wire;
#[derive(Clone, uniffi::Record)]
pub struct LnUrlPayRequestData {
pub callback: String,
pub min_sendable: u64,
pub max_sendable: u64,
pub metadata: String,
pub comment_allowed: u64,
pub description: String,
pub lnurl: String,
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlWithdrawRequestData {
pub callback: String,
pub k1: String,
pub default_description: String,
pub min_withdrawable: u64,
pub max_withdrawable: u64,
pub lnurl: String,
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlPayRequest {
pub data: LnUrlPayRequestData,
pub amount_msat: u64,
pub comment: Option<String>,
pub validate_success_action_url: Option<bool>,
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlWithdrawRequest {
pub data: LnUrlWithdrawRequestData,
pub amount_msat: u64,
pub description: Option<String>,
}
#[derive(Clone, uniffi::Enum)]
pub enum LnUrlPayResult {
EndpointSuccess { data: LnUrlPaySuccessData },
EndpointError { data: LnUrlErrorData },
PayError { data: LnUrlPayErrorData },
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlPaySuccessData {
pub payment_preimage: String,
pub success_action: Option<SuccessActionProcessed>,
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlPayErrorData {
pub payment_hash: String,
pub reason: String,
}
#[derive(Clone, uniffi::Enum)]
pub enum LnUrlWithdrawResult {
Ok { data: LnUrlWithdrawSuccessData },
ErrorStatus { data: LnUrlErrorData },
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlWithdrawSuccessData {
pub invoice: String,
}
#[derive(Clone, uniffi::Record)]
pub struct LnUrlErrorData {
pub reason: String,
}
#[derive(Clone, uniffi::Enum)]
pub enum SuccessActionProcessed {
Message { message: String },
Url { description: String, url: String },
Aes { description: String, plaintext: String },
}
impl From<wire::PayRequestResponse> for LnUrlPayRequestData {
fn from(r: wire::PayRequestResponse) -> Self {
Self {
description: r.description().unwrap_or_default(),
callback: r.callback,
min_sendable: r.min_sendable,
max_sendable: r.max_sendable,
metadata: r.metadata,
comment_allowed: r.comment_allowed.unwrap_or(0),
lnurl: String::new(), }
}
}
impl From<wire::WithdrawRequestResponse> for LnUrlWithdrawRequestData {
fn from(r: wire::WithdrawRequestResponse) -> Self {
Self {
callback: r.callback,
k1: r.k1,
default_description: r.default_description,
min_withdrawable: r.min_withdrawable,
max_withdrawable: r.max_withdrawable,
lnurl: String::new(), }
}
}
impl From<wire::ProcessedSuccessAction> for SuccessActionProcessed {
fn from(a: wire::ProcessedSuccessAction) -> Self {
match a {
wire::ProcessedSuccessAction::Message { message } => {
SuccessActionProcessed::Message { message }
}
wire::ProcessedSuccessAction::Url { description, url } => {
SuccessActionProcessed::Url { description, url }
}
wire::ProcessedSuccessAction::Aes {
description,
plaintext,
} => SuccessActionProcessed::Aes {
description,
plaintext,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pay_request_data_from_conversion() {
let wire_resp = wire::PayRequestResponse {
callback: "https://example.com/cb".to_string(),
max_sendable: 100000,
min_sendable: 1000,
tag: "payRequest".to_string(),
metadata: r#"[["text/plain", "Buy coffee"]]"#.to_string(),
comment_allowed: Some(140),
};
let data: LnUrlPayRequestData = wire_resp.into();
assert_eq!(data.callback, "https://example.com/cb");
assert_eq!(data.min_sendable, 1000);
assert_eq!(data.max_sendable, 100000);
assert_eq!(data.comment_allowed, 140);
assert_eq!(data.description, "Buy coffee");
assert!(data.lnurl.is_empty()); }
#[test]
fn test_pay_request_data_no_comment_allowed() {
let wire_resp = wire::PayRequestResponse {
callback: "https://example.com/cb".to_string(),
max_sendable: 100000,
min_sendable: 1000,
tag: "payRequest".to_string(),
metadata: r#"[["text/plain", "test"]]"#.to_string(),
comment_allowed: None,
};
let data: LnUrlPayRequestData = wire_resp.into();
assert_eq!(data.comment_allowed, 0);
}
#[test]
fn test_withdraw_request_data_from_conversion() {
let wire_resp = wire::WithdrawRequestResponse {
tag: "withdrawRequest".to_string(),
callback: "https://example.com/withdraw".to_string(),
k1: "secret123".to_string(),
default_description: "Withdraw from service".to_string(),
min_withdrawable: 1000,
max_withdrawable: 50000,
};
let data: LnUrlWithdrawRequestData = wire_resp.into();
assert_eq!(data.callback, "https://example.com/withdraw");
assert_eq!(data.k1, "secret123");
assert_eq!(data.default_description, "Withdraw from service");
assert_eq!(data.min_withdrawable, 1000);
assert_eq!(data.max_withdrawable, 50000);
}
#[test]
fn test_processed_success_action_from_message() {
let processed = wire::ProcessedSuccessAction::Message {
message: "Thanks!".to_string(),
};
let sdk: SuccessActionProcessed = processed.into();
match sdk {
SuccessActionProcessed::Message { message } => assert_eq!(message, "Thanks!"),
_ => panic!("Expected Message variant"),
}
}
#[test]
fn test_processed_success_action_from_url() {
let processed = wire::ProcessedSuccessAction::Url {
description: "View order".to_string(),
url: "https://example.com/order".to_string(),
};
let sdk: SuccessActionProcessed = processed.into();
match sdk {
SuccessActionProcessed::Url { description, url } => {
assert_eq!(description, "View order");
assert_eq!(url, "https://example.com/order");
}
_ => panic!("Expected Url variant"),
}
}
#[test]
fn test_processed_success_action_from_aes() {
let processed = wire::ProcessedSuccessAction::Aes {
description: "Your code".to_string(),
plaintext: "ABC-123".to_string(),
};
let sdk: SuccessActionProcessed = processed.into();
match sdk {
SuccessActionProcessed::Aes {
description,
plaintext,
} => {
assert_eq!(description, "Your code");
assert_eq!(plaintext, "ABC-123");
}
_ => panic!("Expected Aes variant"),
}
}
}