From 1b32cc1c731675a36e9926f4fbed1fa0b6b3c65d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 8 Apr 2020 20:51:27 +0200 Subject: [PATCH] wire: Add a function to serialize a raw set of TLV fields The generated wrappers will ignore the raw fields and will only consider the shortcut fields. This function takes the raw fields and serializes them instead. --- plugins/Makefile | 2 +- wire/tlvstream.c | 21 +++++++++++++++++++++ wire/tlvstream.h | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 wire/tlvstream.c diff --git a/plugins/Makefile b/plugins/Makefile index fce5bfd3e..b3cb29d4d 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -59,7 +59,7 @@ plugins/fundchannel: common/addr.o $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_LIB_OBJS) plugins/bcli: bitcoin/chainparams.o $(PLUGIN_BCLI_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) -plugins/keysend: bitcoin/chainparams.o wire/gen_onion_wire.o $(PLUGIN_KEYSEND_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) +plugins/keysend: bitcoin/chainparams.o wire/tlvstream.o wire/gen_onion_wire.o $(PLUGIN_KEYSEND_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(PLUGIN_PAY_OBJS) $(PLUGIN_AUTOCLEAN_OBJS) $(PLUGIN_FUNDCHANNEL_OBJS) $(PLUGIN_BCLI_OBJS) $(PLUGIN_LIB_OBJS): $(PLUGIN_LIB_HEADER) diff --git a/wire/tlvstream.c b/wire/tlvstream.c new file mode 100644 index 000000000..257b53f0c --- /dev/null +++ b/wire/tlvstream.c @@ -0,0 +1,21 @@ +#include +#include + +void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields) +{ + if (!fields) + return; + + for (size_t i = 0; i < tal_count(fields); i++) { + const struct tlv_field *field = &fields[i]; + /* BOLT #1: + * + * The sending node: + ... + * - MUST minimally encode `type` and `length`. + */ + towire_bigsize(pptr, field->numtype); + towire_bigsize(pptr, field->length); + towire(pptr, field->value, field->length); + } +} diff --git a/wire/tlvstream.h b/wire/tlvstream.h index 256feb5e8..5ec9939a7 100644 --- a/wire/tlvstream.h +++ b/wire/tlvstream.h @@ -34,4 +34,7 @@ void towire_tlvs(u8 **pptr, size_t num_types, const void *record); +/* Given any tlvstream serialize the raw fields (untyped ones). */ +void towire_tlvstream_raw(u8 **pptr, const struct tlv_field *fields); + #endif /* LIGHTNING_WIRE_TLVSTREAM_H */