@@ -118,11 +118,12 @@ use crate::ln::channelmanager::PaymentId;
118118use crate :: types:: features:: { Bolt12InvoiceFeatures , InvoiceRequestFeatures , OfferFeatures } ;
119119use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN } ;
120120use crate :: ln:: msgs:: DecodeError ;
121+ use crate :: offers:: alloc:: WithRoundedCapacity ;
121122use crate :: offers:: invoice_macros:: { invoice_accessors_common, invoice_builder_methods_common} ;
122123#[ cfg( test) ]
123124use crate :: offers:: invoice_macros:: invoice_builder_methods_test;
124125use crate :: offers:: invoice_request:: { EXPERIMENTAL_INVOICE_REQUEST_TYPES , ExperimentalInvoiceRequestTlvStream , ExperimentalInvoiceRequestTlvStreamRef , INVOICE_REQUEST_PAYER_ID_TYPE , INVOICE_REQUEST_TYPES , IV_BYTES as INVOICE_REQUEST_IV_BYTES , InvoiceRequest , InvoiceRequestContents , InvoiceRequestTlvStream , InvoiceRequestTlvStreamRef } ;
125- use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , self , SIGNATURE_TLV_RECORD_SIZE , SIGNATURE_TYPES } ;
126+ use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , self , SIGNATURE_TLV_RECORD_SIZE } ;
126127use crate :: offers:: nonce:: Nonce ;
127128use crate :: offers:: offer:: { Amount , EXPERIMENTAL_OFFER_TYPES , ExperimentalOfferTlvStream , ExperimentalOfferTlvStreamRef , OFFER_TYPES , OfferTlvStream , OfferTlvStreamRef , Quantity } ;
128129use crate :: offers:: parse:: { Bolt12ParseError , Bolt12SemanticError , ParsedMessage } ;
@@ -502,32 +503,17 @@ impl UnsignedBolt12Invoice {
502503 let ( _, _, _, invoice_tlv_stream, _, _, experimental_invoice_tlv_stream) =
503504 contents. as_tlv_stream ( ) ;
504505
505- let mut signature_tlv_stream = TlvStream :: new ( invreq_bytes)
506- . range ( SIGNATURE_TYPES )
507- . peekable ( ) ;
508- let signature_len = signature_tlv_stream
509- . peek ( )
510- . map_or ( SIGNATURE_TLV_RECORD_SIZE , |record| record. end - record. start ) ;
511- let signature_tlv_stream_start = signature_tlv_stream
512- . peek ( )
513- . map_or ( 0 , |first_record| first_record. start ) ;
514- let signature_tlv_stream_end = signature_tlv_stream
515- . last ( )
516- . map_or ( 0 , |last_record| last_record. end ) ;
517- let signature_tlv_stream_len = signature_tlv_stream_end - signature_tlv_stream_start;
518-
519506 // Allocate enough space for the invoice, which will include:
520507 // - all TLV records from `invreq_bytes` except signatures,
521508 // - all invoice-specific TLV records, and
522509 // - a signature TLV record once the invoice is signed.
523510 //
524511 // This assumes the invoice will only have one signature using the same number of bytes as
525- // the first (and probably only) signature from the invoice request.
526- let mut bytes = Vec :: with_capacity (
512+ // the invoice request's signature .
513+ let mut bytes = Vec :: with_rounded_capacity (
527514 invreq_bytes. len ( )
528- - signature_tlv_stream_len
529515 + invoice_tlv_stream. serialized_length ( )
530- + signature_len
516+ + if contents . is_for_offer ( ) { 0 } else { SIGNATURE_TLV_RECORD_SIZE }
531517 + experimental_invoice_tlv_stream. serialized_length ( ) ,
532518 ) ;
533519
@@ -545,7 +531,7 @@ impl UnsignedBolt12Invoice {
545531 let mut experimental_tlv_stream = TlvStream :: new ( remaining_bytes)
546532 . range ( EXPERIMENTAL_TYPES )
547533 . peekable ( ) ;
548- let mut experimental_bytes = Vec :: with_capacity (
534+ let mut experimental_bytes = Vec :: with_rounded_capacity (
549535 remaining_bytes. len ( )
550536 - experimental_tlv_stream
551537 . peek ( )
@@ -558,7 +544,6 @@ impl UnsignedBolt12Invoice {
558544 }
559545
560546 experimental_invoice_tlv_stream. write ( & mut experimental_bytes) . unwrap ( ) ;
561- debug_assert_eq ! ( experimental_bytes. len( ) , experimental_bytes. capacity( ) ) ;
562547
563548 let tlv_stream = TlvStream :: new ( & bytes) . chain ( TlvStream :: new ( & experimental_bytes) ) ;
564549 let tagged_hash = TaggedHash :: from_tlv_stream ( SIGNATURE_TAG , tlv_stream) ;
@@ -589,14 +574,6 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
589574 signature_tlv_stream. write( & mut $self. bytes) . unwrap( ) ;
590575
591576 // Append the experimental bytes after the signature.
592- debug_assert_eq!(
593- // The two-byte overallocation results from SIGNATURE_TLV_RECORD_SIZE accommodating TLV
594- // records with types >= 253.
595- $self. bytes. len( )
596- + $self. experimental_bytes. len( )
597- + if $self. contents. is_for_offer( ) { 0 } else { 2 } ,
598- $self. bytes. capacity( ) ,
599- ) ;
600577 $self. bytes. extend_from_slice( & $self. experimental_bytes) ;
601578
602579 Ok ( Bolt12Invoice {
0 commit comments