use crate::Error as LnUrlError;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin_hashes::Hash;
use lightning_invoice::Invoice;
use serde::de::Error;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
pub fn decode_ln_url_response(string: &str) -> Result<LnUrlResponse, LnUrlError> {
let json: serde_json::Value = serde_json::from_str(string)?;
decode_ln_url_response_from_json(json)
}
pub fn decode_ln_url_response_from_json(
json: serde_json::Value,
) -> Result<LnUrlResponse, LnUrlError> {
let obj = json.as_object().ok_or(LnUrlError::InvalidResponse)?;
let tag_str = obj
.get("tag")
.and_then(|v| v.as_str())
.ok_or(LnUrlError::InvalidResponse)?;
let tag = Tag::from_str(tag_str)?;
match tag {
Tag::PayRequest => {
let pay_response: PayResponse = serde_json::from_value(json)?;
Ok(LnUrlResponse::LnUrlPayResponse(pay_response))
}
Tag::WithdrawRequest => {
let resp: WithdrawalResponse = serde_json::from_value(json)?;
Ok(LnUrlResponse::LnUrlWithdrawResponse(resp))
}
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum LnUrlResponse {
LnUrlPayResponse(PayResponse),
LnUrlWithdrawResponse(WithdrawalResponse),
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Tag {
#[serde(rename = "payRequest")]
PayRequest,
#[serde(rename = "withdrawRequest")]
WithdrawRequest,
}
impl FromStr for Tag {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"payRequest" => Ok(Tag::PayRequest),
"withdrawRequest" => Ok(Tag::WithdrawRequest),
_ => Err(serde_json::Error::custom("Unknown tag")),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PayResponse {
pub callback: String,
#[serde(rename = "maxSendable")]
pub max_sendable: u64,
#[serde(rename = "minSendable")]
pub min_sendable: u64,
pub tag: Tag,
pub metadata: String,
}
impl PayResponse {
pub fn metadata_json(&self) -> serde_json::Value {
serde_json::from_str(&self.metadata).unwrap()
}
pub fn metadata_hash(&self) -> [u8; 32] {
Sha256::hash(self.metadata.as_bytes()).into_inner()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LnURLPayInvoice {
pr: String,
}
impl LnURLPayInvoice {
pub fn invoice(&self) -> Invoice {
Invoice::from_str(&self.pr).unwrap()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WithdrawalResponse {
#[serde(rename = "defaultDescription")]
pub default_description: String,
pub callback: String,
pub k1: String,
#[serde(rename = "maxWithdrawable")]
pub max_withdrawable: u64,
#[serde(rename = "minWithdrawable")]
pub min_withdrawable: Option<u64>,
pub tag: Tag,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "status")]
pub enum Response {
#[serde(rename = "ERROR")]
Error { reason: String },
#[serde(rename = "OK")]
Ok { event: Option<String> },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn response_from_str() {
let tests = vec![
(
r#"{"status":"ERROR","reason":"error detail..."}"#,
Response::Error {
reason: "error detail...".to_string(),
},
),
(
r#"{"status":"OK","event":"LOGGEDIN"}"#,
Response::Ok {
event: Some("LOGGEDIN".to_string()),
},
),
];
for test in tests {
let resp: Response = serde_json::from_str(test.0).unwrap();
assert_eq!(resp, test.1);
}
}
#[test]
fn response_to_str() {
let tests = vec![
(
r#"{"status":"ERROR","reason":"error detail..."}"#,
Response::Error {
reason: "error detail...".to_string(),
},
),
(
r#"{"status":"OK","event":"LOGGEDIN"}"#,
Response::Ok {
event: Some("LOGGEDIN".to_string()),
},
),
];
for test in tests {
let json = serde_json::to_string(&test.1).unwrap();
assert_eq!(json, test.0);
}
}
}