Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ldk-server-protos/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ fn generate_protos() {
"#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]",
)
.type_attribute(".", "#[cfg_attr(feature = \"serde\", serde(rename_all = \"snake_case\"))]")
.field_attribute(
"types.Bolt11.secret",
"#[cfg_attr(feature = \"serde\", serde(serialize_with = \"crate::serde_utils::serialize_opt_bytes_hex\"))]",
)
.field_attribute(
"types.Bolt11Jit.secret",
"#[cfg_attr(feature = \"serde\", serde(serialize_with = \"crate::serde_utils::serialize_opt_bytes_hex\"))]",
)
.field_attribute(
"types.Bolt12Offer.secret",
"#[cfg_attr(feature = \"serde\", serde(serialize_with = \"crate::serde_utils::serialize_opt_bytes_hex\"))]",
)
.field_attribute(
"types.Bolt12Refund.secret",
"#[cfg_attr(feature = \"serde\", serde(serialize_with = \"crate::serde_utils::serialize_opt_bytes_hex\"))]",
)
.field_attribute(
"types.Payment.direction",
"#[cfg_attr(feature = \"serde\", serde(serialize_with = \"crate::serde_utils::serialize_payment_direction\"))]",
)
.field_attribute(
"types.Payment.status",
"#[cfg_attr(feature = \"serde\", serde(serialize_with = \"crate::serde_utils::serialize_payment_status\"))]",
)
.compile_protos(
&[
"src/proto/api.proto",
Expand Down
2 changes: 2 additions & 0 deletions ldk-server-protos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ pub mod api;
pub mod endpoints;
pub mod error;
pub mod events;
#[cfg(feature = "serde")]
pub mod serde_utils;
pub mod types;
61 changes: 61 additions & 0 deletions ldk-server-protos/src/serde_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Custom serde serializers for proto types.
//!
//! These are used via `#[serde(serialize_with = "...")]` attributes on generated
//! proto fields to produce human-readable output (hex strings for bytes, enum
//! names for integer enum fields).

use serde::Serializer;

use crate::types::{PaymentDirection, PaymentStatus};

/// Serializes `Option<prost::bytes::Bytes>` as a hex string (or null).
pub fn serialize_opt_bytes_hex<S>(
value: &Option<bytes::Bytes>, serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match value {
Some(bytes) => {
let hex: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
serializer.serialize_some(&hex)
},
None => serializer.serialize_none(),
}
}

/// Serializes an `i32` payment direction field as its string name.
pub fn serialize_payment_direction<S>(value: &i32, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let name = match PaymentDirection::from_i32(*value) {
Some(PaymentDirection::Inbound) => PaymentDirection::Inbound.as_str_name(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make use of the stringify macro instead of having to match each type/variant individually?

Some(PaymentDirection::Outbound) => PaymentDirection::Outbound.as_str_name(),
_ => "UNKNOWN",
};
serializer.serialize_str(name)
}

/// Serializes an `i32` payment status field as its string name.
pub fn serialize_payment_status<S>(value: &i32, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let name = match PaymentStatus::from_i32(*value) {
Some(PaymentStatus::Pending) => PaymentStatus::Pending.as_str_name(),
Some(PaymentStatus::Succeeded) => PaymentStatus::Succeeded.as_str_name(),
Some(PaymentStatus::Failed) => PaymentStatus::Failed.as_str_name(),
_ => "UNKNOWN",
};
serializer.serialize_str(name)
}
24 changes: 24 additions & 0 deletions ldk-server-protos/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ pub struct Payment {
pub fee_paid_msat: ::core::option::Option<u64>,
/// The direction of the payment.
#[prost(enumeration = "PaymentDirection", tag = "4")]
#[cfg_attr(
feature = "serde",
serde(serialize_with = "crate::serde_utils::serialize_payment_direction")
)]
pub direction: i32,
/// The status of the payment.
#[prost(enumeration = "PaymentStatus", tag = "5")]
#[cfg_attr(
feature = "serde",
serde(serialize_with = "crate::serde_utils::serialize_payment_status")
)]
pub status: i32,
/// The timestamp, in seconds since start of the UNIX epoch, when this entry was last updated.
#[prost(uint64, tag = "6")]
Expand Down Expand Up @@ -138,6 +146,10 @@ pub struct Bolt11 {
pub preimage: ::core::option::Option<::prost::alloc::string::String>,
/// The secret used by the payment.
#[prost(bytes = "bytes", optional, tag = "3")]
#[cfg_attr(
feature = "serde",
serde(serialize_with = "crate::serde_utils::serialize_opt_bytes_hex")
)]
pub secret: ::core::option::Option<::prost::bytes::Bytes>,
}
/// Represents a BOLT 11 payment intended to open an LSPS 2 just-in-time channel.
Expand All @@ -154,6 +166,10 @@ pub struct Bolt11Jit {
pub preimage: ::core::option::Option<::prost::alloc::string::String>,
/// The secret used by the payment.
#[prost(bytes = "bytes", optional, tag = "3")]
#[cfg_attr(
feature = "serde",
serde(serialize_with = "crate::serde_utils::serialize_opt_bytes_hex")
)]
pub secret: ::core::option::Option<::prost::bytes::Bytes>,
/// Limits applying to how much fee we allow an LSP to deduct from the payment amount.
///
Expand Down Expand Up @@ -184,6 +200,10 @@ pub struct Bolt12Offer {
pub preimage: ::core::option::Option<::prost::alloc::string::String>,
/// The secret used by the payment.
#[prost(bytes = "bytes", optional, tag = "3")]
#[cfg_attr(
feature = "serde",
serde(serialize_with = "crate::serde_utils::serialize_opt_bytes_hex")
)]
pub secret: ::core::option::Option<::prost::bytes::Bytes>,
/// The hex-encoded ID of the offer this payment is for.
#[prost(string, tag = "4")]
Expand Down Expand Up @@ -213,6 +233,10 @@ pub struct Bolt12Refund {
pub preimage: ::core::option::Option<::prost::alloc::string::String>,
/// The secret used by the payment.
#[prost(bytes = "bytes", optional, tag = "3")]
#[cfg_attr(
feature = "serde",
serde(serialize_with = "crate::serde_utils::serialize_opt_bytes_hex")
)]
pub secret: ::core::option::Option<::prost::bytes::Bytes>,
/// The payer's note for the payment.
/// Truncated to \[PAYER_NOTE_LIMIT\](<https://docs.rs/lightning/latest/lightning/offers/invoice_request/constant.PAYER_NOTE_LIMIT.html>).
Expand Down
Loading