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.
@@ -154,10 +185,12 @@ macro_rules! invoice_request_explicit_payer_id_builder_methods { ($self: ident,
154
185
}
155
186
} }
156
187
157
- macro_rules! invoice_request_derived_payer_id_builder_methods { ( $self: ident, $self_type: ty) => {
188
+ macro_rules! invoice_request_derived_payer_id_builder_methods { (
189
+ $self: ident, $self_type: ty, $secp_context: ty
190
+ ) => {
158
191
pub ( super ) fn deriving_payer_id<ES : Deref >(
159
192
offer: & ' a Offer , expanded_key: & ExpandedKey , entropy_source: ES ,
160
- secp_ctx: & ' b Secp256k1 <T >, payment_id: PaymentId
193
+ secp_ctx: & ' b Secp256k1 <$secp_context >, payment_id: PaymentId
161
194
) -> Self where ES :: Target : EntropySource {
162
195
let nonce = Nonce :: from_entropy_source( entropy_source) ;
163
196
let payment_id = Some ( payment_id) ;
@@ -175,6 +208,8 @@ macro_rules! invoice_request_derived_payer_id_builder_methods { ($self: ident, $
175
208
/// Builds a signed [`InvoiceRequest`] after checking for valid semantics.
176
209
pub fn build_and_sign( $self: $self_type) -> Result <InvoiceRequest , Bolt12SemanticError > {
177
210
let ( unsigned_invoice_request, keys, secp_ctx) = $self. build_with_checks( ) ?;
211
+ #[ cfg( c_bindings) ]
212
+ let mut unsigned_invoice_request = unsigned_invoice_request;
178
213
debug_assert!( keys. is_some( ) ) ;
179
214
180
215
let secp_ctx = secp_ctx. unwrap( ) ;
@@ -189,7 +224,7 @@ macro_rules! invoice_request_derived_payer_id_builder_methods { ($self: ident, $
189
224
} }
190
225
191
226
macro_rules! invoice_request_builder_methods { (
192
- $self: ident, $self_type: ty, $return_type: ty, $return_value: expr
227
+ $self: ident, $self_type: ty, $return_type: ty, $return_value: expr, $secp_context : ty
193
228
) => {
194
229
fn create_contents( offer: & Offer , metadata: Metadata ) -> InvoiceRequestContentsWithoutPayerId {
195
230
let offer = offer. contents. clone( ) ;
@@ -255,7 +290,7 @@ macro_rules! invoice_request_builder_methods { (
255
290
}
256
291
257
292
fn build_with_checks( mut $self: $self_type) -> Result <
258
- ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <T >>) ,
293
+ ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <$secp_context >>) ,
259
294
Bolt12SemanticError
260
295
> {
261
296
#[ cfg( feature = "std" ) ] {
@@ -286,7 +321,7 @@ macro_rules! invoice_request_builder_methods { (
286
321
}
287
322
288
323
fn build_without_checks( mut $self: $self_type) ->
289
- ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <T >>)
324
+ ( UnsignedInvoiceRequest , Option <KeyPair >, Option <& ' b Secp256k1 <$secp_context >>)
290
325
{
291
326
// Create the metadata for stateless verification of a Bolt12Invoice.
292
327
let mut keys = None ;
@@ -317,7 +352,10 @@ macro_rules! invoice_request_builder_methods { (
317
352
let payer_id = $self. payer_id. unwrap( ) ;
318
353
319
354
let invoice_request = InvoiceRequestContents {
355
+ #[ cfg( not( c_bindings) ) ]
320
356
inner: $self. invoice_request,
357
+ #[ cfg( c_bindings) ]
358
+ inner: $self. invoice_request. clone( ) ,
321
359
payer_id,
322
360
} ;
323
361
let unsigned_invoice_request = UnsignedInvoiceRequest :: new( $self. offer, invoice_request) ;
@@ -361,16 +399,70 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
361
399
}
362
400
363
401
impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
364
- invoice_request_derived_payer_id_builder_methods ! ( self , Self ) ;
402
+ invoice_request_derived_payer_id_builder_methods ! ( self , Self , T ) ;
365
403
}
366
404
367
405
impl < ' a , ' b , P : PayerIdStrategy , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , P , T > {
368
- invoice_request_builder_methods ! ( self , Self , Self , self ) ;
406
+ invoice_request_builder_methods ! ( self , Self , Self , self , T ) ;
369
407
370
408
#[ cfg( test) ]
371
409
invoice_request_builder_test_methods ! ( self , Self , Self , self ) ;
372
410
}
373
411
412
+ #[ cfg( all( c_bindings, not( test) ) ) ]
413
+ impl < ' a , ' b > InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > {
414
+ invoice_request_explicit_payer_id_builder_methods ! ( self , & mut Self ) ;
415
+ invoice_request_builder_methods ! ( self , & mut Self , ( ) , ( ) , secp256k1:: All ) ;
416
+ }
417
+
418
+ #[ cfg( all( c_bindings, test) ) ]
419
+ impl < ' a , ' b > InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > {
420
+ invoice_request_explicit_payer_id_builder_methods ! ( self , & mut Self ) ;
421
+ invoice_request_builder_methods ! ( self , & mut Self , & mut Self , self , secp256k1:: All ) ;
422
+ invoice_request_builder_test_methods ! ( self , & mut Self , & mut Self , self ) ;
423
+ }
424
+
425
+ #[ cfg( all( c_bindings, not( test) ) ) ]
426
+ impl < ' a , ' b > InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > {
427
+ invoice_request_derived_payer_id_builder_methods ! ( self , & mut Self , secp256k1:: All ) ;
428
+ invoice_request_builder_methods ! ( self , & mut Self , ( ) , ( ) , secp256k1:: All ) ;
429
+ }
430
+
431
+ #[ cfg( all( c_bindings, test) ) ]
432
+ impl < ' a , ' b > InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > {
433
+ invoice_request_derived_payer_id_builder_methods ! ( self , & mut Self , secp256k1:: All ) ;
434
+ invoice_request_builder_methods ! ( self , & mut Self , & mut Self , self , secp256k1:: All ) ;
435
+ invoice_request_builder_test_methods ! ( self , & mut Self , & mut Self , self ) ;
436
+ }
437
+
438
+ #[ cfg( c_bindings) ]
439
+ impl < ' a , ' b > From < InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > >
440
+ for InvoiceRequestBuilder < ' a , ' b , ExplicitPayerId , secp256k1:: All > {
441
+ fn from ( builder : InvoiceRequestWithExplicitPayerIdBuilder < ' a , ' b > ) -> Self {
442
+ let InvoiceRequestWithExplicitPayerIdBuilder {
443
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
444
+ } = builder;
445
+
446
+ Self {
447
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
448
+ }
449
+ }
450
+ }
451
+
452
+ #[ cfg( c_bindings) ]
453
+ impl < ' a , ' b > From < InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > >
454
+ for InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , secp256k1:: All > {
455
+ fn from ( builder : InvoiceRequestWithDerivedPayerIdBuilder < ' a , ' b > ) -> Self {
456
+ let InvoiceRequestWithDerivedPayerIdBuilder {
457
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
458
+ } = builder;
459
+
460
+ Self {
461
+ offer, invoice_request, payer_id, payer_id_strategy, secp_ctx,
462
+ }
463
+ }
464
+ }
465
+
374
466
/// A semantically valid [`InvoiceRequest`] that hasn't been signed.
375
467
///
376
468
/// # Serialization
@@ -426,17 +518,29 @@ macro_rules! unsigned_invoice_request_sign_method { ($self: ident, $self_type: t
426
518
signature_tlv_stream. write( & mut $self. bytes) . unwrap( ) ;
427
519
428
520
Ok ( InvoiceRequest {
521
+ #[ cfg( not( c_bindings) ) ]
429
522
bytes: $self. bytes,
523
+ #[ cfg( c_bindings) ]
524
+ bytes: $self. bytes. clone( ) ,
525
+ #[ cfg( not( c_bindings) ) ]
430
526
contents: $self. contents,
527
+ #[ cfg( c_bindings) ]
528
+ contents: $self. contents. clone( ) ,
431
529
signature,
432
530
} )
433
531
}
434
532
} }
435
533
534
+ #[ cfg( not( c_bindings) ) ]
436
535
impl UnsignedInvoiceRequest {
437
536
unsigned_invoice_request_sign_method ! ( self , Self ) ;
438
537
}
439
538
539
+ #[ cfg( c_bindings) ]
540
+ impl UnsignedInvoiceRequest {
541
+ unsigned_invoice_request_sign_method ! ( self , & mut Self ) ;
542
+ }
543
+
440
544
impl AsRef < TaggedHash > for UnsignedInvoiceRequest {
441
545
fn as_ref ( & self ) -> & TaggedHash {
442
546
& self . tagged_hash
@@ -984,6 +1088,8 @@ mod tests {
984
1088
. build ( ) . unwrap ( )
985
1089
. request_invoice ( vec ! [ 1 ; 32 ] , payer_pubkey ( ) ) . unwrap ( )
986
1090
. build ( ) . unwrap ( ) ;
1091
+ #[ cfg( c_bindings) ]
1092
+ let mut unsigned_invoice_request = unsigned_invoice_request;
987
1093
988
1094
let mut buffer = Vec :: new ( ) ;
989
1095
unsigned_invoice_request. write ( & mut buffer) . unwrap ( ) ;
0 commit comments