Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit ed65742

Browse files
committed
De-/Serialize addresses assuming their network isn't bogus
.. 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.
1 parent 1a0e896 commit ed65742

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/lsps0/ser.rs

+57
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,63 @@ pub(crate) mod string_amount_option {
633633
}
634634
}
635635

636+
pub(crate) mod unchecked_address {
637+
use crate::prelude::{String, ToString};
638+
use bitcoin::Address;
639+
use core::str::FromStr;
640+
use serde::de::Unexpected;
641+
use serde::{Deserialize, Deserializer, Serializer};
642+
643+
pub(crate) fn serialize<S>(x: &Address, s: S) -> Result<S::Ok, S::Error>
644+
where
645+
S: Serializer,
646+
{
647+
s.serialize_str(&x.to_string())
648+
}
649+
650+
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Address, D::Error>
651+
where
652+
D: Deserializer<'de>,
653+
{
654+
let buf = String::deserialize(deserializer)?;
655+
656+
let parsed_addr = Address::from_str(&buf).map_err(|_| {
657+
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
658+
})?;
659+
Ok(parsed_addr.assume_checked())
660+
}
661+
}
662+
663+
pub(crate) mod unchecked_address_option {
664+
use crate::prelude::{String, ToString};
665+
use bitcoin::Address;
666+
use core::str::FromStr;
667+
use serde::de::Unexpected;
668+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
669+
670+
pub(crate) fn serialize<S>(x: &Option<Address>, s: S) -> Result<S::Ok, S::Error>
671+
where
672+
S: Serializer,
673+
{
674+
let v = x.as_ref().map(|v| v.to_string());
675+
Option::<String>::serialize(&v, s)
676+
}
677+
678+
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Option<bitcoin::Address>, D::Error>
679+
where
680+
D: Deserializer<'de>,
681+
{
682+
if let Some(buf) = Option::<String>::deserialize(deserializer)? {
683+
let val = Address::from_str(&buf).map_err(|_| {
684+
serde::de::Error::invalid_value(Unexpected::Str(&buf), &"invalid address string")
685+
})?;
686+
Ok(Some(val.assume_checked()))
687+
} else {
688+
Ok(None)
689+
}
690+
}
691+
}
692+
636693
pub(crate) mod u32_fee_rate {
637694
use bitcoin::FeeRate;
638695
use serde::{Deserialize, Deserializer, Serializer};

src/lsps1/msgs.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! Message, request, and other primitive types used to implement LSPS1.
22
3-
use crate::lsps0::ser::{string_amount, u32_fee_rate, LSPSMessage, RequestId, ResponseError};
3+
use crate::lsps0::ser::{
4+
string_amount, u32_fee_rate, unchecked_address, unchecked_address_option, LSPSMessage,
5+
RequestId, ResponseError,
6+
};
47

58
use crate::prelude::String;
69

7-
use bitcoin::address::{Address, NetworkUnchecked};
8-
use bitcoin::{FeeRate, OutPoint};
10+
use bitcoin::{Address, FeeRate, OutPoint};
911

1012
use lightning_invoice::Bolt11Invoice;
1113

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

0 commit comments

Comments
 (0)