@@ -124,7 +124,18 @@ pub struct RefundBuilder<'a, T: secp256k1::Signing> {
124
124
secp_ctx : Option < & ' a Secp256k1 < T > > ,
125
125
}
126
126
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 { ( ) => {
128
139
/// Creates a new builder for a refund using the [`Refund::payer_id`] for the public node id to
129
140
/// send to if no [`Refund::paths`] are set. Otherwise, it may be a transient pubkey.
130
141
///
@@ -158,7 +169,7 @@ macro_rules! refund_without_secp256k1_builder_methods { () => {
158
169
} }
159
170
160
171
macro_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 ) ?
162
173
) => {
163
174
/// Similar to [`RefundBuilder::new`] except, if [`RefundBuilder::path`] is called, the payer id
164
175
/// is derived from the given [`ExpandedKey`] and nonce. This provides sender privacy by using a
@@ -175,7 +186,7 @@ macro_rules! refund_builder_methods { (
175
186
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
176
187
pub fn deriving_payer_id<ES : Deref >(
177
188
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
179
190
) -> Result <Self , Bolt12SemanticError > where ES :: Target : EntropySource {
180
191
if amount_msats > MAX_VALUE_MSAT {
181
192
return Err ( Bolt12SemanticError :: InvalidAmount ) ;
@@ -199,15 +210,15 @@ macro_rules! refund_builder_methods { (
199
210
/// already passed is valid and can be checked for using [`Refund::is_expired`].
200
211
///
201
212
/// 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 {
203
214
$self. refund. absolute_expiry = Some ( absolute_expiry) ;
204
215
$return_value
205
216
}
206
217
207
218
/// Sets the [`Refund::issuer`].
208
219
///
209
220
/// 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 {
211
222
$self. refund. issuer = Some ( issuer) ;
212
223
$return_value
213
224
}
@@ -217,7 +228,7 @@ macro_rules! refund_builder_methods { (
217
228
///
218
229
/// Successive calls to this method will add another blinded path. Caller is responsible for not
219
230
/// 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 {
221
232
$self. refund. paths. get_or_insert_with( Vec :: new) . push( path) ;
222
233
$return_value
223
234
}
@@ -234,7 +245,7 @@ macro_rules! refund_builder_methods { (
234
245
/// [`Network::Bitcoin`] is assumed.
235
246
///
236
247
/// 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 {
238
249
$self. refund. chain = Some ( chain) ;
239
250
$return_value
240
251
}
@@ -248,21 +259,21 @@ macro_rules! refund_builder_methods { (
248
259
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
249
260
/// [`InvoiceRequest::quantity`]: crate::offers::invoice_request::InvoiceRequest::quantity
250
261
/// [`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 {
252
263
$self. refund. quantity = Some ( quantity) ;
253
264
$return_value
254
265
}
255
266
256
267
/// Sets the [`Refund::payer_note`].
257
268
///
258
269
/// 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 {
260
271
$self. refund. payer_note = Some ( payer_note) ;
261
272
$return_value
262
273
}
263
274
264
275
/// 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 > {
266
277
if $self. refund. chain( ) == $self. refund. implied_chain( ) {
267
278
$self. refund. chain = None ;
268
279
}
@@ -293,34 +304,65 @@ macro_rules! refund_builder_methods { (
293
304
let mut bytes = Vec :: new( ) ;
294
305
$self. refund. write( & mut bytes) . unwrap( ) ;
295
306
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
+ } )
297
314
}
298
315
} }
299
316
300
317
#[ cfg( test) ]
301
318
macro_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 ) ?
303
320
) => {
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 {
305
323
$self. refund. paths = None ;
306
324
$return_value
307
325
}
308
326
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 {
310
329
$self. refund. features = features;
311
330
$return_value
312
331
}
313
332
} }
314
333
315
334
impl < ' a > RefundBuilder < ' a , secp256k1:: SignOnly > {
316
- refund_without_secp256k1_builder_methods ! ( ) ;
335
+ refund_explicit_metadata_builder_methods ! ( ) ;
317
336
}
318
337
319
338
impl < ' 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 ) ;
321
340
322
341
#[ 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
+ }
324
366
}
325
367
326
368
/// A `Refund` is a request to send an [`Bolt12Invoice`] without a preceding [`Offer`].
@@ -798,7 +840,15 @@ impl core::fmt::Display for Refund {
798
840
799
841
#[ cfg( test) ]
800
842
mod 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
+ } ;
802
852
803
853
use bitcoin:: blockdata:: constants:: ChainHash ;
804
854
use bitcoin:: network:: constants:: Network ;
0 commit comments