Skip to content

Commit aadd9af

Browse files
committed
Common offers test_utils module
Move utility functions used across all offers modules into a common module. Avoids duplicating larger utilities such as payment_path across more than one module.
1 parent 2c566c6 commit aadd9af

File tree

6 files changed

+122
-151
lines changed

6 files changed

+122
-151
lines changed

lightning/src/offers/invoice.rs

Lines changed: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -766,68 +766,27 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
766766

767767
#[cfg(test)]
768768
mod tests {
769-
use super::{DEFAULT_RELATIVE_EXPIRY, BlindedPayInfo, FallbackAddress, FullInvoiceTlvStreamRef, Invoice, InvoiceTlvStreamRef, SIGNATURE_TAG};
769+
use super::{DEFAULT_RELATIVE_EXPIRY, FallbackAddress, FullInvoiceTlvStreamRef, Invoice, InvoiceTlvStreamRef, SIGNATURE_TAG};
770770

771771
use bitcoin::blockdata::script::Script;
772772
use bitcoin::hashes::Hash;
773773
use bitcoin::network::constants::Network;
774-
use bitcoin::secp256k1::{KeyPair, Message, PublicKey, Secp256k1, SecretKey, XOnlyPublicKey, self};
775-
use bitcoin::secp256k1::schnorr::Signature;
774+
use bitcoin::secp256k1::{XOnlyPublicKey, self};
776775
use bitcoin::util::address::{Address, Payload, WitnessVersion};
777776
use bitcoin::util::schnorr::TweakedPublicKey;
778-
use core::convert::{Infallible, TryFrom};
777+
use core::convert::TryFrom;
779778
use core::time::Duration;
780-
use crate::ln::PaymentHash;
781779
use crate::ln::msgs::DecodeError;
782-
use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
780+
use crate::ln::features::Bolt12InvoiceFeatures;
783781
use crate::offers::invoice_request::InvoiceRequestTlvStreamRef;
784782
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self};
785783
use crate::offers::offer::{OfferBuilder, OfferTlvStreamRef};
786784
use crate::offers::parse::{ParseError, SemanticError};
787785
use crate::offers::payer::PayerTlvStreamRef;
788786
use crate::offers::refund::RefundBuilder;
789-
use crate::onion_message::{BlindedHop, BlindedPath};
787+
use crate::offers::test_utils::*;
790788
use crate::util::ser::{BigSize, Iterable, Writeable};
791789

792-
fn payer_keys() -> KeyPair {
793-
let secp_ctx = Secp256k1::new();
794-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
795-
}
796-
797-
fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
798-
let secp_ctx = Secp256k1::new();
799-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
800-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
801-
}
802-
803-
fn payer_pubkey() -> PublicKey {
804-
payer_keys().public_key()
805-
}
806-
807-
fn recipient_keys() -> KeyPair {
808-
let secp_ctx = Secp256k1::new();
809-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
810-
}
811-
812-
fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
813-
let secp_ctx = Secp256k1::new();
814-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
815-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
816-
}
817-
818-
fn recipient_pubkey() -> PublicKey {
819-
recipient_keys().public_key()
820-
}
821-
822-
fn pubkey(byte: u8) -> PublicKey {
823-
let secp_ctx = Secp256k1::new();
824-
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
825-
}
826-
827-
fn privkey(byte: u8) -> SecretKey {
828-
SecretKey::from_slice(&[byte; 32]).unwrap()
829-
}
830-
831790
trait ToBytes {
832791
fn to_bytes(&self) -> Vec<u8>;
833792
}
@@ -844,58 +803,6 @@ mod tests {
844803
}
845804
}
846805

847-
fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
848-
let paths = vec![
849-
BlindedPath {
850-
introduction_node_id: pubkey(40),
851-
blinding_point: pubkey(41),
852-
blinded_hops: vec![
853-
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
854-
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
855-
],
856-
},
857-
BlindedPath {
858-
introduction_node_id: pubkey(40),
859-
blinding_point: pubkey(41),
860-
blinded_hops: vec![
861-
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
862-
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
863-
],
864-
},
865-
];
866-
867-
let payinfo = vec![
868-
BlindedPayInfo {
869-
fee_base_msat: 1,
870-
fee_proportional_millionths: 1_000,
871-
cltv_expiry_delta: 42,
872-
htlc_minimum_msat: 100,
873-
htlc_maximum_msat: 1_000_000_000_000,
874-
features: BlindedHopFeatures::empty(),
875-
},
876-
BlindedPayInfo {
877-
fee_base_msat: 1,
878-
fee_proportional_millionths: 1_000,
879-
cltv_expiry_delta: 42,
880-
htlc_minimum_msat: 100,
881-
htlc_maximum_msat: 1_000_000_000_000,
882-
features: BlindedHopFeatures::empty(),
883-
},
884-
];
885-
886-
paths.into_iter().zip(payinfo.into_iter()).collect()
887-
}
888-
889-
fn payment_hash() -> PaymentHash {
890-
PaymentHash([42; 32])
891-
}
892-
893-
fn now() -> Duration {
894-
std::time::SystemTime::now()
895-
.duration_since(std::time::SystemTime::UNIX_EPOCH)
896-
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH")
897-
}
898-
899806
#[test]
900807
fn builds_invoice_for_offer_with_defaults() {
901808
let payment_paths = payment_paths();

lightning/src/offers/invoice_request.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,7 @@ mod tests {
532532

533533
use bitcoin::blockdata::constants::ChainHash;
534534
use bitcoin::network::constants::Network;
535-
use bitcoin::secp256k1::{KeyPair, Message, PublicKey, Secp256k1, SecretKey, self};
536-
use bitcoin::secp256k1::schnorr::Signature;
535+
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey, self};
537536
use core::convert::{Infallible, TryFrom};
538537
use core::num::NonZeroU64;
539538
#[cfg(feature = "std")]
@@ -544,35 +543,10 @@ mod tests {
544543
use crate::offers::offer::{Amount, OfferBuilder, OfferTlvStreamRef, Quantity};
545544
use crate::offers::parse::{ParseError, SemanticError};
546545
use crate::offers::payer::PayerTlvStreamRef;
546+
use crate::offers::test_utils::*;
547547
use crate::util::ser::{BigSize, Writeable};
548548
use crate::util::string::PrintableString;
549549

550-
fn payer_keys() -> KeyPair {
551-
let secp_ctx = Secp256k1::new();
552-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
553-
}
554-
555-
fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
556-
let secp_ctx = Secp256k1::new();
557-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
558-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
559-
}
560-
561-
fn payer_pubkey() -> PublicKey {
562-
payer_keys().public_key()
563-
}
564-
565-
fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
566-
let secp_ctx = Secp256k1::new();
567-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
568-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
569-
}
570-
571-
fn recipient_pubkey() -> PublicKey {
572-
let secp_ctx = Secp256k1::new();
573-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap()).public_key()
574-
}
575-
576550
#[test]
577551
fn builds_invoice_request_with_defaults() {
578552
let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey())

lightning/src/offers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ pub mod offer;
1919
pub mod parse;
2020
mod payer;
2121
pub mod refund;
22+
#[cfg(test)]
23+
mod test_utils;

lightning/src/offers/offer.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -665,26 +665,17 @@ mod tests {
665665

666666
use bitcoin::blockdata::constants::ChainHash;
667667
use bitcoin::network::constants::Network;
668-
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
669668
use core::convert::TryFrom;
670669
use core::num::NonZeroU64;
671670
use core::time::Duration;
672671
use crate::ln::features::OfferFeatures;
673672
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
674673
use crate::offers::parse::{ParseError, SemanticError};
674+
use crate::offers::test_utils::*;
675675
use crate::onion_message::{BlindedHop, BlindedPath};
676676
use crate::util::ser::{BigSize, Writeable};
677677
use crate::util::string::PrintableString;
678678

679-
fn pubkey(byte: u8) -> PublicKey {
680-
let secp_ctx = Secp256k1::new();
681-
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
682-
}
683-
684-
fn privkey(byte: u8) -> SecretKey {
685-
SecretKey::from_slice(&[byte; 32]).unwrap()
686-
}
687-
688679
#[test]
689680
fn builds_offer_with_defaults() {
690681
let offer = OfferBuilder::new("foo".into(), pubkey(42)).build().unwrap();

lightning/src/offers/refund.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ mod tests {
575575

576576
use bitcoin::blockdata::constants::ChainHash;
577577
use bitcoin::network::constants::Network;
578-
use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
578+
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
579579
use core::convert::TryFrom;
580580
use core::time::Duration;
581581
use crate::ln::features::{InvoiceRequestFeatures, OfferFeatures};
@@ -584,24 +584,11 @@ mod tests {
584584
use crate::offers::offer::OfferTlvStreamRef;
585585
use crate::offers::parse::{ParseError, SemanticError};
586586
use crate::offers::payer::PayerTlvStreamRef;
587+
use crate::offers::test_utils::*;
587588
use crate::onion_message::{BlindedHop, BlindedPath};
588589
use crate::util::ser::{BigSize, Writeable};
589590
use crate::util::string::PrintableString;
590591

591-
fn payer_pubkey() -> PublicKey {
592-
let secp_ctx = Secp256k1::new();
593-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap()).public_key()
594-
}
595-
596-
fn pubkey(byte: u8) -> PublicKey {
597-
let secp_ctx = Secp256k1::new();
598-
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
599-
}
600-
601-
fn privkey(byte: u8) -> SecretKey {
602-
SecretKey::from_slice(&[byte; 32]).unwrap()
603-
}
604-
605592
trait ToBytes {
606593
fn to_bytes(&self) -> Vec<u8>;
607594
}

lightning/src/offers/test_utils.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Utilities for testing BOLT 12 Offers interfaces
11+
12+
use bitcoin::secp256k1::{KeyPair, Message, PublicKey, Secp256k1, SecretKey};
13+
use bitcoin::secp256k1::schnorr::Signature;
14+
use core::convert::Infallible;
15+
use core::time::Duration;
16+
use crate::ln::PaymentHash;
17+
use crate::ln::features::BlindedHopFeatures;
18+
use crate::offers::invoice::BlindedPayInfo;
19+
use crate::onion_message::{BlindedHop, BlindedPath};
20+
21+
pub(super) fn payer_keys() -> KeyPair {
22+
let secp_ctx = Secp256k1::new();
23+
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
24+
}
25+
26+
pub(super) fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
27+
let secp_ctx = Secp256k1::new();
28+
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
29+
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
30+
}
31+
32+
pub(super) fn payer_pubkey() -> PublicKey {
33+
payer_keys().public_key()
34+
}
35+
36+
pub(super) fn recipient_keys() -> KeyPair {
37+
let secp_ctx = Secp256k1::new();
38+
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
39+
}
40+
41+
pub(super) fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
42+
let secp_ctx = Secp256k1::new();
43+
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
44+
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
45+
}
46+
47+
pub(super) fn recipient_pubkey() -> PublicKey {
48+
recipient_keys().public_key()
49+
}
50+
51+
pub(super) fn pubkey(byte: u8) -> PublicKey {
52+
let secp_ctx = Secp256k1::new();
53+
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
54+
}
55+
56+
pub(super) fn privkey(byte: u8) -> SecretKey {
57+
SecretKey::from_slice(&[byte; 32]).unwrap()
58+
}
59+
60+
pub(super) fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
61+
let paths = vec![
62+
BlindedPath {
63+
introduction_node_id: pubkey(40),
64+
blinding_point: pubkey(41),
65+
blinded_hops: vec![
66+
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
67+
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
68+
],
69+
},
70+
BlindedPath {
71+
introduction_node_id: pubkey(40),
72+
blinding_point: pubkey(41),
73+
blinded_hops: vec![
74+
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
75+
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
76+
],
77+
},
78+
];
79+
80+
let payinfo = vec![
81+
BlindedPayInfo {
82+
fee_base_msat: 1,
83+
fee_proportional_millionths: 1_000,
84+
cltv_expiry_delta: 42,
85+
htlc_minimum_msat: 100,
86+
htlc_maximum_msat: 1_000_000_000_000,
87+
features: BlindedHopFeatures::empty(),
88+
},
89+
BlindedPayInfo {
90+
fee_base_msat: 1,
91+
fee_proportional_millionths: 1_000,
92+
cltv_expiry_delta: 42,
93+
htlc_minimum_msat: 100,
94+
htlc_maximum_msat: 1_000_000_000_000,
95+
features: BlindedHopFeatures::empty(),
96+
},
97+
];
98+
99+
paths.into_iter().zip(payinfo.into_iter()).collect()
100+
}
101+
102+
pub(super) fn payment_hash() -> PaymentHash {
103+
PaymentHash([42; 32])
104+
}
105+
106+
pub(super) fn now() -> Duration {
107+
std::time::SystemTime::now()
108+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
109+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH")
110+
}

0 commit comments

Comments
 (0)