Skip to content

Commit ce7a02d

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 a332bfc commit ce7a02d

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
@@ -777,68 +777,27 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
777777

778778
#[cfg(test)]
779779
mod tests {
780-
use super::{DEFAULT_RELATIVE_EXPIRY, BlindedPayInfo, FallbackAddress, FullInvoiceTlvStreamRef, Invoice, InvoiceTlvStreamRef, SIGNATURE_TAG};
780+
use super::{DEFAULT_RELATIVE_EXPIRY, FallbackAddress, FullInvoiceTlvStreamRef, Invoice, InvoiceTlvStreamRef, SIGNATURE_TAG};
781781

782782
use bitcoin::blockdata::script::Script;
783783
use bitcoin::hashes::Hash;
784784
use bitcoin::network::constants::Network;
785-
use bitcoin::secp256k1::{KeyPair, Message, PublicKey, Secp256k1, SecretKey, XOnlyPublicKey, self};
786-
use bitcoin::secp256k1::schnorr::Signature;
785+
use bitcoin::secp256k1::{Message, Secp256k1, XOnlyPublicKey, self};
787786
use bitcoin::util::address::{Address, Payload, WitnessVersion};
788787
use bitcoin::util::schnorr::TweakedPublicKey;
789-
use core::convert::{Infallible, TryFrom};
788+
use core::convert::TryFrom;
790789
use core::time::Duration;
791-
use crate::ln::PaymentHash;
792790
use crate::ln::msgs::DecodeError;
793-
use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
791+
use crate::ln::features::Bolt12InvoiceFeatures;
794792
use crate::offers::invoice_request::InvoiceRequestTlvStreamRef;
795793
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self};
796794
use crate::offers::offer::{OfferBuilder, OfferTlvStreamRef, Quantity};
797795
use crate::offers::parse::{ParseError, SemanticError};
798796
use crate::offers::payer::PayerTlvStreamRef;
799797
use crate::offers::refund::RefundBuilder;
800-
use crate::onion_message::{BlindedHop, BlindedPath};
798+
use crate::offers::test_utils::*;
801799
use crate::util::ser::{BigSize, Iterable, Writeable};
802800

803-
fn payer_keys() -> KeyPair {
804-
let secp_ctx = Secp256k1::new();
805-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
806-
}
807-
808-
fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
809-
let secp_ctx = Secp256k1::new();
810-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
811-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
812-
}
813-
814-
fn payer_pubkey() -> PublicKey {
815-
payer_keys().public_key()
816-
}
817-
818-
fn recipient_keys() -> KeyPair {
819-
let secp_ctx = Secp256k1::new();
820-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
821-
}
822-
823-
fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
824-
let secp_ctx = Secp256k1::new();
825-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
826-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
827-
}
828-
829-
fn recipient_pubkey() -> PublicKey {
830-
recipient_keys().public_key()
831-
}
832-
833-
fn pubkey(byte: u8) -> PublicKey {
834-
let secp_ctx = Secp256k1::new();
835-
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
836-
}
837-
838-
fn privkey(byte: u8) -> SecretKey {
839-
SecretKey::from_slice(&[byte; 32]).unwrap()
840-
}
841-
842801
trait ToBytes {
843802
fn to_bytes(&self) -> Vec<u8>;
844803
}
@@ -855,58 +814,6 @@ mod tests {
855814
}
856815
}
857816

858-
fn payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> {
859-
let paths = vec![
860-
BlindedPath {
861-
introduction_node_id: pubkey(40),
862-
blinding_point: pubkey(41),
863-
blinded_hops: vec![
864-
BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
865-
BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
866-
],
867-
},
868-
BlindedPath {
869-
introduction_node_id: pubkey(40),
870-
blinding_point: pubkey(41),
871-
blinded_hops: vec![
872-
BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
873-
BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
874-
],
875-
},
876-
];
877-
878-
let payinfo = vec![
879-
BlindedPayInfo {
880-
fee_base_msat: 1,
881-
fee_proportional_millionths: 1_000,
882-
cltv_expiry_delta: 42,
883-
htlc_minimum_msat: 100,
884-
htlc_maximum_msat: 1_000_000_000_000,
885-
features: BlindedHopFeatures::empty(),
886-
},
887-
BlindedPayInfo {
888-
fee_base_msat: 1,
889-
fee_proportional_millionths: 1_000,
890-
cltv_expiry_delta: 42,
891-
htlc_minimum_msat: 100,
892-
htlc_maximum_msat: 1_000_000_000_000,
893-
features: BlindedHopFeatures::empty(),
894-
},
895-
];
896-
897-
paths.into_iter().zip(payinfo.into_iter()).collect()
898-
}
899-
900-
fn payment_hash() -> PaymentHash {
901-
PaymentHash([42; 32])
902-
}
903-
904-
fn now() -> Duration {
905-
std::time::SystemTime::now()
906-
.duration_since(std::time::SystemTime::UNIX_EPOCH)
907-
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH")
908-
}
909-
910817
#[test]
911818
fn builds_invoice_for_offer_with_defaults() {
912819
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
@@ -666,26 +666,17 @@ mod tests {
666666

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

680-
fn pubkey(byte: u8) -> PublicKey {
681-
let secp_ctx = Secp256k1::new();
682-
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
683-
}
684-
685-
fn privkey(byte: u8) -> SecretKey {
686-
SecretKey::from_slice(&[byte; 32]).unwrap()
687-
}
688-
689680
#[test]
690681
fn builds_offer_with_defaults() {
691682
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)