Skip to content

Commit bafc912

Browse files
committed
plugins: lsps: move feature helpers to core module
This removes the util.rs module and leaves us with proto, core and cln_adapters Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
1 parent 85d47a5 commit bafc912

File tree

4 files changed

+3
-101
lines changed

4 files changed

+3
-101
lines changed

plugins/lsps-plugin/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use cln_lsps::{
1212
},
1313
core::{
1414
client::LspsClient,
15+
features::is_feature_bit_set_reversed,
1516
tlv::{encode_tu64, TLV_FORWARD_AMT, TLV_PAYMENT_SECRET},
1617
transport::{MultiplexedTransport, PendingRequests},
1718
},
1819
proto::{
1920
lsps0::{Msat, LSP_FEATURE_BIT},
2021
lsps2::{compute_opening_fee, Lsps2BuyResponse, Lsps2GetInfoResponse, OpeningFeeParams},
2122
},
22-
util,
2323
};
2424
use cln_plugin::options;
2525
use cln_rpc::{
@@ -772,7 +772,7 @@ async fn check_peer_lsp_status(
772772
let has_lsp_feature = if let Some(f_str) = &peer.features {
773773
let feature_bits = hex::decode(f_str)
774774
.map_err(|e| anyhow!("Invalid feature bits hex for peer {peer_id}, {f_str}: {e}"))?;
775-
util::is_feature_bit_set_reversed(&feature_bits, LSP_FEATURE_BIT)
775+
is_feature_bit_set_reversed(&feature_bits, LSP_FEATURE_BIT)
776776
} else {
777777
false
778778
};
Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use anyhow::anyhow;
2-
use anyhow::Result;
3-
use cln_rpc::primitives::PublicKey;
41
use core::fmt;
5-
use serde_json::Value;
6-
use std::str::FromStr;
72

83
/// Checks whether a feature bit is set in a bitmap interpreted as
94
/// **big-endian across bytes**, while keeping **LSB-first within each byte**.
@@ -97,103 +92,10 @@ impl std::error::Error for UnwrapError {
9792
}
9893
}
9994

100-
/// Wraps a payload with a peer ID for internal LSPS message transmission.
101-
pub fn try_wrap_payload_with_peer_id(payload: &[u8], peer_id: PublicKey) -> Result<Vec<u8>> {
102-
// We expect the payload to be valid json, so no empty payload allowed, also
103-
// checks that we have curly braces at start and end.
104-
if payload.is_empty() || payload[0] != b'{' || payload[payload.len() - 1] != b'}' {
105-
return Err(anyhow!("payload no valid json"));
106-
}
107-
108-
let pubkey_hex = peer_id.to_string();
109-
let mut result = Vec::with_capacity(pubkey_hex.len() + payload.len() + 13);
110-
111-
result.extend_from_slice(&payload[..payload.len() - 1]);
112-
result.extend_from_slice(b",\"peer_id\":\"");
113-
result.extend_from_slice(pubkey_hex.as_bytes());
114-
result.extend_from_slice(b"\"}");
115-
Ok(result)
116-
}
117-
118-
/// Safely unwraps payload data and a peer ID
119-
pub fn try_unwrap_payload_with_peer_id(data: &[u8]) -> Result<(Vec<u8>, PublicKey)> {
120-
let mut json: Value =
121-
serde_json::from_slice(data).map_err(|e| UnwrapError::SerdeFailure(e.to_string()))?;
122-
123-
if let Value::Object(ref mut map) = json {
124-
if let Some(Value::String(peer_id)) = map.remove("peer_id") {
125-
let modified_json = serde_json::to_string(&json)
126-
.map_err(|e| UnwrapError::SerdeFailure(e.to_string()))?;
127-
return Ok((
128-
modified_json.into_bytes(),
129-
PublicKey::from_str(&peer_id)
130-
.map_err(|e| UnwrapError::InvalidPublicKey(e.to_string()))?,
131-
));
132-
}
133-
}
134-
Err(UnwrapError::InvalidPublicKey(String::from(
135-
"public key missing",
136-
)))?
137-
}
138-
139-
/// Unwraps payload data and peer ID, panicking on error
140-
///
141-
/// This is a convenience function for cases where one knows the data is valid.
142-
pub fn unwrap_payload_with_peer_id(data: &[u8]) -> (Vec<u8>, PublicKey) {
143-
try_unwrap_payload_with_peer_id(data).expect("Failed to unwrap payload with peer_id")
144-
}
145-
146-
/// Wraps payload data and peer ID, panicking on error
147-
///
148-
/// This is a convenience function for cases where one knows that the payload is
149-
/// valid.
150-
pub fn wrap_payload_with_peer_id(payload: &[u8], peer_id: PublicKey) -> Vec<u8> {
151-
try_wrap_payload_with_peer_id(payload, peer_id).expect("Failed to wrap payload with peer_id")
152-
}
153-
15495
#[cfg(test)]
15596
mod tests {
156-
use serde_json::json;
157-
15897
use super::*;
15998

160-
// Valid test public key
161-
const PUBKEY: [u8; 33] = [
162-
0x02, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87,
163-
0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16,
164-
0xf8, 0x17, 0x98,
165-
];
166-
167-
#[test]
168-
fn test_wrap_and_unwrap_roundtrip() {
169-
let peer_id = PublicKey::from_slice(&PUBKEY).unwrap();
170-
let payload =
171-
json!({"jsonrpc": "2.0","method": "some-method","params": {},"id": "some-id"});
172-
let wrapped = wrap_payload_with_peer_id(payload.to_string().as_bytes(), peer_id);
173-
174-
let (unwrapped_payload, unwrapped_peer_id) = unwrap_payload_with_peer_id(&wrapped);
175-
let value: serde_json::Value = serde_json::from_slice(&unwrapped_payload).unwrap();
176-
177-
assert_eq!(value, payload);
178-
assert_eq!(unwrapped_peer_id, peer_id);
179-
}
180-
181-
#[test]
182-
fn test_invalid_pubkey() {
183-
let mut invalid_data = vec![0u8; 40];
184-
// Set an invalid public key (all zeros)
185-
invalid_data[0] = 0x02; // Valid prefix
186-
// But rest remains zeros which is invalid
187-
let payload = json!({"jsonrpc": "2.0","method": "some-method","params": {},"id": "some-id","peer_id": hex::encode(&invalid_data)});
188-
189-
let result = try_unwrap_payload_with_peer_id(payload.to_string().as_bytes());
190-
assert!(result.is_err());
191-
assert!(matches!(
192-
result.unwrap_err().downcast_ref::<UnwrapError>(),
193-
Some(UnwrapError::InvalidPublicKey(_))
194-
));
195-
}
196-
19799
#[test]
198100
fn test_basic_bit_checks() {
199101
// Example bitmap:

plugins/lsps-plugin/src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod client;
2+
pub mod features;
23
pub mod lsps2;
34
pub mod router;
45
pub mod server;

plugins/lsps-plugin/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod cln_adapters;
22
pub mod core;
33
pub mod proto;
4-
pub mod util;

0 commit comments

Comments
 (0)