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