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