Skip to content

Commit 1d1e13c

Browse files
committed
f - Add builds_offer_with_paths test
1 parent 8e8dde9 commit 1d1e13c

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

lightning/src/offers/offer.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub enum Destination {
4444
impl OfferBuilder {
4545
///
4646
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();
4948
let (node_id, paths) = match destination {
5049
Destination::NodeId(node_id) => (Some(node_id), None),
5150
Destination::Path(path) => (None, Some(vec![path])),
@@ -93,6 +92,12 @@ impl OfferBuilder {
9392
self
9493
}
9594

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+
96101
///
97102
pub fn quantity_fixed(mut self, quantity: NonZeroU64) -> Self {
98103
let quantity = Some(quantity.get()).filter(|quantity| *quantity != 1);
@@ -372,7 +377,7 @@ type Empty = ();
372377

373378
#[cfg(test)]
374379
mod tests {
375-
use super::{Amount, Destination, OfferBuilder, SendInvoice, merkle};
380+
use super::{Amount, BlindedPath, Destination, OfferBuilder, OnionMessagePath, SendInvoice, merkle};
376381

377382
use bitcoin::blockdata::constants::genesis_block;
378383
use bitcoin::network::constants::Network;
@@ -391,6 +396,15 @@ mod tests {
391396
SecretKey::from_slice(&[45; 32]).unwrap()
392397
}
393398

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+
394408
#[test]
395409
fn builds_offer_with_defaults() {
396410
let offer = OfferBuilder::new("foo".into(), Destination::NodeId(pubkey())).build();
@@ -403,6 +417,7 @@ mod tests {
403417
assert_eq!(offer.features(), None);
404418
assert_eq!(offer.absolute_expiry(), None);
405419
assert!(!offer.is_expired());
420+
assert_eq!(offer.paths(), None);
406421
assert_eq!(offer.issuer(), None);
407422
assert_eq!(offer.quantity_min(), 1);
408423
assert_eq!(offer.quantity_max(), 1);
@@ -538,6 +553,55 @@ mod tests {
538553
assert_eq!(offer.as_tlv_stream().absolute_expiry, Some(past_expiry.as_secs().into()));
539554
}
540555

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+
541605
#[test]
542606
fn builds_offer_with_issuer() {
543607
let offer = OfferBuilder::new("foo".into(), Destination::NodeId(pubkey()))

lightning/src/util/ser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ impl<T: Readable> Readable for WithLength<Vec<T>, u8> {
562562
Ok(Self(result, core::marker::PhantomData))
563563
}
564564
}
565+
impl<T, U> From<Vec<T>> for WithLength<Vec<T>, U> {
566+
fn from(v: Vec<T>) -> Self { Self(v, core::marker::PhantomData) }
567+
}
565568

566569
/// For variable-length values within TLV record where the length is encoded as part of the record.
567570
/// Used to prevent encoding the length twice.

0 commit comments

Comments
 (0)