|
1 | | -use anyhow::anyhow; |
2 | | -use anyhow::Result; |
3 | | -use cln_rpc::primitives::PublicKey; |
4 | 1 | use core::fmt; |
5 | | -use serde_json::Value; |
6 | | -use std::str::FromStr; |
7 | 2 |
|
8 | 3 | /// Checks whether a feature bit is set in a bitmap interpreted as |
9 | 4 | /// **big-endian across bytes**, while keeping **LSB-first within each byte**. |
@@ -97,103 +92,10 @@ impl std::error::Error for UnwrapError { |
97 | 92 | } |
98 | 93 | } |
99 | 94 |
|
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 | | - |
154 | 95 | #[cfg(test)] |
155 | 96 | mod tests { |
156 | | - use serde_json::json; |
157 | | - |
158 | 97 | use super::*; |
159 | 98 |
|
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 | | - |
197 | 99 | #[test] |
198 | 100 | fn test_basic_bit_checks() { |
199 | 101 | // Example bitmap: |
|
0 commit comments