Skip to content

Commit

Permalink
msggen: add support for short_channel_id_dir and pubkey
Browse files Browse the repository at this point in the history
Changelog-None
  • Loading branch information
daywalker90 authored and rustyrussell committed Nov 28, 2024
1 parent 2e90f59 commit 703c115
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,60 @@ impl ShortChannelId {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ShortChannelIdDir {
pub short_channel_id: ShortChannelId,
pub direction: u32,
}
impl Serialize for ShortChannelIdDir {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

impl<'de> Deserialize<'de> for ShortChannelIdDir {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
let s: String = Deserialize::deserialize(deserializer)?;
Ok(Self::from_str(&s).map_err(|e| Error::custom(e.to_string()))?)
}
}
impl FromStr for ShortChannelIdDir {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Result<Vec<String>, _> = s.split('/').map(|p| p.parse()).collect();
let parts = parts.with_context(|| format!("Malformed short_channel_id_dir: {}", s))?;
if parts.len() != 2 {
return Err(anyhow!(
"Malformed short_channel_id_dir: element count mismatch"
));
}

Ok(ShortChannelIdDir {
short_channel_id: ShortChannelId::from_str(&parts[0])?,
direction: parts[1].parse::<u32>()?,
})
}
}
impl Display for ShortChannelIdDir {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}x{}x{}/{}",
self.short_channel_id.block(),
self.short_channel_id.txindex(),
self.short_channel_id.outnum(),
self.direction
)
}
}

#[derive(Clone, Copy, Debug)]
pub struct Secret([u8; 32]);

Expand Down
4 changes: 4 additions & 0 deletions contrib/msggen/msggen/gen/grpc/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
"secret": f"i.to_vec()",
"hash": f"<Sha256 as AsRef<[u8]>>::as_ref(&i).to_vec()",
"short_channel_id": f"i.to_string()",
"short_channel_id_dir": f"i.to_string()",
"pubkey": f"i.serialize().to_vec()",
}.get(typ, f"i.into()")

self.write(f"// Field: {f.path}\n", numindent=3)
Expand Down Expand Up @@ -110,6 +112,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None):
"txid?": f"c.{name}.map(|v| hex::decode(v).unwrap())",
"short_channel_id": f"c.{name}.to_string()",
"short_channel_id?": f"c.{name}.map(|v| v.to_string())",
"short_channel_id_dir": f"c.{name}.to_string()",
"short_channel_id_dir?": f"c.{name}.map(|v| v.to_string())",
"hash": f"<Sha256 as AsRef<[u8]>>::as_ref(&c.{name}).to_vec()",
"hash?": f"c.{name}.map(|v| <Sha256 as AsRef<[u8]>>::as_ref(&v).to_vec())",
"secret": f"c.{name}.to_vec()",
Expand Down
4 changes: 4 additions & 0 deletions contrib/msggen/msggen/gen/grpc/unconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None) -> No
"secret": f"s.try_into().unwrap()",
"hash": f"Sha256::from_slice(&s).unwrap()",
"short_channel_id": f"cln_rpc::primitives::ShortChannelId::from_str(&s).unwrap()",
"short_channel_id_dir": f"cln_rpc::primitives::ShortChannelIdDir::from_str(&s).unwrap()",
"pubkey": f"PublicKey::from_slice(&s).unwrap()",
}.get(typ, f"s.into()")

# TODO fix properly
Expand Down Expand Up @@ -121,6 +123,8 @@ def generate_composite(self, prefix, field: CompositeField, override=None) -> No
"DecodeRoutehintList?": f"c.{name}.map(|drl| drl.into())",
"short_channel_id": f"cln_rpc::primitives::ShortChannelId::from_str(&c.{name}).unwrap()",
"short_channel_id?": f"c.{name}.map(|v| cln_rpc::primitives::ShortChannelId::from_str(&v).unwrap())",
"short_channel_id_dir": f"cln_rpc::primitives::ShortChannelIdDir::from_str(&c.{name}).unwrap()",
"short_channel_id_dir?": f"c.{name}.map(|v| cln_rpc::primitives::ShortChannelIdDir::from_str(&v).unwrap())",
"secret": f"c.{name}.try_into().unwrap()",
"secret?": f"c.{name}.map(|v| v.try_into().unwrap())",
"hash": f"Sha256::from_slice(&c.{name}).unwrap()",
Expand Down
1 change: 1 addition & 0 deletions contrib/msggen/msggen/gen/grpc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"number": "double",
"pubkey": "bytes",
"short_channel_id": "string",
"short_channel_id_dir": "string",
"signature": "string",
"string": "string",
"txid": "bytes",
Expand Down
1 change: 1 addition & 0 deletions contrib/msggen/msggen/gen/grpc2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, dest: TextIO):
"integer": "m.{name}",
"boolean": "m.{name}",
"short_channel_id": "m.{name}",
"short_channel_id_dir": "m.{name}",
"msat": "amount2msat(m.{name})",
"sat": "amount2sat(m.{name})",
"currency": "m.{name}",
Expand Down
1 change: 1 addition & 0 deletions contrib/msggen/msggen/gen/rpc/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"number": "f64",
"pubkey": "PublicKey",
"short_channel_id": "ShortChannelId",
"short_channel_id_dir": "ShortChannelIdDir",
"signature": "String",
"string": "String",
"txid": "String",
Expand Down

0 comments on commit 703c115

Please sign in to comment.