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 13, 2024
1 parent 7d7dd5c commit 3a57eb8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/lsps0/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,29 @@ pub(crate) mod u32_fee_rate {
Ok(FeeRate::from_sat_per_kwu(fee_rate_sat_kwu as u64))
}
}

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

pub(crate) fn serialize<S>(x: &bitcoin::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<bitcoin::Address, D::Error>
where
D: Deserializer<'de>,
{
let buf = String::deserialize(deserializer)?;

let parsed_addr = bitcoin::Address::from_str(&buf).map_err(|_| {
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
})?;
Ok(parsed_addr.assume_checked())
}
}
7 changes: 5 additions & 2 deletions src/lsps1/msgs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! 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, LSPSMessage, RequestId, ResponseError,
};

use crate::prelude::String;

Expand Down Expand Up @@ -182,7 +184,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 3a57eb8

Please sign in to comment.