@@ -124,7 +124,18 @@ pub struct RefundBuilder<'a, T: secp256k1::Signing> {
124124 secp_ctx : Option < & ' a Secp256k1 < T > > ,
125125}
126126
127- macro_rules! refund_without_secp256k1_builder_methods { ( ) => {
127+ /// Builds a [`Refund`] for the "offer for money" flow.
128+ ///
129+ /// See [module-level documentation] for usage.
130+ ///
131+ /// [module-level documentation]: self
132+ #[ cfg( c_bindings) ]
133+ pub struct RefundMaybeWithDerivedMetadataBuilder < ' a > {
134+ refund : RefundContents ,
135+ secp_ctx : Option < & ' a Secp256k1 < secp256k1:: All > > ,
136+ }
137+
138+ macro_rules! refund_explicit_metadata_builder_methods { ( ) => {
128139 /// Creates a new builder for a refund using the [`Refund::payer_id`] for the public node id to
129140 /// send to if no [`Refund::paths`] are set. Otherwise, it may be a transient pubkey.
130141 ///
@@ -158,7 +169,7 @@ macro_rules! refund_without_secp256k1_builder_methods { () => {
158169} }
159170
160171macro_rules! refund_builder_methods { (
161- $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
172+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr, $secp_context : ty $ ( , $self_mut : tt ) ?
162173) => {
163174 /// Similar to [`RefundBuilder::new`] except, if [`RefundBuilder::path`] is called, the payer id
164175 /// is derived from the given [`ExpandedKey`] and nonce. This provides sender privacy by using a
@@ -175,7 +186,7 @@ macro_rules! refund_builder_methods { (
175186 /// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
176187 pub fn deriving_payer_id<ES : Deref >(
177188 description: String , node_id: PublicKey , expanded_key: & ExpandedKey , entropy_source: ES ,
178- secp_ctx: & ' a Secp256k1 <T >, amount_msats: u64 , payment_id: PaymentId
189+ secp_ctx: & ' a Secp256k1 <$secp_context >, amount_msats: u64 , payment_id: PaymentId
179190 ) -> Result <Self , Bolt12SemanticError > where ES :: Target : EntropySource {
180191 if amount_msats > MAX_VALUE_MSAT {
181192 return Err ( Bolt12SemanticError :: InvalidAmount ) ;
@@ -199,15 +210,15 @@ macro_rules! refund_builder_methods { (
199210 /// already passed is valid and can be checked for using [`Refund::is_expired`].
200211 ///
201212 /// Successive calls to this method will override the previous setting.
202- pub fn absolute_expiry( mut $self: $self_type, absolute_expiry: Duration ) -> $return_type {
213+ pub fn absolute_expiry( $ ( $self_mut ) * $self: $self_type, absolute_expiry: Duration ) -> $return_type {
203214 $self. refund. absolute_expiry = Some ( absolute_expiry) ;
204215 $return_value
205216 }
206217
207218 /// Sets the [`Refund::issuer`].
208219 ///
209220 /// Successive calls to this method will override the previous setting.
210- pub fn issuer( mut $self: $self_type, issuer: String ) -> $return_type {
221+ pub fn issuer( $ ( $self_mut ) * $self: $self_type, issuer: String ) -> $return_type {
211222 $self. refund. issuer = Some ( issuer) ;
212223 $return_value
213224 }
@@ -217,7 +228,7 @@ macro_rules! refund_builder_methods { (
217228 ///
218229 /// Successive calls to this method will add another blinded path. Caller is responsible for not
219230 /// adding duplicate paths.
220- pub fn path( mut $self: $self_type, path: BlindedPath ) -> $return_type {
231+ pub fn path( $ ( $self_mut ) * $self: $self_type, path: BlindedPath ) -> $return_type {
221232 $self. refund. paths. get_or_insert_with( Vec :: new) . push( path) ;
222233 $return_value
223234 }
@@ -234,7 +245,7 @@ macro_rules! refund_builder_methods { (
234245 /// [`Network::Bitcoin`] is assumed.
235246 ///
236247 /// Successive calls to this method will override the previous setting.
237- pub ( crate ) fn chain_hash( mut $self: $self_type, chain: ChainHash ) -> $return_type {
248+ pub ( crate ) fn chain_hash( $ ( $self_mut ) * $self: $self_type, chain: ChainHash ) -> $return_type {
238249 $self. refund. chain = Some ( chain) ;
239250 $return_value
240251 }
@@ -248,21 +259,21 @@ macro_rules! refund_builder_methods { (
248259 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
249260 /// [`InvoiceRequest::quantity`]: crate::offers::invoice_request::InvoiceRequest::quantity
250261 /// [`Offer`]: crate::offers::offer::Offer
251- pub fn quantity( mut $self: $self_type, quantity: u64 ) -> $return_type {
262+ pub fn quantity( $ ( $self_mut ) * $self: $self_type, quantity: u64 ) -> $return_type {
252263 $self. refund. quantity = Some ( quantity) ;
253264 $return_value
254265 }
255266
256267 /// Sets the [`Refund::payer_note`].
257268 ///
258269 /// Successive calls to this method will override the previous setting.
259- pub fn payer_note( mut $self: $self_type, payer_note: String ) -> $return_type {
270+ pub fn payer_note( $ ( $self_mut ) * $self: $self_type, payer_note: String ) -> $return_type {
260271 $self. refund. payer_note = Some ( payer_note) ;
261272 $return_value
262273 }
263274
264275 /// Builds a [`Refund`] after checking for valid semantics.
265- pub fn build( mut $self: $self_type) -> Result <Refund , Bolt12SemanticError > {
276+ pub fn build( $ ( $self_mut ) * $self: $self_type) -> Result <Refund , Bolt12SemanticError > {
266277 if $self. refund. chain( ) == $self. refund. implied_chain( ) {
267278 $self. refund. chain = None ;
268279 }
@@ -293,34 +304,65 @@ macro_rules! refund_builder_methods { (
293304 let mut bytes = Vec :: new( ) ;
294305 $self. refund. write( & mut bytes) . unwrap( ) ;
295306
296- Ok ( Refund { bytes, contents: $self. refund } )
307+ Ok ( Refund {
308+ bytes,
309+ #[ cfg( not( c_bindings) ) ]
310+ contents: $self. refund,
311+ #[ cfg( c_bindings) ]
312+ contents: $self. refund. clone( ) ,
313+ } )
297314 }
298315} }
299316
300317#[ cfg( test) ]
301318macro_rules! refund_builder_test_methods { (
302- $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
319+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr $ ( , $self_mut : tt ) ?
303320) => {
304- pub ( crate ) fn clear_paths( mut $self: $self_type) -> $return_type {
321+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
322+ pub ( crate ) fn clear_paths( $( $self_mut) * $self: $self_type) -> $return_type {
305323 $self. refund. paths = None ;
306324 $return_value
307325 }
308326
309- fn features_unchecked( mut $self: $self_type, features: InvoiceRequestFeatures ) -> $return_type {
327+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
328+ fn features_unchecked( $( $self_mut) * $self: $self_type, features: InvoiceRequestFeatures ) -> $return_type {
310329 $self. refund. features = features;
311330 $return_value
312331 }
313332} }
314333
315334impl < ' a > RefundBuilder < ' a , secp256k1:: SignOnly > {
316- refund_without_secp256k1_builder_methods ! ( ) ;
335+ refund_explicit_metadata_builder_methods ! ( ) ;
317336}
318337
319338impl < ' a , T : secp256k1:: Signing > RefundBuilder < ' a , T > {
320- refund_builder_methods ! ( self , Self , Self , self ) ;
339+ refund_builder_methods ! ( self , Self , Self , self , T , mut ) ;
321340
322341 #[ cfg( test) ]
323- refund_builder_test_methods ! ( self , Self , Self , self ) ;
342+ refund_builder_test_methods ! ( self , Self , Self , self , mut ) ;
343+ }
344+
345+ #[ cfg( all( c_bindings, not( test) ) ) ]
346+ impl < ' a > RefundMaybeWithDerivedMetadataBuilder < ' a > {
347+ refund_explicit_metadata_builder_methods ! ( ) ;
348+ refund_builder_methods ! ( self , & mut Self , ( ) , ( ) , secp256k1:: All ) ;
349+ }
350+
351+ #[ cfg( all( c_bindings, test) ) ]
352+ impl < ' a > RefundMaybeWithDerivedMetadataBuilder < ' a > {
353+ refund_explicit_metadata_builder_methods ! ( ) ;
354+ refund_builder_methods ! ( self , & mut Self , & mut Self , self , secp256k1:: All ) ;
355+ refund_builder_test_methods ! ( self , & mut Self , & mut Self , self ) ;
356+ }
357+
358+ #[ cfg( c_bindings) ]
359+ impl < ' a > From < RefundBuilder < ' a , secp256k1:: All > >
360+ for RefundMaybeWithDerivedMetadataBuilder < ' a > {
361+ fn from ( builder : RefundBuilder < ' a , secp256k1:: All > ) -> Self {
362+ let RefundBuilder { refund, secp_ctx } = builder;
363+
364+ Self { refund, secp_ctx }
365+ }
324366}
325367
326368/// A `Refund` is a request to send an [`Bolt12Invoice`] without a preceding [`Offer`].
@@ -798,7 +840,15 @@ impl core::fmt::Display for Refund {
798840
799841#[ cfg( test) ]
800842mod tests {
801- use super :: { Refund , RefundBuilder , RefundTlvStreamRef } ;
843+ use super :: { Refund , RefundTlvStreamRef } ;
844+ #[ cfg( not( c_bindings) ) ]
845+ use {
846+ super :: RefundBuilder ,
847+ } ;
848+ #[ cfg( c_bindings) ]
849+ use {
850+ super :: RefundMaybeWithDerivedMetadataBuilder as RefundBuilder ,
851+ } ;
802852
803853 use bitcoin:: blockdata:: constants:: ChainHash ;
804854 use bitcoin:: network:: constants:: Network ;
0 commit comments