|
| 1 | +//! Backfill structs for missing or incomplete Core Lightning types. |
| 2 | +//! |
| 3 | +//! This module provides struct implementations that are not available or |
| 4 | +//! fully accessible in the core-lightning crate, enabling better compatibility |
| 5 | +//! and interoperability with Core Lightning's RPC interface. |
| 6 | +use cln_rpc::primitives::{Amount, ShortChannelId}; |
| 7 | +use hex::FromHex; |
| 8 | +use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
| 9 | + |
| 10 | +use crate::core::tlv::TlvStream; |
| 11 | + |
| 12 | +#[derive(Debug, Deserialize)] |
| 13 | +#[allow(unused)] |
| 14 | +pub struct Onion { |
| 15 | + pub forward_msat: Option<Amount>, |
| 16 | + #[serde(deserialize_with = "from_hex")] |
| 17 | + pub next_onion: Vec<u8>, |
| 18 | + pub outgoing_cltv_value: Option<u32>, |
| 19 | + pub payload: TlvStream, |
| 20 | + // pub payload: TlvStream, |
| 21 | + #[serde(deserialize_with = "from_hex")] |
| 22 | + pub shared_secret: Vec<u8>, |
| 23 | + pub short_channel_id: Option<ShortChannelId>, |
| 24 | + pub total_msat: Option<Amount>, |
| 25 | + #[serde(rename = "type")] |
| 26 | + pub type_: Option<String>, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Deserialize)] |
| 30 | +#[allow(unused)] |
| 31 | +pub struct Htlc { |
| 32 | + pub amount_msat: Amount, |
| 33 | + pub cltv_expiry: u32, |
| 34 | + pub cltv_expiry_relative: u16, |
| 35 | + pub id: u64, |
| 36 | + #[serde(deserialize_with = "from_hex")] |
| 37 | + pub payment_hash: Vec<u8>, |
| 38 | + pub short_channel_id: ShortChannelId, |
| 39 | + pub extra_tlvs: Option<TlvStream>, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug, Serialize, PartialEq, Eq)] |
| 43 | +#[serde(rename_all = "snake_case")] |
| 44 | +pub enum HtlcAcceptedResult { |
| 45 | + Continue, |
| 46 | + Fail, |
| 47 | + Resolve, |
| 48 | +} |
| 49 | + |
| 50 | +impl std::fmt::Display for HtlcAcceptedResult { |
| 51 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 52 | + let s = match self { |
| 53 | + HtlcAcceptedResult::Continue => "continue", |
| 54 | + HtlcAcceptedResult::Fail => "fail", |
| 55 | + HtlcAcceptedResult::Resolve => "resolve", |
| 56 | + }; |
| 57 | + write!(f, "{s}") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug, Deserialize)] |
| 62 | +pub struct HtlcAcceptedRequest { |
| 63 | + pub htlc: Htlc, |
| 64 | + pub onion: Onion, |
| 65 | + pub forward_to: Option<String>, |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug, Serialize)] |
| 69 | +pub struct HtlcAcceptedResponse { |
| 70 | + pub result: HtlcAcceptedResult, |
| 71 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 72 | + pub payment_key: Option<String>, |
| 73 | + #[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")] |
| 74 | + pub payload: Option<Vec<u8>>, |
| 75 | + #[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")] |
| 76 | + pub forward_to: Option<Vec<u8>>, |
| 77 | + #[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")] |
| 78 | + pub extra_tlvs: Option<Vec<u8>>, |
| 79 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 80 | + pub failure_message: Option<String>, |
| 81 | + #[serde(skip_serializing_if = "Option::is_none", serialize_with = "to_hex")] |
| 82 | + pub failure_onion: Option<Vec<u8>>, |
| 83 | +} |
| 84 | + |
| 85 | +impl HtlcAcceptedResponse { |
| 86 | + pub fn continue_( |
| 87 | + payload: Option<Vec<u8>>, |
| 88 | + forward_to: Option<Vec<u8>>, |
| 89 | + extra_tlvs: Option<Vec<u8>>, |
| 90 | + ) -> Self { |
| 91 | + Self { |
| 92 | + result: HtlcAcceptedResult::Continue, |
| 93 | + payment_key: None, |
| 94 | + payload, |
| 95 | + forward_to, |
| 96 | + extra_tlvs, |
| 97 | + failure_message: None, |
| 98 | + failure_onion: None, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + pub fn fail(failure_message: Option<String>, failure_onion: Option<Vec<u8>>) -> Self { |
| 103 | + Self { |
| 104 | + result: HtlcAcceptedResult::Fail, |
| 105 | + payment_key: None, |
| 106 | + payload: None, |
| 107 | + forward_to: None, |
| 108 | + extra_tlvs: None, |
| 109 | + failure_message, |
| 110 | + failure_onion, |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[derive(Debug, Deserialize)] |
| 116 | +pub struct InvoicePaymentRequest { |
| 117 | + pub payment: InvoicePaymentRequestPayment, |
| 118 | +} |
| 119 | + |
| 120 | +#[derive(Debug, Deserialize)] |
| 121 | +pub struct InvoicePaymentRequestPayment { |
| 122 | + pub label: String, |
| 123 | + pub preimage: String, |
| 124 | + pub msat: u64, |
| 125 | +} |
| 126 | + |
| 127 | +#[derive(Debug, Deserialize)] |
| 128 | +pub struct OpenChannelRequest { |
| 129 | + pub openchannel: OpenChannelRequestOpenChannel, |
| 130 | +} |
| 131 | + |
| 132 | +#[derive(Debug, Deserialize)] |
| 133 | +pub struct OpenChannelRequestOpenChannel { |
| 134 | + pub id: String, |
| 135 | + pub funding_msat: u64, |
| 136 | + pub push_msat: u64, |
| 137 | + pub dust_limit_msat: u64, |
| 138 | + pub max_htlc_value_in_flight_msat: u64, |
| 139 | + pub channel_reserve_msat: u64, |
| 140 | + pub htlc_minimum_msat: u64, |
| 141 | + pub feerate_per_kw: u32, |
| 142 | + pub to_self_delay: u32, |
| 143 | + pub max_accepted_htlcs: u32, |
| 144 | + pub channel_flags: u64, |
| 145 | +} |
| 146 | + |
| 147 | +/// Deserializes a lowercase hex string to a `Vec<u8>`. |
| 148 | +pub fn from_hex<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> |
| 149 | +where |
| 150 | + D: Deserializer<'de>, |
| 151 | +{ |
| 152 | + use serde::de::Error; |
| 153 | + String::deserialize(deserializer) |
| 154 | + .and_then(|string| Vec::from_hex(string).map_err(|err| Error::custom(err.to_string()))) |
| 155 | +} |
| 156 | + |
| 157 | +pub fn to_hex<S>(bytes: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error> |
| 158 | +where |
| 159 | + S: Serializer, |
| 160 | +{ |
| 161 | + match bytes { |
| 162 | + Some(data) => serializer.serialize_str(&hex::encode(data)), |
| 163 | + None => serializer.serialize_none(), |
| 164 | + } |
| 165 | +} |
0 commit comments