Common offers test_utils module
[rust-lightning] / lightning / src / offers / test_utils.rs
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 }