@@ -516,6 +516,33 @@ static char *decode_9(struct bolt11 *b11,
516516 return NULL ;
517517}
518518
519+ /* BOLT #11:
520+ *
521+ * `m` (27): `data_length` variable. Additional metadata to attach to
522+ * the payment. Note that the size of this field is limited by the
523+ * maximum hop payload size. Long metadata fields reduce the maximum
524+ * route length.
525+ */
526+ static char * decode_m (struct bolt11 * b11 ,
527+ struct hash_u5 * hu5 ,
528+ u5 * * data , size_t * data_len ,
529+ size_t data_length ,
530+ bool * have_m )
531+ {
532+ size_t mlen = (data_length * 5 ) / 8 ;
533+
534+ if (* have_m )
535+ return unknown_field (b11 , hu5 , data , data_len , 'm' ,
536+ data_length );
537+
538+ b11 -> metadata = tal_arr (b11 , u8 , mlen );
539+ pull_bits_certain (hu5 , data , data_len , b11 -> metadata ,
540+ data_length * 5 , false);
541+
542+ * have_m = true;
543+ return NULL ;
544+ }
545+
519546struct bolt11 * new_bolt11 (const tal_t * ctx ,
520547 const struct amount_msat * msat TAKES )
521548{
@@ -535,6 +562,7 @@ struct bolt11 *new_bolt11(const tal_t *ctx,
535562 */
536563 b11 -> min_final_cltv_expiry = 18 ;
537564 b11 -> payment_secret = NULL ;
565+ b11 -> metadata = NULL ;
538566
539567 if (msat )
540568 b11 -> msat = tal_dup (b11 , struct amount_msat , msat );
@@ -557,7 +585,7 @@ struct bolt11 *bolt11_decode_nosig(const tal_t *ctx, const char *str,
557585 struct bolt11 * b11 = new_bolt11 (ctx , NULL );
558586 struct hash_u5 hu5 ;
559587 bool have_p = false, have_d = false, have_h = false,
560- have_x = false, have_c = false, have_s = false;
588+ have_x = false, have_c = false, have_s = false, have_m = false ;
561589
562590 * have_n = false;
563591 b11 -> routes = tal_arr (b11 , struct route_info * , 0 );
@@ -760,6 +788,10 @@ struct bolt11 *bolt11_decode_nosig(const tal_t *ctx, const char *str,
760788 problem = decode_s (b11 , & hu5 , & data , & data_len ,
761789 data_length , & have_s );
762790 break ;
791+ case 'm' :
792+ problem = decode_m (b11 , & hu5 , & data , & data_len ,
793+ data_length , & have_m );
794+ break ;
763795 default :
764796 unknown_field (b11 , & hu5 , & data , & data_len ,
765797 bech32_charset [type ], data_length );
@@ -936,6 +968,11 @@ static void encode_p(u5 **data, const struct sha256 *hash)
936968 push_field (data , 'p' , hash , 256 );
937969}
938970
971+ static void encode_m (u5 * * data , const u8 * metadata )
972+ {
973+ push_field (data , 'm' , metadata , tal_bytelen (metadata ) * CHAR_BIT );
974+ }
975+
939976static void encode_d (u5 * * data , const char * description )
940977{
941978 push_field (data , 'd' , description , strlen (description ) * CHAR_BIT );
@@ -1011,13 +1048,20 @@ static void encode_r(u5 **data, const struct route_info *r)
10111048 tal_free (rinfo );
10121049}
10131050
1014- static void maybe_encode_9 (u5 * * data , const u8 * features )
1051+ static void maybe_encode_9 (u5 * * data , const u8 * features ,
1052+ bool have_payment_metadata )
10151053{
10161054 u5 * f5 = tal_arr (NULL , u5 , 0 );
10171055
10181056 for (size_t i = 0 ; i < tal_count (features ) * CHAR_BIT ; i ++ ) {
10191057 if (!feature_is_set (features , i ))
10201058 continue ;
1059+
1060+ /* Don't set option_payment_metadata unless we acually use it */
1061+ if (!have_payment_metadata
1062+ && COMPULSORY_FEATURE (i ) == OPT_PAYMENT_METADATA )
1063+ continue ;
1064+
10211065 /* We expand it out so it makes a BE 5-bit/btye bitfield */
10221066 set_feature_bit (& f5 , (i / 5 ) * 8 + (i % 5 ));
10231067 }
@@ -1131,6 +1175,9 @@ char *bolt11_encode_(const tal_t *ctx,
11311175 if (b11 -> expiry != DEFAULT_X )
11321176 encode_x (& data , b11 -> expiry );
11331177
1178+ if (b11 -> metadata )
1179+ encode_m (& data , b11 -> metadata );
1180+
11341181 /* BOLT #11:
11351182 * - MUST include one `c` field (`min_final_cltv_expiry`).
11361183 */
@@ -1145,7 +1192,7 @@ char *bolt11_encode_(const tal_t *ctx,
11451192 for (size_t i = 0 ; i < tal_count (b11 -> routes ); i ++ )
11461193 encode_r (& data , b11 -> routes [i ]);
11471194
1148- maybe_encode_9 (& data , b11 -> features );
1195+ maybe_encode_9 (& data , b11 -> features , b11 -> metadata != NULL );
11491196
11501197 list_for_each (& b11 -> extra_fields , extra , list )
11511198 if (!encode_extra (& data , extra ))
0 commit comments