@@ -116,15 +116,25 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Refund ~~~~~";
116
116
///
117
117
/// See [module-level documentation] for usage.
118
118
///
119
- /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
120
- ///
121
119
/// [module-level documentation]: self
120
+ #[ cfg( not( c_bindings) ) ]
122
121
pub struct RefundBuilder < ' a , T : secp256k1:: Signing > {
123
122
refund : RefundContents ,
124
123
secp_ctx : Option < & ' a Secp256k1 < T > > ,
125
124
}
126
125
127
- impl < ' a > RefundBuilder < ' a , secp256k1:: SignOnly > {
126
+ /// Builds a [`Refund`] for the "offer for money" flow.
127
+ ///
128
+ /// See [module-level documentation] for usage.
129
+ ///
130
+ /// [module-level documentation]: self
131
+ #[ cfg( c_bindings) ]
132
+ pub struct RefundBuilder < ' a > {
133
+ refund : RefundContents ,
134
+ secp_ctx : Option < & ' a Secp256k1 < secp256k1:: All > > ,
135
+ }
136
+
137
+ macro_rules! refund_without_secp256k1_builder_methods { ( ) => {
128
138
/// Creates a new builder for a refund using the [`Refund::payer_id`] for the public node id to
129
139
/// send to if no [`Refund::paths`] are set. Otherwise, it may be a transient pubkey.
130
140
///
@@ -155,9 +165,11 @@ impl<'a> RefundBuilder<'a, secp256k1::SignOnly> {
155
165
secp_ctx: None ,
156
166
} )
157
167
}
158
- }
168
+ } }
159
169
160
- impl < ' a , T : secp256k1:: Signing > RefundBuilder < ' a , T > {
170
+ macro_rules! refund_builder_methods { (
171
+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
172
+ ) => {
161
173
/// Similar to [`RefundBuilder::new`] except, if [`RefundBuilder::path`] is called, the payer id
162
174
/// is derived from the given [`ExpandedKey`] and nonce. This provides sender privacy by using a
163
175
/// different payer id for each refund, assuming a different nonce is used. Otherwise, the
@@ -173,7 +185,11 @@ impl<'a, T: secp256k1::Signing> RefundBuilder<'a, T> {
173
185
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
174
186
pub fn deriving_payer_id<ES : Deref >(
175
187
description: String , node_id: PublicKey , expanded_key: & ExpandedKey , entropy_source: ES ,
176
- secp_ctx : & ' a Secp256k1 < T > , amount_msats : u64 , payment_id : PaymentId
188
+ #[ cfg( not( c_bindings) ) ]
189
+ secp_ctx: & ' a Secp256k1 <T >,
190
+ #[ cfg( c_bindings) ]
191
+ secp_ctx: & ' a Secp256k1 <secp256k1:: All >,
192
+ amount_msats: u64 , payment_id: PaymentId
177
193
) -> Result <Self , Bolt12SemanticError > where ES :: Target : EntropySource {
178
194
if amount_msats > MAX_VALUE_MSAT {
179
195
return Err ( Bolt12SemanticError :: InvalidAmount ) ;
@@ -197,44 +213,44 @@ impl<'a, T: secp256k1::Signing> RefundBuilder<'a, T> {
197
213
/// already passed is valid and can be checked for using [`Refund::is_expired`].
198
214
///
199
215
/// Successive calls to this method will override the previous setting.
200
- pub fn absolute_expiry ( mut self , absolute_expiry : Duration ) -> Self {
201
- self . refund . absolute_expiry = Some ( absolute_expiry) ;
202
- self
216
+ pub fn absolute_expiry( mut $ self: $self_type , absolute_expiry: Duration ) -> $return_type {
217
+ $ self. refund. absolute_expiry = Some ( absolute_expiry) ;
218
+ $return_value
203
219
}
204
220
205
221
/// Sets the [`Refund::issuer`].
206
222
///
207
223
/// Successive calls to this method will override the previous setting.
208
- pub fn issuer ( mut self , issuer : String ) -> Self {
209
- self . refund . issuer = Some ( issuer) ;
210
- self
224
+ pub fn issuer( mut $ self: $self_type , issuer: String ) -> $return_type {
225
+ $ self. refund. issuer = Some ( issuer) ;
226
+ $return_value
211
227
}
212
228
213
229
/// Adds a blinded path to [`Refund::paths`]. Must include at least one path if only connected
214
230
/// by private channels or if [`Refund::payer_id`] is not a public node id.
215
231
///
216
232
/// Successive calls to this method will add another blinded path. Caller is responsible for not
217
233
/// adding duplicate paths.
218
- pub fn path ( mut self , path : BlindedPath ) -> Self {
219
- self . refund . paths . get_or_insert_with ( Vec :: new) . push ( path) ;
220
- self
234
+ pub fn path( mut $ self: $self_type , path: BlindedPath ) -> $return_type {
235
+ $ self. refund. paths. get_or_insert_with( Vec :: new) . push( path) ;
236
+ $return_value
221
237
}
222
238
223
239
/// Sets the [`Refund::chain`] of the given [`Network`] for paying an invoice. If not
224
240
/// called, [`Network::Bitcoin`] is assumed.
225
241
///
226
242
/// Successive calls to this method will override the previous setting.
227
- pub fn chain ( self , network : Network ) -> Self {
228
- self . chain_hash ( ChainHash :: using_genesis_block ( network) )
243
+ pub fn chain( $ self: $self_type , network: Network ) -> $return_type {
244
+ $ self. chain_hash( ChainHash :: using_genesis_block( network) )
229
245
}
230
246
231
247
/// Sets the [`Refund::chain`] of the given [`ChainHash`] for paying an invoice. If not called,
232
248
/// [`Network::Bitcoin`] is assumed.
233
249
///
234
250
/// Successive calls to this method will override the previous setting.
235
- pub ( crate ) fn chain_hash ( mut self , chain : ChainHash ) -> Self {
236
- self . refund . chain = Some ( chain) ;
237
- self
251
+ pub ( crate ) fn chain_hash( mut $ self: $self_type , chain: ChainHash ) -> $return_type {
252
+ $ self. refund. chain = Some ( chain) ;
253
+ $return_value
238
254
}
239
255
240
256
/// Sets [`Refund::quantity`] of items. This is purely for informational purposes. It is useful
@@ -246,66 +262,98 @@ impl<'a, T: secp256k1::Signing> RefundBuilder<'a, T> {
246
262
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
247
263
/// [`InvoiceRequest::quantity`]: crate::offers::invoice_request::InvoiceRequest::quantity
248
264
/// [`Offer`]: crate::offers::offer::Offer
249
- pub fn quantity ( mut self , quantity : u64 ) -> Self {
250
- self . refund . quantity = Some ( quantity) ;
251
- self
265
+ pub fn quantity( mut $ self: $self_type , quantity: u64 ) -> $return_type {
266
+ $ self. refund. quantity = Some ( quantity) ;
267
+ $return_value
252
268
}
253
269
254
270
/// Sets the [`Refund::payer_note`].
255
271
///
256
272
/// Successive calls to this method will override the previous setting.
257
- pub fn payer_note ( mut self , payer_note : String ) -> Self {
258
- self . refund . payer_note = Some ( payer_note) ;
259
- self
273
+ pub fn payer_note( mut $ self: $self_type , payer_note: String ) -> $return_type {
274
+ $ self. refund. payer_note = Some ( payer_note) ;
275
+ $return_value
260
276
}
261
277
262
278
/// Builds a [`Refund`] after checking for valid semantics.
263
- pub fn build ( mut self ) -> Result < Refund , Bolt12SemanticError > {
264
- if self . refund . chain ( ) == self . refund . implied_chain ( ) {
265
- self . refund . chain = None ;
279
+ pub fn build( mut $ self: $self_type ) -> Result <Refund , Bolt12SemanticError > {
280
+ if $ self. refund. chain( ) == $ self. refund. implied_chain( ) {
281
+ $ self. refund. chain = None ;
266
282
}
267
283
268
284
// Create the metadata for stateless verification of a Bolt12Invoice.
269
- if self . refund . payer . 0 . has_derivation_material ( ) {
270
- let mut metadata = core:: mem:: take ( & mut self . refund . payer . 0 ) ;
285
+ if $ self. refund. payer. 0 . has_derivation_material( ) {
286
+ let mut metadata = core:: mem:: take( & mut $ self. refund. payer. 0 ) ;
271
287
272
- if self . refund . paths . is_none ( ) {
288
+ if $ self. refund. paths. is_none( ) {
273
289
metadata = metadata. without_keys( ) ;
274
290
}
275
291
276
- let mut tlv_stream = self . refund . as_tlv_stream ( ) ;
292
+ let mut tlv_stream = $ self. refund. as_tlv_stream( ) ;
277
293
tlv_stream. 0 . metadata = None ;
278
294
if metadata. derives_payer_keys( ) {
279
295
tlv_stream. 2 . payer_id = None ;
280
296
}
281
297
282
- let ( derived_metadata, keys) = metadata. derive_from ( tlv_stream, self . secp_ctx ) ;
298
+ let ( derived_metadata, keys) = metadata. derive_from( tlv_stream, $ self. secp_ctx) ;
283
299
metadata = derived_metadata;
284
300
if let Some ( keys) = keys {
285
- self . refund . payer_id = keys. public_key ( ) ;
301
+ $ self. refund. payer_id = keys. public_key( ) ;
286
302
}
287
303
288
- self . refund . payer . 0 = metadata;
304
+ $ self. refund. payer. 0 = metadata;
289
305
}
290
306
291
307
let mut bytes = Vec :: new( ) ;
292
- self . refund . write ( & mut bytes) . unwrap ( ) ;
308
+ $ self. refund. write( & mut bytes) . unwrap( ) ;
293
309
294
- Ok ( Refund { bytes, contents : self . refund } )
310
+ #[ cfg( not( c_bindings) ) ] {
311
+ Ok ( Refund { bytes, contents: $self. refund } )
312
+ }
313
+ #[ cfg( c_bindings) ] {
314
+ Ok ( Refund { bytes, contents: $self. refund. clone( ) } )
315
+ }
295
316
}
296
- }
317
+ } }
297
318
298
319
#[ cfg( test) ]
299
- impl < ' a , T : secp256k1:: Signing > RefundBuilder < ' a , T > {
300
- pub ( crate ) fn clear_paths ( mut self ) -> Self {
301
- self . refund . paths = None ;
302
- self
320
+ macro_rules! refund_builder_test_methods { (
321
+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
322
+ ) => {
323
+ pub ( crate ) fn clear_paths( mut $self: $self_type) -> $return_type {
324
+ $self. refund. paths = None ;
325
+ $return_value
303
326
}
304
327
305
- fn features_unchecked ( mut self , features : InvoiceRequestFeatures ) -> Self {
306
- self . refund . features = features;
307
- self
328
+ fn features_unchecked( mut $ self: $self_type , features: InvoiceRequestFeatures ) -> $return_type {
329
+ $ self. refund. features = features;
330
+ $return_value
308
331
}
332
+ } }
333
+
334
+ #[ cfg( not( c_bindings) ) ]
335
+ impl < ' a > RefundBuilder < ' a , secp256k1:: SignOnly > {
336
+ refund_without_secp256k1_builder_methods ! ( ) ;
337
+ }
338
+
339
+ #[ cfg( not( c_bindings) ) ]
340
+ impl < ' a , T : secp256k1:: Signing > RefundBuilder < ' a , T > {
341
+ refund_builder_methods ! ( self , Self , Self , self ) ;
342
+
343
+ #[ cfg( test) ]
344
+ refund_builder_test_methods ! ( self , Self , Self , self ) ;
345
+ }
346
+
347
+ #[ cfg( c_bindings) ]
348
+ impl < ' a > RefundBuilder < ' a > {
349
+ refund_without_secp256k1_builder_methods ! ( ) ;
350
+ #[ cfg( not( test) ) ]
351
+ refund_builder_methods ! ( self , & mut Self , ( ) , ( ) ) ;
352
+
353
+ #[ cfg( test) ]
354
+ refund_builder_methods ! ( self , & mut Self , & mut Self , self ) ;
355
+ #[ cfg( test) ]
356
+ refund_builder_test_methods ! ( self , & mut Self , & mut Self , self ) ;
309
357
}
310
358
311
359
/// A `Refund` is a request to send an [`Bolt12Invoice`] without a preceding [`Offer`].
0 commit comments