230c6aa1628d96a180276d228d5742369062886f
[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::blinded_path::{BlindedHop, BlindedPath};
17 use crate::sign::EntropySource;
18 use crate::ln::PaymentHash;
19 use crate::ln::features::BlindedHopFeatures;
20 use crate::offers::invoice::BlindedPayInfo;
21
22 pub(super) fn payer_keys() -> KeyPair {
23         let secp_ctx = Secp256k1::new();
24         KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
25 }
26
27 pub(super) fn payer_sign(digest: &Message) -> Result<Signature, Infallible> {
28         let secp_ctx = Secp256k1::new();
29         let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
30         Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
31 }
32
33 pub(super) fn payer_pubkey() -> PublicKey {
34         payer_keys().public_key()
35 }
36
37 pub(super) fn recipient_keys() -> KeyPair {
38         let secp_ctx = Secp256k1::new();
39         KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap())
40 }
41
42 pub(super) fn recipient_sign(digest: &Message) -> Result<Signature, Infallible> {
43         let secp_ctx = Secp256k1::new();
44         let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());
45         Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys))
46 }
47
48 pub(super) fn recipient_pubkey() -> PublicKey {
49         recipient_keys().public_key()
50 }
51
52 pub(super) fn pubkey(byte: u8) -> PublicKey {
53         let secp_ctx = Secp256k1::new();
54         PublicKey::from_secret_key(&secp_ctx, &privkey(byte))
55 }
56
57 pub(super) fn privkey(byte: u8) -> SecretKey {
58         SecretKey::from_slice(&[byte; 32]).unwrap()
59 }
60
61 pub(super) fn payment_paths() -> Vec<(BlindedPayInfo, BlindedPath)> {
62         let paths = vec![
63                 BlindedPath {
64                         introduction_node_id: pubkey(40),
65                         blinding_point: pubkey(41),
66                         blinded_hops: vec![
67                                 BlindedHop { blinded_node_id: pubkey(43), encrypted_payload: vec![0; 43] },
68                                 BlindedHop { blinded_node_id: pubkey(44), encrypted_payload: vec![0; 44] },
69                         ],
70                 },
71                 BlindedPath {
72                         introduction_node_id: pubkey(40),
73                         blinding_point: pubkey(41),
74                         blinded_hops: vec![
75                                 BlindedHop { blinded_node_id: pubkey(45), encrypted_payload: vec![0; 45] },
76                                 BlindedHop { blinded_node_id: pubkey(46), encrypted_payload: vec![0; 46] },
77                         ],
78                 },
79         ];
80
81         let payinfo = vec![
82                 BlindedPayInfo {
83                         fee_base_msat: 1,
84                         fee_proportional_millionths: 1_000,
85                         cltv_expiry_delta: 42,
86                         htlc_minimum_msat: 100,
87                         htlc_maximum_msat: 1_000_000_000_000,
88                         features: BlindedHopFeatures::empty(),
89                 },
90                 BlindedPayInfo {
91                         fee_base_msat: 1,
92                         fee_proportional_millionths: 1_000,
93                         cltv_expiry_delta: 42,
94                         htlc_minimum_msat: 100,
95                         htlc_maximum_msat: 1_000_000_000_000,
96                         features: BlindedHopFeatures::empty(),
97                 },
98         ];
99
100         payinfo.into_iter().zip(paths.into_iter()).collect()
101 }
102
103 pub(super) fn payment_hash() -> PaymentHash {
104         PaymentHash([42; 32])
105 }
106
107 pub(super) fn now() -> Duration {
108         std::time::SystemTime::now()
109                 .duration_since(std::time::SystemTime::UNIX_EPOCH)
110                 .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH")
111 }
112
113 pub(super) struct FixedEntropy;
114
115 impl EntropySource for FixedEntropy {
116         fn get_secure_random_bytes(&self) -> [u8; 32] {
117                 [42; 32]
118         }
119 }