44#include <ccan/crypto/shachain/shachain.h>
55#include <common/htlc_wire.h>
66#include <common/onionreply.h>
7+ #include <wire/tlvstream.h>
78
89static struct failed_htlc * failed_htlc_dup (const tal_t * ctx ,
910 const struct failed_htlc * f TAKES )
@@ -33,7 +34,8 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
3334 const u8 onion_routing_packet [TOTAL_PACKET_SIZE (ROUTING_INFO_SIZE )],
3435 const struct pubkey * path_key TAKES ,
3536 const struct preimage * preimage TAKES ,
36- const struct failed_htlc * failed TAKES )
37+ const struct failed_htlc * failed TAKES ,
38+ const struct tlv_field * extra_tlvs TAKES )
3739{
3840 struct existing_htlc * existing = tal (ctx , struct existing_htlc );
3941
@@ -51,6 +53,17 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx,
5153 existing -> failed = failed_htlc_dup (existing , failed );
5254 else
5355 existing -> failed = NULL ;
56+ if (extra_tlvs ) {
57+ existing -> extra_tlvs = tal_dup_talarr (existing , struct tlv_field , extra_tlvs );
58+ for (size_t i = 0 ; i < tal_count (extra_tlvs ); i ++ ) {
59+ /* We need to attach the value to the correct parent */
60+ existing -> extra_tlvs [i ].value
61+ = tal_dup_talarr (existing , u8 ,
62+ existing -> extra_tlvs [i ].value );
63+ }
64+ } else {
65+ existing -> extra_tlvs = NULL ;
66+ }
5467
5568 return existing ;
5669}
@@ -70,6 +83,16 @@ void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
7083 towire_pubkey (pptr , added -> path_key );
7184 } else
7285 towire_bool (pptr , false);
86+ if (added -> extra_tlvs ) {
87+ u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
88+ towire_tlvstream_raw (& tmp_pptr , added -> extra_tlvs );
89+
90+ towire_bool (pptr , true);
91+ towire_u16 (pptr , tal_bytelen (tmp_pptr ));
92+ towire_u8_array (pptr , tmp_pptr ,
93+ tal_bytelen (tmp_pptr ));
94+ } else
95+ towire_bool (pptr , false);
7396 towire_bool (pptr , added -> fail_immediate );
7497}
7598
@@ -97,6 +120,16 @@ void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing)
97120 towire_pubkey (pptr , existing -> path_key );
98121 } else
99122 towire_bool (pptr , false);
123+ if (existing -> extra_tlvs ) {
124+ u8 * tmp_pptr = tal_arr (tmpctx , u8 , 0 );
125+ towire_tlvstream_raw (& tmp_pptr , existing -> extra_tlvs );
126+
127+ towire_bool (pptr , true);
128+ towire_u16 (pptr , tal_bytelen (tmp_pptr ));
129+ towire_u8_array (pptr , tmp_pptr ,
130+ tal_bytelen (tmp_pptr ));
131+ } else
132+ towire_bool (pptr , false);
100133}
101134
102135void towire_fulfilled_htlc (u8 * * pptr , const struct fulfilled_htlc * fulfilled )
@@ -163,6 +196,20 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
163196 fromwire_pubkey (cursor , max , added -> path_key );
164197 } else
165198 added -> path_key = NULL ;
199+ if (fromwire_bool (cursor , max )) {
200+ size_t tlv_len = fromwire_u16 (cursor , max );
201+ /* NOTE: We might consider to be more strict and only allow for
202+ * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
203+ * record. */
204+ const u64 * allowed = cast_const (u64 * , FROMWIRE_TLV_ANY_TYPE );
205+ added -> extra_tlvs = tal_arr (added , struct tlv_field , 0 );
206+ if (!fromwire_tlv (cursor , & tlv_len , NULL , 0 , added ,
207+ & added -> extra_tlvs , allowed , NULL , NULL )) {
208+ tal_free (added -> extra_tlvs );
209+ added -> extra_tlvs = NULL ;
210+ }
211+ } else
212+ added -> extra_tlvs = NULL ;
166213 added -> fail_immediate = fromwire_bool (cursor , max );
167214}
168215
@@ -192,6 +239,20 @@ struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,
192239 fromwire_pubkey (cursor , max , existing -> path_key );
193240 } else
194241 existing -> path_key = NULL ;
242+ if (fromwire_bool (cursor , max )) {
243+ size_t tlv_len = fromwire_u16 (cursor , max );
244+ /* NOTE: We might consider to be more strict and only allow for
245+ * known tlv types from the tlvs_tlv_update_add_htlc_tlvs
246+ * record. */
247+ const u64 * allowed = cast_const (u64 * , FROMWIRE_TLV_ANY_TYPE );
248+ existing -> extra_tlvs = tal_arr (existing , struct tlv_field , 0 );
249+ if (!fromwire_tlv (cursor , & tlv_len , NULL , 0 , existing ,
250+ & existing -> extra_tlvs , allowed , NULL , NULL )) {
251+ tal_free (existing -> extra_tlvs );
252+ existing -> extra_tlvs = NULL ;
253+ }
254+ } else
255+ existing -> extra_tlvs = NULL ;
195256 return existing ;
196257}
197258
0 commit comments