@@ -118,11 +118,12 @@ use crate::ln::channelmanager::PaymentId;
118
118
use crate :: types:: features:: { Bolt12InvoiceFeatures , InvoiceRequestFeatures , OfferFeatures } ;
119
119
use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN } ;
120
120
use crate :: ln:: msgs:: DecodeError ;
121
+ use crate :: offers:: alloc:: WithRoundedCapacity ;
121
122
use crate :: offers:: invoice_macros:: { invoice_accessors_common, invoice_builder_methods_common} ;
122
123
#[ cfg( test) ]
123
124
use crate :: offers:: invoice_macros:: invoice_builder_methods_test;
124
125
use 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 } ;
126
127
use crate :: offers:: nonce:: Nonce ;
127
128
use crate :: offers:: offer:: { Amount , EXPERIMENTAL_OFFER_TYPES , ExperimentalOfferTlvStream , ExperimentalOfferTlvStreamRef , OFFER_TYPES , OfferTlvStream , OfferTlvStreamRef , Quantity } ;
128
129
use crate :: offers:: parse:: { Bolt12ParseError , Bolt12SemanticError , ParsedMessage } ;
@@ -502,32 +503,17 @@ impl UnsignedBolt12Invoice {
502
503
let ( _, _, _, invoice_tlv_stream, _, _, experimental_invoice_tlv_stream) =
503
504
contents. as_tlv_stream ( ) ;
504
505
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
-
519
506
// Allocate enough space for the invoice, which will include:
520
507
// - all TLV records from `invreq_bytes` except signatures,
521
508
// - all invoice-specific TLV records, and
522
509
// - a signature TLV record once the invoice is signed.
523
510
//
524
511
// 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 (
527
514
invreq_bytes. len ( )
528
- - signature_tlv_stream_len
529
515
+ invoice_tlv_stream. serialized_length ( )
530
- + signature_len
516
+ + if contents . is_for_offer ( ) { 0 } else { SIGNATURE_TLV_RECORD_SIZE }
531
517
+ experimental_invoice_tlv_stream. serialized_length ( ) ,
532
518
) ;
533
519
@@ -545,7 +531,7 @@ impl UnsignedBolt12Invoice {
545
531
let mut experimental_tlv_stream = TlvStream :: new ( remaining_bytes)
546
532
. range ( EXPERIMENTAL_TYPES )
547
533
. peekable ( ) ;
548
- let mut experimental_bytes = Vec :: with_capacity (
534
+ let mut experimental_bytes = Vec :: with_rounded_capacity (
549
535
remaining_bytes. len ( )
550
536
- experimental_tlv_stream
551
537
. peek ( )
@@ -558,7 +544,6 @@ impl UnsignedBolt12Invoice {
558
544
}
559
545
560
546
experimental_invoice_tlv_stream. write ( & mut experimental_bytes) . unwrap ( ) ;
561
- debug_assert_eq ! ( experimental_bytes. len( ) , experimental_bytes. capacity( ) ) ;
562
547
563
548
let tlv_stream = TlvStream :: new ( & bytes) . chain ( TlvStream :: new ( & experimental_bytes) ) ;
564
549
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
589
574
signature_tlv_stream. write( & mut $self. bytes) . unwrap( ) ;
590
575
591
576
// 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
- ) ;
600
577
$self. bytes. extend_from_slice( & $self. experimental_bytes) ;
601
578
602
579
Ok ( Bolt12Invoice {
0 commit comments