1
+ #include "bitcoin/pubkey.h"
2
+ #include "ccan/tal/tal.h"
3
+ #include "common/json_parse_simple.h"
4
+ #include "common/json_stream.h"
5
+ #include "common/utils.h"
1
6
#include "config.h"
7
+ #include "jsmn.h"
8
+ #include "lightningd/log.h"
9
+ #include "wire/peer_wiregen.h"
2
10
#include <ccan/cast/cast.h>
3
11
#include <ccan/mem/mem.h>
4
12
#include <ccan/tal/str/str.h>
11
19
#include <common/onion_decode.h>
12
20
#include <common/onionreply.h>
13
21
#include <common/timeout.h>
22
+ #include <common/bigsize.h>
14
23
#include <connectd/connectd_wiregen.h>
15
24
#include <db/exec.h>
16
25
#include <gossipd/gossipd_wiregen.h>
23
32
#include <lightningd/plugin_hook.h>
24
33
#include <lightningd/subd.h>
25
34
#include <onchaind/onchaind_wiregen.h>
35
+ #include <stdio.h>
36
+ #include <wire/tlvstream.h>
37
+ #include <wire/onion_wiregen.h>
26
38
27
39
#ifndef SUPERVERBOSE
28
40
#define SUPERVERBOSE (...)
@@ -695,6 +707,7 @@ const u8 *send_htlc_out(const tal_t *ctx,
695
707
struct amount_msat final_msat ,
696
708
const struct sha256 * payment_hash ,
697
709
const struct pubkey * path_key ,
710
+ const struct tlv_field * extra_tlvs ,
698
711
u64 partid ,
699
712
u64 groupid ,
700
713
const u8 * onion_routing_packet ,
@@ -729,7 +742,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
729
742
/* Make peer's daemon own it, catch if it dies. */
730
743
* houtp = new_htlc_out (out -> owner , out , amount , cltv ,
731
744
payment_hash , onion_routing_packet ,
732
- path_key , in == NULL ,
745
+ path_key , extra_tlvs ,
746
+ in == NULL ,
733
747
final_msat ,
734
748
partid , groupid , in );
735
749
tal_add_destructor (* houtp , destroy_hout_subd_died );
@@ -742,6 +756,13 @@ const u8 *send_htlc_out(const tal_t *ctx,
742
756
* houtp );
743
757
}
744
758
759
+ if (extra_tlvs ) {
760
+ raw_tlvs = tal_arr (tmpctx , u8 , 0 );
761
+ towire_tlvstream_raw (& raw_tlvs ,
762
+ tal_dup_talarr (tmpctx , struct tlv_field ,
763
+ extra_tlvs ));
764
+ }
765
+
745
766
msg = towire_channeld_offer_htlc (out , amount , cltv , payment_hash ,
746
767
onion_routing_packet , path_key ,
747
768
raw_tlvs );
@@ -797,7 +818,8 @@ static void forward_htlc(struct htlc_in *hin,
797
818
const struct short_channel_id * forward_scid ,
798
819
const struct channel_id * forward_to ,
799
820
const u8 next_onion [TOTAL_PACKET_SIZE (ROUTING_INFO_SIZE )],
800
- const struct pubkey * next_path_key )
821
+ const struct pubkey * next_path_key ,
822
+ const struct tlv_field * extra_tlvs )
801
823
{
802
824
const u8 * failmsg ;
803
825
struct lightningd * ld = hin -> key .channel -> peer -> ld ;
@@ -912,7 +934,7 @@ static void forward_htlc(struct htlc_in *hin,
912
934
failmsg = send_htlc_out (tmpctx , next , amt_to_forward ,
913
935
outgoing_cltv_value , AMOUNT_MSAT (0 ),
914
936
& hin -> payment_hash ,
915
- next_path_key , 0 /* partid */ , 0 /* groupid */ ,
937
+ next_path_key , extra_tlvs , 0 /* partid */ , 0 /* groupid */ ,
916
938
next_onion , hin , & hout );
917
939
if (!failmsg )
918
940
return ;
@@ -942,6 +964,7 @@ struct htlc_accepted_hook_payload {
942
964
u64 failtlvtype ;
943
965
size_t failtlvpos ;
944
966
const char * failexplanation ;
967
+ u8 * extra_tlvs_raw ;
945
968
};
946
969
947
970
static void
@@ -998,8 +1021,8 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
998
1021
struct htlc_in * hin = request -> hin ;
999
1022
struct lightningd * ld = request -> ld ;
1000
1023
struct preimage payment_preimage ;
1001
- const jsmntok_t * resulttok , * paykeytok , * payloadtok , * fwdtok ;
1002
- u8 * failonion ;
1024
+ const jsmntok_t * resulttok , * paykeytok , * payloadtok , * fwdtok , * extra_tlvs_tok ;
1025
+ u8 * failonion , * raw_tlvs ;
1003
1026
1004
1027
if (!toks || !buffer )
1005
1028
return true;
@@ -1013,6 +1036,49 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
1013
1036
json_strdup (tmpctx , buffer , toks ));
1014
1037
}
1015
1038
1039
+ extra_tlvs_tok = json_get_member (buffer , toks , "extra_tlvs" );
1040
+ if (extra_tlvs_tok ) {
1041
+ size_t max ;
1042
+ struct tlv_update_add_htlc_tlvs * check_extra_tlvs ;
1043
+
1044
+ raw_tlvs = json_tok_bin_from_hex (tmpctx , buffer ,
1045
+ extra_tlvs_tok );
1046
+ if (!raw_tlvs )
1047
+ fatal ("Bad custom tlvs for htlc_accepted"
1048
+ " hook: %.*s" ,
1049
+ extra_tlvs_tok -> end - extra_tlvs_tok -> start ,
1050
+ buffer + extra_tlvs_tok -> start );
1051
+
1052
+ max = tal_bytelen (raw_tlvs );
1053
+
1054
+ /* We check if the custom tlvs are still valid BOLT#1 tlvs.
1055
+ * As these are appended to forwarded htlcs we check for valid
1056
+ * update_add_htlc_tlvs (restricts to known even types).
1057
+ * NOTE: We may be less strict and allow unknown evens .*/
1058
+ const u8 * cursor = raw_tlvs ;
1059
+ check_extra_tlvs = fromwire_tlv_update_add_htlc_tlvs (tmpctx ,
1060
+ & cursor ,
1061
+ & max );
1062
+ if (!check_extra_tlvs ) {
1063
+ fatal ("htlc_accepted_hook returned bad extra_tlvs %s" ,
1064
+ tal_hex (tmpctx , raw_tlvs ));
1065
+ }
1066
+
1067
+ /* If we got a blinded path key we replace the next path key
1068
+ * with it. */
1069
+ if (check_extra_tlvs -> blinded_path ) {
1070
+ tal_free (request -> next_path_key );
1071
+ request -> next_path_key
1072
+ = tal_steal (request ,
1073
+ check_extra_tlvs -> blinded_path );
1074
+ }
1075
+
1076
+ /* We made it and got a valid extra_tlvs: Replace the current
1077
+ * extra_tlvs with it. */
1078
+ tal_free (request -> extra_tlvs_raw );
1079
+ request -> extra_tlvs_raw = tal_steal (request , raw_tlvs );
1080
+ }
1081
+
1016
1082
payloadtok = json_get_member (buffer , toks , "payload" );
1017
1083
if (payloadtok ) {
1018
1084
u8 * payload = json_tok_bin_from_hex (rs , buffer , payloadtok );
@@ -1170,6 +1236,9 @@ static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p,
1170
1236
json_add_u32 (s , "cltv_expiry" , expiry );
1171
1237
json_add_s32 (s , "cltv_expiry_relative" , expiry - blockheight );
1172
1238
json_add_sha256 (s , "payment_hash" , & hin -> payment_hash );
1239
+ if (p -> extra_tlvs_raw ) {
1240
+ json_add_hex_talarr (s , "extra_tlvs" , p -> extra_tlvs_raw );
1241
+ }
1173
1242
json_object_end (s );
1174
1243
}
1175
1244
@@ -1200,13 +1269,25 @@ htlc_accepted_hook_final(struct htlc_accepted_hook_payload *request STEALS)
1200
1269
NULL , request -> failtlvtype ,
1201
1270
request -> failtlvpos )));
1202
1271
} else if (rs -> nextcase == ONION_FORWARD ) {
1272
+ struct tlv_field * extra_tlvs ;
1273
+
1274
+ if (request -> extra_tlvs_raw ) {
1275
+ const u8 * cursor = request -> extra_tlvs_raw ;
1276
+ size_t max = tal_bytelen (cursor );
1277
+ extra_tlvs = tal_arr (request , struct tlv_field , 0 );
1278
+ fromwire_tlv (& cursor , & max , NULL , 0 , request ,
1279
+ & extra_tlvs , NULL , NULL , NULL );
1280
+ } else {
1281
+ extra_tlvs = NULL ;
1282
+ }
1283
+
1203
1284
forward_htlc (hin , hin -> cltv_expiry ,
1204
1285
request -> payload -> amt_to_forward ,
1205
1286
request -> payload -> outgoing_cltv ,
1206
1287
request -> payload -> forward_channel ,
1207
1288
request -> fwd_channel_id ,
1208
1289
serialize_onionpacket (tmpctx , rs -> next ),
1209
- request -> next_path_key );
1290
+ request -> next_path_key , extra_tlvs );
1210
1291
} else
1211
1292
handle_localpay (hin ,
1212
1293
request -> payload -> amt_to_forward ,
@@ -1484,6 +1565,14 @@ static bool peer_accepted_htlc(const tal_t *ctx,
1484
1565
hook_payload -> fwd_channel_id
1485
1566
= calc_forwarding_channel (ld , hook_payload );
1486
1567
1568
+ if (hin -> extra_tlvs ) {
1569
+ hook_payload -> extra_tlvs_raw = tal_arr (hook_payload , u8 , 0 );
1570
+ towire_tlvstream_raw (& hook_payload -> extra_tlvs_raw ,
1571
+ hin -> extra_tlvs );
1572
+ } else {
1573
+ hook_payload -> extra_tlvs_raw = NULL ;
1574
+ }
1575
+
1487
1576
plugin_hook_call_htlc_accepted (ld , NULL , hook_payload );
1488
1577
1489
1578
/* Falling through here is ok, after all the HTLC locked */
@@ -2210,6 +2299,7 @@ static bool channel_added_their_htlc(struct channel *channel,
2210
2299
op ? & shared_secret : NULL ,
2211
2300
added -> path_key ,
2212
2301
added -> onion_routing_packet ,
2302
+ added -> extra_tlvs ,
2213
2303
added -> fail_immediate );
2214
2304
2215
2305
/* Save an incoming htlc to the wallet */
@@ -2641,13 +2731,15 @@ const struct existing_htlc **peer_htlcs(const tal_t *ctx,
2641
2731
else
2642
2732
f = NULL ;
2643
2733
2734
+
2644
2735
existing = new_existing_htlc (htlcs , hin -> key .id , hin -> hstate ,
2645
2736
hin -> msat , & hin -> payment_hash ,
2646
2737
hin -> cltv_expiry ,
2647
2738
hin -> onion_routing_packet ,
2648
2739
hin -> path_key ,
2649
2740
hin -> preimage ,
2650
- f , NULL );
2741
+ f ,
2742
+ hin -> extra_tlvs );
2651
2743
tal_arr_expand (& htlcs , existing );
2652
2744
}
2653
2745
@@ -2679,7 +2771,8 @@ const struct existing_htlc **peer_htlcs(const tal_t *ctx,
2679
2771
hout -> onion_routing_packet ,
2680
2772
hout -> path_key ,
2681
2773
hout -> preimage ,
2682
- f , NULL );
2774
+ f ,
2775
+ hout -> extra_tlvs );
2683
2776
tal_arr_expand (& htlcs , existing );
2684
2777
}
2685
2778
0 commit comments