Skip to content

Commit 703c115

Browse files
daywalker90rustyrussell
authored andcommitted
msggen: add support for short_channel_id_dir and pubkey
Changelog-None
1 parent 2e90f59 commit 703c115

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

cln-rpc/src/primitives.rs

+54
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,60 @@ impl ShortChannelId {
267267
}
268268
}
269269

270+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
271+
pub struct ShortChannelIdDir {
272+
pub short_channel_id: ShortChannelId,
273+
pub direction: u32,
274+
}
275+
impl Serialize for ShortChannelIdDir {
276+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
277+
where
278+
S: Serializer,
279+
{
280+
serializer.serialize_str(&self.to_string())
281+
}
282+
}
283+
284+
impl<'de> Deserialize<'de> for ShortChannelIdDir {
285+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
286+
where
287+
D: Deserializer<'de>,
288+
{
289+
use serde::de::Error;
290+
let s: String = Deserialize::deserialize(deserializer)?;
291+
Ok(Self::from_str(&s).map_err(|e| Error::custom(e.to_string()))?)
292+
}
293+
}
294+
impl FromStr for ShortChannelIdDir {
295+
type Err = crate::Error;
296+
fn from_str(s: &str) -> Result<Self, Self::Err> {
297+
let parts: Result<Vec<String>, _> = s.split('/').map(|p| p.parse()).collect();
298+
let parts = parts.with_context(|| format!("Malformed short_channel_id_dir: {}", s))?;
299+
if parts.len() != 2 {
300+
return Err(anyhow!(
301+
"Malformed short_channel_id_dir: element count mismatch"
302+
));
303+
}
304+
305+
Ok(ShortChannelIdDir {
306+
short_channel_id: ShortChannelId::from_str(&parts[0])?,
307+
direction: parts[1].parse::<u32>()?,
308+
})
309+
}
310+
}
311+
impl Display for ShortChannelIdDir {
312+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
313+
write!(
314+
f,
315+
"{}x{}x{}/{}",
316+
self.short_channel_id.block(),
317+
self.short_channel_id.txindex(),
318+
self.short_channel_id.outnum(),
319+
self.direction
320+
)
321+
}
322+
}
323+
270324
#[derive(Clone, Copy, Debug)]
271325
pub struct Secret([u8; 32]);
272326

contrib/msggen/msggen/gen/grpc/convert.py

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
6969
"secret": f"i.to_vec()",
7070
"hash": f"<Sha256 as AsRef<[u8]>>::as_ref(&i).to_vec()",
7171
"short_channel_id": f"i.to_string()",
72+
"short_channel_id_dir": f"i.to_string()",
73+
"pubkey": f"i.serialize().to_vec()",
7274
}.get(typ, f"i.into()")
7375

7476
self.write(f"// Field: {f.path}\n", numindent=3)
@@ -110,6 +112,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
110112
"txid?": f"c.{name}.map(|v| hex::decode(v).unwrap())",
111113
"short_channel_id": f"c.{name}.to_string()",
112114
"short_channel_id?": f"c.{name}.map(|v| v.to_string())",
115+
"short_channel_id_dir": f"c.{name}.to_string()",
116+
"short_channel_id_dir?": f"c.{name}.map(|v| v.to_string())",
113117
"hash": f"<Sha256 as AsRef<[u8]>>::as_ref(&c.{name}).to_vec()",
114118
"hash?": f"c.{name}.map(|v| <Sha256 as AsRef<[u8]>>::as_ref(&v).to_vec())",
115119
"secret": f"c.{name}.to_vec()",

contrib/msggen/msggen/gen/grpc/unconvert.py

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None) -> No
5454
"secret": f"s.try_into().unwrap()",
5555
"hash": f"Sha256::from_slice(&s).unwrap()",
5656
"short_channel_id": f"cln_rpc::primitives::ShortChannelId::from_str(&s).unwrap()",
57+
"short_channel_id_dir": f"cln_rpc::primitives::ShortChannelIdDir::from_str(&s).unwrap()",
58+
"pubkey": f"PublicKey::from_slice(&s).unwrap()",
5759
}.get(typ, f"s.into()")
5860

5961
# TODO fix properly
@@ -121,6 +123,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None) -> No
121123
"DecodeRoutehintList?": f"c.{name}.map(|drl| drl.into())",
122124
"short_channel_id": f"cln_rpc::primitives::ShortChannelId::from_str(&c.{name}).unwrap()",
123125
"short_channel_id?": f"c.{name}.map(|v| cln_rpc::primitives::ShortChannelId::from_str(&v).unwrap())",
126+
"short_channel_id_dir": f"cln_rpc::primitives::ShortChannelIdDir::from_str(&c.{name}).unwrap()",
127+
"short_channel_id_dir?": f"c.{name}.map(|v| cln_rpc::primitives::ShortChannelIdDir::from_str(&v).unwrap())",
124128
"secret": f"c.{name}.try_into().unwrap()",
125129
"secret?": f"c.{name}.map(|v| v.try_into().unwrap())",
126130
"hash": f"Sha256::from_slice(&c.{name}).unwrap()",

contrib/msggen/msggen/gen/grpc/util.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"number": "double",
1616
"pubkey": "bytes",
1717
"short_channel_id": "string",
18+
"short_channel_id_dir": "string",
1819
"signature": "string",
1920
"string": "string",
2021
"txid": "bytes",

contrib/msggen/msggen/gen/grpc2py.py

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, dest: TextIO):
5353
"integer": "m.{name}",
5454
"boolean": "m.{name}",
5555
"short_channel_id": "m.{name}",
56+
"short_channel_id_dir": "m.{name}",
5657
"msat": "amount2msat(m.{name})",
5758
"sat": "amount2sat(m.{name})",
5859
"currency": "m.{name}",

contrib/msggen/msggen/gen/rpc/rust.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"number": "f64",
3434
"pubkey": "PublicKey",
3535
"short_channel_id": "ShortChannelId",
36+
"short_channel_id_dir": "ShortChannelIdDir",
3637
"signature": "String",
3738
"string": "String",
3839
"txid": "String",

0 commit comments

Comments
 (0)