Skip to content

Commit

Permalink
De-/Serialize addresses assuming their network isn't bogus
Browse files Browse the repository at this point in the history
.. as we have no real way to check the network at the point of
deserialzation, and we want to handle `bitcoin::Addresses`, not uncheck
addresses, in particular when it comes to serialization.
  • Loading branch information
tnull committed Aug 14, 2024
1 parent 564320e commit b014a08
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
57 changes: 57 additions & 0 deletions src/lsps0/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,63 @@ pub(crate) mod string_amount_option {
}
}

pub(crate) mod unchecked_address {
use crate::prelude::{String, ToString};
use bitcoin::Address;
use core::str::FromStr;
use serde::de::Unexpected;
use serde::{Deserialize, Deserializer, Serializer};

pub(crate) fn serialize<S>(x: &Address, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(&x.to_string())
}

pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Address, D::Error>
where
D: Deserializer<'de>,
{
let buf = String::deserialize(deserializer)?;

let parsed_addr = Address::from_str(&buf).map_err(|_| {
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
})?;
Ok(parsed_addr.assume_checked())
}
}

pub(crate) mod unchecked_address_option {
use crate::prelude::{String, ToString};
use bitcoin::Address;
use core::str::FromStr;
use serde::de::Unexpected;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub(crate) fn serialize<S>(x: &Option<Address>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let v = x.as_ref().map(|v| v.to_string());
Option::<String>::serialize(&v, s)
}

pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Option<bitcoin::Address>, D::Error>
where
D: Deserializer<'de>,
{
if let Some(buf) = Option::<String>::deserialize(deserializer)? {
let val = Address::from_str(&buf).map_err(|_| {
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
})?;
Ok(Some(val.assume_checked()))
} else {
Ok(None)
}
}
}

pub(crate) mod u32_fee_rate {
use bitcoin::FeeRate;
use serde::{Deserialize, Deserializer, Serializer};
Expand Down
14 changes: 9 additions & 5 deletions src/lsps1/msgs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Message, request, and other primitive types used to implement LSPS1.

use crate::lsps0::ser::{string_amount, u32_fee_rate, LSPSMessage, RequestId, ResponseError};
use crate::lsps0::ser::{
string_amount, u32_fee_rate, unchecked_address, unchecked_address_option, LSPSMessage,
RequestId, ResponseError,
};

use crate::prelude::String;

use bitcoin::address::{Address, NetworkUnchecked};
use bitcoin::{FeeRate, OutPoint};
use bitcoin::{Address, FeeRate, OutPoint};

use lightning_invoice::Bolt11Invoice;

Expand Down Expand Up @@ -106,7 +108,8 @@ pub struct OrderParameters {
/// May contain arbitrary associated data like a coupon code or a authentication token.
pub token: Option<String>,
/// The address where the LSP will send the funds if the order fails.
pub refund_onchain_address: Option<Address<NetworkUnchecked>>,
#[serde(with = "unchecked_address_option")]
pub refund_onchain_address: Option<Address>,
/// Indicates if the channel should be announced to the network.
pub announce_channel: bool,
}
Expand Down Expand Up @@ -182,7 +185,8 @@ pub struct OnchainPaymentInfo {
pub order_total_sat: u64,
/// An on-chain address the client can send [`Self::order_total_sat`] to to have the channel
/// opened.
pub address: Address<NetworkUnchecked>,
#[serde(with = "unchecked_address")]
pub address: Address,
/// The minimum number of block confirmations that are required for the on-chain payment to be
/// considered confirmed.
pub min_onchain_payment_confirmations: Option<u16>,
Expand Down

0 comments on commit b014a08

Please sign in to comment.