@@ -363,6 +363,8 @@ macro_rules! invoice_builder_methods { (
363
363
InvoiceFields {
364
364
payment_paths, created_at, relative_expiry: None , payment_hash, amount_msats,
365
365
fallbacks: None , features: Bolt12InvoiceFeatures :: empty( ) , signing_pubkey,
366
+ #[ cfg( test) ]
367
+ experimental_baz: None ,
366
368
}
367
369
}
368
370
@@ -634,6 +636,8 @@ struct InvoiceFields {
634
636
fallbacks : Option < Vec < FallbackAddress > > ,
635
637
features : Bolt12InvoiceFeatures ,
636
638
signing_pubkey : PublicKey ,
639
+ #[ cfg( test) ]
640
+ experimental_baz : Option < u64 > ,
637
641
}
638
642
639
643
macro_rules! invoice_accessors { ( $self: ident, $contents: expr) => {
@@ -1217,7 +1221,10 @@ impl InvoiceFields {
1217
1221
node_id : Some ( & self . signing_pubkey ) ,
1218
1222
message_paths : None ,
1219
1223
} ,
1220
- ExperimentalInvoiceTlvStreamRef { } ,
1224
+ ExperimentalInvoiceTlvStreamRef {
1225
+ #[ cfg( test) ]
1226
+ experimental_baz : self . experimental_baz ,
1227
+ } ,
1221
1228
)
1222
1229
}
1223
1230
}
@@ -1306,12 +1313,20 @@ tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef<'a>, INVOICE_TYPES, {
1306
1313
} ) ;
1307
1314
1308
1315
/// Valid type range for experimental invoice TLV records.
1309
- const EXPERIMENTAL_INVOICE_TYPES : core:: ops:: RangeFrom < u64 > = 3_000_000_000 ..;
1316
+ pub ( super ) const EXPERIMENTAL_INVOICE_TYPES : core:: ops:: RangeFrom < u64 > = 3_000_000_000 ..;
1310
1317
1318
+ #[ cfg( not( test) ) ]
1311
1319
tlv_stream ! (
1312
1320
ExperimentalInvoiceTlvStream , ExperimentalInvoiceTlvStreamRef , EXPERIMENTAL_INVOICE_TYPES , { }
1313
1321
) ;
1314
1322
1323
+ #[ cfg( test) ]
1324
+ tlv_stream ! (
1325
+ ExperimentalInvoiceTlvStream , ExperimentalInvoiceTlvStreamRef , EXPERIMENTAL_INVOICE_TYPES , {
1326
+ ( 3_999_999_999 , experimental_baz: ( u64 , HighZeroBytesDroppedBigSize ) ) ,
1327
+ }
1328
+ ) ;
1329
+
1315
1330
pub ( super ) type BlindedPathIter < ' a > = core:: iter:: Map <
1316
1331
core:: slice:: Iter < ' a , BlindedPaymentPath > ,
1317
1332
for <' r > fn ( & ' r BlindedPaymentPath ) -> & ' r BlindedPath ,
@@ -1446,7 +1461,10 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
1446
1461
} ,
1447
1462
experimental_offer_tlv_stream,
1448
1463
experimental_invoice_request_tlv_stream,
1449
- ExperimentalInvoiceTlvStream { } ,
1464
+ ExperimentalInvoiceTlvStream {
1465
+ #[ cfg( test) ]
1466
+ experimental_baz,
1467
+ } ,
1450
1468
) = tlv_stream;
1451
1469
1452
1470
if message_paths. is_some ( ) { return Err ( Bolt12SemanticError :: UnexpectedPaths ) }
@@ -1473,6 +1491,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
1473
1491
let fields = InvoiceFields {
1474
1492
payment_paths, created_at, relative_expiry, payment_hash, amount_msats, fallbacks,
1475
1493
features, signing_pubkey,
1494
+ #[ cfg( test) ]
1495
+ experimental_baz,
1476
1496
} ;
1477
1497
1478
1498
check_invoice_signing_pubkey ( & fields. signing_pubkey , & offer_tlv_stream) ?;
@@ -1543,7 +1563,7 @@ pub(super) fn check_invoice_signing_pubkey(
1543
1563
1544
1564
#[ cfg( test) ]
1545
1565
mod tests {
1546
- use super :: { Bolt12Invoice , DEFAULT_RELATIVE_EXPIRY , ExperimentalInvoiceTlvStreamRef , FallbackAddress , FullInvoiceTlvStreamRef , INVOICE_TYPES , InvoiceTlvStreamRef , SIGNATURE_TAG , UnsignedBolt12Invoice } ;
1566
+ use super :: { Bolt12Invoice , DEFAULT_RELATIVE_EXPIRY , EXPERIMENTAL_INVOICE_TYPES , ExperimentalInvoiceTlvStreamRef , FallbackAddress , FullInvoiceTlvStreamRef , INVOICE_TYPES , InvoiceTlvStreamRef , SIGNATURE_TAG , UnsignedBolt12Invoice } ;
1547
1567
1548
1568
use bitcoin:: { CompressedPublicKey , WitnessProgram , WitnessVersion } ;
1549
1569
use bitcoin:: constants:: ChainHash ;
@@ -1563,7 +1583,7 @@ mod tests {
1563
1583
use crate :: ln:: inbound_payment:: ExpandedKey ;
1564
1584
use crate :: ln:: msgs:: DecodeError ;
1565
1585
use crate :: offers:: invoice_request:: { ExperimentalInvoiceRequestTlvStreamRef , InvoiceRequestTlvStreamRef } ;
1566
- use crate :: offers:: merkle:: { SignError , SignatureTlvStreamRef , TaggedHash , self } ;
1586
+ use crate :: offers:: merkle:: { SignError , SignatureTlvStreamRef , TaggedHash , TlvStream , self } ;
1567
1587
use crate :: offers:: nonce:: Nonce ;
1568
1588
use crate :: offers:: offer:: { Amount , ExperimentalOfferTlvStreamRef , OfferTlvStreamRef , Quantity } ;
1569
1589
use crate :: prelude:: * ;
@@ -1739,7 +1759,9 @@ mod tests {
1739
1759
ExperimentalInvoiceRequestTlvStreamRef {
1740
1760
experimental_bar: None ,
1741
1761
} ,
1742
- ExperimentalInvoiceTlvStreamRef { } ,
1762
+ ExperimentalInvoiceTlvStreamRef {
1763
+ experimental_baz: None ,
1764
+ } ,
1743
1765
) ,
1744
1766
) ;
1745
1767
@@ -1839,7 +1861,9 @@ mod tests {
1839
1861
ExperimentalInvoiceRequestTlvStreamRef {
1840
1862
experimental_bar: None ,
1841
1863
} ,
1842
- ExperimentalInvoiceTlvStreamRef { } ,
1864
+ ExperimentalInvoiceTlvStreamRef {
1865
+ experimental_baz: None ,
1866
+ } ,
1843
1867
) ,
1844
1868
) ;
1845
1869
@@ -2686,6 +2710,97 @@ mod tests {
2686
2710
}
2687
2711
}
2688
2712
2713
+ #[ test]
2714
+ fn parses_invoice_with_experimental_tlv_records ( ) {
2715
+ let secp_ctx = Secp256k1 :: new ( ) ;
2716
+ let keys = Keypair :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
2717
+ let invoice = OfferBuilder :: new ( keys. public_key ( ) )
2718
+ . amount_msats ( 1000 )
2719
+ . build ( ) . unwrap ( )
2720
+ . request_invoice ( vec ! [ 1 ; 32 ] , payer_pubkey ( ) ) . unwrap ( )
2721
+ . build ( ) . unwrap ( )
2722
+ . sign ( payer_sign) . unwrap ( )
2723
+ . respond_with_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) ) . unwrap ( )
2724
+ . experimental_baz ( 42 )
2725
+ . build ( ) . unwrap ( )
2726
+ . sign ( |message : & UnsignedBolt12Invoice |
2727
+ Ok ( secp_ctx. sign_schnorr_no_aux_rand ( message. as_ref ( ) . as_digest ( ) , & keys) )
2728
+ )
2729
+ . unwrap ( ) ;
2730
+
2731
+ let mut buffer = Vec :: new ( ) ;
2732
+ invoice. write ( & mut buffer) . unwrap ( ) ;
2733
+
2734
+ assert ! ( Bolt12Invoice :: try_from( buffer) . is_ok( ) ) ;
2735
+
2736
+ const UNKNOWN_ODD_TYPE : u64 = EXPERIMENTAL_INVOICE_TYPES . start + 1 ;
2737
+ assert ! ( UNKNOWN_ODD_TYPE % 2 == 1 ) ;
2738
+
2739
+ let mut unsigned_invoice = OfferBuilder :: new ( keys. public_key ( ) )
2740
+ . amount_msats ( 1000 )
2741
+ . build ( ) . unwrap ( )
2742
+ . request_invoice ( vec ! [ 1 ; 32 ] , payer_pubkey ( ) ) . unwrap ( )
2743
+ . build ( ) . unwrap ( )
2744
+ . sign ( payer_sign) . unwrap ( )
2745
+ . respond_with_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) ) . unwrap ( )
2746
+ . build ( ) . unwrap ( ) ;
2747
+
2748
+ BigSize ( UNKNOWN_ODD_TYPE ) . write ( & mut unsigned_invoice. experimental_bytes ) . unwrap ( ) ;
2749
+ BigSize ( 32 ) . write ( & mut unsigned_invoice. experimental_bytes ) . unwrap ( ) ;
2750
+ [ 42u8 ; 32 ] . write ( & mut unsigned_invoice. experimental_bytes ) . unwrap ( ) ;
2751
+
2752
+ let tlv_stream = TlvStream :: new ( & unsigned_invoice. bytes )
2753
+ . chain ( TlvStream :: new ( & unsigned_invoice. experimental_bytes ) ) ;
2754
+ unsigned_invoice. tagged_hash = TaggedHash :: from_tlv_stream ( SIGNATURE_TAG , tlv_stream) ;
2755
+
2756
+ let invoice = unsigned_invoice
2757
+ . sign ( |message : & UnsignedBolt12Invoice |
2758
+ Ok ( secp_ctx. sign_schnorr_no_aux_rand ( message. as_ref ( ) . as_digest ( ) , & keys) )
2759
+ )
2760
+ . unwrap ( ) ;
2761
+
2762
+ let mut encoded_invoice = Vec :: new ( ) ;
2763
+ invoice. write ( & mut encoded_invoice) . unwrap ( ) ;
2764
+
2765
+ if let Err ( e) = Bolt12Invoice :: try_from ( encoded_invoice) {
2766
+ panic ! ( "error parsing invoice: {:?}" , e) ;
2767
+ }
2768
+
2769
+ const UNKNOWN_EVEN_TYPE : u64 = EXPERIMENTAL_INVOICE_TYPES . start ;
2770
+ assert ! ( UNKNOWN_EVEN_TYPE % 2 == 0 ) ;
2771
+
2772
+ let mut unsigned_invoice = OfferBuilder :: new ( keys. public_key ( ) )
2773
+ . amount_msats ( 1000 )
2774
+ . build ( ) . unwrap ( )
2775
+ . request_invoice ( vec ! [ 1 ; 32 ] , payer_pubkey ( ) ) . unwrap ( )
2776
+ . build ( ) . unwrap ( )
2777
+ . sign ( payer_sign) . unwrap ( )
2778
+ . respond_with_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) ) . unwrap ( )
2779
+ . build ( ) . unwrap ( ) ;
2780
+
2781
+ BigSize ( UNKNOWN_EVEN_TYPE ) . write ( & mut unsigned_invoice. experimental_bytes ) . unwrap ( ) ;
2782
+ BigSize ( 32 ) . write ( & mut unsigned_invoice. experimental_bytes ) . unwrap ( ) ;
2783
+ [ 42u8 ; 32 ] . write ( & mut unsigned_invoice. experimental_bytes ) . unwrap ( ) ;
2784
+
2785
+ let tlv_stream = TlvStream :: new ( & unsigned_invoice. bytes )
2786
+ . chain ( TlvStream :: new ( & unsigned_invoice. experimental_bytes ) ) ;
2787
+ unsigned_invoice. tagged_hash = TaggedHash :: from_tlv_stream ( SIGNATURE_TAG , tlv_stream) ;
2788
+
2789
+ let invoice = unsigned_invoice
2790
+ . sign ( |message : & UnsignedBolt12Invoice |
2791
+ Ok ( secp_ctx. sign_schnorr_no_aux_rand ( message. as_ref ( ) . as_digest ( ) , & keys) )
2792
+ )
2793
+ . unwrap ( ) ;
2794
+
2795
+ let mut encoded_invoice = Vec :: new ( ) ;
2796
+ invoice. write ( & mut encoded_invoice) . unwrap ( ) ;
2797
+
2798
+ match Bolt12Invoice :: try_from ( encoded_invoice) {
2799
+ Ok ( _) => panic ! ( "expected error" ) ,
2800
+ Err ( e) => assert_eq ! ( e, Bolt12ParseError :: Decode ( DecodeError :: UnknownRequiredFeature ) ) ,
2801
+ }
2802
+ }
2803
+
2689
2804
#[ test]
2690
2805
fn fails_parsing_invoice_with_out_of_range_tlv_records ( ) {
2691
2806
let invoice = OfferBuilder :: new ( recipient_pubkey ( ) )
0 commit comments