Skip to content

Commit 5d906ac

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 f114515 commit 5d906ac

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

773773
#[cfg(test)]
774774
mod tests {
775-
use super::{DEFAULT_RELATIVE_EXPIRY, BlindedPayInfo, FallbackAddress, FullInvoiceTlvStreamRef, Invoice, InvoiceTlvStreamRef, SIGNATURE_TAG};
775+
use super::{DEFAULT_RELATIVE_EXPIRY, FallbackAddress, FullInvoiceTlvStreamRef, Invoice, InvoiceTlvStreamRef, SIGNATURE_TAG};
776776

777777
use bitcoin::blockdata::script::Script;
778778
use bitcoin::hashes::Hash;
779779
use bitcoin::network::constants::Network;
780-
use bitcoin::secp256k1::{KeyPair, Message, PublicKey, Secp256k1, SecretKey, XOnlyPublicKey, self};
781-
use bitcoin::secp256k1::schnorr::Signature;
780+
use bitcoin::secp256k1::{XOnlyPublicKey, self};
782781
use bitcoin::util::address::{Address, Payload, WitnessVersion};
783782
use bitcoin::util::schnorr::TweakedPublicKey;
784-
use core::convert::{Infallible, TryFrom};
783+
use core::convert::TryFrom;
785784
use core::time::Duration;
786-
use crate::ln::PaymentHash;
787785
use crate::ln::msgs::DecodeError;
788-
use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
786+
use crate::ln::features::Bolt12InvoiceFeatures;
789787
use crate::offers::invoice_request::InvoiceRequestTlvStreamRef;
790788
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self};
791789
use crate::offers::offer::{OfferBuilder, OfferTlvStreamRef, Quantity};
792790
use crate::offers::parse::{ParseError, SemanticError};
793791
use crate::offers::payer::PayerTlvStreamRef;
794792
use crate::offers::refund::RefundBuilder;
795-
use crate::onion_message::{BlindedHop, BlindedPath};
793+
use crate::offers::test_utils::*;
796794
use crate::util::ser::{BigSize, Iterable, Writeable};
797795

798-
fn payer_keys() -> KeyPair {
799-
let secp_ctx = Secp256k1::new();
800-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
801-
}
802-
803-
fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
804-
let secp_ctx = Secp256k1::new();
805-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
806-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
807-
}
808-
809-
fn payer_pubkey() -> PublicKey {
810-
payer_keys().public_key()
811-
}
812-
813-
fn recipient_keys() -> KeyPair {
814-
let secp_ctx = Secp256k1::new();
815-
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
816-
}
817-
818-
fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
819-
let secp_ctx = Secp256k1::new();
820-
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
821-
Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
822-
}
823-
824-
fn recipient_pubkey() -> PublicKey {
825-
recipient_keys().public_key()
826-
}
827-
828-
fn pubkey(byte: u8) -> PublicKey {
829-
let secp_ctx = Secp256k1::new();
830-
PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
831-
}
832-
833-
fn privkey(byte: u8) -> SecretKey {
834-
SecretKey::from_slice(&[byte; 32]).unwrap()
835-
}
836-
837796
trait ToBytes {
838797
fn to_bytes(&self) -> Vec<u8>;
839798
}
@@ -850,58 +809,6 @@ mod tests {
850809
}
851810
}
852811

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