36
36
//! let pubkey = PublicKey::from(keys);
37
37
//! let mut buffer = Vec::new();
38
38
//!
39
+ //! # use lightning::offers::invoice_request::{ExplicitPayerId, InvoiceRequestBuilder};
40
+ //! # <InvoiceRequestBuilder<ExplicitPayerId, _>>::from(
39
41
//! "lno1qcp4256ypq"
40
42
//! .parse::<Offer>()?
41
43
//! .request_invoice(vec![42; 64], pubkey)?
44
+ //! # )
42
45
//! .chain(Network::Testnet)?
43
46
//! .amount_msats(1000)?
44
47
//! .quantity(5)?
@@ -99,6 +102,34 @@ pub struct InvoiceRequestBuilder<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signi
99
102
secp_ctx : Option < & ' b Secp256k1 < T > > ,
100
103
}
101
104
105
+ /// Builds an [`InvoiceRequest`] from an [`Offer`] for the "offer to be paid" flow.
106
+ ///
107
+ /// See [module-level documentation] for usage.
108
+ ///
109
+ /// [module-level documentation]: self
110
+ #[ cfg( c_bindings) ]
111
+ pub struct InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > {
112
+ offer : & ' a Offer ,
113
+ invoice_request : InvoiceRequestContentsWithoutPayerId ,
114
+ payer_id : Option < PublicKey > ,
115
+ payer_id_strategy : core:: marker:: PhantomData < ExplicitPayerId > ,
116
+ secp_ctx : Option < & ' b Secp256k1 < secp256k1:: All > > ,
117
+ }
118
+
119
+ /// Builds an [`InvoiceRequest`] from an [`Offer`] for the "offer to be paid" flow.
120
+ ///
121
+ /// See [module-level documentation] for usage.
122
+ ///
123
+ /// [module-level documentation]: self
124
+ #[ cfg( c_bindings) ]
125
+ pub struct InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > {
126
+ offer : & ' a Offer ,
127
+ invoice_request : InvoiceRequestContentsWithoutPayerId ,
128
+ payer_id : Option < PublicKey > ,
129
+ payer_id_strategy : core:: marker:: PhantomData < DerivedPayerId > ,
130
+ secp_ctx : Option < & ' b Secp256k1 < secp256k1:: All > > ,
131
+ }
132
+
102
133
/// Indicates how [`InvoiceRequest::payer_id`] will be set.
103
134
///
104
135
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
@@ -118,6 +149,7 @@ impl PayerIdStrategy for ExplicitPayerId {}
118
149
impl PayerIdStrategy for DerivedPayerId { }
119
150
120
151
macro_rules! invoice_request_explicit_payer_id_builder_methods { ( $self: ident, $self_type: ty) => {
152
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
121
153
pub ( super ) fn new( offer: & ' a Offer , metadata: Vec <u8 >, payer_id: PublicKey ) -> Self {
122
154
Self {
123
155
offer,
@@ -128,6 +160,7 @@ macro_rules! invoice_request_explicit_payer_id_builder_methods { ($self: ident,
128
160
}
129
161
}
130
162
163
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
131
164
pub ( super ) fn deriving_metadata<ES : Deref >(
132
165
offer: & ' a Offer , payer_id: PublicKey , expanded_key: & ExpandedKey , entropy_source: ES ,
133
166
payment_id: PaymentId ,
@@ -154,10 +187,13 @@ macro_rules! invoice_request_explicit_payer_id_builder_methods { ($self: ident,
154
187
}
155
188
} }
156
189
157
- macro_rules! invoice_request_derived_payer_id_builder_methods { ( $self: ident, $self_type: ty) => {
190
+ macro_rules! invoice_request_derived_payer_id_builder_methods { (
191
+ $self: ident, $self_type: ty, $secp_context: ty
192
+ ) => {
193
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
158
194
pub ( super ) fn deriving_payer_id<ES : Deref >(
159
195
offer: & ' a Offer , expanded_key: & ExpandedKey , entropy_source: ES ,
160
- secp_ctx: & ' b Secp256k1 <T >, payment_id: PaymentId
196
+ secp_ctx: & ' b Secp256k1 <$secp_context >, payment_id: PaymentId
161
197
) -> Self where ES :: Target : EntropySource {
162
198
let nonce = Nonce :: from_entropy_source( entropy_source) ;
163
199
let payment_id = Some ( payment_id) ;
@@ -175,6 +211,8 @@ macro_rules! invoice_request_derived_payer_id_builder_methods { ($self: ident, $
175
211
/// Builds a signed [`InvoiceRequest`] after checking for valid semantics.
176
212
pub fn build_and_sign( $self: $self_type) -> Result <InvoiceRequest , Bolt12SemanticError > {
177
213
let ( unsigned_invoice_request, keys, secp_ctx) = $self. build_with_checks( ) ?;
214
+ #[ cfg( c_bindings) ]
215
+ let mut unsigned_invoice_request = unsigned_invoice_request;
178
216
debug_assert!( keys. is_some( ) ) ;
179
217
180
218
let secp_ctx = secp_ctx. unwrap( ) ;
@@ -189,8 +227,9 @@ macro_rules! invoice_request_derived_payer_id_builder_methods { ($self: ident, $
189
227
} }
190
228
191
229
macro_rules! invoice_request_builder_methods { (
192
- $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
230
+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr, $secp_context : ty $ ( , $self_mut : tt ) ?
193
231
) => {
232
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
194
233
fn create_contents( offer: & Offer , metadata: Metadata ) -> InvoiceRequestContentsWithoutPayerId {
195
234
let offer = offer. contents. clone( ) ;
196
235
InvoiceRequestContentsWithoutPayerId {
@@ -213,7 +252,7 @@ macro_rules! invoice_request_builder_methods { (
213
252
/// offer.
214
253
///
215
254
/// Successive calls to this method will override the previous setting.
216
- pub ( crate ) fn chain_hash( mut $self: $self_type, chain: ChainHash ) -> Result <$return_type, Bolt12SemanticError > {
255
+ pub ( crate ) fn chain_hash( $ ( $self_mut ) * $self: $self_type, chain: ChainHash ) -> Result <$return_type, Bolt12SemanticError > {
217
256
if !$self. offer. supports_chain( chain) {
218
257
return Err ( Bolt12SemanticError :: UnsupportedChain ) ;
219
258
}
@@ -228,7 +267,7 @@ macro_rules! invoice_request_builder_methods { (
228
267
/// Successive calls to this method will override the previous setting.
229
268
///
230
269
/// [`quantity`]: Self::quantity
231
- pub fn amount_msats( mut $self: $self_type, amount_msats: u64 ) -> Result <$return_type, Bolt12SemanticError > {
270
+ pub fn amount_msats( $ ( $self_mut ) * $self: $self_type, amount_msats: u64 ) -> Result <$return_type, Bolt12SemanticError > {
232
271
$self. invoice_request. offer. check_amount_msats_for_quantity(
233
272
Some ( amount_msats) , $self. invoice_request. quantity
234
273
) ?;
@@ -240,7 +279,7 @@ macro_rules! invoice_request_builder_methods { (
240
279
/// does not conform to [`Offer::is_valid_quantity`].
241
280
///
242
281
/// Successive calls to this method will override the previous setting.
243
- pub fn quantity( mut $self: $self_type, quantity: u64 ) -> Result <$return_type, Bolt12SemanticError > {
282
+ pub fn quantity( $ ( $self_mut ) * $self: $self_type, quantity: u64 ) -> Result <$return_type, Bolt12SemanticError > {
244
283
$self. invoice_request. offer. check_quantity( Some ( quantity) ) ?;
245
284
$self. invoice_request. quantity = Some ( quantity) ;
246
285
Ok ( $return_value)
@@ -249,13 +288,13 @@ macro_rules! invoice_request_builder_methods { (
249
288
/// Sets the [`InvoiceRequest::payer_note`].
250
289
///
251
290
/// Successive calls to this method will override the previous setting.
252
- pub fn payer_note( mut $self: $self_type, payer_note: String ) -> $return_type {
291
+ pub fn payer_note( $ ( $self_mut ) * $self: $self_type, payer_note: String ) -> $return_type {
253
292
$self. invoice_request. payer_note = Some ( payer_note) ;
254
293
$return_value
255
294
}
256
295
257
- fn build_with_checks( mut $self: $self_type) -> Result <
258
- ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <T >>) ,
296
+ fn build_with_checks( $ ( $self_mut ) * $self: $self_type) -> Result <
297
+ ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <$secp_context >>) ,
259
298
Bolt12SemanticError
260
299
> {
261
300
#[ cfg( feature = "std" ) ] {
@@ -285,8 +324,8 @@ macro_rules! invoice_request_builder_methods { (
285
324
Ok ( $self. build_without_checks( ) )
286
325
}
287
326
288
- fn build_without_checks( mut $self: $self_type) ->
289
- ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <T >>)
327
+ fn build_without_checks( $ ( $self_mut ) * $self: $self_type) ->
328
+ ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <$secp_context >>)
290
329
{
291
330
// Create the metadata for stateless verification of a Bolt12Invoice.
292
331
let mut keys = None ;
@@ -317,7 +356,10 @@ macro_rules! invoice_request_builder_methods { (
317
356
let payer_id = $self. payer_id. unwrap( ) ;
318
357
319
358
let invoice_request = InvoiceRequestContents {
359
+ #[ cfg( not( c_bindings) ) ]
320
360
inner: $self. invoice_request,
361
+ #[ cfg( c_bindings) ]
362
+ inner: $self. invoice_request. clone( ) ,
321
363
payer_id,
322
364
} ;
323
365
let unsigned_invoice_request = UnsignedInvoiceRequest :: new( $self. offer, invoice_request) ;
@@ -328,29 +370,34 @@ macro_rules! invoice_request_builder_methods { (
328
370
329
371
#[ cfg( test) ]
330
372
macro_rules! invoice_request_builder_test_methods { (
331
- $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
373
+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr $ ( , $self_mut : tt ) ?
332
374
) => {
333
- fn chain_unchecked( mut $self: $self_type, network: Network ) -> $return_type {
375
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
376
+ fn chain_unchecked( $( $self_mut) * $self: $self_type, network: Network ) -> $return_type {
334
377
let chain = ChainHash :: using_genesis_block( network) ;
335
378
$self. invoice_request. chain = Some ( chain) ;
336
379
$return_value
337
380
}
338
381
339
- fn amount_msats_unchecked( mut $self: $self_type, amount_msats: u64 ) -> $return_type {
382
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
383
+ fn amount_msats_unchecked( $( $self_mut) * $self: $self_type, amount_msats: u64 ) -> $return_type {
340
384
$self. invoice_request. amount_msats = Some ( amount_msats) ;
341
385
$return_value
342
386
}
343
387
344
- fn features_unchecked( mut $self: $self_type, features: InvoiceRequestFeatures ) -> $return_type {
388
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
389
+ fn features_unchecked( $( $self_mut) * $self: $self_type, features: InvoiceRequestFeatures ) -> $return_type {
345
390
$self. invoice_request. features = features;
346
391
$return_value
347
392
}
348
393
349
- fn quantity_unchecked( mut $self: $self_type, quantity: u64 ) -> $return_type {
394
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
395
+ fn quantity_unchecked( $( $self_mut) * $self: $self_type, quantity: u64 ) -> $return_type {
350
396
$self. invoice_request. quantity = Some ( quantity) ;
351
397
$return_value
352
398
}
353
399
400
+ #[ cfg_attr( c_bindings, allow( dead_code) ) ]
354
401
pub ( super ) fn build_unchecked( $self: $self_type) -> UnsignedInvoiceRequest {
355
402
$self. build_without_checks( ) . 0
356
403
}
@@ -361,14 +408,68 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
361
408
}
362
409
363
410
impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
364
- invoice_request_derived_payer_id_builder_methods ! ( self , Self ) ;
411
+ invoice_request_derived_payer_id_builder_methods ! ( self , Self , T ) ;
365
412
}
366
413
367
414
impl < ' a , ' b , P : PayerIdStrategy , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , P , T > {
368
- invoice_request_builder_methods ! ( self , Self , Self , self ) ;
415
+ invoice_request_builder_methods ! ( self , Self , Self , self , T , mut ) ;
369
416
370
417
#[ cfg( test) ]
371
- invoice_request_builder_test_methods ! ( self , Self , Self , self ) ;
418
+ invoice_request_builder_test_methods ! ( self , Self , Self , self , mut ) ;
419
+ }
420
+
421
+ #[ cfg( all( c_bindings, not( test) ) ) ]
422
+ impl < ' a , ' b > InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > {
423
+ invoice_request_explicit_payer_id_builder_methods ! ( self , & mut Self ) ;
424
+ invoice_request_builder_methods ! ( self , & mut Self , ( ) , ( ) , secp256k1:: All ) ;
425
+ }
426
+
427
+ #[ cfg( all( c_bindings, test) ) ]
428
+ impl < ' a , ' b > InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > {
429
+ invoice_request_explicit_payer_id_builder_methods ! ( self , & mut Self ) ;
430
+ invoice_request_builder_methods ! ( self , & mut Self , & mut Self , self , secp256k1:: All ) ;
431
+ invoice_request_builder_test_methods ! ( self , & mut Self , & mut Self , self ) ;
432
+ }
433
+
434
+ #[ cfg( all( c_bindings, not( test) ) ) ]
435
+ impl < ' a , ' b > InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > {
436
+ invoice_request_derived_payer_id_builder_methods ! ( self , & mut Self , secp256k1:: All ) ;
437
+ invoice_request_builder_methods ! ( self , & mut Self , ( ) , ( ) , secp256k1:: All ) ;
438
+ }
439
+
440
+ #[ cfg( all( c_bindings, test) ) ]
441
+ impl < ' a , ' b > InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > {
442
+ invoice_request_derived_payer_id_builder_methods ! ( self , & mut Self , secp256k1:: All ) ;
443
+ invoice_request_builder_methods ! ( self , & mut Self , & mut Self , self , secp256k1:: All ) ;
444
+ invoice_request_builder_test_methods ! ( self , & mut Self , & mut Self , self ) ;
445
+ }
446
+
447
+ #[ cfg( c_bindings) ]
448
+ impl < ' a , ' b > From < InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > >
449
+ for InvoiceRequestBuilder < ' a , ' b , ExplicitPayerId , secp256k1:: All > {
450
+ fn from ( builder : InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > ) -> Self {
451
+ let InvoiceRequestWithExplicitPayerIdBuilder {
452
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
453
+ } = builder;
454
+
455
+ Self {
456
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
457
+ }
458
+ }
459
+ }
460
+
461
+ #[ cfg( c_bindings) ]
462
+ impl < ' a , ' b > From < InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > >
463
+ for InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , secp256k1:: All > {
464
+ fn from ( builder : InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > ) -> Self {
465
+ let InvoiceRequestWithDerivedPayerIdBuilder {
466
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
467
+ } = builder;
468
+
469
+ Self {
470
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
471
+ }
472
+ }
372
473
}
373
474
374
475
/// A semantically valid [`InvoiceRequest`] that hasn't been signed.
@@ -406,13 +507,15 @@ impl UnsignedInvoiceRequest {
406
507
}
407
508
}
408
509
409
- macro_rules! unsigned_invoice_request_sign_method { ( $self: ident, $self_type: ty) => {
510
+ macro_rules! unsigned_invoice_request_sign_method { (
511
+ $self: ident, $self_type: ty $( , $self_mut: tt) ?
512
+ ) => {
410
513
/// Signs the [`TaggedHash`] of the invoice request using the given function.
411
514
///
412
515
/// Note: The hash computation may have included unknown, odd TLV records.
413
516
///
414
517
/// This is not exported to bindings users as functions are not yet mapped.
415
- pub fn sign<F , E >( mut $self: $self_type, sign: F ) -> Result <InvoiceRequest , SignError <E >>
518
+ pub fn sign<F , E >( $ ( $self_mut ) * $self: $self_type, sign: F ) -> Result <InvoiceRequest , SignError <E >>
416
519
where
417
520
F : FnOnce ( & Self ) -> Result <Signature , E >
418
521
{
@@ -426,15 +529,27 @@ macro_rules! unsigned_invoice_request_sign_method { ($self: ident, $self_type: t
426
529
signature_tlv_stream. write( & mut $self. bytes) . unwrap( ) ;
427
530
428
531
Ok ( InvoiceRequest {
532
+ #[ cfg( not( c_bindings) ) ]
429
533
bytes: $self. bytes,
534
+ #[ cfg( c_bindings) ]
535
+ bytes: $self. bytes. clone( ) ,
536
+ #[ cfg( not( c_bindings) ) ]
430
537
contents: $self. contents,
538
+ #[ cfg( c_bindings) ]
539
+ contents: $self. contents. clone( ) ,
431
540
signature,
432
541
} )
433
542
}
434
543
} }
435
544
545
+ #[ cfg( not( c_bindings) ) ]
546
+ impl UnsignedInvoiceRequest {
547
+ unsigned_invoice_request_sign_method ! ( self , Self , mut ) ;
548
+ }
549
+
550
+ #[ cfg( c_bindings) ]
436
551
impl UnsignedInvoiceRequest {
437
- unsigned_invoice_request_sign_method ! ( self , Self ) ;
552
+ unsigned_invoice_request_sign_method ! ( self , & mut Self ) ;
438
553
}
439
554
440
555
impl AsRef < TaggedHash > for UnsignedInvoiceRequest {
@@ -984,6 +1099,8 @@ mod tests {
984
1099
. build ( ) . unwrap ( )
985
1100
. request_invoice ( vec ! [ 1 ; 32 ] , payer_pubkey ( ) ) . unwrap ( )
986
1101
. build ( ) . unwrap ( ) ;
1102
+ #[ cfg( c_bindings) ]
1103
+ let mut unsigned_invoice_request = unsigned_invoice_request;
987
1104
988
1105
let mut buffer = Vec :: new ( ) ;
989
1106
unsigned_invoice_request. write ( & mut buffer) . unwrap ( ) ;
0 commit comments