msggen: add support for short_channel_id_dir and pubkey

Changelog-None
This commit is contained in:
daywalker90 2024-11-09 16:46:21 +01:00 committed by Rusty Russell
parent 2e90f59dfe
commit 703c11515d
6 changed files with 65 additions and 0 deletions

View file

@ -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]);

View file

@ -69,6 +69,8 @@ class GrpcConverterGenerator(IGenerator):
"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)
@ -110,6 +112,8 @@ class GrpcConverterGenerator(IGenerator):
"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()",

View file

@ -54,6 +54,8 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
"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
@ -121,6 +123,8 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
"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()",

View file

@ -15,6 +15,7 @@ typemap = {
"number": "double",
"pubkey": "bytes",
"short_channel_id": "string",
"short_channel_id_dir": "string",
"signature": "string",
"string": "string",
"txid": "bytes",

View file

@ -53,6 +53,7 @@ class Grpc2PyGenerator(IGenerator):
"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}",

View file

@ -33,6 +33,7 @@ typemap = {
"number": "f64",
"pubkey": "PublicKey",
"short_channel_id": "ShortChannelId",
"short_channel_id_dir": "ShortChannelIdDir",
"signature": "String",
"string": "String",
"txid": "String",