@@ -44,8 +44,7 @@ pub enum Destination {
44
44
impl OfferBuilder {
45
45
///
46
46
pub fn new ( description : String , destination : Destination ) -> Self {
47
- // TODO: Replace with sha256::Hash::all_zeros()
48
- let id = sha256:: Hash :: from_engine ( sha256:: HashEngine :: default ( ) ) ;
47
+ let id = sha256:: Hash :: all_zeros ( ) ;
49
48
let ( node_id, paths) = match destination {
50
49
Destination :: NodeId ( node_id) => ( Some ( node_id) , None ) ,
51
50
Destination :: Path ( path) => ( None , Some ( vec ! [ path] ) ) ,
@@ -93,6 +92,12 @@ impl OfferBuilder {
93
92
self
94
93
}
95
94
95
+ ///
96
+ pub fn path ( mut self , path : BlindedPath ) -> Self {
97
+ self . offer . paths . get_or_insert_with ( Vec :: new) . push ( path) ;
98
+ self
99
+ }
100
+
96
101
///
97
102
pub fn quantity_fixed ( mut self , quantity : NonZeroU64 ) -> Self {
98
103
let quantity = Some ( quantity. get ( ) ) . filter ( |quantity| * quantity != 1 ) ;
@@ -372,7 +377,7 @@ type Empty = ();
372
377
373
378
#[ cfg( test) ]
374
379
mod tests {
375
- use super :: { Amount , Destination , OfferBuilder , SendInvoice , merkle} ;
380
+ use super :: { Amount , BlindedPath , Destination , OfferBuilder , OnionMessagePath , SendInvoice , merkle} ;
376
381
377
382
use bitcoin:: blockdata:: constants:: genesis_block;
378
383
use bitcoin:: network:: constants:: Network ;
@@ -391,6 +396,15 @@ mod tests {
391
396
SecretKey :: from_slice ( & [ 45 ; 32 ] ) . unwrap ( )
392
397
}
393
398
399
+ fn blinded_pubkey ( byte : u8 ) -> PublicKey {
400
+ let secp_ctx = Secp256k1 :: new ( ) ;
401
+ PublicKey :: from_secret_key ( & secp_ctx, & blinded_privkey ( byte) )
402
+ }
403
+
404
+ fn blinded_privkey ( byte : u8 ) -> SecretKey {
405
+ SecretKey :: from_slice ( & [ byte; 32 ] ) . unwrap ( )
406
+ }
407
+
394
408
#[ test]
395
409
fn builds_offer_with_defaults ( ) {
396
410
let offer = OfferBuilder :: new ( "foo" . into ( ) , Destination :: NodeId ( pubkey ( ) ) ) . build ( ) ;
@@ -403,6 +417,7 @@ mod tests {
403
417
assert_eq ! ( offer. features( ) , None ) ;
404
418
assert_eq ! ( offer. absolute_expiry( ) , None ) ;
405
419
assert ! ( !offer. is_expired( ) ) ;
420
+ assert_eq ! ( offer. paths( ) , None ) ;
406
421
assert_eq ! ( offer. issuer( ) , None ) ;
407
422
assert_eq ! ( offer. quantity_min( ) , 1 ) ;
408
423
assert_eq ! ( offer. quantity_max( ) , 1 ) ;
@@ -538,6 +553,55 @@ mod tests {
538
553
assert_eq ! ( offer. as_tlv_stream( ) . absolute_expiry, Some ( past_expiry. as_secs( ) . into( ) ) ) ;
539
554
}
540
555
556
+ #[ test]
557
+ fn builds_offer_with_paths ( ) {
558
+ // TODO: Use more realistic data
559
+ let paths = vec ! [
560
+ BlindedPath {
561
+ blinding: pubkey( ) ,
562
+ path: vec![
563
+ OnionMessagePath {
564
+ node_id: blinded_pubkey( 43 ) , encrypted_recipient_data: vec![ 0 ; 43 ] ,
565
+ } ,
566
+ OnionMessagePath {
567
+ node_id: blinded_pubkey( 44 ) , encrypted_recipient_data: vec![ 0 ; 44 ] ,
568
+ } ,
569
+ ] . into( ) ,
570
+ } ,
571
+ BlindedPath {
572
+ blinding: pubkey( ) ,
573
+ path: vec![
574
+ OnionMessagePath {
575
+ node_id: blinded_pubkey( 45 ) , encrypted_recipient_data: vec![ 0 ; 45 ] ,
576
+ } ,
577
+ OnionMessagePath {
578
+ node_id: blinded_pubkey( 46 ) , encrypted_recipient_data: vec![ 0 ; 46 ] ,
579
+ } ,
580
+ ] . into( ) ,
581
+ } ,
582
+ ] ;
583
+
584
+ let offer = OfferBuilder :: new ( "foo" . into ( ) , Destination :: NodeId ( pubkey ( ) ) )
585
+ . path ( paths[ 0 ] . clone ( ) )
586
+ . path ( paths[ 1 ] . clone ( ) )
587
+ . build ( ) ;
588
+ let tlv_stream = offer. as_tlv_stream ( ) ;
589
+ assert_eq ! ( offer. paths( ) , Some ( & paths) ) ;
590
+ assert_eq ! ( offer. node_id( ) , pubkey( ) ) ;
591
+ assert_ne ! ( pubkey( ) , blinded_pubkey( 44 ) ) ;
592
+ assert_eq ! ( tlv_stream. paths, Some ( ( & paths) . into( ) ) ) ;
593
+ assert_eq ! ( tlv_stream. node_id, Some ( & pubkey( ) ) ) ;
594
+
595
+ let offer = OfferBuilder :: new ( "foo" . into ( ) , Destination :: Path ( paths[ 0 ] . clone ( ) ) )
596
+ . path ( paths[ 1 ] . clone ( ) )
597
+ . build ( ) ;
598
+ let tlv_stream = offer. as_tlv_stream ( ) ;
599
+ assert_eq ! ( offer. paths( ) , Some ( & paths) ) ;
600
+ assert_eq ! ( offer. node_id( ) , blinded_pubkey( 44 ) ) ;
601
+ assert_eq ! ( tlv_stream. paths, Some ( ( & paths) . into( ) ) ) ;
602
+ assert_eq ! ( tlv_stream. node_id, None ) ;
603
+ }
604
+
541
605
#[ test]
542
606
fn builds_offer_with_issuer ( ) {
543
607
let offer = OfferBuilder :: new ( "foo" . into ( ) , Destination :: NodeId ( pubkey ( ) ) )
0 commit comments