Skip to content

Commit 1a1c0a3

Browse files
committed
msggen: Map the extratlvs field of keysend
Changelog-Added: cln-rpc: `keysend` now exposes the `extratlvs` field
1 parent 93c9505 commit 1a1c0a3

File tree

11 files changed

+245
-133
lines changed

11 files changed

+245
-133
lines changed

cln-grpc/proto/node.proto

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cln-grpc/proto/primitives.proto

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@ message Routehint {
7272
}
7373
message RoutehintList {
7474
repeated Routehint hints = 2;
75-
}
75+
}
76+
77+
78+
message TlvEntry {
79+
uint64 type = 1;
80+
bytes value = 2;
81+
}
82+
message TlvStream {
83+
repeated TlvEntry entries = 1;
84+
}

cln-grpc/src/convert.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cln-grpc/src/pb.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,40 @@ impl From<RouteHop> for cln_rpc::primitives::Routehop {
114114
}
115115
}
116116
}
117+
117118
impl From<Routehint> for cln_rpc::primitives::Routehint {
118119
fn from(c: Routehint) -> Self {
119120
Self {
120121
hops: c.hops.into_iter().map(|h| h.into()).collect(),
121122
}
122123
}
123124
}
125+
124126
impl From<RoutehintList> for cln_rpc::primitives::RoutehintList {
125127
fn from(c: RoutehintList) -> Self {
126128
Self {
127129
hints: c.hints.into_iter().map(|h| h.into()).collect(),
128130
}
129131
}
130132
}
133+
134+
impl From<TlvStream> for cln_rpc::primitives::TlvStream {
135+
fn from(s: TlvStream) -> Self {
136+
Self {
137+
entries: s.entries.into_iter().map(|e| e.into()).collect(),
138+
}
139+
}
140+
}
141+
142+
impl From<TlvEntry> for cln_rpc::primitives::TlvEntry {
143+
fn from(e: TlvEntry) -> Self {
144+
Self {
145+
typ: e.r#type,
146+
value: e.value,
147+
}
148+
}
149+
}
150+
131151
#[cfg(test)]
132152
mod test {
133153
use super::*;

cln-grpc/src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ fn test_keysend() {
278278
}],
279279
}],
280280
}),
281+
extratlvs: None,
281282
};
282283

283284
let u: cln_rpc::model::KeysendRequest = g.into();

cln-rpc/src/model.rs

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cln-rpc/src/primitives.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ mod test {
548548
let serialized: String = serde_json::to_string(&od).unwrap();
549549
assert_eq!(a, serialized);
550550
}
551+
552+
#[test]
553+
fn tlvstream() {
554+
let stream = TlvStream {
555+
entries: vec![
556+
TlvEntry { typ: 31337, value: vec![1,2,3,4,5]},
557+
TlvEntry { typ: 42, value: vec![]},
558+
],
559+
};
560+
561+
let res = serde_json::to_string(&stream).unwrap();
562+
assert_eq!(res, "{\"31337\":\"0102030405\",\"42\":\"\"}");
563+
}
551564
}
552565

553566
#[derive(Clone, Debug, PartialEq)]
@@ -623,4 +636,50 @@ impl Display for RpcError {
623636
}
624637
}
625638

626-
impl std::error::Error for RpcError {}
639+
impl std::error::Error for RpcError {}
640+
641+
#[derive(Clone, Debug)]
642+
pub struct TlvEntry {
643+
pub typ: u64,
644+
pub value: Vec<u8>,
645+
}
646+
647+
#[derive(Clone, Debug)]
648+
pub struct TlvStream {
649+
pub entries: Vec<TlvEntry>,
650+
}
651+
652+
impl<'de> Deserialize<'de> for TlvStream {
653+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
654+
where
655+
D: Deserializer<'de>,
656+
{
657+
let map: std::collections::HashMap<u64, String> = Deserialize::deserialize(deserializer)?;
658+
659+
let entries = map
660+
.iter()
661+
.map(|(k, v)| TlvEntry {
662+
typ: *k,
663+
value: hex::decode(v).unwrap(),
664+
})
665+
.collect();
666+
667+
Ok(TlvStream { entries })
668+
}
669+
}
670+
671+
impl Serialize for TlvStream {
672+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
673+
where
674+
S: Serializer,
675+
{
676+
use serde::ser::SerializeMap;
677+
678+
let mut map = serializer.serialize_map(Some(self.entries.len()))?;
679+
for e in &self.entries {
680+
map.serialize_key(&e.typ)?;
681+
map.serialize_value(&hex::encode(&e.value))?;
682+
}
683+
map.end()
684+
}
685+
}

contrib/msggen/msggen/gen/grpc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ def generate_composite(self, prefix, field: CompositeField) -> None:
440440
'hash': f'Sha256::from_slice(&c.{name}).unwrap()',
441441
'hash?': f'c.{name}.map(|v| Sha256::from_slice(&v).unwrap())',
442442
'txid': f'hex::encode(&c.{name})',
443+
'TlvStream?': f'c.{name}.map(|s| s.into())',
443444
}.get(
444445
typ,
445446
f'c.{name}' # default to just assignment

contrib/msggen/msggen/model.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,21 @@ def __str__(self):
343343
DatastoreKeyField = ArrayField(itemtype=PrimitiveField("string", None, None), dims=1, path=None, description=None)
344344
InvoiceExposeprivatechannelsField = PrimitiveField("boolean", None, None)
345345
PayExclude = ArrayField(itemtype=PrimitiveField("string", None, None), dims=1, path=None, description=None)
346-
RoutehintListField = PrimitiveField("RoutehintList", None, None)
346+
RoutehintListField = PrimitiveField(
347+
"RoutehintList",
348+
None,
349+
None
350+
)
351+
352+
# TlvStreams are special, they don't have preset dict-keys, rather
353+
# they can specify `u64` keys pointing to hex payloads. So the schema
354+
# has to rely on additionalProperties to make it work.
355+
TlvStreamField = PrimitiveField(
356+
"TlvStream",
357+
None,
358+
None
359+
)
360+
347361
# Override fields with manually managed types, fieldpath -> field mapping
348362
overrides = {
349363
'Invoice.label': InvoiceLabelField,
@@ -355,6 +369,7 @@ def __str__(self):
355369
'Invoice.exposeprivatechannels': InvoiceExposeprivatechannelsField,
356370
'Pay.exclude': PayExclude,
357371
'KeySend.routehints': RoutehintListField,
372+
'KeySend.extratlvs': TlvStreamField,
358373
}
359374

360375

contrib/pyln-testing/pyln/testing/node_pb2.py

Lines changed: 108 additions & 118 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contrib/pyln-testing/pyln/testing/primitives_pb2.py

Lines changed: 25 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)