@@ -887,7 +887,8 @@ impl VerifiedInvoiceRequest {
887887 amount_msats : * amount_msats,
888888 features : features. clone ( ) ,
889889 quantity : * quantity,
890- payer_note : payer_note. clone ( ) . map ( |mut s| { s. truncate ( 512 ) ; UntrustedString ( s) } ) ,
890+ payer_note_truncated : payer_note. clone ( )
891+ . map ( |mut s| { s. truncate ( PAYER_NOTE_LIMIT ) ; UntrustedString ( s) } ) ,
891892 }
892893 }
893894}
@@ -1142,10 +1143,13 @@ pub struct InvoiceRequestFields {
11421143 pub quantity : Option < u64 > ,
11431144
11441145 /// A payer-provided note which will be seen by the recipient and reflected back in the invoice
1145- /// response.
1146- pub payer_note : Option < UntrustedString > ,
1146+ /// response. Truncated to [`PAYER_NOTE_LIMIT`] characters.
1147+ pub payer_note_truncated : Option < UntrustedString > ,
11471148}
11481149
1150+ /// The maximum number of characters included in [`InvoiceRequestFields::payer_note_truncated`].
1151+ pub const PAYER_NOTE_LIMIT : usize = 512 ;
1152+
11491153impl Writeable for InvoiceRequestFields {
11501154 fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
11511155 write_tlv_fields ! ( writer, {
@@ -1154,7 +1158,7 @@ impl Writeable for InvoiceRequestFields {
11541158 ( 4 , self . amount_msats, option) ,
11551159 ( 6 , WithoutLength ( & self . features) , required) ,
11561160 ( 8 , self . quantity, option) ,
1157- ( 10 , self . payer_note , option) ,
1161+ ( 10 , self . payer_note_truncated , option) ,
11581162 } ) ;
11591163 Ok ( ( ) )
11601164 }
@@ -1168,19 +1172,20 @@ impl Readable for InvoiceRequestFields {
11681172 ( 4 , amount_msats, option) ,
11691173 ( 6 , features, ( option, encoding: ( InvoiceRequestFeatures , WithoutLength ) ) ) ,
11701174 ( 8 , quantity, option) ,
1171- ( 10 , payer_note , option) ,
1175+ ( 10 , payer_note_truncated , option) ,
11721176 } ) ;
11731177 let features = features. unwrap_or ( InvoiceRequestFeatures :: empty ( ) ) ;
11741178
11751179 Ok ( InvoiceRequestFields {
1176- payer_id : payer_id. 0 . unwrap ( ) , chain, amount_msats, features, quantity, payer_note
1180+ payer_id : payer_id. 0 . unwrap ( ) , chain, amount_msats, features, quantity,
1181+ payer_note_truncated,
11771182 } )
11781183 }
11791184}
11801185
11811186#[ cfg( test) ]
11821187mod tests {
1183- use super :: { InvoiceRequest , InvoiceRequestFields , InvoiceRequestTlvStreamRef , SIGNATURE_TAG , UnsignedInvoiceRequest } ;
1188+ use super :: { InvoiceRequest , InvoiceRequestFields , InvoiceRequestTlvStreamRef , PAYER_NOTE_LIMIT , SIGNATURE_TAG , UnsignedInvoiceRequest } ;
11841189
11851190 use bitcoin:: blockdata:: constants:: ChainHash ;
11861191 use bitcoin:: network:: constants:: Network ;
@@ -2267,7 +2272,7 @@ mod tests {
22672272 . chain ( Network :: Testnet ) . unwrap ( )
22682273 . amount_msats ( 1001 ) . unwrap ( )
22692274 . quantity ( 1 ) . unwrap ( )
2270- . payer_note ( "0" . repeat ( 1024 ) )
2275+ . payer_note ( "0" . repeat ( PAYER_NOTE_LIMIT * 2 ) )
22712276 . build ( ) . unwrap ( )
22722277 . sign ( payer_sign) . unwrap ( ) ;
22732278 match invoice_request. verify ( & expanded_key, & secp_ctx) {
@@ -2281,7 +2286,7 @@ mod tests {
22812286 amount_msats: Some ( 1001 ) ,
22822287 features: InvoiceRequestFeatures :: empty( ) ,
22832288 quantity: Some ( 1 ) ,
2284- payer_note : Some ( UntrustedString ( "0" . repeat( 512 ) ) ) ,
2289+ payer_note_truncated : Some ( UntrustedString ( "0" . repeat( PAYER_NOTE_LIMIT ) ) ) ,
22852290 }
22862291 ) ;
22872292 } ,
0 commit comments